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