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