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