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