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