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 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 20 #include <net/transp_v6.h> 21 #endif 22 #include <net/mptcp.h> 23 #include "protocol.h" 24 #include "mib.h" 25 26 #define MPTCP_SAME_STATE TCP_MAX_STATES 27 28 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 29 struct mptcp6_sock { 30 struct mptcp_sock msk; 31 struct ipv6_pinfo np; 32 }; 33 #endif 34 35 struct mptcp_skb_cb { 36 u32 offset; 37 }; 38 39 #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 40 41 static struct percpu_counter mptcp_sockets_allocated; 42 43 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 44 * completed yet or has failed, return the subflow socket. 45 * Otherwise return NULL. 46 */ 47 static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 48 { 49 if (!msk->subflow || READ_ONCE(msk->can_ack)) 50 return NULL; 51 52 return msk->subflow; 53 } 54 55 static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk) 56 { 57 return msk->first && !sk_is_mptcp(msk->first); 58 } 59 60 static struct socket *mptcp_is_tcpsk(struct sock *sk) 61 { 62 struct socket *sock = sk->sk_socket; 63 64 if (sock->sk != sk) 65 return NULL; 66 67 if (unlikely(sk->sk_prot == &tcp_prot)) { 68 /* we are being invoked after mptcp_accept() has 69 * accepted a non-mp-capable flow: sk is a tcp_sk, 70 * not an mptcp one. 71 * 72 * Hand the socket over to tcp so all further socket ops 73 * bypass mptcp. 74 */ 75 sock->ops = &inet_stream_ops; 76 return sock; 77 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 78 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 79 sock->ops = &inet6_stream_ops; 80 return sock; 81 #endif 82 } 83 84 return NULL; 85 } 86 87 static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk) 88 { 89 struct socket *sock; 90 91 sock_owned_by_me((const struct sock *)msk); 92 93 sock = mptcp_is_tcpsk((struct sock *)msk); 94 if (unlikely(sock)) 95 return sock; 96 97 if (likely(!__mptcp_needs_tcp_fallback(msk))) 98 return NULL; 99 100 return msk->subflow; 101 } 102 103 static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk) 104 { 105 return !msk->first; 106 } 107 108 static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state) 109 { 110 struct mptcp_subflow_context *subflow; 111 struct sock *sk = (struct sock *)msk; 112 struct socket *ssock; 113 int err; 114 115 ssock = __mptcp_tcp_fallback(msk); 116 if (unlikely(ssock)) 117 return ssock; 118 119 ssock = __mptcp_nmpc_socket(msk); 120 if (ssock) 121 goto set_state; 122 123 if (!__mptcp_can_create_subflow(msk)) 124 return ERR_PTR(-EINVAL); 125 126 err = mptcp_subflow_create_socket(sk, &ssock); 127 if (err) 128 return ERR_PTR(err); 129 130 msk->first = ssock->sk; 131 msk->subflow = ssock; 132 subflow = mptcp_subflow_ctx(ssock->sk); 133 list_add(&subflow->node, &msk->conn_list); 134 subflow->request_mptcp = 1; 135 136 set_state: 137 if (state != MPTCP_SAME_STATE) 138 inet_sk_state_store(sk, state); 139 return ssock; 140 } 141 142 static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 143 struct sk_buff *skb, 144 unsigned int offset, size_t copy_len) 145 { 146 struct sock *sk = (struct sock *)msk; 147 struct sk_buff *tail; 148 149 __skb_unlink(skb, &ssk->sk_receive_queue); 150 151 skb_ext_reset(skb); 152 skb_orphan(skb); 153 msk->ack_seq += copy_len; 154 155 tail = skb_peek_tail(&sk->sk_receive_queue); 156 if (offset == 0 && tail) { 157 bool fragstolen; 158 int delta; 159 160 if (skb_try_coalesce(tail, skb, &fragstolen, &delta)) { 161 kfree_skb_partial(skb, fragstolen); 162 atomic_add(delta, &sk->sk_rmem_alloc); 163 sk_mem_charge(sk, delta); 164 return; 165 } 166 } 167 168 skb_set_owner_r(skb, sk); 169 __skb_queue_tail(&sk->sk_receive_queue, skb); 170 MPTCP_SKB_CB(skb)->offset = offset; 171 } 172 173 /* both sockets must be locked */ 174 static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, 175 struct sock *ssk) 176 { 177 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 178 u64 dsn = mptcp_subflow_get_mapped_dsn(subflow); 179 180 /* revalidate data sequence number. 181 * 182 * mptcp_subflow_data_available() is usually called 183 * without msk lock. Its unlikely (but possible) 184 * that msk->ack_seq has been advanced since the last 185 * call found in-sequence data. 186 */ 187 if (likely(dsn == msk->ack_seq)) 188 return true; 189 190 subflow->data_avail = 0; 191 return mptcp_subflow_data_available(ssk); 192 } 193 194 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 195 struct sock *ssk, 196 unsigned int *bytes) 197 { 198 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 199 struct sock *sk = (struct sock *)msk; 200 unsigned int moved = 0; 201 bool more_data_avail; 202 struct tcp_sock *tp; 203 bool done = false; 204 205 if (!mptcp_subflow_dsn_valid(msk, ssk)) { 206 *bytes = 0; 207 return false; 208 } 209 210 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 211 int rcvbuf = max(ssk->sk_rcvbuf, sk->sk_rcvbuf); 212 213 if (rcvbuf > sk->sk_rcvbuf) 214 sk->sk_rcvbuf = rcvbuf; 215 } 216 217 tp = tcp_sk(ssk); 218 do { 219 u32 map_remaining, offset; 220 u32 seq = tp->copied_seq; 221 struct sk_buff *skb; 222 bool fin; 223 224 /* try to move as much data as available */ 225 map_remaining = subflow->map_data_len - 226 mptcp_subflow_get_map_offset(subflow); 227 228 skb = skb_peek(&ssk->sk_receive_queue); 229 if (!skb) 230 break; 231 232 offset = seq - TCP_SKB_CB(skb)->seq; 233 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 234 if (fin) { 235 done = true; 236 seq++; 237 } 238 239 if (offset < skb->len) { 240 size_t len = skb->len - offset; 241 242 if (tp->urg_data) 243 done = true; 244 245 __mptcp_move_skb(msk, ssk, skb, offset, len); 246 seq += len; 247 moved += len; 248 249 if (WARN_ON_ONCE(map_remaining < len)) 250 break; 251 } else { 252 WARN_ON_ONCE(!fin); 253 sk_eat_skb(ssk, skb); 254 done = true; 255 } 256 257 WRITE_ONCE(tp->copied_seq, seq); 258 more_data_avail = mptcp_subflow_data_available(ssk); 259 260 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) { 261 done = true; 262 break; 263 } 264 } while (more_data_avail); 265 266 *bytes = moved; 267 268 return done; 269 } 270 271 /* In most cases we will be able to lock the mptcp socket. If its already 272 * owned, we need to defer to the work queue to avoid ABBA deadlock. 273 */ 274 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 275 { 276 struct sock *sk = (struct sock *)msk; 277 unsigned int moved = 0; 278 279 if (READ_ONCE(sk->sk_lock.owned)) 280 return false; 281 282 if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock))) 283 return false; 284 285 /* must re-check after taking the lock */ 286 if (!READ_ONCE(sk->sk_lock.owned)) 287 __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 288 289 spin_unlock_bh(&sk->sk_lock.slock); 290 291 return moved > 0; 292 } 293 294 void mptcp_data_ready(struct sock *sk, struct sock *ssk) 295 { 296 struct mptcp_sock *msk = mptcp_sk(sk); 297 298 set_bit(MPTCP_DATA_READY, &msk->flags); 299 300 if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) && 301 move_skbs_to_msk(msk, ssk)) 302 goto wake; 303 304 /* don't schedule if mptcp sk is (still) over limit */ 305 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) 306 goto wake; 307 308 /* mptcp socket is owned, release_cb should retry */ 309 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, 310 &sk->sk_tsq_flags)) { 311 sock_hold(sk); 312 313 /* need to try again, its possible release_cb() has already 314 * been called after the test_and_set_bit() above. 315 */ 316 move_skbs_to_msk(msk, ssk); 317 } 318 wake: 319 sk->sk_data_ready(sk); 320 } 321 322 static void __mptcp_flush_join_list(struct mptcp_sock *msk) 323 { 324 if (likely(list_empty(&msk->join_list))) 325 return; 326 327 spin_lock_bh(&msk->join_list_lock); 328 list_splice_tail_init(&msk->join_list, &msk->conn_list); 329 spin_unlock_bh(&msk->join_list_lock); 330 } 331 332 static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk) 333 { 334 long tout = ssk && inet_csk(ssk)->icsk_pending ? 335 inet_csk(ssk)->icsk_timeout - jiffies : 0; 336 337 if (tout <= 0) 338 tout = mptcp_sk(sk)->timer_ival; 339 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 340 } 341 342 static bool mptcp_timer_pending(struct sock *sk) 343 { 344 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 345 } 346 347 static void mptcp_reset_timer(struct sock *sk) 348 { 349 struct inet_connection_sock *icsk = inet_csk(sk); 350 unsigned long tout; 351 352 /* should never be called with mptcp level timer cleared */ 353 tout = READ_ONCE(mptcp_sk(sk)->timer_ival); 354 if (WARN_ON_ONCE(!tout)) 355 tout = TCP_RTO_MIN; 356 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 357 } 358 359 void mptcp_data_acked(struct sock *sk) 360 { 361 mptcp_reset_timer(sk); 362 363 if (!sk_stream_is_writeable(sk) && 364 schedule_work(&mptcp_sk(sk)->work)) 365 sock_hold(sk); 366 } 367 368 void mptcp_subflow_eof(struct sock *sk) 369 { 370 struct mptcp_sock *msk = mptcp_sk(sk); 371 372 if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) && 373 schedule_work(&msk->work)) 374 sock_hold(sk); 375 } 376 377 static void mptcp_stop_timer(struct sock *sk) 378 { 379 struct inet_connection_sock *icsk = inet_csk(sk); 380 381 sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 382 mptcp_sk(sk)->timer_ival = 0; 383 } 384 385 static bool mptcp_ext_cache_refill(struct mptcp_sock *msk) 386 { 387 const struct sock *sk = (const struct sock *)msk; 388 389 if (!msk->cached_ext) 390 msk->cached_ext = __skb_ext_alloc(sk->sk_allocation); 391 392 return !!msk->cached_ext; 393 } 394 395 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 396 { 397 struct mptcp_subflow_context *subflow; 398 struct sock *sk = (struct sock *)msk; 399 400 sock_owned_by_me(sk); 401 402 mptcp_for_each_subflow(msk, subflow) { 403 if (subflow->data_avail) 404 return mptcp_subflow_tcp_sock(subflow); 405 } 406 407 return NULL; 408 } 409 410 static bool mptcp_skb_can_collapse_to(u64 write_seq, 411 const struct sk_buff *skb, 412 const struct mptcp_ext *mpext) 413 { 414 if (!tcp_skb_can_collapse_to(skb)) 415 return false; 416 417 /* can collapse only if MPTCP level sequence is in order */ 418 return mpext && mpext->data_seq + mpext->data_len == write_seq; 419 } 420 421 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 422 const struct page_frag *pfrag, 423 const struct mptcp_data_frag *df) 424 { 425 return df && pfrag->page == df->page && 426 df->data_seq + df->data_len == msk->write_seq; 427 } 428 429 static void dfrag_uncharge(struct sock *sk, int len) 430 { 431 sk_mem_uncharge(sk, len); 432 sk_wmem_queued_add(sk, -len); 433 } 434 435 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 436 { 437 int len = dfrag->data_len + dfrag->overhead; 438 439 list_del(&dfrag->list); 440 dfrag_uncharge(sk, len); 441 put_page(dfrag->page); 442 } 443 444 static void mptcp_clean_una(struct sock *sk) 445 { 446 struct mptcp_sock *msk = mptcp_sk(sk); 447 struct mptcp_data_frag *dtmp, *dfrag; 448 u64 snd_una = atomic64_read(&msk->snd_una); 449 bool cleaned = false; 450 451 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 452 if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 453 break; 454 455 dfrag_clear(sk, dfrag); 456 cleaned = true; 457 } 458 459 dfrag = mptcp_rtx_head(sk); 460 if (dfrag && after64(snd_una, dfrag->data_seq)) { 461 u64 delta = dfrag->data_seq + dfrag->data_len - snd_una; 462 463 dfrag->data_seq += delta; 464 dfrag->data_len -= delta; 465 466 dfrag_uncharge(sk, delta); 467 cleaned = true; 468 } 469 470 if (cleaned) { 471 sk_mem_reclaim_partial(sk); 472 473 /* Only wake up writers if a subflow is ready */ 474 if (test_bit(MPTCP_SEND_SPACE, &msk->flags)) 475 sk_stream_write_space(sk); 476 } 477 } 478 479 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 480 * data 481 */ 482 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 483 { 484 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 485 pfrag, sk->sk_allocation))) 486 return true; 487 488 sk->sk_prot->enter_memory_pressure(sk); 489 sk_stream_moderate_sndbuf(sk); 490 return false; 491 } 492 493 static struct mptcp_data_frag * 494 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 495 int orig_offset) 496 { 497 int offset = ALIGN(orig_offset, sizeof(long)); 498 struct mptcp_data_frag *dfrag; 499 500 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 501 dfrag->data_len = 0; 502 dfrag->data_seq = msk->write_seq; 503 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 504 dfrag->offset = offset + sizeof(struct mptcp_data_frag); 505 dfrag->page = pfrag->page; 506 507 return dfrag; 508 } 509 510 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 511 struct msghdr *msg, struct mptcp_data_frag *dfrag, 512 long *timeo, int *pmss_now, 513 int *ps_goal) 514 { 515 int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0; 516 bool dfrag_collapsed, can_collapse = false; 517 struct mptcp_sock *msk = mptcp_sk(sk); 518 struct mptcp_ext *mpext = NULL; 519 bool retransmission = !!dfrag; 520 struct sk_buff *skb, *tail; 521 struct page_frag *pfrag; 522 struct page *page; 523 u64 *write_seq; 524 size_t psize; 525 526 /* use the mptcp page cache so that we can easily move the data 527 * from one substream to another, but do per subflow memory accounting 528 * Note: pfrag is used only !retransmission, but the compiler if 529 * fooled into a warning if we don't init here 530 */ 531 pfrag = sk_page_frag(sk); 532 if (!retransmission) { 533 write_seq = &msk->write_seq; 534 page = pfrag->page; 535 } else { 536 write_seq = &dfrag->data_seq; 537 page = dfrag->page; 538 } 539 540 /* compute copy limit */ 541 mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags); 542 *pmss_now = mss_now; 543 *ps_goal = size_goal; 544 avail_size = size_goal; 545 skb = tcp_write_queue_tail(ssk); 546 if (skb) { 547 mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 548 549 /* Limit the write to the size available in the 550 * current skb, if any, so that we create at most a new skb. 551 * Explicitly tells TCP internals to avoid collapsing on later 552 * queue management operation, to avoid breaking the ext <-> 553 * SSN association set here 554 */ 555 can_collapse = (size_goal - skb->len > 0) && 556 mptcp_skb_can_collapse_to(*write_seq, skb, mpext); 557 if (!can_collapse) 558 TCP_SKB_CB(skb)->eor = 1; 559 else 560 avail_size = size_goal - skb->len; 561 } 562 563 if (!retransmission) { 564 /* reuse tail pfrag, if possible, or carve a new one from the 565 * page allocator 566 */ 567 dfrag = mptcp_rtx_tail(sk); 568 offset = pfrag->offset; 569 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 570 if (!dfrag_collapsed) { 571 dfrag = mptcp_carve_data_frag(msk, pfrag, offset); 572 offset = dfrag->offset; 573 frag_truesize = dfrag->overhead; 574 } 575 psize = min_t(size_t, pfrag->size - offset, avail_size); 576 577 /* Copy to page */ 578 pr_debug("left=%zu", msg_data_left(msg)); 579 psize = copy_page_from_iter(pfrag->page, offset, 580 min_t(size_t, msg_data_left(msg), 581 psize), 582 &msg->msg_iter); 583 pr_debug("left=%zu", msg_data_left(msg)); 584 if (!psize) 585 return -EINVAL; 586 587 if (!sk_wmem_schedule(sk, psize + dfrag->overhead)) 588 return -ENOMEM; 589 } else { 590 offset = dfrag->offset; 591 psize = min_t(size_t, dfrag->data_len, avail_size); 592 } 593 594 /* tell the TCP stack to delay the push so that we can safely 595 * access the skb after the sendpages call 596 */ 597 ret = do_tcp_sendpages(ssk, page, offset, psize, 598 msg->msg_flags | MSG_SENDPAGE_NOTLAST | MSG_DONTWAIT); 599 if (ret <= 0) 600 return ret; 601 602 frag_truesize += ret; 603 if (!retransmission) { 604 if (unlikely(ret < psize)) 605 iov_iter_revert(&msg->msg_iter, psize - ret); 606 607 /* send successful, keep track of sent data for mptcp-level 608 * retransmission 609 */ 610 dfrag->data_len += ret; 611 if (!dfrag_collapsed) { 612 get_page(dfrag->page); 613 list_add_tail(&dfrag->list, &msk->rtx_queue); 614 sk_wmem_queued_add(sk, frag_truesize); 615 } else { 616 sk_wmem_queued_add(sk, ret); 617 } 618 619 /* charge data on mptcp rtx queue to the master socket 620 * Note: we charge such data both to sk and ssk 621 */ 622 sk->sk_forward_alloc -= frag_truesize; 623 } 624 625 /* if the tail skb extension is still the cached one, collapsing 626 * really happened. Note: we can't check for 'same skb' as the sk_buff 627 * hdr on tail can be transmitted, freed and re-allocated by the 628 * do_tcp_sendpages() call 629 */ 630 tail = tcp_write_queue_tail(ssk); 631 if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) { 632 WARN_ON_ONCE(!can_collapse); 633 mpext->data_len += ret; 634 goto out; 635 } 636 637 skb = tcp_write_queue_tail(ssk); 638 mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext); 639 msk->cached_ext = NULL; 640 641 memset(mpext, 0, sizeof(*mpext)); 642 mpext->data_seq = *write_seq; 643 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 644 mpext->data_len = ret; 645 mpext->use_map = 1; 646 mpext->dsn64 = 1; 647 648 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 649 mpext->data_seq, mpext->subflow_seq, mpext->data_len, 650 mpext->dsn64); 651 652 out: 653 if (!retransmission) 654 pfrag->offset += frag_truesize; 655 *write_seq += ret; 656 mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 657 658 return ret; 659 } 660 661 static void mptcp_nospace(struct mptcp_sock *msk, struct socket *sock) 662 { 663 clear_bit(MPTCP_SEND_SPACE, &msk->flags); 664 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */ 665 666 /* enables sk->write_space() callbacks */ 667 set_bit(SOCK_NOSPACE, &sock->flags); 668 } 669 670 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 671 { 672 struct mptcp_subflow_context *subflow; 673 struct sock *backup = NULL; 674 675 sock_owned_by_me((const struct sock *)msk); 676 677 if (!mptcp_ext_cache_refill(msk)) 678 return NULL; 679 680 mptcp_for_each_subflow(msk, subflow) { 681 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 682 683 if (!sk_stream_memory_free(ssk)) { 684 struct socket *sock = ssk->sk_socket; 685 686 if (sock) 687 mptcp_nospace(msk, sock); 688 689 return NULL; 690 } 691 692 if (subflow->backup) { 693 if (!backup) 694 backup = ssk; 695 696 continue; 697 } 698 699 return ssk; 700 } 701 702 return backup; 703 } 704 705 static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk) 706 { 707 struct socket *sock; 708 709 if (likely(sk_stream_is_writeable(ssk))) 710 return; 711 712 sock = READ_ONCE(ssk->sk_socket); 713 if (sock) 714 mptcp_nospace(msk, sock); 715 } 716 717 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 718 { 719 int mss_now = 0, size_goal = 0, ret = 0; 720 struct mptcp_sock *msk = mptcp_sk(sk); 721 struct page_frag *pfrag; 722 struct socket *ssock; 723 size_t copied = 0; 724 struct sock *ssk; 725 bool tx_ok; 726 long timeo; 727 728 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 729 return -EOPNOTSUPP; 730 731 lock_sock(sk); 732 733 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 734 735 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 736 ret = sk_stream_wait_connect(sk, &timeo); 737 if (ret) 738 goto out; 739 } 740 741 fallback: 742 ssock = __mptcp_tcp_fallback(msk); 743 if (unlikely(ssock)) { 744 release_sock(sk); 745 pr_debug("fallback passthrough"); 746 ret = sock_sendmsg(ssock, msg); 747 return ret >= 0 ? ret + copied : (copied ? copied : ret); 748 } 749 750 pfrag = sk_page_frag(sk); 751 restart: 752 mptcp_clean_una(sk); 753 754 wait_for_sndbuf: 755 __mptcp_flush_join_list(msk); 756 ssk = mptcp_subflow_get_send(msk); 757 while (!sk_stream_memory_free(sk) || 758 !ssk || 759 !mptcp_page_frag_refill(ssk, pfrag)) { 760 if (ssk) { 761 /* make sure retransmit timer is 762 * running before we wait for memory. 763 * 764 * The retransmit timer might be needed 765 * to make the peer send an up-to-date 766 * MPTCP Ack. 767 */ 768 mptcp_set_timeout(sk, ssk); 769 if (!mptcp_timer_pending(sk)) 770 mptcp_reset_timer(sk); 771 } 772 773 ret = sk_stream_wait_memory(sk, &timeo); 774 if (ret) 775 goto out; 776 777 mptcp_clean_una(sk); 778 779 ssk = mptcp_subflow_get_send(msk); 780 if (list_empty(&msk->conn_list)) { 781 ret = -ENOTCONN; 782 goto out; 783 } 784 } 785 786 pr_debug("conn_list->subflow=%p", ssk); 787 788 lock_sock(ssk); 789 tx_ok = msg_data_left(msg); 790 while (tx_ok) { 791 ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now, 792 &size_goal); 793 if (ret < 0) { 794 if (ret == -EAGAIN && timeo > 0) { 795 mptcp_set_timeout(sk, ssk); 796 release_sock(ssk); 797 goto restart; 798 } 799 break; 800 } 801 if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) { 802 /* Can happen for passive sockets: 803 * 3WHS negotiated MPTCP, but first packet after is 804 * plain TCP (e.g. due to middlebox filtering unknown 805 * options). 806 * 807 * Fall back to TCP. 808 */ 809 release_sock(ssk); 810 goto fallback; 811 } 812 813 copied += ret; 814 815 tx_ok = msg_data_left(msg); 816 if (!tx_ok) 817 break; 818 819 if (!sk_stream_memory_free(ssk) || 820 !mptcp_page_frag_refill(ssk, pfrag) || 821 !mptcp_ext_cache_refill(msk)) { 822 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 823 tcp_push(ssk, msg->msg_flags, mss_now, 824 tcp_sk(ssk)->nonagle, size_goal); 825 mptcp_set_timeout(sk, ssk); 826 release_sock(ssk); 827 goto restart; 828 } 829 830 /* memory is charged to mptcp level socket as well, i.e. 831 * if msg is very large, mptcp socket may run out of buffer 832 * space. mptcp_clean_una() will release data that has 833 * been acked at mptcp level in the mean time, so there is 834 * a good chance we can continue sending data right away. 835 * 836 * Normally, when the tcp subflow can accept more data, then 837 * so can the MPTCP socket. However, we need to cope with 838 * peers that might lag behind in their MPTCP-level 839 * acknowledgements, i.e. data might have been acked at 840 * tcp level only. So, we must also check the MPTCP socket 841 * limits before we send more data. 842 */ 843 if (unlikely(!sk_stream_memory_free(sk))) { 844 tcp_push(ssk, msg->msg_flags, mss_now, 845 tcp_sk(ssk)->nonagle, size_goal); 846 mptcp_clean_una(sk); 847 if (!sk_stream_memory_free(sk)) { 848 /* can't send more for now, need to wait for 849 * MPTCP-level ACKs from peer. 850 * 851 * Wakeup will happen via mptcp_clean_una(). 852 */ 853 mptcp_set_timeout(sk, ssk); 854 release_sock(ssk); 855 goto wait_for_sndbuf; 856 } 857 } 858 } 859 860 mptcp_set_timeout(sk, ssk); 861 if (copied) { 862 ret = copied; 863 tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle, 864 size_goal); 865 866 /* start the timer, if it's not pending */ 867 if (!mptcp_timer_pending(sk)) 868 mptcp_reset_timer(sk); 869 } 870 871 ssk_check_wmem(msk, ssk); 872 release_sock(ssk); 873 out: 874 release_sock(sk); 875 return ret; 876 } 877 878 static void mptcp_wait_data(struct sock *sk, long *timeo) 879 { 880 DEFINE_WAIT_FUNC(wait, woken_wake_function); 881 struct mptcp_sock *msk = mptcp_sk(sk); 882 883 add_wait_queue(sk_sleep(sk), &wait); 884 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 885 886 sk_wait_event(sk, timeo, 887 test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); 888 889 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 890 remove_wait_queue(sk_sleep(sk), &wait); 891 } 892 893 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 894 struct msghdr *msg, 895 size_t len) 896 { 897 struct sock *sk = (struct sock *)msk; 898 struct sk_buff *skb; 899 int copied = 0; 900 901 while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) { 902 u32 offset = MPTCP_SKB_CB(skb)->offset; 903 u32 data_len = skb->len - offset; 904 u32 count = min_t(size_t, len - copied, data_len); 905 int err; 906 907 err = skb_copy_datagram_msg(skb, offset, msg, count); 908 if (unlikely(err < 0)) { 909 if (!copied) 910 return err; 911 break; 912 } 913 914 copied += count; 915 916 if (count < data_len) { 917 MPTCP_SKB_CB(skb)->offset += count; 918 break; 919 } 920 921 __skb_unlink(skb, &sk->sk_receive_queue); 922 __kfree_skb(skb); 923 924 if (copied >= len) 925 break; 926 } 927 928 return copied; 929 } 930 931 static bool __mptcp_move_skbs(struct mptcp_sock *msk) 932 { 933 unsigned int moved = 0; 934 bool done; 935 936 do { 937 struct sock *ssk = mptcp_subflow_recv_lookup(msk); 938 939 if (!ssk) 940 break; 941 942 lock_sock(ssk); 943 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 944 release_sock(ssk); 945 } while (!done); 946 947 return moved > 0; 948 } 949 950 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 951 int nonblock, int flags, int *addr_len) 952 { 953 struct mptcp_sock *msk = mptcp_sk(sk); 954 struct socket *ssock; 955 int copied = 0; 956 int target; 957 long timeo; 958 959 if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) 960 return -EOPNOTSUPP; 961 962 lock_sock(sk); 963 ssock = __mptcp_tcp_fallback(msk); 964 if (unlikely(ssock)) { 965 fallback: 966 release_sock(sk); 967 pr_debug("fallback-read subflow=%p", 968 mptcp_subflow_ctx(ssock->sk)); 969 copied = sock_recvmsg(ssock, msg, flags); 970 return copied; 971 } 972 973 timeo = sock_rcvtimeo(sk, nonblock); 974 975 len = min_t(size_t, len, INT_MAX); 976 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 977 __mptcp_flush_join_list(msk); 978 979 while (len > (size_t)copied) { 980 int bytes_read; 981 982 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied); 983 if (unlikely(bytes_read < 0)) { 984 if (!copied) 985 copied = bytes_read; 986 goto out_err; 987 } 988 989 copied += bytes_read; 990 991 if (skb_queue_empty(&sk->sk_receive_queue) && 992 __mptcp_move_skbs(msk)) 993 continue; 994 995 /* only the master socket status is relevant here. The exit 996 * conditions mirror closely tcp_recvmsg() 997 */ 998 if (copied >= target) 999 break; 1000 1001 if (copied) { 1002 if (sk->sk_err || 1003 sk->sk_state == TCP_CLOSE || 1004 (sk->sk_shutdown & RCV_SHUTDOWN) || 1005 !timeo || 1006 signal_pending(current)) 1007 break; 1008 } else { 1009 if (sk->sk_err) { 1010 copied = sock_error(sk); 1011 break; 1012 } 1013 1014 if (sk->sk_shutdown & RCV_SHUTDOWN) 1015 break; 1016 1017 if (sk->sk_state == TCP_CLOSE) { 1018 copied = -ENOTCONN; 1019 break; 1020 } 1021 1022 if (!timeo) { 1023 copied = -EAGAIN; 1024 break; 1025 } 1026 1027 if (signal_pending(current)) { 1028 copied = sock_intr_errno(timeo); 1029 break; 1030 } 1031 } 1032 1033 pr_debug("block timeout %ld", timeo); 1034 mptcp_wait_data(sk, &timeo); 1035 ssock = __mptcp_tcp_fallback(msk); 1036 if (unlikely(ssock)) 1037 goto fallback; 1038 } 1039 1040 if (skb_queue_empty(&sk->sk_receive_queue)) { 1041 /* entire backlog drained, clear DATA_READY. */ 1042 clear_bit(MPTCP_DATA_READY, &msk->flags); 1043 1044 /* .. race-breaker: ssk might have gotten new data 1045 * after last __mptcp_move_skbs() returned false. 1046 */ 1047 if (unlikely(__mptcp_move_skbs(msk))) 1048 set_bit(MPTCP_DATA_READY, &msk->flags); 1049 } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) { 1050 /* data to read but mptcp_wait_data() cleared DATA_READY */ 1051 set_bit(MPTCP_DATA_READY, &msk->flags); 1052 } 1053 out_err: 1054 release_sock(sk); 1055 return copied; 1056 } 1057 1058 static void mptcp_retransmit_handler(struct sock *sk) 1059 { 1060 struct mptcp_sock *msk = mptcp_sk(sk); 1061 1062 if (atomic64_read(&msk->snd_una) == msk->write_seq) { 1063 mptcp_stop_timer(sk); 1064 } else { 1065 set_bit(MPTCP_WORK_RTX, &msk->flags); 1066 if (schedule_work(&msk->work)) 1067 sock_hold(sk); 1068 } 1069 } 1070 1071 static void mptcp_retransmit_timer(struct timer_list *t) 1072 { 1073 struct inet_connection_sock *icsk = from_timer(icsk, t, 1074 icsk_retransmit_timer); 1075 struct sock *sk = &icsk->icsk_inet.sk; 1076 1077 bh_lock_sock(sk); 1078 if (!sock_owned_by_user(sk)) { 1079 mptcp_retransmit_handler(sk); 1080 } else { 1081 /* delegate our work to tcp_release_cb() */ 1082 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, 1083 &sk->sk_tsq_flags)) 1084 sock_hold(sk); 1085 } 1086 bh_unlock_sock(sk); 1087 sock_put(sk); 1088 } 1089 1090 /* Find an idle subflow. Return NULL if there is unacked data at tcp 1091 * level. 1092 * 1093 * A backup subflow is returned only if that is the only kind available. 1094 */ 1095 static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk) 1096 { 1097 struct mptcp_subflow_context *subflow; 1098 struct sock *backup = NULL; 1099 1100 sock_owned_by_me((const struct sock *)msk); 1101 1102 mptcp_for_each_subflow(msk, subflow) { 1103 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1104 1105 /* still data outstanding at TCP level? Don't retransmit. */ 1106 if (!tcp_write_queue_empty(ssk)) 1107 return NULL; 1108 1109 if (subflow->backup) { 1110 if (!backup) 1111 backup = ssk; 1112 continue; 1113 } 1114 1115 return ssk; 1116 } 1117 1118 return backup; 1119 } 1120 1121 /* subflow sockets can be either outgoing (connect) or incoming 1122 * (accept). 1123 * 1124 * Outgoing subflows use in-kernel sockets. 1125 * Incoming subflows do not have their own 'struct socket' allocated, 1126 * so we need to use tcp_close() after detaching them from the mptcp 1127 * parent socket. 1128 */ 1129 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 1130 struct mptcp_subflow_context *subflow, 1131 long timeout) 1132 { 1133 struct socket *sock = READ_ONCE(ssk->sk_socket); 1134 1135 list_del(&subflow->node); 1136 1137 if (sock && sock != sk->sk_socket) { 1138 /* outgoing subflow */ 1139 sock_release(sock); 1140 } else { 1141 /* incoming subflow */ 1142 tcp_close(ssk, timeout); 1143 } 1144 } 1145 1146 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 1147 { 1148 return 0; 1149 } 1150 1151 static void mptcp_check_for_eof(struct mptcp_sock *msk) 1152 { 1153 struct mptcp_subflow_context *subflow; 1154 struct sock *sk = (struct sock *)msk; 1155 int receivers = 0; 1156 1157 mptcp_for_each_subflow(msk, subflow) 1158 receivers += !subflow->rx_eof; 1159 1160 if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) { 1161 /* hopefully temporary hack: propagate shutdown status 1162 * to msk, when all subflows agree on it 1163 */ 1164 sk->sk_shutdown |= RCV_SHUTDOWN; 1165 1166 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 1167 set_bit(MPTCP_DATA_READY, &msk->flags); 1168 sk->sk_data_ready(sk); 1169 } 1170 } 1171 1172 static void mptcp_worker(struct work_struct *work) 1173 { 1174 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 1175 struct sock *ssk, *sk = &msk->sk.icsk_inet.sk; 1176 int orig_len, orig_offset, mss_now = 0, size_goal = 0; 1177 struct mptcp_data_frag *dfrag; 1178 u64 orig_write_seq; 1179 size_t copied = 0; 1180 struct msghdr msg; 1181 long timeo = 0; 1182 1183 lock_sock(sk); 1184 mptcp_clean_una(sk); 1185 __mptcp_flush_join_list(msk); 1186 __mptcp_move_skbs(msk); 1187 1188 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 1189 mptcp_check_for_eof(msk); 1190 1191 if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 1192 goto unlock; 1193 1194 dfrag = mptcp_rtx_head(sk); 1195 if (!dfrag) 1196 goto unlock; 1197 1198 if (!mptcp_ext_cache_refill(msk)) 1199 goto reset_unlock; 1200 1201 ssk = mptcp_subflow_get_retrans(msk); 1202 if (!ssk) 1203 goto reset_unlock; 1204 1205 lock_sock(ssk); 1206 1207 msg.msg_flags = MSG_DONTWAIT; 1208 orig_len = dfrag->data_len; 1209 orig_offset = dfrag->offset; 1210 orig_write_seq = dfrag->data_seq; 1211 while (dfrag->data_len > 0) { 1212 int ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo, 1213 &mss_now, &size_goal); 1214 if (ret < 0) 1215 break; 1216 1217 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 1218 copied += ret; 1219 dfrag->data_len -= ret; 1220 dfrag->offset += ret; 1221 1222 if (!mptcp_ext_cache_refill(msk)) 1223 break; 1224 } 1225 if (copied) 1226 tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle, 1227 size_goal); 1228 1229 dfrag->data_seq = orig_write_seq; 1230 dfrag->offset = orig_offset; 1231 dfrag->data_len = orig_len; 1232 1233 mptcp_set_timeout(sk, ssk); 1234 release_sock(ssk); 1235 1236 reset_unlock: 1237 if (!mptcp_timer_pending(sk)) 1238 mptcp_reset_timer(sk); 1239 1240 unlock: 1241 release_sock(sk); 1242 sock_put(sk); 1243 } 1244 1245 static int __mptcp_init_sock(struct sock *sk) 1246 { 1247 struct mptcp_sock *msk = mptcp_sk(sk); 1248 1249 spin_lock_init(&msk->join_list_lock); 1250 1251 INIT_LIST_HEAD(&msk->conn_list); 1252 INIT_LIST_HEAD(&msk->join_list); 1253 INIT_LIST_HEAD(&msk->rtx_queue); 1254 __set_bit(MPTCP_SEND_SPACE, &msk->flags); 1255 INIT_WORK(&msk->work, mptcp_worker); 1256 1257 msk->first = NULL; 1258 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 1259 1260 mptcp_pm_data_init(msk); 1261 1262 /* re-use the csk retrans timer for MPTCP-level retrans */ 1263 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 1264 1265 return 0; 1266 } 1267 1268 static int mptcp_init_sock(struct sock *sk) 1269 { 1270 struct net *net = sock_net(sk); 1271 int ret; 1272 1273 if (!mptcp_is_enabled(net)) 1274 return -ENOPROTOOPT; 1275 1276 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 1277 return -ENOMEM; 1278 1279 ret = __mptcp_init_sock(sk); 1280 if (ret) 1281 return ret; 1282 1283 sk_sockets_allocated_inc(sk); 1284 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[2]; 1285 1286 return 0; 1287 } 1288 1289 static void __mptcp_clear_xmit(struct sock *sk) 1290 { 1291 struct mptcp_sock *msk = mptcp_sk(sk); 1292 struct mptcp_data_frag *dtmp, *dfrag; 1293 1294 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 1295 1296 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 1297 dfrag_clear(sk, dfrag); 1298 } 1299 1300 static void mptcp_cancel_work(struct sock *sk) 1301 { 1302 struct mptcp_sock *msk = mptcp_sk(sk); 1303 1304 if (cancel_work_sync(&msk->work)) 1305 sock_put(sk); 1306 } 1307 1308 static void mptcp_subflow_shutdown(struct sock *ssk, int how, 1309 bool data_fin_tx_enable, u64 data_fin_tx_seq) 1310 { 1311 lock_sock(ssk); 1312 1313 switch (ssk->sk_state) { 1314 case TCP_LISTEN: 1315 if (!(how & RCV_SHUTDOWN)) 1316 break; 1317 /* fall through */ 1318 case TCP_SYN_SENT: 1319 tcp_disconnect(ssk, O_NONBLOCK); 1320 break; 1321 default: 1322 if (data_fin_tx_enable) { 1323 struct mptcp_subflow_context *subflow; 1324 1325 subflow = mptcp_subflow_ctx(ssk); 1326 subflow->data_fin_tx_seq = data_fin_tx_seq; 1327 subflow->data_fin_tx_enable = 1; 1328 } 1329 1330 ssk->sk_shutdown |= how; 1331 tcp_shutdown(ssk, how); 1332 break; 1333 } 1334 1335 /* Wake up anyone sleeping in poll. */ 1336 ssk->sk_state_change(ssk); 1337 release_sock(ssk); 1338 } 1339 1340 /* Called with msk lock held, releases such lock before returning */ 1341 static void mptcp_close(struct sock *sk, long timeout) 1342 { 1343 struct mptcp_subflow_context *subflow, *tmp; 1344 struct mptcp_sock *msk = mptcp_sk(sk); 1345 LIST_HEAD(conn_list); 1346 u64 data_fin_tx_seq; 1347 1348 lock_sock(sk); 1349 1350 inet_sk_state_store(sk, TCP_CLOSE); 1351 1352 /* be sure to always acquire the join list lock, to sync vs 1353 * mptcp_finish_join(). 1354 */ 1355 spin_lock_bh(&msk->join_list_lock); 1356 list_splice_tail_init(&msk->join_list, &msk->conn_list); 1357 spin_unlock_bh(&msk->join_list_lock); 1358 list_splice_init(&msk->conn_list, &conn_list); 1359 1360 data_fin_tx_seq = msk->write_seq; 1361 1362 __mptcp_clear_xmit(sk); 1363 1364 release_sock(sk); 1365 1366 list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 1367 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1368 1369 subflow->data_fin_tx_seq = data_fin_tx_seq; 1370 subflow->data_fin_tx_enable = 1; 1371 __mptcp_close_ssk(sk, ssk, subflow, timeout); 1372 } 1373 1374 mptcp_cancel_work(sk); 1375 mptcp_pm_close(msk); 1376 1377 __skb_queue_purge(&sk->sk_receive_queue); 1378 1379 sk_common_release(sk); 1380 } 1381 1382 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 1383 { 1384 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1385 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 1386 struct ipv6_pinfo *msk6 = inet6_sk(msk); 1387 1388 msk->sk_v6_daddr = ssk->sk_v6_daddr; 1389 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 1390 1391 if (msk6 && ssk6) { 1392 msk6->saddr = ssk6->saddr; 1393 msk6->flow_label = ssk6->flow_label; 1394 } 1395 #endif 1396 1397 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 1398 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 1399 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 1400 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 1401 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 1402 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 1403 } 1404 1405 static int mptcp_disconnect(struct sock *sk, int flags) 1406 { 1407 /* Should never be called. 1408 * inet_stream_connect() calls ->disconnect, but that 1409 * refers to the subflow socket, not the mptcp one. 1410 */ 1411 WARN_ON_ONCE(1); 1412 return 0; 1413 } 1414 1415 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1416 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 1417 { 1418 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 1419 1420 return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 1421 } 1422 #endif 1423 1424 struct sock *mptcp_sk_clone(const struct sock *sk, 1425 const struct mptcp_options_received *mp_opt, 1426 struct request_sock *req) 1427 { 1428 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 1429 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 1430 struct mptcp_sock *msk; 1431 u64 ack_seq; 1432 1433 if (!nsk) 1434 return NULL; 1435 1436 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1437 if (nsk->sk_family == AF_INET6) 1438 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 1439 #endif 1440 1441 __mptcp_init_sock(nsk); 1442 1443 msk = mptcp_sk(nsk); 1444 msk->local_key = subflow_req->local_key; 1445 msk->token = subflow_req->token; 1446 msk->subflow = NULL; 1447 1448 if (unlikely(mptcp_token_new_accept(subflow_req->token, nsk))) { 1449 nsk->sk_state = TCP_CLOSE; 1450 bh_unlock_sock(nsk); 1451 1452 /* we can't call into mptcp_close() here - possible BH context 1453 * free the sock directly. 1454 * sk_clone_lock() sets nsk refcnt to two, hence call sk_free() 1455 * too. 1456 */ 1457 sk_common_release(nsk); 1458 sk_free(nsk); 1459 return NULL; 1460 } 1461 1462 msk->write_seq = subflow_req->idsn + 1; 1463 atomic64_set(&msk->snd_una, msk->write_seq); 1464 if (mp_opt->mp_capable) { 1465 msk->can_ack = true; 1466 msk->remote_key = mp_opt->sndr_key; 1467 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 1468 ack_seq++; 1469 msk->ack_seq = ack_seq; 1470 } 1471 1472 sock_reset_flag(nsk, SOCK_RCU_FREE); 1473 /* will be fully established after successful MPC subflow creation */ 1474 inet_sk_state_store(nsk, TCP_SYN_RECV); 1475 bh_unlock_sock(nsk); 1476 1477 /* keep a single reference */ 1478 __sock_put(nsk); 1479 return nsk; 1480 } 1481 1482 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 1483 bool kern) 1484 { 1485 struct mptcp_sock *msk = mptcp_sk(sk); 1486 struct socket *listener; 1487 struct sock *newsk; 1488 1489 listener = __mptcp_nmpc_socket(msk); 1490 if (WARN_ON_ONCE(!listener)) { 1491 *err = -EINVAL; 1492 return NULL; 1493 } 1494 1495 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 1496 newsk = inet_csk_accept(listener->sk, flags, err, kern); 1497 if (!newsk) 1498 return NULL; 1499 1500 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 1501 1502 if (sk_is_mptcp(newsk)) { 1503 struct mptcp_subflow_context *subflow; 1504 struct sock *new_mptcp_sock; 1505 struct sock *ssk = newsk; 1506 1507 subflow = mptcp_subflow_ctx(newsk); 1508 new_mptcp_sock = subflow->conn; 1509 1510 /* is_mptcp should be false if subflow->conn is missing, see 1511 * subflow_syn_recv_sock() 1512 */ 1513 if (WARN_ON_ONCE(!new_mptcp_sock)) { 1514 tcp_sk(newsk)->is_mptcp = 0; 1515 return newsk; 1516 } 1517 1518 /* acquire the 2nd reference for the owning socket */ 1519 sock_hold(new_mptcp_sock); 1520 1521 local_bh_disable(); 1522 bh_lock_sock(new_mptcp_sock); 1523 msk = mptcp_sk(new_mptcp_sock); 1524 msk->first = newsk; 1525 1526 newsk = new_mptcp_sock; 1527 mptcp_copy_inaddrs(newsk, ssk); 1528 list_add(&subflow->node, &msk->conn_list); 1529 inet_sk_state_store(newsk, TCP_ESTABLISHED); 1530 1531 bh_unlock_sock(new_mptcp_sock); 1532 1533 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 1534 local_bh_enable(); 1535 } else { 1536 MPTCP_INC_STATS(sock_net(sk), 1537 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 1538 } 1539 1540 return newsk; 1541 } 1542 1543 static void mptcp_destroy(struct sock *sk) 1544 { 1545 struct mptcp_sock *msk = mptcp_sk(sk); 1546 1547 mptcp_token_destroy(msk->token); 1548 if (msk->cached_ext) 1549 __skb_ext_put(msk->cached_ext); 1550 1551 sk_sockets_allocated_dec(sk); 1552 } 1553 1554 static int mptcp_setsockopt(struct sock *sk, int level, int optname, 1555 char __user *optval, unsigned int optlen) 1556 { 1557 struct mptcp_sock *msk = mptcp_sk(sk); 1558 struct socket *ssock; 1559 1560 pr_debug("msk=%p", msk); 1561 1562 /* @@ the meaning of setsockopt() when the socket is connected and 1563 * there are multiple subflows is not yet defined. It is up to the 1564 * MPTCP-level socket to configure the subflows until the subflow 1565 * is in TCP fallback, when TCP socket options are passed through 1566 * to the one remaining subflow. 1567 */ 1568 lock_sock(sk); 1569 ssock = __mptcp_tcp_fallback(msk); 1570 release_sock(sk); 1571 if (ssock) 1572 return tcp_setsockopt(ssock->sk, level, optname, optval, 1573 optlen); 1574 1575 return -EOPNOTSUPP; 1576 } 1577 1578 static int mptcp_getsockopt(struct sock *sk, int level, int optname, 1579 char __user *optval, int __user *option) 1580 { 1581 struct mptcp_sock *msk = mptcp_sk(sk); 1582 struct socket *ssock; 1583 1584 pr_debug("msk=%p", msk); 1585 1586 /* @@ the meaning of setsockopt() when the socket is connected and 1587 * there are multiple subflows is not yet defined. It is up to the 1588 * MPTCP-level socket to configure the subflows until the subflow 1589 * is in TCP fallback, when socket options are passed through 1590 * to the one remaining subflow. 1591 */ 1592 lock_sock(sk); 1593 ssock = __mptcp_tcp_fallback(msk); 1594 release_sock(sk); 1595 if (ssock) 1596 return tcp_getsockopt(ssock->sk, level, optname, optval, 1597 option); 1598 1599 return -EOPNOTSUPP; 1600 } 1601 1602 #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \ 1603 TCPF_WRITE_TIMER_DEFERRED) 1604 1605 /* this is very alike tcp_release_cb() but we must handle differently a 1606 * different set of events 1607 */ 1608 static void mptcp_release_cb(struct sock *sk) 1609 { 1610 unsigned long flags, nflags; 1611 1612 do { 1613 flags = sk->sk_tsq_flags; 1614 if (!(flags & MPTCP_DEFERRED_ALL)) 1615 return; 1616 nflags = flags & ~MPTCP_DEFERRED_ALL; 1617 } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); 1618 1619 sock_release_ownership(sk); 1620 1621 if (flags & TCPF_DELACK_TIMER_DEFERRED) { 1622 struct mptcp_sock *msk = mptcp_sk(sk); 1623 struct sock *ssk; 1624 1625 ssk = mptcp_subflow_recv_lookup(msk); 1626 if (!ssk || !schedule_work(&msk->work)) 1627 __sock_put(sk); 1628 } 1629 1630 if (flags & TCPF_WRITE_TIMER_DEFERRED) { 1631 mptcp_retransmit_handler(sk); 1632 __sock_put(sk); 1633 } 1634 } 1635 1636 static int mptcp_get_port(struct sock *sk, unsigned short snum) 1637 { 1638 struct mptcp_sock *msk = mptcp_sk(sk); 1639 struct socket *ssock; 1640 1641 ssock = __mptcp_nmpc_socket(msk); 1642 pr_debug("msk=%p, subflow=%p", msk, ssock); 1643 if (WARN_ON_ONCE(!ssock)) 1644 return -EINVAL; 1645 1646 return inet_csk_get_port(ssock->sk, snum); 1647 } 1648 1649 void mptcp_finish_connect(struct sock *ssk) 1650 { 1651 struct mptcp_subflow_context *subflow; 1652 struct mptcp_sock *msk; 1653 struct sock *sk; 1654 u64 ack_seq; 1655 1656 subflow = mptcp_subflow_ctx(ssk); 1657 sk = subflow->conn; 1658 msk = mptcp_sk(sk); 1659 1660 if (!subflow->mp_capable) { 1661 MPTCP_INC_STATS(sock_net(sk), 1662 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK); 1663 return; 1664 } 1665 1666 pr_debug("msk=%p, token=%u", sk, subflow->token); 1667 1668 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 1669 ack_seq++; 1670 subflow->map_seq = ack_seq; 1671 subflow->map_subflow_seq = 1; 1672 subflow->rel_write_seq = 1; 1673 1674 /* the socket is not connected yet, no msk/subflow ops can access/race 1675 * accessing the field below 1676 */ 1677 WRITE_ONCE(msk->remote_key, subflow->remote_key); 1678 WRITE_ONCE(msk->local_key, subflow->local_key); 1679 WRITE_ONCE(msk->token, subflow->token); 1680 WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 1681 WRITE_ONCE(msk->ack_seq, ack_seq); 1682 WRITE_ONCE(msk->can_ack, 1); 1683 atomic64_set(&msk->snd_una, msk->write_seq); 1684 1685 mptcp_pm_new_connection(msk, 0); 1686 } 1687 1688 static void mptcp_sock_graft(struct sock *sk, struct socket *parent) 1689 { 1690 write_lock_bh(&sk->sk_callback_lock); 1691 rcu_assign_pointer(sk->sk_wq, &parent->wq); 1692 sk_set_socket(sk, parent); 1693 sk->sk_uid = SOCK_INODE(parent)->i_uid; 1694 write_unlock_bh(&sk->sk_callback_lock); 1695 } 1696 1697 bool mptcp_finish_join(struct sock *sk) 1698 { 1699 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1700 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 1701 struct sock *parent = (void *)msk; 1702 struct socket *parent_sock; 1703 bool ret; 1704 1705 pr_debug("msk=%p, subflow=%p", msk, subflow); 1706 1707 /* mptcp socket already closing? */ 1708 if (inet_sk_state_load(parent) != TCP_ESTABLISHED) 1709 return false; 1710 1711 if (!msk->pm.server_side) 1712 return true; 1713 1714 if (!mptcp_pm_allow_new_subflow(msk)) 1715 return false; 1716 1717 /* active connections are already on conn_list, and we can't acquire 1718 * msk lock here. 1719 * use the join list lock as synchronization point and double-check 1720 * msk status to avoid racing with mptcp_close() 1721 */ 1722 spin_lock_bh(&msk->join_list_lock); 1723 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED; 1724 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) 1725 list_add_tail(&subflow->node, &msk->join_list); 1726 spin_unlock_bh(&msk->join_list_lock); 1727 if (!ret) 1728 return false; 1729 1730 /* attach to msk socket only after we are sure he will deal with us 1731 * at close time 1732 */ 1733 parent_sock = READ_ONCE(parent->sk_socket); 1734 if (parent_sock && !sk->sk_socket) 1735 mptcp_sock_graft(sk, parent_sock); 1736 subflow->map_seq = msk->ack_seq; 1737 return true; 1738 } 1739 1740 static bool mptcp_memory_free(const struct sock *sk, int wake) 1741 { 1742 struct mptcp_sock *msk = mptcp_sk(sk); 1743 1744 return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true; 1745 } 1746 1747 static struct proto mptcp_prot = { 1748 .name = "MPTCP", 1749 .owner = THIS_MODULE, 1750 .init = mptcp_init_sock, 1751 .disconnect = mptcp_disconnect, 1752 .close = mptcp_close, 1753 .accept = mptcp_accept, 1754 .setsockopt = mptcp_setsockopt, 1755 .getsockopt = mptcp_getsockopt, 1756 .shutdown = tcp_shutdown, 1757 .destroy = mptcp_destroy, 1758 .sendmsg = mptcp_sendmsg, 1759 .recvmsg = mptcp_recvmsg, 1760 .release_cb = mptcp_release_cb, 1761 .hash = inet_hash, 1762 .unhash = inet_unhash, 1763 .get_port = mptcp_get_port, 1764 .sockets_allocated = &mptcp_sockets_allocated, 1765 .memory_allocated = &tcp_memory_allocated, 1766 .memory_pressure = &tcp_memory_pressure, 1767 .stream_memory_free = mptcp_memory_free, 1768 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 1769 .sysctl_mem = sysctl_tcp_mem, 1770 .obj_size = sizeof(struct mptcp_sock), 1771 .no_autobind = true, 1772 }; 1773 1774 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 1775 { 1776 struct mptcp_sock *msk = mptcp_sk(sock->sk); 1777 struct socket *ssock; 1778 int err; 1779 1780 lock_sock(sock->sk); 1781 ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE); 1782 if (IS_ERR(ssock)) { 1783 err = PTR_ERR(ssock); 1784 goto unlock; 1785 } 1786 1787 err = ssock->ops->bind(ssock, uaddr, addr_len); 1788 if (!err) 1789 mptcp_copy_inaddrs(sock->sk, ssock->sk); 1790 1791 unlock: 1792 release_sock(sock->sk); 1793 return err; 1794 } 1795 1796 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 1797 int addr_len, int flags) 1798 { 1799 struct mptcp_sock *msk = mptcp_sk(sock->sk); 1800 struct socket *ssock; 1801 int err; 1802 1803 lock_sock(sock->sk); 1804 if (sock->state != SS_UNCONNECTED && msk->subflow) { 1805 /* pending connection or invalid state, let existing subflow 1806 * cope with that 1807 */ 1808 ssock = msk->subflow; 1809 goto do_connect; 1810 } 1811 1812 ssock = __mptcp_socket_create(msk, TCP_SYN_SENT); 1813 if (IS_ERR(ssock)) { 1814 err = PTR_ERR(ssock); 1815 goto unlock; 1816 } 1817 1818 #ifdef CONFIG_TCP_MD5SIG 1819 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 1820 * TCP option space. 1821 */ 1822 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 1823 mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0; 1824 #endif 1825 1826 do_connect: 1827 err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 1828 sock->state = ssock->state; 1829 1830 /* on successful connect, the msk state will be moved to established by 1831 * subflow_finish_connect() 1832 */ 1833 if (!err || err == EINPROGRESS) 1834 mptcp_copy_inaddrs(sock->sk, ssock->sk); 1835 else 1836 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 1837 1838 unlock: 1839 release_sock(sock->sk); 1840 return err; 1841 } 1842 1843 static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr, 1844 int peer) 1845 { 1846 if (sock->sk->sk_prot == &tcp_prot) { 1847 /* we are being invoked from __sys_accept4, after 1848 * mptcp_accept() has just accepted a non-mp-capable 1849 * flow: sk is a tcp_sk, not an mptcp one. 1850 * 1851 * Hand the socket over to tcp so all further socket ops 1852 * bypass mptcp. 1853 */ 1854 sock->ops = &inet_stream_ops; 1855 } 1856 1857 return inet_getname(sock, uaddr, peer); 1858 } 1859 1860 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1861 static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr, 1862 int peer) 1863 { 1864 if (sock->sk->sk_prot == &tcpv6_prot) { 1865 /* we are being invoked from __sys_accept4 after 1866 * mptcp_accept() has accepted a non-mp-capable 1867 * subflow: sk is a tcp_sk, not mptcp. 1868 * 1869 * Hand the socket over to tcp so all further 1870 * socket ops bypass mptcp. 1871 */ 1872 sock->ops = &inet6_stream_ops; 1873 } 1874 1875 return inet6_getname(sock, uaddr, peer); 1876 } 1877 #endif 1878 1879 static int mptcp_listen(struct socket *sock, int backlog) 1880 { 1881 struct mptcp_sock *msk = mptcp_sk(sock->sk); 1882 struct socket *ssock; 1883 int err; 1884 1885 pr_debug("msk=%p", msk); 1886 1887 lock_sock(sock->sk); 1888 ssock = __mptcp_socket_create(msk, TCP_LISTEN); 1889 if (IS_ERR(ssock)) { 1890 err = PTR_ERR(ssock); 1891 goto unlock; 1892 } 1893 1894 sock_set_flag(sock->sk, SOCK_RCU_FREE); 1895 1896 err = ssock->ops->listen(ssock, backlog); 1897 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 1898 if (!err) 1899 mptcp_copy_inaddrs(sock->sk, ssock->sk); 1900 1901 unlock: 1902 release_sock(sock->sk); 1903 return err; 1904 } 1905 1906 static bool is_tcp_proto(const struct proto *p) 1907 { 1908 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1909 return p == &tcp_prot || p == &tcpv6_prot; 1910 #else 1911 return p == &tcp_prot; 1912 #endif 1913 } 1914 1915 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 1916 int flags, bool kern) 1917 { 1918 struct mptcp_sock *msk = mptcp_sk(sock->sk); 1919 struct socket *ssock; 1920 int err; 1921 1922 pr_debug("msk=%p", msk); 1923 1924 lock_sock(sock->sk); 1925 if (sock->sk->sk_state != TCP_LISTEN) 1926 goto unlock_fail; 1927 1928 ssock = __mptcp_nmpc_socket(msk); 1929 if (!ssock) 1930 goto unlock_fail; 1931 1932 sock_hold(ssock->sk); 1933 release_sock(sock->sk); 1934 1935 err = ssock->ops->accept(sock, newsock, flags, kern); 1936 if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) { 1937 struct mptcp_sock *msk = mptcp_sk(newsock->sk); 1938 struct mptcp_subflow_context *subflow; 1939 1940 /* set ssk->sk_socket of accept()ed flows to mptcp socket. 1941 * This is needed so NOSPACE flag can be set from tcp stack. 1942 */ 1943 __mptcp_flush_join_list(msk); 1944 list_for_each_entry(subflow, &msk->conn_list, node) { 1945 struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1946 1947 if (!ssk->sk_socket) 1948 mptcp_sock_graft(ssk, newsock); 1949 } 1950 } 1951 1952 sock_put(ssock->sk); 1953 return err; 1954 1955 unlock_fail: 1956 release_sock(sock->sk); 1957 return -EINVAL; 1958 } 1959 1960 static __poll_t mptcp_poll(struct file *file, struct socket *sock, 1961 struct poll_table_struct *wait) 1962 { 1963 struct sock *sk = sock->sk; 1964 struct mptcp_sock *msk; 1965 struct socket *ssock; 1966 __poll_t mask = 0; 1967 1968 msk = mptcp_sk(sk); 1969 lock_sock(sk); 1970 ssock = __mptcp_tcp_fallback(msk); 1971 if (!ssock) 1972 ssock = __mptcp_nmpc_socket(msk); 1973 if (ssock) { 1974 mask = ssock->ops->poll(file, ssock, wait); 1975 release_sock(sk); 1976 return mask; 1977 } 1978 1979 release_sock(sk); 1980 sock_poll_wait(file, sock, wait); 1981 lock_sock(sk); 1982 1983 if (test_bit(MPTCP_DATA_READY, &msk->flags)) 1984 mask = EPOLLIN | EPOLLRDNORM; 1985 if (sk_stream_is_writeable(sk) && 1986 test_bit(MPTCP_SEND_SPACE, &msk->flags)) 1987 mask |= EPOLLOUT | EPOLLWRNORM; 1988 if (sk->sk_shutdown & RCV_SHUTDOWN) 1989 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 1990 1991 release_sock(sk); 1992 1993 return mask; 1994 } 1995 1996 static int mptcp_shutdown(struct socket *sock, int how) 1997 { 1998 struct mptcp_sock *msk = mptcp_sk(sock->sk); 1999 struct mptcp_subflow_context *subflow; 2000 struct socket *ssock; 2001 int ret = 0; 2002 2003 pr_debug("sk=%p, how=%d", msk, how); 2004 2005 lock_sock(sock->sk); 2006 ssock = __mptcp_tcp_fallback(msk); 2007 if (ssock) { 2008 release_sock(sock->sk); 2009 return inet_shutdown(ssock, how); 2010 } 2011 2012 if (how == SHUT_WR || how == SHUT_RDWR) 2013 inet_sk_state_store(sock->sk, TCP_FIN_WAIT1); 2014 2015 how++; 2016 2017 if ((how & ~SHUTDOWN_MASK) || !how) { 2018 ret = -EINVAL; 2019 goto out_unlock; 2020 } 2021 2022 if (sock->state == SS_CONNECTING) { 2023 if ((1 << sock->sk->sk_state) & 2024 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) 2025 sock->state = SS_DISCONNECTING; 2026 else 2027 sock->state = SS_CONNECTED; 2028 } 2029 2030 __mptcp_flush_join_list(msk); 2031 mptcp_for_each_subflow(msk, subflow) { 2032 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 2033 2034 mptcp_subflow_shutdown(tcp_sk, how, 1, msk->write_seq); 2035 } 2036 2037 out_unlock: 2038 release_sock(sock->sk); 2039 2040 return ret; 2041 } 2042 2043 static const struct proto_ops mptcp_stream_ops = { 2044 .family = PF_INET, 2045 .owner = THIS_MODULE, 2046 .release = inet_release, 2047 .bind = mptcp_bind, 2048 .connect = mptcp_stream_connect, 2049 .socketpair = sock_no_socketpair, 2050 .accept = mptcp_stream_accept, 2051 .getname = mptcp_v4_getname, 2052 .poll = mptcp_poll, 2053 .ioctl = inet_ioctl, 2054 .gettstamp = sock_gettstamp, 2055 .listen = mptcp_listen, 2056 .shutdown = mptcp_shutdown, 2057 .setsockopt = sock_common_setsockopt, 2058 .getsockopt = sock_common_getsockopt, 2059 .sendmsg = inet_sendmsg, 2060 .recvmsg = inet_recvmsg, 2061 .mmap = sock_no_mmap, 2062 .sendpage = inet_sendpage, 2063 #ifdef CONFIG_COMPAT 2064 .compat_setsockopt = compat_sock_common_setsockopt, 2065 .compat_getsockopt = compat_sock_common_getsockopt, 2066 #endif 2067 }; 2068 2069 static struct inet_protosw mptcp_protosw = { 2070 .type = SOCK_STREAM, 2071 .protocol = IPPROTO_MPTCP, 2072 .prot = &mptcp_prot, 2073 .ops = &mptcp_stream_ops, 2074 .flags = INET_PROTOSW_ICSK, 2075 }; 2076 2077 void mptcp_proto_init(void) 2078 { 2079 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 2080 2081 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 2082 panic("Failed to allocate MPTCP pcpu counter\n"); 2083 2084 mptcp_subflow_init(); 2085 mptcp_pm_init(); 2086 2087 if (proto_register(&mptcp_prot, 1) != 0) 2088 panic("Failed to register MPTCP proto.\n"); 2089 2090 inet_register_protosw(&mptcp_protosw); 2091 2092 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 2093 } 2094 2095 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2096 static const struct proto_ops mptcp_v6_stream_ops = { 2097 .family = PF_INET6, 2098 .owner = THIS_MODULE, 2099 .release = inet6_release, 2100 .bind = mptcp_bind, 2101 .connect = mptcp_stream_connect, 2102 .socketpair = sock_no_socketpair, 2103 .accept = mptcp_stream_accept, 2104 .getname = mptcp_v6_getname, 2105 .poll = mptcp_poll, 2106 .ioctl = inet6_ioctl, 2107 .gettstamp = sock_gettstamp, 2108 .listen = mptcp_listen, 2109 .shutdown = mptcp_shutdown, 2110 .setsockopt = sock_common_setsockopt, 2111 .getsockopt = sock_common_getsockopt, 2112 .sendmsg = inet6_sendmsg, 2113 .recvmsg = inet6_recvmsg, 2114 .mmap = sock_no_mmap, 2115 .sendpage = inet_sendpage, 2116 #ifdef CONFIG_COMPAT 2117 .compat_ioctl = inet6_compat_ioctl, 2118 .compat_setsockopt = compat_sock_common_setsockopt, 2119 .compat_getsockopt = compat_sock_common_getsockopt, 2120 #endif 2121 }; 2122 2123 static struct proto mptcp_v6_prot; 2124 2125 static void mptcp_v6_destroy(struct sock *sk) 2126 { 2127 mptcp_destroy(sk); 2128 inet6_destroy_sock(sk); 2129 } 2130 2131 static struct inet_protosw mptcp_v6_protosw = { 2132 .type = SOCK_STREAM, 2133 .protocol = IPPROTO_MPTCP, 2134 .prot = &mptcp_v6_prot, 2135 .ops = &mptcp_v6_stream_ops, 2136 .flags = INET_PROTOSW_ICSK, 2137 }; 2138 2139 int mptcp_proto_v6_init(void) 2140 { 2141 int err; 2142 2143 mptcp_v6_prot = mptcp_prot; 2144 strcpy(mptcp_v6_prot.name, "MPTCPv6"); 2145 mptcp_v6_prot.slab = NULL; 2146 mptcp_v6_prot.destroy = mptcp_v6_destroy; 2147 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 2148 2149 err = proto_register(&mptcp_v6_prot, 1); 2150 if (err) 2151 return err; 2152 2153 err = inet6_register_protosw(&mptcp_v6_protosw); 2154 if (err) 2155 proto_unregister(&mptcp_v6_prot); 2156 2157 return err; 2158 } 2159 #endif 2160