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