1f870fa0bSMat Martineau // SPDX-License-Identifier: GPL-2.0 2f870fa0bSMat Martineau /* Multipath TCP 3f870fa0bSMat Martineau * 4f870fa0bSMat Martineau * Copyright (c) 2017 - 2019, Intel Corporation. 5f870fa0bSMat Martineau */ 6f870fa0bSMat Martineau 7f870fa0bSMat Martineau #define pr_fmt(fmt) "MPTCP: " fmt 8f870fa0bSMat Martineau 9f870fa0bSMat Martineau #include <linux/kernel.h> 10f870fa0bSMat Martineau #include <linux/module.h> 11f870fa0bSMat Martineau #include <linux/netdevice.h> 127a6a6cbcSPaolo Abeni #include <linux/sched/signal.h> 137a6a6cbcSPaolo Abeni #include <linux/atomic.h> 14f870fa0bSMat Martineau #include <net/sock.h> 15f870fa0bSMat Martineau #include <net/inet_common.h> 16f870fa0bSMat Martineau #include <net/inet_hashtables.h> 17f870fa0bSMat Martineau #include <net/protocol.h> 18f870fa0bSMat Martineau #include <net/tcp.h> 19cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 20cf7da0d6SPeter Krystad #include <net/transp_v6.h> 21cf7da0d6SPeter Krystad #endif 22f870fa0bSMat Martineau #include <net/mptcp.h> 23f870fa0bSMat Martineau #include "protocol.h" 24fc518953SFlorian Westphal #include "mib.h" 25f870fa0bSMat Martineau 262303f994SPeter Krystad #define MPTCP_SAME_STATE TCP_MAX_STATES 272303f994SPeter Krystad 28b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 29b0519de8SFlorian Westphal struct mptcp6_sock { 30b0519de8SFlorian Westphal struct mptcp_sock msk; 31b0519de8SFlorian Westphal struct ipv6_pinfo np; 32b0519de8SFlorian Westphal }; 33b0519de8SFlorian Westphal #endif 34b0519de8SFlorian Westphal 356771bfd9SFlorian Westphal struct mptcp_skb_cb { 366771bfd9SFlorian Westphal u32 offset; 376771bfd9SFlorian Westphal }; 386771bfd9SFlorian Westphal 396771bfd9SFlorian Westphal #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) 406771bfd9SFlorian Westphal 41d027236cSPaolo Abeni static struct percpu_counter mptcp_sockets_allocated; 42d027236cSPaolo Abeni 432303f994SPeter Krystad /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not 442303f994SPeter Krystad * completed yet or has failed, return the subflow socket. 452303f994SPeter Krystad * Otherwise return NULL. 462303f994SPeter Krystad */ 472303f994SPeter Krystad static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk) 482303f994SPeter Krystad { 49d22f4988SChristoph Paasch if (!msk->subflow || READ_ONCE(msk->can_ack)) 502303f994SPeter Krystad return NULL; 512303f994SPeter Krystad 522303f994SPeter Krystad return msk->subflow; 532303f994SPeter Krystad } 542303f994SPeter Krystad 558ab183deSPaolo Abeni static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk) 568ab183deSPaolo Abeni { 578ab183deSPaolo Abeni return msk->first && !sk_is_mptcp(msk->first); 588ab183deSPaolo Abeni } 598ab183deSPaolo Abeni 600b4f33deSFlorian Westphal static struct socket *mptcp_is_tcpsk(struct sock *sk) 610b4f33deSFlorian Westphal { 620b4f33deSFlorian Westphal struct socket *sock = sk->sk_socket; 630b4f33deSFlorian Westphal 640b4f33deSFlorian Westphal if (sock->sk != sk) 650b4f33deSFlorian Westphal return NULL; 660b4f33deSFlorian Westphal 670b4f33deSFlorian Westphal if (unlikely(sk->sk_prot == &tcp_prot)) { 680b4f33deSFlorian Westphal /* we are being invoked after mptcp_accept() has 690b4f33deSFlorian Westphal * accepted a non-mp-capable flow: sk is a tcp_sk, 700b4f33deSFlorian Westphal * not an mptcp one. 710b4f33deSFlorian Westphal * 720b4f33deSFlorian Westphal * Hand the socket over to tcp so all further socket ops 730b4f33deSFlorian Westphal * bypass mptcp. 740b4f33deSFlorian Westphal */ 750b4f33deSFlorian Westphal sock->ops = &inet_stream_ops; 760b4f33deSFlorian Westphal return sock; 770b4f33deSFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 780b4f33deSFlorian Westphal } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { 790b4f33deSFlorian Westphal sock->ops = &inet6_stream_ops; 800b4f33deSFlorian Westphal return sock; 810b4f33deSFlorian Westphal #endif 820b4f33deSFlorian Westphal } 830b4f33deSFlorian Westphal 840b4f33deSFlorian Westphal return NULL; 850b4f33deSFlorian Westphal } 860b4f33deSFlorian Westphal 878ab183deSPaolo Abeni static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk) 88cec37a6eSPeter Krystad { 890b4f33deSFlorian Westphal struct socket *sock; 900b4f33deSFlorian Westphal 91cec37a6eSPeter Krystad sock_owned_by_me((const struct sock *)msk); 92cec37a6eSPeter Krystad 930b4f33deSFlorian Westphal sock = mptcp_is_tcpsk((struct sock *)msk); 940b4f33deSFlorian Westphal if (unlikely(sock)) 950b4f33deSFlorian Westphal return sock; 960b4f33deSFlorian Westphal 978ab183deSPaolo Abeni if (likely(!__mptcp_needs_tcp_fallback(msk))) 98cec37a6eSPeter Krystad return NULL; 99cec37a6eSPeter Krystad 1002c22c06cSFlorian Westphal return msk->subflow; 1018ab183deSPaolo Abeni } 1028ab183deSPaolo Abeni 1032303f994SPeter Krystad static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk) 1042303f994SPeter Krystad { 1058ab183deSPaolo Abeni return !msk->first; 1062303f994SPeter Krystad } 1072303f994SPeter Krystad 1082303f994SPeter Krystad static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state) 1092303f994SPeter Krystad { 1102303f994SPeter Krystad struct mptcp_subflow_context *subflow; 1112303f994SPeter Krystad struct sock *sk = (struct sock *)msk; 1122303f994SPeter Krystad struct socket *ssock; 1132303f994SPeter Krystad int err; 1142303f994SPeter Krystad 1150b4f33deSFlorian Westphal ssock = __mptcp_tcp_fallback(msk); 1160b4f33deSFlorian Westphal if (unlikely(ssock)) 1170b4f33deSFlorian Westphal return ssock; 1180b4f33deSFlorian Westphal 1192303f994SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 1202303f994SPeter Krystad if (ssock) 1212303f994SPeter Krystad goto set_state; 1222303f994SPeter Krystad 1232303f994SPeter Krystad if (!__mptcp_can_create_subflow(msk)) 1242303f994SPeter Krystad return ERR_PTR(-EINVAL); 1252303f994SPeter Krystad 1262303f994SPeter Krystad err = mptcp_subflow_create_socket(sk, &ssock); 1272303f994SPeter Krystad if (err) 1282303f994SPeter Krystad return ERR_PTR(err); 1292303f994SPeter Krystad 1308ab183deSPaolo Abeni msk->first = ssock->sk; 1312303f994SPeter Krystad msk->subflow = ssock; 1322303f994SPeter Krystad subflow = mptcp_subflow_ctx(ssock->sk); 133cec37a6eSPeter Krystad list_add(&subflow->node, &msk->conn_list); 1342303f994SPeter Krystad subflow->request_mptcp = 1; 1352303f994SPeter Krystad 1362303f994SPeter Krystad set_state: 1372303f994SPeter Krystad if (state != MPTCP_SAME_STATE) 1382303f994SPeter Krystad inet_sk_state_store(sk, state); 1392303f994SPeter Krystad return ssock; 1402303f994SPeter Krystad } 1412303f994SPeter Krystad 1426771bfd9SFlorian Westphal static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk, 1436771bfd9SFlorian Westphal struct sk_buff *skb, 1446771bfd9SFlorian Westphal unsigned int offset, size_t copy_len) 1456771bfd9SFlorian Westphal { 1466771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 1476771bfd9SFlorian Westphal 1486771bfd9SFlorian Westphal __skb_unlink(skb, &ssk->sk_receive_queue); 149600911ffSFlorian Westphal skb_set_owner_r(skb, sk); 1506771bfd9SFlorian Westphal __skb_queue_tail(&sk->sk_receive_queue, skb); 1516771bfd9SFlorian Westphal 1526771bfd9SFlorian Westphal msk->ack_seq += copy_len; 1536771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset = offset; 1546771bfd9SFlorian Westphal } 1556771bfd9SFlorian Westphal 156de06f573SFlorian Westphal /* both sockets must be locked */ 157de06f573SFlorian Westphal static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, 158de06f573SFlorian Westphal struct sock *ssk) 159de06f573SFlorian Westphal { 160de06f573SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 161de06f573SFlorian Westphal u64 dsn = mptcp_subflow_get_mapped_dsn(subflow); 162de06f573SFlorian Westphal 163de06f573SFlorian Westphal /* revalidate data sequence number. 164de06f573SFlorian Westphal * 165de06f573SFlorian Westphal * mptcp_subflow_data_available() is usually called 166de06f573SFlorian Westphal * without msk lock. Its unlikely (but possible) 167de06f573SFlorian Westphal * that msk->ack_seq has been advanced since the last 168de06f573SFlorian Westphal * call found in-sequence data. 169de06f573SFlorian Westphal */ 170de06f573SFlorian Westphal if (likely(dsn == msk->ack_seq)) 171de06f573SFlorian Westphal return true; 172de06f573SFlorian Westphal 173de06f573SFlorian Westphal subflow->data_avail = 0; 174de06f573SFlorian Westphal return mptcp_subflow_data_available(ssk); 175de06f573SFlorian Westphal } 176de06f573SFlorian Westphal 1776771bfd9SFlorian Westphal static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, 1786771bfd9SFlorian Westphal struct sock *ssk, 1796771bfd9SFlorian Westphal unsigned int *bytes) 1806771bfd9SFlorian Westphal { 1816771bfd9SFlorian Westphal struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 182600911ffSFlorian Westphal struct sock *sk = (struct sock *)msk; 1836771bfd9SFlorian Westphal unsigned int moved = 0; 1846771bfd9SFlorian Westphal bool more_data_avail; 1856771bfd9SFlorian Westphal struct tcp_sock *tp; 1866771bfd9SFlorian Westphal bool done = false; 187600911ffSFlorian Westphal 188de06f573SFlorian Westphal if (!mptcp_subflow_dsn_valid(msk, ssk)) { 189de06f573SFlorian Westphal *bytes = 0; 190de06f573SFlorian Westphal return false; 191de06f573SFlorian Westphal } 192de06f573SFlorian Westphal 193ec33916dSFlorian Westphal if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 194ec33916dSFlorian Westphal int rcvbuf = max(ssk->sk_rcvbuf, sk->sk_rcvbuf); 195ec33916dSFlorian Westphal 196600911ffSFlorian Westphal if (rcvbuf > sk->sk_rcvbuf) 197600911ffSFlorian Westphal sk->sk_rcvbuf = rcvbuf; 198ec33916dSFlorian Westphal } 1996771bfd9SFlorian Westphal 2006771bfd9SFlorian Westphal tp = tcp_sk(ssk); 2016771bfd9SFlorian Westphal do { 2026771bfd9SFlorian Westphal u32 map_remaining, offset; 2036771bfd9SFlorian Westphal u32 seq = tp->copied_seq; 2046771bfd9SFlorian Westphal struct sk_buff *skb; 2056771bfd9SFlorian Westphal bool fin; 2066771bfd9SFlorian Westphal 2076771bfd9SFlorian Westphal /* try to move as much data as available */ 2086771bfd9SFlorian Westphal map_remaining = subflow->map_data_len - 2096771bfd9SFlorian Westphal mptcp_subflow_get_map_offset(subflow); 2106771bfd9SFlorian Westphal 2116771bfd9SFlorian Westphal skb = skb_peek(&ssk->sk_receive_queue); 2126771bfd9SFlorian Westphal if (!skb) 2136771bfd9SFlorian Westphal break; 2146771bfd9SFlorian Westphal 2156771bfd9SFlorian Westphal offset = seq - TCP_SKB_CB(skb)->seq; 2166771bfd9SFlorian Westphal fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN; 2176771bfd9SFlorian Westphal if (fin) { 2186771bfd9SFlorian Westphal done = true; 2196771bfd9SFlorian Westphal seq++; 2206771bfd9SFlorian Westphal } 2216771bfd9SFlorian Westphal 2226771bfd9SFlorian Westphal if (offset < skb->len) { 2236771bfd9SFlorian Westphal size_t len = skb->len - offset; 2246771bfd9SFlorian Westphal 2256771bfd9SFlorian Westphal if (tp->urg_data) 2266771bfd9SFlorian Westphal done = true; 2276771bfd9SFlorian Westphal 2286771bfd9SFlorian Westphal __mptcp_move_skb(msk, ssk, skb, offset, len); 2296771bfd9SFlorian Westphal seq += len; 2306771bfd9SFlorian Westphal moved += len; 2316771bfd9SFlorian Westphal 2326771bfd9SFlorian Westphal if (WARN_ON_ONCE(map_remaining < len)) 2336771bfd9SFlorian Westphal break; 2346771bfd9SFlorian Westphal } else { 2356771bfd9SFlorian Westphal WARN_ON_ONCE(!fin); 2366771bfd9SFlorian Westphal sk_eat_skb(ssk, skb); 2376771bfd9SFlorian Westphal done = true; 2386771bfd9SFlorian Westphal } 2396771bfd9SFlorian Westphal 2406771bfd9SFlorian Westphal WRITE_ONCE(tp->copied_seq, seq); 2416771bfd9SFlorian Westphal more_data_avail = mptcp_subflow_data_available(ssk); 242600911ffSFlorian Westphal 243600911ffSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) { 244600911ffSFlorian Westphal done = true; 245600911ffSFlorian Westphal break; 246600911ffSFlorian Westphal } 2476771bfd9SFlorian Westphal } while (more_data_avail); 2486771bfd9SFlorian Westphal 2496771bfd9SFlorian Westphal *bytes = moved; 2506771bfd9SFlorian Westphal 2516771bfd9SFlorian Westphal return done; 2526771bfd9SFlorian Westphal } 2536771bfd9SFlorian Westphal 2542e52213cSFlorian Westphal /* In most cases we will be able to lock the mptcp socket. If its already 2552e52213cSFlorian Westphal * owned, we need to defer to the work queue to avoid ABBA deadlock. 2562e52213cSFlorian Westphal */ 2572e52213cSFlorian Westphal static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk) 2582e52213cSFlorian Westphal { 2592e52213cSFlorian Westphal struct sock *sk = (struct sock *)msk; 2602e52213cSFlorian Westphal unsigned int moved = 0; 2612e52213cSFlorian Westphal 2622e52213cSFlorian Westphal if (READ_ONCE(sk->sk_lock.owned)) 2632e52213cSFlorian Westphal return false; 2642e52213cSFlorian Westphal 2652e52213cSFlorian Westphal if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock))) 2662e52213cSFlorian Westphal return false; 2672e52213cSFlorian Westphal 2682e52213cSFlorian Westphal /* must re-check after taking the lock */ 2692e52213cSFlorian Westphal if (!READ_ONCE(sk->sk_lock.owned)) 2702e52213cSFlorian Westphal __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 2712e52213cSFlorian Westphal 2722e52213cSFlorian Westphal spin_unlock_bh(&sk->sk_lock.slock); 2732e52213cSFlorian Westphal 2742e52213cSFlorian Westphal return moved > 0; 2752e52213cSFlorian Westphal } 2762e52213cSFlorian Westphal 2772e52213cSFlorian Westphal void mptcp_data_ready(struct sock *sk, struct sock *ssk) 278101f6f85SFlorian Westphal { 279101f6f85SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 280101f6f85SFlorian Westphal 281101f6f85SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 2826771bfd9SFlorian Westphal 2832e52213cSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) && 2842e52213cSFlorian Westphal move_skbs_to_msk(msk, ssk)) 2852e52213cSFlorian Westphal goto wake; 2862e52213cSFlorian Westphal 287600911ffSFlorian Westphal /* don't schedule if mptcp sk is (still) over limit */ 288600911ffSFlorian Westphal if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) 289600911ffSFlorian Westphal goto wake; 290600911ffSFlorian Westphal 29114c441b5SPaolo Abeni /* mptcp socket is owned, release_cb should retry */ 29214c441b5SPaolo Abeni if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, 29314c441b5SPaolo Abeni &sk->sk_tsq_flags)) { 29414c441b5SPaolo Abeni sock_hold(sk); 2956771bfd9SFlorian Westphal 29614c441b5SPaolo Abeni /* need to try again, its possible release_cb() has already 29714c441b5SPaolo Abeni * been called after the test_and_set_bit() above. 29814c441b5SPaolo Abeni */ 29914c441b5SPaolo Abeni move_skbs_to_msk(msk, ssk); 30014c441b5SPaolo Abeni } 301600911ffSFlorian Westphal wake: 302101f6f85SFlorian Westphal sk->sk_data_ready(sk); 303101f6f85SFlorian Westphal } 304101f6f85SFlorian Westphal 305ec3edaa7SPeter Krystad static void __mptcp_flush_join_list(struct mptcp_sock *msk) 306ec3edaa7SPeter Krystad { 307ec3edaa7SPeter Krystad if (likely(list_empty(&msk->join_list))) 308ec3edaa7SPeter Krystad return; 309ec3edaa7SPeter Krystad 310ec3edaa7SPeter Krystad spin_lock_bh(&msk->join_list_lock); 311ec3edaa7SPeter Krystad list_splice_tail_init(&msk->join_list, &msk->conn_list); 312ec3edaa7SPeter Krystad spin_unlock_bh(&msk->join_list_lock); 313ec3edaa7SPeter Krystad } 314ec3edaa7SPeter Krystad 315b51f9b80SPaolo Abeni static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk) 316b51f9b80SPaolo Abeni { 317b51f9b80SPaolo Abeni long tout = ssk && inet_csk(ssk)->icsk_pending ? 318b51f9b80SPaolo Abeni inet_csk(ssk)->icsk_timeout - jiffies : 0; 319b51f9b80SPaolo Abeni 320b51f9b80SPaolo Abeni if (tout <= 0) 321b51f9b80SPaolo Abeni tout = mptcp_sk(sk)->timer_ival; 322b51f9b80SPaolo Abeni mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN; 323b51f9b80SPaolo Abeni } 324b51f9b80SPaolo Abeni 325b51f9b80SPaolo Abeni static bool mptcp_timer_pending(struct sock *sk) 326b51f9b80SPaolo Abeni { 327b51f9b80SPaolo Abeni return timer_pending(&inet_csk(sk)->icsk_retransmit_timer); 328b51f9b80SPaolo Abeni } 329b51f9b80SPaolo Abeni 330b51f9b80SPaolo Abeni static void mptcp_reset_timer(struct sock *sk) 331b51f9b80SPaolo Abeni { 332b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 333b51f9b80SPaolo Abeni unsigned long tout; 334b51f9b80SPaolo Abeni 335b51f9b80SPaolo Abeni /* should never be called with mptcp level timer cleared */ 336b51f9b80SPaolo Abeni tout = READ_ONCE(mptcp_sk(sk)->timer_ival); 337b51f9b80SPaolo Abeni if (WARN_ON_ONCE(!tout)) 338b51f9b80SPaolo Abeni tout = TCP_RTO_MIN; 339b51f9b80SPaolo Abeni sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout); 340b51f9b80SPaolo Abeni } 341b51f9b80SPaolo Abeni 342b51f9b80SPaolo Abeni void mptcp_data_acked(struct sock *sk) 343b51f9b80SPaolo Abeni { 344b51f9b80SPaolo Abeni mptcp_reset_timer(sk); 3453b1d6210SPaolo Abeni 3463b1d6210SPaolo Abeni if (!sk_stream_is_writeable(sk) && 3473b1d6210SPaolo Abeni schedule_work(&mptcp_sk(sk)->work)) 3483b1d6210SPaolo Abeni sock_hold(sk); 349b51f9b80SPaolo Abeni } 350b51f9b80SPaolo Abeni 35159832e24SFlorian Westphal void mptcp_subflow_eof(struct sock *sk) 35259832e24SFlorian Westphal { 35359832e24SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 35459832e24SFlorian Westphal 35559832e24SFlorian Westphal if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) && 35659832e24SFlorian Westphal schedule_work(&msk->work)) 35759832e24SFlorian Westphal sock_hold(sk); 35859832e24SFlorian Westphal } 35959832e24SFlorian Westphal 360b51f9b80SPaolo Abeni static void mptcp_stop_timer(struct sock *sk) 361b51f9b80SPaolo Abeni { 362b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = inet_csk(sk); 363b51f9b80SPaolo Abeni 364b51f9b80SPaolo Abeni sk_stop_timer(sk, &icsk->icsk_retransmit_timer); 365b51f9b80SPaolo Abeni mptcp_sk(sk)->timer_ival = 0; 366b51f9b80SPaolo Abeni } 367b51f9b80SPaolo Abeni 3686d0060f6SMat Martineau static bool mptcp_ext_cache_refill(struct mptcp_sock *msk) 3696d0060f6SMat Martineau { 3706d0060f6SMat Martineau if (!msk->cached_ext) 3716d0060f6SMat Martineau msk->cached_ext = __skb_ext_alloc(); 3726d0060f6SMat Martineau 3736d0060f6SMat Martineau return !!msk->cached_ext; 3746d0060f6SMat Martineau } 3756d0060f6SMat Martineau 3767a6a6cbcSPaolo Abeni static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk) 3777a6a6cbcSPaolo Abeni { 3787a6a6cbcSPaolo Abeni struct mptcp_subflow_context *subflow; 3797a6a6cbcSPaolo Abeni struct sock *sk = (struct sock *)msk; 3807a6a6cbcSPaolo Abeni 3817a6a6cbcSPaolo Abeni sock_owned_by_me(sk); 3827a6a6cbcSPaolo Abeni 3837a6a6cbcSPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 3847a6a6cbcSPaolo Abeni if (subflow->data_avail) 3857a6a6cbcSPaolo Abeni return mptcp_subflow_tcp_sock(subflow); 3867a6a6cbcSPaolo Abeni } 3877a6a6cbcSPaolo Abeni 3887a6a6cbcSPaolo Abeni return NULL; 3897a6a6cbcSPaolo Abeni } 3907a6a6cbcSPaolo Abeni 3913f8e0aaeSPaolo Abeni static bool mptcp_skb_can_collapse_to(u64 write_seq, 39257040755SPaolo Abeni const struct sk_buff *skb, 39357040755SPaolo Abeni const struct mptcp_ext *mpext) 3946d0060f6SMat Martineau { 39557040755SPaolo Abeni if (!tcp_skb_can_collapse_to(skb)) 39657040755SPaolo Abeni return false; 39757040755SPaolo Abeni 39857040755SPaolo Abeni /* can collapse only if MPTCP level sequence is in order */ 3993f8e0aaeSPaolo Abeni return mpext && mpext->data_seq + mpext->data_len == write_seq; 40057040755SPaolo Abeni } 40157040755SPaolo Abeni 40218b683bfSPaolo Abeni static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk, 40318b683bfSPaolo Abeni const struct page_frag *pfrag, 40418b683bfSPaolo Abeni const struct mptcp_data_frag *df) 40518b683bfSPaolo Abeni { 40618b683bfSPaolo Abeni return df && pfrag->page == df->page && 40718b683bfSPaolo Abeni df->data_seq + df->data_len == msk->write_seq; 40818b683bfSPaolo Abeni } 40918b683bfSPaolo Abeni 410d027236cSPaolo Abeni static void dfrag_uncharge(struct sock *sk, int len) 41118b683bfSPaolo Abeni { 412d027236cSPaolo Abeni sk_mem_uncharge(sk, len); 4137948f6ccSFlorian Westphal sk_wmem_queued_add(sk, -len); 414d027236cSPaolo Abeni } 415d027236cSPaolo Abeni 416d027236cSPaolo Abeni static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag) 417d027236cSPaolo Abeni { 418d027236cSPaolo Abeni int len = dfrag->data_len + dfrag->overhead; 419d027236cSPaolo Abeni 42018b683bfSPaolo Abeni list_del(&dfrag->list); 421d027236cSPaolo Abeni dfrag_uncharge(sk, len); 42218b683bfSPaolo Abeni put_page(dfrag->page); 42318b683bfSPaolo Abeni } 42418b683bfSPaolo Abeni 42518b683bfSPaolo Abeni static void mptcp_clean_una(struct sock *sk) 42618b683bfSPaolo Abeni { 42718b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 42818b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 42918b683bfSPaolo Abeni u64 snd_una = atomic64_read(&msk->snd_una); 430d027236cSPaolo Abeni bool cleaned = false; 43118b683bfSPaolo Abeni 43218b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) { 43318b683bfSPaolo Abeni if (after64(dfrag->data_seq + dfrag->data_len, snd_una)) 43418b683bfSPaolo Abeni break; 43518b683bfSPaolo Abeni 436d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 437d027236cSPaolo Abeni cleaned = true; 438d027236cSPaolo Abeni } 439d027236cSPaolo Abeni 4407948f6ccSFlorian Westphal dfrag = mptcp_rtx_head(sk); 4417948f6ccSFlorian Westphal if (dfrag && after64(snd_una, dfrag->data_seq)) { 4427948f6ccSFlorian Westphal u64 delta = dfrag->data_seq + dfrag->data_len - snd_una; 4437948f6ccSFlorian Westphal 4447948f6ccSFlorian Westphal dfrag->data_seq += delta; 4457948f6ccSFlorian Westphal dfrag->data_len -= delta; 4467948f6ccSFlorian Westphal 4477948f6ccSFlorian Westphal dfrag_uncharge(sk, delta); 4487948f6ccSFlorian Westphal cleaned = true; 4497948f6ccSFlorian Westphal } 4507948f6ccSFlorian Westphal 451d027236cSPaolo Abeni if (cleaned) { 452d027236cSPaolo Abeni sk_mem_reclaim_partial(sk); 4537948f6ccSFlorian Westphal 4547948f6ccSFlorian Westphal /* Only wake up writers if a subflow is ready */ 4557948f6ccSFlorian Westphal if (test_bit(MPTCP_SEND_SPACE, &msk->flags)) 4567948f6ccSFlorian Westphal sk_stream_write_space(sk); 45718b683bfSPaolo Abeni } 45818b683bfSPaolo Abeni } 45918b683bfSPaolo Abeni 46018b683bfSPaolo Abeni /* ensure we get enough memory for the frag hdr, beyond some minimal amount of 46118b683bfSPaolo Abeni * data 46218b683bfSPaolo Abeni */ 46318b683bfSPaolo Abeni static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 46418b683bfSPaolo Abeni { 46518b683bfSPaolo Abeni if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag), 46618b683bfSPaolo Abeni pfrag, sk->sk_allocation))) 46718b683bfSPaolo Abeni return true; 46818b683bfSPaolo Abeni 46918b683bfSPaolo Abeni sk->sk_prot->enter_memory_pressure(sk); 47018b683bfSPaolo Abeni sk_stream_moderate_sndbuf(sk); 47118b683bfSPaolo Abeni return false; 47218b683bfSPaolo Abeni } 47318b683bfSPaolo Abeni 47418b683bfSPaolo Abeni static struct mptcp_data_frag * 47518b683bfSPaolo Abeni mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag, 47618b683bfSPaolo Abeni int orig_offset) 47718b683bfSPaolo Abeni { 47818b683bfSPaolo Abeni int offset = ALIGN(orig_offset, sizeof(long)); 47918b683bfSPaolo Abeni struct mptcp_data_frag *dfrag; 48018b683bfSPaolo Abeni 48118b683bfSPaolo Abeni dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset); 48218b683bfSPaolo Abeni dfrag->data_len = 0; 48318b683bfSPaolo Abeni dfrag->data_seq = msk->write_seq; 48418b683bfSPaolo Abeni dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag); 48518b683bfSPaolo Abeni dfrag->offset = offset + sizeof(struct mptcp_data_frag); 48618b683bfSPaolo Abeni dfrag->page = pfrag->page; 48718b683bfSPaolo Abeni 48818b683bfSPaolo Abeni return dfrag; 48918b683bfSPaolo Abeni } 49018b683bfSPaolo Abeni 49157040755SPaolo Abeni static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, 4923f8e0aaeSPaolo Abeni struct msghdr *msg, struct mptcp_data_frag *dfrag, 4933f8e0aaeSPaolo Abeni long *timeo, int *pmss_now, 49457040755SPaolo Abeni int *ps_goal) 49557040755SPaolo Abeni { 49618b683bfSPaolo Abeni int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0; 49718b683bfSPaolo Abeni bool dfrag_collapsed, can_collapse = false; 4986d0060f6SMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 4996d0060f6SMat Martineau struct mptcp_ext *mpext = NULL; 5003f8e0aaeSPaolo Abeni bool retransmission = !!dfrag; 50157040755SPaolo Abeni struct sk_buff *skb, *tail; 5026d0060f6SMat Martineau struct page_frag *pfrag; 5033f8e0aaeSPaolo Abeni struct page *page; 5043f8e0aaeSPaolo Abeni u64 *write_seq; 5056d0060f6SMat Martineau size_t psize; 5066d0060f6SMat Martineau 5076d0060f6SMat Martineau /* use the mptcp page cache so that we can easily move the data 5086d0060f6SMat Martineau * from one substream to another, but do per subflow memory accounting 5093f8e0aaeSPaolo Abeni * Note: pfrag is used only !retransmission, but the compiler if 5103f8e0aaeSPaolo Abeni * fooled into a warning if we don't init here 5116d0060f6SMat Martineau */ 5126d0060f6SMat Martineau pfrag = sk_page_frag(sk); 5133f8e0aaeSPaolo Abeni while ((!retransmission && !mptcp_page_frag_refill(ssk, pfrag)) || 5146d0060f6SMat Martineau !mptcp_ext_cache_refill(msk)) { 5156d0060f6SMat Martineau ret = sk_stream_wait_memory(ssk, timeo); 5166d0060f6SMat Martineau if (ret) 5176d0060f6SMat Martineau return ret; 51818b683bfSPaolo Abeni 51918b683bfSPaolo Abeni /* if sk_stream_wait_memory() sleeps snd_una can change 52018b683bfSPaolo Abeni * significantly, refresh the rtx queue 52118b683bfSPaolo Abeni */ 52218b683bfSPaolo Abeni mptcp_clean_una(sk); 52318b683bfSPaolo Abeni 5248ab183deSPaolo Abeni if (unlikely(__mptcp_needs_tcp_fallback(msk))) 5258ab183deSPaolo Abeni return 0; 5266d0060f6SMat Martineau } 5273f8e0aaeSPaolo Abeni if (!retransmission) { 5283f8e0aaeSPaolo Abeni write_seq = &msk->write_seq; 5293f8e0aaeSPaolo Abeni page = pfrag->page; 5303f8e0aaeSPaolo Abeni } else { 5313f8e0aaeSPaolo Abeni write_seq = &dfrag->data_seq; 5323f8e0aaeSPaolo Abeni page = dfrag->page; 5333f8e0aaeSPaolo Abeni } 5346d0060f6SMat Martineau 5356d0060f6SMat Martineau /* compute copy limit */ 5366d0060f6SMat Martineau mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags); 53757040755SPaolo Abeni *pmss_now = mss_now; 53857040755SPaolo Abeni *ps_goal = size_goal; 53957040755SPaolo Abeni avail_size = size_goal; 54057040755SPaolo Abeni skb = tcp_write_queue_tail(ssk); 54157040755SPaolo Abeni if (skb) { 54257040755SPaolo Abeni mpext = skb_ext_find(skb, SKB_EXT_MPTCP); 5436d0060f6SMat Martineau 54457040755SPaolo Abeni /* Limit the write to the size available in the 54557040755SPaolo Abeni * current skb, if any, so that we create at most a new skb. 54657040755SPaolo Abeni * Explicitly tells TCP internals to avoid collapsing on later 54757040755SPaolo Abeni * queue management operation, to avoid breaking the ext <-> 54857040755SPaolo Abeni * SSN association set here 54957040755SPaolo Abeni */ 55057040755SPaolo Abeni can_collapse = (size_goal - skb->len > 0) && 5513f8e0aaeSPaolo Abeni mptcp_skb_can_collapse_to(*write_seq, skb, mpext); 55257040755SPaolo Abeni if (!can_collapse) 55357040755SPaolo Abeni TCP_SKB_CB(skb)->eor = 1; 55457040755SPaolo Abeni else 55557040755SPaolo Abeni avail_size = size_goal - skb->len; 55657040755SPaolo Abeni } 55718b683bfSPaolo Abeni 5583f8e0aaeSPaolo Abeni if (!retransmission) { 5593f8e0aaeSPaolo Abeni /* reuse tail pfrag, if possible, or carve a new one from the 5603f8e0aaeSPaolo Abeni * page allocator 56118b683bfSPaolo Abeni */ 56218b683bfSPaolo Abeni dfrag = mptcp_rtx_tail(sk); 56318b683bfSPaolo Abeni offset = pfrag->offset; 56418b683bfSPaolo Abeni dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag); 56518b683bfSPaolo Abeni if (!dfrag_collapsed) { 56618b683bfSPaolo Abeni dfrag = mptcp_carve_data_frag(msk, pfrag, offset); 56718b683bfSPaolo Abeni offset = dfrag->offset; 56818b683bfSPaolo Abeni frag_truesize = dfrag->overhead; 56918b683bfSPaolo Abeni } 57018b683bfSPaolo Abeni psize = min_t(size_t, pfrag->size - offset, avail_size); 57157040755SPaolo Abeni 57257040755SPaolo Abeni /* Copy to page */ 5736d0060f6SMat Martineau pr_debug("left=%zu", msg_data_left(msg)); 57418b683bfSPaolo Abeni psize = copy_page_from_iter(pfrag->page, offset, 5753f8e0aaeSPaolo Abeni min_t(size_t, msg_data_left(msg), 5763f8e0aaeSPaolo Abeni psize), 5776d0060f6SMat Martineau &msg->msg_iter); 5786d0060f6SMat Martineau pr_debug("left=%zu", msg_data_left(msg)); 5796d0060f6SMat Martineau if (!psize) 5806d0060f6SMat Martineau return -EINVAL; 5816d0060f6SMat Martineau 582d027236cSPaolo Abeni if (!sk_wmem_schedule(sk, psize + dfrag->overhead)) 583d027236cSPaolo Abeni return -ENOMEM; 5843f8e0aaeSPaolo Abeni } else { 5853f8e0aaeSPaolo Abeni offset = dfrag->offset; 5863f8e0aaeSPaolo Abeni psize = min_t(size_t, dfrag->data_len, avail_size); 5873f8e0aaeSPaolo Abeni } 588d027236cSPaolo Abeni 58957040755SPaolo Abeni /* tell the TCP stack to delay the push so that we can safely 59057040755SPaolo Abeni * access the skb after the sendpages call 5916d0060f6SMat Martineau */ 5923f8e0aaeSPaolo Abeni ret = do_tcp_sendpages(ssk, page, offset, psize, 5936d0060f6SMat Martineau msg->msg_flags | MSG_SENDPAGE_NOTLAST); 5946d0060f6SMat Martineau if (ret <= 0) 5956d0060f6SMat Martineau return ret; 59618b683bfSPaolo Abeni 59718b683bfSPaolo Abeni frag_truesize += ret; 5983f8e0aaeSPaolo Abeni if (!retransmission) { 5996d0060f6SMat Martineau if (unlikely(ret < psize)) 6006d0060f6SMat Martineau iov_iter_revert(&msg->msg_iter, psize - ret); 6016d0060f6SMat Martineau 60218b683bfSPaolo Abeni /* send successful, keep track of sent data for mptcp-level 60318b683bfSPaolo Abeni * retransmission 60418b683bfSPaolo Abeni */ 60518b683bfSPaolo Abeni dfrag->data_len += ret; 60618b683bfSPaolo Abeni if (!dfrag_collapsed) { 60718b683bfSPaolo Abeni get_page(dfrag->page); 60818b683bfSPaolo Abeni list_add_tail(&dfrag->list, &msk->rtx_queue); 6093f8e0aaeSPaolo Abeni sk_wmem_queued_add(sk, frag_truesize); 6103f8e0aaeSPaolo Abeni } else { 6113f8e0aaeSPaolo Abeni sk_wmem_queued_add(sk, ret); 61218b683bfSPaolo Abeni } 61318b683bfSPaolo Abeni 614d027236cSPaolo Abeni /* charge data on mptcp rtx queue to the master socket 615d027236cSPaolo Abeni * Note: we charge such data both to sk and ssk 616d027236cSPaolo Abeni */ 617d027236cSPaolo Abeni sk->sk_forward_alloc -= frag_truesize; 6183f8e0aaeSPaolo Abeni } 619d027236cSPaolo Abeni 62057040755SPaolo Abeni /* if the tail skb extension is still the cached one, collapsing 62157040755SPaolo Abeni * really happened. Note: we can't check for 'same skb' as the sk_buff 62257040755SPaolo Abeni * hdr on tail can be transmitted, freed and re-allocated by the 62357040755SPaolo Abeni * do_tcp_sendpages() call 62457040755SPaolo Abeni */ 62557040755SPaolo Abeni tail = tcp_write_queue_tail(ssk); 62657040755SPaolo Abeni if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) { 62757040755SPaolo Abeni WARN_ON_ONCE(!can_collapse); 62857040755SPaolo Abeni mpext->data_len += ret; 62957040755SPaolo Abeni goto out; 63057040755SPaolo Abeni } 63157040755SPaolo Abeni 6326d0060f6SMat Martineau skb = tcp_write_queue_tail(ssk); 6336d0060f6SMat Martineau mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext); 6346d0060f6SMat Martineau msk->cached_ext = NULL; 6356d0060f6SMat Martineau 6366d0060f6SMat Martineau memset(mpext, 0, sizeof(*mpext)); 6373f8e0aaeSPaolo Abeni mpext->data_seq = *write_seq; 6386d0060f6SMat Martineau mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq; 6396d0060f6SMat Martineau mpext->data_len = ret; 6406d0060f6SMat Martineau mpext->use_map = 1; 6416d0060f6SMat Martineau mpext->dsn64 = 1; 6426d0060f6SMat Martineau 6436d0060f6SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d", 6446d0060f6SMat Martineau mpext->data_seq, mpext->subflow_seq, mpext->data_len, 6456d0060f6SMat Martineau mpext->dsn64); 6466d0060f6SMat Martineau 64757040755SPaolo Abeni out: 6483f8e0aaeSPaolo Abeni if (!retransmission) 64918b683bfSPaolo Abeni pfrag->offset += frag_truesize; 6503f8e0aaeSPaolo Abeni *write_seq += ret; 6516d0060f6SMat Martineau mptcp_subflow_ctx(ssk)->rel_write_seq += ret; 6526d0060f6SMat Martineau 6536d0060f6SMat Martineau return ret; 6546d0060f6SMat Martineau } 6556d0060f6SMat Martineau 656f296234cSPeter Krystad static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk) 657f296234cSPeter Krystad { 658f296234cSPeter Krystad struct mptcp_subflow_context *subflow; 659f296234cSPeter Krystad struct sock *backup = NULL; 660f296234cSPeter Krystad 661f296234cSPeter Krystad sock_owned_by_me((const struct sock *)msk); 662f296234cSPeter Krystad 663f296234cSPeter Krystad mptcp_for_each_subflow(msk, subflow) { 664f296234cSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 665f296234cSPeter Krystad 666f296234cSPeter Krystad if (!sk_stream_memory_free(ssk)) { 667f296234cSPeter Krystad struct socket *sock = ssk->sk_socket; 668f296234cSPeter Krystad 669f296234cSPeter Krystad if (sock) { 670f296234cSPeter Krystad clear_bit(MPTCP_SEND_SPACE, &msk->flags); 671f296234cSPeter Krystad smp_mb__after_atomic(); 672f296234cSPeter Krystad 673f296234cSPeter Krystad /* enables sk->write_space() callbacks */ 674f296234cSPeter Krystad set_bit(SOCK_NOSPACE, &sock->flags); 675f296234cSPeter Krystad } 676f296234cSPeter Krystad 677f296234cSPeter Krystad return NULL; 678f296234cSPeter Krystad } 679f296234cSPeter Krystad 680f296234cSPeter Krystad if (subflow->backup) { 681f296234cSPeter Krystad if (!backup) 682f296234cSPeter Krystad backup = ssk; 683f296234cSPeter Krystad 684f296234cSPeter Krystad continue; 685f296234cSPeter Krystad } 686f296234cSPeter Krystad 687f296234cSPeter Krystad return ssk; 688f296234cSPeter Krystad } 689f296234cSPeter Krystad 690f296234cSPeter Krystad return backup; 691f296234cSPeter Krystad } 692f296234cSPeter Krystad 6931891c4a0SFlorian Westphal static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk) 6941891c4a0SFlorian Westphal { 6951891c4a0SFlorian Westphal struct socket *sock; 6961891c4a0SFlorian Westphal 6971891c4a0SFlorian Westphal if (likely(sk_stream_is_writeable(ssk))) 6981891c4a0SFlorian Westphal return; 6991891c4a0SFlorian Westphal 7001891c4a0SFlorian Westphal sock = READ_ONCE(ssk->sk_socket); 7011891c4a0SFlorian Westphal 7021891c4a0SFlorian Westphal if (sock) { 7031891c4a0SFlorian Westphal clear_bit(MPTCP_SEND_SPACE, &msk->flags); 7041891c4a0SFlorian Westphal smp_mb__after_atomic(); 7051891c4a0SFlorian Westphal /* set NOSPACE only after clearing SEND_SPACE flag */ 7061891c4a0SFlorian Westphal set_bit(SOCK_NOSPACE, &sock->flags); 7071891c4a0SFlorian Westphal } 7081891c4a0SFlorian Westphal } 7091891c4a0SFlorian Westphal 710f870fa0bSMat Martineau static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 711f870fa0bSMat Martineau { 71257040755SPaolo Abeni int mss_now = 0, size_goal = 0, ret = 0; 713f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 714cec37a6eSPeter Krystad struct socket *ssock; 7156d0060f6SMat Martineau size_t copied = 0; 716cec37a6eSPeter Krystad struct sock *ssk; 7176d0060f6SMat Martineau long timeo; 718f870fa0bSMat Martineau 719f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 720f870fa0bSMat Martineau return -EOPNOTSUPP; 721f870fa0bSMat Martineau 722cec37a6eSPeter Krystad lock_sock(sk); 7231954b860SMat Martineau 7241954b860SMat Martineau timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 7251954b860SMat Martineau 7261954b860SMat Martineau if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) { 7271954b860SMat Martineau ret = sk_stream_wait_connect(sk, &timeo); 7281954b860SMat Martineau if (ret) 7291954b860SMat Martineau goto out; 7301954b860SMat Martineau } 7311954b860SMat Martineau 732e154659bSFlorian Westphal fallback: 733cec37a6eSPeter Krystad ssock = __mptcp_tcp_fallback(msk); 7348ab183deSPaolo Abeni if (unlikely(ssock)) { 735e154659bSFlorian Westphal release_sock(sk); 736cec37a6eSPeter Krystad pr_debug("fallback passthrough"); 737cec37a6eSPeter Krystad ret = sock_sendmsg(ssock, msg); 7388ab183deSPaolo Abeni return ret >= 0 ? ret + copied : (copied ? copied : ret); 739cec37a6eSPeter Krystad } 740cec37a6eSPeter Krystad 74118b683bfSPaolo Abeni mptcp_clean_una(sk); 74218b683bfSPaolo Abeni 743ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 744f296234cSPeter Krystad ssk = mptcp_subflow_get_send(msk); 745f296234cSPeter Krystad while (!sk_stream_memory_free(sk) || !ssk) { 746f296234cSPeter Krystad ret = sk_stream_wait_memory(sk, &timeo); 747f296234cSPeter Krystad if (ret) 748f296234cSPeter Krystad goto out; 749f296234cSPeter Krystad 75018b683bfSPaolo Abeni mptcp_clean_una(sk); 75118b683bfSPaolo Abeni 752f296234cSPeter Krystad ssk = mptcp_subflow_get_send(msk); 753f296234cSPeter Krystad if (list_empty(&msk->conn_list)) { 754f296234cSPeter Krystad ret = -ENOTCONN; 755f296234cSPeter Krystad goto out; 756f296234cSPeter Krystad } 757cec37a6eSPeter Krystad } 758cec37a6eSPeter Krystad 7596d0060f6SMat Martineau pr_debug("conn_list->subflow=%p", ssk); 760cec37a6eSPeter Krystad 7616d0060f6SMat Martineau lock_sock(ssk); 7626d0060f6SMat Martineau while (msg_data_left(msg)) { 7633f8e0aaeSPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now, 76457040755SPaolo Abeni &size_goal); 7656d0060f6SMat Martineau if (ret < 0) 7666d0060f6SMat Martineau break; 7678ab183deSPaolo Abeni if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) { 768e154659bSFlorian Westphal /* Can happen for passive sockets: 769e154659bSFlorian Westphal * 3WHS negotiated MPTCP, but first packet after is 770e154659bSFlorian Westphal * plain TCP (e.g. due to middlebox filtering unknown 771e154659bSFlorian Westphal * options). 772e154659bSFlorian Westphal * 773e154659bSFlorian Westphal * Fall back to TCP. 774e154659bSFlorian Westphal */ 7758ab183deSPaolo Abeni release_sock(ssk); 7768ab183deSPaolo Abeni goto fallback; 7778ab183deSPaolo Abeni } 7786d0060f6SMat Martineau 7796d0060f6SMat Martineau copied += ret; 7806d0060f6SMat Martineau } 7816d0060f6SMat Martineau 782b51f9b80SPaolo Abeni mptcp_set_timeout(sk, ssk); 78357040755SPaolo Abeni if (copied) { 7846d0060f6SMat Martineau ret = copied; 78557040755SPaolo Abeni tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle, 78657040755SPaolo Abeni size_goal); 787b51f9b80SPaolo Abeni 788b51f9b80SPaolo Abeni /* start the timer, if it's not pending */ 789b51f9b80SPaolo Abeni if (!mptcp_timer_pending(sk)) 790b51f9b80SPaolo Abeni mptcp_reset_timer(sk); 79157040755SPaolo Abeni } 7926d0060f6SMat Martineau 7931891c4a0SFlorian Westphal ssk_check_wmem(msk, ssk); 7946d0060f6SMat Martineau release_sock(ssk); 7951954b860SMat Martineau out: 796cec37a6eSPeter Krystad release_sock(sk); 797cec37a6eSPeter Krystad return ret; 798f870fa0bSMat Martineau } 799f870fa0bSMat Martineau 8007a6a6cbcSPaolo Abeni static void mptcp_wait_data(struct sock *sk, long *timeo) 8017a6a6cbcSPaolo Abeni { 8027a6a6cbcSPaolo Abeni DEFINE_WAIT_FUNC(wait, woken_wake_function); 8037a6a6cbcSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 8047a6a6cbcSPaolo Abeni 8057a6a6cbcSPaolo Abeni add_wait_queue(sk_sleep(sk), &wait); 8067a6a6cbcSPaolo Abeni sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 8077a6a6cbcSPaolo Abeni 8087a6a6cbcSPaolo Abeni sk_wait_event(sk, timeo, 8097a6a6cbcSPaolo Abeni test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait); 8107a6a6cbcSPaolo Abeni 8117a6a6cbcSPaolo Abeni sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 8127a6a6cbcSPaolo Abeni remove_wait_queue(sk_sleep(sk), &wait); 8137a6a6cbcSPaolo Abeni } 8147a6a6cbcSPaolo Abeni 8156771bfd9SFlorian Westphal static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk, 8166771bfd9SFlorian Westphal struct msghdr *msg, 8176771bfd9SFlorian Westphal size_t len) 8186771bfd9SFlorian Westphal { 8196771bfd9SFlorian Westphal struct sock *sk = (struct sock *)msk; 8206771bfd9SFlorian Westphal struct sk_buff *skb; 8216771bfd9SFlorian Westphal int copied = 0; 8226771bfd9SFlorian Westphal 8236771bfd9SFlorian Westphal while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) { 8246771bfd9SFlorian Westphal u32 offset = MPTCP_SKB_CB(skb)->offset; 8256771bfd9SFlorian Westphal u32 data_len = skb->len - offset; 8266771bfd9SFlorian Westphal u32 count = min_t(size_t, len - copied, data_len); 8276771bfd9SFlorian Westphal int err; 8286771bfd9SFlorian Westphal 8296771bfd9SFlorian Westphal err = skb_copy_datagram_msg(skb, offset, msg, count); 8306771bfd9SFlorian Westphal if (unlikely(err < 0)) { 8316771bfd9SFlorian Westphal if (!copied) 8326771bfd9SFlorian Westphal return err; 8336771bfd9SFlorian Westphal break; 8346771bfd9SFlorian Westphal } 8356771bfd9SFlorian Westphal 8366771bfd9SFlorian Westphal copied += count; 8376771bfd9SFlorian Westphal 8386771bfd9SFlorian Westphal if (count < data_len) { 8396771bfd9SFlorian Westphal MPTCP_SKB_CB(skb)->offset += count; 8406771bfd9SFlorian Westphal break; 8416771bfd9SFlorian Westphal } 8426771bfd9SFlorian Westphal 8436771bfd9SFlorian Westphal __skb_unlink(skb, &sk->sk_receive_queue); 8446771bfd9SFlorian Westphal __kfree_skb(skb); 8456771bfd9SFlorian Westphal 8466771bfd9SFlorian Westphal if (copied >= len) 8476771bfd9SFlorian Westphal break; 8486771bfd9SFlorian Westphal } 8496771bfd9SFlorian Westphal 8506771bfd9SFlorian Westphal return copied; 8516771bfd9SFlorian Westphal } 8526771bfd9SFlorian Westphal 8536771bfd9SFlorian Westphal static bool __mptcp_move_skbs(struct mptcp_sock *msk) 8546771bfd9SFlorian Westphal { 8556771bfd9SFlorian Westphal unsigned int moved = 0; 8566771bfd9SFlorian Westphal bool done; 8576771bfd9SFlorian Westphal 8586771bfd9SFlorian Westphal do { 8596771bfd9SFlorian Westphal struct sock *ssk = mptcp_subflow_recv_lookup(msk); 8606771bfd9SFlorian Westphal 8616771bfd9SFlorian Westphal if (!ssk) 8626771bfd9SFlorian Westphal break; 8636771bfd9SFlorian Westphal 8646771bfd9SFlorian Westphal lock_sock(ssk); 8656771bfd9SFlorian Westphal done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved); 8666771bfd9SFlorian Westphal release_sock(ssk); 8676771bfd9SFlorian Westphal } while (!done); 8686771bfd9SFlorian Westphal 8696771bfd9SFlorian Westphal return moved > 0; 8706771bfd9SFlorian Westphal } 8716771bfd9SFlorian Westphal 872f870fa0bSMat Martineau static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 873f870fa0bSMat Martineau int nonblock, int flags, int *addr_len) 874f870fa0bSMat Martineau { 875f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 876cec37a6eSPeter Krystad struct socket *ssock; 877cec37a6eSPeter Krystad int copied = 0; 8787a6a6cbcSPaolo Abeni int target; 8797a6a6cbcSPaolo Abeni long timeo; 880f870fa0bSMat Martineau 881f870fa0bSMat Martineau if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT)) 882f870fa0bSMat Martineau return -EOPNOTSUPP; 883f870fa0bSMat Martineau 884cec37a6eSPeter Krystad lock_sock(sk); 885cec37a6eSPeter Krystad ssock = __mptcp_tcp_fallback(msk); 8868ab183deSPaolo Abeni if (unlikely(ssock)) { 8878ab183deSPaolo Abeni fallback: 888e154659bSFlorian Westphal release_sock(sk); 889cec37a6eSPeter Krystad pr_debug("fallback-read subflow=%p", 890cec37a6eSPeter Krystad mptcp_subflow_ctx(ssock->sk)); 891cec37a6eSPeter Krystad copied = sock_recvmsg(ssock, msg, flags); 892cec37a6eSPeter Krystad return copied; 893cec37a6eSPeter Krystad } 894cec37a6eSPeter Krystad 8957a6a6cbcSPaolo Abeni timeo = sock_rcvtimeo(sk, nonblock); 8967a6a6cbcSPaolo Abeni 8977a6a6cbcSPaolo Abeni len = min_t(size_t, len, INT_MAX); 8987a6a6cbcSPaolo Abeni target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 899ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 9007a6a6cbcSPaolo Abeni 9016771bfd9SFlorian Westphal while (len > (size_t)copied) { 9027a6a6cbcSPaolo Abeni int bytes_read; 9037a6a6cbcSPaolo Abeni 9046771bfd9SFlorian Westphal bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied); 9056771bfd9SFlorian Westphal if (unlikely(bytes_read < 0)) { 9067a6a6cbcSPaolo Abeni if (!copied) 9077a6a6cbcSPaolo Abeni copied = bytes_read; 9086771bfd9SFlorian Westphal goto out_err; 909cec37a6eSPeter Krystad } 910cec37a6eSPeter Krystad 9117a6a6cbcSPaolo Abeni copied += bytes_read; 9127a6a6cbcSPaolo Abeni 9136771bfd9SFlorian Westphal if (skb_queue_empty(&sk->sk_receive_queue) && 9146771bfd9SFlorian Westphal __mptcp_move_skbs(msk)) 9156771bfd9SFlorian Westphal continue; 9167a6a6cbcSPaolo Abeni 9177a6a6cbcSPaolo Abeni /* only the master socket status is relevant here. The exit 9187a6a6cbcSPaolo Abeni * conditions mirror closely tcp_recvmsg() 9197a6a6cbcSPaolo Abeni */ 9207a6a6cbcSPaolo Abeni if (copied >= target) 9217a6a6cbcSPaolo Abeni break; 9227a6a6cbcSPaolo Abeni 9237a6a6cbcSPaolo Abeni if (copied) { 9247a6a6cbcSPaolo Abeni if (sk->sk_err || 9257a6a6cbcSPaolo Abeni sk->sk_state == TCP_CLOSE || 9267a6a6cbcSPaolo Abeni (sk->sk_shutdown & RCV_SHUTDOWN) || 9277a6a6cbcSPaolo Abeni !timeo || 9287a6a6cbcSPaolo Abeni signal_pending(current)) 9297a6a6cbcSPaolo Abeni break; 9307a6a6cbcSPaolo Abeni } else { 9317a6a6cbcSPaolo Abeni if (sk->sk_err) { 9327a6a6cbcSPaolo Abeni copied = sock_error(sk); 9337a6a6cbcSPaolo Abeni break; 9347a6a6cbcSPaolo Abeni } 9357a6a6cbcSPaolo Abeni 9367a6a6cbcSPaolo Abeni if (sk->sk_shutdown & RCV_SHUTDOWN) 9377a6a6cbcSPaolo Abeni break; 9387a6a6cbcSPaolo Abeni 9397a6a6cbcSPaolo Abeni if (sk->sk_state == TCP_CLOSE) { 9407a6a6cbcSPaolo Abeni copied = -ENOTCONN; 9417a6a6cbcSPaolo Abeni break; 9427a6a6cbcSPaolo Abeni } 9437a6a6cbcSPaolo Abeni 9447a6a6cbcSPaolo Abeni if (!timeo) { 9457a6a6cbcSPaolo Abeni copied = -EAGAIN; 9467a6a6cbcSPaolo Abeni break; 9477a6a6cbcSPaolo Abeni } 9487a6a6cbcSPaolo Abeni 9497a6a6cbcSPaolo Abeni if (signal_pending(current)) { 9507a6a6cbcSPaolo Abeni copied = sock_intr_errno(timeo); 9517a6a6cbcSPaolo Abeni break; 9527a6a6cbcSPaolo Abeni } 9537a6a6cbcSPaolo Abeni } 9547a6a6cbcSPaolo Abeni 9557a6a6cbcSPaolo Abeni pr_debug("block timeout %ld", timeo); 9567a6a6cbcSPaolo Abeni mptcp_wait_data(sk, &timeo); 9578ab183deSPaolo Abeni if (unlikely(__mptcp_tcp_fallback(msk))) 9588ab183deSPaolo Abeni goto fallback; 9597a6a6cbcSPaolo Abeni } 9607a6a6cbcSPaolo Abeni 9616771bfd9SFlorian Westphal if (skb_queue_empty(&sk->sk_receive_queue)) { 9626771bfd9SFlorian Westphal /* entire backlog drained, clear DATA_READY. */ 9637a6a6cbcSPaolo Abeni clear_bit(MPTCP_DATA_READY, &msk->flags); 9647a6a6cbcSPaolo Abeni 9656771bfd9SFlorian Westphal /* .. race-breaker: ssk might have gotten new data 9666771bfd9SFlorian Westphal * after last __mptcp_move_skbs() returned false. 9677a6a6cbcSPaolo Abeni */ 9686771bfd9SFlorian Westphal if (unlikely(__mptcp_move_skbs(msk))) 9696771bfd9SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 9706771bfd9SFlorian Westphal } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) { 9716771bfd9SFlorian Westphal /* data to read but mptcp_wait_data() cleared DATA_READY */ 9727a6a6cbcSPaolo Abeni set_bit(MPTCP_DATA_READY, &msk->flags); 9737a6a6cbcSPaolo Abeni } 9746771bfd9SFlorian Westphal out_err: 975cec37a6eSPeter Krystad release_sock(sk); 976cec37a6eSPeter Krystad return copied; 977cec37a6eSPeter Krystad } 978cec37a6eSPeter Krystad 979b51f9b80SPaolo Abeni static void mptcp_retransmit_handler(struct sock *sk) 980b51f9b80SPaolo Abeni { 981b51f9b80SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 982b51f9b80SPaolo Abeni 9833b1d6210SPaolo Abeni if (atomic64_read(&msk->snd_una) == msk->write_seq) { 984b51f9b80SPaolo Abeni mptcp_stop_timer(sk); 9853b1d6210SPaolo Abeni } else { 9863b1d6210SPaolo Abeni set_bit(MPTCP_WORK_RTX, &msk->flags); 9873b1d6210SPaolo Abeni if (schedule_work(&msk->work)) 9883b1d6210SPaolo Abeni sock_hold(sk); 9893b1d6210SPaolo Abeni } 990b51f9b80SPaolo Abeni } 991b51f9b80SPaolo Abeni 992b51f9b80SPaolo Abeni static void mptcp_retransmit_timer(struct timer_list *t) 993b51f9b80SPaolo Abeni { 994b51f9b80SPaolo Abeni struct inet_connection_sock *icsk = from_timer(icsk, t, 995b51f9b80SPaolo Abeni icsk_retransmit_timer); 996b51f9b80SPaolo Abeni struct sock *sk = &icsk->icsk_inet.sk; 997b51f9b80SPaolo Abeni 998b51f9b80SPaolo Abeni bh_lock_sock(sk); 999b51f9b80SPaolo Abeni if (!sock_owned_by_user(sk)) { 1000b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 1001b51f9b80SPaolo Abeni } else { 1002b51f9b80SPaolo Abeni /* delegate our work to tcp_release_cb() */ 1003b51f9b80SPaolo Abeni if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, 1004b51f9b80SPaolo Abeni &sk->sk_tsq_flags)) 1005b51f9b80SPaolo Abeni sock_hold(sk); 1006b51f9b80SPaolo Abeni } 1007b51f9b80SPaolo Abeni bh_unlock_sock(sk); 1008b51f9b80SPaolo Abeni sock_put(sk); 1009b51f9b80SPaolo Abeni } 1010b51f9b80SPaolo Abeni 10113b1d6210SPaolo Abeni /* Find an idle subflow. Return NULL if there is unacked data at tcp 10123b1d6210SPaolo Abeni * level. 10133b1d6210SPaolo Abeni * 10143b1d6210SPaolo Abeni * A backup subflow is returned only if that is the only kind available. 10153b1d6210SPaolo Abeni */ 10163b1d6210SPaolo Abeni static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk) 10173b1d6210SPaolo Abeni { 10183b1d6210SPaolo Abeni struct mptcp_subflow_context *subflow; 10193b1d6210SPaolo Abeni struct sock *backup = NULL; 10203b1d6210SPaolo Abeni 10213b1d6210SPaolo Abeni sock_owned_by_me((const struct sock *)msk); 10223b1d6210SPaolo Abeni 10233b1d6210SPaolo Abeni mptcp_for_each_subflow(msk, subflow) { 10243b1d6210SPaolo Abeni struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 10253b1d6210SPaolo Abeni 10263b1d6210SPaolo Abeni /* still data outstanding at TCP level? Don't retransmit. */ 10273b1d6210SPaolo Abeni if (!tcp_write_queue_empty(ssk)) 10283b1d6210SPaolo Abeni return NULL; 10293b1d6210SPaolo Abeni 10303b1d6210SPaolo Abeni if (subflow->backup) { 10313b1d6210SPaolo Abeni if (!backup) 10323b1d6210SPaolo Abeni backup = ssk; 10333b1d6210SPaolo Abeni continue; 10343b1d6210SPaolo Abeni } 10353b1d6210SPaolo Abeni 10363b1d6210SPaolo Abeni return ssk; 10373b1d6210SPaolo Abeni } 10383b1d6210SPaolo Abeni 10393b1d6210SPaolo Abeni return backup; 10403b1d6210SPaolo Abeni } 10413b1d6210SPaolo Abeni 1042cec37a6eSPeter Krystad /* subflow sockets can be either outgoing (connect) or incoming 1043cec37a6eSPeter Krystad * (accept). 1044cec37a6eSPeter Krystad * 1045cec37a6eSPeter Krystad * Outgoing subflows use in-kernel sockets. 1046cec37a6eSPeter Krystad * Incoming subflows do not have their own 'struct socket' allocated, 1047cec37a6eSPeter Krystad * so we need to use tcp_close() after detaching them from the mptcp 1048cec37a6eSPeter Krystad * parent socket. 1049cec37a6eSPeter Krystad */ 1050cec37a6eSPeter Krystad static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, 1051cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, 1052cec37a6eSPeter Krystad long timeout) 1053cec37a6eSPeter Krystad { 1054cec37a6eSPeter Krystad struct socket *sock = READ_ONCE(ssk->sk_socket); 1055cec37a6eSPeter Krystad 1056cec37a6eSPeter Krystad list_del(&subflow->node); 1057cec37a6eSPeter Krystad 1058cec37a6eSPeter Krystad if (sock && sock != sk->sk_socket) { 1059cec37a6eSPeter Krystad /* outgoing subflow */ 1060cec37a6eSPeter Krystad sock_release(sock); 1061cec37a6eSPeter Krystad } else { 1062cec37a6eSPeter Krystad /* incoming subflow */ 1063cec37a6eSPeter Krystad tcp_close(ssk, timeout); 1064cec37a6eSPeter Krystad } 1065f870fa0bSMat Martineau } 1066f870fa0bSMat Martineau 1067dc24f8b4SPaolo Abeni static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu) 1068dc24f8b4SPaolo Abeni { 1069dc24f8b4SPaolo Abeni return 0; 1070dc24f8b4SPaolo Abeni } 1071dc24f8b4SPaolo Abeni 107259832e24SFlorian Westphal static void mptcp_check_for_eof(struct mptcp_sock *msk) 107359832e24SFlorian Westphal { 107459832e24SFlorian Westphal struct mptcp_subflow_context *subflow; 107559832e24SFlorian Westphal struct sock *sk = (struct sock *)msk; 107659832e24SFlorian Westphal int receivers = 0; 107759832e24SFlorian Westphal 107859832e24SFlorian Westphal mptcp_for_each_subflow(msk, subflow) 107959832e24SFlorian Westphal receivers += !subflow->rx_eof; 108059832e24SFlorian Westphal 108159832e24SFlorian Westphal if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) { 108259832e24SFlorian Westphal /* hopefully temporary hack: propagate shutdown status 108359832e24SFlorian Westphal * to msk, when all subflows agree on it 108459832e24SFlorian Westphal */ 108559832e24SFlorian Westphal sk->sk_shutdown |= RCV_SHUTDOWN; 108659832e24SFlorian Westphal 108759832e24SFlorian Westphal smp_mb__before_atomic(); /* SHUTDOWN must be visible first */ 108859832e24SFlorian Westphal set_bit(MPTCP_DATA_READY, &msk->flags); 108959832e24SFlorian Westphal sk->sk_data_ready(sk); 109059832e24SFlorian Westphal } 109159832e24SFlorian Westphal } 109259832e24SFlorian Westphal 109380992017SPaolo Abeni static void mptcp_worker(struct work_struct *work) 109480992017SPaolo Abeni { 109580992017SPaolo Abeni struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); 10963b1d6210SPaolo Abeni struct sock *ssk, *sk = &msk->sk.icsk_inet.sk; 10973b1d6210SPaolo Abeni int orig_len, orig_offset, ret, mss_now = 0, size_goal = 0; 10983b1d6210SPaolo Abeni struct mptcp_data_frag *dfrag; 10993b1d6210SPaolo Abeni u64 orig_write_seq; 11003b1d6210SPaolo Abeni size_t copied = 0; 11013b1d6210SPaolo Abeni struct msghdr msg; 11023b1d6210SPaolo Abeni long timeo = 0; 110380992017SPaolo Abeni 110480992017SPaolo Abeni lock_sock(sk); 11053b1d6210SPaolo Abeni mptcp_clean_una(sk); 1106ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 11076771bfd9SFlorian Westphal __mptcp_move_skbs(msk); 11083b1d6210SPaolo Abeni 110959832e24SFlorian Westphal if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags)) 111059832e24SFlorian Westphal mptcp_check_for_eof(msk); 111159832e24SFlorian Westphal 11123b1d6210SPaolo Abeni if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) 11133b1d6210SPaolo Abeni goto unlock; 11143b1d6210SPaolo Abeni 11153b1d6210SPaolo Abeni dfrag = mptcp_rtx_head(sk); 11163b1d6210SPaolo Abeni if (!dfrag) 11173b1d6210SPaolo Abeni goto unlock; 11183b1d6210SPaolo Abeni 11193b1d6210SPaolo Abeni ssk = mptcp_subflow_get_retrans(msk); 11203b1d6210SPaolo Abeni if (!ssk) 11213b1d6210SPaolo Abeni goto reset_unlock; 11223b1d6210SPaolo Abeni 11233b1d6210SPaolo Abeni lock_sock(ssk); 11243b1d6210SPaolo Abeni 11253b1d6210SPaolo Abeni msg.msg_flags = MSG_DONTWAIT; 11263b1d6210SPaolo Abeni orig_len = dfrag->data_len; 11273b1d6210SPaolo Abeni orig_offset = dfrag->offset; 11283b1d6210SPaolo Abeni orig_write_seq = dfrag->data_seq; 11293b1d6210SPaolo Abeni while (dfrag->data_len > 0) { 11303b1d6210SPaolo Abeni ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo, &mss_now, 11313b1d6210SPaolo Abeni &size_goal); 11323b1d6210SPaolo Abeni if (ret < 0) 11333b1d6210SPaolo Abeni break; 11343b1d6210SPaolo Abeni 1135fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS); 11363b1d6210SPaolo Abeni copied += ret; 11373b1d6210SPaolo Abeni dfrag->data_len -= ret; 11383b1d6210SPaolo Abeni dfrag->offset += ret; 11393b1d6210SPaolo Abeni } 11403b1d6210SPaolo Abeni if (copied) 11413b1d6210SPaolo Abeni tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle, 11423b1d6210SPaolo Abeni size_goal); 11433b1d6210SPaolo Abeni 11443b1d6210SPaolo Abeni dfrag->data_seq = orig_write_seq; 11453b1d6210SPaolo Abeni dfrag->offset = orig_offset; 11463b1d6210SPaolo Abeni dfrag->data_len = orig_len; 11473b1d6210SPaolo Abeni 11483b1d6210SPaolo Abeni mptcp_set_timeout(sk, ssk); 11493b1d6210SPaolo Abeni release_sock(ssk); 11503b1d6210SPaolo Abeni 11513b1d6210SPaolo Abeni reset_unlock: 11523b1d6210SPaolo Abeni if (!mptcp_timer_pending(sk)) 11533b1d6210SPaolo Abeni mptcp_reset_timer(sk); 11543b1d6210SPaolo Abeni 11553b1d6210SPaolo Abeni unlock: 115680992017SPaolo Abeni release_sock(sk); 115780992017SPaolo Abeni sock_put(sk); 115880992017SPaolo Abeni } 115980992017SPaolo Abeni 1160784325e9SMatthieu Baerts static int __mptcp_init_sock(struct sock *sk) 1161f870fa0bSMat Martineau { 1162cec37a6eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1163cec37a6eSPeter Krystad 1164ec3edaa7SPeter Krystad spin_lock_init(&msk->join_list_lock); 1165ec3edaa7SPeter Krystad 1166cec37a6eSPeter Krystad INIT_LIST_HEAD(&msk->conn_list); 1167ec3edaa7SPeter Krystad INIT_LIST_HEAD(&msk->join_list); 116818b683bfSPaolo Abeni INIT_LIST_HEAD(&msk->rtx_queue); 11691891c4a0SFlorian Westphal __set_bit(MPTCP_SEND_SPACE, &msk->flags); 117080992017SPaolo Abeni INIT_WORK(&msk->work, mptcp_worker); 1171cec37a6eSPeter Krystad 11728ab183deSPaolo Abeni msk->first = NULL; 1173dc24f8b4SPaolo Abeni inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss; 11748ab183deSPaolo Abeni 11751b1c7a0eSPeter Krystad mptcp_pm_data_init(msk); 11761b1c7a0eSPeter Krystad 1177b51f9b80SPaolo Abeni /* re-use the csk retrans timer for MPTCP-level retrans */ 1178b51f9b80SPaolo Abeni timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0); 1179b51f9b80SPaolo Abeni 1180f870fa0bSMat Martineau return 0; 1181f870fa0bSMat Martineau } 1182f870fa0bSMat Martineau 1183784325e9SMatthieu Baerts static int mptcp_init_sock(struct sock *sk) 1184784325e9SMatthieu Baerts { 1185fc518953SFlorian Westphal struct net *net = sock_net(sk); 1186fc518953SFlorian Westphal int ret; 118718b683bfSPaolo Abeni 1188fc518953SFlorian Westphal if (!mptcp_is_enabled(net)) 1189fc518953SFlorian Westphal return -ENOPROTOOPT; 1190fc518953SFlorian Westphal 1191fc518953SFlorian Westphal if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net)) 1192fc518953SFlorian Westphal return -ENOMEM; 1193fc518953SFlorian Westphal 1194fc518953SFlorian Westphal ret = __mptcp_init_sock(sk); 119518b683bfSPaolo Abeni if (ret) 119618b683bfSPaolo Abeni return ret; 119718b683bfSPaolo Abeni 1198d027236cSPaolo Abeni sk_sockets_allocated_inc(sk); 11993f8e0aaeSPaolo Abeni sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[2]; 1200d027236cSPaolo Abeni 120118b683bfSPaolo Abeni return 0; 120218b683bfSPaolo Abeni } 120318b683bfSPaolo Abeni 120418b683bfSPaolo Abeni static void __mptcp_clear_xmit(struct sock *sk) 120518b683bfSPaolo Abeni { 120618b683bfSPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 120718b683bfSPaolo Abeni struct mptcp_data_frag *dtmp, *dfrag; 120818b683bfSPaolo Abeni 1209b51f9b80SPaolo Abeni sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer); 1210b51f9b80SPaolo Abeni 121118b683bfSPaolo Abeni list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) 1212d027236cSPaolo Abeni dfrag_clear(sk, dfrag); 1213784325e9SMatthieu Baerts } 1214784325e9SMatthieu Baerts 121580992017SPaolo Abeni static void mptcp_cancel_work(struct sock *sk) 121680992017SPaolo Abeni { 121780992017SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 121880992017SPaolo Abeni 121980992017SPaolo Abeni if (cancel_work_sync(&msk->work)) 122080992017SPaolo Abeni sock_put(sk); 122180992017SPaolo Abeni } 122280992017SPaolo Abeni 122376c42a29SMat Martineau static void mptcp_subflow_shutdown(struct sock *ssk, int how, 122476c42a29SMat Martineau bool data_fin_tx_enable, u64 data_fin_tx_seq) 122521498490SPeter Krystad { 122621498490SPeter Krystad lock_sock(ssk); 122721498490SPeter Krystad 122821498490SPeter Krystad switch (ssk->sk_state) { 122921498490SPeter Krystad case TCP_LISTEN: 123021498490SPeter Krystad if (!(how & RCV_SHUTDOWN)) 123121498490SPeter Krystad break; 123221498490SPeter Krystad /* fall through */ 123321498490SPeter Krystad case TCP_SYN_SENT: 123421498490SPeter Krystad tcp_disconnect(ssk, O_NONBLOCK); 123521498490SPeter Krystad break; 123621498490SPeter Krystad default: 123776c42a29SMat Martineau if (data_fin_tx_enable) { 123876c42a29SMat Martineau struct mptcp_subflow_context *subflow; 123976c42a29SMat Martineau 124076c42a29SMat Martineau subflow = mptcp_subflow_ctx(ssk); 124176c42a29SMat Martineau subflow->data_fin_tx_seq = data_fin_tx_seq; 124276c42a29SMat Martineau subflow->data_fin_tx_enable = 1; 124376c42a29SMat Martineau } 124476c42a29SMat Martineau 124521498490SPeter Krystad ssk->sk_shutdown |= how; 124621498490SPeter Krystad tcp_shutdown(ssk, how); 124721498490SPeter Krystad break; 124821498490SPeter Krystad } 124921498490SPeter Krystad 125021498490SPeter Krystad /* Wake up anyone sleeping in poll. */ 125121498490SPeter Krystad ssk->sk_state_change(ssk); 125221498490SPeter Krystad release_sock(ssk); 125321498490SPeter Krystad } 125421498490SPeter Krystad 12558ab183deSPaolo Abeni /* Called with msk lock held, releases such lock before returning */ 12562c22c06cSFlorian Westphal static void mptcp_close(struct sock *sk, long timeout) 1257f870fa0bSMat Martineau { 1258cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow, *tmp; 1259f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1260b2c5b614SFlorian Westphal LIST_HEAD(conn_list); 126176c42a29SMat Martineau u64 data_fin_tx_seq; 1262f870fa0bSMat Martineau 12632c22c06cSFlorian Westphal lock_sock(sk); 12642c22c06cSFlorian Westphal 126579c0949eSPeter Krystad mptcp_token_destroy(msk->token); 1266f870fa0bSMat Martineau inet_sk_state_store(sk, TCP_CLOSE); 1267f870fa0bSMat Martineau 1268ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 1269ec3edaa7SPeter Krystad 1270b2c5b614SFlorian Westphal list_splice_init(&msk->conn_list, &conn_list); 1271b2c5b614SFlorian Westphal 127276c42a29SMat Martineau data_fin_tx_seq = msk->write_seq; 127376c42a29SMat Martineau 127418b683bfSPaolo Abeni __mptcp_clear_xmit(sk); 127518b683bfSPaolo Abeni 1276b2c5b614SFlorian Westphal release_sock(sk); 1277b2c5b614SFlorian Westphal 1278b2c5b614SFlorian Westphal list_for_each_entry_safe(subflow, tmp, &conn_list, node) { 1279cec37a6eSPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1280cec37a6eSPeter Krystad 128176c42a29SMat Martineau subflow->data_fin_tx_seq = data_fin_tx_seq; 128276c42a29SMat Martineau subflow->data_fin_tx_enable = 1; 1283cec37a6eSPeter Krystad __mptcp_close_ssk(sk, ssk, subflow, timeout); 1284f870fa0bSMat Martineau } 1285f870fa0bSMat Martineau 128680992017SPaolo Abeni mptcp_cancel_work(sk); 1287926bdeabSPeter Krystad mptcp_pm_close(msk); 128880992017SPaolo Abeni 12896771bfd9SFlorian Westphal __skb_queue_purge(&sk->sk_receive_queue); 12906771bfd9SFlorian Westphal 1291cec37a6eSPeter Krystad sk_common_release(sk); 1292f870fa0bSMat Martineau } 1293f870fa0bSMat Martineau 1294cf7da0d6SPeter Krystad static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk) 1295cf7da0d6SPeter Krystad { 1296cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1297cf7da0d6SPeter Krystad const struct ipv6_pinfo *ssk6 = inet6_sk(ssk); 1298cf7da0d6SPeter Krystad struct ipv6_pinfo *msk6 = inet6_sk(msk); 1299cf7da0d6SPeter Krystad 1300cf7da0d6SPeter Krystad msk->sk_v6_daddr = ssk->sk_v6_daddr; 1301cf7da0d6SPeter Krystad msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr; 1302cf7da0d6SPeter Krystad 1303cf7da0d6SPeter Krystad if (msk6 && ssk6) { 1304cf7da0d6SPeter Krystad msk6->saddr = ssk6->saddr; 1305cf7da0d6SPeter Krystad msk6->flow_label = ssk6->flow_label; 1306cf7da0d6SPeter Krystad } 1307cf7da0d6SPeter Krystad #endif 1308cf7da0d6SPeter Krystad 1309cf7da0d6SPeter Krystad inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num; 1310cf7da0d6SPeter Krystad inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport; 1311cf7da0d6SPeter Krystad inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport; 1312cf7da0d6SPeter Krystad inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr; 1313cf7da0d6SPeter Krystad inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr; 1314cf7da0d6SPeter Krystad inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr; 1315cf7da0d6SPeter Krystad } 1316cf7da0d6SPeter Krystad 131718b683bfSPaolo Abeni static int mptcp_disconnect(struct sock *sk, int flags) 131818b683bfSPaolo Abeni { 131942c556feSFlorian Westphal /* Should never be called. 132042c556feSFlorian Westphal * inet_stream_connect() calls ->disconnect, but that 132142c556feSFlorian Westphal * refers to the subflow socket, not the mptcp one. 132242c556feSFlorian Westphal */ 132342c556feSFlorian Westphal WARN_ON_ONCE(1); 132442c556feSFlorian Westphal return 0; 132518b683bfSPaolo Abeni } 132618b683bfSPaolo Abeni 1327b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1328b0519de8SFlorian Westphal static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk) 1329b0519de8SFlorian Westphal { 1330b0519de8SFlorian Westphal unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo); 1331b0519de8SFlorian Westphal 1332b0519de8SFlorian Westphal return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 1333b0519de8SFlorian Westphal } 1334b0519de8SFlorian Westphal #endif 1335b0519de8SFlorian Westphal 1336fca5c82cSPaolo Abeni struct sock *mptcp_sk_clone(const struct sock *sk, 1337cfde141eSPaolo Abeni const struct mptcp_options_received *mp_opt, 1338fca5c82cSPaolo Abeni struct request_sock *req) 1339b0519de8SFlorian Westphal { 134058b09919SPaolo Abeni struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 1341b0519de8SFlorian Westphal struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC); 134258b09919SPaolo Abeni struct mptcp_sock *msk; 134358b09919SPaolo Abeni u64 ack_seq; 1344b0519de8SFlorian Westphal 1345b0519de8SFlorian Westphal if (!nsk) 1346b0519de8SFlorian Westphal return NULL; 1347b0519de8SFlorian Westphal 1348b0519de8SFlorian Westphal #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1349b0519de8SFlorian Westphal if (nsk->sk_family == AF_INET6) 1350b0519de8SFlorian Westphal inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk); 1351b0519de8SFlorian Westphal #endif 1352b0519de8SFlorian Westphal 135358b09919SPaolo Abeni __mptcp_init_sock(nsk); 135458b09919SPaolo Abeni 135558b09919SPaolo Abeni msk = mptcp_sk(nsk); 135658b09919SPaolo Abeni msk->local_key = subflow_req->local_key; 135758b09919SPaolo Abeni msk->token = subflow_req->token; 135858b09919SPaolo Abeni msk->subflow = NULL; 135958b09919SPaolo Abeni 136058b09919SPaolo Abeni if (unlikely(mptcp_token_new_accept(subflow_req->token, nsk))) { 13619f5ca6a5SFlorian Westphal nsk->sk_state = TCP_CLOSE; 136258b09919SPaolo Abeni bh_unlock_sock(nsk); 136358b09919SPaolo Abeni 136458b09919SPaolo Abeni /* we can't call into mptcp_close() here - possible BH context 13659f5ca6a5SFlorian Westphal * free the sock directly. 13669f5ca6a5SFlorian Westphal * sk_clone_lock() sets nsk refcnt to two, hence call sk_free() 13679f5ca6a5SFlorian Westphal * too. 136858b09919SPaolo Abeni */ 13699f5ca6a5SFlorian Westphal sk_common_release(nsk); 137058b09919SPaolo Abeni sk_free(nsk); 137158b09919SPaolo Abeni return NULL; 137258b09919SPaolo Abeni } 137358b09919SPaolo Abeni 137458b09919SPaolo Abeni msk->write_seq = subflow_req->idsn + 1; 1375cc9d2566SPaolo Abeni atomic64_set(&msk->snd_una, msk->write_seq); 1376cfde141eSPaolo Abeni if (mp_opt->mp_capable) { 137758b09919SPaolo Abeni msk->can_ack = true; 1378cfde141eSPaolo Abeni msk->remote_key = mp_opt->sndr_key; 137958b09919SPaolo Abeni mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq); 138058b09919SPaolo Abeni ack_seq++; 138158b09919SPaolo Abeni msk->ack_seq = ack_seq; 138258b09919SPaolo Abeni } 13837f20d5fcSPaolo Abeni 13845e20087dSFlorian Westphal sock_reset_flag(nsk, SOCK_RCU_FREE); 13857f20d5fcSPaolo Abeni /* will be fully established after successful MPC subflow creation */ 13867f20d5fcSPaolo Abeni inet_sk_state_store(nsk, TCP_SYN_RECV); 138758b09919SPaolo Abeni bh_unlock_sock(nsk); 138858b09919SPaolo Abeni 138958b09919SPaolo Abeni /* keep a single reference */ 139058b09919SPaolo Abeni __sock_put(nsk); 1391b0519de8SFlorian Westphal return nsk; 1392b0519de8SFlorian Westphal } 1393b0519de8SFlorian Westphal 1394cf7da0d6SPeter Krystad static struct sock *mptcp_accept(struct sock *sk, int flags, int *err, 1395cf7da0d6SPeter Krystad bool kern) 1396cf7da0d6SPeter Krystad { 1397cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1398cf7da0d6SPeter Krystad struct socket *listener; 1399cf7da0d6SPeter Krystad struct sock *newsk; 1400cf7da0d6SPeter Krystad 1401cf7da0d6SPeter Krystad listener = __mptcp_nmpc_socket(msk); 1402cf7da0d6SPeter Krystad if (WARN_ON_ONCE(!listener)) { 1403cf7da0d6SPeter Krystad *err = -EINVAL; 1404cf7da0d6SPeter Krystad return NULL; 1405cf7da0d6SPeter Krystad } 1406cf7da0d6SPeter Krystad 1407cf7da0d6SPeter Krystad pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk)); 1408cf7da0d6SPeter Krystad newsk = inet_csk_accept(listener->sk, flags, err, kern); 1409cf7da0d6SPeter Krystad if (!newsk) 1410cf7da0d6SPeter Krystad return NULL; 1411cf7da0d6SPeter Krystad 1412cf7da0d6SPeter Krystad pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk)); 1413cf7da0d6SPeter Krystad 1414cf7da0d6SPeter Krystad if (sk_is_mptcp(newsk)) { 1415cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 1416cf7da0d6SPeter Krystad struct sock *new_mptcp_sock; 1417cf7da0d6SPeter Krystad struct sock *ssk = newsk; 1418cf7da0d6SPeter Krystad 1419cf7da0d6SPeter Krystad subflow = mptcp_subflow_ctx(newsk); 142058b09919SPaolo Abeni new_mptcp_sock = subflow->conn; 142158b09919SPaolo Abeni 142258b09919SPaolo Abeni /* is_mptcp should be false if subflow->conn is missing, see 142358b09919SPaolo Abeni * subflow_syn_recv_sock() 142458b09919SPaolo Abeni */ 142558b09919SPaolo Abeni if (WARN_ON_ONCE(!new_mptcp_sock)) { 142658b09919SPaolo Abeni tcp_sk(newsk)->is_mptcp = 0; 142758b09919SPaolo Abeni return newsk; 142858b09919SPaolo Abeni } 142958b09919SPaolo Abeni 143058b09919SPaolo Abeni /* acquire the 2nd reference for the owning socket */ 143158b09919SPaolo Abeni sock_hold(new_mptcp_sock); 1432cf7da0d6SPeter Krystad 1433cf7da0d6SPeter Krystad local_bh_disable(); 143458b09919SPaolo Abeni bh_lock_sock(new_mptcp_sock); 1435cf7da0d6SPeter Krystad msk = mptcp_sk(new_mptcp_sock); 14368ab183deSPaolo Abeni msk->first = newsk; 1437cf7da0d6SPeter Krystad 1438cf7da0d6SPeter Krystad newsk = new_mptcp_sock; 1439cf7da0d6SPeter Krystad mptcp_copy_inaddrs(newsk, ssk); 1440cf7da0d6SPeter Krystad list_add(&subflow->node, &msk->conn_list); 1441df1036daSFlorian Westphal inet_sk_state_store(newsk, TCP_ESTABLISHED); 1442cf7da0d6SPeter Krystad 1443cf7da0d6SPeter Krystad bh_unlock_sock(new_mptcp_sock); 1444fc518953SFlorian Westphal 1445fc518953SFlorian Westphal __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); 1446cf7da0d6SPeter Krystad local_bh_enable(); 1447fc518953SFlorian Westphal } else { 1448fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 1449fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK); 1450cf7da0d6SPeter Krystad } 1451cf7da0d6SPeter Krystad 1452cf7da0d6SPeter Krystad return newsk; 1453cf7da0d6SPeter Krystad } 1454cf7da0d6SPeter Krystad 145579c0949eSPeter Krystad static void mptcp_destroy(struct sock *sk) 145679c0949eSPeter Krystad { 1457c9fd9c5fSFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 1458c9fd9c5fSFlorian Westphal 1459c9fd9c5fSFlorian Westphal if (msk->cached_ext) 1460c9fd9c5fSFlorian Westphal __skb_ext_put(msk->cached_ext); 1461d027236cSPaolo Abeni 1462d027236cSPaolo Abeni sk_sockets_allocated_dec(sk); 146379c0949eSPeter Krystad } 146479c0949eSPeter Krystad 1465717e79c8SPeter Krystad static int mptcp_setsockopt(struct sock *sk, int level, int optname, 146650e741bbSFlorian Westphal char __user *optval, unsigned int optlen) 1467717e79c8SPeter Krystad { 1468717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1469717e79c8SPeter Krystad struct socket *ssock; 1470717e79c8SPeter Krystad 1471717e79c8SPeter Krystad pr_debug("msk=%p", msk); 1472717e79c8SPeter Krystad 1473717e79c8SPeter Krystad /* @@ the meaning of setsockopt() when the socket is connected and 1474b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 1475b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 1476b6e4a1aeSMat Martineau * is in TCP fallback, when TCP socket options are passed through 1477b6e4a1aeSMat Martineau * to the one remaining subflow. 1478717e79c8SPeter Krystad */ 1479717e79c8SPeter Krystad lock_sock(sk); 1480b6e4a1aeSMat Martineau ssock = __mptcp_tcp_fallback(msk); 1481e154659bSFlorian Westphal release_sock(sk); 1482b6e4a1aeSMat Martineau if (ssock) 1483b6e4a1aeSMat Martineau return tcp_setsockopt(ssock->sk, level, optname, optval, 1484b6e4a1aeSMat Martineau optlen); 148550e741bbSFlorian Westphal 1486b6e4a1aeSMat Martineau return -EOPNOTSUPP; 1487717e79c8SPeter Krystad } 1488717e79c8SPeter Krystad 1489717e79c8SPeter Krystad static int mptcp_getsockopt(struct sock *sk, int level, int optname, 149050e741bbSFlorian Westphal char __user *optval, int __user *option) 1491717e79c8SPeter Krystad { 1492717e79c8SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sk); 1493717e79c8SPeter Krystad struct socket *ssock; 1494717e79c8SPeter Krystad 1495717e79c8SPeter Krystad pr_debug("msk=%p", msk); 1496717e79c8SPeter Krystad 1497b6e4a1aeSMat Martineau /* @@ the meaning of setsockopt() when the socket is connected and 1498b6e4a1aeSMat Martineau * there are multiple subflows is not yet defined. It is up to the 1499b6e4a1aeSMat Martineau * MPTCP-level socket to configure the subflows until the subflow 1500b6e4a1aeSMat Martineau * is in TCP fallback, when socket options are passed through 1501b6e4a1aeSMat Martineau * to the one remaining subflow. 1502717e79c8SPeter Krystad */ 1503717e79c8SPeter Krystad lock_sock(sk); 1504b6e4a1aeSMat Martineau ssock = __mptcp_tcp_fallback(msk); 1505e154659bSFlorian Westphal release_sock(sk); 1506b6e4a1aeSMat Martineau if (ssock) 1507b6e4a1aeSMat Martineau return tcp_getsockopt(ssock->sk, level, optname, optval, 1508b6e4a1aeSMat Martineau option); 150950e741bbSFlorian Westphal 1510b6e4a1aeSMat Martineau return -EOPNOTSUPP; 1511717e79c8SPeter Krystad } 1512717e79c8SPeter Krystad 1513b51f9b80SPaolo Abeni #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \ 1514b51f9b80SPaolo Abeni TCPF_WRITE_TIMER_DEFERRED) 151514c441b5SPaolo Abeni 151614c441b5SPaolo Abeni /* this is very alike tcp_release_cb() but we must handle differently a 151714c441b5SPaolo Abeni * different set of events 151814c441b5SPaolo Abeni */ 151914c441b5SPaolo Abeni static void mptcp_release_cb(struct sock *sk) 152014c441b5SPaolo Abeni { 152114c441b5SPaolo Abeni unsigned long flags, nflags; 152214c441b5SPaolo Abeni 152314c441b5SPaolo Abeni do { 152414c441b5SPaolo Abeni flags = sk->sk_tsq_flags; 152514c441b5SPaolo Abeni if (!(flags & MPTCP_DEFERRED_ALL)) 152614c441b5SPaolo Abeni return; 152714c441b5SPaolo Abeni nflags = flags & ~MPTCP_DEFERRED_ALL; 152814c441b5SPaolo Abeni } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); 152914c441b5SPaolo Abeni 1530b51f9b80SPaolo Abeni sock_release_ownership(sk); 1531b51f9b80SPaolo Abeni 153214c441b5SPaolo Abeni if (flags & TCPF_DELACK_TIMER_DEFERRED) { 153314c441b5SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(sk); 153414c441b5SPaolo Abeni struct sock *ssk; 153514c441b5SPaolo Abeni 153614c441b5SPaolo Abeni ssk = mptcp_subflow_recv_lookup(msk); 153714c441b5SPaolo Abeni if (!ssk || !schedule_work(&msk->work)) 153814c441b5SPaolo Abeni __sock_put(sk); 153914c441b5SPaolo Abeni } 1540b51f9b80SPaolo Abeni 1541b51f9b80SPaolo Abeni if (flags & TCPF_WRITE_TIMER_DEFERRED) { 1542b51f9b80SPaolo Abeni mptcp_retransmit_handler(sk); 1543b51f9b80SPaolo Abeni __sock_put(sk); 1544b51f9b80SPaolo Abeni } 154514c441b5SPaolo Abeni } 154614c441b5SPaolo Abeni 1547cec37a6eSPeter Krystad static int mptcp_get_port(struct sock *sk, unsigned short snum) 1548f870fa0bSMat Martineau { 1549f870fa0bSMat Martineau struct mptcp_sock *msk = mptcp_sk(sk); 1550cec37a6eSPeter Krystad struct socket *ssock; 1551f870fa0bSMat Martineau 1552cec37a6eSPeter Krystad ssock = __mptcp_nmpc_socket(msk); 1553cec37a6eSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, ssock); 1554cec37a6eSPeter Krystad if (WARN_ON_ONCE(!ssock)) 1555cec37a6eSPeter Krystad return -EINVAL; 1556f870fa0bSMat Martineau 1557cec37a6eSPeter Krystad return inet_csk_get_port(ssock->sk, snum); 1558cec37a6eSPeter Krystad } 1559f870fa0bSMat Martineau 1560cec37a6eSPeter Krystad void mptcp_finish_connect(struct sock *ssk) 1561cec37a6eSPeter Krystad { 1562cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow; 1563cec37a6eSPeter Krystad struct mptcp_sock *msk; 1564cec37a6eSPeter Krystad struct sock *sk; 15656d0060f6SMat Martineau u64 ack_seq; 1566f870fa0bSMat Martineau 1567cec37a6eSPeter Krystad subflow = mptcp_subflow_ctx(ssk); 1568cec37a6eSPeter Krystad sk = subflow->conn; 1569cec37a6eSPeter Krystad msk = mptcp_sk(sk); 1570cec37a6eSPeter Krystad 1571fc518953SFlorian Westphal if (!subflow->mp_capable) { 1572fc518953SFlorian Westphal MPTCP_INC_STATS(sock_net(sk), 1573fc518953SFlorian Westphal MPTCP_MIB_MPCAPABLEACTIVEFALLBACK); 1574fc518953SFlorian Westphal return; 1575fc518953SFlorian Westphal } 1576fc518953SFlorian Westphal 1577648ef4b8SMat Martineau pr_debug("msk=%p, token=%u", sk, subflow->token); 1578648ef4b8SMat Martineau 15796d0060f6SMat Martineau mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq); 15806d0060f6SMat Martineau ack_seq++; 1581648ef4b8SMat Martineau subflow->map_seq = ack_seq; 1582648ef4b8SMat Martineau subflow->map_subflow_seq = 1; 15836d0060f6SMat Martineau subflow->rel_write_seq = 1; 15846d0060f6SMat Martineau 1585cec37a6eSPeter Krystad /* the socket is not connected yet, no msk/subflow ops can access/race 1586cec37a6eSPeter Krystad * accessing the field below 1587cec37a6eSPeter Krystad */ 1588cec37a6eSPeter Krystad WRITE_ONCE(msk->remote_key, subflow->remote_key); 1589cec37a6eSPeter Krystad WRITE_ONCE(msk->local_key, subflow->local_key); 159079c0949eSPeter Krystad WRITE_ONCE(msk->token, subflow->token); 15916d0060f6SMat Martineau WRITE_ONCE(msk->write_seq, subflow->idsn + 1); 15926d0060f6SMat Martineau WRITE_ONCE(msk->ack_seq, ack_seq); 1593d22f4988SChristoph Paasch WRITE_ONCE(msk->can_ack, 1); 1594cc9d2566SPaolo Abeni atomic64_set(&msk->snd_una, msk->write_seq); 15951b1c7a0eSPeter Krystad 15961b1c7a0eSPeter Krystad mptcp_pm_new_connection(msk, 0); 1597f870fa0bSMat Martineau } 1598f870fa0bSMat Martineau 1599cf7da0d6SPeter Krystad static void mptcp_sock_graft(struct sock *sk, struct socket *parent) 1600cf7da0d6SPeter Krystad { 1601cf7da0d6SPeter Krystad write_lock_bh(&sk->sk_callback_lock); 1602cf7da0d6SPeter Krystad rcu_assign_pointer(sk->sk_wq, &parent->wq); 1603cf7da0d6SPeter Krystad sk_set_socket(sk, parent); 1604cf7da0d6SPeter Krystad sk->sk_uid = SOCK_INODE(parent)->i_uid; 1605cf7da0d6SPeter Krystad write_unlock_bh(&sk->sk_callback_lock); 1606cf7da0d6SPeter Krystad } 1607cf7da0d6SPeter Krystad 1608f296234cSPeter Krystad bool mptcp_finish_join(struct sock *sk) 1609f296234cSPeter Krystad { 1610f296234cSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1611f296234cSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 1612f296234cSPeter Krystad struct sock *parent = (void *)msk; 1613f296234cSPeter Krystad struct socket *parent_sock; 1614ec3edaa7SPeter Krystad bool ret; 1615f296234cSPeter Krystad 1616f296234cSPeter Krystad pr_debug("msk=%p, subflow=%p", msk, subflow); 1617f296234cSPeter Krystad 1618f296234cSPeter Krystad /* mptcp socket already closing? */ 1619f296234cSPeter Krystad if (inet_sk_state_load(parent) != TCP_ESTABLISHED) 1620f296234cSPeter Krystad return false; 1621f296234cSPeter Krystad 1622f296234cSPeter Krystad if (!msk->pm.server_side) 1623f296234cSPeter Krystad return true; 1624f296234cSPeter Krystad 1625f296234cSPeter Krystad /* passive connection, attach to msk socket */ 1626f296234cSPeter Krystad parent_sock = READ_ONCE(parent->sk_socket); 1627f296234cSPeter Krystad if (parent_sock && !sk->sk_socket) 1628f296234cSPeter Krystad mptcp_sock_graft(sk, parent_sock); 1629f296234cSPeter Krystad 1630ec3edaa7SPeter Krystad ret = mptcp_pm_allow_new_subflow(msk); 1631ec3edaa7SPeter Krystad if (ret) { 1632*64d950aeSChristoph Paasch subflow->map_seq = msk->ack_seq; 1633*64d950aeSChristoph Paasch 1634ec3edaa7SPeter Krystad /* active connections are already on conn_list */ 1635ec3edaa7SPeter Krystad spin_lock_bh(&msk->join_list_lock); 1636ec3edaa7SPeter Krystad if (!WARN_ON_ONCE(!list_empty(&subflow->node))) 1637ec3edaa7SPeter Krystad list_add_tail(&subflow->node, &msk->join_list); 1638ec3edaa7SPeter Krystad spin_unlock_bh(&msk->join_list_lock); 1639ec3edaa7SPeter Krystad } 1640ec3edaa7SPeter Krystad return ret; 1641f296234cSPeter Krystad } 1642f296234cSPeter Krystad 1643f296234cSPeter Krystad bool mptcp_sk_is_subflow(const struct sock *sk) 1644f296234cSPeter Krystad { 1645f296234cSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 1646f296234cSPeter Krystad 1647f296234cSPeter Krystad return subflow->mp_join == 1; 1648f296234cSPeter Krystad } 1649f296234cSPeter Krystad 16501891c4a0SFlorian Westphal static bool mptcp_memory_free(const struct sock *sk, int wake) 16511891c4a0SFlorian Westphal { 16521891c4a0SFlorian Westphal struct mptcp_sock *msk = mptcp_sk(sk); 16531891c4a0SFlorian Westphal 16541891c4a0SFlorian Westphal return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true; 16551891c4a0SFlorian Westphal } 16561891c4a0SFlorian Westphal 1657f870fa0bSMat Martineau static struct proto mptcp_prot = { 1658f870fa0bSMat Martineau .name = "MPTCP", 1659f870fa0bSMat Martineau .owner = THIS_MODULE, 1660f870fa0bSMat Martineau .init = mptcp_init_sock, 166118b683bfSPaolo Abeni .disconnect = mptcp_disconnect, 1662f870fa0bSMat Martineau .close = mptcp_close, 1663cf7da0d6SPeter Krystad .accept = mptcp_accept, 1664717e79c8SPeter Krystad .setsockopt = mptcp_setsockopt, 1665717e79c8SPeter Krystad .getsockopt = mptcp_getsockopt, 1666f870fa0bSMat Martineau .shutdown = tcp_shutdown, 166779c0949eSPeter Krystad .destroy = mptcp_destroy, 1668f870fa0bSMat Martineau .sendmsg = mptcp_sendmsg, 1669f870fa0bSMat Martineau .recvmsg = mptcp_recvmsg, 167014c441b5SPaolo Abeni .release_cb = mptcp_release_cb, 1671f870fa0bSMat Martineau .hash = inet_hash, 1672f870fa0bSMat Martineau .unhash = inet_unhash, 1673cec37a6eSPeter Krystad .get_port = mptcp_get_port, 1674d027236cSPaolo Abeni .sockets_allocated = &mptcp_sockets_allocated, 1675d027236cSPaolo Abeni .memory_allocated = &tcp_memory_allocated, 1676d027236cSPaolo Abeni .memory_pressure = &tcp_memory_pressure, 16771891c4a0SFlorian Westphal .stream_memory_free = mptcp_memory_free, 1678d027236cSPaolo Abeni .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 1679d027236cSPaolo Abeni .sysctl_mem = sysctl_tcp_mem, 1680f870fa0bSMat Martineau .obj_size = sizeof(struct mptcp_sock), 1681f870fa0bSMat Martineau .no_autobind = true, 1682f870fa0bSMat Martineau }; 1683f870fa0bSMat Martineau 16842303f994SPeter Krystad static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 16852303f994SPeter Krystad { 16862303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 16872303f994SPeter Krystad struct socket *ssock; 1688cf7da0d6SPeter Krystad int err; 16892303f994SPeter Krystad 16902303f994SPeter Krystad lock_sock(sock->sk); 16912303f994SPeter Krystad ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE); 16922303f994SPeter Krystad if (IS_ERR(ssock)) { 16932303f994SPeter Krystad err = PTR_ERR(ssock); 16942303f994SPeter Krystad goto unlock; 16952303f994SPeter Krystad } 16962303f994SPeter Krystad 16972303f994SPeter Krystad err = ssock->ops->bind(ssock, uaddr, addr_len); 1698cf7da0d6SPeter Krystad if (!err) 1699cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 17002303f994SPeter Krystad 17012303f994SPeter Krystad unlock: 17022303f994SPeter Krystad release_sock(sock->sk); 17032303f994SPeter Krystad return err; 17042303f994SPeter Krystad } 17052303f994SPeter Krystad 17062303f994SPeter Krystad static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, 17072303f994SPeter Krystad int addr_len, int flags) 17082303f994SPeter Krystad { 17092303f994SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 17102303f994SPeter Krystad struct socket *ssock; 17112303f994SPeter Krystad int err; 17122303f994SPeter Krystad 17132303f994SPeter Krystad lock_sock(sock->sk); 17142303f994SPeter Krystad ssock = __mptcp_socket_create(msk, TCP_SYN_SENT); 17152303f994SPeter Krystad if (IS_ERR(ssock)) { 17162303f994SPeter Krystad err = PTR_ERR(ssock); 17172303f994SPeter Krystad goto unlock; 17182303f994SPeter Krystad } 17192303f994SPeter Krystad 1720cf7da0d6SPeter Krystad #ifdef CONFIG_TCP_MD5SIG 1721cf7da0d6SPeter Krystad /* no MPTCP if MD5SIG is enabled on this socket or we may run out of 1722cf7da0d6SPeter Krystad * TCP option space. 1723cf7da0d6SPeter Krystad */ 1724cf7da0d6SPeter Krystad if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info)) 1725cf7da0d6SPeter Krystad mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0; 1726cf7da0d6SPeter Krystad #endif 1727cf7da0d6SPeter Krystad 17282303f994SPeter Krystad err = ssock->ops->connect(ssock, uaddr, addr_len, flags); 17292303f994SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 1730cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 17312303f994SPeter Krystad 17322303f994SPeter Krystad unlock: 17332303f994SPeter Krystad release_sock(sock->sk); 17342303f994SPeter Krystad return err; 17352303f994SPeter Krystad } 17362303f994SPeter Krystad 1737cf7da0d6SPeter Krystad static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr, 1738cf7da0d6SPeter Krystad int peer) 1739cf7da0d6SPeter Krystad { 1740cf7da0d6SPeter Krystad if (sock->sk->sk_prot == &tcp_prot) { 1741cf7da0d6SPeter Krystad /* we are being invoked from __sys_accept4, after 1742cf7da0d6SPeter Krystad * mptcp_accept() has just accepted a non-mp-capable 1743cf7da0d6SPeter Krystad * flow: sk is a tcp_sk, not an mptcp one. 1744cf7da0d6SPeter Krystad * 1745cf7da0d6SPeter Krystad * Hand the socket over to tcp so all further socket ops 1746cf7da0d6SPeter Krystad * bypass mptcp. 1747cf7da0d6SPeter Krystad */ 1748cf7da0d6SPeter Krystad sock->ops = &inet_stream_ops; 1749cf7da0d6SPeter Krystad } 1750cf7da0d6SPeter Krystad 1751cf7da0d6SPeter Krystad return inet_getname(sock, uaddr, peer); 1752cf7da0d6SPeter Krystad } 1753cf7da0d6SPeter Krystad 1754cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1755cf7da0d6SPeter Krystad static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr, 1756cf7da0d6SPeter Krystad int peer) 1757cf7da0d6SPeter Krystad { 1758cf7da0d6SPeter Krystad if (sock->sk->sk_prot == &tcpv6_prot) { 1759cf7da0d6SPeter Krystad /* we are being invoked from __sys_accept4 after 1760cf7da0d6SPeter Krystad * mptcp_accept() has accepted a non-mp-capable 1761cf7da0d6SPeter Krystad * subflow: sk is a tcp_sk, not mptcp. 1762cf7da0d6SPeter Krystad * 1763cf7da0d6SPeter Krystad * Hand the socket over to tcp so all further 1764cf7da0d6SPeter Krystad * socket ops bypass mptcp. 1765cf7da0d6SPeter Krystad */ 1766cf7da0d6SPeter Krystad sock->ops = &inet6_stream_ops; 1767cf7da0d6SPeter Krystad } 1768cf7da0d6SPeter Krystad 1769cf7da0d6SPeter Krystad return inet6_getname(sock, uaddr, peer); 1770cf7da0d6SPeter Krystad } 1771cf7da0d6SPeter Krystad #endif 1772cf7da0d6SPeter Krystad 1773cf7da0d6SPeter Krystad static int mptcp_listen(struct socket *sock, int backlog) 1774cf7da0d6SPeter Krystad { 1775cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 1776cf7da0d6SPeter Krystad struct socket *ssock; 1777cf7da0d6SPeter Krystad int err; 1778cf7da0d6SPeter Krystad 1779cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 1780cf7da0d6SPeter Krystad 1781cf7da0d6SPeter Krystad lock_sock(sock->sk); 1782cf7da0d6SPeter Krystad ssock = __mptcp_socket_create(msk, TCP_LISTEN); 1783cf7da0d6SPeter Krystad if (IS_ERR(ssock)) { 1784cf7da0d6SPeter Krystad err = PTR_ERR(ssock); 1785cf7da0d6SPeter Krystad goto unlock; 1786cf7da0d6SPeter Krystad } 1787cf7da0d6SPeter Krystad 17885e20087dSFlorian Westphal sock_set_flag(sock->sk, SOCK_RCU_FREE); 17895e20087dSFlorian Westphal 1790cf7da0d6SPeter Krystad err = ssock->ops->listen(ssock, backlog); 1791cf7da0d6SPeter Krystad inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk)); 1792cf7da0d6SPeter Krystad if (!err) 1793cf7da0d6SPeter Krystad mptcp_copy_inaddrs(sock->sk, ssock->sk); 1794cf7da0d6SPeter Krystad 1795cf7da0d6SPeter Krystad unlock: 1796cf7da0d6SPeter Krystad release_sock(sock->sk); 1797cf7da0d6SPeter Krystad return err; 1798cf7da0d6SPeter Krystad } 1799cf7da0d6SPeter Krystad 1800cf7da0d6SPeter Krystad static bool is_tcp_proto(const struct proto *p) 1801cf7da0d6SPeter Krystad { 1802cf7da0d6SPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1803cf7da0d6SPeter Krystad return p == &tcp_prot || p == &tcpv6_prot; 1804cf7da0d6SPeter Krystad #else 1805cf7da0d6SPeter Krystad return p == &tcp_prot; 1806cf7da0d6SPeter Krystad #endif 1807cf7da0d6SPeter Krystad } 1808cf7da0d6SPeter Krystad 1809cf7da0d6SPeter Krystad static int mptcp_stream_accept(struct socket *sock, struct socket *newsock, 1810cf7da0d6SPeter Krystad int flags, bool kern) 1811cf7da0d6SPeter Krystad { 1812cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 1813cf7da0d6SPeter Krystad struct socket *ssock; 1814cf7da0d6SPeter Krystad int err; 1815cf7da0d6SPeter Krystad 1816cf7da0d6SPeter Krystad pr_debug("msk=%p", msk); 1817cf7da0d6SPeter Krystad 1818cf7da0d6SPeter Krystad lock_sock(sock->sk); 1819cf7da0d6SPeter Krystad if (sock->sk->sk_state != TCP_LISTEN) 1820cf7da0d6SPeter Krystad goto unlock_fail; 1821cf7da0d6SPeter Krystad 1822cf7da0d6SPeter Krystad ssock = __mptcp_nmpc_socket(msk); 1823cf7da0d6SPeter Krystad if (!ssock) 1824cf7da0d6SPeter Krystad goto unlock_fail; 1825cf7da0d6SPeter Krystad 1826cf7da0d6SPeter Krystad sock_hold(ssock->sk); 1827cf7da0d6SPeter Krystad release_sock(sock->sk); 1828cf7da0d6SPeter Krystad 1829cf7da0d6SPeter Krystad err = ssock->ops->accept(sock, newsock, flags, kern); 1830cf7da0d6SPeter Krystad if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) { 1831cf7da0d6SPeter Krystad struct mptcp_sock *msk = mptcp_sk(newsock->sk); 1832cf7da0d6SPeter Krystad struct mptcp_subflow_context *subflow; 1833cf7da0d6SPeter Krystad 1834cf7da0d6SPeter Krystad /* set ssk->sk_socket of accept()ed flows to mptcp socket. 1835cf7da0d6SPeter Krystad * This is needed so NOSPACE flag can be set from tcp stack. 1836cf7da0d6SPeter Krystad */ 1837ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 1838cf7da0d6SPeter Krystad list_for_each_entry(subflow, &msk->conn_list, node) { 1839cf7da0d6SPeter Krystad struct sock *ssk = mptcp_subflow_tcp_sock(subflow); 1840cf7da0d6SPeter Krystad 1841cf7da0d6SPeter Krystad if (!ssk->sk_socket) 1842cf7da0d6SPeter Krystad mptcp_sock_graft(ssk, newsock); 1843cf7da0d6SPeter Krystad } 1844cf7da0d6SPeter Krystad } 1845cf7da0d6SPeter Krystad 1846cf7da0d6SPeter Krystad sock_put(ssock->sk); 1847cf7da0d6SPeter Krystad return err; 1848cf7da0d6SPeter Krystad 1849cf7da0d6SPeter Krystad unlock_fail: 1850cf7da0d6SPeter Krystad release_sock(sock->sk); 1851cf7da0d6SPeter Krystad return -EINVAL; 1852cf7da0d6SPeter Krystad } 1853cf7da0d6SPeter Krystad 18542303f994SPeter Krystad static __poll_t mptcp_poll(struct file *file, struct socket *sock, 18552303f994SPeter Krystad struct poll_table_struct *wait) 18562303f994SPeter Krystad { 18571891c4a0SFlorian Westphal struct sock *sk = sock->sk; 18588ab183deSPaolo Abeni struct mptcp_sock *msk; 18591891c4a0SFlorian Westphal struct socket *ssock; 18602303f994SPeter Krystad __poll_t mask = 0; 18612303f994SPeter Krystad 18621891c4a0SFlorian Westphal msk = mptcp_sk(sk); 18631891c4a0SFlorian Westphal lock_sock(sk); 18640b4f33deSFlorian Westphal ssock = __mptcp_tcp_fallback(msk); 18650b4f33deSFlorian Westphal if (!ssock) 18661891c4a0SFlorian Westphal ssock = __mptcp_nmpc_socket(msk); 18671891c4a0SFlorian Westphal if (ssock) { 18681891c4a0SFlorian Westphal mask = ssock->ops->poll(file, ssock, wait); 18691891c4a0SFlorian Westphal release_sock(sk); 18701891c4a0SFlorian Westphal return mask; 18711891c4a0SFlorian Westphal } 18721891c4a0SFlorian Westphal 18731891c4a0SFlorian Westphal release_sock(sk); 18741891c4a0SFlorian Westphal sock_poll_wait(file, sock, wait); 18751891c4a0SFlorian Westphal lock_sock(sk); 18761891c4a0SFlorian Westphal 18771891c4a0SFlorian Westphal if (test_bit(MPTCP_DATA_READY, &msk->flags)) 18781891c4a0SFlorian Westphal mask = EPOLLIN | EPOLLRDNORM; 18791891c4a0SFlorian Westphal if (sk_stream_is_writeable(sk) && 18801891c4a0SFlorian Westphal test_bit(MPTCP_SEND_SPACE, &msk->flags)) 18811891c4a0SFlorian Westphal mask |= EPOLLOUT | EPOLLWRNORM; 18821891c4a0SFlorian Westphal if (sk->sk_shutdown & RCV_SHUTDOWN) 18831891c4a0SFlorian Westphal mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 18841891c4a0SFlorian Westphal 18851891c4a0SFlorian Westphal release_sock(sk); 18861891c4a0SFlorian Westphal 18872303f994SPeter Krystad return mask; 18882303f994SPeter Krystad } 18892303f994SPeter Krystad 189021498490SPeter Krystad static int mptcp_shutdown(struct socket *sock, int how) 189121498490SPeter Krystad { 189221498490SPeter Krystad struct mptcp_sock *msk = mptcp_sk(sock->sk); 189321498490SPeter Krystad struct mptcp_subflow_context *subflow; 18940b4f33deSFlorian Westphal struct socket *ssock; 189521498490SPeter Krystad int ret = 0; 189621498490SPeter Krystad 189721498490SPeter Krystad pr_debug("sk=%p, how=%d", msk, how); 189821498490SPeter Krystad 189921498490SPeter Krystad lock_sock(sock->sk); 19000b4f33deSFlorian Westphal ssock = __mptcp_tcp_fallback(msk); 19010b4f33deSFlorian Westphal if (ssock) { 19020b4f33deSFlorian Westphal release_sock(sock->sk); 19030b4f33deSFlorian Westphal return inet_shutdown(ssock, how); 19040b4f33deSFlorian Westphal } 190521498490SPeter Krystad 190621498490SPeter Krystad if (how == SHUT_WR || how == SHUT_RDWR) 190721498490SPeter Krystad inet_sk_state_store(sock->sk, TCP_FIN_WAIT1); 190821498490SPeter Krystad 190921498490SPeter Krystad how++; 191021498490SPeter Krystad 191121498490SPeter Krystad if ((how & ~SHUTDOWN_MASK) || !how) { 191221498490SPeter Krystad ret = -EINVAL; 191321498490SPeter Krystad goto out_unlock; 191421498490SPeter Krystad } 191521498490SPeter Krystad 191621498490SPeter Krystad if (sock->state == SS_CONNECTING) { 191721498490SPeter Krystad if ((1 << sock->sk->sk_state) & 191821498490SPeter Krystad (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) 191921498490SPeter Krystad sock->state = SS_DISCONNECTING; 192021498490SPeter Krystad else 192121498490SPeter Krystad sock->state = SS_CONNECTED; 192221498490SPeter Krystad } 192321498490SPeter Krystad 1924ec3edaa7SPeter Krystad __mptcp_flush_join_list(msk); 192521498490SPeter Krystad mptcp_for_each_subflow(msk, subflow) { 192621498490SPeter Krystad struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow); 192721498490SPeter Krystad 192876c42a29SMat Martineau mptcp_subflow_shutdown(tcp_sk, how, 1, msk->write_seq); 192921498490SPeter Krystad } 193021498490SPeter Krystad 193121498490SPeter Krystad out_unlock: 193221498490SPeter Krystad release_sock(sock->sk); 193321498490SPeter Krystad 193421498490SPeter Krystad return ret; 193521498490SPeter Krystad } 193621498490SPeter Krystad 1937e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_stream_ops = { 1938e42f1ac6SFlorian Westphal .family = PF_INET, 1939e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 1940e42f1ac6SFlorian Westphal .release = inet_release, 1941e42f1ac6SFlorian Westphal .bind = mptcp_bind, 1942e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 1943e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 1944e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 1945e42f1ac6SFlorian Westphal .getname = mptcp_v4_getname, 1946e42f1ac6SFlorian Westphal .poll = mptcp_poll, 1947e42f1ac6SFlorian Westphal .ioctl = inet_ioctl, 1948e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 1949e42f1ac6SFlorian Westphal .listen = mptcp_listen, 1950e42f1ac6SFlorian Westphal .shutdown = mptcp_shutdown, 1951e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 1952e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 1953e42f1ac6SFlorian Westphal .sendmsg = inet_sendmsg, 1954e42f1ac6SFlorian Westphal .recvmsg = inet_recvmsg, 1955e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 1956e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 1957e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 1958e42f1ac6SFlorian Westphal .compat_setsockopt = compat_sock_common_setsockopt, 1959e42f1ac6SFlorian Westphal .compat_getsockopt = compat_sock_common_getsockopt, 1960e42f1ac6SFlorian Westphal #endif 1961e42f1ac6SFlorian Westphal }; 19622303f994SPeter Krystad 1963f870fa0bSMat Martineau static struct inet_protosw mptcp_protosw = { 1964f870fa0bSMat Martineau .type = SOCK_STREAM, 1965f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 1966f870fa0bSMat Martineau .prot = &mptcp_prot, 19672303f994SPeter Krystad .ops = &mptcp_stream_ops, 19682303f994SPeter Krystad .flags = INET_PROTOSW_ICSK, 1969f870fa0bSMat Martineau }; 1970f870fa0bSMat Martineau 1971784325e9SMatthieu Baerts void mptcp_proto_init(void) 1972f870fa0bSMat Martineau { 19732303f994SPeter Krystad mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo; 19742303f994SPeter Krystad 1975d027236cSPaolo Abeni if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL)) 1976d027236cSPaolo Abeni panic("Failed to allocate MPTCP pcpu counter\n"); 1977d027236cSPaolo Abeni 19782303f994SPeter Krystad mptcp_subflow_init(); 19791b1c7a0eSPeter Krystad mptcp_pm_init(); 19802303f994SPeter Krystad 1981f870fa0bSMat Martineau if (proto_register(&mptcp_prot, 1) != 0) 1982f870fa0bSMat Martineau panic("Failed to register MPTCP proto.\n"); 1983f870fa0bSMat Martineau 1984f870fa0bSMat Martineau inet_register_protosw(&mptcp_protosw); 19856771bfd9SFlorian Westphal 19866771bfd9SFlorian Westphal BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); 1987f870fa0bSMat Martineau } 1988f870fa0bSMat Martineau 1989f870fa0bSMat Martineau #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1990e42f1ac6SFlorian Westphal static const struct proto_ops mptcp_v6_stream_ops = { 1991e42f1ac6SFlorian Westphal .family = PF_INET6, 1992e42f1ac6SFlorian Westphal .owner = THIS_MODULE, 1993e42f1ac6SFlorian Westphal .release = inet6_release, 1994e42f1ac6SFlorian Westphal .bind = mptcp_bind, 1995e42f1ac6SFlorian Westphal .connect = mptcp_stream_connect, 1996e42f1ac6SFlorian Westphal .socketpair = sock_no_socketpair, 1997e42f1ac6SFlorian Westphal .accept = mptcp_stream_accept, 1998e42f1ac6SFlorian Westphal .getname = mptcp_v6_getname, 1999e42f1ac6SFlorian Westphal .poll = mptcp_poll, 2000e42f1ac6SFlorian Westphal .ioctl = inet6_ioctl, 2001e42f1ac6SFlorian Westphal .gettstamp = sock_gettstamp, 2002e42f1ac6SFlorian Westphal .listen = mptcp_listen, 2003e42f1ac6SFlorian Westphal .shutdown = mptcp_shutdown, 2004e42f1ac6SFlorian Westphal .setsockopt = sock_common_setsockopt, 2005e42f1ac6SFlorian Westphal .getsockopt = sock_common_getsockopt, 2006e42f1ac6SFlorian Westphal .sendmsg = inet6_sendmsg, 2007e42f1ac6SFlorian Westphal .recvmsg = inet6_recvmsg, 2008e42f1ac6SFlorian Westphal .mmap = sock_no_mmap, 2009e42f1ac6SFlorian Westphal .sendpage = inet_sendpage, 2010e42f1ac6SFlorian Westphal #ifdef CONFIG_COMPAT 2011e42f1ac6SFlorian Westphal .compat_setsockopt = compat_sock_common_setsockopt, 2012e42f1ac6SFlorian Westphal .compat_getsockopt = compat_sock_common_getsockopt, 2013e42f1ac6SFlorian Westphal #endif 2014e42f1ac6SFlorian Westphal }; 2015e42f1ac6SFlorian Westphal 2016f870fa0bSMat Martineau static struct proto mptcp_v6_prot; 2017f870fa0bSMat Martineau 201879c0949eSPeter Krystad static void mptcp_v6_destroy(struct sock *sk) 201979c0949eSPeter Krystad { 202079c0949eSPeter Krystad mptcp_destroy(sk); 202179c0949eSPeter Krystad inet6_destroy_sock(sk); 202279c0949eSPeter Krystad } 202379c0949eSPeter Krystad 2024f870fa0bSMat Martineau static struct inet_protosw mptcp_v6_protosw = { 2025f870fa0bSMat Martineau .type = SOCK_STREAM, 2026f870fa0bSMat Martineau .protocol = IPPROTO_MPTCP, 2027f870fa0bSMat Martineau .prot = &mptcp_v6_prot, 20282303f994SPeter Krystad .ops = &mptcp_v6_stream_ops, 2029f870fa0bSMat Martineau .flags = INET_PROTOSW_ICSK, 2030f870fa0bSMat Martineau }; 2031f870fa0bSMat Martineau 2032784325e9SMatthieu Baerts int mptcp_proto_v6_init(void) 2033f870fa0bSMat Martineau { 2034f870fa0bSMat Martineau int err; 2035f870fa0bSMat Martineau 2036f870fa0bSMat Martineau mptcp_v6_prot = mptcp_prot; 2037f870fa0bSMat Martineau strcpy(mptcp_v6_prot.name, "MPTCPv6"); 2038f870fa0bSMat Martineau mptcp_v6_prot.slab = NULL; 203979c0949eSPeter Krystad mptcp_v6_prot.destroy = mptcp_v6_destroy; 2040b0519de8SFlorian Westphal mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); 2041f870fa0bSMat Martineau 2042f870fa0bSMat Martineau err = proto_register(&mptcp_v6_prot, 1); 2043f870fa0bSMat Martineau if (err) 2044f870fa0bSMat Martineau return err; 2045f870fa0bSMat Martineau 2046f870fa0bSMat Martineau err = inet6_register_protosw(&mptcp_v6_protosw); 2047f870fa0bSMat Martineau if (err) 2048f870fa0bSMat Martineau proto_unregister(&mptcp_v6_prot); 2049f870fa0bSMat Martineau 2050f870fa0bSMat Martineau return err; 2051f870fa0bSMat Martineau } 2052f870fa0bSMat Martineau #endif 2053