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