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