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