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