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