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 <net/xfrm.h> 25 #include <asm/ioctls.h> 26 #include "protocol.h" 27 #include "mib.h" 28 29 #define CREATE_TRACE_POINTS 30 #include <trace/events/mptcp.h> 31 32 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 33 struct mptcp6_sock { 34 struct mptcp_sock msk; 35 struct ipv6_pinfo np; 36 }; 37 #endif 38 39 enum { 40 MPTCP_CMSG_TS = BIT(0), 41 MPTCP_CMSG_INQ = BIT(1), 42 }; 43 44 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp; 45 46 static void __mptcp_destroy_sock(struct sock *sk); 47 static void mptcp_check_send_data_fin(struct sock *sk); 48 49 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions); 50 static struct net_device mptcp_napi_dev; 51 52 /* Returns end sequence number of the receiver's advertised window */ 53 static u64 mptcp_wnd_end(const struct mptcp_sock *msk) 54 { 55 return READ_ONCE(msk->wnd_end); 56 } 57 58 static bool mptcp_is_tcpsk(struct sock *sk) 59 { 60 struct socket *sock = sk->sk_socket; 61 62 if (unlikely(sk->sk_prot == &tcp_prot)) { 63 /* we are being invoked after mptcp_accept() has 64 * accepted a non-mp-capable flow: sk is a tcp_sk, 65 * not an mptcp one. 66 * 67 * Hand the socket over to tcp so all further socket ops 68 * bypass mptcp. 69 */ 70 sock->ops = &inet_stream_ops; 71 return true; 72 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 73 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 74 sock->ops = &inet6_stream_ops; 75 return true; 76 #endif 77 } 78 79 return false; 80 } 81 82 static int __mptcp_socket_create(struct mptcp_sock *msk) 83 { 84 struct mptcp_subflow_context *subflow; 85 struct sock *sk = (struct sock *)msk; 86 struct socket *ssock; 87 int err; 88 89 err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock); 90 if (err) 91 return err; 92 93 WRITE_ONCE(msk->first, ssock->sk); 94 WRITE_ONCE(msk->subflow, ssock); 95 subflow = mptcp_subflow_ctx(ssock->sk); 96 list_add(&subflow->node, &msk->conn_list); 97 sock_hold(ssock->sk); 98 subflow->request_mptcp = 1; 99 100 /* This is the first subflow, always with id 0 */ 101 subflow->local_id_valid = 1; 102 mptcp_sock_graft(msk->first, sk->sk_socket); 103 104 return 0; 105 } 106 107 /* If the MPC handshake is not started, returns the first subflow, 108 * eventually allocating it. 109 */ 110 struct socket *__mptcp_nmpc_socket(struct mptcp_sock *msk) 111 { 112 struct sock *sk = (struct sock *)msk; 113 int ret; 114 115 if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) 116 return ERR_PTR(-EINVAL); 117 118 if (!msk->subflow) { 119 if (msk->first) 120 return ERR_PTR(-EINVAL); 121 122 ret = __mptcp_socket_create(msk); 123 if (ret) 124 return ERR_PTR(ret); 125 126 mptcp_sockopt_sync(msk, msk->first); 127 } 128 129 return msk->subflow; 130 } 131 132 static void mptcp_drop(struct sock *sk, struct sk_buff *skb) 133 { 134 sk_drops_add(sk, skb); 135 __kfree_skb(skb); 136 } 137 138 static void mptcp_rmem_charge(struct sock *sk, int size) 139 { 140 mptcp_sk(sk)->rmem_fwd_alloc -= size; 141 } 142 143 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, 144 struct sk_buff *from) 145 { 146 bool fragstolen; 147 int delta; 148 149 if (MPTCP_SKB_CB(from)->offset || 150 !skb_try_coalesce(to, from, &fragstolen, &delta)) 151 return false; 152 153 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx", 154 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq, 155 to->len, MPTCP_SKB_CB(from)->end_seq); 156 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq; 157 158 /* note the fwd memory can reach a negative value after accounting 159 * for the delta, but the later skb free will restore a non 160 * negative one 161 */ 162 atomic_add(delta, &sk->sk_rmem_alloc); 163 mptcp_rmem_charge(sk, delta); 164 kfree_skb_partial(from, fragstolen); 165 166 return true; 167 } 168 169 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to, 170 struct sk_buff *from) 171 { 172 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq) 173 return false; 174 175 return mptcp_try_coalesce((struct sock *)msk, to, from); 176 } 177 178 static void __mptcp_rmem_reclaim(struct sock *sk, int amount) 179 { 180 amount >>= PAGE_SHIFT; 181 mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT; 182 __sk_mem_reduce_allocated(sk, amount); 183 } 184 185 static void mptcp_rmem_uncharge(struct sock *sk, int size) 186 { 187 struct mptcp_sock *msk = mptcp_sk(sk); 188 int reclaimable; 189 190 msk->rmem_fwd_alloc += size; 191 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk); 192 193 /* see sk_mem_uncharge() for the rationale behind the following schema */ 194 if (unlikely(reclaimable >= PAGE_SIZE)) 195 __mptcp_rmem_reclaim(sk, reclaimable); 196 } 197 198 static void mptcp_rfree(struct sk_buff *skb) 199 { 200 unsigned int len = skb->truesize; 201 struct sock *sk = skb->sk; 202 203 atomic_sub(len, &sk->sk_rmem_alloc); 204 mptcp_rmem_uncharge(sk, len); 205 } 206 207 void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk) 208 { 209 skb_orphan(skb); 210 skb->sk = sk; 211 skb->destructor = mptcp_rfree; 212 atomic_add(skb->truesize, &sk->sk_rmem_alloc); 213 mptcp_rmem_charge(sk, skb->truesize); 214 } 215 216 /* "inspired" by tcp_data_queue_ofo(), main differences: 217 * - use mptcp seqs 218 * - don't cope with sacks 219 */ 220 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb) 221 { 222 struct sock *sk = (struct sock *)msk; 223 struct rb_node **p, *parent; 224 u64 seq, end_seq, max_seq; 225 struct sk_buff *skb1; 226 227 seq = MPTCP_SKB_CB(skb)->map_seq; 228 end_seq = MPTCP_SKB_CB(skb)->end_seq; 229 max_seq = atomic64_read(&msk->rcv_wnd_sent); 230 231 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq, 232 RB_EMPTY_ROOT(&msk->out_of_order_queue)); 233 if (after64(end_seq, max_seq)) { 234 /* out of window */ 235 mptcp_drop(sk, skb); 236 pr_debug("oow by %lld, rcv_wnd_sent %llu\n", 237 (unsigned long long)end_seq - (unsigned long)max_seq, 238 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent)); 239 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW); 240 return; 241 } 242 243 p = &msk->out_of_order_queue.rb_node; 244 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE); 245 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) { 246 rb_link_node(&skb->rbnode, NULL, p); 247 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 248 msk->ooo_last_skb = skb; 249 goto end; 250 } 251 252 /* with 2 subflows, adding at end of ooo queue is quite likely 253 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. 254 */ 255 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) { 256 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 257 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 258 return; 259 } 260 261 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */ 262 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) { 263 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL); 264 parent = &msk->ooo_last_skb->rbnode; 265 p = &parent->rb_right; 266 goto insert; 267 } 268 269 /* Find place to insert this segment. Handle overlaps on the way. */ 270 parent = NULL; 271 while (*p) { 272 parent = *p; 273 skb1 = rb_to_skb(parent); 274 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 275 p = &parent->rb_left; 276 continue; 277 } 278 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) { 279 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) { 280 /* All the bits are present. Drop. */ 281 mptcp_drop(sk, skb); 282 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 283 return; 284 } 285 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) { 286 /* partial overlap: 287 * | skb | 288 * | skb1 | 289 * continue traversing 290 */ 291 } else { 292 /* skb's seq == skb1's seq and skb covers skb1. 293 * Replace skb1 with skb. 294 */ 295 rb_replace_node(&skb1->rbnode, &skb->rbnode, 296 &msk->out_of_order_queue); 297 mptcp_drop(sk, skb1); 298 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 299 goto merge_right; 300 } 301 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) { 302 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE); 303 return; 304 } 305 p = &parent->rb_right; 306 } 307 308 insert: 309 /* Insert segment into RB tree. */ 310 rb_link_node(&skb->rbnode, parent, p); 311 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue); 312 313 merge_right: 314 /* Remove other segments covered by skb. */ 315 while ((skb1 = skb_rb_next(skb)) != NULL) { 316 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) 317 break; 318 rb_erase(&skb1->rbnode, &msk->out_of_order_queue); 319 mptcp_drop(sk, skb1); 320 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 321 } 322 /* If there is no skb after us, we are the last_skb ! */ 323 if (!skb1) 324 msk->ooo_last_skb = skb; 325 326 end: 327 skb_condense(skb); 328 mptcp_set_owner_r(skb, sk); 329 } 330 331 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size) 332 { 333 struct mptcp_sock *msk = mptcp_sk(sk); 334 int amt, amount; 335 336 if (size <= msk->rmem_fwd_alloc) 337 return true; 338 339 size -= msk->rmem_fwd_alloc; 340 amt = sk_mem_pages(size); 341 amount = amt << PAGE_SHIFT; 342 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) 343 return false; 344 345 msk->rmem_fwd_alloc += amount; 346 return true; 347 } 348 349 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 350 struct sk_buff *skb, unsigned int offset, 351 size_t copy_len) 352 { 353 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 354 struct sock *sk = (struct sock *)msk; 355 struct sk_buff *tail; 356 bool has_rxtstamp; 357 358 __skb_unlink(skb, &ssk->sk_receive_queue); 359 360 skb_ext_reset(skb); 361 skb_orphan(skb); 362 363 /* try to fetch required memory from subflow */ 364 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize)) 365 goto drop; 366 367 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp; 368 369 /* the skb map_seq accounts for the skb offset: 370 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq 371 * value 372 */ 373 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow); 374 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len; 375 MPTCP_SKB_CB(skb)->offset = offset; 376 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp; 377 378 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) { 379 /* in sequence */ 380 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len); 381 tail = skb_peek_tail(&sk->sk_receive_queue); 382 if (tail && mptcp_try_coalesce(sk, tail, skb)) 383 return true; 384 385 mptcp_set_owner_r(skb, sk); 386 __skb_queue_tail(&sk->sk_receive_queue, skb); 387 return true; 388 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) { 389 mptcp_data_queue_ofo(msk, skb); 390 return false; 391 } 392 393 /* old data, keep it simple and drop the whole pkt, sender 394 * will retransmit as needed, if needed. 395 */ 396 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 397 drop: 398 mptcp_drop(sk, skb); 399 return false; 400 } 401 402 static void mptcp_stop_timer(struct sock *sk) 403 { 404 struct inet_connection_sock *icsk = inet_csk(sk); 405 406 sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 407 mptcp_sk(sk)->timer_ival = 0; 408 } 409 410 static void mptcp_close_wake_up(struct sock *sk) 411 { 412 if (sock_flag(sk, SOCK_DEAD)) 413 return; 414 415 sk->sk_state_change(sk); 416 if (sk->sk_shutdown == SHUTDOWN_MASK || 417 sk->sk_state == TCP_CLOSE) 418 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 419 else 420 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 421 } 422 423 static bool mptcp_pending_data_fin_ack(struct sock *sk) 424 { 425 struct mptcp_sock *msk = mptcp_sk(sk); 426 427 return ((1 << sk->sk_state) & 428 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) && 429 msk->write_seq == READ_ONCE(msk->snd_una); 430 } 431 432 static void mptcp_check_data_fin_ack(struct sock *sk) 433 { 434 struct mptcp_sock *msk = mptcp_sk(sk); 435 436 /* Look for an acknowledged DATA_FIN */ 437 if (mptcp_pending_data_fin_ack(sk)) { 438 WRITE_ONCE(msk->snd_data_fin_enable, 0); 439 440 switch (sk->sk_state) { 441 case TCP_FIN_WAIT1: 442 inet_sk_state_store(sk, TCP_FIN_WAIT2); 443 break; 444 case TCP_CLOSING: 445 case TCP_LAST_ACK: 446 inet_sk_state_store(sk, TCP_CLOSE); 447 break; 448 } 449 450 mptcp_close_wake_up(sk); 451 } 452 } 453 454 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) 455 { 456 struct mptcp_sock *msk = mptcp_sk(sk); 457 458 if (READ_ONCE(msk->rcv_data_fin) && 459 ((1 << sk->sk_state) & 460 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) { 461 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq); 462 463 if (msk->ack_seq == rcv_data_fin_seq) { 464 if (seq) 465 *seq = rcv_data_fin_seq; 466 467 return true; 468 } 469 } 470 471 return false; 472 } 473 474 static void mptcp_set_datafin_timeout(struct sock *sk) 475 { 476 struct inet_connection_sock *icsk = inet_csk(sk); 477 u32 retransmits; 478 479 retransmits = min_t(u32, icsk->icsk_retransmits, 480 ilog2(TCP_RTO_MAX / TCP_RTO_MIN)); 481 482 mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits; 483 } 484 485 static void __mptcp_set_timeout(struct sock *sk, long tout) 486 { 487 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 488 } 489 490 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow) 491 { 492 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 493 494 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ? 495 inet_csk(ssk)->icsk_timeout - jiffies : 0; 496 } 497 498 static void mptcp_set_timeout(struct sock *sk) 499 { 500 struct mptcp_subflow_context *subflow; 501 long tout = 0; 502 503 mptcp_for_each_subflow(mptcp_sk(sk), subflow) 504 tout = max(tout, mptcp_timeout_from_subflow(subflow)); 505 __mptcp_set_timeout(sk, tout); 506 } 507 508 static inline bool tcp_can_send_ack(const struct sock *ssk) 509 { 510 return !((1 << inet_sk_state_load(ssk)) & 511 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN)); 512 } 513 514 void __mptcp_subflow_send_ack(struct sock *ssk) 515 { 516 if (tcp_can_send_ack(ssk)) 517 tcp_send_ack(ssk); 518 } 519 520 static void mptcp_subflow_send_ack(struct sock *ssk) 521 { 522 bool slow; 523 524 slow = lock_sock_fast(ssk); 525 __mptcp_subflow_send_ack(ssk); 526 unlock_sock_fast(ssk, slow); 527 } 528 529 static void mptcp_send_ack(struct mptcp_sock *msk) 530 { 531 struct mptcp_subflow_context *subflow; 532 533 mptcp_for_each_subflow(msk, subflow) 534 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow)); 535 } 536 537 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 538 { 539 bool slow; 540 541 slow = lock_sock_fast(ssk); 542 if (tcp_can_send_ack(ssk)) 543 tcp_cleanup_rbuf(ssk, 1); 544 unlock_sock_fast(ssk, slow); 545 } 546 547 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 548 { 549 const struct inet_connection_sock *icsk = inet_csk(ssk); 550 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 551 const struct tcp_sock *tp = tcp_sk(ssk); 552 553 return (ack_pending & ICSK_ACK_SCHED) && 554 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 555 READ_ONCE(icsk->icsk_ack.rcv_mss)) || 556 (rx_empty && ack_pending & 557 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 558 } 559 560 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 561 { 562 int old_space = READ_ONCE(msk->old_wspace); 563 struct mptcp_subflow_context *subflow; 564 struct sock *sk = (struct sock *)msk; 565 int space = __mptcp_space(sk); 566 bool cleanup, rx_empty; 567 568 cleanup = (space > 0) && (space >= (old_space << 1)); 569 rx_empty = !__mptcp_rmem(sk); 570 571 mptcp_for_each_subflow(msk, subflow) { 572 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 573 574 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 575 mptcp_subflow_cleanup_rbuf(ssk); 576 } 577 } 578 579 static bool mptcp_check_data_fin(struct sock *sk) 580 { 581 struct mptcp_sock *msk = mptcp_sk(sk); 582 u64 rcv_data_fin_seq; 583 bool ret = false; 584 585 /* Need to ack a DATA_FIN received from a peer while this side 586 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 587 * msk->rcv_data_fin was set when parsing the incoming options 588 * at the subflow level and the msk lock was not held, so this 589 * is the first opportunity to act on the DATA_FIN and change 590 * the msk state. 591 * 592 * If we are caught up to the sequence number of the incoming 593 * DATA_FIN, send the DATA_ACK now and do state transition. If 594 * not caught up, do nothing and let the recv code send DATA_ACK 595 * when catching up. 596 */ 597 598 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 599 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 600 WRITE_ONCE(msk->rcv_data_fin, 0); 601 602 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN); 603 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 604 605 switch (sk->sk_state) { 606 case TCP_ESTABLISHED: 607 inet_sk_state_store(sk, TCP_CLOSE_WAIT); 608 break; 609 case TCP_FIN_WAIT1: 610 inet_sk_state_store(sk, TCP_CLOSING); 611 break; 612 case TCP_FIN_WAIT2: 613 inet_sk_state_store(sk, TCP_CLOSE); 614 break; 615 default: 616 /* Other states not expected */ 617 WARN_ON_ONCE(1); 618 break; 619 } 620 621 ret = true; 622 if (!__mptcp_check_fallback(msk)) 623 mptcp_send_ack(msk); 624 mptcp_close_wake_up(sk); 625 } 626 return ret; 627 } 628 629 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 630 struct sock *ssk, 631 unsigned int *bytes) 632 { 633 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 634 struct sock *sk = (struct sock *)msk; 635 unsigned int moved = 0; 636 bool more_data_avail; 637 struct tcp_sock *tp; 638 bool done = false; 639 int sk_rbuf; 640 641 sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 642 643 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 644 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 645 646 if (unlikely(ssk_rbuf > sk_rbuf)) { 647 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 648 sk_rbuf = ssk_rbuf; 649 } 650 } 651 652 pr_debug("msk=%p ssk=%p", msk, ssk); 653 tp = tcp_sk(ssk); 654 do { 655 u32 map_remaining, offset; 656 u32 seq = tp->copied_seq; 657 struct sk_buff *skb; 658 bool fin; 659 660 /* try to move as much data as available */ 661 map_remaining = subflow->map_data_len - 662 mptcp_subflow_get_map_offset(subflow); 663 664 skb = skb_peek(&ssk->sk_receive_queue); 665 if (!skb) { 666 /* With racing move_skbs_to_msk() and __mptcp_move_skbs(), 667 * a different CPU can have already processed the pending 668 * data, stop here or we can enter an infinite loop 669 */ 670 if (!moved) 671 done = true; 672 break; 673 } 674 675 if (__mptcp_check_fallback(msk)) { 676 /* Under fallback skbs have no MPTCP extension and TCP could 677 * collapse them between the dummy map creation and the 678 * current dequeue. Be sure to adjust the map size. 679 */ 680 map_remaining = skb->len; 681 subflow->map_data_len = skb->len; 682 } 683 684 offset = seq - TCP_SKB_CB(skb)->seq; 685 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 686 if (fin) { 687 done = true; 688 seq++; 689 } 690 691 if (offset < skb->len) { 692 size_t len = skb->len - offset; 693 694 if (tp->urg_data) 695 done = true; 696 697 if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 698 moved += len; 699 seq += len; 700 701 if (WARN_ON_ONCE(map_remaining < len)) 702 break; 703 } else { 704 WARN_ON_ONCE(!fin); 705 sk_eat_skb(ssk, skb); 706 done = true; 707 } 708 709 WRITE_ONCE(tp->copied_seq, seq); 710 more_data_avail = mptcp_subflow_data_available(ssk); 711 712 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 713 done = true; 714 break; 715 } 716 } while (more_data_avail); 717 718 *bytes += moved; 719 return done; 720 } 721 722 static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 723 { 724 struct sock *sk = (struct sock *)msk; 725 struct sk_buff *skb, *tail; 726 bool moved = false; 727 struct rb_node *p; 728 u64 end_seq; 729 730 p = rb_first(&msk->out_of_order_queue); 731 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 732 while (p) { 733 skb = rb_to_skb(p); 734 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 735 break; 736 737 p = rb_next(p); 738 rb_erase(&skb->rbnode, &msk->out_of_order_queue); 739 740 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 741 msk->ack_seq))) { 742 mptcp_drop(sk, skb); 743 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 744 continue; 745 } 746 747 end_seq = MPTCP_SKB_CB(skb)->end_seq; 748 tail = skb_peek_tail(&sk->sk_receive_queue); 749 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 750 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 751 752 /* skip overlapping data, if any */ 753 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 754 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 755 delta); 756 MPTCP_SKB_CB(skb)->offset += delta; 757 MPTCP_SKB_CB(skb)->map_seq += delta; 758 __skb_queue_tail(&sk->sk_receive_queue, skb); 759 } 760 msk->ack_seq = end_seq; 761 moved = true; 762 } 763 return moved; 764 } 765 766 /* In most cases we will be able to lock the mptcp socket. If its already 767 * owned, we need to defer to the work queue to avoid ABBA deadlock. 768 */ 769 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 770 { 771 struct sock *sk = (struct sock *)msk; 772 unsigned int moved = 0; 773 774 __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 775 __mptcp_ofo_queue(msk); 776 if (unlikely(ssk->sk_err)) { 777 if (!sock_owned_by_user(sk)) 778 __mptcp_error_report(sk); 779 else 780 __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags); 781 } 782 783 /* If the moves have caught up with the DATA_FIN sequence number 784 * it's time to ack the DATA_FIN and change socket state, but 785 * this is not a good place to change state. Let the workqueue 786 * do it. 787 */ 788 if (mptcp_pending_data_fin(sk, NULL)) 789 mptcp_schedule_work(sk); 790 return moved > 0; 791 } 792 793 void mptcp_data_ready(struct sock *sk, struct sock *ssk) 794 { 795 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 796 struct mptcp_sock *msk = mptcp_sk(sk); 797 int sk_rbuf, ssk_rbuf; 798 799 /* The peer can send data while we are shutting down this 800 * subflow at msk destruction time, but we must avoid enqueuing 801 * more data to the msk receive queue 802 */ 803 if (unlikely(subflow->disposable)) 804 return; 805 806 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 807 sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 808 if (unlikely(ssk_rbuf > sk_rbuf)) 809 sk_rbuf = ssk_rbuf; 810 811 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 812 if (__mptcp_rmem(sk) > sk_rbuf) { 813 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 814 return; 815 } 816 817 /* Wake-up the reader only for in-sequence data */ 818 mptcp_data_lock(sk); 819 if (move_skbs_to_msk(msk, ssk)) 820 sk->sk_data_ready(sk); 821 822 mptcp_data_unlock(sk); 823 } 824 825 static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk) 826 { 827 mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq); 828 WRITE_ONCE(msk->allow_infinite_fallback, false); 829 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 830 } 831 832 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk) 833 { 834 struct sock *sk = (struct sock *)msk; 835 836 if (sk->sk_state != TCP_ESTABLISHED) 837 return false; 838 839 /* attach to msk socket only after we are sure we will deal with it 840 * at close time 841 */ 842 if (sk->sk_socket && !ssk->sk_socket) 843 mptcp_sock_graft(ssk, sk->sk_socket); 844 845 mptcp_sockopt_sync_locked(msk, ssk); 846 mptcp_subflow_joined(msk, ssk); 847 return true; 848 } 849 850 static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list) 851 { 852 struct mptcp_subflow_context *tmp, *subflow; 853 struct mptcp_sock *msk = mptcp_sk(sk); 854 855 list_for_each_entry_safe(subflow, tmp, join_list, node) { 856 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 857 bool slow = lock_sock_fast(ssk); 858 859 list_move_tail(&subflow->node, &msk->conn_list); 860 if (!__mptcp_finish_join(msk, ssk)) 861 mptcp_subflow_reset(ssk); 862 unlock_sock_fast(ssk, slow); 863 } 864 } 865 866 static bool mptcp_timer_pending(struct sock *sk) 867 { 868 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 869 } 870 871 static void mptcp_reset_timer(struct sock *sk) 872 { 873 struct inet_connection_sock *icsk = inet_csk(sk); 874 unsigned long tout; 875 876 /* prevent rescheduling on close */ 877 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 878 return; 879 880 tout = mptcp_sk(sk)->timer_ival; 881 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 882 } 883 884 bool mptcp_schedule_work(struct sock *sk) 885 { 886 if (inet_sk_state_load(sk) != TCP_CLOSE && 887 schedule_work(&mptcp_sk(sk)->work)) { 888 /* each subflow already holds a reference to the sk, and the 889 * workqueue is invoked by a subflow, so sk can't go away here. 890 */ 891 sock_hold(sk); 892 return true; 893 } 894 return false; 895 } 896 897 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 898 { 899 struct mptcp_subflow_context *subflow; 900 901 msk_owned_by_me(msk); 902 903 mptcp_for_each_subflow(msk, subflow) { 904 if (READ_ONCE(subflow->data_avail)) 905 return mptcp_subflow_tcp_sock(subflow); 906 } 907 908 return NULL; 909 } 910 911 static bool mptcp_skb_can_collapse_to(u64 write_seq, 912 const struct sk_buff *skb, 913 const struct mptcp_ext *mpext) 914 { 915 if (!tcp_skb_can_collapse_to(skb)) 916 return false; 917 918 /* can collapse only if MPTCP level sequence is in order and this 919 * mapping has not been xmitted yet 920 */ 921 return mpext && mpext->data_seq + mpext->data_len == write_seq && 922 !mpext->frozen; 923 } 924 925 /* we can append data to the given data frag if: 926 * - there is space available in the backing page_frag 927 * - the data frag tail matches the current page_frag free offset 928 * - the data frag end sequence number matches the current write seq 929 */ 930 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 931 const struct page_frag *pfrag, 932 const struct mptcp_data_frag *df) 933 { 934 return df && pfrag->page == df->page && 935 pfrag->size - pfrag->offset > 0 && 936 pfrag->offset == (df->offset + df->data_len) && 937 df->data_seq + df->data_len == msk->write_seq; 938 } 939 940 static void dfrag_uncharge(struct sock *sk, int len) 941 { 942 sk_mem_uncharge(sk, len); 943 sk_wmem_queued_add(sk, -len); 944 } 945 946 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 947 { 948 int len = dfrag->data_len + dfrag->overhead; 949 950 list_del(&dfrag->list); 951 dfrag_uncharge(sk, len); 952 put_page(dfrag->page); 953 } 954 955 static void __mptcp_clean_una(struct sock *sk) 956 { 957 struct mptcp_sock *msk = mptcp_sk(sk); 958 struct mptcp_data_frag *dtmp, *dfrag; 959 u64 snd_una; 960 961 /* on fallback we just need to ignore snd_una, as this is really 962 * plain TCP 963 */ 964 if (__mptcp_check_fallback(msk)) 965 msk->snd_una = READ_ONCE(msk->snd_nxt); 966 967 snd_una = msk->snd_una; 968 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 969 if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 970 break; 971 972 if (unlikely(dfrag == msk->first_pending)) { 973 /* in recovery mode can see ack after the current snd head */ 974 if (WARN_ON_ONCE(!msk->recovery)) 975 break; 976 977 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 978 } 979 980 dfrag_clear(sk, dfrag); 981 } 982 983 dfrag = mptcp_rtx_head(sk); 984 if (dfrag && after64(snd_una, dfrag->data_seq)) { 985 u64 delta = snd_una - dfrag->data_seq; 986 987 /* prevent wrap around in recovery mode */ 988 if (unlikely(delta > dfrag->already_sent)) { 989 if (WARN_ON_ONCE(!msk->recovery)) 990 goto out; 991 if (WARN_ON_ONCE(delta > dfrag->data_len)) 992 goto out; 993 dfrag->already_sent += delta - dfrag->already_sent; 994 } 995 996 dfrag->data_seq += delta; 997 dfrag->offset += delta; 998 dfrag->data_len -= delta; 999 dfrag->already_sent -= delta; 1000 1001 dfrag_uncharge(sk, delta); 1002 } 1003 1004 /* all retransmitted data acked, recovery completed */ 1005 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 1006 msk->recovery = false; 1007 1008 out: 1009 if (snd_una == READ_ONCE(msk->snd_nxt) && 1010 snd_una == READ_ONCE(msk->write_seq)) { 1011 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 1012 mptcp_stop_timer(sk); 1013 } else { 1014 mptcp_reset_timer(sk); 1015 } 1016 } 1017 1018 static void __mptcp_clean_una_wakeup(struct sock *sk) 1019 { 1020 lockdep_assert_held_once(&sk->sk_lock.slock); 1021 1022 __mptcp_clean_una(sk); 1023 mptcp_write_space(sk); 1024 } 1025 1026 static void mptcp_clean_una_wakeup(struct sock *sk) 1027 { 1028 mptcp_data_lock(sk); 1029 __mptcp_clean_una_wakeup(sk); 1030 mptcp_data_unlock(sk); 1031 } 1032 1033 static void mptcp_enter_memory_pressure(struct sock *sk) 1034 { 1035 struct mptcp_subflow_context *subflow; 1036 struct mptcp_sock *msk = mptcp_sk(sk); 1037 bool first = true; 1038 1039 sk_stream_moderate_sndbuf(sk); 1040 mptcp_for_each_subflow(msk, subflow) { 1041 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1042 1043 if (first) 1044 tcp_enter_memory_pressure(ssk); 1045 sk_stream_moderate_sndbuf(ssk); 1046 first = false; 1047 } 1048 } 1049 1050 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1051 * data 1052 */ 1053 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1054 { 1055 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1056 pfrag, sk->sk_allocation))) 1057 return true; 1058 1059 mptcp_enter_memory_pressure(sk); 1060 return false; 1061 } 1062 1063 static struct mptcp_data_frag * 1064 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 1065 int orig_offset) 1066 { 1067 int offset = ALIGN(orig_offset, sizeof(long)); 1068 struct mptcp_data_frag *dfrag; 1069 1070 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 1071 dfrag->data_len = 0; 1072 dfrag->data_seq = msk->write_seq; 1073 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 1074 dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1075 dfrag->already_sent = 0; 1076 dfrag->page = pfrag->page; 1077 1078 return dfrag; 1079 } 1080 1081 struct mptcp_sendmsg_info { 1082 int mss_now; 1083 int size_goal; 1084 u16 limit; 1085 u16 sent; 1086 unsigned int flags; 1087 bool data_lock_held; 1088 }; 1089 1090 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1091 u64 data_seq, int avail_size) 1092 { 1093 u64 window_end = mptcp_wnd_end(msk); 1094 u64 mptcp_snd_wnd; 1095 1096 if (__mptcp_check_fallback(msk)) 1097 return avail_size; 1098 1099 mptcp_snd_wnd = window_end - data_seq; 1100 avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 1101 1102 if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1103 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 1104 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 1105 } 1106 1107 return avail_size; 1108 } 1109 1110 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1111 { 1112 struct skb_ext *mpext = __skb_ext_alloc(gfp); 1113 1114 if (!mpext) 1115 return false; 1116 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1117 return true; 1118 } 1119 1120 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1121 { 1122 struct sk_buff *skb; 1123 1124 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1125 if (likely(skb)) { 1126 if (likely(__mptcp_add_ext(skb, gfp))) { 1127 skb_reserve(skb, MAX_TCP_HEADER); 1128 skb->ip_summed = CHECKSUM_PARTIAL; 1129 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1130 return skb; 1131 } 1132 __kfree_skb(skb); 1133 } else { 1134 mptcp_enter_memory_pressure(sk); 1135 } 1136 return NULL; 1137 } 1138 1139 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1140 { 1141 struct sk_buff *skb; 1142 1143 skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1144 if (!skb) 1145 return NULL; 1146 1147 if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1148 tcp_skb_entail(ssk, skb); 1149 return skb; 1150 } 1151 tcp_skb_tsorted_anchor_cleanup(skb); 1152 kfree_skb(skb); 1153 return NULL; 1154 } 1155 1156 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1157 { 1158 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1159 1160 return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1161 } 1162 1163 /* note: this always recompute the csum on the whole skb, even 1164 * if we just appended a single frag. More status info needed 1165 */ 1166 static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1167 { 1168 struct mptcp_ext *mpext = mptcp_get_ext(skb); 1169 __wsum csum = ~csum_unfold(mpext->csum); 1170 int offset = skb->len - added; 1171 1172 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1173 } 1174 1175 static void mptcp_update_infinite_map(struct mptcp_sock *msk, 1176 struct sock *ssk, 1177 struct mptcp_ext *mpext) 1178 { 1179 if (!mpext) 1180 return; 1181 1182 mpext->infinite_map = 1; 1183 mpext->data_len = 0; 1184 1185 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 1186 mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 1187 pr_fallback(msk); 1188 mptcp_do_fallback(ssk); 1189 } 1190 1191 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1192 struct mptcp_data_frag *dfrag, 1193 struct mptcp_sendmsg_info *info) 1194 { 1195 u64 data_seq = dfrag->data_seq + info->sent; 1196 int offset = dfrag->offset + info->sent; 1197 struct mptcp_sock *msk = mptcp_sk(sk); 1198 bool zero_window_probe = false; 1199 struct mptcp_ext *mpext = NULL; 1200 bool can_coalesce = false; 1201 bool reuse_skb = true; 1202 struct sk_buff *skb; 1203 size_t copy; 1204 int i; 1205 1206 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1207 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 1208 1209 if (WARN_ON_ONCE(info->sent > info->limit || 1210 info->limit > dfrag->data_len)) 1211 return 0; 1212 1213 if (unlikely(!__tcp_can_send(ssk))) 1214 return -EAGAIN; 1215 1216 /* compute send limit */ 1217 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1218 copy = info->size_goal; 1219 1220 skb = tcp_write_queue_tail(ssk); 1221 if (skb && copy > skb->len) { 1222 /* Limit the write to the size available in the 1223 * current skb, if any, so that we create at most a new skb. 1224 * Explicitly tells TCP internals to avoid collapsing on later 1225 * queue management operation, to avoid breaking the ext <-> 1226 * SSN association set here 1227 */ 1228 mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1229 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 1230 TCP_SKB_CB(skb)->eor = 1; 1231 goto alloc_skb; 1232 } 1233 1234 i = skb_shinfo(skb)->nr_frags; 1235 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1236 if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1237 tcp_mark_push(tcp_sk(ssk), skb); 1238 goto alloc_skb; 1239 } 1240 1241 copy -= skb->len; 1242 } else { 1243 alloc_skb: 1244 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1245 if (!skb) 1246 return -ENOMEM; 1247 1248 i = skb_shinfo(skb)->nr_frags; 1249 reuse_skb = false; 1250 mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1251 } 1252 1253 /* Zero window and all data acked? Probe. */ 1254 copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1255 if (copy == 0) { 1256 u64 snd_una = READ_ONCE(msk->snd_una); 1257 1258 if (snd_una != msk->snd_nxt) { 1259 tcp_remove_empty_skb(ssk); 1260 return 0; 1261 } 1262 1263 zero_window_probe = true; 1264 data_seq = snd_una - 1; 1265 copy = 1; 1266 1267 /* all mptcp-level data is acked, no skbs should be present into the 1268 * ssk write queue 1269 */ 1270 WARN_ON_ONCE(reuse_skb); 1271 } 1272 1273 copy = min_t(size_t, copy, info->limit - info->sent); 1274 if (!sk_wmem_schedule(ssk, copy)) { 1275 tcp_remove_empty_skb(ssk); 1276 return -ENOMEM; 1277 } 1278 1279 if (can_coalesce) { 1280 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1281 } else { 1282 get_page(dfrag->page); 1283 skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 1284 } 1285 1286 skb->len += copy; 1287 skb->data_len += copy; 1288 skb->truesize += copy; 1289 sk_wmem_queued_add(ssk, copy); 1290 sk_mem_charge(ssk, copy); 1291 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1292 TCP_SKB_CB(skb)->end_seq += copy; 1293 tcp_skb_pcount_set(skb, 0); 1294 1295 /* on skb reuse we just need to update the DSS len */ 1296 if (reuse_skb) { 1297 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1298 mpext->data_len += copy; 1299 WARN_ON_ONCE(zero_window_probe); 1300 goto out; 1301 } 1302 1303 memset(mpext, 0, sizeof(*mpext)); 1304 mpext->data_seq = data_seq; 1305 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1306 mpext->data_len = copy; 1307 mpext->use_map = 1; 1308 mpext->dsn64 = 1; 1309 1310 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 1311 mpext->data_seq, mpext->subflow_seq, mpext->data_len, 1312 mpext->dsn64); 1313 1314 if (zero_window_probe) { 1315 mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1316 mpext->frozen = 1; 1317 if (READ_ONCE(msk->csum_enabled)) 1318 mptcp_update_data_checksum(skb, copy); 1319 tcp_push_pending_frames(ssk); 1320 return 0; 1321 } 1322 out: 1323 if (READ_ONCE(msk->csum_enabled)) 1324 mptcp_update_data_checksum(skb, copy); 1325 if (mptcp_subflow_ctx(ssk)->send_infinite_map) 1326 mptcp_update_infinite_map(msk, ssk, mpext); 1327 trace_mptcp_sendmsg_frag(mpext); 1328 mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1329 return copy; 1330 } 1331 1332 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1333 sizeof(struct tcphdr) - \ 1334 MAX_TCP_OPTION_SPACE - \ 1335 sizeof(struct ipv6hdr) - \ 1336 sizeof(struct frag_hdr)) 1337 1338 struct subflow_send_info { 1339 struct sock *ssk; 1340 u64 linger_time; 1341 }; 1342 1343 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1344 { 1345 if (!subflow->stale) 1346 return; 1347 1348 subflow->stale = 0; 1349 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1350 } 1351 1352 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1353 { 1354 if (unlikely(subflow->stale)) { 1355 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1356 1357 if (subflow->stale_rcv_tstamp == rcv_tstamp) 1358 return false; 1359 1360 mptcp_subflow_set_active(subflow); 1361 } 1362 return __mptcp_subflow_active(subflow); 1363 } 1364 1365 #define SSK_MODE_ACTIVE 0 1366 #define SSK_MODE_BACKUP 1 1367 #define SSK_MODE_MAX 2 1368 1369 /* implement the mptcp packet scheduler; 1370 * returns the subflow that will transmit the next DSS 1371 * additionally updates the rtx timeout 1372 */ 1373 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1374 { 1375 struct subflow_send_info send_info[SSK_MODE_MAX]; 1376 struct mptcp_subflow_context *subflow; 1377 struct sock *sk = (struct sock *)msk; 1378 u32 pace, burst, wmem; 1379 int i, nr_active = 0; 1380 struct sock *ssk; 1381 u64 linger_time; 1382 long tout = 0; 1383 1384 msk_owned_by_me(msk); 1385 1386 if (__mptcp_check_fallback(msk)) { 1387 if (!msk->first) 1388 return NULL; 1389 return __tcp_can_send(msk->first) && 1390 sk_stream_memory_free(msk->first) ? msk->first : NULL; 1391 } 1392 1393 /* re-use last subflow, if the burst allow that */ 1394 if (msk->last_snd && msk->snd_burst > 0 && 1395 sk_stream_memory_free(msk->last_snd) && 1396 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 1397 mptcp_set_timeout(sk); 1398 return msk->last_snd; 1399 } 1400 1401 /* pick the subflow with the lower wmem/wspace ratio */ 1402 for (i = 0; i < SSK_MODE_MAX; ++i) { 1403 send_info[i].ssk = NULL; 1404 send_info[i].linger_time = -1; 1405 } 1406 1407 mptcp_for_each_subflow(msk, subflow) { 1408 trace_mptcp_subflow_get_send(subflow); 1409 ssk = mptcp_subflow_tcp_sock(subflow); 1410 if (!mptcp_subflow_active(subflow)) 1411 continue; 1412 1413 tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1414 nr_active += !subflow->backup; 1415 pace = subflow->avg_pacing_rate; 1416 if (unlikely(!pace)) { 1417 /* init pacing rate from socket */ 1418 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 1419 pace = subflow->avg_pacing_rate; 1420 if (!pace) 1421 continue; 1422 } 1423 1424 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 1425 if (linger_time < send_info[subflow->backup].linger_time) { 1426 send_info[subflow->backup].ssk = ssk; 1427 send_info[subflow->backup].linger_time = linger_time; 1428 } 1429 } 1430 __mptcp_set_timeout(sk, tout); 1431 1432 /* pick the best backup if no other subflow is active */ 1433 if (!nr_active) 1434 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1435 1436 /* According to the blest algorithm, to avoid HoL blocking for the 1437 * faster flow, we need to: 1438 * - estimate the faster flow linger time 1439 * - use the above to estimate the amount of byte transferred 1440 * by the faster flow 1441 * - check that the amount of queued data is greter than the above, 1442 * otherwise do not use the picked, slower, subflow 1443 * We select the subflow with the shorter estimated time to flush 1444 * the queued mem, which basically ensure the above. We just need 1445 * to check that subflow has a non empty cwin. 1446 */ 1447 ssk = send_info[SSK_MODE_ACTIVE].ssk; 1448 if (!ssk || !sk_stream_memory_free(ssk)) 1449 return NULL; 1450 1451 burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 1452 wmem = READ_ONCE(ssk->sk_wmem_queued); 1453 if (!burst) { 1454 msk->last_snd = NULL; 1455 return ssk; 1456 } 1457 1458 subflow = mptcp_subflow_ctx(ssk); 1459 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 1460 READ_ONCE(ssk->sk_pacing_rate) * burst, 1461 burst + wmem); 1462 msk->last_snd = ssk; 1463 msk->snd_burst = burst; 1464 return ssk; 1465 } 1466 1467 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1468 { 1469 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1470 release_sock(ssk); 1471 } 1472 1473 static void mptcp_update_post_push(struct mptcp_sock *msk, 1474 struct mptcp_data_frag *dfrag, 1475 u32 sent) 1476 { 1477 u64 snd_nxt_new = dfrag->data_seq; 1478 1479 dfrag->already_sent += sent; 1480 1481 msk->snd_burst -= sent; 1482 1483 snd_nxt_new += dfrag->already_sent; 1484 1485 /* snd_nxt_new can be smaller than snd_nxt in case mptcp 1486 * is recovering after a failover. In that event, this re-sends 1487 * old segments. 1488 * 1489 * Thus compute snd_nxt_new candidate based on 1490 * the dfrag->data_seq that was sent and the data 1491 * that has been handed to the subflow for transmission 1492 * and skip update in case it was old dfrag. 1493 */ 1494 if (likely(after64(snd_nxt_new, msk->snd_nxt))) 1495 msk->snd_nxt = snd_nxt_new; 1496 } 1497 1498 void mptcp_check_and_set_pending(struct sock *sk) 1499 { 1500 if (mptcp_send_head(sk)) 1501 mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 1502 } 1503 1504 void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1505 { 1506 struct sock *prev_ssk = NULL, *ssk = NULL; 1507 struct mptcp_sock *msk = mptcp_sk(sk); 1508 struct mptcp_sendmsg_info info = { 1509 .flags = flags, 1510 }; 1511 bool do_check_data_fin = false; 1512 struct mptcp_data_frag *dfrag; 1513 int len; 1514 1515 while ((dfrag = mptcp_send_head(sk))) { 1516 info.sent = dfrag->already_sent; 1517 info.limit = dfrag->data_len; 1518 len = dfrag->data_len - dfrag->already_sent; 1519 while (len > 0) { 1520 int ret = 0; 1521 1522 prev_ssk = ssk; 1523 ssk = mptcp_subflow_get_send(msk); 1524 1525 /* First check. If the ssk has changed since 1526 * the last round, release prev_ssk 1527 */ 1528 if (ssk != prev_ssk && prev_ssk) 1529 mptcp_push_release(prev_ssk, &info); 1530 if (!ssk) 1531 goto out; 1532 1533 /* Need to lock the new subflow only if different 1534 * from the previous one, otherwise we are still 1535 * helding the relevant lock 1536 */ 1537 if (ssk != prev_ssk) 1538 lock_sock(ssk); 1539 1540 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1541 if (ret <= 0) { 1542 if (ret == -EAGAIN) 1543 continue; 1544 mptcp_push_release(ssk, &info); 1545 goto out; 1546 } 1547 1548 do_check_data_fin = true; 1549 info.sent += ret; 1550 len -= ret; 1551 1552 mptcp_update_post_push(msk, dfrag, ret); 1553 } 1554 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1555 } 1556 1557 /* at this point we held the socket lock for the last subflow we used */ 1558 if (ssk) 1559 mptcp_push_release(ssk, &info); 1560 1561 out: 1562 /* ensure the rtx timer is running */ 1563 if (!mptcp_timer_pending(sk)) 1564 mptcp_reset_timer(sk); 1565 if (do_check_data_fin) 1566 mptcp_check_send_data_fin(sk); 1567 } 1568 1569 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 1570 { 1571 struct mptcp_sock *msk = mptcp_sk(sk); 1572 struct mptcp_sendmsg_info info = { 1573 .data_lock_held = true, 1574 }; 1575 struct mptcp_data_frag *dfrag; 1576 struct sock *xmit_ssk; 1577 int len, copied = 0; 1578 1579 info.flags = 0; 1580 while ((dfrag = mptcp_send_head(sk))) { 1581 info.sent = dfrag->already_sent; 1582 info.limit = dfrag->data_len; 1583 len = dfrag->data_len - dfrag->already_sent; 1584 while (len > 0) { 1585 int ret = 0; 1586 1587 /* check for a different subflow usage only after 1588 * spooling the first chunk of data 1589 */ 1590 xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk); 1591 if (!xmit_ssk) 1592 goto out; 1593 if (xmit_ssk != ssk) { 1594 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1595 MPTCP_DELEGATE_SEND); 1596 goto out; 1597 } 1598 1599 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1600 if (ret <= 0) 1601 goto out; 1602 1603 info.sent += ret; 1604 copied += ret; 1605 len -= ret; 1606 first = false; 1607 1608 mptcp_update_post_push(msk, dfrag, ret); 1609 } 1610 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1611 } 1612 1613 out: 1614 /* __mptcp_alloc_tx_skb could have released some wmem and we are 1615 * not going to flush it via release_sock() 1616 */ 1617 if (copied) { 1618 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 1619 info.size_goal); 1620 if (!mptcp_timer_pending(sk)) 1621 mptcp_reset_timer(sk); 1622 1623 if (msk->snd_data_fin_enable && 1624 msk->snd_nxt + 1 == msk->write_seq) 1625 mptcp_schedule_work(sk); 1626 } 1627 } 1628 1629 static void mptcp_set_nospace(struct sock *sk) 1630 { 1631 /* enable autotune */ 1632 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1633 1634 /* will be cleared on avail space */ 1635 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 1636 } 1637 1638 static int mptcp_disconnect(struct sock *sk, int flags); 1639 1640 static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, 1641 size_t len, int *copied_syn) 1642 { 1643 unsigned int saved_flags = msg->msg_flags; 1644 struct mptcp_sock *msk = mptcp_sk(sk); 1645 struct socket *ssock; 1646 struct sock *ssk; 1647 int ret; 1648 1649 /* on flags based fastopen the mptcp is supposed to create the 1650 * first subflow right now. Otherwise we are in the defer_connect 1651 * path, and the first subflow must be already present. 1652 * Since the defer_connect flag is cleared after the first succsful 1653 * fastopen attempt, no need to check for additional subflow status. 1654 */ 1655 if (msg->msg_flags & MSG_FASTOPEN) { 1656 ssock = __mptcp_nmpc_socket(msk); 1657 if (IS_ERR(ssock)) 1658 return PTR_ERR(ssock); 1659 } 1660 if (!msk->first) 1661 return -EINVAL; 1662 1663 ssk = msk->first; 1664 1665 lock_sock(ssk); 1666 msg->msg_flags |= MSG_DONTWAIT; 1667 msk->fastopening = 1; 1668 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 1669 msk->fastopening = 0; 1670 msg->msg_flags = saved_flags; 1671 release_sock(ssk); 1672 1673 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1674 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1675 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1676 msg->msg_namelen, msg->msg_flags, 1); 1677 1678 /* Keep the same behaviour of plain TCP: zero the copied bytes in 1679 * case of any error, except timeout or signal 1680 */ 1681 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1682 *copied_syn = 0; 1683 } else if (ret && ret != -EINPROGRESS) { 1684 /* The disconnect() op called by tcp_sendmsg_fastopen()/ 1685 * __inet_stream_connect() can fail, due to looking check, 1686 * see mptcp_disconnect(). 1687 * Attempt it again outside the problematic scope. 1688 */ 1689 if (!mptcp_disconnect(sk, 0)) 1690 sk->sk_socket->state = SS_UNCONNECTED; 1691 } 1692 inet_sk(sk)->defer_connect = 0; 1693 1694 return ret; 1695 } 1696 1697 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1698 { 1699 struct mptcp_sock *msk = mptcp_sk(sk); 1700 struct page_frag *pfrag; 1701 size_t copied = 0; 1702 int ret = 0; 1703 long timeo; 1704 1705 /* silently ignore everything else */ 1706 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1707 1708 lock_sock(sk); 1709 1710 if (unlikely(inet_sk(sk)->defer_connect || msg->msg_flags & MSG_FASTOPEN)) { 1711 int copied_syn = 0; 1712 1713 ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn); 1714 copied += copied_syn; 1715 if (ret == -EINPROGRESS && copied_syn > 0) 1716 goto out; 1717 else if (ret) 1718 goto do_error; 1719 } 1720 1721 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 1722 1723 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 1724 ret = sk_stream_wait_connect(sk, &timeo); 1725 if (ret) 1726 goto do_error; 1727 } 1728 1729 ret = -EPIPE; 1730 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 1731 goto do_error; 1732 1733 pfrag = sk_page_frag(sk); 1734 1735 while (msg_data_left(msg)) { 1736 int total_ts, frag_truesize = 0; 1737 struct mptcp_data_frag *dfrag; 1738 bool dfrag_collapsed; 1739 size_t psize, offset; 1740 1741 /* reuse tail pfrag, if possible, or carve a new one from the 1742 * page allocator 1743 */ 1744 dfrag = mptcp_pending_tail(sk); 1745 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1746 if (!dfrag_collapsed) { 1747 if (!sk_stream_memory_free(sk)) 1748 goto wait_for_memory; 1749 1750 if (!mptcp_page_frag_refill(sk, pfrag)) 1751 goto wait_for_memory; 1752 1753 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1754 frag_truesize = dfrag->overhead; 1755 } 1756 1757 /* we do not bound vs wspace, to allow a single packet. 1758 * memory accounting will prevent execessive memory usage 1759 * anyway 1760 */ 1761 offset = dfrag->offset + dfrag->data_len; 1762 psize = pfrag->size - offset; 1763 psize = min_t(size_t, psize, msg_data_left(msg)); 1764 total_ts = psize + frag_truesize; 1765 1766 if (!sk_wmem_schedule(sk, total_ts)) 1767 goto wait_for_memory; 1768 1769 if (copy_page_from_iter(dfrag->page, offset, psize, 1770 &msg->msg_iter) != psize) { 1771 ret = -EFAULT; 1772 goto do_error; 1773 } 1774 1775 /* data successfully copied into the write queue */ 1776 sk->sk_forward_alloc -= total_ts; 1777 copied += psize; 1778 dfrag->data_len += psize; 1779 frag_truesize += psize; 1780 pfrag->offset += frag_truesize; 1781 WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1782 1783 /* charge data on mptcp pending queue to the msk socket 1784 * Note: we charge such data both to sk and ssk 1785 */ 1786 sk_wmem_queued_add(sk, frag_truesize); 1787 if (!dfrag_collapsed) { 1788 get_page(dfrag->page); 1789 list_add_tail(&dfrag->list, &msk->rtx_queue); 1790 if (!msk->first_pending) 1791 WRITE_ONCE(msk->first_pending, dfrag); 1792 } 1793 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1794 dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1795 !dfrag_collapsed); 1796 1797 continue; 1798 1799 wait_for_memory: 1800 mptcp_set_nospace(sk); 1801 __mptcp_push_pending(sk, msg->msg_flags); 1802 ret = sk_stream_wait_memory(sk, &timeo); 1803 if (ret) 1804 goto do_error; 1805 } 1806 1807 if (copied) 1808 __mptcp_push_pending(sk, msg->msg_flags); 1809 1810 out: 1811 release_sock(sk); 1812 return copied; 1813 1814 do_error: 1815 if (copied) 1816 goto out; 1817 1818 copied = sk_stream_error(sk, msg->msg_flags, ret); 1819 goto out; 1820 } 1821 1822 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 1823 struct msghdr *msg, 1824 size_t len, int flags, 1825 struct scm_timestamping_internal *tss, 1826 int *cmsg_flags) 1827 { 1828 struct sk_buff *skb, *tmp; 1829 int copied = 0; 1830 1831 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 1832 u32 offset = MPTCP_SKB_CB(skb)->offset; 1833 u32 data_len = skb->len - offset; 1834 u32 count = min_t(size_t, len - copied, data_len); 1835 int err; 1836 1837 if (!(flags & MSG_TRUNC)) { 1838 err = skb_copy_datagram_msg(skb, offset, msg, count); 1839 if (unlikely(err < 0)) { 1840 if (!copied) 1841 return err; 1842 break; 1843 } 1844 } 1845 1846 if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1847 tcp_update_recv_tstamps(skb, tss); 1848 *cmsg_flags |= MPTCP_CMSG_TS; 1849 } 1850 1851 copied += count; 1852 1853 if (count < data_len) { 1854 if (!(flags & MSG_PEEK)) { 1855 MPTCP_SKB_CB(skb)->offset += count; 1856 MPTCP_SKB_CB(skb)->map_seq += count; 1857 } 1858 break; 1859 } 1860 1861 if (!(flags & MSG_PEEK)) { 1862 /* we will bulk release the skb memory later */ 1863 skb->destructor = NULL; 1864 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 1865 __skb_unlink(skb, &msk->receive_queue); 1866 __kfree_skb(skb); 1867 } 1868 1869 if (copied >= len) 1870 break; 1871 } 1872 1873 return copied; 1874 } 1875 1876 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1877 * 1878 * Only difference: Use highest rtt estimate of the subflows in use. 1879 */ 1880 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1881 { 1882 struct mptcp_subflow_context *subflow; 1883 struct sock *sk = (struct sock *)msk; 1884 u32 time, advmss = 1; 1885 u64 rtt_us, mstamp; 1886 1887 msk_owned_by_me(msk); 1888 1889 if (copied <= 0) 1890 return; 1891 1892 msk->rcvq_space.copied += copied; 1893 1894 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1895 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1896 1897 rtt_us = msk->rcvq_space.rtt_us; 1898 if (rtt_us && time < (rtt_us >> 3)) 1899 return; 1900 1901 rtt_us = 0; 1902 mptcp_for_each_subflow(msk, subflow) { 1903 const struct tcp_sock *tp; 1904 u64 sf_rtt_us; 1905 u32 sf_advmss; 1906 1907 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1908 1909 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1910 sf_advmss = READ_ONCE(tp->advmss); 1911 1912 rtt_us = max(sf_rtt_us, rtt_us); 1913 advmss = max(sf_advmss, advmss); 1914 } 1915 1916 msk->rcvq_space.rtt_us = rtt_us; 1917 if (time < (rtt_us >> 3) || rtt_us == 0) 1918 return; 1919 1920 if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1921 goto new_measure; 1922 1923 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1924 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1925 int rcvmem, rcvbuf; 1926 u64 rcvwin, grow; 1927 1928 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1929 1930 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1931 1932 do_div(grow, msk->rcvq_space.space); 1933 rcvwin += (grow << 1); 1934 1935 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1936 while (tcp_win_from_space(sk, rcvmem) < advmss) 1937 rcvmem += 128; 1938 1939 do_div(rcvwin, advmss); 1940 rcvbuf = min_t(u64, rcvwin * rcvmem, 1941 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1942 1943 if (rcvbuf > sk->sk_rcvbuf) { 1944 u32 window_clamp; 1945 1946 window_clamp = tcp_win_from_space(sk, rcvbuf); 1947 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1948 1949 /* Make subflows follow along. If we do not do this, we 1950 * get drops at subflow level if skbs can't be moved to 1951 * the mptcp rx queue fast enough (announced rcv_win can 1952 * exceed ssk->sk_rcvbuf). 1953 */ 1954 mptcp_for_each_subflow(msk, subflow) { 1955 struct sock *ssk; 1956 bool slow; 1957 1958 ssk = mptcp_subflow_tcp_sock(subflow); 1959 slow = lock_sock_fast(ssk); 1960 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1961 tcp_sk(ssk)->window_clamp = window_clamp; 1962 tcp_cleanup_rbuf(ssk, 1); 1963 unlock_sock_fast(ssk, slow); 1964 } 1965 } 1966 } 1967 1968 msk->rcvq_space.space = msk->rcvq_space.copied; 1969 new_measure: 1970 msk->rcvq_space.copied = 0; 1971 msk->rcvq_space.time = mstamp; 1972 } 1973 1974 static void __mptcp_update_rmem(struct sock *sk) 1975 { 1976 struct mptcp_sock *msk = mptcp_sk(sk); 1977 1978 if (!msk->rmem_released) 1979 return; 1980 1981 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 1982 mptcp_rmem_uncharge(sk, msk->rmem_released); 1983 WRITE_ONCE(msk->rmem_released, 0); 1984 } 1985 1986 static void __mptcp_splice_receive_queue(struct sock *sk) 1987 { 1988 struct mptcp_sock *msk = mptcp_sk(sk); 1989 1990 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 1991 } 1992 1993 static bool __mptcp_move_skbs(struct mptcp_sock *msk) 1994 { 1995 struct sock *sk = (struct sock *)msk; 1996 unsigned int moved = 0; 1997 bool ret, done; 1998 1999 do { 2000 struct sock *ssk = mptcp_subflow_recv_lookup(msk); 2001 bool slowpath; 2002 2003 /* we can have data pending in the subflows only if the msk 2004 * receive buffer was full at subflow_data_ready() time, 2005 * that is an unlikely slow path. 2006 */ 2007 if (likely(!ssk)) 2008 break; 2009 2010 slowpath = lock_sock_fast(ssk); 2011 mptcp_data_lock(sk); 2012 __mptcp_update_rmem(sk); 2013 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 2014 mptcp_data_unlock(sk); 2015 2016 if (unlikely(ssk->sk_err)) 2017 __mptcp_error_report(sk); 2018 unlock_sock_fast(ssk, slowpath); 2019 } while (!done); 2020 2021 /* acquire the data lock only if some input data is pending */ 2022 ret = moved > 0; 2023 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 2024 !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 2025 mptcp_data_lock(sk); 2026 __mptcp_update_rmem(sk); 2027 ret |= __mptcp_ofo_queue(msk); 2028 __mptcp_splice_receive_queue(sk); 2029 mptcp_data_unlock(sk); 2030 } 2031 if (ret) 2032 mptcp_check_data_fin((struct sock *)msk); 2033 return !skb_queue_empty(&msk->receive_queue); 2034 } 2035 2036 static unsigned int mptcp_inq_hint(const struct sock *sk) 2037 { 2038 const struct mptcp_sock *msk = mptcp_sk(sk); 2039 const struct sk_buff *skb; 2040 2041 skb = skb_peek(&msk->receive_queue); 2042 if (skb) { 2043 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 2044 2045 if (hint_val >= INT_MAX) 2046 return INT_MAX; 2047 2048 return (unsigned int)hint_val; 2049 } 2050 2051 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 2052 return 1; 2053 2054 return 0; 2055 } 2056 2057 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2058 int flags, int *addr_len) 2059 { 2060 struct mptcp_sock *msk = mptcp_sk(sk); 2061 struct scm_timestamping_internal tss; 2062 int copied = 0, cmsg_flags = 0; 2063 int target; 2064 long timeo; 2065 2066 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2067 if (unlikely(flags & MSG_ERRQUEUE)) 2068 return inet_recv_error(sk, msg, len, addr_len); 2069 2070 lock_sock(sk); 2071 if (unlikely(sk->sk_state == TCP_LISTEN)) { 2072 copied = -ENOTCONN; 2073 goto out_err; 2074 } 2075 2076 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 2077 2078 len = min_t(size_t, len, INT_MAX); 2079 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 2080 2081 if (unlikely(msk->recvmsg_inq)) 2082 cmsg_flags = MPTCP_CMSG_INQ; 2083 2084 while (copied < len) { 2085 int bytes_read; 2086 2087 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 2088 if (unlikely(bytes_read < 0)) { 2089 if (!copied) 2090 copied = bytes_read; 2091 goto out_err; 2092 } 2093 2094 copied += bytes_read; 2095 2096 /* be sure to advertise window change */ 2097 mptcp_cleanup_rbuf(msk); 2098 2099 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2100 continue; 2101 2102 /* only the master socket status is relevant here. The exit 2103 * conditions mirror closely tcp_recvmsg() 2104 */ 2105 if (copied >= target) 2106 break; 2107 2108 if (copied) { 2109 if (sk->sk_err || 2110 sk->sk_state == TCP_CLOSE || 2111 (sk->sk_shutdown & RCV_SHUTDOWN) || 2112 !timeo || 2113 signal_pending(current)) 2114 break; 2115 } else { 2116 if (sk->sk_err) { 2117 copied = sock_error(sk); 2118 break; 2119 } 2120 2121 if (sk->sk_shutdown & RCV_SHUTDOWN) { 2122 /* race breaker: the shutdown could be after the 2123 * previous receive queue check 2124 */ 2125 if (__mptcp_move_skbs(msk)) 2126 continue; 2127 break; 2128 } 2129 2130 if (sk->sk_state == TCP_CLOSE) { 2131 copied = -ENOTCONN; 2132 break; 2133 } 2134 2135 if (!timeo) { 2136 copied = -EAGAIN; 2137 break; 2138 } 2139 2140 if (signal_pending(current)) { 2141 copied = sock_intr_errno(timeo); 2142 break; 2143 } 2144 } 2145 2146 pr_debug("block timeout %ld", timeo); 2147 sk_wait_data(sk, &timeo, NULL); 2148 } 2149 2150 out_err: 2151 if (cmsg_flags && copied >= 0) { 2152 if (cmsg_flags & MPTCP_CMSG_TS) 2153 tcp_recv_timestamp(msg, sk, &tss); 2154 2155 if (cmsg_flags & MPTCP_CMSG_INQ) { 2156 unsigned int inq = mptcp_inq_hint(sk); 2157 2158 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 2159 } 2160 } 2161 2162 pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2163 msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2164 skb_queue_empty(&msk->receive_queue), copied); 2165 if (!(flags & MSG_PEEK)) 2166 mptcp_rcv_space_adjust(msk, copied); 2167 2168 release_sock(sk); 2169 return copied; 2170 } 2171 2172 static void mptcp_retransmit_timer(struct timer_list *t) 2173 { 2174 struct inet_connection_sock *icsk = from_timer(icsk, t, 2175 icsk_retransmit_timer); 2176 struct sock *sk = &icsk->icsk_inet.sk; 2177 struct mptcp_sock *msk = mptcp_sk(sk); 2178 2179 bh_lock_sock(sk); 2180 if (!sock_owned_by_user(sk)) { 2181 /* we need a process context to retransmit */ 2182 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 2183 mptcp_schedule_work(sk); 2184 } else { 2185 /* delegate our work to tcp_release_cb() */ 2186 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2187 } 2188 bh_unlock_sock(sk); 2189 sock_put(sk); 2190 } 2191 2192 static void mptcp_timeout_timer(struct timer_list *t) 2193 { 2194 struct sock *sk = from_timer(sk, t, sk_timer); 2195 2196 mptcp_schedule_work(sk); 2197 sock_put(sk); 2198 } 2199 2200 /* Find an idle subflow. Return NULL if there is unacked data at tcp 2201 * level. 2202 * 2203 * A backup subflow is returned only if that is the only kind available. 2204 */ 2205 static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 2206 { 2207 struct sock *backup = NULL, *pick = NULL; 2208 struct mptcp_subflow_context *subflow; 2209 int min_stale_count = INT_MAX; 2210 2211 msk_owned_by_me(msk); 2212 2213 if (__mptcp_check_fallback(msk)) 2214 return NULL; 2215 2216 mptcp_for_each_subflow(msk, subflow) { 2217 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2218 2219 if (!__mptcp_subflow_active(subflow)) 2220 continue; 2221 2222 /* still data outstanding at TCP level? skip this */ 2223 if (!tcp_rtx_and_write_queues_empty(ssk)) { 2224 mptcp_pm_subflow_chk_stale(msk, ssk); 2225 min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2226 continue; 2227 } 2228 2229 if (subflow->backup) { 2230 if (!backup) 2231 backup = ssk; 2232 continue; 2233 } 2234 2235 if (!pick) 2236 pick = ssk; 2237 } 2238 2239 if (pick) 2240 return pick; 2241 2242 /* use backup only if there are no progresses anywhere */ 2243 return min_stale_count > 1 ? backup : NULL; 2244 } 2245 2246 static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 2247 { 2248 if (msk->subflow) { 2249 iput(SOCK_INODE(msk->subflow)); 2250 WRITE_ONCE(msk->subflow, NULL); 2251 } 2252 } 2253 2254 bool __mptcp_retransmit_pending_data(struct sock *sk) 2255 { 2256 struct mptcp_data_frag *cur, *rtx_head; 2257 struct mptcp_sock *msk = mptcp_sk(sk); 2258 2259 if (__mptcp_check_fallback(msk)) 2260 return false; 2261 2262 if (tcp_rtx_and_write_queues_empty(sk)) 2263 return false; 2264 2265 /* the closing socket has some data untransmitted and/or unacked: 2266 * some data in the mptcp rtx queue has not really xmitted yet. 2267 * keep it simple and re-inject the whole mptcp level rtx queue 2268 */ 2269 mptcp_data_lock(sk); 2270 __mptcp_clean_una_wakeup(sk); 2271 rtx_head = mptcp_rtx_head(sk); 2272 if (!rtx_head) { 2273 mptcp_data_unlock(sk); 2274 return false; 2275 } 2276 2277 msk->recovery_snd_nxt = msk->snd_nxt; 2278 msk->recovery = true; 2279 mptcp_data_unlock(sk); 2280 2281 msk->first_pending = rtx_head; 2282 msk->snd_burst = 0; 2283 2284 /* be sure to clear the "sent status" on all re-injected fragments */ 2285 list_for_each_entry(cur, &msk->rtx_queue, list) { 2286 if (!cur->already_sent) 2287 break; 2288 cur->already_sent = 0; 2289 } 2290 2291 return true; 2292 } 2293 2294 /* flags for __mptcp_close_ssk() */ 2295 #define MPTCP_CF_PUSH BIT(1) 2296 #define MPTCP_CF_FASTCLOSE BIT(2) 2297 2298 /* subflow sockets can be either outgoing (connect) or incoming 2299 * (accept). 2300 * 2301 * Outgoing subflows use in-kernel sockets. 2302 * Incoming subflows do not have their own 'struct socket' allocated, 2303 * so we need to use tcp_close() after detaching them from the mptcp 2304 * parent socket. 2305 */ 2306 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2307 struct mptcp_subflow_context *subflow, 2308 unsigned int flags) 2309 { 2310 struct mptcp_sock *msk = mptcp_sk(sk); 2311 bool dispose_it, need_push = false; 2312 2313 /* If the first subflow moved to a close state before accept, e.g. due 2314 * to an incoming reset, mptcp either: 2315 * - if either the subflow or the msk are dead, destroy the context 2316 * (the subflow socket is deleted by inet_child_forget) and the msk 2317 * - otherwise do nothing at the moment and take action at accept and/or 2318 * listener shutdown - user-space must be able to accept() the closed 2319 * socket. 2320 */ 2321 if (msk->in_accept_queue && msk->first == ssk) { 2322 if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD)) 2323 return; 2324 2325 /* ensure later check in mptcp_worker() will dispose the msk */ 2326 sock_set_flag(sk, SOCK_DEAD); 2327 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2328 mptcp_subflow_drop_ctx(ssk); 2329 goto out_release; 2330 } 2331 2332 dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2333 if (dispose_it) 2334 list_del(&subflow->node); 2335 2336 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2337 2338 if (flags & MPTCP_CF_FASTCLOSE) { 2339 /* be sure to force the tcp_disconnect() path, 2340 * to generate the egress reset 2341 */ 2342 ssk->sk_lingertime = 0; 2343 sock_set_flag(ssk, SOCK_LINGER); 2344 subflow->send_fastclose = 1; 2345 } 2346 2347 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2348 if (!dispose_it) { 2349 /* The MPTCP code never wait on the subflow sockets, TCP-level 2350 * disconnect should never fail 2351 */ 2352 WARN_ON_ONCE(tcp_disconnect(ssk, 0)); 2353 msk->subflow->state = SS_UNCONNECTED; 2354 mptcp_subflow_ctx_reset(subflow); 2355 release_sock(ssk); 2356 2357 goto out; 2358 } 2359 2360 subflow->disposable = 1; 2361 2362 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2363 * the ssk has been already destroyed, we just need to release the 2364 * reference owned by msk; 2365 */ 2366 if (!inet_csk(ssk)->icsk_ulp_ops) { 2367 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2368 kfree_rcu(subflow, rcu); 2369 } else { 2370 /* otherwise tcp will dispose of the ssk and subflow ctx */ 2371 __tcp_close(ssk, 0); 2372 2373 /* close acquired an extra ref */ 2374 __sock_put(ssk); 2375 } 2376 2377 out_release: 2378 release_sock(ssk); 2379 2380 sock_put(ssk); 2381 2382 if (ssk == msk->first) 2383 WRITE_ONCE(msk->first, NULL); 2384 2385 out: 2386 if (ssk == msk->last_snd) 2387 msk->last_snd = NULL; 2388 2389 if (need_push) 2390 __mptcp_push_pending(sk, 0); 2391 } 2392 2393 void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2394 struct mptcp_subflow_context *subflow) 2395 { 2396 if (sk->sk_state == TCP_ESTABLISHED) 2397 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2398 2399 /* subflow aborted before reaching the fully_established status 2400 * attempt the creation of the next subflow 2401 */ 2402 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2403 2404 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2405 } 2406 2407 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2408 { 2409 return 0; 2410 } 2411 2412 static void __mptcp_close_subflow(struct sock *sk) 2413 { 2414 struct mptcp_subflow_context *subflow, *tmp; 2415 struct mptcp_sock *msk = mptcp_sk(sk); 2416 2417 might_sleep(); 2418 2419 mptcp_for_each_subflow_safe(msk, subflow, tmp) { 2420 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2421 2422 if (inet_sk_state_load(ssk) != TCP_CLOSE) 2423 continue; 2424 2425 /* 'subflow_data_ready' will re-sched once rx queue is empty */ 2426 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 2427 continue; 2428 2429 mptcp_close_ssk(sk, ssk, subflow); 2430 } 2431 2432 } 2433 2434 static bool mptcp_should_close(const struct sock *sk) 2435 { 2436 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2437 struct mptcp_subflow_context *subflow; 2438 2439 if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue) 2440 return true; 2441 2442 /* if all subflows are in closed status don't bother with additional 2443 * timeout 2444 */ 2445 mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2446 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2447 TCP_CLOSE) 2448 return false; 2449 } 2450 return true; 2451 } 2452 2453 static void mptcp_check_fastclose(struct mptcp_sock *msk) 2454 { 2455 struct mptcp_subflow_context *subflow, *tmp; 2456 struct sock *sk = (struct sock *)msk; 2457 2458 if (likely(!READ_ONCE(msk->rcv_fastclose))) 2459 return; 2460 2461 mptcp_token_destroy(msk); 2462 2463 mptcp_for_each_subflow_safe(msk, subflow, tmp) { 2464 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2465 bool slow; 2466 2467 slow = lock_sock_fast(tcp_sk); 2468 if (tcp_sk->sk_state != TCP_CLOSE) { 2469 tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 2470 tcp_set_state(tcp_sk, TCP_CLOSE); 2471 } 2472 unlock_sock_fast(tcp_sk, slow); 2473 } 2474 2475 /* Mirror the tcp_reset() error propagation */ 2476 switch (sk->sk_state) { 2477 case TCP_SYN_SENT: 2478 WRITE_ONCE(sk->sk_err, ECONNREFUSED); 2479 break; 2480 case TCP_CLOSE_WAIT: 2481 WRITE_ONCE(sk->sk_err, EPIPE); 2482 break; 2483 case TCP_CLOSE: 2484 return; 2485 default: 2486 WRITE_ONCE(sk->sk_err, ECONNRESET); 2487 } 2488 2489 inet_sk_state_store(sk, TCP_CLOSE); 2490 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 2491 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 2492 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 2493 2494 /* the calling mptcp_worker will properly destroy the socket */ 2495 if (sock_flag(sk, SOCK_DEAD)) 2496 return; 2497 2498 sk->sk_state_change(sk); 2499 sk_error_report(sk); 2500 } 2501 2502 static void __mptcp_retrans(struct sock *sk) 2503 { 2504 struct mptcp_sock *msk = mptcp_sk(sk); 2505 struct mptcp_sendmsg_info info = {}; 2506 struct mptcp_data_frag *dfrag; 2507 size_t copied = 0; 2508 struct sock *ssk; 2509 int ret; 2510 2511 mptcp_clean_una_wakeup(sk); 2512 2513 /* first check ssk: need to kick "stale" logic */ 2514 ssk = mptcp_subflow_get_retrans(msk); 2515 dfrag = mptcp_rtx_head(sk); 2516 if (!dfrag) { 2517 if (mptcp_data_fin_enabled(msk)) { 2518 struct inet_connection_sock *icsk = inet_csk(sk); 2519 2520 icsk->icsk_retransmits++; 2521 mptcp_set_datafin_timeout(sk); 2522 mptcp_send_ack(msk); 2523 2524 goto reset_timer; 2525 } 2526 2527 if (!mptcp_send_head(sk)) 2528 return; 2529 2530 goto reset_timer; 2531 } 2532 2533 if (!ssk) 2534 goto reset_timer; 2535 2536 lock_sock(ssk); 2537 2538 /* limit retransmission to the bytes already sent on some subflows */ 2539 info.sent = 0; 2540 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 2541 while (info.sent < info.limit) { 2542 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 2543 if (ret <= 0) 2544 break; 2545 2546 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 2547 copied += ret; 2548 info.sent += ret; 2549 } 2550 if (copied) { 2551 dfrag->already_sent = max(dfrag->already_sent, info.sent); 2552 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 2553 info.size_goal); 2554 WRITE_ONCE(msk->allow_infinite_fallback, false); 2555 } 2556 2557 release_sock(ssk); 2558 2559 reset_timer: 2560 mptcp_check_and_set_pending(sk); 2561 2562 if (!mptcp_timer_pending(sk)) 2563 mptcp_reset_timer(sk); 2564 } 2565 2566 /* schedule the timeout timer for the relevant event: either close timeout 2567 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 2568 */ 2569 void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 2570 { 2571 struct sock *sk = (struct sock *)msk; 2572 unsigned long timeout, close_timeout; 2573 2574 if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 2575 return; 2576 2577 close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 2578 2579 /* the close timeout takes precedence on the fail one, and here at least one of 2580 * them is active 2581 */ 2582 timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 2583 2584 sk_reset_timer(sk, &sk->sk_timer, timeout); 2585 } 2586 2587 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 2588 { 2589 struct sock *ssk = msk->first; 2590 bool slow; 2591 2592 if (!ssk) 2593 return; 2594 2595 pr_debug("MP_FAIL doesn't respond, reset the subflow"); 2596 2597 slow = lock_sock_fast(ssk); 2598 mptcp_subflow_reset(ssk); 2599 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 2600 unlock_sock_fast(ssk, slow); 2601 2602 mptcp_reset_timeout(msk, 0); 2603 } 2604 2605 static void mptcp_do_fastclose(struct sock *sk) 2606 { 2607 struct mptcp_subflow_context *subflow, *tmp; 2608 struct mptcp_sock *msk = mptcp_sk(sk); 2609 2610 mptcp_for_each_subflow_safe(msk, subflow, tmp) 2611 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2612 subflow, MPTCP_CF_FASTCLOSE); 2613 } 2614 2615 static void mptcp_worker(struct work_struct *work) 2616 { 2617 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 2618 struct sock *sk = (struct sock *)msk; 2619 unsigned long fail_tout; 2620 int state; 2621 2622 lock_sock(sk); 2623 state = sk->sk_state; 2624 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN))) 2625 goto unlock; 2626 2627 mptcp_check_fastclose(msk); 2628 2629 mptcp_pm_nl_work(msk); 2630 2631 mptcp_check_send_data_fin(sk); 2632 mptcp_check_data_fin_ack(sk); 2633 mptcp_check_data_fin(sk); 2634 2635 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2636 __mptcp_close_subflow(sk); 2637 2638 /* There is no point in keeping around an orphaned sk timedout or 2639 * closed, but we need the msk around to reply to incoming DATA_FIN, 2640 * even if it is orphaned and in FIN_WAIT2 state 2641 */ 2642 if (sock_flag(sk, SOCK_DEAD)) { 2643 if (mptcp_should_close(sk)) { 2644 inet_sk_state_store(sk, TCP_CLOSE); 2645 mptcp_do_fastclose(sk); 2646 } 2647 if (sk->sk_state == TCP_CLOSE) { 2648 __mptcp_destroy_sock(sk); 2649 goto unlock; 2650 } 2651 } 2652 2653 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 2654 __mptcp_retrans(sk); 2655 2656 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 2657 if (fail_tout && time_after(jiffies, fail_tout)) 2658 mptcp_mp_fail_no_response(msk); 2659 2660 unlock: 2661 release_sock(sk); 2662 sock_put(sk); 2663 } 2664 2665 static int __mptcp_init_sock(struct sock *sk) 2666 { 2667 struct mptcp_sock *msk = mptcp_sk(sk); 2668 2669 INIT_LIST_HEAD(&msk->conn_list); 2670 INIT_LIST_HEAD(&msk->join_list); 2671 INIT_LIST_HEAD(&msk->rtx_queue); 2672 INIT_WORK(&msk->work, mptcp_worker); 2673 __skb_queue_head_init(&msk->receive_queue); 2674 msk->out_of_order_queue = RB_ROOT; 2675 msk->first_pending = NULL; 2676 msk->rmem_fwd_alloc = 0; 2677 WRITE_ONCE(msk->rmem_released, 0); 2678 msk->timer_ival = TCP_RTO_MIN; 2679 2680 WRITE_ONCE(msk->first, NULL); 2681 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2682 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 2683 WRITE_ONCE(msk->allow_infinite_fallback, true); 2684 msk->recovery = false; 2685 2686 mptcp_pm_data_init(msk); 2687 2688 /* re-use the csk retrans timer for MPTCP-level retrans */ 2689 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2690 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2691 2692 return 0; 2693 } 2694 2695 static void mptcp_ca_reset(struct sock *sk) 2696 { 2697 struct inet_connection_sock *icsk = inet_csk(sk); 2698 2699 tcp_assign_congestion_control(sk); 2700 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2701 2702 /* no need to keep a reference to the ops, the name will suffice */ 2703 tcp_cleanup_congestion_control(sk); 2704 icsk->icsk_ca_ops = NULL; 2705 } 2706 2707 static int mptcp_init_sock(struct sock *sk) 2708 { 2709 struct net *net = sock_net(sk); 2710 int ret; 2711 2712 ret = __mptcp_init_sock(sk); 2713 if (ret) 2714 return ret; 2715 2716 if (!mptcp_is_enabled(net)) 2717 return -ENOPROTOOPT; 2718 2719 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2720 return -ENOMEM; 2721 2722 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2723 2724 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 2725 * propagate the correct value 2726 */ 2727 mptcp_ca_reset(sk); 2728 2729 sk_sockets_allocated_inc(sk); 2730 sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2731 sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2732 2733 return 0; 2734 } 2735 2736 static void __mptcp_clear_xmit(struct sock *sk) 2737 { 2738 struct mptcp_sock *msk = mptcp_sk(sk); 2739 struct mptcp_data_frag *dtmp, *dfrag; 2740 2741 WRITE_ONCE(msk->first_pending, NULL); 2742 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2743 dfrag_clear(sk, dfrag); 2744 } 2745 2746 void mptcp_cancel_work(struct sock *sk) 2747 { 2748 struct mptcp_sock *msk = mptcp_sk(sk); 2749 2750 if (cancel_work_sync(&msk->work)) 2751 __sock_put(sk); 2752 } 2753 2754 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 2755 { 2756 lock_sock(ssk); 2757 2758 switch (ssk->sk_state) { 2759 case TCP_LISTEN: 2760 if (!(how & RCV_SHUTDOWN)) 2761 break; 2762 fallthrough; 2763 case TCP_SYN_SENT: 2764 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK)); 2765 break; 2766 default: 2767 if (__mptcp_check_fallback(mptcp_sk(sk))) { 2768 pr_debug("Fallback"); 2769 ssk->sk_shutdown |= how; 2770 tcp_shutdown(ssk, how); 2771 2772 /* simulate the data_fin ack reception to let the state 2773 * machine move forward 2774 */ 2775 WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt); 2776 mptcp_schedule_work(sk); 2777 } else { 2778 pr_debug("Sending DATA_FIN on subflow %p", ssk); 2779 tcp_send_ack(ssk); 2780 if (!mptcp_timer_pending(sk)) 2781 mptcp_reset_timer(sk); 2782 } 2783 break; 2784 } 2785 2786 release_sock(ssk); 2787 } 2788 2789 static const unsigned char new_state[16] = { 2790 /* current state: new state: action: */ 2791 [0 /* (Invalid) */] = TCP_CLOSE, 2792 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 2793 [TCP_SYN_SENT] = TCP_CLOSE, 2794 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 2795 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 2796 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 2797 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 2798 [TCP_CLOSE] = TCP_CLOSE, 2799 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 2800 [TCP_LAST_ACK] = TCP_LAST_ACK, 2801 [TCP_LISTEN] = TCP_CLOSE, 2802 [TCP_CLOSING] = TCP_CLOSING, 2803 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 2804 }; 2805 2806 static int mptcp_close_state(struct sock *sk) 2807 { 2808 int next = (int)new_state[sk->sk_state]; 2809 int ns = next & TCP_STATE_MASK; 2810 2811 inet_sk_state_store(sk, ns); 2812 2813 return next & TCP_ACTION_FIN; 2814 } 2815 2816 static void mptcp_check_send_data_fin(struct sock *sk) 2817 { 2818 struct mptcp_subflow_context *subflow; 2819 struct mptcp_sock *msk = mptcp_sk(sk); 2820 2821 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2822 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2823 msk->snd_nxt, msk->write_seq); 2824 2825 /* we still need to enqueue subflows or not really shutting down, 2826 * skip this 2827 */ 2828 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2829 mptcp_send_head(sk)) 2830 return; 2831 2832 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2833 2834 mptcp_for_each_subflow(msk, subflow) { 2835 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2836 2837 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2838 } 2839 } 2840 2841 static void __mptcp_wr_shutdown(struct sock *sk) 2842 { 2843 struct mptcp_sock *msk = mptcp_sk(sk); 2844 2845 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2846 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2847 !!mptcp_send_head(sk)); 2848 2849 /* will be ignored by fallback sockets */ 2850 WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2851 WRITE_ONCE(msk->snd_data_fin_enable, 1); 2852 2853 mptcp_check_send_data_fin(sk); 2854 } 2855 2856 static void __mptcp_destroy_sock(struct sock *sk) 2857 { 2858 struct mptcp_sock *msk = mptcp_sk(sk); 2859 2860 pr_debug("msk=%p", msk); 2861 2862 might_sleep(); 2863 2864 mptcp_stop_timer(sk); 2865 sk_stop_timer(sk, &sk->sk_timer); 2866 msk->pm.status = 0; 2867 2868 sk->sk_prot->destroy(sk); 2869 2870 WARN_ON_ONCE(msk->rmem_fwd_alloc); 2871 WARN_ON_ONCE(msk->rmem_released); 2872 sk_stream_kill_queues(sk); 2873 xfrm_sk_free_policy(sk); 2874 2875 sock_put(sk); 2876 } 2877 2878 void __mptcp_unaccepted_force_close(struct sock *sk) 2879 { 2880 sock_set_flag(sk, SOCK_DEAD); 2881 inet_sk_state_store(sk, TCP_CLOSE); 2882 mptcp_do_fastclose(sk); 2883 __mptcp_destroy_sock(sk); 2884 } 2885 2886 static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2887 { 2888 /* Concurrent splices from sk_receive_queue into receive_queue will 2889 * always show at least one non-empty queue when checked in this order. 2890 */ 2891 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2892 skb_queue_empty_lockless(&msk->receive_queue)) 2893 return 0; 2894 2895 return EPOLLIN | EPOLLRDNORM; 2896 } 2897 2898 static void mptcp_check_listen_stop(struct sock *sk) 2899 { 2900 struct sock *ssk; 2901 2902 if (inet_sk_state_load(sk) != TCP_LISTEN) 2903 return; 2904 2905 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 2906 ssk = mptcp_sk(sk)->first; 2907 if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN)) 2908 return; 2909 2910 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2911 mptcp_subflow_queue_clean(sk, ssk); 2912 inet_csk_listen_stop(ssk); 2913 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 2914 tcp_set_state(ssk, TCP_CLOSE); 2915 release_sock(ssk); 2916 } 2917 2918 bool __mptcp_close(struct sock *sk, long timeout) 2919 { 2920 struct mptcp_subflow_context *subflow; 2921 struct mptcp_sock *msk = mptcp_sk(sk); 2922 bool do_cancel_work = false; 2923 int subflows_alive = 0; 2924 2925 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK); 2926 2927 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2928 mptcp_check_listen_stop(sk); 2929 inet_sk_state_store(sk, TCP_CLOSE); 2930 goto cleanup; 2931 } 2932 2933 if (mptcp_check_readable(msk) || timeout < 0) { 2934 /* If the msk has read data, or the caller explicitly ask it, 2935 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose 2936 */ 2937 inet_sk_state_store(sk, TCP_CLOSE); 2938 mptcp_do_fastclose(sk); 2939 timeout = 0; 2940 } else if (mptcp_close_state(sk)) { 2941 __mptcp_wr_shutdown(sk); 2942 } 2943 2944 sk_stream_wait_close(sk, timeout); 2945 2946 cleanup: 2947 /* orphan all the subflows */ 2948 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 2949 mptcp_for_each_subflow(msk, subflow) { 2950 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2951 bool slow = lock_sock_fast_nested(ssk); 2952 2953 subflows_alive += ssk->sk_state != TCP_CLOSE; 2954 2955 /* since the close timeout takes precedence on the fail one, 2956 * cancel the latter 2957 */ 2958 if (ssk == msk->first) 2959 subflow->fail_tout = 0; 2960 2961 /* detach from the parent socket, but allow data_ready to 2962 * push incoming data into the mptcp stack, to properly ack it 2963 */ 2964 ssk->sk_socket = NULL; 2965 ssk->sk_wq = NULL; 2966 unlock_sock_fast(ssk, slow); 2967 } 2968 sock_orphan(sk); 2969 2970 /* all the subflows are closed, only timeout can change the msk 2971 * state, let's not keep resources busy for no reasons 2972 */ 2973 if (subflows_alive == 0) 2974 inet_sk_state_store(sk, TCP_CLOSE); 2975 2976 sock_hold(sk); 2977 pr_debug("msk=%p state=%d", sk, sk->sk_state); 2978 if (msk->token) 2979 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 2980 2981 if (sk->sk_state == TCP_CLOSE) { 2982 __mptcp_destroy_sock(sk); 2983 do_cancel_work = true; 2984 } else { 2985 mptcp_reset_timeout(msk, 0); 2986 } 2987 2988 return do_cancel_work; 2989 } 2990 2991 static void mptcp_close(struct sock *sk, long timeout) 2992 { 2993 bool do_cancel_work; 2994 2995 lock_sock(sk); 2996 2997 do_cancel_work = __mptcp_close(sk, timeout); 2998 release_sock(sk); 2999 if (do_cancel_work) 3000 mptcp_cancel_work(sk); 3001 3002 sock_put(sk); 3003 } 3004 3005 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3006 { 3007 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3008 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3009 struct ipv6_pinfo *msk6 = inet6_sk(msk); 3010 3011 msk->sk_v6_daddr = ssk->sk_v6_daddr; 3012 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3013 3014 if (msk6 && ssk6) { 3015 msk6->saddr = ssk6->saddr; 3016 msk6->flow_label = ssk6->flow_label; 3017 } 3018 #endif 3019 3020 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3021 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3022 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3023 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3024 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3025 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3026 } 3027 3028 static int mptcp_disconnect(struct sock *sk, int flags) 3029 { 3030 struct mptcp_sock *msk = mptcp_sk(sk); 3031 3032 /* Deny disconnect if other threads are blocked in sk_wait_event() 3033 * or inet_wait_for_connect(). 3034 */ 3035 if (sk->sk_wait_pending) 3036 return -EBUSY; 3037 3038 /* We are on the fastopen error path. We can't call straight into the 3039 * subflows cleanup code due to lock nesting (we are already under 3040 * msk->firstsocket lock). 3041 */ 3042 if (msk->fastopening) 3043 return -EBUSY; 3044 3045 mptcp_check_listen_stop(sk); 3046 inet_sk_state_store(sk, TCP_CLOSE); 3047 3048 mptcp_stop_timer(sk); 3049 sk_stop_timer(sk, &sk->sk_timer); 3050 3051 if (msk->token) 3052 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3053 3054 /* msk->subflow is still intact, the following will not free the first 3055 * subflow 3056 */ 3057 mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3058 msk->last_snd = NULL; 3059 WRITE_ONCE(msk->flags, 0); 3060 msk->cb_flags = 0; 3061 msk->push_pending = 0; 3062 msk->recovery = false; 3063 msk->can_ack = false; 3064 msk->fully_established = false; 3065 msk->rcv_data_fin = false; 3066 msk->snd_data_fin_enable = false; 3067 msk->rcv_fastclose = false; 3068 msk->use_64bit_ack = false; 3069 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3070 mptcp_pm_data_reset(msk); 3071 mptcp_ca_reset(sk); 3072 3073 WRITE_ONCE(sk->sk_shutdown, 0); 3074 sk_error_report(sk); 3075 return 0; 3076 } 3077 3078 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3079 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3080 { 3081 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3082 3083 return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3084 } 3085 #endif 3086 3087 struct sock *mptcp_sk_clone_init(const struct sock *sk, 3088 const struct mptcp_options_received *mp_opt, 3089 struct sock *ssk, 3090 struct request_sock *req) 3091 { 3092 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3093 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 3094 struct mptcp_sock *msk; 3095 3096 if (!nsk) 3097 return NULL; 3098 3099 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3100 if (nsk->sk_family == AF_INET6) 3101 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3102 #endif 3103 3104 nsk->sk_wait_pending = 0; 3105 __mptcp_init_sock(nsk); 3106 3107 msk = mptcp_sk(nsk); 3108 msk->local_key = subflow_req->local_key; 3109 msk->token = subflow_req->token; 3110 WRITE_ONCE(msk->subflow, NULL); 3111 msk->in_accept_queue = 1; 3112 WRITE_ONCE(msk->fully_established, false); 3113 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 3114 WRITE_ONCE(msk->csum_enabled, true); 3115 3116 msk->write_seq = subflow_req->idsn + 1; 3117 msk->snd_nxt = msk->write_seq; 3118 msk->snd_una = msk->write_seq; 3119 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3120 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 3121 3122 sock_reset_flag(nsk, SOCK_RCU_FREE); 3123 security_inet_csk_clone(nsk, req); 3124 3125 /* this can't race with mptcp_close(), as the msk is 3126 * not yet exposted to user-space 3127 */ 3128 inet_sk_state_store(nsk, TCP_ESTABLISHED); 3129 3130 /* The msk maintain a ref to each subflow in the connections list */ 3131 WRITE_ONCE(msk->first, ssk); 3132 list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list); 3133 sock_hold(ssk); 3134 3135 /* new mpc subflow takes ownership of the newly 3136 * created mptcp socket 3137 */ 3138 mptcp_token_accept(subflow_req, msk); 3139 3140 /* set msk addresses early to ensure mptcp_pm_get_local_id() 3141 * uses the correct data 3142 */ 3143 mptcp_copy_inaddrs(nsk, ssk); 3144 mptcp_propagate_sndbuf(nsk, ssk); 3145 3146 mptcp_rcv_space_init(msk, ssk); 3147 bh_unlock_sock(nsk); 3148 3149 /* note: the newly allocated socket refcount is 2 now */ 3150 return nsk; 3151 } 3152 3153 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3154 { 3155 const struct tcp_sock *tp = tcp_sk(ssk); 3156 3157 msk->rcvq_space.copied = 0; 3158 msk->rcvq_space.rtt_us = 0; 3159 3160 msk->rcvq_space.time = tp->tcp_mstamp; 3161 3162 /* initial rcv_space offering made to peer */ 3163 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3164 TCP_INIT_CWND * tp->advmss); 3165 if (msk->rcvq_space.space == 0) 3166 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 3167 3168 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3169 } 3170 3171 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3172 bool kern) 3173 { 3174 struct mptcp_sock *msk = mptcp_sk(sk); 3175 struct socket *listener; 3176 struct sock *newsk; 3177 3178 listener = READ_ONCE(msk->subflow); 3179 if (WARN_ON_ONCE(!listener)) { 3180 *err = -EINVAL; 3181 return NULL; 3182 } 3183 3184 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3185 newsk = inet_csk_accept(listener->sk, flags, err, kern); 3186 if (!newsk) 3187 return NULL; 3188 3189 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3190 if (sk_is_mptcp(newsk)) { 3191 struct mptcp_subflow_context *subflow; 3192 struct sock *new_mptcp_sock; 3193 3194 subflow = mptcp_subflow_ctx(newsk); 3195 new_mptcp_sock = subflow->conn; 3196 3197 /* is_mptcp should be false if subflow->conn is missing, see 3198 * subflow_syn_recv_sock() 3199 */ 3200 if (WARN_ON_ONCE(!new_mptcp_sock)) { 3201 tcp_sk(newsk)->is_mptcp = 0; 3202 goto out; 3203 } 3204 3205 newsk = new_mptcp_sock; 3206 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3207 } else { 3208 MPTCP_INC_STATS(sock_net(sk), 3209 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3210 } 3211 3212 out: 3213 newsk->sk_kern_sock = kern; 3214 return newsk; 3215 } 3216 3217 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 3218 { 3219 struct mptcp_subflow_context *subflow, *tmp; 3220 struct sock *sk = (struct sock *)msk; 3221 3222 __mptcp_clear_xmit(sk); 3223 3224 /* join list will be eventually flushed (with rst) at sock lock release time */ 3225 mptcp_for_each_subflow_safe(msk, subflow, tmp) 3226 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3227 3228 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3229 mptcp_data_lock(sk); 3230 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 3231 __skb_queue_purge(&sk->sk_receive_queue); 3232 skb_rbtree_purge(&msk->out_of_order_queue); 3233 mptcp_data_unlock(sk); 3234 3235 /* move all the rx fwd alloc into the sk_mem_reclaim_final in 3236 * inet_sock_destruct() will dispose it 3237 */ 3238 sk->sk_forward_alloc += msk->rmem_fwd_alloc; 3239 msk->rmem_fwd_alloc = 0; 3240 mptcp_token_destroy(msk); 3241 mptcp_pm_free_anno_list(msk); 3242 mptcp_free_local_addr_list(msk); 3243 } 3244 3245 static void mptcp_destroy(struct sock *sk) 3246 { 3247 struct mptcp_sock *msk = mptcp_sk(sk); 3248 3249 /* clears msk->subflow, allowing the following to close 3250 * even the initial subflow 3251 */ 3252 mptcp_dispose_initial_subflow(msk); 3253 mptcp_destroy_common(msk, 0); 3254 sk_sockets_allocated_dec(sk); 3255 } 3256 3257 void __mptcp_data_acked(struct sock *sk) 3258 { 3259 if (!sock_owned_by_user(sk)) 3260 __mptcp_clean_una(sk); 3261 else 3262 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 3263 3264 if (mptcp_pending_data_fin_ack(sk)) 3265 mptcp_schedule_work(sk); 3266 } 3267 3268 void __mptcp_check_push(struct sock *sk, struct sock *ssk) 3269 { 3270 if (!mptcp_send_head(sk)) 3271 return; 3272 3273 if (!sock_owned_by_user(sk)) 3274 __mptcp_subflow_push_pending(sk, ssk, false); 3275 else 3276 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3277 } 3278 3279 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3280 BIT(MPTCP_RETRANSMIT) | \ 3281 BIT(MPTCP_FLUSH_JOIN_LIST)) 3282 3283 /* processes deferred events and flush wmem */ 3284 static void mptcp_release_cb(struct sock *sk) 3285 __must_hold(&sk->sk_lock.slock) 3286 { 3287 struct mptcp_sock *msk = mptcp_sk(sk); 3288 3289 for (;;) { 3290 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3291 msk->push_pending; 3292 struct list_head join_list; 3293 3294 if (!flags) 3295 break; 3296 3297 INIT_LIST_HEAD(&join_list); 3298 list_splice_init(&msk->join_list, &join_list); 3299 3300 /* the following actions acquire the subflow socket lock 3301 * 3302 * 1) can't be invoked in atomic scope 3303 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 3304 * datapath acquires the msk socket spinlock while helding 3305 * the subflow socket lock 3306 */ 3307 msk->push_pending = 0; 3308 msk->cb_flags &= ~flags; 3309 spin_unlock_bh(&sk->sk_lock.slock); 3310 3311 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 3312 __mptcp_flush_join_list(sk, &join_list); 3313 if (flags & BIT(MPTCP_PUSH_PENDING)) 3314 __mptcp_push_pending(sk, 0); 3315 if (flags & BIT(MPTCP_RETRANSMIT)) 3316 __mptcp_retrans(sk); 3317 3318 cond_resched(); 3319 spin_lock_bh(&sk->sk_lock.slock); 3320 } 3321 3322 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 3323 __mptcp_clean_una_wakeup(sk); 3324 if (unlikely(&msk->cb_flags)) { 3325 /* be sure to set the current sk state before tacking actions 3326 * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3327 */ 3328 if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3329 __mptcp_set_connected(sk); 3330 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 3331 __mptcp_error_report(sk); 3332 if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 3333 msk->last_snd = NULL; 3334 } 3335 3336 __mptcp_update_rmem(sk); 3337 } 3338 3339 /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3340 * TCP can't schedule delack timer before the subflow is fully established. 3341 * MPTCP uses the delack timer to do 3rd ack retransmissions 3342 */ 3343 static void schedule_3rdack_retransmission(struct sock *ssk) 3344 { 3345 struct inet_connection_sock *icsk = inet_csk(ssk); 3346 struct tcp_sock *tp = tcp_sk(ssk); 3347 unsigned long timeout; 3348 3349 if (mptcp_subflow_ctx(ssk)->fully_established) 3350 return; 3351 3352 /* reschedule with a timeout above RTT, as we must look only for drop */ 3353 if (tp->srtt_us) 3354 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3355 else 3356 timeout = TCP_TIMEOUT_INIT; 3357 timeout += jiffies; 3358 3359 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3360 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3361 icsk->icsk_ack.timeout = timeout; 3362 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3363 } 3364 3365 void mptcp_subflow_process_delegated(struct sock *ssk) 3366 { 3367 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3368 struct sock *sk = subflow->conn; 3369 3370 if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3371 mptcp_data_lock(sk); 3372 if (!sock_owned_by_user(sk)) 3373 __mptcp_subflow_push_pending(sk, ssk, true); 3374 else 3375 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3376 mptcp_data_unlock(sk); 3377 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3378 } 3379 if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3380 schedule_3rdack_retransmission(ssk); 3381 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3382 } 3383 } 3384 3385 static int mptcp_hash(struct sock *sk) 3386 { 3387 /* should never be called, 3388 * we hash the TCP subflows not the master socket 3389 */ 3390 WARN_ON_ONCE(1); 3391 return 0; 3392 } 3393 3394 static void mptcp_unhash(struct sock *sk) 3395 { 3396 /* called from sk_common_release(), but nothing to do here */ 3397 } 3398 3399 static int mptcp_get_port(struct sock *sk, unsigned short snum) 3400 { 3401 struct mptcp_sock *msk = mptcp_sk(sk); 3402 struct socket *ssock; 3403 3404 ssock = msk->subflow; 3405 pr_debug("msk=%p, subflow=%p", msk, ssock); 3406 if (WARN_ON_ONCE(!ssock)) 3407 return -EINVAL; 3408 3409 return inet_csk_get_port(ssock->sk, snum); 3410 } 3411 3412 void mptcp_finish_connect(struct sock *ssk) 3413 { 3414 struct mptcp_subflow_context *subflow; 3415 struct mptcp_sock *msk; 3416 struct sock *sk; 3417 3418 subflow = mptcp_subflow_ctx(ssk); 3419 sk = subflow->conn; 3420 msk = mptcp_sk(sk); 3421 3422 pr_debug("msk=%p, token=%u", sk, subflow->token); 3423 3424 subflow->map_seq = subflow->iasn; 3425 subflow->map_subflow_seq = 1; 3426 3427 /* the socket is not connected yet, no msk/subflow ops can access/race 3428 * accessing the field below 3429 */ 3430 WRITE_ONCE(msk->local_key, subflow->local_key); 3431 WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3432 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 3433 WRITE_ONCE(msk->snd_una, msk->write_seq); 3434 3435 mptcp_pm_new_connection(msk, ssk, 0); 3436 3437 mptcp_rcv_space_init(msk, ssk); 3438 } 3439 3440 void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3441 { 3442 write_lock_bh(&sk->sk_callback_lock); 3443 rcu_assign_pointer(sk->sk_wq, &parent->wq); 3444 sk_set_socket(sk, parent); 3445 sk->sk_uid = SOCK_INODE(parent)->i_uid; 3446 write_unlock_bh(&sk->sk_callback_lock); 3447 } 3448 3449 bool mptcp_finish_join(struct sock *ssk) 3450 { 3451 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3452 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3453 struct sock *parent = (void *)msk; 3454 bool ret = true; 3455 3456 pr_debug("msk=%p, subflow=%p", msk, subflow); 3457 3458 /* mptcp socket already closing? */ 3459 if (!mptcp_is_fully_established(parent)) { 3460 subflow->reset_reason = MPTCP_RST_EMPTCP; 3461 return false; 3462 } 3463 3464 /* active subflow, already present inside the conn_list */ 3465 if (!list_empty(&subflow->node)) { 3466 mptcp_subflow_joined(msk, ssk); 3467 return true; 3468 } 3469 3470 if (!mptcp_pm_allow_new_subflow(msk)) 3471 goto err_prohibited; 3472 3473 /* If we can't acquire msk socket lock here, let the release callback 3474 * handle it 3475 */ 3476 mptcp_data_lock(parent); 3477 if (!sock_owned_by_user(parent)) { 3478 ret = __mptcp_finish_join(msk, ssk); 3479 if (ret) { 3480 sock_hold(ssk); 3481 list_add_tail(&subflow->node, &msk->conn_list); 3482 } 3483 } else { 3484 sock_hold(ssk); 3485 list_add_tail(&subflow->node, &msk->join_list); 3486 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 3487 } 3488 mptcp_data_unlock(parent); 3489 3490 if (!ret) { 3491 err_prohibited: 3492 subflow->reset_reason = MPTCP_RST_EPROHIBIT; 3493 return false; 3494 } 3495 3496 return true; 3497 } 3498 3499 static void mptcp_shutdown(struct sock *sk, int how) 3500 { 3501 pr_debug("sk=%p, how=%d", sk, how); 3502 3503 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 3504 __mptcp_wr_shutdown(sk); 3505 } 3506 3507 static int mptcp_forward_alloc_get(const struct sock *sk) 3508 { 3509 return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 3510 } 3511 3512 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3513 { 3514 const struct sock *sk = (void *)msk; 3515 u64 delta; 3516 3517 if (sk->sk_state == TCP_LISTEN) 3518 return -EINVAL; 3519 3520 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3521 return 0; 3522 3523 delta = msk->write_seq - v; 3524 if (__mptcp_check_fallback(msk) && msk->first) { 3525 struct tcp_sock *tp = tcp_sk(msk->first); 3526 3527 /* the first subflow is disconnected after close - see 3528 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 3529 * so ignore that status, too. 3530 */ 3531 if (!((1 << msk->first->sk_state) & 3532 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 3533 delta += READ_ONCE(tp->write_seq) - tp->snd_una; 3534 } 3535 if (delta > INT_MAX) 3536 delta = INT_MAX; 3537 3538 return (int)delta; 3539 } 3540 3541 static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3542 { 3543 struct mptcp_sock *msk = mptcp_sk(sk); 3544 bool slow; 3545 int answ; 3546 3547 switch (cmd) { 3548 case SIOCINQ: 3549 if (sk->sk_state == TCP_LISTEN) 3550 return -EINVAL; 3551 3552 lock_sock(sk); 3553 __mptcp_move_skbs(msk); 3554 answ = mptcp_inq_hint(sk); 3555 release_sock(sk); 3556 break; 3557 case SIOCOUTQ: 3558 slow = lock_sock_fast(sk); 3559 answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3560 unlock_sock_fast(sk, slow); 3561 break; 3562 case SIOCOUTQNSD: 3563 slow = lock_sock_fast(sk); 3564 answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3565 unlock_sock_fast(sk, slow); 3566 break; 3567 default: 3568 return -ENOIOCTLCMD; 3569 } 3570 3571 return put_user(answ, (int __user *)arg); 3572 } 3573 3574 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 3575 struct mptcp_subflow_context *subflow) 3576 { 3577 subflow->request_mptcp = 0; 3578 __mptcp_do_fallback(msk); 3579 } 3580 3581 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 3582 { 3583 struct mptcp_subflow_context *subflow; 3584 struct mptcp_sock *msk = mptcp_sk(sk); 3585 struct socket *ssock; 3586 int err = -EINVAL; 3587 3588 ssock = __mptcp_nmpc_socket(msk); 3589 if (IS_ERR(ssock)) 3590 return PTR_ERR(ssock); 3591 3592 mptcp_token_destroy(msk); 3593 inet_sk_state_store(sk, TCP_SYN_SENT); 3594 subflow = mptcp_subflow_ctx(ssock->sk); 3595 #ifdef CONFIG_TCP_MD5SIG 3596 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3597 * TCP option space. 3598 */ 3599 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 3600 mptcp_subflow_early_fallback(msk, subflow); 3601 #endif 3602 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3603 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 3604 mptcp_subflow_early_fallback(msk, subflow); 3605 } 3606 if (likely(!__mptcp_check_fallback(msk))) 3607 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 3608 3609 /* if reaching here via the fastopen/sendmsg path, the caller already 3610 * acquired the subflow socket lock, too. 3611 */ 3612 if (msk->fastopening) 3613 err = __inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK, 1); 3614 else 3615 err = inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK); 3616 inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect; 3617 3618 /* on successful connect, the msk state will be moved to established by 3619 * subflow_finish_connect() 3620 */ 3621 if (unlikely(err && err != -EINPROGRESS)) { 3622 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3623 return err; 3624 } 3625 3626 mptcp_copy_inaddrs(sk, ssock->sk); 3627 3628 /* silence EINPROGRESS and let the caller inet_stream_connect 3629 * handle the connection in progress 3630 */ 3631 return 0; 3632 } 3633 3634 static struct proto mptcp_prot = { 3635 .name = "MPTCP", 3636 .owner = THIS_MODULE, 3637 .init = mptcp_init_sock, 3638 .connect = mptcp_connect, 3639 .disconnect = mptcp_disconnect, 3640 .close = mptcp_close, 3641 .accept = mptcp_accept, 3642 .setsockopt = mptcp_setsockopt, 3643 .getsockopt = mptcp_getsockopt, 3644 .shutdown = mptcp_shutdown, 3645 .destroy = mptcp_destroy, 3646 .sendmsg = mptcp_sendmsg, 3647 .ioctl = mptcp_ioctl, 3648 .recvmsg = mptcp_recvmsg, 3649 .release_cb = mptcp_release_cb, 3650 .hash = mptcp_hash, 3651 .unhash = mptcp_unhash, 3652 .get_port = mptcp_get_port, 3653 .forward_alloc_get = mptcp_forward_alloc_get, 3654 .sockets_allocated = &mptcp_sockets_allocated, 3655 3656 .memory_allocated = &tcp_memory_allocated, 3657 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 3658 3659 .memory_pressure = &tcp_memory_pressure, 3660 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3661 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3662 .sysctl_mem = sysctl_tcp_mem, 3663 .obj_size = sizeof(struct mptcp_sock), 3664 .slab_flags = SLAB_TYPESAFE_BY_RCU, 3665 .no_autobind = true, 3666 }; 3667 3668 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 3669 { 3670 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3671 struct socket *ssock; 3672 int err; 3673 3674 lock_sock(sock->sk); 3675 ssock = __mptcp_nmpc_socket(msk); 3676 if (IS_ERR(ssock)) { 3677 err = PTR_ERR(ssock); 3678 goto unlock; 3679 } 3680 3681 err = ssock->ops->bind(ssock, uaddr, addr_len); 3682 if (!err) 3683 mptcp_copy_inaddrs(sock->sk, ssock->sk); 3684 3685 unlock: 3686 release_sock(sock->sk); 3687 return err; 3688 } 3689 3690 static int mptcp_listen(struct socket *sock, int backlog) 3691 { 3692 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3693 struct sock *sk = sock->sk; 3694 struct socket *ssock; 3695 int err; 3696 3697 pr_debug("msk=%p", msk); 3698 3699 lock_sock(sk); 3700 ssock = __mptcp_nmpc_socket(msk); 3701 if (IS_ERR(ssock)) { 3702 err = PTR_ERR(ssock); 3703 goto unlock; 3704 } 3705 3706 mptcp_token_destroy(msk); 3707 inet_sk_state_store(sk, TCP_LISTEN); 3708 sock_set_flag(sk, SOCK_RCU_FREE); 3709 3710 err = ssock->ops->listen(ssock, backlog); 3711 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3712 if (!err) { 3713 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 3714 mptcp_copy_inaddrs(sk, ssock->sk); 3715 } 3716 3717 mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED); 3718 3719 unlock: 3720 release_sock(sk); 3721 return err; 3722 } 3723 3724 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3725 int flags, bool kern) 3726 { 3727 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3728 struct socket *ssock; 3729 int err; 3730 3731 pr_debug("msk=%p", msk); 3732 3733 /* Buggy applications can call accept on socket states other then LISTEN 3734 * but no need to allocate the first subflow just to error out. 3735 */ 3736 ssock = READ_ONCE(msk->subflow); 3737 if (!ssock) 3738 return -EINVAL; 3739 3740 err = ssock->ops->accept(sock, newsock, flags, kern); 3741 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3742 struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3743 struct mptcp_subflow_context *subflow; 3744 struct sock *newsk = newsock->sk; 3745 3746 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3747 msk->in_accept_queue = 0; 3748 3749 lock_sock(newsk); 3750 3751 /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3752 * This is needed so NOSPACE flag can be set from tcp stack. 3753 */ 3754 mptcp_for_each_subflow(msk, subflow) { 3755 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3756 3757 if (!ssk->sk_socket) 3758 mptcp_sock_graft(ssk, newsock); 3759 } 3760 3761 /* Do late cleanup for the first subflow as necessary. Also 3762 * deal with bad peers not doing a complete shutdown. 3763 */ 3764 if (msk->first && 3765 unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) { 3766 __mptcp_close_ssk(newsk, msk->first, 3767 mptcp_subflow_ctx(msk->first), 0); 3768 if (unlikely(list_empty(&msk->conn_list))) 3769 inet_sk_state_store(newsk, TCP_CLOSE); 3770 } 3771 3772 release_sock(newsk); 3773 } 3774 3775 return err; 3776 } 3777 3778 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 3779 { 3780 struct sock *sk = (struct sock *)msk; 3781 3782 if (sk_stream_is_writeable(sk)) 3783 return EPOLLOUT | EPOLLWRNORM; 3784 3785 mptcp_set_nospace(sk); 3786 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 3787 if (sk_stream_is_writeable(sk)) 3788 return EPOLLOUT | EPOLLWRNORM; 3789 3790 return 0; 3791 } 3792 3793 static __poll_t mptcp_poll(struct file *file, struct socket *sock, 3794 struct poll_table_struct *wait) 3795 { 3796 struct sock *sk = sock->sk; 3797 struct mptcp_sock *msk; 3798 __poll_t mask = 0; 3799 u8 shutdown; 3800 int state; 3801 3802 msk = mptcp_sk(sk); 3803 sock_poll_wait(file, sock, wait); 3804 3805 state = inet_sk_state_load(sk); 3806 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 3807 if (state == TCP_LISTEN) { 3808 struct socket *ssock = READ_ONCE(msk->subflow); 3809 3810 if (WARN_ON_ONCE(!ssock || !ssock->sk)) 3811 return 0; 3812 3813 return inet_csk_listen_poll(ssock->sk); 3814 } 3815 3816 shutdown = READ_ONCE(sk->sk_shutdown); 3817 if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3818 mask |= EPOLLHUP; 3819 if (shutdown & RCV_SHUTDOWN) 3820 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 3821 3822 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 3823 mask |= mptcp_check_readable(msk); 3824 if (shutdown & SEND_SHUTDOWN) 3825 mask |= EPOLLOUT | EPOLLWRNORM; 3826 else 3827 mask |= mptcp_check_writeable(msk); 3828 } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) { 3829 /* cf tcp_poll() note about TFO */ 3830 mask |= EPOLLOUT | EPOLLWRNORM; 3831 } 3832 3833 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */ 3834 smp_rmb(); 3835 if (READ_ONCE(sk->sk_err)) 3836 mask |= EPOLLERR; 3837 3838 return mask; 3839 } 3840 3841 static const struct proto_ops mptcp_stream_ops = { 3842 .family = PF_INET, 3843 .owner = THIS_MODULE, 3844 .release = inet_release, 3845 .bind = mptcp_bind, 3846 .connect = inet_stream_connect, 3847 .socketpair = sock_no_socketpair, 3848 .accept = mptcp_stream_accept, 3849 .getname = inet_getname, 3850 .poll = mptcp_poll, 3851 .ioctl = inet_ioctl, 3852 .gettstamp = sock_gettstamp, 3853 .listen = mptcp_listen, 3854 .shutdown = inet_shutdown, 3855 .setsockopt = sock_common_setsockopt, 3856 .getsockopt = sock_common_getsockopt, 3857 .sendmsg = inet_sendmsg, 3858 .recvmsg = inet_recvmsg, 3859 .mmap = sock_no_mmap, 3860 .sendpage = inet_sendpage, 3861 }; 3862 3863 static struct inet_protosw mptcp_protosw = { 3864 .type = SOCK_STREAM, 3865 .protocol = IPPROTO_MPTCP, 3866 .prot = &mptcp_prot, 3867 .ops = &mptcp_stream_ops, 3868 .flags = INET_PROTOSW_ICSK, 3869 }; 3870 3871 static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3872 { 3873 struct mptcp_delegated_action *delegated; 3874 struct mptcp_subflow_context *subflow; 3875 int work_done = 0; 3876 3877 delegated = container_of(napi, struct mptcp_delegated_action, napi); 3878 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3879 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3880 3881 bh_lock_sock_nested(ssk); 3882 if (!sock_owned_by_user(ssk) && 3883 mptcp_subflow_has_delegated_action(subflow)) 3884 mptcp_subflow_process_delegated(ssk); 3885 /* ... elsewhere tcp_release_cb_override already processed 3886 * the action or will do at next release_sock(). 3887 * In both case must dequeue the subflow here - on the same 3888 * CPU that scheduled it. 3889 */ 3890 bh_unlock_sock(ssk); 3891 sock_put(ssk); 3892 3893 if (++work_done == budget) 3894 return budget; 3895 } 3896 3897 /* always provide a 0 'work_done' argument, so that napi_complete_done 3898 * will not try accessing the NULL napi->dev ptr 3899 */ 3900 napi_complete_done(napi, 0); 3901 return work_done; 3902 } 3903 3904 void __init mptcp_proto_init(void) 3905 { 3906 struct mptcp_delegated_action *delegated; 3907 int cpu; 3908 3909 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 3910 3911 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3912 panic("Failed to allocate MPTCP pcpu counter\n"); 3913 3914 init_dummy_netdev(&mptcp_napi_dev); 3915 for_each_possible_cpu(cpu) { 3916 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3917 INIT_LIST_HEAD(&delegated->head); 3918 netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi, 3919 mptcp_napi_poll); 3920 napi_enable(&delegated->napi); 3921 } 3922 3923 mptcp_subflow_init(); 3924 mptcp_pm_init(); 3925 mptcp_token_init(); 3926 3927 if (proto_register(&mptcp_prot, 1) != 0) 3928 panic("Failed to register MPTCP proto.\n"); 3929 3930 inet_register_protosw(&mptcp_protosw); 3931 3932 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3933 } 3934 3935 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3936 static const struct proto_ops mptcp_v6_stream_ops = { 3937 .family = PF_INET6, 3938 .owner = THIS_MODULE, 3939 .release = inet6_release, 3940 .bind = mptcp_bind, 3941 .connect = inet_stream_connect, 3942 .socketpair = sock_no_socketpair, 3943 .accept = mptcp_stream_accept, 3944 .getname = inet6_getname, 3945 .poll = mptcp_poll, 3946 .ioctl = inet6_ioctl, 3947 .gettstamp = sock_gettstamp, 3948 .listen = mptcp_listen, 3949 .shutdown = inet_shutdown, 3950 .setsockopt = sock_common_setsockopt, 3951 .getsockopt = sock_common_getsockopt, 3952 .sendmsg = inet6_sendmsg, 3953 .recvmsg = inet6_recvmsg, 3954 .mmap = sock_no_mmap, 3955 .sendpage = inet_sendpage, 3956 #ifdef CONFIG_COMPAT 3957 .compat_ioctl = inet6_compat_ioctl, 3958 #endif 3959 }; 3960 3961 static struct proto mptcp_v6_prot; 3962 3963 static struct inet_protosw mptcp_v6_protosw = { 3964 .type = SOCK_STREAM, 3965 .protocol = IPPROTO_MPTCP, 3966 .prot = &mptcp_v6_prot, 3967 .ops = &mptcp_v6_stream_ops, 3968 .flags = INET_PROTOSW_ICSK, 3969 }; 3970 3971 int __init mptcp_proto_v6_init(void) 3972 { 3973 int err; 3974 3975 mptcp_v6_prot = mptcp_prot; 3976 strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3977 mptcp_v6_prot.slab = NULL; 3978 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3979 3980 err = proto_register(&mptcp_v6_prot, 1); 3981 if (err) 3982 return err; 3983 3984 err = inet6_register_protosw(&mptcp_v6_protosw); 3985 if (err) 3986 proto_unregister(&mptcp_v6_prot); 3987 3988 return err; 3989 } 3990 #endif 3991