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