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 tcp_assign_congestion_control(sk); 2428 2429 return 0; 2430 } 2431 2432 static int mptcp_init_sock(struct sock *sk) 2433 { 2434 struct net *net = sock_net(sk); 2435 int ret; 2436 2437 ret = __mptcp_init_sock(sk); 2438 if (ret) 2439 return ret; 2440 2441 if (!mptcp_is_enabled(net)) 2442 return -ENOPROTOOPT; 2443 2444 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 2445 return -ENOMEM; 2446 2447 ret = __mptcp_socket_create(mptcp_sk(sk)); 2448 if (ret) 2449 return ret; 2450 2451 sk_sockets_allocated_inc(sk); 2452 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1]; 2453 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1]; 2454 2455 return 0; 2456 } 2457 2458 static void __mptcp_clear_xmit(struct sock *sk) 2459 { 2460 struct mptcp_sock *msk = mptcp_sk(sk); 2461 struct mptcp_data_frag *dtmp, *dfrag; 2462 struct sk_buff *skb; 2463 2464 WRITE_ONCE(msk->first_pending, NULL); 2465 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 2466 dfrag_clear(sk, dfrag); 2467 while ((skb = __skb_dequeue(&msk->skb_tx_cache)) != NULL) { 2468 sk->sk_forward_alloc += skb->truesize; 2469 kfree_skb(skb); 2470 } 2471 } 2472 2473 static void mptcp_cancel_work(struct sock *sk) 2474 { 2475 struct mptcp_sock *msk = mptcp_sk(sk); 2476 2477 if (cancel_work_sync(&msk->work)) 2478 __sock_put(sk); 2479 } 2480 2481 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how) 2482 { 2483 lock_sock(ssk); 2484 2485 switch (ssk->sk_state) { 2486 case TCP_LISTEN: 2487 if (!(how & RCV_SHUTDOWN)) 2488 break; 2489 fallthrough; 2490 case TCP_SYN_SENT: 2491 tcp_disconnect(ssk, O_NONBLOCK); 2492 break; 2493 default: 2494 if (__mptcp_check_fallback(mptcp_sk(sk))) { 2495 pr_debug("Fallback"); 2496 ssk->sk_shutdown |= how; 2497 tcp_shutdown(ssk, how); 2498 } else { 2499 pr_debug("Sending DATA_FIN on subflow %p", ssk); 2500 mptcp_set_timeout(sk, ssk); 2501 tcp_send_ack(ssk); 2502 if (!mptcp_timer_pending(sk)) 2503 mptcp_reset_timer(sk); 2504 } 2505 break; 2506 } 2507 2508 release_sock(ssk); 2509 } 2510 2511 static const unsigned char new_state[16] = { 2512 /* current state: new state: action: */ 2513 [0 /* (Invalid) */] = TCP_CLOSE, 2514 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 2515 [TCP_SYN_SENT] = TCP_CLOSE, 2516 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, 2517 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1, 2518 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2, 2519 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */ 2520 [TCP_CLOSE] = TCP_CLOSE, 2521 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN, 2522 [TCP_LAST_ACK] = TCP_LAST_ACK, 2523 [TCP_LISTEN] = TCP_CLOSE, 2524 [TCP_CLOSING] = TCP_CLOSING, 2525 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */ 2526 }; 2527 2528 static int mptcp_close_state(struct sock *sk) 2529 { 2530 int next = (int)new_state[sk->sk_state]; 2531 int ns = next & TCP_STATE_MASK; 2532 2533 inet_sk_state_store(sk, ns); 2534 2535 return next & TCP_ACTION_FIN; 2536 } 2537 2538 static void __mptcp_check_send_data_fin(struct sock *sk) 2539 { 2540 struct mptcp_subflow_context *subflow; 2541 struct mptcp_sock *msk = mptcp_sk(sk); 2542 2543 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu", 2544 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk), 2545 msk->snd_nxt, msk->write_seq); 2546 2547 /* we still need to enqueue subflows or not really shutting down, 2548 * skip this 2549 */ 2550 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq || 2551 mptcp_send_head(sk)) 2552 return; 2553 2554 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 2555 2556 /* fallback socket will not get data_fin/ack, can move to the next 2557 * state now 2558 */ 2559 if (__mptcp_check_fallback(msk)) { 2560 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) { 2561 inet_sk_state_store(sk, TCP_CLOSE); 2562 mptcp_close_wake_up(sk); 2563 } else if (sk->sk_state == TCP_FIN_WAIT1) { 2564 inet_sk_state_store(sk, TCP_FIN_WAIT2); 2565 } 2566 } 2567 2568 mptcp_flush_join_list(msk); 2569 mptcp_for_each_subflow(msk, subflow) { 2570 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2571 2572 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN); 2573 } 2574 } 2575 2576 static void __mptcp_wr_shutdown(struct sock *sk) 2577 { 2578 struct mptcp_sock *msk = mptcp_sk(sk); 2579 2580 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d", 2581 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state, 2582 !!mptcp_send_head(sk)); 2583 2584 /* will be ignored by fallback sockets */ 2585 WRITE_ONCE(msk->write_seq, msk->write_seq + 1); 2586 WRITE_ONCE(msk->snd_data_fin_enable, 1); 2587 2588 __mptcp_check_send_data_fin(sk); 2589 } 2590 2591 static void __mptcp_destroy_sock(struct sock *sk) 2592 { 2593 struct mptcp_subflow_context *subflow, *tmp; 2594 struct mptcp_sock *msk = mptcp_sk(sk); 2595 LIST_HEAD(conn_list); 2596 2597 pr_debug("msk=%p", msk); 2598 2599 might_sleep(); 2600 2601 /* be sure to always acquire the join list lock, to sync vs 2602 * mptcp_finish_join(). 2603 */ 2604 spin_lock_bh(&msk->join_list_lock); 2605 list_splice_tail_init(&msk->join_list, &msk->conn_list); 2606 spin_unlock_bh(&msk->join_list_lock); 2607 list_splice_init(&msk->conn_list, &conn_list); 2608 2609 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 2610 sk_stop_timer(sk, &sk->sk_timer); 2611 msk->pm.status = 0; 2612 2613 list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 2614 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2615 __mptcp_close_ssk(sk, ssk, subflow); 2616 } 2617 2618 sk->sk_prot->destroy(sk); 2619 2620 WARN_ON_ONCE(msk->wmem_reserved); 2621 WARN_ON_ONCE(msk->rmem_released); 2622 sk_stream_kill_queues(sk); 2623 xfrm_sk_free_policy(sk); 2624 2625 tcp_cleanup_congestion_control(sk); 2626 sk_refcnt_debug_release(sk); 2627 mptcp_dispose_initial_subflow(msk); 2628 sock_put(sk); 2629 } 2630 2631 static void mptcp_close(struct sock *sk, long timeout) 2632 { 2633 struct mptcp_subflow_context *subflow; 2634 bool do_cancel_work = false; 2635 2636 lock_sock(sk); 2637 sk->sk_shutdown = SHUTDOWN_MASK; 2638 2639 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) { 2640 inet_sk_state_store(sk, TCP_CLOSE); 2641 goto cleanup; 2642 } 2643 2644 if (mptcp_close_state(sk)) 2645 __mptcp_wr_shutdown(sk); 2646 2647 sk_stream_wait_close(sk, timeout); 2648 2649 cleanup: 2650 /* orphan all the subflows */ 2651 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32; 2652 mptcp_for_each_subflow(mptcp_sk(sk), subflow) { 2653 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2654 bool slow = lock_sock_fast(ssk); 2655 2656 sock_orphan(ssk); 2657 unlock_sock_fast(ssk, slow); 2658 } 2659 sock_orphan(sk); 2660 2661 sock_hold(sk); 2662 pr_debug("msk=%p state=%d", sk, sk->sk_state); 2663 if (sk->sk_state == TCP_CLOSE) { 2664 __mptcp_destroy_sock(sk); 2665 do_cancel_work = true; 2666 } else { 2667 sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN); 2668 } 2669 release_sock(sk); 2670 if (do_cancel_work) 2671 mptcp_cancel_work(sk); 2672 2673 if (mptcp_sk(sk)->token) 2674 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL); 2675 2676 sock_put(sk); 2677 } 2678 2679 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 2680 { 2681 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2682 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 2683 struct ipv6_pinfo *msk6 = inet6_sk(msk); 2684 2685 msk->sk_v6_daddr = ssk->sk_v6_daddr; 2686 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 2687 2688 if (msk6 && ssk6) { 2689 msk6->saddr = ssk6->saddr; 2690 msk6->flow_label = ssk6->flow_label; 2691 } 2692 #endif 2693 2694 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 2695 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 2696 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 2697 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 2698 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 2699 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 2700 } 2701 2702 static int mptcp_disconnect(struct sock *sk, int flags) 2703 { 2704 struct mptcp_subflow_context *subflow; 2705 struct mptcp_sock *msk = mptcp_sk(sk); 2706 2707 mptcp_do_flush_join_list(msk); 2708 2709 mptcp_for_each_subflow(msk, subflow) { 2710 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 2711 2712 lock_sock(ssk); 2713 tcp_disconnect(ssk, flags); 2714 release_sock(ssk); 2715 } 2716 return 0; 2717 } 2718 2719 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2720 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 2721 { 2722 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 2723 2724 return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 2725 } 2726 #endif 2727 2728 struct sock *mptcp_sk_clone(const struct sock *sk, 2729 const struct mptcp_options_received *mp_opt, 2730 struct request_sock *req) 2731 { 2732 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 2733 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 2734 struct mptcp_sock *msk; 2735 u64 ack_seq; 2736 2737 if (!nsk) 2738 return NULL; 2739 2740 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2741 if (nsk->sk_family == AF_INET6) 2742 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 2743 #endif 2744 2745 __mptcp_init_sock(nsk); 2746 2747 msk = mptcp_sk(nsk); 2748 msk->local_key = subflow_req->local_key; 2749 msk->token = subflow_req->token; 2750 msk->subflow = NULL; 2751 WRITE_ONCE(msk->fully_established, false); 2752 2753 msk->write_seq = subflow_req->idsn + 1; 2754 msk->snd_nxt = msk->write_seq; 2755 msk->snd_una = msk->write_seq; 2756 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd; 2757 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq; 2758 2759 if (mp_opt->mp_capable) { 2760 msk->can_ack = true; 2761 msk->remote_key = mp_opt->sndr_key; 2762 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 2763 ack_seq++; 2764 WRITE_ONCE(msk->ack_seq, ack_seq); 2765 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 2766 } 2767 2768 sock_reset_flag(nsk, SOCK_RCU_FREE); 2769 /* will be fully established after successful MPC subflow creation */ 2770 inet_sk_state_store(nsk, TCP_SYN_RECV); 2771 2772 security_inet_csk_clone(nsk, req); 2773 bh_unlock_sock(nsk); 2774 2775 /* keep a single reference */ 2776 __sock_put(nsk); 2777 return nsk; 2778 } 2779 2780 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk) 2781 { 2782 const struct tcp_sock *tp = tcp_sk(ssk); 2783 2784 msk->rcvq_space.copied = 0; 2785 msk->rcvq_space.rtt_us = 0; 2786 2787 msk->rcvq_space.time = tp->tcp_mstamp; 2788 2789 /* initial rcv_space offering made to peer */ 2790 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd, 2791 TCP_INIT_CWND * tp->advmss); 2792 if (msk->rcvq_space.space == 0) 2793 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT; 2794 2795 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd); 2796 } 2797 2798 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 2799 bool kern) 2800 { 2801 struct mptcp_sock *msk = mptcp_sk(sk); 2802 struct socket *listener; 2803 struct sock *newsk; 2804 2805 listener = __mptcp_nmpc_socket(msk); 2806 if (WARN_ON_ONCE(!listener)) { 2807 *err = -EINVAL; 2808 return NULL; 2809 } 2810 2811 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 2812 newsk = inet_csk_accept(listener->sk, flags, err, kern); 2813 if (!newsk) 2814 return NULL; 2815 2816 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 2817 if (sk_is_mptcp(newsk)) { 2818 struct mptcp_subflow_context *subflow; 2819 struct sock *new_mptcp_sock; 2820 2821 subflow = mptcp_subflow_ctx(newsk); 2822 new_mptcp_sock = subflow->conn; 2823 2824 /* is_mptcp should be false if subflow->conn is missing, see 2825 * subflow_syn_recv_sock() 2826 */ 2827 if (WARN_ON_ONCE(!new_mptcp_sock)) { 2828 tcp_sk(newsk)->is_mptcp = 0; 2829 return newsk; 2830 } 2831 2832 /* acquire the 2nd reference for the owning socket */ 2833 sock_hold(new_mptcp_sock); 2834 newsk = new_mptcp_sock; 2835 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 2836 } else { 2837 MPTCP_INC_STATS(sock_net(sk), 2838 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 2839 } 2840 2841 return newsk; 2842 } 2843 2844 void mptcp_destroy_common(struct mptcp_sock *msk) 2845 { 2846 struct sock *sk = (struct sock *)msk; 2847 2848 __mptcp_clear_xmit(sk); 2849 2850 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */ 2851 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue); 2852 2853 skb_rbtree_purge(&msk->out_of_order_queue); 2854 mptcp_token_destroy(msk); 2855 mptcp_pm_free_anno_list(msk); 2856 } 2857 2858 static void mptcp_destroy(struct sock *sk) 2859 { 2860 struct mptcp_sock *msk = mptcp_sk(sk); 2861 2862 mptcp_destroy_common(msk); 2863 sk_sockets_allocated_dec(sk); 2864 } 2865 2866 void __mptcp_data_acked(struct sock *sk) 2867 { 2868 if (!sock_owned_by_user(sk)) 2869 __mptcp_clean_una(sk); 2870 else 2871 set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags); 2872 2873 if (mptcp_pending_data_fin_ack(sk)) 2874 mptcp_schedule_work(sk); 2875 } 2876 2877 void __mptcp_check_push(struct sock *sk, struct sock *ssk) 2878 { 2879 if (!mptcp_send_head(sk)) 2880 return; 2881 2882 if (!sock_owned_by_user(sk)) { 2883 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk)); 2884 2885 if (xmit_ssk == ssk) 2886 __mptcp_subflow_push_pending(sk, ssk); 2887 else if (xmit_ssk) 2888 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk)); 2889 } else { 2890 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 2891 } 2892 } 2893 2894 /* processes deferred events and flush wmem */ 2895 static void mptcp_release_cb(struct sock *sk) 2896 { 2897 for (;;) { 2898 unsigned long flags = 0; 2899 2900 if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags)) 2901 flags |= BIT(MPTCP_PUSH_PENDING); 2902 if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags)) 2903 flags |= BIT(MPTCP_RETRANSMIT); 2904 if (!flags) 2905 break; 2906 2907 /* the following actions acquire the subflow socket lock 2908 * 2909 * 1) can't be invoked in atomic scope 2910 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX 2911 * datapath acquires the msk socket spinlock while helding 2912 * the subflow socket lock 2913 */ 2914 2915 spin_unlock_bh(&sk->sk_lock.slock); 2916 if (flags & BIT(MPTCP_PUSH_PENDING)) 2917 __mptcp_push_pending(sk, 0); 2918 if (flags & BIT(MPTCP_RETRANSMIT)) 2919 __mptcp_retrans(sk); 2920 2921 cond_resched(); 2922 spin_lock_bh(&sk->sk_lock.slock); 2923 } 2924 2925 if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags)) 2926 __mptcp_clean_una_wakeup(sk); 2927 if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags)) 2928 __mptcp_error_report(sk); 2929 2930 /* push_pending may touch wmem_reserved, ensure we do the cleanup 2931 * later 2932 */ 2933 __mptcp_update_wmem(sk); 2934 __mptcp_update_rmem(sk); 2935 } 2936 2937 void mptcp_subflow_process_delegated(struct sock *ssk) 2938 { 2939 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 2940 struct sock *sk = subflow->conn; 2941 2942 mptcp_data_lock(sk); 2943 if (!sock_owned_by_user(sk)) 2944 __mptcp_subflow_push_pending(sk, ssk); 2945 else 2946 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags); 2947 mptcp_data_unlock(sk); 2948 mptcp_subflow_delegated_done(subflow); 2949 } 2950 2951 static int mptcp_hash(struct sock *sk) 2952 { 2953 /* should never be called, 2954 * we hash the TCP subflows not the master socket 2955 */ 2956 WARN_ON_ONCE(1); 2957 return 0; 2958 } 2959 2960 static void mptcp_unhash(struct sock *sk) 2961 { 2962 /* called from sk_common_release(), but nothing to do here */ 2963 } 2964 2965 static int mptcp_get_port(struct sock *sk, unsigned short snum) 2966 { 2967 struct mptcp_sock *msk = mptcp_sk(sk); 2968 struct socket *ssock; 2969 2970 ssock = __mptcp_nmpc_socket(msk); 2971 pr_debug("msk=%p, subflow=%p", msk, ssock); 2972 if (WARN_ON_ONCE(!ssock)) 2973 return -EINVAL; 2974 2975 return inet_csk_get_port(ssock->sk, snum); 2976 } 2977 2978 void mptcp_finish_connect(struct sock *ssk) 2979 { 2980 struct mptcp_subflow_context *subflow; 2981 struct mptcp_sock *msk; 2982 struct sock *sk; 2983 u64 ack_seq; 2984 2985 subflow = mptcp_subflow_ctx(ssk); 2986 sk = subflow->conn; 2987 msk = mptcp_sk(sk); 2988 2989 pr_debug("msk=%p, token=%u", sk, subflow->token); 2990 2991 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 2992 ack_seq++; 2993 subflow->map_seq = ack_seq; 2994 subflow->map_subflow_seq = 1; 2995 2996 /* the socket is not connected yet, no msk/subflow ops can access/race 2997 * accessing the field below 2998 */ 2999 WRITE_ONCE(msk->remote_key, subflow->remote_key); 3000 WRITE_ONCE(msk->local_key, subflow->local_key); 3001 WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 3002 WRITE_ONCE(msk->snd_nxt, msk->write_seq); 3003 WRITE_ONCE(msk->ack_seq, ack_seq); 3004 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 3005 WRITE_ONCE(msk->can_ack, 1); 3006 WRITE_ONCE(msk->snd_una, msk->write_seq); 3007 3008 mptcp_pm_new_connection(msk, ssk, 0); 3009 3010 mptcp_rcv_space_init(msk, ssk); 3011 } 3012 3013 void mptcp_sock_graft(struct sock *sk, struct socket *parent) 3014 { 3015 write_lock_bh(&sk->sk_callback_lock); 3016 rcu_assign_pointer(sk->sk_wq, &parent->wq); 3017 sk_set_socket(sk, parent); 3018 sk->sk_uid = SOCK_INODE(parent)->i_uid; 3019 write_unlock_bh(&sk->sk_callback_lock); 3020 } 3021 3022 bool mptcp_finish_join(struct sock *ssk) 3023 { 3024 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 3025 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 3026 struct sock *parent = (void *)msk; 3027 struct socket *parent_sock; 3028 bool ret; 3029 3030 pr_debug("msk=%p, subflow=%p", msk, subflow); 3031 3032 /* mptcp socket already closing? */ 3033 if (!mptcp_is_fully_established(parent)) { 3034 subflow->reset_reason = MPTCP_RST_EMPTCP; 3035 return false; 3036 } 3037 3038 if (!msk->pm.server_side) 3039 goto out; 3040 3041 if (!mptcp_pm_allow_new_subflow(msk)) { 3042 subflow->reset_reason = MPTCP_RST_EPROHIBIT; 3043 return false; 3044 } 3045 3046 /* active connections are already on conn_list, and we can't acquire 3047 * msk lock here. 3048 * use the join list lock as synchronization point and double-check 3049 * msk status to avoid racing with __mptcp_destroy_sock() 3050 */ 3051 spin_lock_bh(&msk->join_list_lock); 3052 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 3053 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) { 3054 list_add_tail(&subflow->node, &msk->join_list); 3055 sock_hold(ssk); 3056 } 3057 spin_unlock_bh(&msk->join_list_lock); 3058 if (!ret) { 3059 subflow->reset_reason = MPTCP_RST_EPROHIBIT; 3060 return false; 3061 } 3062 3063 /* attach to msk socket only after we are sure he will deal with us 3064 * at close time 3065 */ 3066 parent_sock = READ_ONCE(parent->sk_socket); 3067 if (parent_sock && !ssk->sk_socket) 3068 mptcp_sock_graft(ssk, parent_sock); 3069 subflow->map_seq = READ_ONCE(msk->ack_seq); 3070 out: 3071 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC); 3072 return true; 3073 } 3074 3075 static void mptcp_shutdown(struct sock *sk, int how) 3076 { 3077 pr_debug("sk=%p, how=%d", sk, how); 3078 3079 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk)) 3080 __mptcp_wr_shutdown(sk); 3081 } 3082 3083 static struct proto mptcp_prot = { 3084 .name = "MPTCP", 3085 .owner = THIS_MODULE, 3086 .init = mptcp_init_sock, 3087 .disconnect = mptcp_disconnect, 3088 .close = mptcp_close, 3089 .accept = mptcp_accept, 3090 .setsockopt = mptcp_setsockopt, 3091 .getsockopt = mptcp_getsockopt, 3092 .shutdown = mptcp_shutdown, 3093 .destroy = mptcp_destroy, 3094 .sendmsg = mptcp_sendmsg, 3095 .recvmsg = mptcp_recvmsg, 3096 .release_cb = mptcp_release_cb, 3097 .hash = mptcp_hash, 3098 .unhash = mptcp_unhash, 3099 .get_port = mptcp_get_port, 3100 .sockets_allocated = &mptcp_sockets_allocated, 3101 .memory_allocated = &tcp_memory_allocated, 3102 .memory_pressure = &tcp_memory_pressure, 3103 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3104 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3105 .sysctl_mem = sysctl_tcp_mem, 3106 .obj_size = sizeof(struct mptcp_sock), 3107 .slab_flags = SLAB_TYPESAFE_BY_RCU, 3108 .no_autobind = true, 3109 }; 3110 3111 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 3112 { 3113 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3114 struct socket *ssock; 3115 int err; 3116 3117 lock_sock(sock->sk); 3118 ssock = __mptcp_nmpc_socket(msk); 3119 if (!ssock) { 3120 err = -EINVAL; 3121 goto unlock; 3122 } 3123 3124 err = ssock->ops->bind(ssock, uaddr, addr_len); 3125 if (!err) 3126 mptcp_copy_inaddrs(sock->sk, ssock->sk); 3127 3128 unlock: 3129 release_sock(sock->sk); 3130 return err; 3131 } 3132 3133 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk, 3134 struct mptcp_subflow_context *subflow) 3135 { 3136 subflow->request_mptcp = 0; 3137 __mptcp_do_fallback(msk); 3138 } 3139 3140 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 3141 int addr_len, int flags) 3142 { 3143 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3144 struct mptcp_subflow_context *subflow; 3145 struct socket *ssock; 3146 int err; 3147 3148 lock_sock(sock->sk); 3149 if (sock->state != SS_UNCONNECTED && msk->subflow) { 3150 /* pending connection or invalid state, let existing subflow 3151 * cope with that 3152 */ 3153 ssock = msk->subflow; 3154 goto do_connect; 3155 } 3156 3157 ssock = __mptcp_nmpc_socket(msk); 3158 if (!ssock) { 3159 err = -EINVAL; 3160 goto unlock; 3161 } 3162 3163 mptcp_token_destroy(msk); 3164 inet_sk_state_store(sock->sk, TCP_SYN_SENT); 3165 subflow = mptcp_subflow_ctx(ssock->sk); 3166 #ifdef CONFIG_TCP_MD5SIG 3167 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 3168 * TCP option space. 3169 */ 3170 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 3171 mptcp_subflow_early_fallback(msk, subflow); 3172 #endif 3173 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) { 3174 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT); 3175 mptcp_subflow_early_fallback(msk, subflow); 3176 } 3177 if (likely(!__mptcp_check_fallback(msk))) 3178 MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE); 3179 3180 do_connect: 3181 err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 3182 sock->state = ssock->state; 3183 3184 /* on successful connect, the msk state will be moved to established by 3185 * subflow_finish_connect() 3186 */ 3187 if (!err || err == -EINPROGRESS) 3188 mptcp_copy_inaddrs(sock->sk, ssock->sk); 3189 else 3190 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3191 3192 unlock: 3193 release_sock(sock->sk); 3194 return err; 3195 } 3196 3197 static int mptcp_listen(struct socket *sock, int backlog) 3198 { 3199 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3200 struct socket *ssock; 3201 int err; 3202 3203 pr_debug("msk=%p", msk); 3204 3205 lock_sock(sock->sk); 3206 ssock = __mptcp_nmpc_socket(msk); 3207 if (!ssock) { 3208 err = -EINVAL; 3209 goto unlock; 3210 } 3211 3212 mptcp_token_destroy(msk); 3213 inet_sk_state_store(sock->sk, TCP_LISTEN); 3214 sock_set_flag(sock->sk, SOCK_RCU_FREE); 3215 3216 err = ssock->ops->listen(ssock, backlog); 3217 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 3218 if (!err) 3219 mptcp_copy_inaddrs(sock->sk, ssock->sk); 3220 3221 unlock: 3222 release_sock(sock->sk); 3223 return err; 3224 } 3225 3226 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 3227 int flags, bool kern) 3228 { 3229 struct mptcp_sock *msk = mptcp_sk(sock->sk); 3230 struct socket *ssock; 3231 int err; 3232 3233 pr_debug("msk=%p", msk); 3234 3235 lock_sock(sock->sk); 3236 if (sock->sk->sk_state != TCP_LISTEN) 3237 goto unlock_fail; 3238 3239 ssock = __mptcp_nmpc_socket(msk); 3240 if (!ssock) 3241 goto unlock_fail; 3242 3243 clear_bit(MPTCP_DATA_READY, &msk->flags); 3244 sock_hold(ssock->sk); 3245 release_sock(sock->sk); 3246 3247 err = ssock->ops->accept(sock, newsock, flags, kern); 3248 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) { 3249 struct mptcp_sock *msk = mptcp_sk(newsock->sk); 3250 struct mptcp_subflow_context *subflow; 3251 struct sock *newsk = newsock->sk; 3252 3253 lock_sock(newsk); 3254 3255 /* PM/worker can now acquire the first subflow socket 3256 * lock without racing with listener queue cleanup, 3257 * we can notify it, if needed. 3258 * 3259 * Even if remote has reset the initial subflow by now 3260 * the refcnt is still at least one. 3261 */ 3262 subflow = mptcp_subflow_ctx(msk->first); 3263 list_add(&subflow->node, &msk->conn_list); 3264 sock_hold(msk->first); 3265 if (mptcp_is_fully_established(newsk)) 3266 mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL); 3267 3268 mptcp_copy_inaddrs(newsk, msk->first); 3269 mptcp_rcv_space_init(msk, msk->first); 3270 mptcp_propagate_sndbuf(newsk, msk->first); 3271 3272 /* set ssk->sk_socket of accept()ed flows to mptcp socket. 3273 * This is needed so NOSPACE flag can be set from tcp stack. 3274 */ 3275 mptcp_flush_join_list(msk); 3276 mptcp_for_each_subflow(msk, subflow) { 3277 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3278 3279 if (!ssk->sk_socket) 3280 mptcp_sock_graft(ssk, newsock); 3281 } 3282 release_sock(newsk); 3283 } 3284 3285 if (inet_csk_listen_poll(ssock->sk)) 3286 set_bit(MPTCP_DATA_READY, &msk->flags); 3287 sock_put(ssock->sk); 3288 return err; 3289 3290 unlock_fail: 3291 release_sock(sock->sk); 3292 return -EINVAL; 3293 } 3294 3295 static __poll_t mptcp_check_readable(struct mptcp_sock *msk) 3296 { 3297 return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM : 3298 0; 3299 } 3300 3301 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk) 3302 { 3303 struct sock *sk = (struct sock *)msk; 3304 3305 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 3306 return EPOLLOUT | EPOLLWRNORM; 3307 3308 if (sk_stream_is_writeable(sk)) 3309 return EPOLLOUT | EPOLLWRNORM; 3310 3311 mptcp_set_nospace(sk); 3312 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 3313 if (sk_stream_is_writeable(sk)) 3314 return EPOLLOUT | EPOLLWRNORM; 3315 3316 return 0; 3317 } 3318 3319 static __poll_t mptcp_poll(struct file *file, struct socket *sock, 3320 struct poll_table_struct *wait) 3321 { 3322 struct sock *sk = sock->sk; 3323 struct mptcp_sock *msk; 3324 __poll_t mask = 0; 3325 int state; 3326 3327 msk = mptcp_sk(sk); 3328 sock_poll_wait(file, sock, wait); 3329 3330 state = inet_sk_state_load(sk); 3331 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags); 3332 if (state == TCP_LISTEN) 3333 return mptcp_check_readable(msk); 3334 3335 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) { 3336 mask |= mptcp_check_readable(msk); 3337 mask |= mptcp_check_writeable(msk); 3338 } 3339 if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE) 3340 mask |= EPOLLHUP; 3341 if (sk->sk_shutdown & RCV_SHUTDOWN) 3342 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 3343 3344 /* This barrier is coupled with smp_wmb() in tcp_reset() */ 3345 smp_rmb(); 3346 if (sk->sk_err) 3347 mask |= EPOLLERR; 3348 3349 return mask; 3350 } 3351 3352 static const struct proto_ops mptcp_stream_ops = { 3353 .family = PF_INET, 3354 .owner = THIS_MODULE, 3355 .release = inet_release, 3356 .bind = mptcp_bind, 3357 .connect = mptcp_stream_connect, 3358 .socketpair = sock_no_socketpair, 3359 .accept = mptcp_stream_accept, 3360 .getname = inet_getname, 3361 .poll = mptcp_poll, 3362 .ioctl = inet_ioctl, 3363 .gettstamp = sock_gettstamp, 3364 .listen = mptcp_listen, 3365 .shutdown = inet_shutdown, 3366 .setsockopt = sock_common_setsockopt, 3367 .getsockopt = sock_common_getsockopt, 3368 .sendmsg = inet_sendmsg, 3369 .recvmsg = inet_recvmsg, 3370 .mmap = sock_no_mmap, 3371 .sendpage = inet_sendpage, 3372 }; 3373 3374 static struct inet_protosw mptcp_protosw = { 3375 .type = SOCK_STREAM, 3376 .protocol = IPPROTO_MPTCP, 3377 .prot = &mptcp_prot, 3378 .ops = &mptcp_stream_ops, 3379 .flags = INET_PROTOSW_ICSK, 3380 }; 3381 3382 static int mptcp_napi_poll(struct napi_struct *napi, int budget) 3383 { 3384 struct mptcp_delegated_action *delegated; 3385 struct mptcp_subflow_context *subflow; 3386 int work_done = 0; 3387 3388 delegated = container_of(napi, struct mptcp_delegated_action, napi); 3389 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) { 3390 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 3391 3392 bh_lock_sock_nested(ssk); 3393 if (!sock_owned_by_user(ssk) && 3394 mptcp_subflow_has_delegated_action(subflow)) 3395 mptcp_subflow_process_delegated(ssk); 3396 /* ... elsewhere tcp_release_cb_override already processed 3397 * the action or will do at next release_sock(). 3398 * In both case must dequeue the subflow here - on the same 3399 * CPU that scheduled it. 3400 */ 3401 bh_unlock_sock(ssk); 3402 sock_put(ssk); 3403 3404 if (++work_done == budget) 3405 return budget; 3406 } 3407 3408 /* always provide a 0 'work_done' argument, so that napi_complete_done 3409 * will not try accessing the NULL napi->dev ptr 3410 */ 3411 napi_complete_done(napi, 0); 3412 return work_done; 3413 } 3414 3415 void __init mptcp_proto_init(void) 3416 { 3417 struct mptcp_delegated_action *delegated; 3418 int cpu; 3419 3420 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 3421 3422 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 3423 panic("Failed to allocate MPTCP pcpu counter\n"); 3424 3425 init_dummy_netdev(&mptcp_napi_dev); 3426 for_each_possible_cpu(cpu) { 3427 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu); 3428 INIT_LIST_HEAD(&delegated->head); 3429 netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll, 3430 NAPI_POLL_WEIGHT); 3431 napi_enable(&delegated->napi); 3432 } 3433 3434 mptcp_subflow_init(); 3435 mptcp_pm_init(); 3436 mptcp_token_init(); 3437 3438 if (proto_register(&mptcp_prot, 1) != 0) 3439 panic("Failed to register MPTCP proto.\n"); 3440 3441 inet_register_protosw(&mptcp_protosw); 3442 3443 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 3444 } 3445 3446 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 3447 static const struct proto_ops mptcp_v6_stream_ops = { 3448 .family = PF_INET6, 3449 .owner = THIS_MODULE, 3450 .release = inet6_release, 3451 .bind = mptcp_bind, 3452 .connect = mptcp_stream_connect, 3453 .socketpair = sock_no_socketpair, 3454 .accept = mptcp_stream_accept, 3455 .getname = inet6_getname, 3456 .poll = mptcp_poll, 3457 .ioctl = inet6_ioctl, 3458 .gettstamp = sock_gettstamp, 3459 .listen = mptcp_listen, 3460 .shutdown = inet_shutdown, 3461 .setsockopt = sock_common_setsockopt, 3462 .getsockopt = sock_common_getsockopt, 3463 .sendmsg = inet6_sendmsg, 3464 .recvmsg = inet6_recvmsg, 3465 .mmap = sock_no_mmap, 3466 .sendpage = inet_sendpage, 3467 #ifdef CONFIG_COMPAT 3468 .compat_ioctl = inet6_compat_ioctl, 3469 #endif 3470 }; 3471 3472 static struct proto mptcp_v6_prot; 3473 3474 static void mptcp_v6_destroy(struct sock *sk) 3475 { 3476 mptcp_destroy(sk); 3477 inet6_destroy_sock(sk); 3478 } 3479 3480 static struct inet_protosw mptcp_v6_protosw = { 3481 .type = SOCK_STREAM, 3482 .protocol = IPPROTO_MPTCP, 3483 .prot = &mptcp_v6_prot, 3484 .ops = &mptcp_v6_stream_ops, 3485 .flags = INET_PROTOSW_ICSK, 3486 }; 3487 3488 int __init mptcp_proto_v6_init(void) 3489 { 3490 int err; 3491 3492 mptcp_v6_prot = mptcp_prot; 3493 strcpy(mptcp_v6_prot.name, "MPTCPv6"); 3494 mptcp_v6_prot.slab = NULL; 3495 mptcp_v6_prot.destroy = mptcp_v6_destroy; 3496 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 3497 3498 err = proto_register(&mptcp_v6_prot, 1); 3499 if (err) 3500 return err; 3501 3502 err = inet6_register_protosw(&mptcp_v6_protosw); 3503 if (err) 3504 proto_unregister(&mptcp_v6_prot); 3505 3506 return err; 3507 } 3508 #endif 3509