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_sockopt_sync_locked(msk, ssk); 829 return true; 830 } 831 832 static void __mptcp_flush_join_list(struct sock *sk) 833 { 834 struct mptcp_subflow_context *tmp, *subflow; 835 struct mptcp_sock *msk = mptcp_sk(sk); 836 837 list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) { 838 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 839 bool slow = lock_sock_fast(ssk); 840 841 list_move_tail(&subflow->node, &msk->conn_list); 842 if (!__mptcp_finish_join(msk, ssk)) 843 mptcp_subflow_reset(ssk); 844 unlock_sock_fast(ssk, slow); 845 } 846 } 847 848 static bool mptcp_timer_pending(struct sock *sk) 849 { 850 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 851 } 852 853 static void mptcp_reset_timer(struct sock *sk) 854 { 855 struct inet_connection_sock *icsk = inet_csk(sk); 856 unsigned long tout; 857 858 /* prevent rescheduling on close */ 859 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 860 return; 861 862 tout = mptcp_sk(sk)->timer_ival; 863 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 864 } 865 866 bool mptcp_schedule_work(struct sock *sk) 867 { 868 if (inet_sk_state_load(sk) != TCP_CLOSE && 869 schedule_work(&mptcp_sk(sk)->work)) { 870 /* each subflow already holds a reference to the sk, and the 871 * workqueue is invoked by a subflow, so sk can't go away here. 872 */ 873 sock_hold(sk); 874 return true; 875 } 876 return false; 877 } 878 879 void mptcp_subflow_eof(struct sock *sk) 880 { 881 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 882 mptcp_schedule_work(sk); 883 } 884 885 static void mptcp_check_for_eof(struct mptcp_sock *msk) 886 { 887 struct mptcp_subflow_context *subflow; 888 struct sock *sk = (struct sock *)msk; 889 int receivers = 0; 890 891 mptcp_for_each_subflow(msk, subflow) 892 receivers += !subflow->rx_eof; 893 if (receivers) 894 return; 895 896 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 897 /* hopefully temporary hack: propagate shutdown status 898 * to msk, when all subflows agree on it 899 */ 900 sk->sk_shutdown |= RCV_SHUTDOWN; 901 902 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 903 sk->sk_data_ready(sk); 904 } 905 906 switch (sk->sk_state) { 907 case TCP_ESTABLISHED: 908 inet_sk_state_store(sk, TCP_CLOSE_WAIT); 909 break; 910 case TCP_FIN_WAIT1: 911 inet_sk_state_store(sk, TCP_CLOSING); 912 break; 913 case TCP_FIN_WAIT2: 914 inet_sk_state_store(sk, TCP_CLOSE); 915 break; 916 default: 917 return; 918 } 919 mptcp_close_wake_up(sk); 920 } 921 922 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 923 { 924 struct mptcp_subflow_context *subflow; 925 926 msk_owned_by_me(msk); 927 928 mptcp_for_each_subflow(msk, subflow) { 929 if (READ_ONCE(subflow->data_avail)) 930 return mptcp_subflow_tcp_sock(subflow); 931 } 932 933 return NULL; 934 } 935 936 static bool mptcp_skb_can_collapse_to(u64 write_seq, 937 const struct sk_buff *skb, 938 const struct mptcp_ext *mpext) 939 { 940 if (!tcp_skb_can_collapse_to(skb)) 941 return false; 942 943 /* can collapse only if MPTCP level sequence is in order and this 944 * mapping has not been xmitted yet 945 */ 946 return mpext && mpext->data_seq + mpext->data_len == write_seq && 947 !mpext->frozen; 948 } 949 950 /* we can append data to the given data frag if: 951 * - there is space available in the backing page_frag 952 * - the data frag tail matches the current page_frag free offset 953 * - the data frag end sequence number matches the current write seq 954 */ 955 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 956 const struct page_frag *pfrag, 957 const struct mptcp_data_frag *df) 958 { 959 return df && pfrag->page == df->page && 960 pfrag->size - pfrag->offset > 0 && 961 pfrag->offset == (df->offset + df->data_len) && 962 df->data_seq + df->data_len == msk->write_seq; 963 } 964 965 static void dfrag_uncharge(struct sock *sk, int len) 966 { 967 sk_mem_uncharge(sk, len); 968 sk_wmem_queued_add(sk, -len); 969 } 970 971 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 972 { 973 int len = dfrag->data_len + dfrag->overhead; 974 975 list_del(&dfrag->list); 976 dfrag_uncharge(sk, len); 977 put_page(dfrag->page); 978 } 979 980 static void __mptcp_clean_una(struct sock *sk) 981 { 982 struct mptcp_sock *msk = mptcp_sk(sk); 983 struct mptcp_data_frag *dtmp, *dfrag; 984 u64 snd_una; 985 986 /* on fallback we just need to ignore snd_una, as this is really 987 * plain TCP 988 */ 989 if (__mptcp_check_fallback(msk)) 990 msk->snd_una = READ_ONCE(msk->snd_nxt); 991 992 snd_una = msk->snd_una; 993 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 994 if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 995 break; 996 997 if (unlikely(dfrag == msk->first_pending)) { 998 /* in recovery mode can see ack after the current snd head */ 999 if (WARN_ON_ONCE(!msk->recovery)) 1000 break; 1001 1002 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1003 } 1004 1005 dfrag_clear(sk, dfrag); 1006 } 1007 1008 dfrag = mptcp_rtx_head(sk); 1009 if (dfrag && after64(snd_una, dfrag->data_seq)) { 1010 u64 delta = snd_una - dfrag->data_seq; 1011 1012 /* prevent wrap around in recovery mode */ 1013 if (unlikely(delta > dfrag->already_sent)) { 1014 if (WARN_ON_ONCE(!msk->recovery)) 1015 goto out; 1016 if (WARN_ON_ONCE(delta > dfrag->data_len)) 1017 goto out; 1018 dfrag->already_sent += delta - dfrag->already_sent; 1019 } 1020 1021 dfrag->data_seq += delta; 1022 dfrag->offset += delta; 1023 dfrag->data_len -= delta; 1024 dfrag->already_sent -= delta; 1025 1026 dfrag_uncharge(sk, delta); 1027 } 1028 1029 /* all retransmitted data acked, recovery completed */ 1030 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 1031 msk->recovery = false; 1032 1033 out: 1034 if (snd_una == READ_ONCE(msk->snd_nxt) && 1035 snd_una == READ_ONCE(msk->write_seq)) { 1036 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 1037 mptcp_stop_timer(sk); 1038 } else { 1039 mptcp_reset_timer(sk); 1040 } 1041 } 1042 1043 static void __mptcp_clean_una_wakeup(struct sock *sk) 1044 { 1045 lockdep_assert_held_once(&sk->sk_lock.slock); 1046 1047 __mptcp_clean_una(sk); 1048 mptcp_write_space(sk); 1049 } 1050 1051 static void mptcp_clean_una_wakeup(struct sock *sk) 1052 { 1053 mptcp_data_lock(sk); 1054 __mptcp_clean_una_wakeup(sk); 1055 mptcp_data_unlock(sk); 1056 } 1057 1058 static void mptcp_enter_memory_pressure(struct sock *sk) 1059 { 1060 struct mptcp_subflow_context *subflow; 1061 struct mptcp_sock *msk = mptcp_sk(sk); 1062 bool first = true; 1063 1064 sk_stream_moderate_sndbuf(sk); 1065 mptcp_for_each_subflow(msk, subflow) { 1066 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1067 1068 if (first) 1069 tcp_enter_memory_pressure(ssk); 1070 sk_stream_moderate_sndbuf(ssk); 1071 first = false; 1072 } 1073 } 1074 1075 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1076 * data 1077 */ 1078 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1079 { 1080 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1081 pfrag, sk->sk_allocation))) 1082 return true; 1083 1084 mptcp_enter_memory_pressure(sk); 1085 return false; 1086 } 1087 1088 static struct mptcp_data_frag * 1089 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 1090 int orig_offset) 1091 { 1092 int offset = ALIGN(orig_offset, sizeof(long)); 1093 struct mptcp_data_frag *dfrag; 1094 1095 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 1096 dfrag->data_len = 0; 1097 dfrag->data_seq = msk->write_seq; 1098 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 1099 dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1100 dfrag->already_sent = 0; 1101 dfrag->page = pfrag->page; 1102 1103 return dfrag; 1104 } 1105 1106 struct mptcp_sendmsg_info { 1107 int mss_now; 1108 int size_goal; 1109 u16 limit; 1110 u16 sent; 1111 unsigned int flags; 1112 bool data_lock_held; 1113 }; 1114 1115 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk, 1116 u64 data_seq, int avail_size) 1117 { 1118 u64 window_end = mptcp_wnd_end(msk); 1119 u64 mptcp_snd_wnd; 1120 1121 if (__mptcp_check_fallback(msk)) 1122 return avail_size; 1123 1124 mptcp_snd_wnd = window_end - data_seq; 1125 avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size); 1126 1127 if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) { 1128 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd); 1129 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED); 1130 } 1131 1132 return avail_size; 1133 } 1134 1135 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1136 { 1137 struct skb_ext *mpext = __skb_ext_alloc(gfp); 1138 1139 if (!mpext) 1140 return false; 1141 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1142 return true; 1143 } 1144 1145 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1146 { 1147 struct sk_buff *skb; 1148 1149 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1150 if (likely(skb)) { 1151 if (likely(__mptcp_add_ext(skb, gfp))) { 1152 skb_reserve(skb, MAX_TCP_HEADER); 1153 skb->ip_summed = CHECKSUM_PARTIAL; 1154 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 1155 return skb; 1156 } 1157 __kfree_skb(skb); 1158 } else { 1159 mptcp_enter_memory_pressure(sk); 1160 } 1161 return NULL; 1162 } 1163 1164 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1165 { 1166 struct sk_buff *skb; 1167 1168 skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1169 if (!skb) 1170 return NULL; 1171 1172 if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1173 tcp_skb_entail(ssk, skb); 1174 return skb; 1175 } 1176 tcp_skb_tsorted_anchor_cleanup(skb); 1177 kfree_skb(skb); 1178 return NULL; 1179 } 1180 1181 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held) 1182 { 1183 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation; 1184 1185 return __mptcp_alloc_tx_skb(sk, ssk, gfp); 1186 } 1187 1188 /* note: this always recompute the csum on the whole skb, even 1189 * if we just appended a single frag. More status info needed 1190 */ 1191 static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1192 { 1193 struct mptcp_ext *mpext = mptcp_get_ext(skb); 1194 __wsum csum = ~csum_unfold(mpext->csum); 1195 int offset = skb->len - added; 1196 1197 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1198 } 1199 1200 static void mptcp_update_infinite_map(struct mptcp_sock *msk, 1201 struct sock *ssk, 1202 struct mptcp_ext *mpext) 1203 { 1204 if (!mpext) 1205 return; 1206 1207 mpext->infinite_map = 1; 1208 mpext->data_len = 0; 1209 1210 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX); 1211 mptcp_subflow_ctx(ssk)->send_infinite_map = 0; 1212 pr_fallback(msk); 1213 mptcp_do_fallback(ssk); 1214 } 1215 1216 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1217 struct mptcp_data_frag *dfrag, 1218 struct mptcp_sendmsg_info *info) 1219 { 1220 u64 data_seq = dfrag->data_seq + info->sent; 1221 int offset = dfrag->offset + info->sent; 1222 struct mptcp_sock *msk = mptcp_sk(sk); 1223 bool zero_window_probe = false; 1224 struct mptcp_ext *mpext = NULL; 1225 bool can_coalesce = false; 1226 bool reuse_skb = true; 1227 struct sk_buff *skb; 1228 size_t copy; 1229 int i; 1230 1231 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1232 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 1233 1234 if (WARN_ON_ONCE(info->sent > info->limit || 1235 info->limit > dfrag->data_len)) 1236 return 0; 1237 1238 if (unlikely(!__tcp_can_send(ssk))) 1239 return -EAGAIN; 1240 1241 /* compute send limit */ 1242 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1243 copy = info->size_goal; 1244 1245 skb = tcp_write_queue_tail(ssk); 1246 if (skb && copy > skb->len) { 1247 /* Limit the write to the size available in the 1248 * current skb, if any, so that we create at most a new skb. 1249 * Explicitly tells TCP internals to avoid collapsing on later 1250 * queue management operation, to avoid breaking the ext <-> 1251 * SSN association set here 1252 */ 1253 mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1254 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) { 1255 TCP_SKB_CB(skb)->eor = 1; 1256 goto alloc_skb; 1257 } 1258 1259 i = skb_shinfo(skb)->nr_frags; 1260 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset); 1261 if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) { 1262 tcp_mark_push(tcp_sk(ssk), skb); 1263 goto alloc_skb; 1264 } 1265 1266 copy -= skb->len; 1267 } else { 1268 alloc_skb: 1269 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held); 1270 if (!skb) 1271 return -ENOMEM; 1272 1273 i = skb_shinfo(skb)->nr_frags; 1274 reuse_skb = false; 1275 mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1276 } 1277 1278 /* Zero window and all data acked? Probe. */ 1279 copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy); 1280 if (copy == 0) { 1281 u64 snd_una = READ_ONCE(msk->snd_una); 1282 1283 if (snd_una != msk->snd_nxt) { 1284 tcp_remove_empty_skb(ssk); 1285 return 0; 1286 } 1287 1288 zero_window_probe = true; 1289 data_seq = snd_una - 1; 1290 copy = 1; 1291 1292 /* all mptcp-level data is acked, no skbs should be present into the 1293 * ssk write queue 1294 */ 1295 WARN_ON_ONCE(reuse_skb); 1296 } 1297 1298 copy = min_t(size_t, copy, info->limit - info->sent); 1299 if (!sk_wmem_schedule(ssk, copy)) { 1300 tcp_remove_empty_skb(ssk); 1301 return -ENOMEM; 1302 } 1303 1304 if (can_coalesce) { 1305 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1306 } else { 1307 get_page(dfrag->page); 1308 skb_fill_page_desc(skb, i, dfrag->page, offset, copy); 1309 } 1310 1311 skb->len += copy; 1312 skb->data_len += copy; 1313 skb->truesize += copy; 1314 sk_wmem_queued_add(ssk, copy); 1315 sk_mem_charge(ssk, copy); 1316 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy); 1317 TCP_SKB_CB(skb)->end_seq += copy; 1318 tcp_skb_pcount_set(skb, 0); 1319 1320 /* on skb reuse we just need to update the DSS len */ 1321 if (reuse_skb) { 1322 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH; 1323 mpext->data_len += copy; 1324 WARN_ON_ONCE(zero_window_probe); 1325 goto out; 1326 } 1327 1328 memset(mpext, 0, sizeof(*mpext)); 1329 mpext->data_seq = data_seq; 1330 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1331 mpext->data_len = copy; 1332 mpext->use_map = 1; 1333 mpext->dsn64 = 1; 1334 1335 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 1336 mpext->data_seq, mpext->subflow_seq, mpext->data_len, 1337 mpext->dsn64); 1338 1339 if (zero_window_probe) { 1340 mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1341 mpext->frozen = 1; 1342 if (READ_ONCE(msk->csum_enabled)) 1343 mptcp_update_data_checksum(skb, copy); 1344 tcp_push_pending_frames(ssk); 1345 return 0; 1346 } 1347 out: 1348 if (READ_ONCE(msk->csum_enabled)) 1349 mptcp_update_data_checksum(skb, copy); 1350 if (mptcp_subflow_ctx(ssk)->send_infinite_map) 1351 mptcp_update_infinite_map(msk, ssk, mpext); 1352 trace_mptcp_sendmsg_frag(mpext); 1353 mptcp_subflow_ctx(ssk)->rel_write_seq += copy; 1354 return copy; 1355 } 1356 1357 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1358 sizeof(struct tcphdr) - \ 1359 MAX_TCP_OPTION_SPACE - \ 1360 sizeof(struct ipv6hdr) - \ 1361 sizeof(struct frag_hdr)) 1362 1363 struct subflow_send_info { 1364 struct sock *ssk; 1365 u64 linger_time; 1366 }; 1367 1368 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1369 { 1370 if (!subflow->stale) 1371 return; 1372 1373 subflow->stale = 0; 1374 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1375 } 1376 1377 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1378 { 1379 if (unlikely(subflow->stale)) { 1380 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1381 1382 if (subflow->stale_rcv_tstamp == rcv_tstamp) 1383 return false; 1384 1385 mptcp_subflow_set_active(subflow); 1386 } 1387 return __mptcp_subflow_active(subflow); 1388 } 1389 1390 #define SSK_MODE_ACTIVE 0 1391 #define SSK_MODE_BACKUP 1 1392 #define SSK_MODE_MAX 2 1393 1394 /* implement the mptcp packet scheduler; 1395 * returns the subflow that will transmit the next DSS 1396 * additionally updates the rtx timeout 1397 */ 1398 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1399 { 1400 struct subflow_send_info send_info[SSK_MODE_MAX]; 1401 struct mptcp_subflow_context *subflow; 1402 struct sock *sk = (struct sock *)msk; 1403 u32 pace, burst, wmem; 1404 int i, nr_active = 0; 1405 struct sock *ssk; 1406 u64 linger_time; 1407 long tout = 0; 1408 1409 msk_owned_by_me(msk); 1410 1411 if (__mptcp_check_fallback(msk)) { 1412 if (!msk->first) 1413 return NULL; 1414 return __tcp_can_send(msk->first) && 1415 sk_stream_memory_free(msk->first) ? msk->first : NULL; 1416 } 1417 1418 /* re-use last subflow, if the burst allow that */ 1419 if (msk->last_snd && msk->snd_burst > 0 && 1420 sk_stream_memory_free(msk->last_snd) && 1421 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 1422 mptcp_set_timeout(sk); 1423 return msk->last_snd; 1424 } 1425 1426 /* pick the subflow with the lower wmem/wspace ratio */ 1427 for (i = 0; i < SSK_MODE_MAX; ++i) { 1428 send_info[i].ssk = NULL; 1429 send_info[i].linger_time = -1; 1430 } 1431 1432 mptcp_for_each_subflow(msk, subflow) { 1433 trace_mptcp_subflow_get_send(subflow); 1434 ssk = mptcp_subflow_tcp_sock(subflow); 1435 if (!mptcp_subflow_active(subflow)) 1436 continue; 1437 1438 tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1439 nr_active += !subflow->backup; 1440 pace = subflow->avg_pacing_rate; 1441 if (unlikely(!pace)) { 1442 /* init pacing rate from socket */ 1443 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate); 1444 pace = subflow->avg_pacing_rate; 1445 if (!pace) 1446 continue; 1447 } 1448 1449 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace); 1450 if (linger_time < send_info[subflow->backup].linger_time) { 1451 send_info[subflow->backup].ssk = ssk; 1452 send_info[subflow->backup].linger_time = linger_time; 1453 } 1454 } 1455 __mptcp_set_timeout(sk, tout); 1456 1457 /* pick the best backup if no other subflow is active */ 1458 if (!nr_active) 1459 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk; 1460 1461 /* According to the blest algorithm, to avoid HoL blocking for the 1462 * faster flow, we need to: 1463 * - estimate the faster flow linger time 1464 * - use the above to estimate the amount of byte transferred 1465 * by the faster flow 1466 * - check that the amount of queued data is greter than the above, 1467 * otherwise do not use the picked, slower, subflow 1468 * We select the subflow with the shorter estimated time to flush 1469 * the queued mem, which basically ensure the above. We just need 1470 * to check that subflow has a non empty cwin. 1471 */ 1472 ssk = send_info[SSK_MODE_ACTIVE].ssk; 1473 if (!ssk || !sk_stream_memory_free(ssk)) 1474 return NULL; 1475 1476 burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt); 1477 wmem = READ_ONCE(ssk->sk_wmem_queued); 1478 if (!burst) { 1479 msk->last_snd = NULL; 1480 return ssk; 1481 } 1482 1483 subflow = mptcp_subflow_ctx(ssk); 1484 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem + 1485 READ_ONCE(ssk->sk_pacing_rate) * burst, 1486 burst + wmem); 1487 msk->last_snd = ssk; 1488 msk->snd_burst = burst; 1489 return ssk; 1490 } 1491 1492 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info) 1493 { 1494 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1495 release_sock(ssk); 1496 } 1497 1498 static void mptcp_update_post_push(struct mptcp_sock *msk, 1499 struct mptcp_data_frag *dfrag, 1500 u32 sent) 1501 { 1502 u64 snd_nxt_new = dfrag->data_seq; 1503 1504 dfrag->already_sent += sent; 1505 1506 msk->snd_burst -= sent; 1507 1508 snd_nxt_new += dfrag->already_sent; 1509 1510 /* snd_nxt_new can be smaller than snd_nxt in case mptcp 1511 * is recovering after a failover. In that event, this re-sends 1512 * old segments. 1513 * 1514 * Thus compute snd_nxt_new candidate based on 1515 * the dfrag->data_seq that was sent and the data 1516 * that has been handed to the subflow for transmission 1517 * and skip update in case it was old dfrag. 1518 */ 1519 if (likely(after64(snd_nxt_new, msk->snd_nxt))) 1520 msk->snd_nxt = snd_nxt_new; 1521 } 1522 1523 void mptcp_check_and_set_pending(struct sock *sk) 1524 { 1525 if (mptcp_send_head(sk)) 1526 mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING); 1527 } 1528 1529 void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1530 { 1531 struct sock *prev_ssk = NULL, *ssk = NULL; 1532 struct mptcp_sock *msk = mptcp_sk(sk); 1533 struct mptcp_sendmsg_info info = { 1534 .flags = flags, 1535 }; 1536 bool do_check_data_fin = false; 1537 struct mptcp_data_frag *dfrag; 1538 int len; 1539 1540 while ((dfrag = mptcp_send_head(sk))) { 1541 info.sent = dfrag->already_sent; 1542 info.limit = dfrag->data_len; 1543 len = dfrag->data_len - dfrag->already_sent; 1544 while (len > 0) { 1545 int ret = 0; 1546 1547 prev_ssk = ssk; 1548 ssk = mptcp_subflow_get_send(msk); 1549 1550 /* First check. If the ssk has changed since 1551 * the last round, release prev_ssk 1552 */ 1553 if (ssk != prev_ssk && prev_ssk) 1554 mptcp_push_release(prev_ssk, &info); 1555 if (!ssk) 1556 goto out; 1557 1558 /* Need to lock the new subflow only if different 1559 * from the previous one, otherwise we are still 1560 * helding the relevant lock 1561 */ 1562 if (ssk != prev_ssk) 1563 lock_sock(ssk); 1564 1565 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1566 if (ret <= 0) { 1567 if (ret == -EAGAIN) 1568 continue; 1569 mptcp_push_release(ssk, &info); 1570 goto out; 1571 } 1572 1573 do_check_data_fin = true; 1574 info.sent += ret; 1575 len -= ret; 1576 1577 mptcp_update_post_push(msk, dfrag, ret); 1578 } 1579 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1580 } 1581 1582 /* at this point we held the socket lock for the last subflow we used */ 1583 if (ssk) 1584 mptcp_push_release(ssk, &info); 1585 1586 out: 1587 /* ensure the rtx timer is running */ 1588 if (!mptcp_timer_pending(sk)) 1589 mptcp_reset_timer(sk); 1590 if (do_check_data_fin) 1591 __mptcp_check_send_data_fin(sk); 1592 } 1593 1594 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first) 1595 { 1596 struct mptcp_sock *msk = mptcp_sk(sk); 1597 struct mptcp_sendmsg_info info = { 1598 .data_lock_held = true, 1599 }; 1600 struct mptcp_data_frag *dfrag; 1601 struct sock *xmit_ssk; 1602 int len, copied = 0; 1603 1604 info.flags = 0; 1605 while ((dfrag = mptcp_send_head(sk))) { 1606 info.sent = dfrag->already_sent; 1607 info.limit = dfrag->data_len; 1608 len = dfrag->data_len - dfrag->already_sent; 1609 while (len > 0) { 1610 int ret = 0; 1611 1612 /* check for a different subflow usage only after 1613 * spooling the first chunk of data 1614 */ 1615 xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk); 1616 if (!xmit_ssk) 1617 goto out; 1618 if (xmit_ssk != ssk) { 1619 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), 1620 MPTCP_DELEGATE_SEND); 1621 goto out; 1622 } 1623 1624 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1625 if (ret <= 0) 1626 goto out; 1627 1628 info.sent += ret; 1629 copied += ret; 1630 len -= ret; 1631 first = false; 1632 1633 mptcp_update_post_push(msk, dfrag, ret); 1634 } 1635 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1636 } 1637 1638 out: 1639 /* __mptcp_alloc_tx_skb could have released some wmem and we are 1640 * not going to flush it via release_sock() 1641 */ 1642 if (copied) { 1643 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 1644 info.size_goal); 1645 if (!mptcp_timer_pending(sk)) 1646 mptcp_reset_timer(sk); 1647 1648 if (msk->snd_data_fin_enable && 1649 msk->snd_nxt + 1 == msk->write_seq) 1650 mptcp_schedule_work(sk); 1651 } 1652 } 1653 1654 static void mptcp_set_nospace(struct sock *sk) 1655 { 1656 /* enable autotune */ 1657 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1658 1659 /* will be cleared on avail space */ 1660 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags); 1661 } 1662 1663 static int mptcp_disconnect(struct sock *sk, int flags); 1664 1665 static int mptcp_sendmsg_fastopen(struct sock *sk, struct sock *ssk, struct msghdr *msg, 1666 size_t len, int *copied_syn) 1667 { 1668 unsigned int saved_flags = msg->msg_flags; 1669 struct mptcp_sock *msk = mptcp_sk(sk); 1670 int ret; 1671 1672 lock_sock(ssk); 1673 msg->msg_flags |= MSG_DONTWAIT; 1674 msk->connect_flags = O_NONBLOCK; 1675 msk->fastopening = 1; 1676 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL); 1677 msk->fastopening = 0; 1678 msg->msg_flags = saved_flags; 1679 release_sock(ssk); 1680 1681 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */ 1682 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) { 1683 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name, 1684 msg->msg_namelen, msg->msg_flags, 1); 1685 1686 /* Keep the same behaviour of plain TCP: zero the copied bytes in 1687 * case of any error, except timeout or signal 1688 */ 1689 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR) 1690 *copied_syn = 0; 1691 } else if (ret && ret != -EINPROGRESS) { 1692 mptcp_disconnect(sk, 0); 1693 } 1694 1695 return ret; 1696 } 1697 1698 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1699 { 1700 struct mptcp_sock *msk = mptcp_sk(sk); 1701 struct page_frag *pfrag; 1702 struct socket *ssock; 1703 size_t copied = 0; 1704 int ret = 0; 1705 long timeo; 1706 1707 /* silently ignore everything else */ 1708 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN; 1709 1710 lock_sock(sk); 1711 1712 ssock = __mptcp_nmpc_socket(msk); 1713 if (unlikely(ssock && (inet_sk(ssock->sk)->defer_connect || 1714 msg->msg_flags & MSG_FASTOPEN))) { 1715 int copied_syn = 0; 1716 1717 ret = mptcp_sendmsg_fastopen(sk, ssock->sk, msg, len, &copied_syn); 1718 copied += copied_syn; 1719 if (ret == -EINPROGRESS && copied_syn > 0) 1720 goto out; 1721 else if (ret) 1722 goto do_error; 1723 } 1724 1725 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 1726 1727 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 1728 ret = sk_stream_wait_connect(sk, &timeo); 1729 if (ret) 1730 goto do_error; 1731 } 1732 1733 ret = -EPIPE; 1734 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))) 1735 goto do_error; 1736 1737 pfrag = sk_page_frag(sk); 1738 1739 while (msg_data_left(msg)) { 1740 int total_ts, frag_truesize = 0; 1741 struct mptcp_data_frag *dfrag; 1742 bool dfrag_collapsed; 1743 size_t psize, offset; 1744 1745 /* reuse tail pfrag, if possible, or carve a new one from the 1746 * page allocator 1747 */ 1748 dfrag = mptcp_pending_tail(sk); 1749 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 1750 if (!dfrag_collapsed) { 1751 if (!sk_stream_memory_free(sk)) 1752 goto wait_for_memory; 1753 1754 if (!mptcp_page_frag_refill(sk, pfrag)) 1755 goto wait_for_memory; 1756 1757 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset); 1758 frag_truesize = dfrag->overhead; 1759 } 1760 1761 /* we do not bound vs wspace, to allow a single packet. 1762 * memory accounting will prevent execessive memory usage 1763 * anyway 1764 */ 1765 offset = dfrag->offset + dfrag->data_len; 1766 psize = pfrag->size - offset; 1767 psize = min_t(size_t, psize, msg_data_left(msg)); 1768 total_ts = psize + frag_truesize; 1769 1770 if (!sk_wmem_schedule(sk, total_ts)) 1771 goto wait_for_memory; 1772 1773 if (copy_page_from_iter(dfrag->page, offset, psize, 1774 &msg->msg_iter) != psize) { 1775 ret = -EFAULT; 1776 goto do_error; 1777 } 1778 1779 /* data successfully copied into the write queue */ 1780 sk->sk_forward_alloc -= total_ts; 1781 copied += psize; 1782 dfrag->data_len += psize; 1783 frag_truesize += psize; 1784 pfrag->offset += frag_truesize; 1785 WRITE_ONCE(msk->write_seq, msk->write_seq + psize); 1786 1787 /* charge data on mptcp pending queue to the msk socket 1788 * Note: we charge such data both to sk and ssk 1789 */ 1790 sk_wmem_queued_add(sk, frag_truesize); 1791 if (!dfrag_collapsed) { 1792 get_page(dfrag->page); 1793 list_add_tail(&dfrag->list, &msk->rtx_queue); 1794 if (!msk->first_pending) 1795 WRITE_ONCE(msk->first_pending, dfrag); 1796 } 1797 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk, 1798 dfrag->data_seq, dfrag->data_len, dfrag->already_sent, 1799 !dfrag_collapsed); 1800 1801 continue; 1802 1803 wait_for_memory: 1804 mptcp_set_nospace(sk); 1805 __mptcp_push_pending(sk, msg->msg_flags); 1806 ret = sk_stream_wait_memory(sk, &timeo); 1807 if (ret) 1808 goto do_error; 1809 } 1810 1811 if (copied) 1812 __mptcp_push_pending(sk, msg->msg_flags); 1813 1814 out: 1815 release_sock(sk); 1816 return copied; 1817 1818 do_error: 1819 if (copied) 1820 goto out; 1821 1822 copied = sk_stream_error(sk, msg->msg_flags, ret); 1823 goto out; 1824 } 1825 1826 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 1827 struct msghdr *msg, 1828 size_t len, int flags, 1829 struct scm_timestamping_internal *tss, 1830 int *cmsg_flags) 1831 { 1832 struct sk_buff *skb, *tmp; 1833 int copied = 0; 1834 1835 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) { 1836 u32 offset = MPTCP_SKB_CB(skb)->offset; 1837 u32 data_len = skb->len - offset; 1838 u32 count = min_t(size_t, len - copied, data_len); 1839 int err; 1840 1841 if (!(flags & MSG_TRUNC)) { 1842 err = skb_copy_datagram_msg(skb, offset, msg, count); 1843 if (unlikely(err < 0)) { 1844 if (!copied) 1845 return err; 1846 break; 1847 } 1848 } 1849 1850 if (MPTCP_SKB_CB(skb)->has_rxtstamp) { 1851 tcp_update_recv_tstamps(skb, tss); 1852 *cmsg_flags |= MPTCP_CMSG_TS; 1853 } 1854 1855 copied += count; 1856 1857 if (count < data_len) { 1858 if (!(flags & MSG_PEEK)) { 1859 MPTCP_SKB_CB(skb)->offset += count; 1860 MPTCP_SKB_CB(skb)->map_seq += count; 1861 } 1862 break; 1863 } 1864 1865 if (!(flags & MSG_PEEK)) { 1866 /* we will bulk release the skb memory later */ 1867 skb->destructor = NULL; 1868 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize); 1869 __skb_unlink(skb, &msk->receive_queue); 1870 __kfree_skb(skb); 1871 } 1872 1873 if (copied >= len) 1874 break; 1875 } 1876 1877 return copied; 1878 } 1879 1880 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information. 1881 * 1882 * Only difference: Use highest rtt estimate of the subflows in use. 1883 */ 1884 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied) 1885 { 1886 struct mptcp_subflow_context *subflow; 1887 struct sock *sk = (struct sock *)msk; 1888 u32 time, advmss = 1; 1889 u64 rtt_us, mstamp; 1890 1891 msk_owned_by_me(msk); 1892 1893 if (copied <= 0) 1894 return; 1895 1896 msk->rcvq_space.copied += copied; 1897 1898 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC); 1899 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time); 1900 1901 rtt_us = msk->rcvq_space.rtt_us; 1902 if (rtt_us && time < (rtt_us >> 3)) 1903 return; 1904 1905 rtt_us = 0; 1906 mptcp_for_each_subflow(msk, subflow) { 1907 const struct tcp_sock *tp; 1908 u64 sf_rtt_us; 1909 u32 sf_advmss; 1910 1911 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow)); 1912 1913 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us); 1914 sf_advmss = READ_ONCE(tp->advmss); 1915 1916 rtt_us = max(sf_rtt_us, rtt_us); 1917 advmss = max(sf_advmss, advmss); 1918 } 1919 1920 msk->rcvq_space.rtt_us = rtt_us; 1921 if (time < (rtt_us >> 3) || rtt_us == 0) 1922 return; 1923 1924 if (msk->rcvq_space.copied <= msk->rcvq_space.space) 1925 goto new_measure; 1926 1927 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && 1928 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 1929 int rcvmem, rcvbuf; 1930 u64 rcvwin, grow; 1931 1932 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss; 1933 1934 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space); 1935 1936 do_div(grow, msk->rcvq_space.space); 1937 rcvwin += (grow << 1); 1938 1939 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER); 1940 while (tcp_win_from_space(sk, rcvmem) < advmss) 1941 rcvmem += 128; 1942 1943 do_div(rcvwin, advmss); 1944 rcvbuf = min_t(u64, rcvwin * rcvmem, 1945 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 1946 1947 if (rcvbuf > sk->sk_rcvbuf) { 1948 u32 window_clamp; 1949 1950 window_clamp = tcp_win_from_space(sk, rcvbuf); 1951 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); 1952 1953 /* Make subflows follow along. If we do not do this, we 1954 * get drops at subflow level if skbs can't be moved to 1955 * the mptcp rx queue fast enough (announced rcv_win can 1956 * exceed ssk->sk_rcvbuf). 1957 */ 1958 mptcp_for_each_subflow(msk, subflow) { 1959 struct sock *ssk; 1960 bool slow; 1961 1962 ssk = mptcp_subflow_tcp_sock(subflow); 1963 slow = lock_sock_fast(ssk); 1964 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf); 1965 tcp_sk(ssk)->window_clamp = window_clamp; 1966 tcp_cleanup_rbuf(ssk, 1); 1967 unlock_sock_fast(ssk, slow); 1968 } 1969 } 1970 } 1971 1972 msk->rcvq_space.space = msk->rcvq_space.copied; 1973 new_measure: 1974 msk->rcvq_space.copied = 0; 1975 msk->rcvq_space.time = mstamp; 1976 } 1977 1978 static void __mptcp_update_rmem(struct sock *sk) 1979 { 1980 struct mptcp_sock *msk = mptcp_sk(sk); 1981 1982 if (!msk->rmem_released) 1983 return; 1984 1985 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc); 1986 mptcp_rmem_uncharge(sk, msk->rmem_released); 1987 WRITE_ONCE(msk->rmem_released, 0); 1988 } 1989 1990 static void __mptcp_splice_receive_queue(struct sock *sk) 1991 { 1992 struct mptcp_sock *msk = mptcp_sk(sk); 1993 1994 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue); 1995 } 1996 1997 static bool __mptcp_move_skbs(struct mptcp_sock *msk) 1998 { 1999 struct sock *sk = (struct sock *)msk; 2000 unsigned int moved = 0; 2001 bool ret, done; 2002 2003 do { 2004 struct sock *ssk = mptcp_subflow_recv_lookup(msk); 2005 bool slowpath; 2006 2007 /* we can have data pending in the subflows only if the msk 2008 * receive buffer was full at subflow_data_ready() time, 2009 * that is an unlikely slow path. 2010 */ 2011 if (likely(!ssk)) 2012 break; 2013 2014 slowpath = lock_sock_fast(ssk); 2015 mptcp_data_lock(sk); 2016 __mptcp_update_rmem(sk); 2017 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 2018 mptcp_data_unlock(sk); 2019 2020 if (unlikely(ssk->sk_err)) 2021 __mptcp_error_report(sk); 2022 unlock_sock_fast(ssk, slowpath); 2023 } while (!done); 2024 2025 /* acquire the data lock only if some input data is pending */ 2026 ret = moved > 0; 2027 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) || 2028 !skb_queue_empty_lockless(&sk->sk_receive_queue)) { 2029 mptcp_data_lock(sk); 2030 __mptcp_update_rmem(sk); 2031 ret |= __mptcp_ofo_queue(msk); 2032 __mptcp_splice_receive_queue(sk); 2033 mptcp_data_unlock(sk); 2034 } 2035 if (ret) 2036 mptcp_check_data_fin((struct sock *)msk); 2037 return !skb_queue_empty(&msk->receive_queue); 2038 } 2039 2040 static unsigned int mptcp_inq_hint(const struct sock *sk) 2041 { 2042 const struct mptcp_sock *msk = mptcp_sk(sk); 2043 const struct sk_buff *skb; 2044 2045 skb = skb_peek(&msk->receive_queue); 2046 if (skb) { 2047 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 2048 2049 if (hint_val >= INT_MAX) 2050 return INT_MAX; 2051 2052 return (unsigned int)hint_val; 2053 } 2054 2055 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) 2056 return 1; 2057 2058 return 0; 2059 } 2060 2061 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2062 int flags, int *addr_len) 2063 { 2064 struct mptcp_sock *msk = mptcp_sk(sk); 2065 struct scm_timestamping_internal tss; 2066 int copied = 0, cmsg_flags = 0; 2067 int target; 2068 long timeo; 2069 2070 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */ 2071 if (unlikely(flags & MSG_ERRQUEUE)) 2072 return inet_recv_error(sk, msg, len, addr_len); 2073 2074 lock_sock(sk); 2075 if (unlikely(sk->sk_state == TCP_LISTEN)) { 2076 copied = -ENOTCONN; 2077 goto out_err; 2078 } 2079 2080 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 2081 2082 len = min_t(size_t, len, INT_MAX); 2083 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 2084 2085 if (unlikely(msk->recvmsg_inq)) 2086 cmsg_flags = MPTCP_CMSG_INQ; 2087 2088 while (copied < len) { 2089 int bytes_read; 2090 2091 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags); 2092 if (unlikely(bytes_read < 0)) { 2093 if (!copied) 2094 copied = bytes_read; 2095 goto out_err; 2096 } 2097 2098 copied += bytes_read; 2099 2100 /* be sure to advertise window change */ 2101 mptcp_cleanup_rbuf(msk); 2102 2103 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk)) 2104 continue; 2105 2106 /* only the master socket status is relevant here. The exit 2107 * conditions mirror closely tcp_recvmsg() 2108 */ 2109 if (copied >= target) 2110 break; 2111 2112 if (copied) { 2113 if (sk->sk_err || 2114 sk->sk_state == TCP_CLOSE || 2115 (sk->sk_shutdown & RCV_SHUTDOWN) || 2116 !timeo || 2117 signal_pending(current)) 2118 break; 2119 } else { 2120 if (sk->sk_err) { 2121 copied = sock_error(sk); 2122 break; 2123 } 2124 2125 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 2126 mptcp_check_for_eof(msk); 2127 2128 if (sk->sk_shutdown & RCV_SHUTDOWN) { 2129 /* race breaker: the shutdown could be after the 2130 * previous receive queue check 2131 */ 2132 if (__mptcp_move_skbs(msk)) 2133 continue; 2134 break; 2135 } 2136 2137 if (sk->sk_state == TCP_CLOSE) { 2138 copied = -ENOTCONN; 2139 break; 2140 } 2141 2142 if (!timeo) { 2143 copied = -EAGAIN; 2144 break; 2145 } 2146 2147 if (signal_pending(current)) { 2148 copied = sock_intr_errno(timeo); 2149 break; 2150 } 2151 } 2152 2153 pr_debug("block timeout %ld", timeo); 2154 sk_wait_data(sk, &timeo, NULL); 2155 } 2156 2157 out_err: 2158 if (cmsg_flags && copied >= 0) { 2159 if (cmsg_flags & MPTCP_CMSG_TS) 2160 tcp_recv_timestamp(msg, sk, &tss); 2161 2162 if (cmsg_flags & MPTCP_CMSG_INQ) { 2163 unsigned int inq = mptcp_inq_hint(sk); 2164 2165 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); 2166 } 2167 } 2168 2169 pr_debug("msk=%p rx queue empty=%d:%d copied=%d", 2170 msk, skb_queue_empty_lockless(&sk->sk_receive_queue), 2171 skb_queue_empty(&msk->receive_queue), copied); 2172 if (!(flags & MSG_PEEK)) 2173 mptcp_rcv_space_adjust(msk, copied); 2174 2175 release_sock(sk); 2176 return copied; 2177 } 2178 2179 static void mptcp_retransmit_timer(struct timer_list *t) 2180 { 2181 struct inet_connection_sock *icsk = from_timer(icsk, t, 2182 icsk_retransmit_timer); 2183 struct sock *sk = &icsk->icsk_inet.sk; 2184 struct mptcp_sock *msk = mptcp_sk(sk); 2185 2186 bh_lock_sock(sk); 2187 if (!sock_owned_by_user(sk)) { 2188 /* we need a process context to retransmit */ 2189 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags)) 2190 mptcp_schedule_work(sk); 2191 } else { 2192 /* delegate our work to tcp_release_cb() */ 2193 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags); 2194 } 2195 bh_unlock_sock(sk); 2196 sock_put(sk); 2197 } 2198 2199 static void mptcp_timeout_timer(struct timer_list *t) 2200 { 2201 struct sock *sk = from_timer(sk, t, sk_timer); 2202 2203 mptcp_schedule_work(sk); 2204 sock_put(sk); 2205 } 2206 2207 /* Find an idle subflow. Return NULL if there is unacked data at tcp 2208 * level. 2209 * 2210 * A backup subflow is returned only if that is the only kind available. 2211 */ 2212 static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk) 2213 { 2214 struct sock *backup = NULL, *pick = NULL; 2215 struct mptcp_subflow_context *subflow; 2216 int min_stale_count = INT_MAX; 2217 2218 msk_owned_by_me(msk); 2219 2220 if (__mptcp_check_fallback(msk)) 2221 return NULL; 2222 2223 mptcp_for_each_subflow(msk, subflow) { 2224 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2225 2226 if (!__mptcp_subflow_active(subflow)) 2227 continue; 2228 2229 /* still data outstanding at TCP level? skip this */ 2230 if (!tcp_rtx_and_write_queues_empty(ssk)) { 2231 mptcp_pm_subflow_chk_stale(msk, ssk); 2232 min_stale_count = min_t(int, min_stale_count, subflow->stale_count); 2233 continue; 2234 } 2235 2236 if (subflow->backup) { 2237 if (!backup) 2238 backup = ssk; 2239 continue; 2240 } 2241 2242 if (!pick) 2243 pick = ssk; 2244 } 2245 2246 if (pick) 2247 return pick; 2248 2249 /* use backup only if there are no progresses anywhere */ 2250 return min_stale_count > 1 ? backup : NULL; 2251 } 2252 2253 static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk) 2254 { 2255 if (msk->subflow) { 2256 iput(SOCK_INODE(msk->subflow)); 2257 msk->subflow = NULL; 2258 } 2259 } 2260 2261 bool __mptcp_retransmit_pending_data(struct sock *sk) 2262 { 2263 struct mptcp_data_frag *cur, *rtx_head; 2264 struct mptcp_sock *msk = mptcp_sk(sk); 2265 2266 if (__mptcp_check_fallback(msk)) 2267 return false; 2268 2269 if (tcp_rtx_and_write_queues_empty(sk)) 2270 return false; 2271 2272 /* the closing socket has some data untransmitted and/or unacked: 2273 * some data in the mptcp rtx queue has not really xmitted yet. 2274 * keep it simple and re-inject the whole mptcp level rtx queue 2275 */ 2276 mptcp_data_lock(sk); 2277 __mptcp_clean_una_wakeup(sk); 2278 rtx_head = mptcp_rtx_head(sk); 2279 if (!rtx_head) { 2280 mptcp_data_unlock(sk); 2281 return false; 2282 } 2283 2284 msk->recovery_snd_nxt = msk->snd_nxt; 2285 msk->recovery = true; 2286 mptcp_data_unlock(sk); 2287 2288 msk->first_pending = rtx_head; 2289 msk->snd_burst = 0; 2290 2291 /* be sure to clear the "sent status" on all re-injected fragments */ 2292 list_for_each_entry(cur, &msk->rtx_queue, list) { 2293 if (!cur->already_sent) 2294 break; 2295 cur->already_sent = 0; 2296 } 2297 2298 return true; 2299 } 2300 2301 /* flags for __mptcp_close_ssk() */ 2302 #define MPTCP_CF_PUSH BIT(1) 2303 #define MPTCP_CF_FASTCLOSE BIT(2) 2304 2305 /* subflow sockets can be either outgoing (connect) or incoming 2306 * (accept). 2307 * 2308 * Outgoing subflows use in-kernel sockets. 2309 * Incoming subflows do not have their own 'struct socket' allocated, 2310 * so we need to use tcp_close() after detaching them from the mptcp 2311 * parent socket. 2312 */ 2313 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2314 struct mptcp_subflow_context *subflow, 2315 unsigned int flags) 2316 { 2317 struct mptcp_sock *msk = mptcp_sk(sk); 2318 bool need_push, dispose_it; 2319 2320 dispose_it = !msk->subflow || ssk != msk->subflow->sk; 2321 if (dispose_it) 2322 list_del(&subflow->node); 2323 2324 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING); 2325 2326 if (flags & MPTCP_CF_FASTCLOSE) { 2327 /* be sure to force the tcp_disconnect() path, 2328 * to generate the egress reset 2329 */ 2330 ssk->sk_lingertime = 0; 2331 sock_set_flag(ssk, SOCK_LINGER); 2332 subflow->send_fastclose = 1; 2333 } 2334 2335 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk); 2336 if (!dispose_it) { 2337 tcp_disconnect(ssk, 0); 2338 msk->subflow->state = SS_UNCONNECTED; 2339 mptcp_subflow_ctx_reset(subflow); 2340 release_sock(ssk); 2341 2342 goto out; 2343 } 2344 2345 subflow->disposable = 1; 2346 2347 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops 2348 * the ssk has been already destroyed, we just need to release the 2349 * reference owned by msk; 2350 */ 2351 if (!inet_csk(ssk)->icsk_ulp_ops) { 2352 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD)); 2353 kfree_rcu(subflow, rcu); 2354 } else if (msk->in_accept_queue && msk->first == ssk) { 2355 /* if the first subflow moved to a close state, e.g. due to 2356 * incoming reset and we reach here before inet_child_forget() 2357 * the TCP stack could later try to close it via 2358 * inet_csk_listen_stop(), or deliver it to the user space via 2359 * accept(). 2360 * We can't delete the subflow - or risk a double free - nor let 2361 * the msk survive - or will be leaked in the non accept scenario: 2362 * fallback and let TCP cope with the subflow cleanup. 2363 */ 2364 WARN_ON_ONCE(sock_flag(ssk, SOCK_DEAD)); 2365 mptcp_subflow_drop_ctx(ssk); 2366 } else { 2367 /* otherwise tcp will dispose of the ssk and subflow ctx */ 2368 if (ssk->sk_state == TCP_LISTEN) 2369 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); 2370 2371 __tcp_close(ssk, 0); 2372 2373 /* close acquired an extra ref */ 2374 __sock_put(ssk); 2375 } 2376 release_sock(ssk); 2377 2378 sock_put(ssk); 2379 2380 if (ssk == msk->first) 2381 msk->first = NULL; 2382 2383 out: 2384 if (ssk == msk->last_snd) 2385 msk->last_snd = NULL; 2386 2387 if (need_push) 2388 __mptcp_push_pending(sk, 0); 2389 } 2390 2391 void mptcp_close_ssk(struct sock *sk, struct sock *ssk, 2392 struct mptcp_subflow_context *subflow) 2393 { 2394 if (sk->sk_state == TCP_ESTABLISHED) 2395 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL); 2396 2397 /* subflow aborted before reaching the fully_established status 2398 * attempt the creation of the next subflow 2399 */ 2400 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow); 2401 2402 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH); 2403 } 2404 2405 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 2406 { 2407 return 0; 2408 } 2409 2410 static void __mptcp_close_subflow(struct sock *sk) 2411 { 2412 struct mptcp_subflow_context *subflow, *tmp; 2413 struct mptcp_sock *msk = mptcp_sk(sk); 2414 2415 might_sleep(); 2416 2417 mptcp_for_each_subflow_safe(msk, subflow, tmp) { 2418 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2419 2420 if (inet_sk_state_load(ssk) != TCP_CLOSE) 2421 continue; 2422 2423 /* 'subflow_data_ready' will re-sched once rx queue is empty */ 2424 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) 2425 continue; 2426 2427 mptcp_close_ssk(sk, ssk, subflow); 2428 } 2429 2430 /* if the MPC subflow has been closed before the msk is accepted, 2431 * msk will never be accept-ed, close it now 2432 */ 2433 if (!msk->first && msk->in_accept_queue) { 2434 sock_set_flag(sk, SOCK_DEAD); 2435 inet_sk_state_store(sk, TCP_CLOSE); 2436 } 2437 } 2438 2439 static bool mptcp_check_close_timeout(const struct sock *sk) 2440 { 2441 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp; 2442 struct mptcp_subflow_context *subflow; 2443 2444 if (delta >= TCP_TIMEWAIT_LEN) 2445 return true; 2446 2447 /* if all subflows are in closed status don't bother with additional 2448 * timeout 2449 */ 2450 mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2451 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) != 2452 TCP_CLOSE) 2453 return false; 2454 } 2455 return true; 2456 } 2457 2458 static void mptcp_check_fastclose(struct mptcp_sock *msk) 2459 { 2460 struct mptcp_subflow_context *subflow, *tmp; 2461 struct sock *sk = (struct sock *)msk; 2462 2463 if (likely(!READ_ONCE(msk->rcv_fastclose))) 2464 return; 2465 2466 mptcp_token_destroy(msk); 2467 2468 mptcp_for_each_subflow_safe(msk, subflow, tmp) { 2469 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2470 bool slow; 2471 2472 slow = lock_sock_fast(tcp_sk); 2473 if (tcp_sk->sk_state != TCP_CLOSE) { 2474 tcp_send_active_reset(tcp_sk, GFP_ATOMIC); 2475 tcp_set_state(tcp_sk, TCP_CLOSE); 2476 } 2477 unlock_sock_fast(tcp_sk, slow); 2478 } 2479 2480 /* Mirror the tcp_reset() error propagation */ 2481 switch (sk->sk_state) { 2482 case TCP_SYN_SENT: 2483 sk->sk_err = ECONNREFUSED; 2484 break; 2485 case TCP_CLOSE_WAIT: 2486 sk->sk_err = EPIPE; 2487 break; 2488 case TCP_CLOSE: 2489 return; 2490 default: 2491 sk->sk_err = ECONNRESET; 2492 } 2493 2494 inet_sk_state_store(sk, TCP_CLOSE); 2495 sk->sk_shutdown = SHUTDOWN_MASK; 2496 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 2497 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags); 2498 2499 /* the calling mptcp_worker will properly destroy the socket */ 2500 if (sock_flag(sk, SOCK_DEAD)) 2501 return; 2502 2503 sk->sk_state_change(sk); 2504 sk_error_report(sk); 2505 } 2506 2507 static void __mptcp_retrans(struct sock *sk) 2508 { 2509 struct mptcp_sock *msk = mptcp_sk(sk); 2510 struct mptcp_sendmsg_info info = {}; 2511 struct mptcp_data_frag *dfrag; 2512 size_t copied = 0; 2513 struct sock *ssk; 2514 int ret; 2515 2516 mptcp_clean_una_wakeup(sk); 2517 2518 /* first check ssk: need to kick "stale" logic */ 2519 ssk = mptcp_subflow_get_retrans(msk); 2520 dfrag = mptcp_rtx_head(sk); 2521 if (!dfrag) { 2522 if (mptcp_data_fin_enabled(msk)) { 2523 struct inet_connection_sock *icsk = inet_csk(sk); 2524 2525 icsk->icsk_retransmits++; 2526 mptcp_set_datafin_timeout(sk); 2527 mptcp_send_ack(msk); 2528 2529 goto reset_timer; 2530 } 2531 2532 if (!mptcp_send_head(sk)) 2533 return; 2534 2535 goto reset_timer; 2536 } 2537 2538 if (!ssk) 2539 goto reset_timer; 2540 2541 lock_sock(ssk); 2542 2543 /* limit retransmission to the bytes already sent on some subflows */ 2544 info.sent = 0; 2545 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent; 2546 while (info.sent < info.limit) { 2547 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 2548 if (ret <= 0) 2549 break; 2550 2551 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 2552 copied += ret; 2553 info.sent += ret; 2554 } 2555 if (copied) { 2556 dfrag->already_sent = max(dfrag->already_sent, info.sent); 2557 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 2558 info.size_goal); 2559 WRITE_ONCE(msk->allow_infinite_fallback, false); 2560 } 2561 2562 release_sock(ssk); 2563 2564 reset_timer: 2565 mptcp_check_and_set_pending(sk); 2566 2567 if (!mptcp_timer_pending(sk)) 2568 mptcp_reset_timer(sk); 2569 } 2570 2571 /* schedule the timeout timer for the relevant event: either close timeout 2572 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one 2573 */ 2574 void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout) 2575 { 2576 struct sock *sk = (struct sock *)msk; 2577 unsigned long timeout, close_timeout; 2578 2579 if (!fail_tout && !sock_flag(sk, SOCK_DEAD)) 2580 return; 2581 2582 close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN; 2583 2584 /* the close timeout takes precedence on the fail one, and here at least one of 2585 * them is active 2586 */ 2587 timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout; 2588 2589 sk_reset_timer(sk, &sk->sk_timer, timeout); 2590 } 2591 2592 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk) 2593 { 2594 struct sock *ssk = msk->first; 2595 bool slow; 2596 2597 if (!ssk) 2598 return; 2599 2600 pr_debug("MP_FAIL doesn't respond, reset the subflow"); 2601 2602 slow = lock_sock_fast(ssk); 2603 mptcp_subflow_reset(ssk); 2604 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0); 2605 unlock_sock_fast(ssk, slow); 2606 2607 mptcp_reset_timeout(msk, 0); 2608 } 2609 2610 static void mptcp_do_fastclose(struct sock *sk) 2611 { 2612 struct mptcp_subflow_context *subflow, *tmp; 2613 struct mptcp_sock *msk = mptcp_sk(sk); 2614 2615 mptcp_for_each_subflow_safe(msk, subflow, tmp) 2616 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), 2617 subflow, MPTCP_CF_FASTCLOSE); 2618 } 2619 2620 static void mptcp_worker(struct work_struct *work) 2621 { 2622 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 2623 struct sock *sk = (struct sock *)msk; 2624 unsigned long fail_tout; 2625 int state; 2626 2627 lock_sock(sk); 2628 state = sk->sk_state; 2629 if (unlikely(state == TCP_CLOSE)) 2630 goto unlock; 2631 2632 mptcp_check_data_fin_ack(sk); 2633 2634 mptcp_check_fastclose(msk); 2635 2636 mptcp_pm_nl_work(msk); 2637 2638 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 2639 mptcp_check_for_eof(msk); 2640 2641 __mptcp_check_send_data_fin(sk); 2642 mptcp_check_data_fin(sk); 2643 2644 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2645 __mptcp_close_subflow(sk); 2646 2647 /* There is no point in keeping around an orphaned sk timedout or 2648 * closed, but we need the msk around to reply to incoming DATA_FIN, 2649 * even if it is orphaned and in FIN_WAIT2 state 2650 */ 2651 if (sock_flag(sk, SOCK_DEAD)) { 2652 if (mptcp_check_close_timeout(sk)) { 2653 inet_sk_state_store(sk, TCP_CLOSE); 2654 mptcp_do_fastclose(sk); 2655 } 2656 if (sk->sk_state == TCP_CLOSE) { 2657 __mptcp_destroy_sock(sk); 2658 goto unlock; 2659 } 2660 } 2661 2662 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 2663 __mptcp_retrans(sk); 2664 2665 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0; 2666 if (fail_tout && time_after(jiffies, fail_tout)) 2667 mptcp_mp_fail_no_response(msk); 2668 2669 unlock: 2670 release_sock(sk); 2671 sock_put(sk); 2672 } 2673 2674 static int __mptcp_init_sock(struct sock *sk) 2675 { 2676 struct mptcp_sock *msk = mptcp_sk(sk); 2677 2678 INIT_LIST_HEAD(&msk->conn_list); 2679 INIT_LIST_HEAD(&msk->join_list); 2680 INIT_LIST_HEAD(&msk->rtx_queue); 2681 INIT_WORK(&msk->work, mptcp_worker); 2682 __skb_queue_head_init(&msk->receive_queue); 2683 msk->out_of_order_queue = RB_ROOT; 2684 msk->first_pending = NULL; 2685 msk->rmem_fwd_alloc = 0; 2686 WRITE_ONCE(msk->rmem_released, 0); 2687 msk->timer_ival = TCP_RTO_MIN; 2688 2689 msk->first = NULL; 2690 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2691 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 2692 WRITE_ONCE(msk->allow_infinite_fallback, true); 2693 msk->recovery = false; 2694 2695 mptcp_pm_data_init(msk); 2696 2697 /* re-use the csk retrans timer for MPTCP-level retrans */ 2698 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2699 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2700 2701 return 0; 2702 } 2703 2704 static void mptcp_ca_reset(struct sock *sk) 2705 { 2706 struct inet_connection_sock *icsk = inet_csk(sk); 2707 2708 tcp_assign_congestion_control(sk); 2709 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2710 2711 /* no need to keep a reference to the ops, the name will suffice */ 2712 tcp_cleanup_congestion_control(sk); 2713 icsk->icsk_ca_ops = NULL; 2714 } 2715 2716 static int mptcp_init_sock(struct sock *sk) 2717 { 2718 struct net *net = sock_net(sk); 2719 int ret; 2720 2721 ret = __mptcp_init_sock(sk); 2722 if (ret) 2723 return ret; 2724 2725 if (!mptcp_is_enabled(net)) 2726 return -ENOPROTOOPT; 2727 2728 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2729 return -ENOMEM; 2730 2731 ret = __mptcp_socket_create(mptcp_sk(sk)); 2732 if (ret) 2733 return ret; 2734 2735 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags); 2736 2737 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 2738 * propagate the correct value 2739 */ 2740 mptcp_ca_reset(sk); 2741 2742 sk_sockets_allocated_inc(sk); 2743 sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]); 2744 sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]); 2745 2746 return 0; 2747 } 2748 2749 static void __mptcp_clear_xmit(struct sock *sk) 2750 { 2751 struct mptcp_sock *msk = mptcp_sk(sk); 2752 struct mptcp_data_frag *dtmp, *dfrag; 2753 2754 WRITE_ONCE(msk->first_pending, NULL); 2755 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2756 dfrag_clear(sk, dfrag); 2757 } 2758 2759 void mptcp_cancel_work(struct sock *sk) 2760 { 2761 struct mptcp_sock *msk = mptcp_sk(sk); 2762 2763 if (cancel_work_sync(&msk->work)) 2764 __sock_put(sk); 2765 } 2766 2767 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 2768 { 2769 lock_sock(ssk); 2770 2771 switch (ssk->sk_state) { 2772 case TCP_LISTEN: 2773 if (!(how & RCV_SHUTDOWN)) 2774 break; 2775 fallthrough; 2776 case TCP_SYN_SENT: 2777 tcp_disconnect(ssk, O_NONBLOCK); 2778 break; 2779 default: 2780 if (__mptcp_check_fallback(mptcp_sk(sk))) { 2781 pr_debug("Fallback"); 2782 ssk->sk_shutdown |= how; 2783 tcp_shutdown(ssk, how); 2784 } else { 2785 pr_debug("Sending DATA_FIN on subflow %p", ssk); 2786 tcp_send_ack(ssk); 2787 if (!mptcp_timer_pending(sk)) 2788 mptcp_reset_timer(sk); 2789 } 2790 break; 2791 } 2792 2793 release_sock(ssk); 2794 } 2795 2796 static const unsigned char new_state[16] = { 2797 /* current state: new state: action: */ 2798 [0 /* (Invalid) */] = TCP_CLOSE, 2799 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 2800 [TCP_SYN_SENT] = TCP_CLOSE, 2801 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 2802 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 2803 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 2804 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 2805 [TCP_CLOSE] = TCP_CLOSE, 2806 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 2807 [TCP_LAST_ACK] = TCP_LAST_ACK, 2808 [TCP_LISTEN] = TCP_CLOSE, 2809 [TCP_CLOSING] = TCP_CLOSING, 2810 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 2811 }; 2812 2813 static int mptcp_close_state(struct sock *sk) 2814 { 2815 int next = (int)new_state[sk->sk_state]; 2816 int ns = next & TCP_STATE_MASK; 2817 2818 inet_sk_state_store(sk, ns); 2819 2820 return next & TCP_ACTION_FIN; 2821 } 2822 2823 static void __mptcp_check_send_data_fin(struct sock *sk) 2824 { 2825 struct mptcp_subflow_context *subflow; 2826 struct mptcp_sock *msk = mptcp_sk(sk); 2827 2828 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2829 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2830 msk->snd_nxt, msk->write_seq); 2831 2832 /* we still need to enqueue subflows or not really shutting down, 2833 * skip this 2834 */ 2835 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2836 mptcp_send_head(sk)) 2837 return; 2838 2839 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2840 2841 /* fallback socket will not get data_fin/ack, can move to the next 2842 * state now 2843 */ 2844 if (__mptcp_check_fallback(msk)) { 2845 WRITE_ONCE(msk->snd_una, msk->write_seq); 2846 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2847 inet_sk_state_store(sk, TCP_CLOSE); 2848 mptcp_close_wake_up(sk); 2849 } else if (sk->sk_state == TCP_FIN_WAIT1) { 2850 inet_sk_state_store(sk, TCP_FIN_WAIT2); 2851 } 2852 } 2853 2854 mptcp_for_each_subflow(msk, subflow) { 2855 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2856 2857 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2858 } 2859 } 2860 2861 static void __mptcp_wr_shutdown(struct sock *sk) 2862 { 2863 struct mptcp_sock *msk = mptcp_sk(sk); 2864 2865 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2866 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2867 !!mptcp_send_head(sk)); 2868 2869 /* will be ignored by fallback sockets */ 2870 WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2871 WRITE_ONCE(msk->snd_data_fin_enable, 1); 2872 2873 __mptcp_check_send_data_fin(sk); 2874 } 2875 2876 static void __mptcp_destroy_sock(struct sock *sk) 2877 { 2878 struct mptcp_sock *msk = mptcp_sk(sk); 2879 2880 pr_debug("msk=%p", msk); 2881 2882 might_sleep(); 2883 2884 mptcp_stop_timer(sk); 2885 sk_stop_timer(sk, &sk->sk_timer); 2886 msk->pm.status = 0; 2887 2888 sk->sk_prot->destroy(sk); 2889 2890 WARN_ON_ONCE(msk->rmem_fwd_alloc); 2891 WARN_ON_ONCE(msk->rmem_released); 2892 sk_stream_kill_queues(sk); 2893 xfrm_sk_free_policy(sk); 2894 2895 sock_put(sk); 2896 } 2897 2898 static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 2899 { 2900 /* Concurrent splices from sk_receive_queue into receive_queue will 2901 * always show at least one non-empty queue when checked in this order. 2902 */ 2903 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) && 2904 skb_queue_empty_lockless(&msk->receive_queue)) 2905 return 0; 2906 2907 return EPOLLIN | EPOLLRDNORM; 2908 } 2909 2910 static void mptcp_listen_inuse_dec(struct sock *sk) 2911 { 2912 if (inet_sk_state_load(sk) == TCP_LISTEN) 2913 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 2914 } 2915 2916 bool __mptcp_close(struct sock *sk, long timeout) 2917 { 2918 struct mptcp_subflow_context *subflow; 2919 struct mptcp_sock *msk = mptcp_sk(sk); 2920 bool do_cancel_work = false; 2921 int subflows_alive = 0; 2922 2923 sk->sk_shutdown = SHUTDOWN_MASK; 2924 2925 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2926 mptcp_listen_inuse_dec(sk); 2927 inet_sk_state_store(sk, TCP_CLOSE); 2928 goto cleanup; 2929 } 2930 2931 if (mptcp_check_readable(msk)) { 2932 /* the msk has read data, do the MPTCP equivalent of TCP reset */ 2933 inet_sk_state_store(sk, TCP_CLOSE); 2934 mptcp_do_fastclose(sk); 2935 } else if (mptcp_close_state(sk)) { 2936 __mptcp_wr_shutdown(sk); 2937 } 2938 2939 sk_stream_wait_close(sk, timeout); 2940 2941 cleanup: 2942 /* orphan all the subflows */ 2943 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 2944 mptcp_for_each_subflow(msk, subflow) { 2945 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2946 bool slow = lock_sock_fast_nested(ssk); 2947 2948 subflows_alive += ssk->sk_state != TCP_CLOSE; 2949 2950 /* since the close timeout takes precedence on the fail one, 2951 * cancel the latter 2952 */ 2953 if (ssk == msk->first) 2954 subflow->fail_tout = 0; 2955 2956 /* detach from the parent socket, but allow data_ready to 2957 * push incoming data into the mptcp stack, to properly ack it 2958 */ 2959 ssk->sk_socket = NULL; 2960 ssk->sk_wq = NULL; 2961 unlock_sock_fast(ssk, slow); 2962 } 2963 sock_orphan(sk); 2964 2965 /* all the subflows are closed, only timeout can change the msk 2966 * state, let's not keep resources busy for no reasons 2967 */ 2968 if (subflows_alive == 0) 2969 inet_sk_state_store(sk, TCP_CLOSE); 2970 2971 sock_hold(sk); 2972 pr_debug("msk=%p state=%d", sk, sk->sk_state); 2973 if (msk->token) 2974 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 2975 2976 if (sk->sk_state == TCP_CLOSE) { 2977 __mptcp_destroy_sock(sk); 2978 do_cancel_work = true; 2979 } else { 2980 mptcp_reset_timeout(msk, 0); 2981 } 2982 2983 return do_cancel_work; 2984 } 2985 2986 static void mptcp_close(struct sock *sk, long timeout) 2987 { 2988 bool do_cancel_work; 2989 2990 lock_sock(sk); 2991 2992 do_cancel_work = __mptcp_close(sk, timeout); 2993 release_sock(sk); 2994 if (do_cancel_work) 2995 mptcp_cancel_work(sk); 2996 2997 sock_put(sk); 2998 } 2999 3000 void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 3001 { 3002 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3003 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 3004 struct ipv6_pinfo *msk6 = inet6_sk(msk); 3005 3006 msk->sk_v6_daddr = ssk->sk_v6_daddr; 3007 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 3008 3009 if (msk6 && ssk6) { 3010 msk6->saddr = ssk6->saddr; 3011 msk6->flow_label = ssk6->flow_label; 3012 } 3013 #endif 3014 3015 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 3016 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 3017 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 3018 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 3019 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 3020 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 3021 } 3022 3023 static int mptcp_disconnect(struct sock *sk, int flags) 3024 { 3025 struct mptcp_sock *msk = mptcp_sk(sk); 3026 3027 /* We are on the fastopen error path. We can't call straight into the 3028 * subflows cleanup code due to lock nesting (we are already under 3029 * msk->firstsocket lock). Do nothing and leave the cleanup to the 3030 * caller. 3031 */ 3032 if (msk->fastopening) 3033 return 0; 3034 3035 mptcp_listen_inuse_dec(sk); 3036 inet_sk_state_store(sk, TCP_CLOSE); 3037 3038 mptcp_stop_timer(sk); 3039 sk_stop_timer(sk, &sk->sk_timer); 3040 3041 if (msk->token) 3042 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL); 3043 3044 /* msk->subflow is still intact, the following will not free the first 3045 * subflow 3046 */ 3047 mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE); 3048 msk->last_snd = NULL; 3049 WRITE_ONCE(msk->flags, 0); 3050 msk->cb_flags = 0; 3051 msk->push_pending = 0; 3052 msk->recovery = false; 3053 msk->can_ack = false; 3054 msk->fully_established = false; 3055 msk->rcv_data_fin = false; 3056 msk->snd_data_fin_enable = false; 3057 msk->rcv_fastclose = false; 3058 msk->use_64bit_ack = false; 3059 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 3060 mptcp_pm_data_reset(msk); 3061 mptcp_ca_reset(sk); 3062 3063 sk->sk_shutdown = 0; 3064 sk_error_report(sk); 3065 return 0; 3066 } 3067 3068 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3069 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 3070 { 3071 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 3072 3073 return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 3074 } 3075 #endif 3076 3077 struct sock *mptcp_sk_clone(const struct sock *sk, 3078 const struct mptcp_options_received *mp_opt, 3079 struct request_sock *req) 3080 { 3081 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 3082 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 3083 struct mptcp_sock *msk; 3084 3085 if (!nsk) 3086 return NULL; 3087 3088 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3089 if (nsk->sk_family == AF_INET6) 3090 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 3091 #endif 3092 3093 __mptcp_init_sock(nsk); 3094 3095 msk = mptcp_sk(nsk); 3096 msk->local_key = subflow_req->local_key; 3097 msk->token = subflow_req->token; 3098 msk->subflow = NULL; 3099 msk->in_accept_queue = 1; 3100 WRITE_ONCE(msk->fully_established, false); 3101 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 3102 WRITE_ONCE(msk->csum_enabled, true); 3103 3104 msk->write_seq = subflow_req->idsn + 1; 3105 msk->snd_nxt = msk->write_seq; 3106 msk->snd_una = msk->write_seq; 3107 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 3108 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 3109 3110 sock_reset_flag(nsk, SOCK_RCU_FREE); 3111 /* will be fully established after successful MPC subflow creation */ 3112 inet_sk_state_store(nsk, TCP_SYN_RECV); 3113 3114 security_inet_csk_clone(nsk, req); 3115 bh_unlock_sock(nsk); 3116 3117 /* note: the newly allocated socket refcount is 2 now */ 3118 return nsk; 3119 } 3120 3121 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 3122 { 3123 const struct tcp_sock *tp = tcp_sk(ssk); 3124 3125 msk->rcvq_space.copied = 0; 3126 msk->rcvq_space.rtt_us = 0; 3127 3128 msk->rcvq_space.time = tp->tcp_mstamp; 3129 3130 /* initial rcv_space offering made to peer */ 3131 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 3132 TCP_INIT_CWND * tp->advmss); 3133 if (msk->rcvq_space.space == 0) 3134 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 3135 3136 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 3137 } 3138 3139 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 3140 bool kern) 3141 { 3142 struct mptcp_sock *msk = mptcp_sk(sk); 3143 struct socket *listener; 3144 struct sock *newsk; 3145 3146 listener = __mptcp_nmpc_socket(msk); 3147 if (WARN_ON_ONCE(!listener)) { 3148 *err = -EINVAL; 3149 return NULL; 3150 } 3151 3152 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 3153 newsk = inet_csk_accept(listener->sk, flags, err, kern); 3154 if (!newsk) 3155 return NULL; 3156 3157 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 3158 if (sk_is_mptcp(newsk)) { 3159 struct mptcp_subflow_context *subflow; 3160 struct sock *new_mptcp_sock; 3161 3162 subflow = mptcp_subflow_ctx(newsk); 3163 new_mptcp_sock = subflow->conn; 3164 3165 /* is_mptcp should be false if subflow->conn is missing, see 3166 * subflow_syn_recv_sock() 3167 */ 3168 if (WARN_ON_ONCE(!new_mptcp_sock)) { 3169 tcp_sk(newsk)->is_mptcp = 0; 3170 goto out; 3171 } 3172 3173 newsk = new_mptcp_sock; 3174 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 3175 } else { 3176 MPTCP_INC_STATS(sock_net(sk), 3177 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 3178 } 3179 3180 out: 3181 newsk->sk_kern_sock = kern; 3182 return newsk; 3183 } 3184 3185 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags) 3186 { 3187 struct mptcp_subflow_context *subflow, *tmp; 3188 struct sock *sk = (struct sock *)msk; 3189 3190 __mptcp_clear_xmit(sk); 3191 3192 /* join list will be eventually flushed (with rst) at sock lock release time */ 3193 mptcp_for_each_subflow_safe(msk, subflow, tmp) 3194 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags); 3195 3196 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 3197 mptcp_data_lock(sk); 3198 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 3199 __skb_queue_purge(&sk->sk_receive_queue); 3200 skb_rbtree_purge(&msk->out_of_order_queue); 3201 mptcp_data_unlock(sk); 3202 3203 /* move all the rx fwd alloc into the sk_mem_reclaim_final in 3204 * inet_sock_destruct() will dispose it 3205 */ 3206 sk->sk_forward_alloc += msk->rmem_fwd_alloc; 3207 msk->rmem_fwd_alloc = 0; 3208 mptcp_token_destroy(msk); 3209 mptcp_pm_free_anno_list(msk); 3210 mptcp_free_local_addr_list(msk); 3211 } 3212 3213 static void mptcp_destroy(struct sock *sk) 3214 { 3215 struct mptcp_sock *msk = mptcp_sk(sk); 3216 3217 /* clears msk->subflow, allowing the following to close 3218 * even the initial subflow 3219 */ 3220 mptcp_dispose_initial_subflow(msk); 3221 mptcp_destroy_common(msk, 0); 3222 sk_sockets_allocated_dec(sk); 3223 } 3224 3225 void __mptcp_data_acked(struct sock *sk) 3226 { 3227 if (!sock_owned_by_user(sk)) 3228 __mptcp_clean_una(sk); 3229 else 3230 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags); 3231 3232 if (mptcp_pending_data_fin_ack(sk)) 3233 mptcp_schedule_work(sk); 3234 } 3235 3236 void __mptcp_check_push(struct sock *sk, struct sock *ssk) 3237 { 3238 if (!mptcp_send_head(sk)) 3239 return; 3240 3241 if (!sock_owned_by_user(sk)) 3242 __mptcp_subflow_push_pending(sk, ssk, false); 3243 else 3244 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3245 } 3246 3247 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \ 3248 BIT(MPTCP_RETRANSMIT) | \ 3249 BIT(MPTCP_FLUSH_JOIN_LIST)) 3250 3251 /* processes deferred events and flush wmem */ 3252 static void mptcp_release_cb(struct sock *sk) 3253 __must_hold(&sk->sk_lock.slock) 3254 { 3255 struct mptcp_sock *msk = mptcp_sk(sk); 3256 3257 for (;;) { 3258 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) | 3259 msk->push_pending; 3260 if (!flags) 3261 break; 3262 3263 /* the following actions acquire the subflow socket lock 3264 * 3265 * 1) can't be invoked in atomic scope 3266 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 3267 * datapath acquires the msk socket spinlock while helding 3268 * the subflow socket lock 3269 */ 3270 msk->push_pending = 0; 3271 msk->cb_flags &= ~flags; 3272 spin_unlock_bh(&sk->sk_lock.slock); 3273 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST)) 3274 __mptcp_flush_join_list(sk); 3275 if (flags & BIT(MPTCP_PUSH_PENDING)) 3276 __mptcp_push_pending(sk, 0); 3277 if (flags & BIT(MPTCP_RETRANSMIT)) 3278 __mptcp_retrans(sk); 3279 3280 cond_resched(); 3281 spin_lock_bh(&sk->sk_lock.slock); 3282 } 3283 3284 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags)) 3285 __mptcp_clean_una_wakeup(sk); 3286 if (unlikely(&msk->cb_flags)) { 3287 /* be sure to set the current sk state before tacking actions 3288 * depending on sk_state, that is processing MPTCP_ERROR_REPORT 3289 */ 3290 if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags)) 3291 __mptcp_set_connected(sk); 3292 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags)) 3293 __mptcp_error_report(sk); 3294 if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags)) 3295 msk->last_snd = NULL; 3296 } 3297 3298 __mptcp_update_rmem(sk); 3299 } 3300 3301 /* MP_JOIN client subflow must wait for 4th ack before sending any data: 3302 * TCP can't schedule delack timer before the subflow is fully established. 3303 * MPTCP uses the delack timer to do 3rd ack retransmissions 3304 */ 3305 static void schedule_3rdack_retransmission(struct sock *ssk) 3306 { 3307 struct inet_connection_sock *icsk = inet_csk(ssk); 3308 struct tcp_sock *tp = tcp_sk(ssk); 3309 unsigned long timeout; 3310 3311 if (mptcp_subflow_ctx(ssk)->fully_established) 3312 return; 3313 3314 /* reschedule with a timeout above RTT, as we must look only for drop */ 3315 if (tp->srtt_us) 3316 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1)); 3317 else 3318 timeout = TCP_TIMEOUT_INIT; 3319 timeout += jiffies; 3320 3321 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 3322 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 3323 icsk->icsk_ack.timeout = timeout; 3324 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); 3325 } 3326 3327 void mptcp_subflow_process_delegated(struct sock *ssk) 3328 { 3329 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3330 struct sock *sk = subflow->conn; 3331 3332 if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) { 3333 mptcp_data_lock(sk); 3334 if (!sock_owned_by_user(sk)) 3335 __mptcp_subflow_push_pending(sk, ssk, true); 3336 else 3337 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags); 3338 mptcp_data_unlock(sk); 3339 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND); 3340 } 3341 if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) { 3342 schedule_3rdack_retransmission(ssk); 3343 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK); 3344 } 3345 } 3346 3347 static int mptcp_hash(struct sock *sk) 3348 { 3349 /* should never be called, 3350 * we hash the TCP subflows not the master socket 3351 */ 3352 WARN_ON_ONCE(1); 3353 return 0; 3354 } 3355 3356 static void mptcp_unhash(struct sock *sk) 3357 { 3358 /* called from sk_common_release(), but nothing to do here */ 3359 } 3360 3361 static int mptcp_get_port(struct sock *sk, unsigned short snum) 3362 { 3363 struct mptcp_sock *msk = mptcp_sk(sk); 3364 struct socket *ssock; 3365 3366 ssock = __mptcp_nmpc_socket(msk); 3367 pr_debug("msk=%p, subflow=%p", msk, ssock); 3368 if (WARN_ON_ONCE(!ssock)) 3369 return -EINVAL; 3370 3371 return inet_csk_get_port(ssock->sk, snum); 3372 } 3373 3374 void mptcp_finish_connect(struct sock *ssk) 3375 { 3376 struct mptcp_subflow_context *subflow; 3377 struct mptcp_sock *msk; 3378 struct sock *sk; 3379 3380 subflow = mptcp_subflow_ctx(ssk); 3381 sk = subflow->conn; 3382 msk = mptcp_sk(sk); 3383 3384 pr_debug("msk=%p, token=%u", sk, subflow->token); 3385 3386 subflow->map_seq = subflow->iasn; 3387 subflow->map_subflow_seq = 1; 3388 3389 /* the socket is not connected yet, no msk/subflow ops can access/race 3390 * accessing the field below 3391 */ 3392 WRITE_ONCE(msk->local_key, subflow->local_key); 3393 WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3394 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 3395 WRITE_ONCE(msk->snd_una, msk->write_seq); 3396 3397 mptcp_pm_new_connection(msk, ssk, 0); 3398 3399 mptcp_rcv_space_init(msk, ssk); 3400 } 3401 3402 void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3403 { 3404 write_lock_bh(&sk->sk_callback_lock); 3405 rcu_assign_pointer(sk->sk_wq, &parent->wq); 3406 sk_set_socket(sk, parent); 3407 sk->sk_uid = SOCK_INODE(parent)->i_uid; 3408 write_unlock_bh(&sk->sk_callback_lock); 3409 } 3410 3411 bool mptcp_finish_join(struct sock *ssk) 3412 { 3413 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3414 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3415 struct sock *parent = (void *)msk; 3416 bool ret = true; 3417 3418 pr_debug("msk=%p, subflow=%p", msk, subflow); 3419 3420 /* mptcp socket already closing? */ 3421 if (!mptcp_is_fully_established(parent)) { 3422 subflow->reset_reason = MPTCP_RST_EMPTCP; 3423 return false; 3424 } 3425 3426 if (!list_empty(&subflow->node)) 3427 goto out; 3428 3429 if (!mptcp_pm_allow_new_subflow(msk)) 3430 goto err_prohibited; 3431 3432 /* active connections are already on conn_list. 3433 * If we can't acquire msk socket lock here, let the release callback 3434 * handle it 3435 */ 3436 mptcp_data_lock(parent); 3437 if (!sock_owned_by_user(parent)) { 3438 ret = __mptcp_finish_join(msk, ssk); 3439 if (ret) { 3440 sock_hold(ssk); 3441 list_add_tail(&subflow->node, &msk->conn_list); 3442 } 3443 } else { 3444 sock_hold(ssk); 3445 list_add_tail(&subflow->node, &msk->join_list); 3446 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags); 3447 } 3448 mptcp_data_unlock(parent); 3449 3450 if (!ret) { 3451 err_prohibited: 3452 subflow->reset_reason = MPTCP_RST_EPROHIBIT; 3453 return false; 3454 } 3455 3456 subflow->map_seq = READ_ONCE(msk->ack_seq); 3457 WRITE_ONCE(msk->allow_infinite_fallback, false); 3458 3459 out: 3460 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 3461 return true; 3462 } 3463 3464 static void mptcp_shutdown(struct sock *sk, int how) 3465 { 3466 pr_debug("sk=%p, how=%d", sk, how); 3467 3468 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 3469 __mptcp_wr_shutdown(sk); 3470 } 3471 3472 static int mptcp_forward_alloc_get(const struct sock *sk) 3473 { 3474 return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; 3475 } 3476 3477 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) 3478 { 3479 const struct sock *sk = (void *)msk; 3480 u64 delta; 3481 3482 if (sk->sk_state == TCP_LISTEN) 3483 return -EINVAL; 3484 3485 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 3486 return 0; 3487 3488 delta = msk->write_seq - v; 3489 if (__mptcp_check_fallback(msk) && msk->first) { 3490 struct tcp_sock *tp = tcp_sk(msk->first); 3491 3492 /* the first subflow is disconnected after close - see 3493 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq 3494 * so ignore that status, too. 3495 */ 3496 if (!((1 << msk->first->sk_state) & 3497 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))) 3498 delta += READ_ONCE(tp->write_seq) - tp->snd_una; 3499 } 3500 if (delta > INT_MAX) 3501 delta = INT_MAX; 3502 3503 return (int)delta; 3504 } 3505 3506 static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3507 { 3508 struct mptcp_sock *msk = mptcp_sk(sk); 3509 bool slow; 3510 int answ; 3511 3512 switch (cmd) { 3513 case SIOCINQ: 3514 if (sk->sk_state == TCP_LISTEN) 3515 return -EINVAL; 3516 3517 lock_sock(sk); 3518 __mptcp_move_skbs(msk); 3519 answ = mptcp_inq_hint(sk); 3520 release_sock(sk); 3521 break; 3522 case SIOCOUTQ: 3523 slow = lock_sock_fast(sk); 3524 answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); 3525 unlock_sock_fast(sk, slow); 3526 break; 3527 case SIOCOUTQNSD: 3528 slow = lock_sock_fast(sk); 3529 answ = mptcp_ioctl_outq(msk, msk->snd_nxt); 3530 unlock_sock_fast(sk, slow); 3531 break; 3532 default: 3533 return -ENOIOCTLCMD; 3534 } 3535 3536 return put_user(answ, (int __user *)arg); 3537 } 3538 3539 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 3540 struct mptcp_subflow_context *subflow) 3541 { 3542 subflow->request_mptcp = 0; 3543 __mptcp_do_fallback(msk); 3544 } 3545 3546 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 3547 { 3548 struct mptcp_subflow_context *subflow; 3549 struct mptcp_sock *msk = mptcp_sk(sk); 3550 struct socket *ssock; 3551 int err = -EINVAL; 3552 3553 ssock = __mptcp_nmpc_socket(msk); 3554 if (!ssock) 3555 return -EINVAL; 3556 3557 mptcp_token_destroy(msk); 3558 inet_sk_state_store(sk, TCP_SYN_SENT); 3559 subflow = mptcp_subflow_ctx(ssock->sk); 3560 #ifdef CONFIG_TCP_MD5SIG 3561 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3562 * TCP option space. 3563 */ 3564 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 3565 mptcp_subflow_early_fallback(msk, subflow); 3566 #endif 3567 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3568 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 3569 mptcp_subflow_early_fallback(msk, subflow); 3570 } 3571 if (likely(!__mptcp_check_fallback(msk))) 3572 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE); 3573 3574 /* if reaching here via the fastopen/sendmsg path, the caller already 3575 * acquired the subflow socket lock, too. 3576 */ 3577 if (msk->fastopening) 3578 err = __inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags, 1); 3579 else 3580 err = inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags); 3581 inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect; 3582 3583 /* on successful connect, the msk state will be moved to established by 3584 * subflow_finish_connect() 3585 */ 3586 if (unlikely(err && err != -EINPROGRESS)) { 3587 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3588 return err; 3589 } 3590 3591 mptcp_copy_inaddrs(sk, ssock->sk); 3592 3593 /* unblocking connect, mptcp-level inet_stream_connect will error out 3594 * without changing the socket state, update it here. 3595 */ 3596 if (err == -EINPROGRESS) 3597 sk->sk_socket->state = ssock->state; 3598 return err; 3599 } 3600 3601 static struct proto mptcp_prot = { 3602 .name = "MPTCP", 3603 .owner = THIS_MODULE, 3604 .init = mptcp_init_sock, 3605 .connect = mptcp_connect, 3606 .disconnect = mptcp_disconnect, 3607 .close = mptcp_close, 3608 .accept = mptcp_accept, 3609 .setsockopt = mptcp_setsockopt, 3610 .getsockopt = mptcp_getsockopt, 3611 .shutdown = mptcp_shutdown, 3612 .destroy = mptcp_destroy, 3613 .sendmsg = mptcp_sendmsg, 3614 .ioctl = mptcp_ioctl, 3615 .recvmsg = mptcp_recvmsg, 3616 .release_cb = mptcp_release_cb, 3617 .hash = mptcp_hash, 3618 .unhash = mptcp_unhash, 3619 .get_port = mptcp_get_port, 3620 .forward_alloc_get = mptcp_forward_alloc_get, 3621 .sockets_allocated = &mptcp_sockets_allocated, 3622 3623 .memory_allocated = &tcp_memory_allocated, 3624 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 3625 3626 .memory_pressure = &tcp_memory_pressure, 3627 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3628 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3629 .sysctl_mem = sysctl_tcp_mem, 3630 .obj_size = sizeof(struct mptcp_sock), 3631 .slab_flags = SLAB_TYPESAFE_BY_RCU, 3632 .no_autobind = true, 3633 }; 3634 3635 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 3636 { 3637 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3638 struct socket *ssock; 3639 int err; 3640 3641 lock_sock(sock->sk); 3642 ssock = __mptcp_nmpc_socket(msk); 3643 if (!ssock) { 3644 err = -EINVAL; 3645 goto unlock; 3646 } 3647 3648 err = ssock->ops->bind(ssock, uaddr, addr_len); 3649 if (!err) 3650 mptcp_copy_inaddrs(sock->sk, ssock->sk); 3651 3652 unlock: 3653 release_sock(sock->sk); 3654 return err; 3655 } 3656 3657 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 3658 int addr_len, int flags) 3659 { 3660 int ret; 3661 3662 lock_sock(sock->sk); 3663 mptcp_sk(sock->sk)->connect_flags = flags; 3664 ret = __inet_stream_connect(sock, uaddr, addr_len, flags, 0); 3665 release_sock(sock->sk); 3666 return ret; 3667 } 3668 3669 static int mptcp_listen(struct socket *sock, int backlog) 3670 { 3671 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3672 struct sock *sk = sock->sk; 3673 struct socket *ssock; 3674 int err; 3675 3676 pr_debug("msk=%p", msk); 3677 3678 lock_sock(sk); 3679 ssock = __mptcp_nmpc_socket(msk); 3680 if (!ssock) { 3681 err = -EINVAL; 3682 goto unlock; 3683 } 3684 3685 mptcp_token_destroy(msk); 3686 inet_sk_state_store(sk, TCP_LISTEN); 3687 sock_set_flag(sk, SOCK_RCU_FREE); 3688 3689 err = ssock->ops->listen(ssock, backlog); 3690 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk)); 3691 if (!err) { 3692 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 3693 mptcp_copy_inaddrs(sk, ssock->sk); 3694 } 3695 3696 mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED); 3697 3698 unlock: 3699 release_sock(sk); 3700 return err; 3701 } 3702 3703 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3704 int flags, bool kern) 3705 { 3706 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3707 struct socket *ssock; 3708 int err; 3709 3710 pr_debug("msk=%p", msk); 3711 3712 ssock = __mptcp_nmpc_socket(msk); 3713 if (!ssock) 3714 return -EINVAL; 3715 3716 err = ssock->ops->accept(sock, newsock, flags, kern); 3717 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3718 struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3719 struct mptcp_subflow_context *subflow; 3720 struct sock *newsk = newsock->sk; 3721 3722 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); 3723 msk->in_accept_queue = 0; 3724 3725 lock_sock(newsk); 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