1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2017 - 2019, Intel Corporation.
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/sched/signal.h>
13 #include <linux/atomic.h>
15 #include <net/inet_common.h>
16 #include <net/inet_hashtables.h>
17 #include <net/protocol.h>
19 #include <net/tcp_states.h>
20 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21 #include <net/transp_v6.h>
23 #include <net/mptcp.h>
25 #include <asm/ioctls.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/mptcp.h>
32 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
34 struct mptcp_sock msk;
40 MPTCP_CMSG_TS = BIT(0),
41 MPTCP_CMSG_INQ = BIT(1),
44 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
46 static void __mptcp_destroy_sock(struct sock *sk);
47 static void mptcp_check_send_data_fin(struct sock *sk);
49 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
50 static struct net_device mptcp_napi_dev;
52 /* Returns end sequence number of the receiver's advertised window */
53 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
55 return READ_ONCE(msk->wnd_end);
58 static bool mptcp_is_tcpsk(struct sock *sk)
60 struct socket *sock = sk->sk_socket;
62 if (unlikely(sk->sk_prot == &tcp_prot)) {
63 /* we are being invoked after mptcp_accept() has
64 * accepted a non-mp-capable flow: sk is a tcp_sk,
67 * Hand the socket over to tcp so all further socket ops
70 WRITE_ONCE(sock->ops, &inet_stream_ops);
72 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
73 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
74 WRITE_ONCE(sock->ops, &inet6_stream_ops);
82 static int __mptcp_socket_create(struct mptcp_sock *msk)
84 struct mptcp_subflow_context *subflow;
85 struct sock *sk = (struct sock *)msk;
89 err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
93 msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio;
94 WRITE_ONCE(msk->first, ssock->sk);
95 subflow = mptcp_subflow_ctx(ssock->sk);
96 list_add(&subflow->node, &msk->conn_list);
98 subflow->request_mptcp = 1;
99 subflow->subflow_id = msk->subflow_id++;
101 /* This is the first subflow, always with id 0 */
102 subflow->local_id_valid = 1;
103 mptcp_sock_graft(msk->first, sk->sk_socket);
104 iput(SOCK_INODE(ssock));
109 /* If the MPC handshake is not started, returns the first subflow,
110 * eventually allocating it.
112 struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk)
114 struct sock *sk = (struct sock *)msk;
117 if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
118 return ERR_PTR(-EINVAL);
121 ret = __mptcp_socket_create(msk);
125 mptcp_sockopt_sync(msk, msk->first);
131 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
133 sk_drops_add(sk, skb);
137 static void mptcp_rmem_fwd_alloc_add(struct sock *sk, int size)
139 WRITE_ONCE(mptcp_sk(sk)->rmem_fwd_alloc,
140 mptcp_sk(sk)->rmem_fwd_alloc + size);
143 static void mptcp_rmem_charge(struct sock *sk, int size)
145 mptcp_rmem_fwd_alloc_add(sk, -size);
148 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
149 struct sk_buff *from)
154 if (MPTCP_SKB_CB(from)->offset ||
155 !skb_try_coalesce(to, from, &fragstolen, &delta))
158 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
159 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
160 to->len, MPTCP_SKB_CB(from)->end_seq);
161 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
163 /* note the fwd memory can reach a negative value after accounting
164 * for the delta, but the later skb free will restore a non
167 atomic_add(delta, &sk->sk_rmem_alloc);
168 mptcp_rmem_charge(sk, delta);
169 kfree_skb_partial(from, fragstolen);
174 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
175 struct sk_buff *from)
177 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
180 return mptcp_try_coalesce((struct sock *)msk, to, from);
183 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
185 amount >>= PAGE_SHIFT;
186 mptcp_rmem_charge(sk, amount << PAGE_SHIFT);
187 __sk_mem_reduce_allocated(sk, amount);
190 static void mptcp_rmem_uncharge(struct sock *sk, int size)
192 struct mptcp_sock *msk = mptcp_sk(sk);
195 mptcp_rmem_fwd_alloc_add(sk, size);
196 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
198 /* see sk_mem_uncharge() for the rationale behind the following schema */
199 if (unlikely(reclaimable >= PAGE_SIZE))
200 __mptcp_rmem_reclaim(sk, reclaimable);
203 static void mptcp_rfree(struct sk_buff *skb)
205 unsigned int len = skb->truesize;
206 struct sock *sk = skb->sk;
208 atomic_sub(len, &sk->sk_rmem_alloc);
209 mptcp_rmem_uncharge(sk, len);
212 void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
216 skb->destructor = mptcp_rfree;
217 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
218 mptcp_rmem_charge(sk, skb->truesize);
221 /* "inspired" by tcp_data_queue_ofo(), main differences:
223 * - don't cope with sacks
225 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
227 struct sock *sk = (struct sock *)msk;
228 struct rb_node **p, *parent;
229 u64 seq, end_seq, max_seq;
230 struct sk_buff *skb1;
232 seq = MPTCP_SKB_CB(skb)->map_seq;
233 end_seq = MPTCP_SKB_CB(skb)->end_seq;
234 max_seq = atomic64_read(&msk->rcv_wnd_sent);
236 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
237 RB_EMPTY_ROOT(&msk->out_of_order_queue));
238 if (after64(end_seq, max_seq)) {
241 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
242 (unsigned long long)end_seq - (unsigned long)max_seq,
243 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
244 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
248 p = &msk->out_of_order_queue.rb_node;
249 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
250 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
251 rb_link_node(&skb->rbnode, NULL, p);
252 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
253 msk->ooo_last_skb = skb;
257 /* with 2 subflows, adding at end of ooo queue is quite likely
258 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
260 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
261 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
262 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
266 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
267 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
268 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
269 parent = &msk->ooo_last_skb->rbnode;
270 p = &parent->rb_right;
274 /* Find place to insert this segment. Handle overlaps on the way. */
278 skb1 = rb_to_skb(parent);
279 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
280 p = &parent->rb_left;
283 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
284 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
285 /* All the bits are present. Drop. */
287 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
290 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
294 * continue traversing
297 /* skb's seq == skb1's seq and skb covers skb1.
298 * Replace skb1 with skb.
300 rb_replace_node(&skb1->rbnode, &skb->rbnode,
301 &msk->out_of_order_queue);
302 mptcp_drop(sk, skb1);
303 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
306 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
307 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
310 p = &parent->rb_right;
314 /* Insert segment into RB tree. */
315 rb_link_node(&skb->rbnode, parent, p);
316 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
319 /* Remove other segments covered by skb. */
320 while ((skb1 = skb_rb_next(skb)) != NULL) {
321 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
323 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
324 mptcp_drop(sk, skb1);
325 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
327 /* If there is no skb after us, we are the last_skb ! */
329 msk->ooo_last_skb = skb;
333 mptcp_set_owner_r(skb, sk);
336 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
338 struct mptcp_sock *msk = mptcp_sk(sk);
341 if (size <= msk->rmem_fwd_alloc)
344 size -= msk->rmem_fwd_alloc;
345 amt = sk_mem_pages(size);
346 amount = amt << PAGE_SHIFT;
347 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
350 mptcp_rmem_fwd_alloc_add(sk, amount);
354 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
355 struct sk_buff *skb, unsigned int offset,
358 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
359 struct sock *sk = (struct sock *)msk;
360 struct sk_buff *tail;
363 __skb_unlink(skb, &ssk->sk_receive_queue);
368 /* try to fetch required memory from subflow */
369 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
372 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
374 /* the skb map_seq accounts for the skb offset:
375 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
378 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
379 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
380 MPTCP_SKB_CB(skb)->offset = offset;
381 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
383 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
385 msk->bytes_received += copy_len;
386 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
387 tail = skb_peek_tail(&sk->sk_receive_queue);
388 if (tail && mptcp_try_coalesce(sk, tail, skb))
391 mptcp_set_owner_r(skb, sk);
392 __skb_queue_tail(&sk->sk_receive_queue, skb);
394 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
395 mptcp_data_queue_ofo(msk, skb);
399 /* old data, keep it simple and drop the whole pkt, sender
400 * will retransmit as needed, if needed.
402 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
408 static void mptcp_stop_timer(struct sock *sk)
410 struct inet_connection_sock *icsk = inet_csk(sk);
412 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
413 mptcp_sk(sk)->timer_ival = 0;
416 static void mptcp_close_wake_up(struct sock *sk)
418 if (sock_flag(sk, SOCK_DEAD))
421 sk->sk_state_change(sk);
422 if (sk->sk_shutdown == SHUTDOWN_MASK ||
423 sk->sk_state == TCP_CLOSE)
424 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
426 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
429 static bool mptcp_pending_data_fin_ack(struct sock *sk)
431 struct mptcp_sock *msk = mptcp_sk(sk);
433 return ((1 << sk->sk_state) &
434 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
435 msk->write_seq == READ_ONCE(msk->snd_una);
438 static void mptcp_check_data_fin_ack(struct sock *sk)
440 struct mptcp_sock *msk = mptcp_sk(sk);
442 /* Look for an acknowledged DATA_FIN */
443 if (mptcp_pending_data_fin_ack(sk)) {
444 WRITE_ONCE(msk->snd_data_fin_enable, 0);
446 switch (sk->sk_state) {
448 inet_sk_state_store(sk, TCP_FIN_WAIT2);
452 inet_sk_state_store(sk, TCP_CLOSE);
456 mptcp_close_wake_up(sk);
460 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
462 struct mptcp_sock *msk = mptcp_sk(sk);
464 if (READ_ONCE(msk->rcv_data_fin) &&
465 ((1 << sk->sk_state) &
466 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
467 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
469 if (msk->ack_seq == rcv_data_fin_seq) {
471 *seq = rcv_data_fin_seq;
480 static void mptcp_set_datafin_timeout(struct sock *sk)
482 struct inet_connection_sock *icsk = inet_csk(sk);
485 retransmits = min_t(u32, icsk->icsk_retransmits,
486 ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
488 mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
491 static void __mptcp_set_timeout(struct sock *sk, long tout)
493 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
496 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
498 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
500 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
501 inet_csk(ssk)->icsk_timeout - jiffies : 0;
504 static void mptcp_set_timeout(struct sock *sk)
506 struct mptcp_subflow_context *subflow;
509 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
510 tout = max(tout, mptcp_timeout_from_subflow(subflow));
511 __mptcp_set_timeout(sk, tout);
514 static inline bool tcp_can_send_ack(const struct sock *ssk)
516 return !((1 << inet_sk_state_load(ssk)) &
517 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
520 void __mptcp_subflow_send_ack(struct sock *ssk)
522 if (tcp_can_send_ack(ssk))
526 static void mptcp_subflow_send_ack(struct sock *ssk)
530 slow = lock_sock_fast(ssk);
531 __mptcp_subflow_send_ack(ssk);
532 unlock_sock_fast(ssk, slow);
535 static void mptcp_send_ack(struct mptcp_sock *msk)
537 struct mptcp_subflow_context *subflow;
539 mptcp_for_each_subflow(msk, subflow)
540 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
543 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
547 slow = lock_sock_fast(ssk);
548 if (tcp_can_send_ack(ssk))
549 tcp_cleanup_rbuf(ssk, 1);
550 unlock_sock_fast(ssk, slow);
553 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
555 const struct inet_connection_sock *icsk = inet_csk(ssk);
556 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
557 const struct tcp_sock *tp = tcp_sk(ssk);
559 return (ack_pending & ICSK_ACK_SCHED) &&
560 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
561 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
562 (rx_empty && ack_pending &
563 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
566 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
568 int old_space = READ_ONCE(msk->old_wspace);
569 struct mptcp_subflow_context *subflow;
570 struct sock *sk = (struct sock *)msk;
571 int space = __mptcp_space(sk);
572 bool cleanup, rx_empty;
574 cleanup = (space > 0) && (space >= (old_space << 1));
575 rx_empty = !__mptcp_rmem(sk);
577 mptcp_for_each_subflow(msk, subflow) {
578 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
580 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
581 mptcp_subflow_cleanup_rbuf(ssk);
585 static bool mptcp_check_data_fin(struct sock *sk)
587 struct mptcp_sock *msk = mptcp_sk(sk);
588 u64 rcv_data_fin_seq;
591 /* Need to ack a DATA_FIN received from a peer while this side
592 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
593 * msk->rcv_data_fin was set when parsing the incoming options
594 * at the subflow level and the msk lock was not held, so this
595 * is the first opportunity to act on the DATA_FIN and change
598 * If we are caught up to the sequence number of the incoming
599 * DATA_FIN, send the DATA_ACK now and do state transition. If
600 * not caught up, do nothing and let the recv code send DATA_ACK
604 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
605 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
606 WRITE_ONCE(msk->rcv_data_fin, 0);
608 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
609 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
611 switch (sk->sk_state) {
612 case TCP_ESTABLISHED:
613 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
616 inet_sk_state_store(sk, TCP_CLOSING);
619 inet_sk_state_store(sk, TCP_CLOSE);
622 /* Other states not expected */
628 if (!__mptcp_check_fallback(msk))
630 mptcp_close_wake_up(sk);
635 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
639 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
640 struct sock *sk = (struct sock *)msk;
641 unsigned int moved = 0;
642 bool more_data_avail;
647 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
649 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
650 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
652 if (unlikely(ssk_rbuf > sk_rbuf)) {
653 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
658 pr_debug("msk=%p ssk=%p", msk, ssk);
661 u32 map_remaining, offset;
662 u32 seq = tp->copied_seq;
666 /* try to move as much data as available */
667 map_remaining = subflow->map_data_len -
668 mptcp_subflow_get_map_offset(subflow);
670 skb = skb_peek(&ssk->sk_receive_queue);
672 /* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
673 * a different CPU can have already processed the pending
674 * data, stop here or we can enter an infinite loop
681 if (__mptcp_check_fallback(msk)) {
682 /* Under fallback skbs have no MPTCP extension and TCP could
683 * collapse them between the dummy map creation and the
684 * current dequeue. Be sure to adjust the map size.
686 map_remaining = skb->len;
687 subflow->map_data_len = skb->len;
690 offset = seq - TCP_SKB_CB(skb)->seq;
691 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
697 if (offset < skb->len) {
698 size_t len = skb->len - offset;
703 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
707 if (WARN_ON_ONCE(map_remaining < len))
711 sk_eat_skb(ssk, skb);
715 WRITE_ONCE(tp->copied_seq, seq);
716 more_data_avail = mptcp_subflow_data_available(ssk);
718 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
722 } while (more_data_avail);
728 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
730 struct sock *sk = (struct sock *)msk;
731 struct sk_buff *skb, *tail;
736 p = rb_first(&msk->out_of_order_queue);
737 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
740 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
744 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
746 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
749 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
753 end_seq = MPTCP_SKB_CB(skb)->end_seq;
754 tail = skb_peek_tail(&sk->sk_receive_queue);
755 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
756 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
758 /* skip overlapping data, if any */
759 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
760 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
762 MPTCP_SKB_CB(skb)->offset += delta;
763 MPTCP_SKB_CB(skb)->map_seq += delta;
764 __skb_queue_tail(&sk->sk_receive_queue, skb);
766 msk->bytes_received += end_seq - msk->ack_seq;
767 msk->ack_seq = end_seq;
773 static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
775 int err = sock_error(ssk);
781 /* only propagate errors on fallen-back sockets or
784 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk)))
787 /* We need to propagate only transition to CLOSE state.
788 * Orphaned socket will see such state change via
789 * subflow_sched_work_if_closed() and that path will properly
790 * destroy the msk as needed.
792 ssk_state = inet_sk_state_load(ssk);
793 if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
794 inet_sk_state_store(sk, ssk_state);
795 WRITE_ONCE(sk->sk_err, -err);
797 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
803 void __mptcp_error_report(struct sock *sk)
805 struct mptcp_subflow_context *subflow;
806 struct mptcp_sock *msk = mptcp_sk(sk);
808 mptcp_for_each_subflow(msk, subflow)
809 if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
813 /* In most cases we will be able to lock the mptcp socket. If its already
814 * owned, we need to defer to the work queue to avoid ABBA deadlock.
816 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
818 struct sock *sk = (struct sock *)msk;
819 unsigned int moved = 0;
821 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
822 __mptcp_ofo_queue(msk);
823 if (unlikely(ssk->sk_err)) {
824 if (!sock_owned_by_user(sk))
825 __mptcp_error_report(sk);
827 __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags);
830 /* If the moves have caught up with the DATA_FIN sequence number
831 * it's time to ack the DATA_FIN and change socket state, but
832 * this is not a good place to change state. Let the workqueue
835 if (mptcp_pending_data_fin(sk, NULL))
836 mptcp_schedule_work(sk);
840 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
842 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
843 struct mptcp_sock *msk = mptcp_sk(sk);
844 int sk_rbuf, ssk_rbuf;
846 /* The peer can send data while we are shutting down this
847 * subflow at msk destruction time, but we must avoid enqueuing
848 * more data to the msk receive queue
850 if (unlikely(subflow->disposable))
853 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
854 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
855 if (unlikely(ssk_rbuf > sk_rbuf))
858 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
859 if (__mptcp_rmem(sk) > sk_rbuf) {
860 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
864 /* Wake-up the reader only for in-sequence data */
866 if (move_skbs_to_msk(msk, ssk))
867 sk->sk_data_ready(sk);
869 mptcp_data_unlock(sk);
872 static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
874 mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
875 WRITE_ONCE(msk->allow_infinite_fallback, false);
876 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
879 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
881 struct sock *sk = (struct sock *)msk;
883 if (sk->sk_state != TCP_ESTABLISHED)
886 /* attach to msk socket only after we are sure we will deal with it
889 if (sk->sk_socket && !ssk->sk_socket)
890 mptcp_sock_graft(ssk, sk->sk_socket);
892 mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
893 mptcp_sockopt_sync_locked(msk, ssk);
894 mptcp_subflow_joined(msk, ssk);
898 static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
900 struct mptcp_subflow_context *tmp, *subflow;
901 struct mptcp_sock *msk = mptcp_sk(sk);
903 list_for_each_entry_safe(subflow, tmp, join_list, node) {
904 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
905 bool slow = lock_sock_fast(ssk);
907 list_move_tail(&subflow->node, &msk->conn_list);
908 if (!__mptcp_finish_join(msk, ssk))
909 mptcp_subflow_reset(ssk);
910 unlock_sock_fast(ssk, slow);
914 static bool mptcp_timer_pending(struct sock *sk)
916 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
919 static void mptcp_reset_timer(struct sock *sk)
921 struct inet_connection_sock *icsk = inet_csk(sk);
924 /* prevent rescheduling on close */
925 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
928 tout = mptcp_sk(sk)->timer_ival;
929 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
932 bool mptcp_schedule_work(struct sock *sk)
934 if (inet_sk_state_load(sk) != TCP_CLOSE &&
935 schedule_work(&mptcp_sk(sk)->work)) {
936 /* each subflow already holds a reference to the sk, and the
937 * workqueue is invoked by a subflow, so sk can't go away here.
945 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
947 struct mptcp_subflow_context *subflow;
949 msk_owned_by_me(msk);
951 mptcp_for_each_subflow(msk, subflow) {
952 if (READ_ONCE(subflow->data_avail))
953 return mptcp_subflow_tcp_sock(subflow);
959 static bool mptcp_skb_can_collapse_to(u64 write_seq,
960 const struct sk_buff *skb,
961 const struct mptcp_ext *mpext)
963 if (!tcp_skb_can_collapse_to(skb))
966 /* can collapse only if MPTCP level sequence is in order and this
967 * mapping has not been xmitted yet
969 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
973 /* we can append data to the given data frag if:
974 * - there is space available in the backing page_frag
975 * - the data frag tail matches the current page_frag free offset
976 * - the data frag end sequence number matches the current write seq
978 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
979 const struct page_frag *pfrag,
980 const struct mptcp_data_frag *df)
982 return df && pfrag->page == df->page &&
983 pfrag->size - pfrag->offset > 0 &&
984 pfrag->offset == (df->offset + df->data_len) &&
985 df->data_seq + df->data_len == msk->write_seq;
988 static void dfrag_uncharge(struct sock *sk, int len)
990 sk_mem_uncharge(sk, len);
991 sk_wmem_queued_add(sk, -len);
994 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
996 int len = dfrag->data_len + dfrag->overhead;
998 list_del(&dfrag->list);
999 dfrag_uncharge(sk, len);
1000 put_page(dfrag->page);
1003 static void __mptcp_clean_una(struct sock *sk)
1005 struct mptcp_sock *msk = mptcp_sk(sk);
1006 struct mptcp_data_frag *dtmp, *dfrag;
1009 snd_una = msk->snd_una;
1010 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1011 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1014 if (unlikely(dfrag == msk->first_pending)) {
1015 /* in recovery mode can see ack after the current snd head */
1016 if (WARN_ON_ONCE(!msk->recovery))
1019 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1022 dfrag_clear(sk, dfrag);
1025 dfrag = mptcp_rtx_head(sk);
1026 if (dfrag && after64(snd_una, dfrag->data_seq)) {
1027 u64 delta = snd_una - dfrag->data_seq;
1029 /* prevent wrap around in recovery mode */
1030 if (unlikely(delta > dfrag->already_sent)) {
1031 if (WARN_ON_ONCE(!msk->recovery))
1033 if (WARN_ON_ONCE(delta > dfrag->data_len))
1035 dfrag->already_sent += delta - dfrag->already_sent;
1038 dfrag->data_seq += delta;
1039 dfrag->offset += delta;
1040 dfrag->data_len -= delta;
1041 dfrag->already_sent -= delta;
1043 dfrag_uncharge(sk, delta);
1046 /* all retransmitted data acked, recovery completed */
1047 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1048 msk->recovery = false;
1051 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1052 snd_una == READ_ONCE(msk->write_seq)) {
1053 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1054 mptcp_stop_timer(sk);
1056 mptcp_reset_timer(sk);
1060 static void __mptcp_clean_una_wakeup(struct sock *sk)
1062 lockdep_assert_held_once(&sk->sk_lock.slock);
1064 __mptcp_clean_una(sk);
1065 mptcp_write_space(sk);
1068 static void mptcp_clean_una_wakeup(struct sock *sk)
1070 mptcp_data_lock(sk);
1071 __mptcp_clean_una_wakeup(sk);
1072 mptcp_data_unlock(sk);
1075 static void mptcp_enter_memory_pressure(struct sock *sk)
1077 struct mptcp_subflow_context *subflow;
1078 struct mptcp_sock *msk = mptcp_sk(sk);
1081 sk_stream_moderate_sndbuf(sk);
1082 mptcp_for_each_subflow(msk, subflow) {
1083 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1086 tcp_enter_memory_pressure(ssk);
1087 sk_stream_moderate_sndbuf(ssk);
1092 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1095 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1097 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1098 pfrag, sk->sk_allocation)))
1101 mptcp_enter_memory_pressure(sk);
1105 static struct mptcp_data_frag *
1106 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1109 int offset = ALIGN(orig_offset, sizeof(long));
1110 struct mptcp_data_frag *dfrag;
1112 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1113 dfrag->data_len = 0;
1114 dfrag->data_seq = msk->write_seq;
1115 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1116 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1117 dfrag->already_sent = 0;
1118 dfrag->page = pfrag->page;
1123 struct mptcp_sendmsg_info {
1129 bool data_lock_held;
1132 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1133 u64 data_seq, int avail_size)
1135 u64 window_end = mptcp_wnd_end(msk);
1138 if (__mptcp_check_fallback(msk))
1141 mptcp_snd_wnd = window_end - data_seq;
1142 avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1144 if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1145 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1146 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1152 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1154 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1158 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1162 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1164 struct sk_buff *skb;
1166 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1168 if (likely(__mptcp_add_ext(skb, gfp))) {
1169 skb_reserve(skb, MAX_TCP_HEADER);
1170 skb->ip_summed = CHECKSUM_PARTIAL;
1171 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1176 mptcp_enter_memory_pressure(sk);
1181 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1183 struct sk_buff *skb;
1185 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1189 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1190 tcp_skb_entail(ssk, skb);
1193 tcp_skb_tsorted_anchor_cleanup(skb);
1198 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1200 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1202 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1205 /* note: this always recompute the csum on the whole skb, even
1206 * if we just appended a single frag. More status info needed
1208 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1210 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1211 __wsum csum = ~csum_unfold(mpext->csum);
1212 int offset = skb->len - added;
1214 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1217 static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1219 struct mptcp_ext *mpext)
1224 mpext->infinite_map = 1;
1225 mpext->data_len = 0;
1227 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1228 mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1230 mptcp_do_fallback(ssk);
1233 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1234 struct mptcp_data_frag *dfrag,
1235 struct mptcp_sendmsg_info *info)
1237 u64 data_seq = dfrag->data_seq + info->sent;
1238 int offset = dfrag->offset + info->sent;
1239 struct mptcp_sock *msk = mptcp_sk(sk);
1240 bool zero_window_probe = false;
1241 struct mptcp_ext *mpext = NULL;
1242 bool can_coalesce = false;
1243 bool reuse_skb = true;
1244 struct sk_buff *skb;
1248 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1249 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1251 if (WARN_ON_ONCE(info->sent > info->limit ||
1252 info->limit > dfrag->data_len))
1255 if (unlikely(!__tcp_can_send(ssk)))
1258 /* compute send limit */
1259 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1260 copy = info->size_goal;
1262 skb = tcp_write_queue_tail(ssk);
1263 if (skb && copy > skb->len) {
1264 /* Limit the write to the size available in the
1265 * current skb, if any, so that we create at most a new skb.
1266 * Explicitly tells TCP internals to avoid collapsing on later
1267 * queue management operation, to avoid breaking the ext <->
1268 * SSN association set here
1270 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1271 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1272 TCP_SKB_CB(skb)->eor = 1;
1276 i = skb_shinfo(skb)->nr_frags;
1277 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1278 if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
1279 tcp_mark_push(tcp_sk(ssk), skb);
1286 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1290 i = skb_shinfo(skb)->nr_frags;
1292 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1295 /* Zero window and all data acked? Probe. */
1296 copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1298 u64 snd_una = READ_ONCE(msk->snd_una);
1300 if (snd_una != msk->snd_nxt) {
1301 tcp_remove_empty_skb(ssk);
1305 zero_window_probe = true;
1306 data_seq = snd_una - 1;
1309 /* all mptcp-level data is acked, no skbs should be present into the
1312 WARN_ON_ONCE(reuse_skb);
1315 copy = min_t(size_t, copy, info->limit - info->sent);
1316 if (!sk_wmem_schedule(ssk, copy)) {
1317 tcp_remove_empty_skb(ssk);
1322 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1324 get_page(dfrag->page);
1325 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1329 skb->data_len += copy;
1330 skb->truesize += copy;
1331 sk_wmem_queued_add(ssk, copy);
1332 sk_mem_charge(ssk, copy);
1333 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1334 TCP_SKB_CB(skb)->end_seq += copy;
1335 tcp_skb_pcount_set(skb, 0);
1337 /* on skb reuse we just need to update the DSS len */
1339 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1340 mpext->data_len += copy;
1341 WARN_ON_ONCE(zero_window_probe);
1345 memset(mpext, 0, sizeof(*mpext));
1346 mpext->data_seq = data_seq;
1347 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1348 mpext->data_len = copy;
1352 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1353 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1356 if (zero_window_probe) {
1357 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1359 if (READ_ONCE(msk->csum_enabled))
1360 mptcp_update_data_checksum(skb, copy);
1361 tcp_push_pending_frames(ssk);
1365 if (READ_ONCE(msk->csum_enabled))
1366 mptcp_update_data_checksum(skb, copy);
1367 if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1368 mptcp_update_infinite_map(msk, ssk, mpext);
1369 trace_mptcp_sendmsg_frag(mpext);
1370 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1374 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1375 sizeof(struct tcphdr) - \
1376 MAX_TCP_OPTION_SPACE - \
1377 sizeof(struct ipv6hdr) - \
1378 sizeof(struct frag_hdr))
1380 struct subflow_send_info {
1385 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1387 if (!subflow->stale)
1391 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1394 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1396 if (unlikely(subflow->stale)) {
1397 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1399 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1402 mptcp_subflow_set_active(subflow);
1404 return __mptcp_subflow_active(subflow);
1407 #define SSK_MODE_ACTIVE 0
1408 #define SSK_MODE_BACKUP 1
1409 #define SSK_MODE_MAX 2
1411 /* implement the mptcp packet scheduler;
1412 * returns the subflow that will transmit the next DSS
1413 * additionally updates the rtx timeout
1415 struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1417 struct subflow_send_info send_info[SSK_MODE_MAX];
1418 struct mptcp_subflow_context *subflow;
1419 struct sock *sk = (struct sock *)msk;
1420 u32 pace, burst, wmem;
1421 int i, nr_active = 0;
1426 /* pick the subflow with the lower wmem/wspace ratio */
1427 for (i = 0; i < SSK_MODE_MAX; ++i) {
1428 send_info[i].ssk = NULL;
1429 send_info[i].linger_time = -1;
1432 mptcp_for_each_subflow(msk, subflow) {
1433 trace_mptcp_subflow_get_send(subflow);
1434 ssk = mptcp_subflow_tcp_sock(subflow);
1435 if (!mptcp_subflow_active(subflow))
1438 tout = max(tout, mptcp_timeout_from_subflow(subflow));
1439 nr_active += !subflow->backup;
1440 pace = subflow->avg_pacing_rate;
1441 if (unlikely(!pace)) {
1442 /* init pacing rate from socket */
1443 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1444 pace = subflow->avg_pacing_rate;
1449 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1450 if (linger_time < send_info[subflow->backup].linger_time) {
1451 send_info[subflow->backup].ssk = ssk;
1452 send_info[subflow->backup].linger_time = linger_time;
1455 __mptcp_set_timeout(sk, tout);
1457 /* pick the best backup if no other subflow is active */
1459 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1461 /* According to the blest algorithm, to avoid HoL blocking for the
1462 * faster flow, we need to:
1463 * - estimate the faster flow linger time
1464 * - use the above to estimate the amount of byte transferred
1465 * by the faster flow
1466 * - check that the amount of queued data is greter than the above,
1467 * otherwise do not use the picked, slower, subflow
1468 * We select the subflow with the shorter estimated time to flush
1469 * the queued mem, which basically ensure the above. We just need
1470 * to check that subflow has a non empty cwin.
1472 ssk = send_info[SSK_MODE_ACTIVE].ssk;
1473 if (!ssk || !sk_stream_memory_free(ssk))
1476 burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1477 wmem = READ_ONCE(ssk->sk_wmem_queued);
1481 subflow = mptcp_subflow_ctx(ssk);
1482 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1483 READ_ONCE(ssk->sk_pacing_rate) * burst,
1485 msk->snd_burst = burst;
1489 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1491 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1495 static void mptcp_update_post_push(struct mptcp_sock *msk,
1496 struct mptcp_data_frag *dfrag,
1499 u64 snd_nxt_new = dfrag->data_seq;
1501 dfrag->already_sent += sent;
1503 msk->snd_burst -= sent;
1505 snd_nxt_new += dfrag->already_sent;
1507 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1508 * is recovering after a failover. In that event, this re-sends
1511 * Thus compute snd_nxt_new candidate based on
1512 * the dfrag->data_seq that was sent and the data
1513 * that has been handed to the subflow for transmission
1514 * and skip update in case it was old dfrag.
1516 if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
1517 msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
1518 msk->snd_nxt = snd_nxt_new;
1522 void mptcp_check_and_set_pending(struct sock *sk)
1524 if (mptcp_send_head(sk))
1525 mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING);
1528 static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
1529 struct mptcp_sendmsg_info *info)
1531 struct mptcp_sock *msk = mptcp_sk(sk);
1532 struct mptcp_data_frag *dfrag;
1533 int len, copied = 0, err = 0;
1535 while ((dfrag = mptcp_send_head(sk))) {
1536 info->sent = dfrag->already_sent;
1537 info->limit = dfrag->data_len;
1538 len = dfrag->data_len - dfrag->already_sent;
1542 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
1544 err = copied ? : ret;
1552 mptcp_update_post_push(msk, dfrag, ret);
1554 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1556 if (msk->snd_burst <= 0 ||
1557 !sk_stream_memory_free(ssk) ||
1558 !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
1562 mptcp_set_timeout(sk);
1570 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1572 struct sock *prev_ssk = NULL, *ssk = NULL;
1573 struct mptcp_sock *msk = mptcp_sk(sk);
1574 struct mptcp_sendmsg_info info = {
1577 bool do_check_data_fin = false;
1580 while (mptcp_send_head(sk) && (push_count > 0)) {
1581 struct mptcp_subflow_context *subflow;
1584 if (mptcp_sched_get_send(msk))
1589 mptcp_for_each_subflow(msk, subflow) {
1590 if (READ_ONCE(subflow->scheduled)) {
1591 mptcp_subflow_set_scheduled(subflow, false);
1594 ssk = mptcp_subflow_tcp_sock(subflow);
1595 if (ssk != prev_ssk) {
1596 /* First check. If the ssk has changed since
1597 * the last round, release prev_ssk
1600 mptcp_push_release(prev_ssk, &info);
1602 /* Need to lock the new subflow only if different
1603 * from the previous one, otherwise we are still
1604 * helding the relevant lock
1611 ret = __subflow_push_pending(sk, ssk, &info);
1613 if (ret != -EAGAIN ||
1614 (1 << ssk->sk_state) &
1615 (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
1619 do_check_data_fin = true;
1624 /* at this point we held the socket lock for the last subflow we used */
1626 mptcp_push_release(ssk, &info);
1628 /* ensure the rtx timer is running */
1629 if (!mptcp_timer_pending(sk))
1630 mptcp_reset_timer(sk);
1631 if (do_check_data_fin)
1632 mptcp_check_send_data_fin(sk);
1635 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
1637 struct mptcp_sock *msk = mptcp_sk(sk);
1638 struct mptcp_sendmsg_info info = {
1639 .data_lock_held = true,
1641 bool keep_pushing = true;
1642 struct sock *xmit_ssk;
1646 while (mptcp_send_head(sk) && keep_pushing) {
1647 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1650 /* check for a different subflow usage only after
1651 * spooling the first chunk of data
1654 mptcp_subflow_set_scheduled(subflow, false);
1655 ret = __subflow_push_pending(sk, ssk, &info);
1663 if (mptcp_sched_get_send(msk))
1666 if (READ_ONCE(subflow->scheduled)) {
1667 mptcp_subflow_set_scheduled(subflow, false);
1668 ret = __subflow_push_pending(sk, ssk, &info);
1670 keep_pushing = false;
1674 mptcp_for_each_subflow(msk, subflow) {
1675 if (READ_ONCE(subflow->scheduled)) {
1676 xmit_ssk = mptcp_subflow_tcp_sock(subflow);
1677 if (xmit_ssk != ssk) {
1678 mptcp_subflow_delegate(subflow,
1679 MPTCP_DELEGATE_SEND);
1680 keep_pushing = false;
1687 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1688 * not going to flush it via release_sock()
1691 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1693 if (!mptcp_timer_pending(sk))
1694 mptcp_reset_timer(sk);
1696 if (msk->snd_data_fin_enable &&
1697 msk->snd_nxt + 1 == msk->write_seq)
1698 mptcp_schedule_work(sk);
1702 static void mptcp_set_nospace(struct sock *sk)
1704 /* enable autotune */
1705 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1707 /* will be cleared on avail space */
1708 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1711 static int mptcp_disconnect(struct sock *sk, int flags);
1713 static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1714 size_t len, int *copied_syn)
1716 unsigned int saved_flags = msg->msg_flags;
1717 struct mptcp_sock *msk = mptcp_sk(sk);
1721 /* on flags based fastopen the mptcp is supposed to create the
1722 * first subflow right now. Otherwise we are in the defer_connect
1723 * path, and the first subflow must be already present.
1724 * Since the defer_connect flag is cleared after the first succsful
1725 * fastopen attempt, no need to check for additional subflow status.
1727 if (msg->msg_flags & MSG_FASTOPEN) {
1728 ssk = __mptcp_nmpc_sk(msk);
1730 return PTR_ERR(ssk);
1738 msg->msg_flags |= MSG_DONTWAIT;
1739 msk->fastopening = 1;
1740 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1741 msk->fastopening = 0;
1742 msg->msg_flags = saved_flags;
1745 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1746 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1747 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1748 msg->msg_namelen, msg->msg_flags, 1);
1750 /* Keep the same behaviour of plain TCP: zero the copied bytes in
1751 * case of any error, except timeout or signal
1753 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1755 } else if (ret && ret != -EINPROGRESS) {
1756 /* The disconnect() op called by tcp_sendmsg_fastopen()/
1757 * __inet_stream_connect() can fail, due to looking check,
1758 * see mptcp_disconnect().
1759 * Attempt it again outside the problematic scope.
1761 if (!mptcp_disconnect(sk, 0))
1762 sk->sk_socket->state = SS_UNCONNECTED;
1764 inet_clear_bit(DEFER_CONNECT, sk);
1769 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1771 struct mptcp_sock *msk = mptcp_sk(sk);
1772 struct page_frag *pfrag;
1777 /* silently ignore everything else */
1778 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1782 if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
1783 msg->msg_flags & MSG_FASTOPEN)) {
1786 ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1787 copied += copied_syn;
1788 if (ret == -EINPROGRESS && copied_syn > 0)
1794 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1796 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1797 ret = sk_stream_wait_connect(sk, &timeo);
1803 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1806 pfrag = sk_page_frag(sk);
1808 while (msg_data_left(msg)) {
1809 int total_ts, frag_truesize = 0;
1810 struct mptcp_data_frag *dfrag;
1811 bool dfrag_collapsed;
1812 size_t psize, offset;
1814 /* reuse tail pfrag, if possible, or carve a new one from the
1817 dfrag = mptcp_pending_tail(sk);
1818 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1819 if (!dfrag_collapsed) {
1820 if (!sk_stream_memory_free(sk))
1821 goto wait_for_memory;
1823 if (!mptcp_page_frag_refill(sk, pfrag))
1824 goto wait_for_memory;
1826 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1827 frag_truesize = dfrag->overhead;
1830 /* we do not bound vs wspace, to allow a single packet.
1831 * memory accounting will prevent execessive memory usage
1834 offset = dfrag->offset + dfrag->data_len;
1835 psize = pfrag->size - offset;
1836 psize = min_t(size_t, psize, msg_data_left(msg));
1837 total_ts = psize + frag_truesize;
1839 if (!sk_wmem_schedule(sk, total_ts))
1840 goto wait_for_memory;
1842 if (copy_page_from_iter(dfrag->page, offset, psize,
1843 &msg->msg_iter) != psize) {
1848 /* data successfully copied into the write queue */
1849 sk_forward_alloc_add(sk, -total_ts);
1851 dfrag->data_len += psize;
1852 frag_truesize += psize;
1853 pfrag->offset += frag_truesize;
1854 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1856 /* charge data on mptcp pending queue to the msk socket
1857 * Note: we charge such data both to sk and ssk
1859 sk_wmem_queued_add(sk, frag_truesize);
1860 if (!dfrag_collapsed) {
1861 get_page(dfrag->page);
1862 list_add_tail(&dfrag->list, &msk->rtx_queue);
1863 if (!msk->first_pending)
1864 WRITE_ONCE(msk->first_pending, dfrag);
1866 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1867 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1873 mptcp_set_nospace(sk);
1874 __mptcp_push_pending(sk, msg->msg_flags);
1875 ret = sk_stream_wait_memory(sk, &timeo);
1881 __mptcp_push_pending(sk, msg->msg_flags);
1891 copied = sk_stream_error(sk, msg->msg_flags, ret);
1895 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1897 size_t len, int flags,
1898 struct scm_timestamping_internal *tss,
1901 struct sk_buff *skb, *tmp;
1904 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1905 u32 offset = MPTCP_SKB_CB(skb)->offset;
1906 u32 data_len = skb->len - offset;
1907 u32 count = min_t(size_t, len - copied, data_len);
1910 if (!(flags & MSG_TRUNC)) {
1911 err = skb_copy_datagram_msg(skb, offset, msg, count);
1912 if (unlikely(err < 0)) {
1919 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1920 tcp_update_recv_tstamps(skb, tss);
1921 *cmsg_flags |= MPTCP_CMSG_TS;
1926 if (count < data_len) {
1927 if (!(flags & MSG_PEEK)) {
1928 MPTCP_SKB_CB(skb)->offset += count;
1929 MPTCP_SKB_CB(skb)->map_seq += count;
1934 if (!(flags & MSG_PEEK)) {
1935 /* we will bulk release the skb memory later */
1936 skb->destructor = NULL;
1937 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1938 __skb_unlink(skb, &msk->receive_queue);
1949 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1951 * Only difference: Use highest rtt estimate of the subflows in use.
1953 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1955 struct mptcp_subflow_context *subflow;
1956 struct sock *sk = (struct sock *)msk;
1957 u8 scaling_ratio = U8_MAX;
1958 u32 time, advmss = 1;
1961 msk_owned_by_me(msk);
1966 msk->rcvq_space.copied += copied;
1968 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1969 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1971 rtt_us = msk->rcvq_space.rtt_us;
1972 if (rtt_us && time < (rtt_us >> 3))
1976 mptcp_for_each_subflow(msk, subflow) {
1977 const struct tcp_sock *tp;
1981 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1983 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1984 sf_advmss = READ_ONCE(tp->advmss);
1986 rtt_us = max(sf_rtt_us, rtt_us);
1987 advmss = max(sf_advmss, advmss);
1988 scaling_ratio = min(tp->scaling_ratio, scaling_ratio);
1991 msk->rcvq_space.rtt_us = rtt_us;
1992 msk->scaling_ratio = scaling_ratio;
1993 if (time < (rtt_us >> 3) || rtt_us == 0)
1996 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1999 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
2000 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
2004 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
2006 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
2008 do_div(grow, msk->rcvq_space.space);
2009 rcvwin += (grow << 1);
2011 rcvbuf = min_t(u64, __tcp_space_from_win(scaling_ratio, rcvwin),
2012 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
2014 if (rcvbuf > sk->sk_rcvbuf) {
2017 window_clamp = __tcp_win_from_space(scaling_ratio, rcvbuf);
2018 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
2020 /* Make subflows follow along. If we do not do this, we
2021 * get drops at subflow level if skbs can't be moved to
2022 * the mptcp rx queue fast enough (announced rcv_win can
2023 * exceed ssk->sk_rcvbuf).
2025 mptcp_for_each_subflow(msk, subflow) {
2029 ssk = mptcp_subflow_tcp_sock(subflow);
2030 slow = lock_sock_fast(ssk);
2031 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
2032 tcp_sk(ssk)->window_clamp = window_clamp;
2033 tcp_cleanup_rbuf(ssk, 1);
2034 unlock_sock_fast(ssk, slow);
2039 msk->rcvq_space.space = msk->rcvq_space.copied;
2041 msk->rcvq_space.copied = 0;
2042 msk->rcvq_space.time = mstamp;
2045 static void __mptcp_update_rmem(struct sock *sk)
2047 struct mptcp_sock *msk = mptcp_sk(sk);
2049 if (!msk->rmem_released)
2052 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
2053 mptcp_rmem_uncharge(sk, msk->rmem_released);
2054 WRITE_ONCE(msk->rmem_released, 0);
2057 static void __mptcp_splice_receive_queue(struct sock *sk)
2059 struct mptcp_sock *msk = mptcp_sk(sk);
2061 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
2064 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
2066 struct sock *sk = (struct sock *)msk;
2067 unsigned int moved = 0;
2071 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
2074 /* we can have data pending in the subflows only if the msk
2075 * receive buffer was full at subflow_data_ready() time,
2076 * that is an unlikely slow path.
2081 slowpath = lock_sock_fast(ssk);
2082 mptcp_data_lock(sk);
2083 __mptcp_update_rmem(sk);
2084 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
2085 mptcp_data_unlock(sk);
2087 if (unlikely(ssk->sk_err))
2088 __mptcp_error_report(sk);
2089 unlock_sock_fast(ssk, slowpath);
2092 /* acquire the data lock only if some input data is pending */
2094 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
2095 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
2096 mptcp_data_lock(sk);
2097 __mptcp_update_rmem(sk);
2098 ret |= __mptcp_ofo_queue(msk);
2099 __mptcp_splice_receive_queue(sk);
2100 mptcp_data_unlock(sk);
2103 mptcp_check_data_fin((struct sock *)msk);
2104 return !skb_queue_empty(&msk->receive_queue);
2107 static unsigned int mptcp_inq_hint(const struct sock *sk)
2109 const struct mptcp_sock *msk = mptcp_sk(sk);
2110 const struct sk_buff *skb;
2112 skb = skb_peek(&msk->receive_queue);
2114 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
2116 if (hint_val >= INT_MAX)
2119 return (unsigned int)hint_val;
2122 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2128 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2129 int flags, int *addr_len)
2131 struct mptcp_sock *msk = mptcp_sk(sk);
2132 struct scm_timestamping_internal tss;
2133 int copied = 0, cmsg_flags = 0;
2137 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2138 if (unlikely(flags & MSG_ERRQUEUE))
2139 return inet_recv_error(sk, msg, len, addr_len);
2142 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2147 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2149 len = min_t(size_t, len, INT_MAX);
2150 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2152 if (unlikely(msk->recvmsg_inq))
2153 cmsg_flags = MPTCP_CMSG_INQ;
2155 while (copied < len) {
2158 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2159 if (unlikely(bytes_read < 0)) {
2161 copied = bytes_read;
2165 copied += bytes_read;
2167 /* be sure to advertise window change */
2168 mptcp_cleanup_rbuf(msk);
2170 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2173 /* only the master socket status is relevant here. The exit
2174 * conditions mirror closely tcp_recvmsg()
2176 if (copied >= target)
2181 sk->sk_state == TCP_CLOSE ||
2182 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2184 signal_pending(current))
2188 copied = sock_error(sk);
2192 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2193 /* race breaker: the shutdown could be after the
2194 * previous receive queue check
2196 if (__mptcp_move_skbs(msk))
2201 if (sk->sk_state == TCP_CLOSE) {
2211 if (signal_pending(current)) {
2212 copied = sock_intr_errno(timeo);
2217 pr_debug("block timeout %ld", timeo);
2218 sk_wait_data(sk, &timeo, NULL);
2222 if (cmsg_flags && copied >= 0) {
2223 if (cmsg_flags & MPTCP_CMSG_TS)
2224 tcp_recv_timestamp(msg, sk, &tss);
2226 if (cmsg_flags & MPTCP_CMSG_INQ) {
2227 unsigned int inq = mptcp_inq_hint(sk);
2229 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2233 pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2234 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2235 skb_queue_empty(&msk->receive_queue), copied);
2236 if (!(flags & MSG_PEEK))
2237 mptcp_rcv_space_adjust(msk, copied);
2243 static void mptcp_retransmit_timer(struct timer_list *t)
2245 struct inet_connection_sock *icsk = from_timer(icsk, t,
2246 icsk_retransmit_timer);
2247 struct sock *sk = &icsk->icsk_inet.sk;
2248 struct mptcp_sock *msk = mptcp_sk(sk);
2251 if (!sock_owned_by_user(sk)) {
2252 /* we need a process context to retransmit */
2253 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2254 mptcp_schedule_work(sk);
2256 /* delegate our work to tcp_release_cb() */
2257 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2263 static void mptcp_timeout_timer(struct timer_list *t)
2265 struct sock *sk = from_timer(sk, t, sk_timer);
2267 mptcp_schedule_work(sk);
2271 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2274 * A backup subflow is returned only if that is the only kind available.
2276 struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2278 struct sock *backup = NULL, *pick = NULL;
2279 struct mptcp_subflow_context *subflow;
2280 int min_stale_count = INT_MAX;
2282 mptcp_for_each_subflow(msk, subflow) {
2283 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2285 if (!__mptcp_subflow_active(subflow))
2288 /* still data outstanding at TCP level? skip this */
2289 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2290 mptcp_pm_subflow_chk_stale(msk, ssk);
2291 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2295 if (subflow->backup) {
2308 /* use backup only if there are no progresses anywhere */
2309 return min_stale_count > 1 ? backup : NULL;
2312 bool __mptcp_retransmit_pending_data(struct sock *sk)
2314 struct mptcp_data_frag *cur, *rtx_head;
2315 struct mptcp_sock *msk = mptcp_sk(sk);
2317 if (__mptcp_check_fallback(msk))
2320 if (tcp_rtx_and_write_queues_empty(sk))
2323 /* the closing socket has some data untransmitted and/or unacked:
2324 * some data in the mptcp rtx queue has not really xmitted yet.
2325 * keep it simple and re-inject the whole mptcp level rtx queue
2327 mptcp_data_lock(sk);
2328 __mptcp_clean_una_wakeup(sk);
2329 rtx_head = mptcp_rtx_head(sk);
2331 mptcp_data_unlock(sk);
2335 msk->recovery_snd_nxt = msk->snd_nxt;
2336 msk->recovery = true;
2337 mptcp_data_unlock(sk);
2339 msk->first_pending = rtx_head;
2342 /* be sure to clear the "sent status" on all re-injected fragments */
2343 list_for_each_entry(cur, &msk->rtx_queue, list) {
2344 if (!cur->already_sent)
2346 cur->already_sent = 0;
2352 /* flags for __mptcp_close_ssk() */
2353 #define MPTCP_CF_PUSH BIT(1)
2354 #define MPTCP_CF_FASTCLOSE BIT(2)
2356 /* subflow sockets can be either outgoing (connect) or incoming
2359 * Outgoing subflows use in-kernel sockets.
2360 * Incoming subflows do not have their own 'struct socket' allocated,
2361 * so we need to use tcp_close() after detaching them from the mptcp
2364 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2365 struct mptcp_subflow_context *subflow,
2368 struct mptcp_sock *msk = mptcp_sk(sk);
2369 bool dispose_it, need_push = false;
2371 /* If the first subflow moved to a close state before accept, e.g. due
2372 * to an incoming reset, mptcp either:
2373 * - if either the subflow or the msk are dead, destroy the context
2374 * (the subflow socket is deleted by inet_child_forget) and the msk
2375 * - otherwise do nothing at the moment and take action at accept and/or
2376 * listener shutdown - user-space must be able to accept() the closed
2379 if (msk->in_accept_queue && msk->first == ssk) {
2380 if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD))
2383 /* ensure later check in mptcp_worker() will dispose the msk */
2384 sock_set_flag(sk, SOCK_DEAD);
2385 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2386 mptcp_subflow_drop_ctx(ssk);
2390 dispose_it = msk->free_first || ssk != msk->first;
2392 list_del(&subflow->node);
2394 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2396 if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) {
2397 /* be sure to force the tcp_disconnect() path,
2398 * to generate the egress reset
2400 ssk->sk_lingertime = 0;
2401 sock_set_flag(ssk, SOCK_LINGER);
2402 subflow->send_fastclose = 1;
2405 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2407 /* The MPTCP code never wait on the subflow sockets, TCP-level
2408 * disconnect should never fail
2410 WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2411 mptcp_subflow_ctx_reset(subflow);
2417 subflow->disposable = 1;
2419 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2420 * the ssk has been already destroyed, we just need to release the
2421 * reference owned by msk;
2423 if (!inet_csk(ssk)->icsk_ulp_ops) {
2424 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2425 kfree_rcu(subflow, rcu);
2427 /* otherwise tcp will dispose of the ssk and subflow ctx */
2428 __tcp_close(ssk, 0);
2430 /* close acquired an extra ref */
2435 __mptcp_subflow_error_report(sk, ssk);
2440 if (ssk == msk->first)
2441 WRITE_ONCE(msk->first, NULL);
2445 __mptcp_push_pending(sk, 0);
2448 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2449 struct mptcp_subflow_context *subflow)
2451 if (sk->sk_state == TCP_ESTABLISHED)
2452 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2454 /* subflow aborted before reaching the fully_established status
2455 * attempt the creation of the next subflow
2457 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2459 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2462 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2467 static void __mptcp_close_subflow(struct sock *sk)
2469 struct mptcp_subflow_context *subflow, *tmp;
2470 struct mptcp_sock *msk = mptcp_sk(sk);
2474 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2475 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2477 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2480 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2481 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2484 mptcp_close_ssk(sk, ssk, subflow);
2489 static bool mptcp_should_close(const struct sock *sk)
2491 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2492 struct mptcp_subflow_context *subflow;
2494 if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue)
2497 /* if all subflows are in closed status don't bother with additional
2500 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2501 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2508 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2510 struct mptcp_subflow_context *subflow, *tmp;
2511 struct sock *sk = (struct sock *)msk;
2513 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2516 mptcp_token_destroy(msk);
2518 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2519 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2522 slow = lock_sock_fast(tcp_sk);
2523 if (tcp_sk->sk_state != TCP_CLOSE) {
2524 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2525 tcp_set_state(tcp_sk, TCP_CLOSE);
2527 unlock_sock_fast(tcp_sk, slow);
2530 /* Mirror the tcp_reset() error propagation */
2531 switch (sk->sk_state) {
2533 WRITE_ONCE(sk->sk_err, ECONNREFUSED);
2535 case TCP_CLOSE_WAIT:
2536 WRITE_ONCE(sk->sk_err, EPIPE);
2541 WRITE_ONCE(sk->sk_err, ECONNRESET);
2544 inet_sk_state_store(sk, TCP_CLOSE);
2545 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2546 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2547 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2549 /* the calling mptcp_worker will properly destroy the socket */
2550 if (sock_flag(sk, SOCK_DEAD))
2553 sk->sk_state_change(sk);
2554 sk_error_report(sk);
2557 static void __mptcp_retrans(struct sock *sk)
2559 struct mptcp_sock *msk = mptcp_sk(sk);
2560 struct mptcp_subflow_context *subflow;
2561 struct mptcp_sendmsg_info info = {};
2562 struct mptcp_data_frag *dfrag;
2567 mptcp_clean_una_wakeup(sk);
2569 /* first check ssk: need to kick "stale" logic */
2570 err = mptcp_sched_get_retrans(msk);
2571 dfrag = mptcp_rtx_head(sk);
2573 if (mptcp_data_fin_enabled(msk)) {
2574 struct inet_connection_sock *icsk = inet_csk(sk);
2576 icsk->icsk_retransmits++;
2577 mptcp_set_datafin_timeout(sk);
2578 mptcp_send_ack(msk);
2583 if (!mptcp_send_head(sk))
2592 mptcp_for_each_subflow(msk, subflow) {
2593 if (READ_ONCE(subflow->scheduled)) {
2596 mptcp_subflow_set_scheduled(subflow, false);
2598 ssk = mptcp_subflow_tcp_sock(subflow);
2602 /* limit retransmission to the bytes already sent on some subflows */
2604 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
2605 dfrag->already_sent;
2606 while (info.sent < info.limit) {
2607 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2611 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2616 len = max(copied, len);
2617 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2619 WRITE_ONCE(msk->allow_infinite_fallback, false);
2626 msk->bytes_retrans += len;
2627 dfrag->already_sent = max(dfrag->already_sent, len);
2630 mptcp_check_and_set_pending(sk);
2632 if (!mptcp_timer_pending(sk))
2633 mptcp_reset_timer(sk);
2636 /* schedule the timeout timer for the relevant event: either close timeout
2637 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2639 void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout)
2641 struct sock *sk = (struct sock *)msk;
2642 unsigned long timeout, close_timeout;
2644 if (!fail_tout && !sock_flag(sk, SOCK_DEAD))
2647 close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN;
2649 /* the close timeout takes precedence on the fail one, and here at least one of
2652 timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout;
2654 sk_reset_timer(sk, &sk->sk_timer, timeout);
2657 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2659 struct sock *ssk = msk->first;
2665 pr_debug("MP_FAIL doesn't respond, reset the subflow");
2667 slow = lock_sock_fast(ssk);
2668 mptcp_subflow_reset(ssk);
2669 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2670 unlock_sock_fast(ssk, slow);
2672 mptcp_reset_timeout(msk, 0);
2675 static void mptcp_do_fastclose(struct sock *sk)
2677 struct mptcp_subflow_context *subflow, *tmp;
2678 struct mptcp_sock *msk = mptcp_sk(sk);
2680 inet_sk_state_store(sk, TCP_CLOSE);
2681 mptcp_for_each_subflow_safe(msk, subflow, tmp)
2682 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2683 subflow, MPTCP_CF_FASTCLOSE);
2686 static void mptcp_worker(struct work_struct *work)
2688 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2689 struct sock *sk = (struct sock *)msk;
2690 unsigned long fail_tout;
2694 state = sk->sk_state;
2695 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2698 mptcp_check_fastclose(msk);
2700 mptcp_pm_nl_work(msk);
2702 mptcp_check_send_data_fin(sk);
2703 mptcp_check_data_fin_ack(sk);
2704 mptcp_check_data_fin(sk);
2706 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2707 __mptcp_close_subflow(sk);
2709 /* There is no point in keeping around an orphaned sk timedout or
2710 * closed, but we need the msk around to reply to incoming DATA_FIN,
2711 * even if it is orphaned and in FIN_WAIT2 state
2713 if (sock_flag(sk, SOCK_DEAD)) {
2714 if (mptcp_should_close(sk))
2715 mptcp_do_fastclose(sk);
2717 if (sk->sk_state == TCP_CLOSE) {
2718 __mptcp_destroy_sock(sk);
2723 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2724 __mptcp_retrans(sk);
2726 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2727 if (fail_tout && time_after(jiffies, fail_tout))
2728 mptcp_mp_fail_no_response(msk);
2735 static void __mptcp_init_sock(struct sock *sk)
2737 struct mptcp_sock *msk = mptcp_sk(sk);
2739 INIT_LIST_HEAD(&msk->conn_list);
2740 INIT_LIST_HEAD(&msk->join_list);
2741 INIT_LIST_HEAD(&msk->rtx_queue);
2742 INIT_WORK(&msk->work, mptcp_worker);
2743 __skb_queue_head_init(&msk->receive_queue);
2744 msk->out_of_order_queue = RB_ROOT;
2745 msk->first_pending = NULL;
2746 msk->rmem_fwd_alloc = 0;
2747 WRITE_ONCE(msk->rmem_released, 0);
2748 msk->timer_ival = TCP_RTO_MIN;
2750 WRITE_ONCE(msk->first, NULL);
2751 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2752 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2753 WRITE_ONCE(msk->allow_infinite_fallback, true);
2754 msk->recovery = false;
2755 msk->subflow_id = 1;
2757 mptcp_pm_data_init(msk);
2759 /* re-use the csk retrans timer for MPTCP-level retrans */
2760 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2761 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2764 static void mptcp_ca_reset(struct sock *sk)
2766 struct inet_connection_sock *icsk = inet_csk(sk);
2768 tcp_assign_congestion_control(sk);
2769 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2771 /* no need to keep a reference to the ops, the name will suffice */
2772 tcp_cleanup_congestion_control(sk);
2773 icsk->icsk_ca_ops = NULL;
2776 static int mptcp_init_sock(struct sock *sk)
2778 struct net *net = sock_net(sk);
2781 __mptcp_init_sock(sk);
2783 if (!mptcp_is_enabled(net))
2784 return -ENOPROTOOPT;
2786 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2789 ret = mptcp_init_sched(mptcp_sk(sk),
2790 mptcp_sched_find(mptcp_get_scheduler(net)));
2794 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2796 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2797 * propagate the correct value
2801 sk_sockets_allocated_inc(sk);
2802 sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2803 sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2808 static void __mptcp_clear_xmit(struct sock *sk)
2810 struct mptcp_sock *msk = mptcp_sk(sk);
2811 struct mptcp_data_frag *dtmp, *dfrag;
2813 WRITE_ONCE(msk->first_pending, NULL);
2814 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2815 dfrag_clear(sk, dfrag);
2818 void mptcp_cancel_work(struct sock *sk)
2820 struct mptcp_sock *msk = mptcp_sk(sk);
2822 if (cancel_work_sync(&msk->work))
2826 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2830 switch (ssk->sk_state) {
2832 if (!(how & RCV_SHUTDOWN))
2836 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2839 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2840 pr_debug("Fallback");
2841 ssk->sk_shutdown |= how;
2842 tcp_shutdown(ssk, how);
2844 /* simulate the data_fin ack reception to let the state
2845 * machine move forward
2847 WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2848 mptcp_schedule_work(sk);
2850 pr_debug("Sending DATA_FIN on subflow %p", ssk);
2852 if (!mptcp_timer_pending(sk))
2853 mptcp_reset_timer(sk);
2861 static const unsigned char new_state[16] = {
2862 /* current state: new state: action: */
2863 [0 /* (Invalid) */] = TCP_CLOSE,
2864 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2865 [TCP_SYN_SENT] = TCP_CLOSE,
2866 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2867 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2868 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2869 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2870 [TCP_CLOSE] = TCP_CLOSE,
2871 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2872 [TCP_LAST_ACK] = TCP_LAST_ACK,
2873 [TCP_LISTEN] = TCP_CLOSE,
2874 [TCP_CLOSING] = TCP_CLOSING,
2875 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2878 static int mptcp_close_state(struct sock *sk)
2880 int next = (int)new_state[sk->sk_state];
2881 int ns = next & TCP_STATE_MASK;
2883 inet_sk_state_store(sk, ns);
2885 return next & TCP_ACTION_FIN;
2888 static void mptcp_check_send_data_fin(struct sock *sk)
2890 struct mptcp_subflow_context *subflow;
2891 struct mptcp_sock *msk = mptcp_sk(sk);
2893 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2894 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2895 msk->snd_nxt, msk->write_seq);
2897 /* we still need to enqueue subflows or not really shutting down,
2900 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2901 mptcp_send_head(sk))
2904 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2906 mptcp_for_each_subflow(msk, subflow) {
2907 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2909 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2913 static void __mptcp_wr_shutdown(struct sock *sk)
2915 struct mptcp_sock *msk = mptcp_sk(sk);
2917 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2918 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2919 !!mptcp_send_head(sk));
2921 /* will be ignored by fallback sockets */
2922 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2923 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2925 mptcp_check_send_data_fin(sk);
2928 static void __mptcp_destroy_sock(struct sock *sk)
2930 struct mptcp_sock *msk = mptcp_sk(sk);
2932 pr_debug("msk=%p", msk);
2936 mptcp_stop_timer(sk);
2937 sk_stop_timer(sk, &sk->sk_timer);
2939 mptcp_release_sched(msk);
2941 sk->sk_prot->destroy(sk);
2943 WARN_ON_ONCE(msk->rmem_fwd_alloc);
2944 WARN_ON_ONCE(msk->rmem_released);
2945 sk_stream_kill_queues(sk);
2946 xfrm_sk_free_policy(sk);
2951 void __mptcp_unaccepted_force_close(struct sock *sk)
2953 sock_set_flag(sk, SOCK_DEAD);
2954 mptcp_do_fastclose(sk);
2955 __mptcp_destroy_sock(sk);
2958 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
2960 /* Concurrent splices from sk_receive_queue into receive_queue will
2961 * always show at least one non-empty queue when checked in this order.
2963 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
2964 skb_queue_empty_lockless(&msk->receive_queue))
2967 return EPOLLIN | EPOLLRDNORM;
2970 static void mptcp_check_listen_stop(struct sock *sk)
2974 if (inet_sk_state_load(sk) != TCP_LISTEN)
2977 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
2978 ssk = mptcp_sk(sk)->first;
2979 if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
2982 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2983 tcp_set_state(ssk, TCP_CLOSE);
2984 mptcp_subflow_queue_clean(sk, ssk);
2985 inet_csk_listen_stop(ssk);
2986 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
2990 bool __mptcp_close(struct sock *sk, long timeout)
2992 struct mptcp_subflow_context *subflow;
2993 struct mptcp_sock *msk = mptcp_sk(sk);
2994 bool do_cancel_work = false;
2995 int subflows_alive = 0;
2997 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2999 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
3000 mptcp_check_listen_stop(sk);
3001 inet_sk_state_store(sk, TCP_CLOSE);
3005 if (mptcp_check_readable(msk) || timeout < 0) {
3006 /* If the msk has read data, or the caller explicitly ask it,
3007 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
3009 mptcp_do_fastclose(sk);
3011 } else if (mptcp_close_state(sk)) {
3012 __mptcp_wr_shutdown(sk);
3015 sk_stream_wait_close(sk, timeout);
3018 /* orphan all the subflows */
3019 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
3020 mptcp_for_each_subflow(msk, subflow) {
3021 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3022 bool slow = lock_sock_fast_nested(ssk);
3024 subflows_alive += ssk->sk_state != TCP_CLOSE;
3026 /* since the close timeout takes precedence on the fail one,
3029 if (ssk == msk->first)
3030 subflow->fail_tout = 0;
3032 /* detach from the parent socket, but allow data_ready to
3033 * push incoming data into the mptcp stack, to properly ack it
3035 ssk->sk_socket = NULL;
3037 unlock_sock_fast(ssk, slow);
3041 /* all the subflows are closed, only timeout can change the msk
3042 * state, let's not keep resources busy for no reasons
3044 if (subflows_alive == 0)
3045 inet_sk_state_store(sk, TCP_CLOSE);
3048 pr_debug("msk=%p state=%d", sk, sk->sk_state);
3050 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3052 if (sk->sk_state == TCP_CLOSE) {
3053 __mptcp_destroy_sock(sk);
3054 do_cancel_work = true;
3056 mptcp_reset_timeout(msk, 0);
3059 return do_cancel_work;
3062 static void mptcp_close(struct sock *sk, long timeout)
3064 bool do_cancel_work;
3068 do_cancel_work = __mptcp_close(sk, timeout);
3071 mptcp_cancel_work(sk);
3076 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3078 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3079 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3080 struct ipv6_pinfo *msk6 = inet6_sk(msk);
3082 msk->sk_v6_daddr = ssk->sk_v6_daddr;
3083 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3086 msk6->saddr = ssk6->saddr;
3087 msk6->flow_label = ssk6->flow_label;
3091 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3092 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3093 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3094 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3095 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3096 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3099 static int mptcp_disconnect(struct sock *sk, int flags)
3101 struct mptcp_sock *msk = mptcp_sk(sk);
3103 /* Deny disconnect if other threads are blocked in sk_wait_event()
3104 * or inet_wait_for_connect().
3106 if (sk->sk_wait_pending)
3109 /* We are on the fastopen error path. We can't call straight into the
3110 * subflows cleanup code due to lock nesting (we are already under
3111 * msk->firstsocket lock).
3113 if (msk->fastopening)
3116 mptcp_check_listen_stop(sk);
3117 inet_sk_state_store(sk, TCP_CLOSE);
3119 mptcp_stop_timer(sk);
3120 sk_stop_timer(sk, &sk->sk_timer);
3123 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3125 /* msk->subflow is still intact, the following will not free the first
3128 mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3129 WRITE_ONCE(msk->flags, 0);
3131 msk->push_pending = 0;
3132 msk->recovery = false;
3133 msk->can_ack = false;
3134 msk->fully_established = false;
3135 msk->rcv_data_fin = false;
3136 msk->snd_data_fin_enable = false;
3137 msk->rcv_fastclose = false;
3138 msk->use_64bit_ack = false;
3139 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3140 mptcp_pm_data_reset(msk);
3142 msk->bytes_acked = 0;
3143 msk->bytes_received = 0;
3144 msk->bytes_sent = 0;
3145 msk->bytes_retrans = 0;
3147 WRITE_ONCE(sk->sk_shutdown, 0);
3148 sk_error_report(sk);
3152 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3153 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3155 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3157 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3161 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3162 const struct mptcp_options_received *mp_opt,
3164 struct request_sock *req)
3166 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3167 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3168 struct mptcp_sock *msk;
3173 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3174 if (nsk->sk_family == AF_INET6)
3175 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3178 nsk->sk_wait_pending = 0;
3179 __mptcp_init_sock(nsk);
3181 msk = mptcp_sk(nsk);
3182 msk->local_key = subflow_req->local_key;
3183 msk->token = subflow_req->token;
3184 msk->in_accept_queue = 1;
3185 WRITE_ONCE(msk->fully_established, false);
3186 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3187 WRITE_ONCE(msk->csum_enabled, true);
3189 msk->write_seq = subflow_req->idsn + 1;
3190 msk->snd_nxt = msk->write_seq;
3191 msk->snd_una = msk->write_seq;
3192 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
3193 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3194 mptcp_init_sched(msk, mptcp_sk(sk)->sched);
3196 /* passive msk is created after the first/MPC subflow */
3197 msk->subflow_id = 2;
3199 sock_reset_flag(nsk, SOCK_RCU_FREE);
3200 security_inet_csk_clone(nsk, req);
3202 /* this can't race with mptcp_close(), as the msk is
3203 * not yet exposted to user-space
3205 inet_sk_state_store(nsk, TCP_ESTABLISHED);
3207 /* The msk maintain a ref to each subflow in the connections list */
3208 WRITE_ONCE(msk->first, ssk);
3209 list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list);
3212 /* new mpc subflow takes ownership of the newly
3213 * created mptcp socket
3215 mptcp_token_accept(subflow_req, msk);
3217 /* set msk addresses early to ensure mptcp_pm_get_local_id()
3218 * uses the correct data
3220 mptcp_copy_inaddrs(nsk, ssk);
3221 mptcp_propagate_sndbuf(nsk, ssk);
3223 mptcp_rcv_space_init(msk, ssk);
3224 bh_unlock_sock(nsk);
3226 /* note: the newly allocated socket refcount is 2 now */
3230 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3232 const struct tcp_sock *tp = tcp_sk(ssk);
3234 msk->rcvq_space.copied = 0;
3235 msk->rcvq_space.rtt_us = 0;
3237 msk->rcvq_space.time = tp->tcp_mstamp;
3239 /* initial rcv_space offering made to peer */
3240 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3241 TCP_INIT_CWND * tp->advmss);
3242 if (msk->rcvq_space.space == 0)
3243 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3245 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3248 static struct sock *mptcp_accept(struct sock *ssk, int flags, int *err,
3253 pr_debug("ssk=%p, listener=%p", ssk, mptcp_subflow_ctx(ssk));
3254 newsk = inet_csk_accept(ssk, flags, err, kern);
3258 pr_debug("newsk=%p, subflow is mptcp=%d", newsk, sk_is_mptcp(newsk));
3259 if (sk_is_mptcp(newsk)) {
3260 struct mptcp_subflow_context *subflow;
3261 struct sock *new_mptcp_sock;
3263 subflow = mptcp_subflow_ctx(newsk);
3264 new_mptcp_sock = subflow->conn;
3266 /* is_mptcp should be false if subflow->conn is missing, see
3267 * subflow_syn_recv_sock()
3269 if (WARN_ON_ONCE(!new_mptcp_sock)) {
3270 tcp_sk(newsk)->is_mptcp = 0;
3274 newsk = new_mptcp_sock;
3275 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3277 MPTCP_INC_STATS(sock_net(ssk),
3278 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3282 newsk->sk_kern_sock = kern;
3286 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
3288 struct mptcp_subflow_context *subflow, *tmp;
3289 struct sock *sk = (struct sock *)msk;
3291 __mptcp_clear_xmit(sk);
3293 /* join list will be eventually flushed (with rst) at sock lock release time */
3294 mptcp_for_each_subflow_safe(msk, subflow, tmp)
3295 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3297 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3298 mptcp_data_lock(sk);
3299 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
3300 __skb_queue_purge(&sk->sk_receive_queue);
3301 skb_rbtree_purge(&msk->out_of_order_queue);
3302 mptcp_data_unlock(sk);
3304 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3305 * inet_sock_destruct() will dispose it
3307 sk_forward_alloc_add(sk, msk->rmem_fwd_alloc);
3308 WRITE_ONCE(msk->rmem_fwd_alloc, 0);
3309 mptcp_token_destroy(msk);
3310 mptcp_pm_free_anno_list(msk);
3311 mptcp_free_local_addr_list(msk);
3314 static void mptcp_destroy(struct sock *sk)
3316 struct mptcp_sock *msk = mptcp_sk(sk);
3318 /* allow the following to close even the initial subflow */
3319 msk->free_first = 1;
3320 mptcp_destroy_common(msk, 0);
3321 sk_sockets_allocated_dec(sk);
3324 void __mptcp_data_acked(struct sock *sk)
3326 if (!sock_owned_by_user(sk))
3327 __mptcp_clean_una(sk);
3329 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3331 if (mptcp_pending_data_fin_ack(sk))
3332 mptcp_schedule_work(sk);
3335 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3337 if (!mptcp_send_head(sk))
3340 if (!sock_owned_by_user(sk))
3341 __mptcp_subflow_push_pending(sk, ssk, false);
3343 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3346 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3347 BIT(MPTCP_RETRANSMIT) | \
3348 BIT(MPTCP_FLUSH_JOIN_LIST))
3350 /* processes deferred events and flush wmem */
3351 static void mptcp_release_cb(struct sock *sk)
3352 __must_hold(&sk->sk_lock.slock)
3354 struct mptcp_sock *msk = mptcp_sk(sk);
3357 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) |
3359 struct list_head join_list;
3364 INIT_LIST_HEAD(&join_list);
3365 list_splice_init(&msk->join_list, &join_list);
3367 /* the following actions acquire the subflow socket lock
3369 * 1) can't be invoked in atomic scope
3370 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3371 * datapath acquires the msk socket spinlock while helding
3372 * the subflow socket lock
3374 msk->push_pending = 0;
3375 msk->cb_flags &= ~flags;
3376 spin_unlock_bh(&sk->sk_lock.slock);
3378 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3379 __mptcp_flush_join_list(sk, &join_list);
3380 if (flags & BIT(MPTCP_PUSH_PENDING))
3381 __mptcp_push_pending(sk, 0);
3382 if (flags & BIT(MPTCP_RETRANSMIT))
3383 __mptcp_retrans(sk);
3386 spin_lock_bh(&sk->sk_lock.slock);
3389 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3390 __mptcp_clean_una_wakeup(sk);
3391 if (unlikely(msk->cb_flags)) {
3392 /* be sure to set the current sk state before tacking actions
3393 * depending on sk_state, that is processing MPTCP_ERROR_REPORT
3395 if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags))
3396 __mptcp_set_connected(sk);
3397 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3398 __mptcp_error_report(sk);
3401 __mptcp_update_rmem(sk);
3404 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3405 * TCP can't schedule delack timer before the subflow is fully established.
3406 * MPTCP uses the delack timer to do 3rd ack retransmissions
3408 static void schedule_3rdack_retransmission(struct sock *ssk)
3410 struct inet_connection_sock *icsk = inet_csk(ssk);
3411 struct tcp_sock *tp = tcp_sk(ssk);
3412 unsigned long timeout;
3414 if (mptcp_subflow_ctx(ssk)->fully_established)
3417 /* reschedule with a timeout above RTT, as we must look only for drop */
3419 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3421 timeout = TCP_TIMEOUT_INIT;
3424 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3425 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3426 icsk->icsk_ack.timeout = timeout;
3427 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3430 void mptcp_subflow_process_delegated(struct sock *ssk)
3432 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3433 struct sock *sk = subflow->conn;
3435 if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) {
3436 mptcp_data_lock(sk);
3437 if (!sock_owned_by_user(sk))
3438 __mptcp_subflow_push_pending(sk, ssk, true);
3440 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3441 mptcp_data_unlock(sk);
3442 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND);
3444 if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) {
3445 schedule_3rdack_retransmission(ssk);
3446 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK);
3450 static int mptcp_hash(struct sock *sk)
3452 /* should never be called,
3453 * we hash the TCP subflows not the master socket
3459 static void mptcp_unhash(struct sock *sk)
3461 /* called from sk_common_release(), but nothing to do here */
3464 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3466 struct mptcp_sock *msk = mptcp_sk(sk);
3468 pr_debug("msk=%p, ssk=%p", msk, msk->first);
3469 if (WARN_ON_ONCE(!msk->first))
3472 return inet_csk_get_port(msk->first, snum);
3475 void mptcp_finish_connect(struct sock *ssk)
3477 struct mptcp_subflow_context *subflow;
3478 struct mptcp_sock *msk;
3481 subflow = mptcp_subflow_ctx(ssk);
3485 pr_debug("msk=%p, token=%u", sk, subflow->token);
3487 subflow->map_seq = subflow->iasn;
3488 subflow->map_subflow_seq = 1;
3490 /* the socket is not connected yet, no msk/subflow ops can access/race
3491 * accessing the field below
3493 WRITE_ONCE(msk->local_key, subflow->local_key);
3494 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3495 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3496 WRITE_ONCE(msk->snd_una, msk->write_seq);
3498 mptcp_pm_new_connection(msk, ssk, 0);
3500 mptcp_rcv_space_init(msk, ssk);
3503 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3505 write_lock_bh(&sk->sk_callback_lock);
3506 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3507 sk_set_socket(sk, parent);
3508 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3509 write_unlock_bh(&sk->sk_callback_lock);
3512 bool mptcp_finish_join(struct sock *ssk)
3514 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3515 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3516 struct sock *parent = (void *)msk;
3519 pr_debug("msk=%p, subflow=%p", msk, subflow);
3521 /* mptcp socket already closing? */
3522 if (!mptcp_is_fully_established(parent)) {
3523 subflow->reset_reason = MPTCP_RST_EMPTCP;
3527 /* active subflow, already present inside the conn_list */
3528 if (!list_empty(&subflow->node)) {
3529 mptcp_subflow_joined(msk, ssk);
3533 if (!mptcp_pm_allow_new_subflow(msk))
3534 goto err_prohibited;
3536 /* If we can't acquire msk socket lock here, let the release callback
3539 mptcp_data_lock(parent);
3540 if (!sock_owned_by_user(parent)) {
3541 ret = __mptcp_finish_join(msk, ssk);
3544 list_add_tail(&subflow->node, &msk->conn_list);
3548 list_add_tail(&subflow->node, &msk->join_list);
3549 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3551 mptcp_data_unlock(parent);
3555 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3562 static void mptcp_shutdown(struct sock *sk, int how)
3564 pr_debug("sk=%p, how=%d", sk, how);
3566 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3567 __mptcp_wr_shutdown(sk);
3570 static int mptcp_forward_alloc_get(const struct sock *sk)
3572 return READ_ONCE(sk->sk_forward_alloc) +
3573 READ_ONCE(mptcp_sk(sk)->rmem_fwd_alloc);
3576 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3578 const struct sock *sk = (void *)msk;
3581 if (sk->sk_state == TCP_LISTEN)
3584 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3587 delta = msk->write_seq - v;
3588 if (__mptcp_check_fallback(msk) && msk->first) {
3589 struct tcp_sock *tp = tcp_sk(msk->first);
3591 /* the first subflow is disconnected after close - see
3592 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3593 * so ignore that status, too.
3595 if (!((1 << msk->first->sk_state) &
3596 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3597 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3599 if (delta > INT_MAX)
3605 static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3607 struct mptcp_sock *msk = mptcp_sk(sk);
3612 if (sk->sk_state == TCP_LISTEN)
3616 __mptcp_move_skbs(msk);
3617 *karg = mptcp_inq_hint(sk);
3621 slow = lock_sock_fast(sk);
3622 *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3623 unlock_sock_fast(sk, slow);
3626 slow = lock_sock_fast(sk);
3627 *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3628 unlock_sock_fast(sk, slow);
3631 return -ENOIOCTLCMD;
3637 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3638 struct mptcp_subflow_context *subflow)
3640 subflow->request_mptcp = 0;
3641 __mptcp_do_fallback(msk);
3644 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3646 struct mptcp_subflow_context *subflow;
3647 struct mptcp_sock *msk = mptcp_sk(sk);
3651 ssk = __mptcp_nmpc_sk(msk);
3653 return PTR_ERR(ssk);
3655 inet_sk_state_store(sk, TCP_SYN_SENT);
3656 subflow = mptcp_subflow_ctx(ssk);
3657 #ifdef CONFIG_TCP_MD5SIG
3658 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3661 if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
3662 mptcp_subflow_early_fallback(msk, subflow);
3664 if (subflow->request_mptcp && mptcp_token_new_connect(ssk)) {
3665 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT);
3666 mptcp_subflow_early_fallback(msk, subflow);
3668 if (likely(!__mptcp_check_fallback(msk)))
3669 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3671 /* if reaching here via the fastopen/sendmsg path, the caller already
3672 * acquired the subflow socket lock, too.
3674 if (!msk->fastopening)
3677 /* the following mirrors closely a very small chunk of code from
3678 * __inet_stream_connect()
3680 if (ssk->sk_state != TCP_CLOSE)
3683 if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
3684 err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3689 err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3693 inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3696 if (!msk->fastopening)
3699 /* on successful connect, the msk state will be moved to established by
3700 * subflow_finish_connect()
3702 if (unlikely(err)) {
3703 /* avoid leaving a dangling token in an unconnected socket */
3704 mptcp_token_destroy(msk);
3705 inet_sk_state_store(sk, TCP_CLOSE);
3709 mptcp_copy_inaddrs(sk, ssk);
3713 static struct proto mptcp_prot = {
3715 .owner = THIS_MODULE,
3716 .init = mptcp_init_sock,
3717 .connect = mptcp_connect,
3718 .disconnect = mptcp_disconnect,
3719 .close = mptcp_close,
3720 .accept = mptcp_accept,
3721 .setsockopt = mptcp_setsockopt,
3722 .getsockopt = mptcp_getsockopt,
3723 .shutdown = mptcp_shutdown,
3724 .destroy = mptcp_destroy,
3725 .sendmsg = mptcp_sendmsg,
3726 .ioctl = mptcp_ioctl,
3727 .recvmsg = mptcp_recvmsg,
3728 .release_cb = mptcp_release_cb,
3730 .unhash = mptcp_unhash,
3731 .get_port = mptcp_get_port,
3732 .forward_alloc_get = mptcp_forward_alloc_get,
3733 .sockets_allocated = &mptcp_sockets_allocated,
3735 .memory_allocated = &tcp_memory_allocated,
3736 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3738 .memory_pressure = &tcp_memory_pressure,
3739 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3740 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3741 .sysctl_mem = sysctl_tcp_mem,
3742 .obj_size = sizeof(struct mptcp_sock),
3743 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3744 .no_autobind = true,
3747 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3749 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3750 struct sock *ssk, *sk = sock->sk;
3754 ssk = __mptcp_nmpc_sk(msk);
3760 if (sk->sk_family == AF_INET)
3761 err = inet_bind_sk(ssk, uaddr, addr_len);
3762 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3763 else if (sk->sk_family == AF_INET6)
3764 err = inet6_bind_sk(ssk, uaddr, addr_len);
3767 mptcp_copy_inaddrs(sk, ssk);
3774 static int mptcp_listen(struct socket *sock, int backlog)
3776 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3777 struct sock *sk = sock->sk;
3781 pr_debug("msk=%p", msk);
3786 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
3789 ssk = __mptcp_nmpc_sk(msk);
3795 inet_sk_state_store(sk, TCP_LISTEN);
3796 sock_set_flag(sk, SOCK_RCU_FREE);
3799 err = __inet_listen_sk(ssk, backlog);
3801 inet_sk_state_store(sk, inet_sk_state_load(ssk));
3804 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3805 mptcp_copy_inaddrs(sk, ssk);
3806 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
3814 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3815 int flags, bool kern)
3817 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3818 struct sock *ssk, *newsk;
3821 pr_debug("msk=%p", msk);
3823 /* Buggy applications can call accept on socket states other then LISTEN
3824 * but no need to allocate the first subflow just to error out.
3826 ssk = READ_ONCE(msk->first);
3830 newsk = mptcp_accept(ssk, flags, &err, kern);
3836 __inet_accept(sock, newsock, newsk);
3837 if (!mptcp_is_tcpsk(newsock->sk)) {
3838 struct mptcp_sock *msk = mptcp_sk(newsk);
3839 struct mptcp_subflow_context *subflow;
3841 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3842 msk->in_accept_queue = 0;
3844 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3845 * This is needed so NOSPACE flag can be set from tcp stack.
3847 mptcp_for_each_subflow(msk, subflow) {
3848 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3850 if (!ssk->sk_socket)
3851 mptcp_sock_graft(ssk, newsock);
3854 /* Do late cleanup for the first subflow as necessary. Also
3855 * deal with bad peers not doing a complete shutdown.
3857 if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
3858 __mptcp_close_ssk(newsk, msk->first,
3859 mptcp_subflow_ctx(msk->first), 0);
3860 if (unlikely(list_is_singular(&msk->conn_list)))
3861 inet_sk_state_store(newsk, TCP_CLOSE);
3864 release_sock(newsk);
3869 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3871 struct sock *sk = (struct sock *)msk;
3873 if (sk_stream_is_writeable(sk))
3874 return EPOLLOUT | EPOLLWRNORM;
3876 mptcp_set_nospace(sk);
3877 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3878 if (sk_stream_is_writeable(sk))
3879 return EPOLLOUT | EPOLLWRNORM;
3884 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3885 struct poll_table_struct *wait)
3887 struct sock *sk = sock->sk;
3888 struct mptcp_sock *msk;
3894 sock_poll_wait(file, sock, wait);
3896 state = inet_sk_state_load(sk);
3897 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
3898 if (state == TCP_LISTEN) {
3899 struct sock *ssk = READ_ONCE(msk->first);
3901 if (WARN_ON_ONCE(!ssk))
3904 return inet_csk_listen_poll(ssk);
3907 shutdown = READ_ONCE(sk->sk_shutdown);
3908 if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3910 if (shutdown & RCV_SHUTDOWN)
3911 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3913 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3914 mask |= mptcp_check_readable(msk);
3915 if (shutdown & SEND_SHUTDOWN)
3916 mask |= EPOLLOUT | EPOLLWRNORM;
3918 mask |= mptcp_check_writeable(msk);
3919 } else if (state == TCP_SYN_SENT &&
3920 inet_test_bit(DEFER_CONNECT, sk)) {
3921 /* cf tcp_poll() note about TFO */
3922 mask |= EPOLLOUT | EPOLLWRNORM;
3925 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
3927 if (READ_ONCE(sk->sk_err))
3933 static const struct proto_ops mptcp_stream_ops = {
3935 .owner = THIS_MODULE,
3936 .release = inet_release,
3938 .connect = inet_stream_connect,
3939 .socketpair = sock_no_socketpair,
3940 .accept = mptcp_stream_accept,
3941 .getname = inet_getname,
3943 .ioctl = inet_ioctl,
3944 .gettstamp = sock_gettstamp,
3945 .listen = mptcp_listen,
3946 .shutdown = inet_shutdown,
3947 .setsockopt = sock_common_setsockopt,
3948 .getsockopt = sock_common_getsockopt,
3949 .sendmsg = inet_sendmsg,
3950 .recvmsg = inet_recvmsg,
3951 .mmap = sock_no_mmap,
3954 static struct inet_protosw mptcp_protosw = {
3955 .type = SOCK_STREAM,
3956 .protocol = IPPROTO_MPTCP,
3957 .prot = &mptcp_prot,
3958 .ops = &mptcp_stream_ops,
3959 .flags = INET_PROTOSW_ICSK,
3962 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3964 struct mptcp_delegated_action *delegated;
3965 struct mptcp_subflow_context *subflow;
3968 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3969 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3970 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3972 bh_lock_sock_nested(ssk);
3973 if (!sock_owned_by_user(ssk) &&
3974 mptcp_subflow_has_delegated_action(subflow))
3975 mptcp_subflow_process_delegated(ssk);
3976 /* ... elsewhere tcp_release_cb_override already processed
3977 * the action or will do at next release_sock().
3978 * In both case must dequeue the subflow here - on the same
3979 * CPU that scheduled it.
3981 bh_unlock_sock(ssk);
3984 if (++work_done == budget)
3988 /* always provide a 0 'work_done' argument, so that napi_complete_done
3989 * will not try accessing the NULL napi->dev ptr
3991 napi_complete_done(napi, 0);
3995 void __init mptcp_proto_init(void)
3997 struct mptcp_delegated_action *delegated;
4000 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
4002 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4003 panic("Failed to allocate MPTCP pcpu counter\n");
4005 init_dummy_netdev(&mptcp_napi_dev);
4006 for_each_possible_cpu(cpu) {
4007 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4008 INIT_LIST_HEAD(&delegated->head);
4009 netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
4011 napi_enable(&delegated->napi);
4014 mptcp_subflow_init();
4019 if (proto_register(&mptcp_prot, 1) != 0)
4020 panic("Failed to register MPTCP proto.\n");
4022 inet_register_protosw(&mptcp_protosw);
4024 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4027 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4028 static const struct proto_ops mptcp_v6_stream_ops = {
4030 .owner = THIS_MODULE,
4031 .release = inet6_release,
4033 .connect = inet_stream_connect,
4034 .socketpair = sock_no_socketpair,
4035 .accept = mptcp_stream_accept,
4036 .getname = inet6_getname,
4038 .ioctl = inet6_ioctl,
4039 .gettstamp = sock_gettstamp,
4040 .listen = mptcp_listen,
4041 .shutdown = inet_shutdown,
4042 .setsockopt = sock_common_setsockopt,
4043 .getsockopt = sock_common_getsockopt,
4044 .sendmsg = inet6_sendmsg,
4045 .recvmsg = inet6_recvmsg,
4046 .mmap = sock_no_mmap,
4047 #ifdef CONFIG_COMPAT
4048 .compat_ioctl = inet6_compat_ioctl,
4052 static struct proto mptcp_v6_prot;
4054 static struct inet_protosw mptcp_v6_protosw = {
4055 .type = SOCK_STREAM,
4056 .protocol = IPPROTO_MPTCP,
4057 .prot = &mptcp_v6_prot,
4058 .ops = &mptcp_v6_stream_ops,
4059 .flags = INET_PROTOSW_ICSK,
4062 int __init mptcp_proto_v6_init(void)
4066 mptcp_v6_prot = mptcp_prot;
4067 strcpy(mptcp_v6_prot.name, "MPTCPv6");
4068 mptcp_v6_prot.slab = NULL;
4069 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4070 mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4072 err = proto_register(&mptcp_v6_prot, 1);
4076 err = inet6_register_protosw(&mptcp_v6_protosw);
4078 proto_unregister(&mptcp_v6_prot);