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