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 static void mptcp_send_ack(struct mptcp_sock *msk) 444 { 445 struct mptcp_subflow_context *subflow; 446 447 mptcp_for_each_subflow(msk, subflow) { 448 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 449 bool slow; 450 451 slow = lock_sock_fast(ssk); 452 if (tcp_can_send_ack(ssk)) 453 tcp_send_ack(ssk); 454 unlock_sock_fast(ssk, slow); 455 } 456 } 457 458 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk) 459 { 460 bool slow; 461 462 slow = lock_sock_fast(ssk); 463 if (tcp_can_send_ack(ssk)) 464 tcp_cleanup_rbuf(ssk, 1); 465 unlock_sock_fast(ssk, slow); 466 } 467 468 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty) 469 { 470 const struct inet_connection_sock *icsk = inet_csk(ssk); 471 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending); 472 const struct tcp_sock *tp = tcp_sk(ssk); 473 474 return (ack_pending & ICSK_ACK_SCHED) && 475 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) > 476 READ_ONCE(icsk->icsk_ack.rcv_mss)) || 477 (rx_empty && ack_pending & 478 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED))); 479 } 480 481 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk) 482 { 483 int old_space = READ_ONCE(msk->old_wspace); 484 struct mptcp_subflow_context *subflow; 485 struct sock *sk = (struct sock *)msk; 486 int space = __mptcp_space(sk); 487 bool cleanup, rx_empty; 488 489 cleanup = (space > 0) && (space >= (old_space << 1)); 490 rx_empty = !__mptcp_rmem(sk); 491 492 mptcp_for_each_subflow(msk, subflow) { 493 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 494 495 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty)) 496 mptcp_subflow_cleanup_rbuf(ssk); 497 } 498 } 499 500 static bool mptcp_check_data_fin(struct sock *sk) 501 { 502 struct mptcp_sock *msk = mptcp_sk(sk); 503 u64 rcv_data_fin_seq; 504 bool ret = false; 505 506 if (__mptcp_check_fallback(msk)) 507 return ret; 508 509 /* Need to ack a DATA_FIN received from a peer while this side 510 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2. 511 * msk->rcv_data_fin was set when parsing the incoming options 512 * at the subflow level and the msk lock was not held, so this 513 * is the first opportunity to act on the DATA_FIN and change 514 * the msk state. 515 * 516 * If we are caught up to the sequence number of the incoming 517 * DATA_FIN, send the DATA_ACK now and do state transition. If 518 * not caught up, do nothing and let the recv code send DATA_ACK 519 * when catching up. 520 */ 521 522 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) { 523 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1); 524 WRITE_ONCE(msk->rcv_data_fin, 0); 525 526 sk->sk_shutdown |= RCV_SHUTDOWN; 527 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 528 set_bit(MPTCP_DATA_READY, &msk->flags); 529 530 switch (sk->sk_state) { 531 case TCP_ESTABLISHED: 532 inet_sk_state_store(sk, TCP_CLOSE_WAIT); 533 break; 534 case TCP_FIN_WAIT1: 535 inet_sk_state_store(sk, TCP_CLOSING); 536 break; 537 case TCP_FIN_WAIT2: 538 inet_sk_state_store(sk, TCP_CLOSE); 539 break; 540 default: 541 /* Other states not expected */ 542 WARN_ON_ONCE(1); 543 break; 544 } 545 546 ret = true; 547 mptcp_send_ack(msk); 548 mptcp_close_wake_up(sk); 549 } 550 return ret; 551 } 552 553 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 554 struct sock *ssk, 555 unsigned int *bytes) 556 { 557 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 558 struct sock *sk = (struct sock *)msk; 559 unsigned int moved = 0; 560 bool more_data_avail; 561 struct tcp_sock *tp; 562 bool done = false; 563 int sk_rbuf; 564 565 sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 566 567 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 568 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 569 570 if (unlikely(ssk_rbuf > sk_rbuf)) { 571 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf); 572 sk_rbuf = ssk_rbuf; 573 } 574 } 575 576 pr_debug("msk=%p ssk=%p", msk, ssk); 577 tp = tcp_sk(ssk); 578 do { 579 u32 map_remaining, offset; 580 u32 seq = tp->copied_seq; 581 struct sk_buff *skb; 582 bool fin; 583 584 /* try to move as much data as available */ 585 map_remaining = subflow->map_data_len - 586 mptcp_subflow_get_map_offset(subflow); 587 588 skb = skb_peek(&ssk->sk_receive_queue); 589 if (!skb) { 590 /* if no data is found, a racing workqueue/recvmsg 591 * already processed the new data, stop here or we 592 * can enter an infinite loop 593 */ 594 if (!moved) 595 done = true; 596 break; 597 } 598 599 if (__mptcp_check_fallback(msk)) { 600 /* if we are running under the workqueue, TCP could have 601 * collapsed skbs between dummy map creation and now 602 * be sure to adjust the size 603 */ 604 map_remaining = skb->len; 605 subflow->map_data_len = skb->len; 606 } 607 608 offset = seq - TCP_SKB_CB(skb)->seq; 609 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 610 if (fin) { 611 done = true; 612 seq++; 613 } 614 615 if (offset < skb->len) { 616 size_t len = skb->len - offset; 617 618 if (tp->urg_data) 619 done = true; 620 621 if (__mptcp_move_skb(msk, ssk, skb, offset, len)) 622 moved += len; 623 seq += len; 624 625 if (WARN_ON_ONCE(map_remaining < len)) 626 break; 627 } else { 628 WARN_ON_ONCE(!fin); 629 sk_eat_skb(ssk, skb); 630 done = true; 631 } 632 633 WRITE_ONCE(tp->copied_seq, seq); 634 more_data_avail = mptcp_subflow_data_available(ssk); 635 636 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) { 637 done = true; 638 break; 639 } 640 } while (more_data_avail); 641 642 *bytes += moved; 643 return done; 644 } 645 646 static bool __mptcp_ofo_queue(struct mptcp_sock *msk) 647 { 648 struct sock *sk = (struct sock *)msk; 649 struct sk_buff *skb, *tail; 650 bool moved = false; 651 struct rb_node *p; 652 u64 end_seq; 653 654 p = rb_first(&msk->out_of_order_queue); 655 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue)); 656 while (p) { 657 skb = rb_to_skb(p); 658 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) 659 break; 660 661 p = rb_next(p); 662 rb_erase(&skb->rbnode, &msk->out_of_order_queue); 663 664 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq, 665 msk->ack_seq))) { 666 mptcp_drop(sk, skb); 667 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA); 668 continue; 669 } 670 671 end_seq = MPTCP_SKB_CB(skb)->end_seq; 672 tail = skb_peek_tail(&sk->sk_receive_queue); 673 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) { 674 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; 675 676 /* skip overlapping data, if any */ 677 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d", 678 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, 679 delta); 680 MPTCP_SKB_CB(skb)->offset += delta; 681 __skb_queue_tail(&sk->sk_receive_queue, skb); 682 } 683 msk->ack_seq = end_seq; 684 moved = true; 685 } 686 return moved; 687 } 688 689 /* In most cases we will be able to lock the mptcp socket. If its already 690 * owned, we need to defer to the work queue to avoid ABBA deadlock. 691 */ 692 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 693 { 694 struct sock *sk = (struct sock *)msk; 695 unsigned int moved = 0; 696 697 __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 698 __mptcp_ofo_queue(msk); 699 if (unlikely(ssk->sk_err)) { 700 if (!sock_owned_by_user(sk)) 701 __mptcp_error_report(sk); 702 else 703 set_bit(MPTCP_ERROR_REPORT, &msk->flags); 704 } 705 706 /* If the moves have caught up with the DATA_FIN sequence number 707 * it's time to ack the DATA_FIN and change socket state, but 708 * this is not a good place to change state. Let the workqueue 709 * do it. 710 */ 711 if (mptcp_pending_data_fin(sk, NULL)) 712 mptcp_schedule_work(sk); 713 return moved > 0; 714 } 715 716 void mptcp_data_ready(struct sock *sk, struct sock *ssk) 717 { 718 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 719 struct mptcp_sock *msk = mptcp_sk(sk); 720 int sk_rbuf, ssk_rbuf; 721 722 /* The peer can send data while we are shutting down this 723 * subflow at msk destruction time, but we must avoid enqueuing 724 * more data to the msk receive queue 725 */ 726 if (unlikely(subflow->disposable)) 727 return; 728 729 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf); 730 sk_rbuf = READ_ONCE(sk->sk_rcvbuf); 731 if (unlikely(ssk_rbuf > sk_rbuf)) 732 sk_rbuf = ssk_rbuf; 733 734 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/ 735 if (__mptcp_rmem(sk) > sk_rbuf) { 736 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED); 737 return; 738 } 739 740 /* Wake-up the reader only for in-sequence data */ 741 mptcp_data_lock(sk); 742 if (move_skbs_to_msk(msk, ssk)) { 743 set_bit(MPTCP_DATA_READY, &msk->flags); 744 sk->sk_data_ready(sk); 745 } 746 mptcp_data_unlock(sk); 747 } 748 749 static bool mptcp_do_flush_join_list(struct mptcp_sock *msk) 750 { 751 struct mptcp_subflow_context *subflow; 752 bool ret = false; 753 754 if (likely(list_empty(&msk->join_list))) 755 return false; 756 757 spin_lock_bh(&msk->join_list_lock); 758 list_for_each_entry(subflow, &msk->join_list, node) { 759 u32 sseq = READ_ONCE(subflow->setsockopt_seq); 760 761 mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow)); 762 if (READ_ONCE(msk->setsockopt_seq) != sseq) 763 ret = true; 764 } 765 list_splice_tail_init(&msk->join_list, &msk->conn_list); 766 spin_unlock_bh(&msk->join_list_lock); 767 768 return ret; 769 } 770 771 void __mptcp_flush_join_list(struct mptcp_sock *msk) 772 { 773 if (likely(!mptcp_do_flush_join_list(msk))) 774 return; 775 776 if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags)) 777 mptcp_schedule_work((struct sock *)msk); 778 } 779 780 static void mptcp_flush_join_list(struct mptcp_sock *msk) 781 { 782 bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags); 783 784 might_sleep(); 785 786 if (!mptcp_do_flush_join_list(msk) && !sync_needed) 787 return; 788 789 mptcp_sockopt_sync_all(msk); 790 } 791 792 static bool mptcp_timer_pending(struct sock *sk) 793 { 794 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 795 } 796 797 static void mptcp_reset_timer(struct sock *sk) 798 { 799 struct inet_connection_sock *icsk = inet_csk(sk); 800 unsigned long tout; 801 802 /* prevent rescheduling on close */ 803 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE)) 804 return; 805 806 tout = mptcp_sk(sk)->timer_ival; 807 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 808 } 809 810 bool mptcp_schedule_work(struct sock *sk) 811 { 812 if (inet_sk_state_load(sk) != TCP_CLOSE && 813 schedule_work(&mptcp_sk(sk)->work)) { 814 /* each subflow already holds a reference to the sk, and the 815 * workqueue is invoked by a subflow, so sk can't go away here. 816 */ 817 sock_hold(sk); 818 return true; 819 } 820 return false; 821 } 822 823 void mptcp_subflow_eof(struct sock *sk) 824 { 825 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags)) 826 mptcp_schedule_work(sk); 827 } 828 829 static void mptcp_check_for_eof(struct mptcp_sock *msk) 830 { 831 struct mptcp_subflow_context *subflow; 832 struct sock *sk = (struct sock *)msk; 833 int receivers = 0; 834 835 mptcp_for_each_subflow(msk, subflow) 836 receivers += !subflow->rx_eof; 837 if (receivers) 838 return; 839 840 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) { 841 /* hopefully temporary hack: propagate shutdown status 842 * to msk, when all subflows agree on it 843 */ 844 sk->sk_shutdown |= RCV_SHUTDOWN; 845 846 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 847 set_bit(MPTCP_DATA_READY, &msk->flags); 848 sk->sk_data_ready(sk); 849 } 850 851 switch (sk->sk_state) { 852 case TCP_ESTABLISHED: 853 inet_sk_state_store(sk, TCP_CLOSE_WAIT); 854 break; 855 case TCP_FIN_WAIT1: 856 inet_sk_state_store(sk, TCP_CLOSING); 857 break; 858 case TCP_FIN_WAIT2: 859 inet_sk_state_store(sk, TCP_CLOSE); 860 break; 861 default: 862 return; 863 } 864 mptcp_close_wake_up(sk); 865 } 866 867 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 868 { 869 struct mptcp_subflow_context *subflow; 870 struct sock *sk = (struct sock *)msk; 871 872 sock_owned_by_me(sk); 873 874 mptcp_for_each_subflow(msk, subflow) { 875 if (READ_ONCE(subflow->data_avail)) 876 return mptcp_subflow_tcp_sock(subflow); 877 } 878 879 return NULL; 880 } 881 882 static bool mptcp_skb_can_collapse_to(u64 write_seq, 883 const struct sk_buff *skb, 884 const struct mptcp_ext *mpext) 885 { 886 if (!tcp_skb_can_collapse_to(skb)) 887 return false; 888 889 /* can collapse only if MPTCP level sequence is in order and this 890 * mapping has not been xmitted yet 891 */ 892 return mpext && mpext->data_seq + mpext->data_len == write_seq && 893 !mpext->frozen; 894 } 895 896 /* we can append data to the given data frag if: 897 * - there is space available in the backing page_frag 898 * - the data frag tail matches the current page_frag free offset 899 * - the data frag end sequence number matches the current write seq 900 */ 901 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 902 const struct page_frag *pfrag, 903 const struct mptcp_data_frag *df) 904 { 905 return df && pfrag->page == df->page && 906 pfrag->size - pfrag->offset > 0 && 907 pfrag->offset == (df->offset + df->data_len) && 908 df->data_seq + df->data_len == msk->write_seq; 909 } 910 911 static int mptcp_wmem_with_overhead(int size) 912 { 913 return size + ((sizeof(struct mptcp_data_frag) * size) >> PAGE_SHIFT); 914 } 915 916 static void __mptcp_wmem_reserve(struct sock *sk, int size) 917 { 918 int amount = mptcp_wmem_with_overhead(size); 919 struct mptcp_sock *msk = mptcp_sk(sk); 920 921 WARN_ON_ONCE(msk->wmem_reserved); 922 if (WARN_ON_ONCE(amount < 0)) 923 amount = 0; 924 925 if (amount <= sk->sk_forward_alloc) 926 goto reserve; 927 928 /* under memory pressure try to reserve at most a single page 929 * otherwise try to reserve the full estimate and fallback 930 * to a single page before entering the error path 931 */ 932 if ((tcp_under_memory_pressure(sk) && amount > PAGE_SIZE) || 933 !sk_wmem_schedule(sk, amount)) { 934 if (amount <= PAGE_SIZE) 935 goto nomem; 936 937 amount = PAGE_SIZE; 938 if (!sk_wmem_schedule(sk, amount)) 939 goto nomem; 940 } 941 942 reserve: 943 msk->wmem_reserved = amount; 944 sk->sk_forward_alloc -= amount; 945 return; 946 947 nomem: 948 /* we will wait for memory on next allocation */ 949 msk->wmem_reserved = -1; 950 } 951 952 static void __mptcp_update_wmem(struct sock *sk) 953 { 954 struct mptcp_sock *msk = mptcp_sk(sk); 955 956 #ifdef CONFIG_LOCKDEP 957 WARN_ON_ONCE(!lockdep_is_held(&sk->sk_lock.slock)); 958 #endif 959 960 if (!msk->wmem_reserved) 961 return; 962 963 if (msk->wmem_reserved < 0) 964 msk->wmem_reserved = 0; 965 if (msk->wmem_reserved > 0) { 966 sk->sk_forward_alloc += msk->wmem_reserved; 967 msk->wmem_reserved = 0; 968 } 969 } 970 971 static bool mptcp_wmem_alloc(struct sock *sk, int size) 972 { 973 struct mptcp_sock *msk = mptcp_sk(sk); 974 975 /* check for pre-existing error condition */ 976 if (msk->wmem_reserved < 0) 977 return false; 978 979 if (msk->wmem_reserved >= size) 980 goto account; 981 982 mptcp_data_lock(sk); 983 if (!sk_wmem_schedule(sk, size)) { 984 mptcp_data_unlock(sk); 985 return false; 986 } 987 988 sk->sk_forward_alloc -= size; 989 msk->wmem_reserved += size; 990 mptcp_data_unlock(sk); 991 992 account: 993 msk->wmem_reserved -= size; 994 return true; 995 } 996 997 static void mptcp_wmem_uncharge(struct sock *sk, int size) 998 { 999 struct mptcp_sock *msk = mptcp_sk(sk); 1000 1001 if (msk->wmem_reserved < 0) 1002 msk->wmem_reserved = 0; 1003 msk->wmem_reserved += size; 1004 } 1005 1006 static void mptcp_mem_reclaim_partial(struct sock *sk) 1007 { 1008 struct mptcp_sock *msk = mptcp_sk(sk); 1009 1010 /* if we are experiencing a transint allocation error, 1011 * the forward allocation memory has been already 1012 * released 1013 */ 1014 if (msk->wmem_reserved < 0) 1015 return; 1016 1017 mptcp_data_lock(sk); 1018 sk->sk_forward_alloc += msk->wmem_reserved; 1019 sk_mem_reclaim_partial(sk); 1020 msk->wmem_reserved = sk->sk_forward_alloc; 1021 sk->sk_forward_alloc = 0; 1022 mptcp_data_unlock(sk); 1023 } 1024 1025 static void dfrag_uncharge(struct sock *sk, int len) 1026 { 1027 sk_mem_uncharge(sk, len); 1028 sk_wmem_queued_add(sk, -len); 1029 } 1030 1031 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 1032 { 1033 int len = dfrag->data_len + dfrag->overhead; 1034 1035 list_del(&dfrag->list); 1036 dfrag_uncharge(sk, len); 1037 put_page(dfrag->page); 1038 } 1039 1040 static void __mptcp_clean_una(struct sock *sk) 1041 { 1042 struct mptcp_sock *msk = mptcp_sk(sk); 1043 struct mptcp_data_frag *dtmp, *dfrag; 1044 bool cleaned = false; 1045 u64 snd_una; 1046 1047 /* on fallback we just need to ignore snd_una, as this is really 1048 * plain TCP 1049 */ 1050 if (__mptcp_check_fallback(msk)) 1051 msk->snd_una = READ_ONCE(msk->snd_nxt); 1052 1053 snd_una = msk->snd_una; 1054 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 1055 if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 1056 break; 1057 1058 if (unlikely(dfrag == msk->first_pending)) { 1059 /* in recovery mode can see ack after the current snd head */ 1060 if (WARN_ON_ONCE(!msk->recovery)) 1061 break; 1062 1063 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1064 } 1065 1066 dfrag_clear(sk, dfrag); 1067 cleaned = true; 1068 } 1069 1070 dfrag = mptcp_rtx_head(sk); 1071 if (dfrag && after64(snd_una, dfrag->data_seq)) { 1072 u64 delta = snd_una - dfrag->data_seq; 1073 1074 /* prevent wrap around in recovery mode */ 1075 if (unlikely(delta > dfrag->already_sent)) { 1076 if (WARN_ON_ONCE(!msk->recovery)) 1077 goto out; 1078 if (WARN_ON_ONCE(delta > dfrag->data_len)) 1079 goto out; 1080 dfrag->already_sent += delta - dfrag->already_sent; 1081 } 1082 1083 dfrag->data_seq += delta; 1084 dfrag->offset += delta; 1085 dfrag->data_len -= delta; 1086 dfrag->already_sent -= delta; 1087 1088 dfrag_uncharge(sk, delta); 1089 cleaned = true; 1090 } 1091 1092 /* all retransmitted data acked, recovery completed */ 1093 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt)) 1094 msk->recovery = false; 1095 1096 out: 1097 if (cleaned) { 1098 if (tcp_under_memory_pressure(sk)) { 1099 __mptcp_update_wmem(sk); 1100 sk_mem_reclaim_partial(sk); 1101 } 1102 } 1103 1104 if (snd_una == READ_ONCE(msk->snd_nxt) && !msk->recovery) { 1105 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk)) 1106 mptcp_stop_timer(sk); 1107 } else { 1108 mptcp_reset_timer(sk); 1109 } 1110 } 1111 1112 static void __mptcp_clean_una_wakeup(struct sock *sk) 1113 { 1114 #ifdef CONFIG_LOCKDEP 1115 WARN_ON_ONCE(!lockdep_is_held(&sk->sk_lock.slock)); 1116 #endif 1117 __mptcp_clean_una(sk); 1118 mptcp_write_space(sk); 1119 } 1120 1121 static void mptcp_clean_una_wakeup(struct sock *sk) 1122 { 1123 mptcp_data_lock(sk); 1124 __mptcp_clean_una_wakeup(sk); 1125 mptcp_data_unlock(sk); 1126 } 1127 1128 static void mptcp_enter_memory_pressure(struct sock *sk) 1129 { 1130 struct mptcp_subflow_context *subflow; 1131 struct mptcp_sock *msk = mptcp_sk(sk); 1132 bool first = true; 1133 1134 sk_stream_moderate_sndbuf(sk); 1135 mptcp_for_each_subflow(msk, subflow) { 1136 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1137 1138 if (first) 1139 tcp_enter_memory_pressure(ssk); 1140 sk_stream_moderate_sndbuf(ssk); 1141 first = false; 1142 } 1143 } 1144 1145 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 1146 * data 1147 */ 1148 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1149 { 1150 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 1151 pfrag, sk->sk_allocation))) 1152 return true; 1153 1154 mptcp_enter_memory_pressure(sk); 1155 return false; 1156 } 1157 1158 static struct mptcp_data_frag * 1159 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 1160 int orig_offset) 1161 { 1162 int offset = ALIGN(orig_offset, sizeof(long)); 1163 struct mptcp_data_frag *dfrag; 1164 1165 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 1166 dfrag->data_len = 0; 1167 dfrag->data_seq = msk->write_seq; 1168 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 1169 dfrag->offset = offset + sizeof(struct mptcp_data_frag); 1170 dfrag->already_sent = 0; 1171 dfrag->page = pfrag->page; 1172 1173 return dfrag; 1174 } 1175 1176 struct mptcp_sendmsg_info { 1177 int mss_now; 1178 int size_goal; 1179 u16 limit; 1180 u16 sent; 1181 unsigned int flags; 1182 }; 1183 1184 static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq, 1185 int avail_size) 1186 { 1187 u64 window_end = mptcp_wnd_end(msk); 1188 1189 if (__mptcp_check_fallback(msk)) 1190 return avail_size; 1191 1192 if (!before64(data_seq + avail_size, window_end)) { 1193 u64 allowed_size = window_end - data_seq; 1194 1195 return min_t(unsigned int, allowed_size, avail_size); 1196 } 1197 1198 return avail_size; 1199 } 1200 1201 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp) 1202 { 1203 struct skb_ext *mpext = __skb_ext_alloc(gfp); 1204 1205 if (!mpext) 1206 return false; 1207 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext); 1208 return true; 1209 } 1210 1211 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp) 1212 { 1213 struct sk_buff *skb; 1214 1215 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp); 1216 if (likely(skb)) { 1217 if (likely(__mptcp_add_ext(skb, gfp))) { 1218 skb_reserve(skb, MAX_TCP_HEADER); 1219 skb->reserved_tailroom = skb->end - skb->tail; 1220 return skb; 1221 } 1222 __kfree_skb(skb); 1223 } else { 1224 mptcp_enter_memory_pressure(sk); 1225 } 1226 return NULL; 1227 } 1228 1229 static bool __mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp) 1230 { 1231 struct sk_buff *skb; 1232 1233 if (ssk->sk_tx_skb_cache) { 1234 skb = ssk->sk_tx_skb_cache; 1235 if (unlikely(!skb_ext_find(skb, SKB_EXT_MPTCP) && 1236 !__mptcp_add_ext(skb, gfp))) 1237 return false; 1238 return true; 1239 } 1240 1241 skb = __mptcp_do_alloc_tx_skb(sk, gfp); 1242 if (!skb) 1243 return false; 1244 1245 if (likely(sk_wmem_schedule(ssk, skb->truesize))) { 1246 ssk->sk_tx_skb_cache = skb; 1247 return true; 1248 } 1249 kfree_skb(skb); 1250 return false; 1251 } 1252 1253 static bool mptcp_must_reclaim_memory(struct sock *sk, struct sock *ssk) 1254 { 1255 return !ssk->sk_tx_skb_cache && 1256 tcp_under_memory_pressure(sk); 1257 } 1258 1259 static bool mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk) 1260 { 1261 if (unlikely(mptcp_must_reclaim_memory(sk, ssk))) 1262 mptcp_mem_reclaim_partial(sk); 1263 return __mptcp_alloc_tx_skb(sk, ssk, sk->sk_allocation); 1264 } 1265 1266 /* note: this always recompute the csum on the whole skb, even 1267 * if we just appended a single frag. More status info needed 1268 */ 1269 static void mptcp_update_data_checksum(struct sk_buff *skb, int added) 1270 { 1271 struct mptcp_ext *mpext = mptcp_get_ext(skb); 1272 __wsum csum = ~csum_unfold(mpext->csum); 1273 int offset = skb->len - added; 1274 1275 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset)); 1276 } 1277 1278 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 1279 struct mptcp_data_frag *dfrag, 1280 struct mptcp_sendmsg_info *info) 1281 { 1282 u64 data_seq = dfrag->data_seq + info->sent; 1283 struct mptcp_sock *msk = mptcp_sk(sk); 1284 bool zero_window_probe = false; 1285 struct mptcp_ext *mpext = NULL; 1286 struct sk_buff *skb, *tail; 1287 bool can_collapse = false; 1288 int size_bias = 0; 1289 int avail_size; 1290 size_t ret = 0; 1291 1292 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u", 1293 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent); 1294 1295 /* compute send limit */ 1296 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags); 1297 avail_size = info->size_goal; 1298 skb = tcp_write_queue_tail(ssk); 1299 if (skb) { 1300 /* Limit the write to the size available in the 1301 * current skb, if any, so that we create at most a new skb. 1302 * Explicitly tells TCP internals to avoid collapsing on later 1303 * queue management operation, to avoid breaking the ext <-> 1304 * SSN association set here 1305 */ 1306 mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 1307 can_collapse = (info->size_goal - skb->len > 0) && 1308 mptcp_skb_can_collapse_to(data_seq, skb, mpext); 1309 if (!can_collapse) { 1310 TCP_SKB_CB(skb)->eor = 1; 1311 } else { 1312 size_bias = skb->len; 1313 avail_size = info->size_goal - skb->len; 1314 } 1315 } 1316 1317 /* Zero window and all data acked? Probe. */ 1318 avail_size = mptcp_check_allowed_size(msk, data_seq, avail_size); 1319 if (avail_size == 0) { 1320 u64 snd_una = READ_ONCE(msk->snd_una); 1321 1322 if (skb || snd_una != msk->snd_nxt) 1323 return 0; 1324 zero_window_probe = true; 1325 data_seq = snd_una - 1; 1326 avail_size = 1; 1327 } 1328 1329 if (WARN_ON_ONCE(info->sent > info->limit || 1330 info->limit > dfrag->data_len)) 1331 return 0; 1332 1333 ret = info->limit - info->sent; 1334 tail = tcp_build_frag(ssk, avail_size + size_bias, info->flags, 1335 dfrag->page, dfrag->offset + info->sent, &ret); 1336 if (!tail) { 1337 tcp_remove_empty_skb(sk, tcp_write_queue_tail(ssk)); 1338 return -ENOMEM; 1339 } 1340 1341 /* if the tail skb is still the cached one, collapsing really happened. 1342 */ 1343 if (skb == tail) { 1344 TCP_SKB_CB(tail)->tcp_flags &= ~TCPHDR_PSH; 1345 mpext->data_len += ret; 1346 WARN_ON_ONCE(!can_collapse); 1347 WARN_ON_ONCE(zero_window_probe); 1348 goto out; 1349 } 1350 1351 mpext = skb_ext_find(tail, SKB_EXT_MPTCP); 1352 if (WARN_ON_ONCE(!mpext)) { 1353 /* should never reach here, stream corrupted */ 1354 return -EINVAL; 1355 } 1356 1357 memset(mpext, 0, sizeof(*mpext)); 1358 mpext->data_seq = data_seq; 1359 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 1360 mpext->data_len = ret; 1361 mpext->use_map = 1; 1362 mpext->dsn64 = 1; 1363 1364 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 1365 mpext->data_seq, mpext->subflow_seq, mpext->data_len, 1366 mpext->dsn64); 1367 1368 if (zero_window_probe) { 1369 mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 1370 mpext->frozen = 1; 1371 if (READ_ONCE(msk->csum_enabled)) 1372 mptcp_update_data_checksum(tail, ret); 1373 tcp_push_pending_frames(ssk); 1374 return 0; 1375 } 1376 out: 1377 if (READ_ONCE(msk->csum_enabled)) 1378 mptcp_update_data_checksum(tail, ret); 1379 mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 1380 return ret; 1381 } 1382 1383 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \ 1384 sizeof(struct tcphdr) - \ 1385 MAX_TCP_OPTION_SPACE - \ 1386 sizeof(struct ipv6hdr) - \ 1387 sizeof(struct frag_hdr)) 1388 1389 struct subflow_send_info { 1390 struct sock *ssk; 1391 u64 ratio; 1392 }; 1393 1394 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow) 1395 { 1396 if (!subflow->stale) 1397 return; 1398 1399 subflow->stale = 0; 1400 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER); 1401 } 1402 1403 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) 1404 { 1405 if (unlikely(subflow->stale)) { 1406 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp); 1407 1408 if (subflow->stale_rcv_tstamp == rcv_tstamp) 1409 return false; 1410 1411 mptcp_subflow_set_active(subflow); 1412 } 1413 return __mptcp_subflow_active(subflow); 1414 } 1415 1416 /* implement the mptcp packet scheduler; 1417 * returns the subflow that will transmit the next DSS 1418 * additionally updates the rtx timeout 1419 */ 1420 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 1421 { 1422 struct subflow_send_info send_info[2]; 1423 struct mptcp_subflow_context *subflow; 1424 struct sock *sk = (struct sock *)msk; 1425 int i, nr_active = 0; 1426 struct sock *ssk; 1427 long tout = 0; 1428 u64 ratio; 1429 u32 pace; 1430 1431 sock_owned_by_me(sk); 1432 1433 if (__mptcp_check_fallback(msk)) { 1434 if (!msk->first) 1435 return NULL; 1436 return sk_stream_memory_free(msk->first) ? msk->first : NULL; 1437 } 1438 1439 /* re-use last subflow, if the burst allow that */ 1440 if (msk->last_snd && msk->snd_burst > 0 && 1441 sk_stream_memory_free(msk->last_snd) && 1442 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) { 1443 mptcp_set_timeout(sk); 1444 return msk->last_snd; 1445 } 1446 1447 /* pick the subflow with the lower wmem/wspace ratio */ 1448 for (i = 0; i < 2; ++i) { 1449 send_info[i].ssk = NULL; 1450 send_info[i].ratio = -1; 1451 } 1452 mptcp_for_each_subflow(msk, subflow) { 1453 trace_mptcp_subflow_get_send(subflow); 1454 ssk = mptcp_subflow_tcp_sock(subflow); 1455 if (!mptcp_subflow_active(subflow)) 1456 continue; 1457 1458 tout = max(tout, mptcp_timeout_from_subflow(subflow)); 1459 nr_active += !subflow->backup; 1460 if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd) 1461 continue; 1462 1463 pace = READ_ONCE(ssk->sk_pacing_rate); 1464 if (!pace) 1465 continue; 1466 1467 ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, 1468 pace); 1469 if (ratio < send_info[subflow->backup].ratio) { 1470 send_info[subflow->backup].ssk = ssk; 1471 send_info[subflow->backup].ratio = ratio; 1472 } 1473 } 1474 __mptcp_set_timeout(sk, tout); 1475 1476 /* pick the best backup if no other subflow is active */ 1477 if (!nr_active) 1478 send_info[0].ssk = send_info[1].ssk; 1479 1480 if (send_info[0].ssk) { 1481 msk->last_snd = send_info[0].ssk; 1482 msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE, 1483 tcp_sk(msk->last_snd)->snd_wnd); 1484 return msk->last_snd; 1485 } 1486 1487 return NULL; 1488 } 1489 1490 static void mptcp_push_release(struct sock *sk, struct sock *ssk, 1491 struct mptcp_sendmsg_info *info) 1492 { 1493 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal); 1494 release_sock(ssk); 1495 } 1496 1497 void __mptcp_push_pending(struct sock *sk, unsigned int flags) 1498 { 1499 struct sock *prev_ssk = NULL, *ssk = NULL; 1500 struct mptcp_sock *msk = mptcp_sk(sk); 1501 struct mptcp_sendmsg_info info = { 1502 .flags = flags, 1503 }; 1504 struct mptcp_data_frag *dfrag; 1505 int len, copied = 0; 1506 1507 while ((dfrag = mptcp_send_head(sk))) { 1508 info.sent = dfrag->already_sent; 1509 info.limit = dfrag->data_len; 1510 len = dfrag->data_len - dfrag->already_sent; 1511 while (len > 0) { 1512 int ret = 0; 1513 1514 prev_ssk = ssk; 1515 mptcp_flush_join_list(msk); 1516 ssk = mptcp_subflow_get_send(msk); 1517 1518 /* First check. If the ssk has changed since 1519 * the last round, release prev_ssk 1520 */ 1521 if (ssk != prev_ssk && prev_ssk) 1522 mptcp_push_release(sk, prev_ssk, &info); 1523 if (!ssk) 1524 goto out; 1525 1526 /* Need to lock the new subflow only if different 1527 * from the previous one, otherwise we are still 1528 * helding the relevant lock 1529 */ 1530 if (ssk != prev_ssk) 1531 lock_sock(ssk); 1532 1533 /* keep it simple and always provide a new skb for the 1534 * subflow, even if we will not use it when collapsing 1535 * on the pending one 1536 */ 1537 if (!mptcp_alloc_tx_skb(sk, ssk)) { 1538 mptcp_push_release(sk, ssk, &info); 1539 goto out; 1540 } 1541 1542 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 1543 if (ret <= 0) { 1544 mptcp_push_release(sk, ssk, &info); 1545 goto out; 1546 } 1547 1548 info.sent += ret; 1549 dfrag->already_sent += ret; 1550 msk->snd_nxt += ret; 1551 msk->snd_burst -= ret; 1552 msk->tx_pending_data -= ret; 1553 copied += ret; 1554 len -= ret; 1555 } 1556 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk)); 1557 } 1558 1559 /* at this point we held the socket lock for the last subflow we used */ 1560 if (ssk) 1561 mptcp_push_release(sk, ssk, &info); 1562 1563 out: 1564 /* ensure the rtx timer is running */ 1565 if (!mptcp_timer_pending(sk)) 1566 mptcp_reset_timer(sk); 1567 if (copied) 1568 __mptcp_check_send_data_fin(sk); 1569 } 1570 1571 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk) 1572 { 1573 struct mptcp_sock *msk = mptcp_sk(sk); 1574 struct mptcp_sendmsg_info info; 1575 struct mptcp_data_frag *dfrag; 1576 struct sock *xmit_ssk; 1577 int len, copied = 0; 1578 bool first = true; 1579 1580 info.flags = 0; 1581 while ((dfrag = mptcp_send_head(sk))) { 1582 info.sent = dfrag->already_sent; 1583 info.limit = dfrag->data_len; 1584 len = dfrag->data_len - dfrag->already_sent; 1585 while (len > 0) { 1586 int ret = 0; 1587 1588 /* the caller already invoked the packet scheduler, 1589 * check for a different subflow usage only after 1590 * spooling the first chunk of data 1591 */ 1592 xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk)); 1593 if (!xmit_ssk) 1594 goto out; 1595 if (xmit_ssk != ssk) { 1596 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); 1597 goto out; 1598 } 1599 1600 if (unlikely(mptcp_must_reclaim_memory(sk, ssk))) { 1601 __mptcp_update_wmem(sk); 1602 sk_mem_reclaim_partial(sk); 1603 } 1604 if (!__mptcp_alloc_tx_skb(sk, ssk, GFP_ATOMIC)) 1605 goto out; 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 if (!mptcp_alloc_tx_skb(sk, ssk)) 2413 break; 2414 2415 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info); 2416 if (ret <= 0) 2417 break; 2418 2419 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 2420 copied += ret; 2421 info.sent += ret; 2422 } 2423 if (copied) { 2424 dfrag->already_sent = max(dfrag->already_sent, info.sent); 2425 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle, 2426 info.size_goal); 2427 } 2428 2429 release_sock(ssk); 2430 2431 reset_timer: 2432 if (!mptcp_timer_pending(sk)) 2433 mptcp_reset_timer(sk); 2434 } 2435 2436 static void mptcp_worker(struct work_struct *work) 2437 { 2438 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 2439 struct sock *sk = &msk->sk.icsk_inet.sk; 2440 int state; 2441 2442 lock_sock(sk); 2443 state = sk->sk_state; 2444 if (unlikely(state == TCP_CLOSE)) 2445 goto unlock; 2446 2447 mptcp_check_data_fin_ack(sk); 2448 mptcp_flush_join_list(msk); 2449 2450 mptcp_check_fastclose(msk); 2451 2452 if (msk->pm.status) 2453 mptcp_pm_nl_work(msk); 2454 2455 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 2456 mptcp_check_for_eof(msk); 2457 2458 __mptcp_check_send_data_fin(sk); 2459 mptcp_check_data_fin(sk); 2460 2461 /* There is no point in keeping around an orphaned sk timedout or 2462 * closed, but we need the msk around to reply to incoming DATA_FIN, 2463 * even if it is orphaned and in FIN_WAIT2 state 2464 */ 2465 if (sock_flag(sk, SOCK_DEAD) && 2466 (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) { 2467 inet_sk_state_store(sk, TCP_CLOSE); 2468 __mptcp_destroy_sock(sk); 2469 goto unlock; 2470 } 2471 2472 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) 2473 __mptcp_close_subflow(msk); 2474 2475 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 2476 __mptcp_retrans(sk); 2477 2478 unlock: 2479 release_sock(sk); 2480 sock_put(sk); 2481 } 2482 2483 static int __mptcp_init_sock(struct sock *sk) 2484 { 2485 struct mptcp_sock *msk = mptcp_sk(sk); 2486 2487 spin_lock_init(&msk->join_list_lock); 2488 2489 INIT_LIST_HEAD(&msk->conn_list); 2490 INIT_LIST_HEAD(&msk->join_list); 2491 INIT_LIST_HEAD(&msk->rtx_queue); 2492 INIT_WORK(&msk->work, mptcp_worker); 2493 __skb_queue_head_init(&msk->receive_queue); 2494 msk->out_of_order_queue = RB_ROOT; 2495 msk->first_pending = NULL; 2496 msk->wmem_reserved = 0; 2497 WRITE_ONCE(msk->rmem_released, 0); 2498 msk->tx_pending_data = 0; 2499 msk->timer_ival = TCP_RTO_MIN; 2500 2501 msk->first = NULL; 2502 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 2503 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk))); 2504 msk->recovery = false; 2505 2506 mptcp_pm_data_init(msk); 2507 2508 /* re-use the csk retrans timer for MPTCP-level retrans */ 2509 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 2510 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0); 2511 2512 return 0; 2513 } 2514 2515 static int mptcp_init_sock(struct sock *sk) 2516 { 2517 struct inet_connection_sock *icsk = inet_csk(sk); 2518 struct net *net = sock_net(sk); 2519 int ret; 2520 2521 ret = __mptcp_init_sock(sk); 2522 if (ret) 2523 return ret; 2524 2525 if (!mptcp_is_enabled(net)) 2526 return -ENOPROTOOPT; 2527 2528 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2529 return -ENOMEM; 2530 2531 ret = __mptcp_socket_create(mptcp_sk(sk)); 2532 if (ret) 2533 return ret; 2534 2535 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will 2536 * propagate the correct value 2537 */ 2538 tcp_assign_congestion_control(sk); 2539 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name); 2540 2541 /* no need to keep a reference to the ops, the name will suffice */ 2542 tcp_cleanup_congestion_control(sk); 2543 icsk->icsk_ca_ops = NULL; 2544 2545 sk_sockets_allocated_inc(sk); 2546 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 2547 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 2548 2549 return 0; 2550 } 2551 2552 static void __mptcp_clear_xmit(struct sock *sk) 2553 { 2554 struct mptcp_sock *msk = mptcp_sk(sk); 2555 struct mptcp_data_frag *dtmp, *dfrag; 2556 2557 WRITE_ONCE(msk->first_pending, NULL); 2558 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2559 dfrag_clear(sk, dfrag); 2560 } 2561 2562 static void mptcp_cancel_work(struct sock *sk) 2563 { 2564 struct mptcp_sock *msk = mptcp_sk(sk); 2565 2566 if (cancel_work_sync(&msk->work)) 2567 __sock_put(sk); 2568 } 2569 2570 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 2571 { 2572 lock_sock(ssk); 2573 2574 switch (ssk->sk_state) { 2575 case TCP_LISTEN: 2576 if (!(how & RCV_SHUTDOWN)) 2577 break; 2578 fallthrough; 2579 case TCP_SYN_SENT: 2580 tcp_disconnect(ssk, O_NONBLOCK); 2581 break; 2582 default: 2583 if (__mptcp_check_fallback(mptcp_sk(sk))) { 2584 pr_debug("Fallback"); 2585 ssk->sk_shutdown |= how; 2586 tcp_shutdown(ssk, how); 2587 } else { 2588 pr_debug("Sending DATA_FIN on subflow %p", ssk); 2589 tcp_send_ack(ssk); 2590 if (!mptcp_timer_pending(sk)) 2591 mptcp_reset_timer(sk); 2592 } 2593 break; 2594 } 2595 2596 release_sock(ssk); 2597 } 2598 2599 static const unsigned char new_state[16] = { 2600 /* current state: new state: action: */ 2601 [0 /* (Invalid) */] = TCP_CLOSE, 2602 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 2603 [TCP_SYN_SENT] = TCP_CLOSE, 2604 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 2605 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 2606 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 2607 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 2608 [TCP_CLOSE] = TCP_CLOSE, 2609 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 2610 [TCP_LAST_ACK] = TCP_LAST_ACK, 2611 [TCP_LISTEN] = TCP_CLOSE, 2612 [TCP_CLOSING] = TCP_CLOSING, 2613 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 2614 }; 2615 2616 static int mptcp_close_state(struct sock *sk) 2617 { 2618 int next = (int)new_state[sk->sk_state]; 2619 int ns = next & TCP_STATE_MASK; 2620 2621 inet_sk_state_store(sk, ns); 2622 2623 return next & TCP_ACTION_FIN; 2624 } 2625 2626 static void __mptcp_check_send_data_fin(struct sock *sk) 2627 { 2628 struct mptcp_subflow_context *subflow; 2629 struct mptcp_sock *msk = mptcp_sk(sk); 2630 2631 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2632 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2633 msk->snd_nxt, msk->write_seq); 2634 2635 /* we still need to enqueue subflows or not really shutting down, 2636 * skip this 2637 */ 2638 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2639 mptcp_send_head(sk)) 2640 return; 2641 2642 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2643 2644 /* fallback socket will not get data_fin/ack, can move to the next 2645 * state now 2646 */ 2647 if (__mptcp_check_fallback(msk)) { 2648 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2649 inet_sk_state_store(sk, TCP_CLOSE); 2650 mptcp_close_wake_up(sk); 2651 } else if (sk->sk_state == TCP_FIN_WAIT1) { 2652 inet_sk_state_store(sk, TCP_FIN_WAIT2); 2653 } 2654 } 2655 2656 mptcp_flush_join_list(msk); 2657 mptcp_for_each_subflow(msk, subflow) { 2658 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2659 2660 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2661 } 2662 } 2663 2664 static void __mptcp_wr_shutdown(struct sock *sk) 2665 { 2666 struct mptcp_sock *msk = mptcp_sk(sk); 2667 2668 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2669 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2670 !!mptcp_send_head(sk)); 2671 2672 /* will be ignored by fallback sockets */ 2673 WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2674 WRITE_ONCE(msk->snd_data_fin_enable, 1); 2675 2676 __mptcp_check_send_data_fin(sk); 2677 } 2678 2679 static void __mptcp_destroy_sock(struct sock *sk) 2680 { 2681 struct mptcp_subflow_context *subflow, *tmp; 2682 struct mptcp_sock *msk = mptcp_sk(sk); 2683 LIST_HEAD(conn_list); 2684 2685 pr_debug("msk=%p", msk); 2686 2687 might_sleep(); 2688 2689 /* be sure to always acquire the join list lock, to sync vs 2690 * mptcp_finish_join(). 2691 */ 2692 spin_lock_bh(&msk->join_list_lock); 2693 list_splice_tail_init(&msk->join_list, &msk->conn_list); 2694 spin_unlock_bh(&msk->join_list_lock); 2695 list_splice_init(&msk->conn_list, &conn_list); 2696 2697 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 2698 sk_stop_timer(sk, &sk->sk_timer); 2699 msk->pm.status = 0; 2700 2701 list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2702 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2703 __mptcp_close_ssk(sk, ssk, subflow); 2704 } 2705 2706 sk->sk_prot->destroy(sk); 2707 2708 WARN_ON_ONCE(msk->wmem_reserved); 2709 WARN_ON_ONCE(msk->rmem_released); 2710 sk_stream_kill_queues(sk); 2711 xfrm_sk_free_policy(sk); 2712 2713 sk_refcnt_debug_release(sk); 2714 mptcp_dispose_initial_subflow(msk); 2715 sock_put(sk); 2716 } 2717 2718 static void mptcp_close(struct sock *sk, long timeout) 2719 { 2720 struct mptcp_subflow_context *subflow; 2721 bool do_cancel_work = false; 2722 2723 lock_sock(sk); 2724 sk->sk_shutdown = SHUTDOWN_MASK; 2725 2726 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2727 inet_sk_state_store(sk, TCP_CLOSE); 2728 goto cleanup; 2729 } 2730 2731 if (mptcp_close_state(sk)) 2732 __mptcp_wr_shutdown(sk); 2733 2734 sk_stream_wait_close(sk, timeout); 2735 2736 cleanup: 2737 /* orphan all the subflows */ 2738 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 2739 mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2740 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2741 bool slow = lock_sock_fast(ssk); 2742 2743 sock_orphan(ssk); 2744 unlock_sock_fast(ssk, slow); 2745 } 2746 sock_orphan(sk); 2747 2748 sock_hold(sk); 2749 pr_debug("msk=%p state=%d", sk, sk->sk_state); 2750 if (sk->sk_state == TCP_CLOSE) { 2751 __mptcp_destroy_sock(sk); 2752 do_cancel_work = true; 2753 } else { 2754 sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); 2755 } 2756 release_sock(sk); 2757 if (do_cancel_work) 2758 mptcp_cancel_work(sk); 2759 2760 if (mptcp_sk(sk)->token) 2761 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 2762 2763 sock_put(sk); 2764 } 2765 2766 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2767 { 2768 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2769 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2770 struct ipv6_pinfo *msk6 = inet6_sk(msk); 2771 2772 msk->sk_v6_daddr = ssk->sk_v6_daddr; 2773 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2774 2775 if (msk6 && ssk6) { 2776 msk6->saddr = ssk6->saddr; 2777 msk6->flow_label = ssk6->flow_label; 2778 } 2779 #endif 2780 2781 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2782 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2783 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2784 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2785 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2786 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2787 } 2788 2789 static int mptcp_disconnect(struct sock *sk, int flags) 2790 { 2791 struct mptcp_subflow_context *subflow; 2792 struct mptcp_sock *msk = mptcp_sk(sk); 2793 2794 mptcp_do_flush_join_list(msk); 2795 2796 mptcp_for_each_subflow(msk, subflow) { 2797 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2798 2799 lock_sock(ssk); 2800 tcp_disconnect(ssk, flags); 2801 release_sock(ssk); 2802 } 2803 return 0; 2804 } 2805 2806 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2807 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2808 { 2809 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2810 2811 return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2812 } 2813 #endif 2814 2815 struct sock *mptcp_sk_clone(const struct sock *sk, 2816 const struct mptcp_options_received *mp_opt, 2817 struct request_sock *req) 2818 { 2819 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2820 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 2821 struct mptcp_sock *msk; 2822 u64 ack_seq; 2823 2824 if (!nsk) 2825 return NULL; 2826 2827 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2828 if (nsk->sk_family == AF_INET6) 2829 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2830 #endif 2831 2832 __mptcp_init_sock(nsk); 2833 2834 msk = mptcp_sk(nsk); 2835 msk->local_key = subflow_req->local_key; 2836 msk->token = subflow_req->token; 2837 msk->subflow = NULL; 2838 WRITE_ONCE(msk->fully_established, false); 2839 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) 2840 WRITE_ONCE(msk->csum_enabled, true); 2841 2842 msk->write_seq = subflow_req->idsn + 1; 2843 msk->snd_nxt = msk->write_seq; 2844 msk->snd_una = msk->write_seq; 2845 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 2846 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 2847 2848 if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) { 2849 msk->can_ack = true; 2850 msk->remote_key = mp_opt->sndr_key; 2851 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 2852 ack_seq++; 2853 WRITE_ONCE(msk->ack_seq, ack_seq); 2854 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 2855 } 2856 2857 sock_reset_flag(nsk, SOCK_RCU_FREE); 2858 /* will be fully established after successful MPC subflow creation */ 2859 inet_sk_state_store(nsk, TCP_SYN_RECV); 2860 2861 security_inet_csk_clone(nsk, req); 2862 bh_unlock_sock(nsk); 2863 2864 /* keep a single reference */ 2865 __sock_put(nsk); 2866 return nsk; 2867 } 2868 2869 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2870 { 2871 const struct tcp_sock *tp = tcp_sk(ssk); 2872 2873 msk->rcvq_space.copied = 0; 2874 msk->rcvq_space.rtt_us = 0; 2875 2876 msk->rcvq_space.time = tp->tcp_mstamp; 2877 2878 /* initial rcv_space offering made to peer */ 2879 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2880 TCP_INIT_CWND * tp->advmss); 2881 if (msk->rcvq_space.space == 0) 2882 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 2883 2884 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 2885 } 2886 2887 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2888 bool kern) 2889 { 2890 struct mptcp_sock *msk = mptcp_sk(sk); 2891 struct socket *listener; 2892 struct sock *newsk; 2893 2894 listener = __mptcp_nmpc_socket(msk); 2895 if (WARN_ON_ONCE(!listener)) { 2896 *err = -EINVAL; 2897 return NULL; 2898 } 2899 2900 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 2901 newsk = inet_csk_accept(listener->sk, flags, err, kern); 2902 if (!newsk) 2903 return NULL; 2904 2905 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 2906 if (sk_is_mptcp(newsk)) { 2907 struct mptcp_subflow_context *subflow; 2908 struct sock *new_mptcp_sock; 2909 2910 subflow = mptcp_subflow_ctx(newsk); 2911 new_mptcp_sock = subflow->conn; 2912 2913 /* is_mptcp should be false if subflow->conn is missing, see 2914 * subflow_syn_recv_sock() 2915 */ 2916 if (WARN_ON_ONCE(!new_mptcp_sock)) { 2917 tcp_sk(newsk)->is_mptcp = 0; 2918 return newsk; 2919 } 2920 2921 /* acquire the 2nd reference for the owning socket */ 2922 sock_hold(new_mptcp_sock); 2923 newsk = new_mptcp_sock; 2924 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 2925 } else { 2926 MPTCP_INC_STATS(sock_net(sk), 2927 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 2928 } 2929 2930 return newsk; 2931 } 2932 2933 void mptcp_destroy_common(struct mptcp_sock *msk) 2934 { 2935 struct sock *sk = (struct sock *)msk; 2936 2937 __mptcp_clear_xmit(sk); 2938 2939 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 2940 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 2941 2942 skb_rbtree_purge(&msk->out_of_order_queue); 2943 mptcp_token_destroy(msk); 2944 mptcp_pm_free_anno_list(msk); 2945 } 2946 2947 static void mptcp_destroy(struct sock *sk) 2948 { 2949 struct mptcp_sock *msk = mptcp_sk(sk); 2950 2951 mptcp_destroy_common(msk); 2952 sk_sockets_allocated_dec(sk); 2953 } 2954 2955 void __mptcp_data_acked(struct sock *sk) 2956 { 2957 if (!sock_owned_by_user(sk)) 2958 __mptcp_clean_una(sk); 2959 else 2960 set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags); 2961 2962 if (mptcp_pending_data_fin_ack(sk)) 2963 mptcp_schedule_work(sk); 2964 } 2965 2966 void __mptcp_check_push(struct sock *sk, struct sock *ssk) 2967 { 2968 if (!mptcp_send_head(sk)) 2969 return; 2970 2971 if (!sock_owned_by_user(sk)) { 2972 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 2973 2974 if (xmit_ssk == ssk) 2975 __mptcp_subflow_push_pending(sk, ssk); 2976 else if (xmit_ssk) 2977 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); 2978 } else { 2979 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 2980 } 2981 } 2982 2983 /* processes deferred events and flush wmem */ 2984 static void mptcp_release_cb(struct sock *sk) 2985 { 2986 for (;;) { 2987 unsigned long flags = 0; 2988 2989 if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) 2990 flags |= BIT(MPTCP_PUSH_PENDING); 2991 if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags)) 2992 flags |= BIT(MPTCP_RETRANSMIT); 2993 if (!flags) 2994 break; 2995 2996 /* the following actions acquire the subflow socket lock 2997 * 2998 * 1) can't be invoked in atomic scope 2999 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 3000 * datapath acquires the msk socket spinlock while helding 3001 * the subflow socket lock 3002 */ 3003 3004 spin_unlock_bh(&sk->sk_lock.slock); 3005 if (flags & BIT(MPTCP_PUSH_PENDING)) 3006 __mptcp_push_pending(sk, 0); 3007 if (flags & BIT(MPTCP_RETRANSMIT)) 3008 __mptcp_retrans(sk); 3009 3010 cond_resched(); 3011 spin_lock_bh(&sk->sk_lock.slock); 3012 } 3013 3014 /* be sure to set the current sk state before tacking actions 3015 * depending on sk_state 3016 */ 3017 if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags)) 3018 __mptcp_set_connected(sk); 3019 if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags)) 3020 __mptcp_clean_una_wakeup(sk); 3021 if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags)) 3022 __mptcp_error_report(sk); 3023 3024 /* push_pending may touch wmem_reserved, ensure we do the cleanup 3025 * later 3026 */ 3027 __mptcp_update_wmem(sk); 3028 __mptcp_update_rmem(sk); 3029 } 3030 3031 void mptcp_subflow_process_delegated(struct sock *ssk) 3032 { 3033 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3034 struct sock *sk = subflow->conn; 3035 3036 mptcp_data_lock(sk); 3037 if (!sock_owned_by_user(sk)) 3038 __mptcp_subflow_push_pending(sk, ssk); 3039 else 3040 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 3041 mptcp_data_unlock(sk); 3042 mptcp_subflow_delegated_done(subflow); 3043 } 3044 3045 static int mptcp_hash(struct sock *sk) 3046 { 3047 /* should never be called, 3048 * we hash the TCP subflows not the master socket 3049 */ 3050 WARN_ON_ONCE(1); 3051 return 0; 3052 } 3053 3054 static void mptcp_unhash(struct sock *sk) 3055 { 3056 /* called from sk_common_release(), but nothing to do here */ 3057 } 3058 3059 static int mptcp_get_port(struct sock *sk, unsigned short snum) 3060 { 3061 struct mptcp_sock *msk = mptcp_sk(sk); 3062 struct socket *ssock; 3063 3064 ssock = __mptcp_nmpc_socket(msk); 3065 pr_debug("msk=%p, subflow=%p", msk, ssock); 3066 if (WARN_ON_ONCE(!ssock)) 3067 return -EINVAL; 3068 3069 return inet_csk_get_port(ssock->sk, snum); 3070 } 3071 3072 void mptcp_finish_connect(struct sock *ssk) 3073 { 3074 struct mptcp_subflow_context *subflow; 3075 struct mptcp_sock *msk; 3076 struct sock *sk; 3077 u64 ack_seq; 3078 3079 subflow = mptcp_subflow_ctx(ssk); 3080 sk = subflow->conn; 3081 msk = mptcp_sk(sk); 3082 3083 pr_debug("msk=%p, token=%u", sk, subflow->token); 3084 3085 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 3086 ack_seq++; 3087 subflow->map_seq = ack_seq; 3088 subflow->map_subflow_seq = 1; 3089 3090 /* the socket is not connected yet, no msk/subflow ops can access/race 3091 * accessing the field below 3092 */ 3093 WRITE_ONCE(msk->remote_key, subflow->remote_key); 3094 WRITE_ONCE(msk->local_key, subflow->local_key); 3095 WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3096 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 3097 WRITE_ONCE(msk->ack_seq, ack_seq); 3098 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 3099 WRITE_ONCE(msk->can_ack, 1); 3100 WRITE_ONCE(msk->snd_una, msk->write_seq); 3101 3102 mptcp_pm_new_connection(msk, ssk, 0); 3103 3104 mptcp_rcv_space_init(msk, ssk); 3105 } 3106 3107 void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3108 { 3109 write_lock_bh(&sk->sk_callback_lock); 3110 rcu_assign_pointer(sk->sk_wq, &parent->wq); 3111 sk_set_socket(sk, parent); 3112 sk->sk_uid = SOCK_INODE(parent)->i_uid; 3113 write_unlock_bh(&sk->sk_callback_lock); 3114 } 3115 3116 bool mptcp_finish_join(struct sock *ssk) 3117 { 3118 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3119 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3120 struct sock *parent = (void *)msk; 3121 struct socket *parent_sock; 3122 bool ret; 3123 3124 pr_debug("msk=%p, subflow=%p", msk, subflow); 3125 3126 /* mptcp socket already closing? */ 3127 if (!mptcp_is_fully_established(parent)) { 3128 subflow->reset_reason = MPTCP_RST_EMPTCP; 3129 return false; 3130 } 3131 3132 if (!msk->pm.server_side) 3133 goto out; 3134 3135 if (!mptcp_pm_allow_new_subflow(msk)) { 3136 subflow->reset_reason = MPTCP_RST_EPROHIBIT; 3137 return false; 3138 } 3139 3140 /* active connections are already on conn_list, and we can't acquire 3141 * msk lock here. 3142 * use the join list lock as synchronization point and double-check 3143 * msk status to avoid racing with __mptcp_destroy_sock() 3144 */ 3145 spin_lock_bh(&msk->join_list_lock); 3146 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 3147 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { 3148 list_add_tail(&subflow->node, &msk->join_list); 3149 sock_hold(ssk); 3150 } 3151 spin_unlock_bh(&msk->join_list_lock); 3152 if (!ret) { 3153 subflow->reset_reason = MPTCP_RST_EPROHIBIT; 3154 return false; 3155 } 3156 3157 /* attach to msk socket only after we are sure he will deal with us 3158 * at close time 3159 */ 3160 parent_sock = READ_ONCE(parent->sk_socket); 3161 if (parent_sock && !ssk->sk_socket) 3162 mptcp_sock_graft(ssk, parent_sock); 3163 subflow->map_seq = READ_ONCE(msk->ack_seq); 3164 out: 3165 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 3166 return true; 3167 } 3168 3169 static void mptcp_shutdown(struct sock *sk, int how) 3170 { 3171 pr_debug("sk=%p, how=%d", sk, how); 3172 3173 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 3174 __mptcp_wr_shutdown(sk); 3175 } 3176 3177 static struct proto mptcp_prot = { 3178 .name = "MPTCP", 3179 .owner = THIS_MODULE, 3180 .init = mptcp_init_sock, 3181 .disconnect = mptcp_disconnect, 3182 .close = mptcp_close, 3183 .accept = mptcp_accept, 3184 .setsockopt = mptcp_setsockopt, 3185 .getsockopt = mptcp_getsockopt, 3186 .shutdown = mptcp_shutdown, 3187 .destroy = mptcp_destroy, 3188 .sendmsg = mptcp_sendmsg, 3189 .recvmsg = mptcp_recvmsg, 3190 .release_cb = mptcp_release_cb, 3191 .hash = mptcp_hash, 3192 .unhash = mptcp_unhash, 3193 .get_port = mptcp_get_port, 3194 .sockets_allocated = &mptcp_sockets_allocated, 3195 .memory_allocated = &tcp_memory_allocated, 3196 .memory_pressure = &tcp_memory_pressure, 3197 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3198 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3199 .sysctl_mem = sysctl_tcp_mem, 3200 .obj_size = sizeof(struct mptcp_sock), 3201 .slab_flags = SLAB_TYPESAFE_BY_RCU, 3202 .no_autobind = true, 3203 }; 3204 3205 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 3206 { 3207 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3208 struct socket *ssock; 3209 int err; 3210 3211 lock_sock(sock->sk); 3212 ssock = __mptcp_nmpc_socket(msk); 3213 if (!ssock) { 3214 err = -EINVAL; 3215 goto unlock; 3216 } 3217 3218 err = ssock->ops->bind(ssock, uaddr, addr_len); 3219 if (!err) 3220 mptcp_copy_inaddrs(sock->sk, ssock->sk); 3221 3222 unlock: 3223 release_sock(sock->sk); 3224 return err; 3225 } 3226 3227 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 3228 struct mptcp_subflow_context *subflow) 3229 { 3230 subflow->request_mptcp = 0; 3231 __mptcp_do_fallback(msk); 3232 } 3233 3234 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 3235 int addr_len, int flags) 3236 { 3237 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3238 struct mptcp_subflow_context *subflow; 3239 struct socket *ssock; 3240 int err; 3241 3242 lock_sock(sock->sk); 3243 if (sock->state != SS_UNCONNECTED && msk->subflow) { 3244 /* pending connection or invalid state, let existing subflow 3245 * cope with that 3246 */ 3247 ssock = msk->subflow; 3248 goto do_connect; 3249 } 3250 3251 ssock = __mptcp_nmpc_socket(msk); 3252 if (!ssock) { 3253 err = -EINVAL; 3254 goto unlock; 3255 } 3256 3257 mptcp_token_destroy(msk); 3258 inet_sk_state_store(sock->sk, TCP_SYN_SENT); 3259 subflow = mptcp_subflow_ctx(ssock->sk); 3260 #ifdef CONFIG_TCP_MD5SIG 3261 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3262 * TCP option space. 3263 */ 3264 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 3265 mptcp_subflow_early_fallback(msk, subflow); 3266 #endif 3267 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3268 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 3269 mptcp_subflow_early_fallback(msk, subflow); 3270 } 3271 if (likely(!__mptcp_check_fallback(msk))) 3272 MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE); 3273 3274 do_connect: 3275 err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 3276 sock->state = ssock->state; 3277 3278 /* on successful connect, the msk state will be moved to established by 3279 * subflow_finish_connect() 3280 */ 3281 if (!err || err == -EINPROGRESS) 3282 mptcp_copy_inaddrs(sock->sk, ssock->sk); 3283 else 3284 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3285 3286 unlock: 3287 release_sock(sock->sk); 3288 return err; 3289 } 3290 3291 static int mptcp_listen(struct socket *sock, int backlog) 3292 { 3293 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3294 struct socket *ssock; 3295 int err; 3296 3297 pr_debug("msk=%p", msk); 3298 3299 lock_sock(sock->sk); 3300 ssock = __mptcp_nmpc_socket(msk); 3301 if (!ssock) { 3302 err = -EINVAL; 3303 goto unlock; 3304 } 3305 3306 mptcp_token_destroy(msk); 3307 inet_sk_state_store(sock->sk, TCP_LISTEN); 3308 sock_set_flag(sock->sk, SOCK_RCU_FREE); 3309 3310 err = ssock->ops->listen(ssock, backlog); 3311 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3312 if (!err) 3313 mptcp_copy_inaddrs(sock->sk, ssock->sk); 3314 3315 unlock: 3316 release_sock(sock->sk); 3317 return err; 3318 } 3319 3320 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3321 int flags, bool kern) 3322 { 3323 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3324 struct socket *ssock; 3325 int err; 3326 3327 pr_debug("msk=%p", msk); 3328 3329 lock_sock(sock->sk); 3330 if (sock->sk->sk_state != TCP_LISTEN) 3331 goto unlock_fail; 3332 3333 ssock = __mptcp_nmpc_socket(msk); 3334 if (!ssock) 3335 goto unlock_fail; 3336 3337 clear_bit(MPTCP_DATA_READY, &msk->flags); 3338 sock_hold(ssock->sk); 3339 release_sock(sock->sk); 3340 3341 err = ssock->ops->accept(sock, newsock, flags, kern); 3342 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3343 struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3344 struct mptcp_subflow_context *subflow; 3345 struct sock *newsk = newsock->sk; 3346 3347 lock_sock(newsk); 3348 3349 /* PM/worker can now acquire the first subflow socket 3350 * lock without racing with listener queue cleanup, 3351 * we can notify it, if needed. 3352 * 3353 * Even if remote has reset the initial subflow by now 3354 * the refcnt is still at least one. 3355 */ 3356 subflow = mptcp_subflow_ctx(msk->first); 3357 list_add(&subflow->node, &msk->conn_list); 3358 sock_hold(msk->first); 3359 if (mptcp_is_fully_established(newsk)) 3360 mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); 3361 3362 mptcp_copy_inaddrs(newsk, msk->first); 3363 mptcp_rcv_space_init(msk, msk->first); 3364 mptcp_propagate_sndbuf(newsk, msk->first); 3365 3366 /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3367 * This is needed so NOSPACE flag can be set from tcp stack. 3368 */ 3369 mptcp_flush_join_list(msk); 3370 mptcp_for_each_subflow(msk, subflow) { 3371 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3372 3373 if (!ssk->sk_socket) 3374 mptcp_sock_graft(ssk, newsock); 3375 } 3376 release_sock(newsk); 3377 } 3378 3379 if (inet_csk_listen_poll(ssock->sk)) 3380 set_bit(MPTCP_DATA_READY, &msk->flags); 3381 sock_put(ssock->sk); 3382 return err; 3383 3384 unlock_fail: 3385 release_sock(sock->sk); 3386 return -EINVAL; 3387 } 3388 3389 static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 3390 { 3391 return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 3392 0; 3393 } 3394 3395 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 3396 { 3397 struct sock *sk = (struct sock *)msk; 3398 3399 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3400 return EPOLLOUT | EPOLLWRNORM; 3401 3402 if (sk_stream_is_writeable(sk)) 3403 return EPOLLOUT | EPOLLWRNORM; 3404 3405 mptcp_set_nospace(sk); 3406 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 3407 if (sk_stream_is_writeable(sk)) 3408 return EPOLLOUT | EPOLLWRNORM; 3409 3410 return 0; 3411 } 3412 3413 static __poll_t mptcp_poll(struct file *file, struct socket *sock, 3414 struct poll_table_struct *wait) 3415 { 3416 struct sock *sk = sock->sk; 3417 struct mptcp_sock *msk; 3418 __poll_t mask = 0; 3419 int state; 3420 3421 msk = mptcp_sk(sk); 3422 sock_poll_wait(file, sock, wait); 3423 3424 state = inet_sk_state_load(sk); 3425 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 3426 if (state == TCP_LISTEN) 3427 return mptcp_check_readable(msk); 3428 3429 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 3430 mask |= mptcp_check_readable(msk); 3431 mask |= mptcp_check_writeable(msk); 3432 } 3433 if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3434 mask |= EPOLLHUP; 3435 if (sk->sk_shutdown & RCV_SHUTDOWN) 3436 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 3437 3438 /* This barrier is coupled with smp_wmb() in tcp_reset() */ 3439 smp_rmb(); 3440 if (sk->sk_err) 3441 mask |= EPOLLERR; 3442 3443 return mask; 3444 } 3445 3446 static const struct proto_ops mptcp_stream_ops = { 3447 .family = PF_INET, 3448 .owner = THIS_MODULE, 3449 .release = inet_release, 3450 .bind = mptcp_bind, 3451 .connect = mptcp_stream_connect, 3452 .socketpair = sock_no_socketpair, 3453 .accept = mptcp_stream_accept, 3454 .getname = inet_getname, 3455 .poll = mptcp_poll, 3456 .ioctl = inet_ioctl, 3457 .gettstamp = sock_gettstamp, 3458 .listen = mptcp_listen, 3459 .shutdown = inet_shutdown, 3460 .setsockopt = sock_common_setsockopt, 3461 .getsockopt = sock_common_getsockopt, 3462 .sendmsg = inet_sendmsg, 3463 .recvmsg = inet_recvmsg, 3464 .mmap = sock_no_mmap, 3465 .sendpage = inet_sendpage, 3466 }; 3467 3468 static struct inet_protosw mptcp_protosw = { 3469 .type = SOCK_STREAM, 3470 .protocol = IPPROTO_MPTCP, 3471 .prot = &mptcp_prot, 3472 .ops = &mptcp_stream_ops, 3473 .flags = INET_PROTOSW_ICSK, 3474 }; 3475 3476 static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3477 { 3478 struct mptcp_delegated_action *delegated; 3479 struct mptcp_subflow_context *subflow; 3480 int work_done = 0; 3481 3482 delegated = container_of(napi, struct mptcp_delegated_action, napi); 3483 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3484 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3485 3486 bh_lock_sock_nested(ssk); 3487 if (!sock_owned_by_user(ssk) && 3488 mptcp_subflow_has_delegated_action(subflow)) 3489 mptcp_subflow_process_delegated(ssk); 3490 /* ... elsewhere tcp_release_cb_override already processed 3491 * the action or will do at next release_sock(). 3492 * In both case must dequeue the subflow here - on the same 3493 * CPU that scheduled it. 3494 */ 3495 bh_unlock_sock(ssk); 3496 sock_put(ssk); 3497 3498 if (++work_done == budget) 3499 return budget; 3500 } 3501 3502 /* always provide a 0 'work_done' argument, so that napi_complete_done 3503 * will not try accessing the NULL napi->dev ptr 3504 */ 3505 napi_complete_done(napi, 0); 3506 return work_done; 3507 } 3508 3509 void __init mptcp_proto_init(void) 3510 { 3511 struct mptcp_delegated_action *delegated; 3512 int cpu; 3513 3514 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 3515 3516 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3517 panic("Failed to allocate MPTCP pcpu counter\n"); 3518 3519 init_dummy_netdev(&mptcp_napi_dev); 3520 for_each_possible_cpu(cpu) { 3521 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3522 INIT_LIST_HEAD(&delegated->head); 3523 netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll, 3524 NAPI_POLL_WEIGHT); 3525 napi_enable(&delegated->napi); 3526 } 3527 3528 mptcp_subflow_init(); 3529 mptcp_pm_init(); 3530 mptcp_token_init(); 3531 3532 if (proto_register(&mptcp_prot, 1) != 0) 3533 panic("Failed to register MPTCP proto.\n"); 3534 3535 inet_register_protosw(&mptcp_protosw); 3536 3537 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3538 } 3539 3540 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3541 static const struct proto_ops mptcp_v6_stream_ops = { 3542 .family = PF_INET6, 3543 .owner = THIS_MODULE, 3544 .release = inet6_release, 3545 .bind = mptcp_bind, 3546 .connect = mptcp_stream_connect, 3547 .socketpair = sock_no_socketpair, 3548 .accept = mptcp_stream_accept, 3549 .getname = inet6_getname, 3550 .poll = mptcp_poll, 3551 .ioctl = inet6_ioctl, 3552 .gettstamp = sock_gettstamp, 3553 .listen = mptcp_listen, 3554 .shutdown = inet_shutdown, 3555 .setsockopt = sock_common_setsockopt, 3556 .getsockopt = sock_common_getsockopt, 3557 .sendmsg = inet6_sendmsg, 3558 .recvmsg = inet6_recvmsg, 3559 .mmap = sock_no_mmap, 3560 .sendpage = inet_sendpage, 3561 #ifdef CONFIG_COMPAT 3562 .compat_ioctl = inet6_compat_ioctl, 3563 #endif 3564 }; 3565 3566 static struct proto mptcp_v6_prot; 3567 3568 static void mptcp_v6_destroy(struct sock *sk) 3569 { 3570 mptcp_destroy(sk); 3571 inet6_destroy_sock(sk); 3572 } 3573 3574 static struct inet_protosw mptcp_v6_protosw = { 3575 .type = SOCK_STREAM, 3576 .protocol = IPPROTO_MPTCP, 3577 .prot = &mptcp_v6_prot, 3578 .ops = &mptcp_v6_stream_ops, 3579 .flags = INET_PROTOSW_ICSK, 3580 }; 3581 3582 int __init mptcp_proto_v6_init(void) 3583 { 3584 int err; 3585 3586 mptcp_v6_prot = mptcp_prot; 3587 strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3588 mptcp_v6_prot.slab = NULL; 3589 mptcp_v6_prot.destroy = mptcp_v6_destroy; 3590 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3591 3592 err = proto_register(&mptcp_v6_prot, 1); 3593 if (err) 3594 return err; 3595 3596 err = inet6_register_protosw(&mptcp_v6_protosw); 3597 if (err) 3598 proto_unregister(&mptcp_v6_prot); 3599 3600 return err; 3601 } 3602 #endif 3603