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