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