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