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