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>
27 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
29 struct mptcp_sock msk;
40 #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
42 static struct percpu_counter mptcp_sockets_allocated;
44 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
45 * completed yet or has failed, return the subflow socket.
46 * Otherwise return NULL.
48 static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
50 if (!msk->subflow || READ_ONCE(msk->can_ack))
56 static bool mptcp_is_tcpsk(struct sock *sk)
58 struct socket *sock = sk->sk_socket;
60 if (unlikely(sk->sk_prot == &tcp_prot)) {
61 /* we are being invoked after mptcp_accept() has
62 * accepted a non-mp-capable flow: sk is a tcp_sk,
65 * Hand the socket over to tcp so all further socket ops
68 sock->ops = &inet_stream_ops;
70 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
71 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
72 sock->ops = &inet6_stream_ops;
80 static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk)
82 sock_owned_by_me((const struct sock *)msk);
84 if (likely(!__mptcp_check_fallback(msk)))
90 static int __mptcp_socket_create(struct mptcp_sock *msk)
92 struct mptcp_subflow_context *subflow;
93 struct sock *sk = (struct sock *)msk;
97 err = mptcp_subflow_create_socket(sk, &ssock);
101 msk->first = ssock->sk;
102 msk->subflow = ssock;
103 subflow = mptcp_subflow_ctx(ssock->sk);
104 list_add(&subflow->node, &msk->conn_list);
105 subflow->request_mptcp = 1;
107 /* accept() will wait on first subflow sk_wq, and we always wakes up
110 RCU_INIT_POINTER(msk->first->sk_wq, &sk->sk_socket->wq);
115 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
117 sk_drops_add(sk, skb);
121 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
122 struct sk_buff *from)
127 if (MPTCP_SKB_CB(from)->offset ||
128 !skb_try_coalesce(to, from, &fragstolen, &delta))
131 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
132 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
133 to->len, MPTCP_SKB_CB(from)->end_seq);
134 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
135 kfree_skb_partial(from, fragstolen);
136 atomic_add(delta, &sk->sk_rmem_alloc);
137 sk_mem_charge(sk, delta);
141 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
142 struct sk_buff *from)
144 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
147 return mptcp_try_coalesce((struct sock *)msk, to, from);
150 /* "inspired" by tcp_data_queue_ofo(), main differences:
152 * - don't cope with sacks
154 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
156 struct sock *sk = (struct sock *)msk;
157 struct rb_node **p, *parent;
158 u64 seq, end_seq, max_seq;
159 struct sk_buff *skb1;
162 seq = MPTCP_SKB_CB(skb)->map_seq;
163 end_seq = MPTCP_SKB_CB(skb)->end_seq;
164 space = tcp_space(sk);
165 max_seq = space > 0 ? space + msk->ack_seq : msk->ack_seq;
167 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
168 RB_EMPTY_ROOT(&msk->out_of_order_queue));
169 if (after64(seq, max_seq)) {
172 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
176 p = &msk->out_of_order_queue.rb_node;
177 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
178 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
179 rb_link_node(&skb->rbnode, NULL, p);
180 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
181 msk->ooo_last_skb = skb;
185 /* with 2 subflows, adding at end of ooo queue is quite likely
186 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
188 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
189 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
190 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
194 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
195 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
196 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
197 parent = &msk->ooo_last_skb->rbnode;
198 p = &parent->rb_right;
202 /* Find place to insert this segment. Handle overlaps on the way. */
206 skb1 = rb_to_skb(parent);
207 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
208 p = &parent->rb_left;
211 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
212 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
213 /* All the bits are present. Drop. */
215 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
218 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
222 * continue traversing
225 /* skb's seq == skb1's seq and skb covers skb1.
226 * Replace skb1 with skb.
228 rb_replace_node(&skb1->rbnode, &skb->rbnode,
229 &msk->out_of_order_queue);
230 mptcp_drop(sk, skb1);
231 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
234 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
235 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
238 p = &parent->rb_right;
242 /* Insert segment into RB tree. */
243 rb_link_node(&skb->rbnode, parent, p);
244 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
247 /* Remove other segments covered by skb. */
248 while ((skb1 = skb_rb_next(skb)) != NULL) {
249 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
251 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
252 mptcp_drop(sk, skb1);
253 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
255 /* If there is no skb after us, we are the last_skb ! */
257 msk->ooo_last_skb = skb;
261 skb_set_owner_r(skb, sk);
264 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
265 struct sk_buff *skb, unsigned int offset,
268 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
269 struct sock *sk = (struct sock *)msk;
270 struct sk_buff *tail;
272 __skb_unlink(skb, &ssk->sk_receive_queue);
277 /* try to fetch required memory from subflow */
278 if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
279 if (ssk->sk_forward_alloc < skb->truesize)
281 __sk_mem_reclaim(ssk, skb->truesize);
282 if (!sk_rmem_schedule(sk, skb, skb->truesize))
286 /* the skb map_seq accounts for the skb offset:
287 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
290 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
291 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
292 MPTCP_SKB_CB(skb)->offset = offset;
294 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
296 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
297 tail = skb_peek_tail(&sk->sk_receive_queue);
298 if (tail && mptcp_try_coalesce(sk, tail, skb))
301 skb_set_owner_r(skb, sk);
302 __skb_queue_tail(&sk->sk_receive_queue, skb);
304 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
305 mptcp_data_queue_ofo(msk, skb);
309 /* old data, keep it simple and drop the whole pkt, sender
310 * will retransmit as needed, if needed.
312 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
318 static void mptcp_stop_timer(struct sock *sk)
320 struct inet_connection_sock *icsk = inet_csk(sk);
322 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
323 mptcp_sk(sk)->timer_ival = 0;
326 static void mptcp_check_data_fin_ack(struct sock *sk)
328 struct mptcp_sock *msk = mptcp_sk(sk);
330 if (__mptcp_check_fallback(msk))
333 /* Look for an acknowledged DATA_FIN */
334 if (((1 << sk->sk_state) &
335 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
336 msk->write_seq == atomic64_read(&msk->snd_una)) {
337 mptcp_stop_timer(sk);
339 WRITE_ONCE(msk->snd_data_fin_enable, 0);
341 switch (sk->sk_state) {
343 inet_sk_state_store(sk, TCP_FIN_WAIT2);
344 sk->sk_state_change(sk);
348 inet_sk_state_store(sk, TCP_CLOSE);
349 sk->sk_state_change(sk);
353 if (sk->sk_shutdown == SHUTDOWN_MASK ||
354 sk->sk_state == TCP_CLOSE)
355 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
357 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
361 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
363 struct mptcp_sock *msk = mptcp_sk(sk);
365 if (READ_ONCE(msk->rcv_data_fin) &&
366 ((1 << sk->sk_state) &
367 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
368 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
370 if (msk->ack_seq == rcv_data_fin_seq) {
372 *seq = rcv_data_fin_seq;
381 static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
383 long tout = ssk && inet_csk(ssk)->icsk_pending ?
384 inet_csk(ssk)->icsk_timeout - jiffies : 0;
387 tout = mptcp_sk(sk)->timer_ival;
388 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
391 static void mptcp_check_data_fin(struct sock *sk)
393 struct mptcp_sock *msk = mptcp_sk(sk);
394 u64 rcv_data_fin_seq;
396 if (__mptcp_check_fallback(msk) || !msk->first)
399 /* Need to ack a DATA_FIN received from a peer while this side
400 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
401 * msk->rcv_data_fin was set when parsing the incoming options
402 * at the subflow level and the msk lock was not held, so this
403 * is the first opportunity to act on the DATA_FIN and change
406 * If we are caught up to the sequence number of the incoming
407 * DATA_FIN, send the DATA_ACK now and do state transition. If
408 * not caught up, do nothing and let the recv code send DATA_ACK
412 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
413 struct mptcp_subflow_context *subflow;
415 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
416 WRITE_ONCE(msk->rcv_data_fin, 0);
418 sk->sk_shutdown |= RCV_SHUTDOWN;
419 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
420 set_bit(MPTCP_DATA_READY, &msk->flags);
422 switch (sk->sk_state) {
423 case TCP_ESTABLISHED:
424 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
427 inet_sk_state_store(sk, TCP_CLOSING);
430 inet_sk_state_store(sk, TCP_CLOSE);
431 // @@ Close subflows now?
434 /* Other states not expected */
439 mptcp_set_timeout(sk, NULL);
440 mptcp_for_each_subflow(msk, subflow) {
441 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
448 sk->sk_state_change(sk);
450 if (sk->sk_shutdown == SHUTDOWN_MASK ||
451 sk->sk_state == TCP_CLOSE)
452 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
454 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
458 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
462 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
463 struct sock *sk = (struct sock *)msk;
464 unsigned int moved = 0;
465 bool more_data_avail;
470 pr_debug("msk=%p ssk=%p", msk, ssk);
472 old_copied_seq = tp->copied_seq;
474 u32 map_remaining, offset;
475 u32 seq = tp->copied_seq;
479 /* try to move as much data as available */
480 map_remaining = subflow->map_data_len -
481 mptcp_subflow_get_map_offset(subflow);
483 skb = skb_peek(&ssk->sk_receive_queue);
485 /* if no data is found, a racing workqueue/recvmsg
486 * already processed the new data, stop here or we
487 * can enter an infinite loop
494 if (__mptcp_check_fallback(msk)) {
495 /* if we are running under the workqueue, TCP could have
496 * collapsed skbs between dummy map creation and now
497 * be sure to adjust the size
499 map_remaining = skb->len;
500 subflow->map_data_len = skb->len;
503 offset = seq - TCP_SKB_CB(skb)->seq;
504 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
510 if (offset < skb->len) {
511 size_t len = skb->len - offset;
516 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
520 if (WARN_ON_ONCE(map_remaining < len))
524 sk_eat_skb(ssk, skb);
528 WRITE_ONCE(tp->copied_seq, seq);
529 more_data_avail = mptcp_subflow_data_available(ssk);
531 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) {
535 } while (more_data_avail);
538 if (tp->copied_seq != old_copied_seq)
539 tcp_cleanup_rbuf(ssk, 1);
544 static bool mptcp_ofo_queue(struct mptcp_sock *msk)
546 struct sock *sk = (struct sock *)msk;
547 struct sk_buff *skb, *tail;
552 p = rb_first(&msk->out_of_order_queue);
553 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
556 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
560 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
562 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
565 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
569 end_seq = MPTCP_SKB_CB(skb)->end_seq;
570 tail = skb_peek_tail(&sk->sk_receive_queue);
571 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
572 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
574 /* skip overlapping data, if any */
575 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
576 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
578 MPTCP_SKB_CB(skb)->offset += delta;
579 __skb_queue_tail(&sk->sk_receive_queue, skb);
581 msk->ack_seq = end_seq;
587 /* In most cases we will be able to lock the mptcp socket. If its already
588 * owned, we need to defer to the work queue to avoid ABBA deadlock.
590 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
592 struct sock *sk = (struct sock *)msk;
593 unsigned int moved = 0;
595 if (READ_ONCE(sk->sk_lock.owned))
598 if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock)))
601 /* must re-check after taking the lock */
602 if (!READ_ONCE(sk->sk_lock.owned)) {
603 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
604 mptcp_ofo_queue(msk);
606 /* If the moves have caught up with the DATA_FIN sequence number
607 * it's time to ack the DATA_FIN and change socket state, but
608 * this is not a good place to change state. Let the workqueue
611 if (mptcp_pending_data_fin(sk, NULL) &&
612 schedule_work(&msk->work))
616 spin_unlock_bh(&sk->sk_lock.slock);
621 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
623 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
624 struct mptcp_sock *msk = mptcp_sk(sk);
627 /* move_skbs_to_msk below can legitly clear the data_avail flag,
628 * but we will need later to properly woke the reader, cache its
631 wake = subflow->data_avail == MPTCP_SUBFLOW_DATA_AVAIL;
633 set_bit(MPTCP_DATA_READY, &msk->flags);
635 if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) &&
636 move_skbs_to_msk(msk, ssk))
639 /* don't schedule if mptcp sk is (still) over limit */
640 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf))
643 /* mptcp socket is owned, release_cb should retry */
644 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
645 &sk->sk_tsq_flags)) {
648 /* need to try again, its possible release_cb() has already
649 * been called after the test_and_set_bit() above.
651 move_skbs_to_msk(msk, ssk);
655 sk->sk_data_ready(sk);
658 static void __mptcp_flush_join_list(struct mptcp_sock *msk)
660 if (likely(list_empty(&msk->join_list)))
663 spin_lock_bh(&msk->join_list_lock);
664 list_splice_tail_init(&msk->join_list, &msk->conn_list);
665 spin_unlock_bh(&msk->join_list_lock);
668 static bool mptcp_timer_pending(struct sock *sk)
670 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
673 static void mptcp_reset_timer(struct sock *sk)
675 struct inet_connection_sock *icsk = inet_csk(sk);
678 /* should never be called with mptcp level timer cleared */
679 tout = READ_ONCE(mptcp_sk(sk)->timer_ival);
680 if (WARN_ON_ONCE(!tout))
682 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
685 void mptcp_data_acked(struct sock *sk)
687 mptcp_reset_timer(sk);
689 if ((!test_bit(MPTCP_SEND_SPACE, &mptcp_sk(sk)->flags) ||
690 (inet_sk_state_load(sk) != TCP_ESTABLISHED)) &&
691 schedule_work(&mptcp_sk(sk)->work))
695 void mptcp_subflow_eof(struct sock *sk)
697 struct mptcp_sock *msk = mptcp_sk(sk);
699 if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) &&
700 schedule_work(&msk->work))
704 static void mptcp_check_for_eof(struct mptcp_sock *msk)
706 struct mptcp_subflow_context *subflow;
707 struct sock *sk = (struct sock *)msk;
710 mptcp_for_each_subflow(msk, subflow)
711 receivers += !subflow->rx_eof;
713 if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
714 /* hopefully temporary hack: propagate shutdown status
715 * to msk, when all subflows agree on it
717 sk->sk_shutdown |= RCV_SHUTDOWN;
719 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
720 set_bit(MPTCP_DATA_READY, &msk->flags);
721 sk->sk_data_ready(sk);
725 static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
727 const struct sock *sk = (const struct sock *)msk;
729 if (!msk->cached_ext)
730 msk->cached_ext = __skb_ext_alloc(sk->sk_allocation);
732 return !!msk->cached_ext;
735 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
737 struct mptcp_subflow_context *subflow;
738 struct sock *sk = (struct sock *)msk;
740 sock_owned_by_me(sk);
742 mptcp_for_each_subflow(msk, subflow) {
743 if (subflow->data_avail)
744 return mptcp_subflow_tcp_sock(subflow);
750 static bool mptcp_skb_can_collapse_to(u64 write_seq,
751 const struct sk_buff *skb,
752 const struct mptcp_ext *mpext)
754 if (!tcp_skb_can_collapse_to(skb))
757 /* can collapse only if MPTCP level sequence is in order */
758 return mpext && mpext->data_seq + mpext->data_len == write_seq;
761 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
762 const struct page_frag *pfrag,
763 const struct mptcp_data_frag *df)
765 return df && pfrag->page == df->page &&
766 df->data_seq + df->data_len == msk->write_seq;
769 static void dfrag_uncharge(struct sock *sk, int len)
771 sk_mem_uncharge(sk, len);
772 sk_wmem_queued_add(sk, -len);
775 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
777 int len = dfrag->data_len + dfrag->overhead;
779 list_del(&dfrag->list);
780 dfrag_uncharge(sk, len);
781 put_page(dfrag->page);
784 static bool mptcp_is_writeable(struct mptcp_sock *msk)
786 struct mptcp_subflow_context *subflow;
788 if (!sk_stream_is_writeable((struct sock *)msk))
791 mptcp_for_each_subflow(msk, subflow) {
792 if (sk_stream_is_writeable(subflow->tcp_sock))
798 static void mptcp_clean_una(struct sock *sk)
800 struct mptcp_sock *msk = mptcp_sk(sk);
801 struct mptcp_data_frag *dtmp, *dfrag;
802 bool cleaned = false;
805 /* on fallback we just need to ignore snd_una, as this is really
808 if (__mptcp_check_fallback(msk))
809 atomic64_set(&msk->snd_una, msk->write_seq);
810 snd_una = atomic64_read(&msk->snd_una);
812 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
813 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
816 dfrag_clear(sk, dfrag);
820 dfrag = mptcp_rtx_head(sk);
821 if (dfrag && after64(snd_una, dfrag->data_seq)) {
822 u64 delta = snd_una - dfrag->data_seq;
824 if (WARN_ON_ONCE(delta > dfrag->data_len))
827 dfrag->data_seq += delta;
828 dfrag->offset += delta;
829 dfrag->data_len -= delta;
831 dfrag_uncharge(sk, delta);
837 sk_mem_reclaim_partial(sk);
839 /* Only wake up writers if a subflow is ready */
840 if (mptcp_is_writeable(msk)) {
841 set_bit(MPTCP_SEND_SPACE, &mptcp_sk(sk)->flags);
842 smp_mb__after_atomic();
844 /* set SEND_SPACE before sk_stream_write_space clears
847 sk_stream_write_space(sk);
852 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
855 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
857 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
858 pfrag, sk->sk_allocation)))
861 sk->sk_prot->enter_memory_pressure(sk);
862 sk_stream_moderate_sndbuf(sk);
866 static struct mptcp_data_frag *
867 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
870 int offset = ALIGN(orig_offset, sizeof(long));
871 struct mptcp_data_frag *dfrag;
873 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
875 dfrag->data_seq = msk->write_seq;
876 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
877 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
878 dfrag->page = pfrag->page;
883 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
884 struct msghdr *msg, struct mptcp_data_frag *dfrag,
885 long *timeo, int *pmss_now,
888 int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0;
889 bool dfrag_collapsed, can_collapse = false;
890 struct mptcp_sock *msk = mptcp_sk(sk);
891 struct mptcp_ext *mpext = NULL;
892 bool retransmission = !!dfrag;
893 struct sk_buff *skb, *tail;
894 struct page_frag *pfrag;
899 /* use the mptcp page cache so that we can easily move the data
900 * from one substream to another, but do per subflow memory accounting
901 * Note: pfrag is used only !retransmission, but the compiler if
902 * fooled into a warning if we don't init here
904 pfrag = sk_page_frag(sk);
905 if (!retransmission) {
906 write_seq = &msk->write_seq;
909 write_seq = &dfrag->data_seq;
913 /* compute copy limit */
914 mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags);
916 *ps_goal = size_goal;
917 avail_size = size_goal;
918 skb = tcp_write_queue_tail(ssk);
920 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
922 /* Limit the write to the size available in the
923 * current skb, if any, so that we create at most a new skb.
924 * Explicitly tells TCP internals to avoid collapsing on later
925 * queue management operation, to avoid breaking the ext <->
926 * SSN association set here
928 can_collapse = (size_goal - skb->len > 0) &&
929 mptcp_skb_can_collapse_to(*write_seq, skb, mpext);
931 TCP_SKB_CB(skb)->eor = 1;
933 avail_size = size_goal - skb->len;
936 if (!retransmission) {
937 /* reuse tail pfrag, if possible, or carve a new one from the
940 dfrag = mptcp_rtx_tail(sk);
941 offset = pfrag->offset;
942 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
943 if (!dfrag_collapsed) {
944 dfrag = mptcp_carve_data_frag(msk, pfrag, offset);
945 offset = dfrag->offset;
946 frag_truesize = dfrag->overhead;
948 psize = min_t(size_t, pfrag->size - offset, avail_size);
951 pr_debug("left=%zu", msg_data_left(msg));
952 psize = copy_page_from_iter(pfrag->page, offset,
953 min_t(size_t, msg_data_left(msg),
956 pr_debug("left=%zu", msg_data_left(msg));
960 if (!sk_wmem_schedule(sk, psize + dfrag->overhead)) {
961 iov_iter_revert(&msg->msg_iter, psize);
965 offset = dfrag->offset;
966 psize = min_t(size_t, dfrag->data_len, avail_size);
969 /* tell the TCP stack to delay the push so that we can safely
970 * access the skb after the sendpages call
972 ret = do_tcp_sendpages(ssk, page, offset, psize,
973 msg->msg_flags | MSG_SENDPAGE_NOTLAST | MSG_DONTWAIT);
976 iov_iter_revert(&msg->msg_iter, psize);
980 frag_truesize += ret;
981 if (!retransmission) {
982 if (unlikely(ret < psize))
983 iov_iter_revert(&msg->msg_iter, psize - ret);
985 /* send successful, keep track of sent data for mptcp-level
988 dfrag->data_len += ret;
989 if (!dfrag_collapsed) {
990 get_page(dfrag->page);
991 list_add_tail(&dfrag->list, &msk->rtx_queue);
992 sk_wmem_queued_add(sk, frag_truesize);
994 sk_wmem_queued_add(sk, ret);
997 /* charge data on mptcp rtx queue to the master socket
998 * Note: we charge such data both to sk and ssk
1000 sk->sk_forward_alloc -= frag_truesize;
1003 /* if the tail skb extension is still the cached one, collapsing
1004 * really happened. Note: we can't check for 'same skb' as the sk_buff
1005 * hdr on tail can be transmitted, freed and re-allocated by the
1006 * do_tcp_sendpages() call
1008 tail = tcp_write_queue_tail(ssk);
1009 if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) {
1010 WARN_ON_ONCE(!can_collapse);
1011 mpext->data_len += ret;
1015 skb = tcp_write_queue_tail(ssk);
1016 mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext);
1017 msk->cached_ext = NULL;
1019 memset(mpext, 0, sizeof(*mpext));
1020 mpext->data_seq = *write_seq;
1021 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1022 mpext->data_len = ret;
1026 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1027 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1031 if (!retransmission)
1032 pfrag->offset += frag_truesize;
1033 WRITE_ONCE(*write_seq, *write_seq + ret);
1034 mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
1039 static void mptcp_nospace(struct mptcp_sock *msk)
1041 struct mptcp_subflow_context *subflow;
1043 clear_bit(MPTCP_SEND_SPACE, &msk->flags);
1044 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
1046 mptcp_for_each_subflow(msk, subflow) {
1047 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1048 struct socket *sock = READ_ONCE(ssk->sk_socket);
1050 /* enables ssk->write_space() callbacks */
1052 set_bit(SOCK_NOSPACE, &sock->flags);
1056 static bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1058 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1060 /* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */
1061 if (subflow->request_join && !subflow->fully_established)
1064 /* only send if our side has not closed yet */
1065 return ((1 << ssk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT));
1068 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1069 sizeof(struct tcphdr) - \
1070 MAX_TCP_OPTION_SPACE - \
1071 sizeof(struct ipv6hdr) - \
1072 sizeof(struct frag_hdr))
1074 struct subflow_send_info {
1079 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk,
1082 struct subflow_send_info send_info[2];
1083 struct mptcp_subflow_context *subflow;
1084 int i, nr_active = 0;
1089 sock_owned_by_me((struct sock *)msk);
1092 if (!mptcp_ext_cache_refill(msk))
1095 if (__mptcp_check_fallback(msk)) {
1098 *sndbuf = msk->first->sk_sndbuf;
1099 return sk_stream_memory_free(msk->first) ? msk->first : NULL;
1102 /* re-use last subflow, if the burst allow that */
1103 if (msk->last_snd && msk->snd_burst > 0 &&
1104 sk_stream_memory_free(msk->last_snd) &&
1105 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1106 mptcp_for_each_subflow(msk, subflow) {
1107 ssk = mptcp_subflow_tcp_sock(subflow);
1108 *sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf);
1110 return msk->last_snd;
1113 /* pick the subflow with the lower wmem/wspace ratio */
1114 for (i = 0; i < 2; ++i) {
1115 send_info[i].ssk = NULL;
1116 send_info[i].ratio = -1;
1118 mptcp_for_each_subflow(msk, subflow) {
1119 ssk = mptcp_subflow_tcp_sock(subflow);
1120 if (!mptcp_subflow_active(subflow))
1123 nr_active += !subflow->backup;
1124 *sndbuf = max(tcp_sk(ssk)->snd_wnd, *sndbuf);
1125 if (!sk_stream_memory_free(subflow->tcp_sock))
1128 pace = READ_ONCE(ssk->sk_pacing_rate);
1132 ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32,
1134 if (ratio < send_info[subflow->backup].ratio) {
1135 send_info[subflow->backup].ssk = ssk;
1136 send_info[subflow->backup].ratio = ratio;
1140 pr_debug("msk=%p nr_active=%d ssk=%p:%lld backup=%p:%lld",
1141 msk, nr_active, send_info[0].ssk, send_info[0].ratio,
1142 send_info[1].ssk, send_info[1].ratio);
1144 /* pick the best backup if no other subflow is active */
1146 send_info[0].ssk = send_info[1].ssk;
1148 if (send_info[0].ssk) {
1149 msk->last_snd = send_info[0].ssk;
1150 msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE,
1151 sk_stream_wspace(msk->last_snd));
1152 return msk->last_snd;
1157 static void ssk_check_wmem(struct mptcp_sock *msk)
1159 if (unlikely(!mptcp_is_writeable(msk)))
1163 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1165 int mss_now = 0, size_goal = 0, ret = 0;
1166 struct mptcp_sock *msk = mptcp_sk(sk);
1167 struct page_frag *pfrag;
1174 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
1179 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1181 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1182 ret = sk_stream_wait_connect(sk, &timeo);
1187 pfrag = sk_page_frag(sk);
1189 mptcp_clean_una(sk);
1191 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
1196 __mptcp_flush_join_list(msk);
1197 ssk = mptcp_subflow_get_send(msk, &sndbuf);
1198 while (!sk_stream_memory_free(sk) ||
1200 !mptcp_page_frag_refill(ssk, pfrag)) {
1202 /* make sure retransmit timer is
1203 * running before we wait for memory.
1205 * The retransmit timer might be needed
1206 * to make the peer send an up-to-date
1209 mptcp_set_timeout(sk, ssk);
1210 if (!mptcp_timer_pending(sk))
1211 mptcp_reset_timer(sk);
1215 ret = sk_stream_wait_memory(sk, &timeo);
1219 mptcp_clean_una(sk);
1221 ssk = mptcp_subflow_get_send(msk, &sndbuf);
1222 if (list_empty(&msk->conn_list)) {
1228 /* do auto tuning */
1229 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK) &&
1230 sndbuf > READ_ONCE(sk->sk_sndbuf))
1231 WRITE_ONCE(sk->sk_sndbuf, sndbuf);
1233 pr_debug("conn_list->subflow=%p", ssk);
1236 tx_ok = msg_data_left(msg);
1238 ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now,
1241 if (ret == -EAGAIN && timeo > 0) {
1242 mptcp_set_timeout(sk, ssk);
1249 /* burst can be negative, we will try move to the next subflow
1250 * at selection time, if possible.
1252 msk->snd_burst -= ret;
1255 tx_ok = msg_data_left(msg);
1259 if (!sk_stream_memory_free(ssk) ||
1260 !mptcp_page_frag_refill(ssk, pfrag) ||
1261 !mptcp_ext_cache_refill(msk)) {
1262 tcp_push(ssk, msg->msg_flags, mss_now,
1263 tcp_sk(ssk)->nonagle, size_goal);
1264 mptcp_set_timeout(sk, ssk);
1269 /* memory is charged to mptcp level socket as well, i.e.
1270 * if msg is very large, mptcp socket may run out of buffer
1271 * space. mptcp_clean_una() will release data that has
1272 * been acked at mptcp level in the mean time, so there is
1273 * a good chance we can continue sending data right away.
1275 * Normally, when the tcp subflow can accept more data, then
1276 * so can the MPTCP socket. However, we need to cope with
1277 * peers that might lag behind in their MPTCP-level
1278 * acknowledgements, i.e. data might have been acked at
1279 * tcp level only. So, we must also check the MPTCP socket
1280 * limits before we send more data.
1282 if (unlikely(!sk_stream_memory_free(sk))) {
1283 tcp_push(ssk, msg->msg_flags, mss_now,
1284 tcp_sk(ssk)->nonagle, size_goal);
1285 mptcp_clean_una(sk);
1286 if (!sk_stream_memory_free(sk)) {
1287 /* can't send more for now, need to wait for
1288 * MPTCP-level ACKs from peer.
1290 * Wakeup will happen via mptcp_clean_una().
1292 mptcp_set_timeout(sk, ssk);
1299 mptcp_set_timeout(sk, ssk);
1301 tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle,
1304 /* start the timer, if it's not pending */
1305 if (!mptcp_timer_pending(sk))
1306 mptcp_reset_timer(sk);
1311 ssk_check_wmem(msk);
1313 return copied ? : ret;
1316 static void mptcp_wait_data(struct sock *sk, long *timeo)
1318 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1319 struct mptcp_sock *msk = mptcp_sk(sk);
1321 add_wait_queue(sk_sleep(sk), &wait);
1322 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1324 sk_wait_event(sk, timeo,
1325 test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait);
1327 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1328 remove_wait_queue(sk_sleep(sk), &wait);
1331 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1335 struct sock *sk = (struct sock *)msk;
1336 struct sk_buff *skb;
1339 while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
1340 u32 offset = MPTCP_SKB_CB(skb)->offset;
1341 u32 data_len = skb->len - offset;
1342 u32 count = min_t(size_t, len - copied, data_len);
1345 err = skb_copy_datagram_msg(skb, offset, msg, count);
1346 if (unlikely(err < 0)) {
1354 if (count < data_len) {
1355 MPTCP_SKB_CB(skb)->offset += count;
1359 __skb_unlink(skb, &sk->sk_receive_queue);
1369 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1371 * Only difference: Use highest rtt estimate of the subflows in use.
1373 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1375 struct mptcp_subflow_context *subflow;
1376 struct sock *sk = (struct sock *)msk;
1377 u32 time, advmss = 1;
1380 sock_owned_by_me(sk);
1385 msk->rcvq_space.copied += copied;
1387 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1388 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1390 rtt_us = msk->rcvq_space.rtt_us;
1391 if (rtt_us && time < (rtt_us >> 3))
1395 mptcp_for_each_subflow(msk, subflow) {
1396 const struct tcp_sock *tp;
1400 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1402 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1403 sf_advmss = READ_ONCE(tp->advmss);
1405 rtt_us = max(sf_rtt_us, rtt_us);
1406 advmss = max(sf_advmss, advmss);
1409 msk->rcvq_space.rtt_us = rtt_us;
1410 if (time < (rtt_us >> 3) || rtt_us == 0)
1413 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1416 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
1417 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1421 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1423 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1425 do_div(grow, msk->rcvq_space.space);
1426 rcvwin += (grow << 1);
1428 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1429 while (tcp_win_from_space(sk, rcvmem) < advmss)
1432 do_div(rcvwin, advmss);
1433 rcvbuf = min_t(u64, rcvwin * rcvmem,
1434 sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
1436 if (rcvbuf > sk->sk_rcvbuf) {
1439 window_clamp = tcp_win_from_space(sk, rcvbuf);
1440 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1442 /* Make subflows follow along. If we do not do this, we
1443 * get drops at subflow level if skbs can't be moved to
1444 * the mptcp rx queue fast enough (announced rcv_win can
1445 * exceed ssk->sk_rcvbuf).
1447 mptcp_for_each_subflow(msk, subflow) {
1451 ssk = mptcp_subflow_tcp_sock(subflow);
1452 slow = lock_sock_fast(ssk);
1453 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1454 tcp_sk(ssk)->window_clamp = window_clamp;
1455 tcp_cleanup_rbuf(ssk, 1);
1456 unlock_sock_fast(ssk, slow);
1461 msk->rcvq_space.space = msk->rcvq_space.copied;
1463 msk->rcvq_space.copied = 0;
1464 msk->rcvq_space.time = mstamp;
1467 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
1469 unsigned int moved = 0;
1472 /* avoid looping forever below on racing close */
1473 if (((struct sock *)msk)->sk_state == TCP_CLOSE)
1476 __mptcp_flush_join_list(msk);
1478 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
1484 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
1488 if (mptcp_ofo_queue(msk) || moved > 0) {
1489 mptcp_check_data_fin((struct sock *)msk);
1495 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1496 int nonblock, int flags, int *addr_len)
1498 struct mptcp_sock *msk = mptcp_sk(sk);
1503 if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT))
1507 timeo = sock_rcvtimeo(sk, nonblock);
1509 len = min_t(size_t, len, INT_MAX);
1510 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1511 __mptcp_flush_join_list(msk);
1513 while (len > (size_t)copied) {
1516 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied);
1517 if (unlikely(bytes_read < 0)) {
1519 copied = bytes_read;
1523 copied += bytes_read;
1525 if (skb_queue_empty(&sk->sk_receive_queue) &&
1526 __mptcp_move_skbs(msk))
1529 /* only the master socket status is relevant here. The exit
1530 * conditions mirror closely tcp_recvmsg()
1532 if (copied >= target)
1537 sk->sk_state == TCP_CLOSE ||
1538 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1540 signal_pending(current))
1544 copied = sock_error(sk);
1548 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
1549 mptcp_check_for_eof(msk);
1551 if (sk->sk_shutdown & RCV_SHUTDOWN)
1554 if (sk->sk_state == TCP_CLOSE) {
1564 if (signal_pending(current)) {
1565 copied = sock_intr_errno(timeo);
1570 pr_debug("block timeout %ld", timeo);
1571 mptcp_wait_data(sk, &timeo);
1574 if (skb_queue_empty(&sk->sk_receive_queue)) {
1575 /* entire backlog drained, clear DATA_READY. */
1576 clear_bit(MPTCP_DATA_READY, &msk->flags);
1578 /* .. race-breaker: ssk might have gotten new data
1579 * after last __mptcp_move_skbs() returned false.
1581 if (unlikely(__mptcp_move_skbs(msk)))
1582 set_bit(MPTCP_DATA_READY, &msk->flags);
1583 } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) {
1584 /* data to read but mptcp_wait_data() cleared DATA_READY */
1585 set_bit(MPTCP_DATA_READY, &msk->flags);
1588 pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d",
1589 msk, test_bit(MPTCP_DATA_READY, &msk->flags),
1590 skb_queue_empty(&sk->sk_receive_queue), copied);
1591 mptcp_rcv_space_adjust(msk, copied);
1597 static void mptcp_retransmit_handler(struct sock *sk)
1599 struct mptcp_sock *msk = mptcp_sk(sk);
1601 if (atomic64_read(&msk->snd_una) == READ_ONCE(msk->write_seq)) {
1602 mptcp_stop_timer(sk);
1604 set_bit(MPTCP_WORK_RTX, &msk->flags);
1605 if (schedule_work(&msk->work))
1610 static void mptcp_retransmit_timer(struct timer_list *t)
1612 struct inet_connection_sock *icsk = from_timer(icsk, t,
1613 icsk_retransmit_timer);
1614 struct sock *sk = &icsk->icsk_inet.sk;
1617 if (!sock_owned_by_user(sk)) {
1618 mptcp_retransmit_handler(sk);
1620 /* delegate our work to tcp_release_cb() */
1621 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED,
1629 /* Find an idle subflow. Return NULL if there is unacked data at tcp
1632 * A backup subflow is returned only if that is the only kind available.
1634 static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
1636 struct mptcp_subflow_context *subflow;
1637 struct sock *backup = NULL;
1639 sock_owned_by_me((const struct sock *)msk);
1641 if (__mptcp_check_fallback(msk))
1644 mptcp_for_each_subflow(msk, subflow) {
1645 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1647 if (!mptcp_subflow_active(subflow))
1650 /* still data outstanding at TCP level? Don't retransmit. */
1651 if (!tcp_write_queue_empty(ssk))
1654 if (subflow->backup) {
1666 /* subflow sockets can be either outgoing (connect) or incoming
1669 * Outgoing subflows use in-kernel sockets.
1670 * Incoming subflows do not have their own 'struct socket' allocated,
1671 * so we need to use tcp_close() after detaching them from the mptcp
1674 void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
1675 struct mptcp_subflow_context *subflow,
1678 struct socket *sock = READ_ONCE(ssk->sk_socket);
1680 list_del(&subflow->node);
1682 if (sock && sock != sk->sk_socket) {
1683 /* outgoing subflow */
1686 /* incoming subflow */
1687 tcp_close(ssk, timeout);
1691 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
1696 static void pm_work(struct mptcp_sock *msk)
1698 struct mptcp_pm_data *pm = &msk->pm;
1700 spin_lock_bh(&msk->pm.lock);
1702 pr_debug("msk=%p status=%x", msk, pm->status);
1703 if (pm->status & BIT(MPTCP_PM_ADD_ADDR_RECEIVED)) {
1704 pm->status &= ~BIT(MPTCP_PM_ADD_ADDR_RECEIVED);
1705 mptcp_pm_nl_add_addr_received(msk);
1707 if (pm->status & BIT(MPTCP_PM_RM_ADDR_RECEIVED)) {
1708 pm->status &= ~BIT(MPTCP_PM_RM_ADDR_RECEIVED);
1709 mptcp_pm_nl_rm_addr_received(msk);
1711 if (pm->status & BIT(MPTCP_PM_ESTABLISHED)) {
1712 pm->status &= ~BIT(MPTCP_PM_ESTABLISHED);
1713 mptcp_pm_nl_fully_established(msk);
1715 if (pm->status & BIT(MPTCP_PM_SUBFLOW_ESTABLISHED)) {
1716 pm->status &= ~BIT(MPTCP_PM_SUBFLOW_ESTABLISHED);
1717 mptcp_pm_nl_subflow_established(msk);
1720 spin_unlock_bh(&msk->pm.lock);
1723 static void __mptcp_close_subflow(struct mptcp_sock *msk)
1725 struct mptcp_subflow_context *subflow, *tmp;
1727 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
1728 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1730 if (inet_sk_state_load(ssk) != TCP_CLOSE)
1733 __mptcp_close_ssk((struct sock *)msk, ssk, subflow, 0);
1737 static void mptcp_worker(struct work_struct *work)
1739 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
1740 struct sock *ssk, *sk = &msk->sk.icsk_inet.sk;
1741 int orig_len, orig_offset, mss_now = 0, size_goal = 0;
1742 struct mptcp_data_frag *dfrag;
1745 struct msghdr msg = {
1746 .msg_flags = MSG_DONTWAIT,
1751 mptcp_clean_una(sk);
1752 mptcp_check_data_fin_ack(sk);
1753 __mptcp_flush_join_list(msk);
1754 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
1755 __mptcp_close_subflow(msk);
1757 __mptcp_move_skbs(msk);
1762 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
1763 mptcp_check_for_eof(msk);
1765 mptcp_check_data_fin(sk);
1767 if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
1770 dfrag = mptcp_rtx_head(sk);
1774 if (!mptcp_ext_cache_refill(msk))
1777 ssk = mptcp_subflow_get_retrans(msk);
1783 orig_len = dfrag->data_len;
1784 orig_offset = dfrag->offset;
1785 orig_write_seq = dfrag->data_seq;
1786 while (dfrag->data_len > 0) {
1787 int ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo,
1788 &mss_now, &size_goal);
1792 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
1794 dfrag->data_len -= ret;
1795 dfrag->offset += ret;
1797 if (!mptcp_ext_cache_refill(msk))
1801 tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle,
1804 dfrag->data_seq = orig_write_seq;
1805 dfrag->offset = orig_offset;
1806 dfrag->data_len = orig_len;
1808 mptcp_set_timeout(sk, ssk);
1812 if (!mptcp_timer_pending(sk))
1813 mptcp_reset_timer(sk);
1820 static int __mptcp_init_sock(struct sock *sk)
1822 struct mptcp_sock *msk = mptcp_sk(sk);
1824 spin_lock_init(&msk->join_list_lock);
1826 INIT_LIST_HEAD(&msk->conn_list);
1827 INIT_LIST_HEAD(&msk->join_list);
1828 INIT_LIST_HEAD(&msk->rtx_queue);
1829 __set_bit(MPTCP_SEND_SPACE, &msk->flags);
1830 INIT_WORK(&msk->work, mptcp_worker);
1831 msk->out_of_order_queue = RB_ROOT;
1834 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
1836 mptcp_pm_data_init(msk);
1838 /* re-use the csk retrans timer for MPTCP-level retrans */
1839 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
1844 static int mptcp_init_sock(struct sock *sk)
1846 struct net *net = sock_net(sk);
1849 ret = __mptcp_init_sock(sk);
1853 if (!mptcp_is_enabled(net))
1854 return -ENOPROTOOPT;
1856 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
1859 ret = __mptcp_socket_create(mptcp_sk(sk));
1863 sk_sockets_allocated_inc(sk);
1864 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
1865 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
1870 static void __mptcp_clear_xmit(struct sock *sk)
1872 struct mptcp_sock *msk = mptcp_sk(sk);
1873 struct mptcp_data_frag *dtmp, *dfrag;
1875 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
1877 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
1878 dfrag_clear(sk, dfrag);
1881 static void mptcp_cancel_work(struct sock *sk)
1883 struct mptcp_sock *msk = mptcp_sk(sk);
1885 if (cancel_work_sync(&msk->work))
1889 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
1893 switch (ssk->sk_state) {
1895 if (!(how & RCV_SHUTDOWN))
1899 tcp_disconnect(ssk, O_NONBLOCK);
1902 if (__mptcp_check_fallback(mptcp_sk(sk))) {
1903 pr_debug("Fallback");
1904 ssk->sk_shutdown |= how;
1905 tcp_shutdown(ssk, how);
1907 pr_debug("Sending DATA_FIN on subflow %p", ssk);
1908 mptcp_set_timeout(sk, ssk);
1917 static const unsigned char new_state[16] = {
1918 /* current state: new state: action: */
1919 [0 /* (Invalid) */] = TCP_CLOSE,
1920 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
1921 [TCP_SYN_SENT] = TCP_CLOSE,
1922 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
1923 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
1924 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
1925 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
1926 [TCP_CLOSE] = TCP_CLOSE,
1927 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
1928 [TCP_LAST_ACK] = TCP_LAST_ACK,
1929 [TCP_LISTEN] = TCP_CLOSE,
1930 [TCP_CLOSING] = TCP_CLOSING,
1931 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
1934 static int mptcp_close_state(struct sock *sk)
1936 int next = (int)new_state[sk->sk_state];
1937 int ns = next & TCP_STATE_MASK;
1939 inet_sk_state_store(sk, ns);
1941 return next & TCP_ACTION_FIN;
1944 static void mptcp_close(struct sock *sk, long timeout)
1946 struct mptcp_subflow_context *subflow, *tmp;
1947 struct mptcp_sock *msk = mptcp_sk(sk);
1948 LIST_HEAD(conn_list);
1951 sk->sk_shutdown = SHUTDOWN_MASK;
1953 if (sk->sk_state == TCP_LISTEN) {
1954 inet_sk_state_store(sk, TCP_CLOSE);
1956 } else if (sk->sk_state == TCP_CLOSE) {
1960 if (__mptcp_check_fallback(msk)) {
1962 } else if (mptcp_close_state(sk)) {
1963 pr_debug("Sending DATA_FIN sk=%p", sk);
1964 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
1965 WRITE_ONCE(msk->snd_data_fin_enable, 1);
1967 mptcp_for_each_subflow(msk, subflow) {
1968 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
1970 mptcp_subflow_shutdown(sk, tcp_sk, SHUTDOWN_MASK);
1974 sk_stream_wait_close(sk, timeout);
1977 inet_sk_state_store(sk, TCP_CLOSE);
1980 /* be sure to always acquire the join list lock, to sync vs
1981 * mptcp_finish_join().
1983 spin_lock_bh(&msk->join_list_lock);
1984 list_splice_tail_init(&msk->join_list, &msk->conn_list);
1985 spin_unlock_bh(&msk->join_list_lock);
1986 list_splice_init(&msk->conn_list, &conn_list);
1988 __mptcp_clear_xmit(sk);
1992 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
1993 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1994 __mptcp_close_ssk(sk, ssk, subflow, timeout);
1997 mptcp_cancel_work(sk);
1999 __skb_queue_purge(&sk->sk_receive_queue);
2001 sk_common_release(sk);
2004 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
2006 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2007 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
2008 struct ipv6_pinfo *msk6 = inet6_sk(msk);
2010 msk->sk_v6_daddr = ssk->sk_v6_daddr;
2011 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
2014 msk6->saddr = ssk6->saddr;
2015 msk6->flow_label = ssk6->flow_label;
2019 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
2020 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
2021 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
2022 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
2023 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
2024 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
2027 static int mptcp_disconnect(struct sock *sk, int flags)
2029 /* Should never be called.
2030 * inet_stream_connect() calls ->disconnect, but that
2031 * refers to the subflow socket, not the mptcp one.
2037 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2038 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
2040 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
2042 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
2046 struct sock *mptcp_sk_clone(const struct sock *sk,
2047 const struct mptcp_options_received *mp_opt,
2048 struct request_sock *req)
2050 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
2051 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
2052 struct mptcp_sock *msk;
2058 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2059 if (nsk->sk_family == AF_INET6)
2060 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
2063 __mptcp_init_sock(nsk);
2065 msk = mptcp_sk(nsk);
2066 msk->local_key = subflow_req->local_key;
2067 msk->token = subflow_req->token;
2068 msk->subflow = NULL;
2069 WRITE_ONCE(msk->fully_established, false);
2071 msk->write_seq = subflow_req->idsn + 1;
2072 atomic64_set(&msk->snd_una, msk->write_seq);
2073 if (mp_opt->mp_capable) {
2074 msk->can_ack = true;
2075 msk->remote_key = mp_opt->sndr_key;
2076 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
2078 WRITE_ONCE(msk->ack_seq, ack_seq);
2081 sock_reset_flag(nsk, SOCK_RCU_FREE);
2082 /* will be fully established after successful MPC subflow creation */
2083 inet_sk_state_store(nsk, TCP_SYN_RECV);
2084 bh_unlock_sock(nsk);
2086 /* keep a single reference */
2091 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
2093 const struct tcp_sock *tp = tcp_sk(ssk);
2095 msk->rcvq_space.copied = 0;
2096 msk->rcvq_space.rtt_us = 0;
2098 msk->rcvq_space.time = tp->tcp_mstamp;
2100 /* initial rcv_space offering made to peer */
2101 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
2102 TCP_INIT_CWND * tp->advmss);
2103 if (msk->rcvq_space.space == 0)
2104 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
2107 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
2110 struct mptcp_sock *msk = mptcp_sk(sk);
2111 struct socket *listener;
2114 listener = __mptcp_nmpc_socket(msk);
2115 if (WARN_ON_ONCE(!listener)) {
2120 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
2121 newsk = inet_csk_accept(listener->sk, flags, err, kern);
2125 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
2126 if (sk_is_mptcp(newsk)) {
2127 struct mptcp_subflow_context *subflow;
2128 struct sock *new_mptcp_sock;
2129 struct sock *ssk = newsk;
2131 subflow = mptcp_subflow_ctx(newsk);
2132 new_mptcp_sock = subflow->conn;
2134 /* is_mptcp should be false if subflow->conn is missing, see
2135 * subflow_syn_recv_sock()
2137 if (WARN_ON_ONCE(!new_mptcp_sock)) {
2138 tcp_sk(newsk)->is_mptcp = 0;
2142 /* acquire the 2nd reference for the owning socket */
2143 sock_hold(new_mptcp_sock);
2146 bh_lock_sock(new_mptcp_sock);
2147 msk = mptcp_sk(new_mptcp_sock);
2150 newsk = new_mptcp_sock;
2151 mptcp_copy_inaddrs(newsk, ssk);
2152 list_add(&subflow->node, &msk->conn_list);
2154 mptcp_rcv_space_init(msk, ssk);
2155 bh_unlock_sock(new_mptcp_sock);
2157 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
2160 MPTCP_INC_STATS(sock_net(sk),
2161 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
2167 void mptcp_destroy_common(struct mptcp_sock *msk)
2169 skb_rbtree_purge(&msk->out_of_order_queue);
2170 mptcp_token_destroy(msk);
2171 mptcp_pm_free_anno_list(msk);
2174 static void mptcp_destroy(struct sock *sk)
2176 struct mptcp_sock *msk = mptcp_sk(sk);
2178 if (msk->cached_ext)
2179 __skb_ext_put(msk->cached_ext);
2181 mptcp_destroy_common(msk);
2182 sk_sockets_allocated_dec(sk);
2185 static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname,
2186 sockptr_t optval, unsigned int optlen)
2188 struct sock *sk = (struct sock *)msk;
2189 struct socket *ssock;
2196 ssock = __mptcp_nmpc_socket(msk);
2202 ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen);
2204 if (optname == SO_REUSEPORT)
2205 sk->sk_reuseport = ssock->sk->sk_reuseport;
2206 else if (optname == SO_REUSEADDR)
2207 sk->sk_reuse = ssock->sk->sk_reuse;
2213 return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen);
2216 static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname,
2217 sockptr_t optval, unsigned int optlen)
2219 struct sock *sk = (struct sock *)msk;
2220 int ret = -EOPNOTSUPP;
2221 struct socket *ssock;
2226 ssock = __mptcp_nmpc_socket(msk);
2232 ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen);
2234 sk->sk_ipv6only = ssock->sk->sk_ipv6only;
2243 static int mptcp_setsockopt(struct sock *sk, int level, int optname,
2244 sockptr_t optval, unsigned int optlen)
2246 struct mptcp_sock *msk = mptcp_sk(sk);
2249 pr_debug("msk=%p", msk);
2251 if (level == SOL_SOCKET)
2252 return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen);
2254 /* @@ the meaning of setsockopt() when the socket is connected and
2255 * there are multiple subflows is not yet defined. It is up to the
2256 * MPTCP-level socket to configure the subflows until the subflow
2257 * is in TCP fallback, when TCP socket options are passed through
2258 * to the one remaining subflow.
2261 ssk = __mptcp_tcp_fallback(msk);
2264 return tcp_setsockopt(ssk, level, optname, optval, optlen);
2266 if (level == SOL_IPV6)
2267 return mptcp_setsockopt_v6(msk, optname, optval, optlen);
2272 static int mptcp_getsockopt(struct sock *sk, int level, int optname,
2273 char __user *optval, int __user *option)
2275 struct mptcp_sock *msk = mptcp_sk(sk);
2278 pr_debug("msk=%p", msk);
2280 /* @@ the meaning of setsockopt() when the socket is connected and
2281 * there are multiple subflows is not yet defined. It is up to the
2282 * MPTCP-level socket to configure the subflows until the subflow
2283 * is in TCP fallback, when socket options are passed through
2284 * to the one remaining subflow.
2287 ssk = __mptcp_tcp_fallback(msk);
2290 return tcp_getsockopt(ssk, level, optname, optval, option);
2295 #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \
2296 TCPF_WRITE_TIMER_DEFERRED)
2298 /* this is very alike tcp_release_cb() but we must handle differently a
2299 * different set of events
2301 static void mptcp_release_cb(struct sock *sk)
2303 unsigned long flags, nflags;
2306 flags = sk->sk_tsq_flags;
2307 if (!(flags & MPTCP_DEFERRED_ALL))
2309 nflags = flags & ~MPTCP_DEFERRED_ALL;
2310 } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
2312 sock_release_ownership(sk);
2314 if (flags & TCPF_DELACK_TIMER_DEFERRED) {
2315 struct mptcp_sock *msk = mptcp_sk(sk);
2318 ssk = mptcp_subflow_recv_lookup(msk);
2319 if (!ssk || !schedule_work(&msk->work))
2323 if (flags & TCPF_WRITE_TIMER_DEFERRED) {
2324 mptcp_retransmit_handler(sk);
2329 static int mptcp_hash(struct sock *sk)
2331 /* should never be called,
2332 * we hash the TCP subflows not the master socket
2338 static void mptcp_unhash(struct sock *sk)
2340 /* called from sk_common_release(), but nothing to do here */
2343 static int mptcp_get_port(struct sock *sk, unsigned short snum)
2345 struct mptcp_sock *msk = mptcp_sk(sk);
2346 struct socket *ssock;
2348 ssock = __mptcp_nmpc_socket(msk);
2349 pr_debug("msk=%p, subflow=%p", msk, ssock);
2350 if (WARN_ON_ONCE(!ssock))
2353 return inet_csk_get_port(ssock->sk, snum);
2356 void mptcp_finish_connect(struct sock *ssk)
2358 struct mptcp_subflow_context *subflow;
2359 struct mptcp_sock *msk;
2363 subflow = mptcp_subflow_ctx(ssk);
2367 pr_debug("msk=%p, token=%u", sk, subflow->token);
2369 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
2371 subflow->map_seq = ack_seq;
2372 subflow->map_subflow_seq = 1;
2374 /* the socket is not connected yet, no msk/subflow ops can access/race
2375 * accessing the field below
2377 WRITE_ONCE(msk->remote_key, subflow->remote_key);
2378 WRITE_ONCE(msk->local_key, subflow->local_key);
2379 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
2380 WRITE_ONCE(msk->ack_seq, ack_seq);
2381 WRITE_ONCE(msk->can_ack, 1);
2382 atomic64_set(&msk->snd_una, msk->write_seq);
2384 mptcp_pm_new_connection(msk, 0);
2386 mptcp_rcv_space_init(msk, ssk);
2389 static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
2391 write_lock_bh(&sk->sk_callback_lock);
2392 rcu_assign_pointer(sk->sk_wq, &parent->wq);
2393 sk_set_socket(sk, parent);
2394 sk->sk_uid = SOCK_INODE(parent)->i_uid;
2395 write_unlock_bh(&sk->sk_callback_lock);
2398 bool mptcp_finish_join(struct sock *sk)
2400 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
2401 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
2402 struct sock *parent = (void *)msk;
2403 struct socket *parent_sock;
2406 pr_debug("msk=%p, subflow=%p", msk, subflow);
2408 /* mptcp socket already closing? */
2409 if (!mptcp_is_fully_established(parent))
2412 if (!msk->pm.server_side)
2415 if (!mptcp_pm_allow_new_subflow(msk))
2418 /* active connections are already on conn_list, and we can't acquire
2420 * use the join list lock as synchronization point and double-check
2421 * msk status to avoid racing with mptcp_close()
2423 spin_lock_bh(&msk->join_list_lock);
2424 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
2425 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node)))
2426 list_add_tail(&subflow->node, &msk->join_list);
2427 spin_unlock_bh(&msk->join_list_lock);
2431 /* attach to msk socket only after we are sure he will deal with us
2434 parent_sock = READ_ONCE(parent->sk_socket);
2435 if (parent_sock && !sk->sk_socket)
2436 mptcp_sock_graft(sk, parent_sock);
2437 subflow->map_seq = READ_ONCE(msk->ack_seq);
2441 static bool mptcp_memory_free(const struct sock *sk, int wake)
2443 struct mptcp_sock *msk = mptcp_sk(sk);
2445 return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true;
2448 static struct proto mptcp_prot = {
2450 .owner = THIS_MODULE,
2451 .init = mptcp_init_sock,
2452 .disconnect = mptcp_disconnect,
2453 .close = mptcp_close,
2454 .accept = mptcp_accept,
2455 .setsockopt = mptcp_setsockopt,
2456 .getsockopt = mptcp_getsockopt,
2457 .shutdown = tcp_shutdown,
2458 .destroy = mptcp_destroy,
2459 .sendmsg = mptcp_sendmsg,
2460 .recvmsg = mptcp_recvmsg,
2461 .release_cb = mptcp_release_cb,
2463 .unhash = mptcp_unhash,
2464 .get_port = mptcp_get_port,
2465 .sockets_allocated = &mptcp_sockets_allocated,
2466 .memory_allocated = &tcp_memory_allocated,
2467 .memory_pressure = &tcp_memory_pressure,
2468 .stream_memory_free = mptcp_memory_free,
2469 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
2470 .sysctl_mem = sysctl_tcp_mem,
2471 .obj_size = sizeof(struct mptcp_sock),
2472 .slab_flags = SLAB_TYPESAFE_BY_RCU,
2473 .no_autobind = true,
2476 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2478 struct mptcp_sock *msk = mptcp_sk(sock->sk);
2479 struct socket *ssock;
2482 lock_sock(sock->sk);
2483 ssock = __mptcp_nmpc_socket(msk);
2489 err = ssock->ops->bind(ssock, uaddr, addr_len);
2491 mptcp_copy_inaddrs(sock->sk, ssock->sk);
2494 release_sock(sock->sk);
2498 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
2499 struct mptcp_subflow_context *subflow)
2501 subflow->request_mptcp = 0;
2502 __mptcp_do_fallback(msk);
2505 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
2506 int addr_len, int flags)
2508 struct mptcp_sock *msk = mptcp_sk(sock->sk);
2509 struct mptcp_subflow_context *subflow;
2510 struct socket *ssock;
2513 lock_sock(sock->sk);
2514 if (sock->state != SS_UNCONNECTED && msk->subflow) {
2515 /* pending connection or invalid state, let existing subflow
2518 ssock = msk->subflow;
2522 ssock = __mptcp_nmpc_socket(msk);
2528 mptcp_token_destroy(msk);
2529 inet_sk_state_store(sock->sk, TCP_SYN_SENT);
2530 subflow = mptcp_subflow_ctx(ssock->sk);
2531 #ifdef CONFIG_TCP_MD5SIG
2532 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
2535 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
2536 mptcp_subflow_early_fallback(msk, subflow);
2538 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk))
2539 mptcp_subflow_early_fallback(msk, subflow);
2542 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
2543 sock->state = ssock->state;
2545 /* on successful connect, the msk state will be moved to established by
2546 * subflow_finish_connect()
2548 if (!err || err == -EINPROGRESS)
2549 mptcp_copy_inaddrs(sock->sk, ssock->sk);
2551 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
2554 release_sock(sock->sk);
2558 static int mptcp_listen(struct socket *sock, int backlog)
2560 struct mptcp_sock *msk = mptcp_sk(sock->sk);
2561 struct socket *ssock;
2564 pr_debug("msk=%p", msk);
2566 lock_sock(sock->sk);
2567 ssock = __mptcp_nmpc_socket(msk);
2573 mptcp_token_destroy(msk);
2574 inet_sk_state_store(sock->sk, TCP_LISTEN);
2575 sock_set_flag(sock->sk, SOCK_RCU_FREE);
2577 err = ssock->ops->listen(ssock, backlog);
2578 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
2580 mptcp_copy_inaddrs(sock->sk, ssock->sk);
2583 release_sock(sock->sk);
2587 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
2588 int flags, bool kern)
2590 struct mptcp_sock *msk = mptcp_sk(sock->sk);
2591 struct socket *ssock;
2594 pr_debug("msk=%p", msk);
2596 lock_sock(sock->sk);
2597 if (sock->sk->sk_state != TCP_LISTEN)
2600 ssock = __mptcp_nmpc_socket(msk);
2604 clear_bit(MPTCP_DATA_READY, &msk->flags);
2605 sock_hold(ssock->sk);
2606 release_sock(sock->sk);
2608 err = ssock->ops->accept(sock, newsock, flags, kern);
2609 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
2610 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
2611 struct mptcp_subflow_context *subflow;
2613 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
2614 * This is needed so NOSPACE flag can be set from tcp stack.
2616 __mptcp_flush_join_list(msk);
2617 mptcp_for_each_subflow(msk, subflow) {
2618 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2620 if (!ssk->sk_socket)
2621 mptcp_sock_graft(ssk, newsock);
2625 if (inet_csk_listen_poll(ssock->sk))
2626 set_bit(MPTCP_DATA_READY, &msk->flags);
2627 sock_put(ssock->sk);
2631 release_sock(sock->sk);
2635 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
2637 return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM :
2641 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
2642 struct poll_table_struct *wait)
2644 struct sock *sk = sock->sk;
2645 struct mptcp_sock *msk;
2650 sock_poll_wait(file, sock, wait);
2652 state = inet_sk_state_load(sk);
2653 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
2654 if (state == TCP_LISTEN)
2655 return mptcp_check_readable(msk);
2657 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
2658 mask |= mptcp_check_readable(msk);
2659 if (test_bit(MPTCP_SEND_SPACE, &msk->flags))
2660 mask |= EPOLLOUT | EPOLLWRNORM;
2662 if (sk->sk_shutdown & RCV_SHUTDOWN)
2663 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2668 static int mptcp_shutdown(struct socket *sock, int how)
2670 struct mptcp_sock *msk = mptcp_sk(sock->sk);
2671 struct mptcp_subflow_context *subflow;
2674 pr_debug("sk=%p, how=%d", msk, how);
2676 lock_sock(sock->sk);
2679 if ((how & ~SHUTDOWN_MASK) || !how) {
2684 if (sock->state == SS_CONNECTING) {
2685 if ((1 << sock->sk->sk_state) &
2686 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
2687 sock->state = SS_DISCONNECTING;
2689 sock->state = SS_CONNECTED;
2692 /* If we've already sent a FIN, or it's a closed state, skip this. */
2693 if (__mptcp_check_fallback(msk)) {
2694 if (how == SHUT_WR || how == SHUT_RDWR)
2695 inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
2697 mptcp_for_each_subflow(msk, subflow) {
2698 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2700 mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
2702 } else if ((how & SEND_SHUTDOWN) &&
2703 ((1 << sock->sk->sk_state) &
2704 (TCPF_ESTABLISHED | TCPF_SYN_SENT |
2705 TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) &&
2706 mptcp_close_state(sock->sk)) {
2707 __mptcp_flush_join_list(msk);
2709 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2710 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2712 mptcp_for_each_subflow(msk, subflow) {
2713 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2715 mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
2719 /* Wake up anyone sleeping in poll. */
2720 sock->sk->sk_state_change(sock->sk);
2723 release_sock(sock->sk);
2728 static const struct proto_ops mptcp_stream_ops = {
2730 .owner = THIS_MODULE,
2731 .release = inet_release,
2733 .connect = mptcp_stream_connect,
2734 .socketpair = sock_no_socketpair,
2735 .accept = mptcp_stream_accept,
2736 .getname = inet_getname,
2738 .ioctl = inet_ioctl,
2739 .gettstamp = sock_gettstamp,
2740 .listen = mptcp_listen,
2741 .shutdown = mptcp_shutdown,
2742 .setsockopt = sock_common_setsockopt,
2743 .getsockopt = sock_common_getsockopt,
2744 .sendmsg = inet_sendmsg,
2745 .recvmsg = inet_recvmsg,
2746 .mmap = sock_no_mmap,
2747 .sendpage = inet_sendpage,
2750 static struct inet_protosw mptcp_protosw = {
2751 .type = SOCK_STREAM,
2752 .protocol = IPPROTO_MPTCP,
2753 .prot = &mptcp_prot,
2754 .ops = &mptcp_stream_ops,
2755 .flags = INET_PROTOSW_ICSK,
2758 void __init mptcp_proto_init(void)
2760 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
2762 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
2763 panic("Failed to allocate MPTCP pcpu counter\n");
2765 mptcp_subflow_init();
2769 if (proto_register(&mptcp_prot, 1) != 0)
2770 panic("Failed to register MPTCP proto.\n");
2772 inet_register_protosw(&mptcp_protosw);
2774 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
2777 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2778 static const struct proto_ops mptcp_v6_stream_ops = {
2780 .owner = THIS_MODULE,
2781 .release = inet6_release,
2783 .connect = mptcp_stream_connect,
2784 .socketpair = sock_no_socketpair,
2785 .accept = mptcp_stream_accept,
2786 .getname = inet6_getname,
2788 .ioctl = inet6_ioctl,
2789 .gettstamp = sock_gettstamp,
2790 .listen = mptcp_listen,
2791 .shutdown = mptcp_shutdown,
2792 .setsockopt = sock_common_setsockopt,
2793 .getsockopt = sock_common_getsockopt,
2794 .sendmsg = inet6_sendmsg,
2795 .recvmsg = inet6_recvmsg,
2796 .mmap = sock_no_mmap,
2797 .sendpage = inet_sendpage,
2798 #ifdef CONFIG_COMPAT
2799 .compat_ioctl = inet6_compat_ioctl,
2803 static struct proto mptcp_v6_prot;
2805 static void mptcp_v6_destroy(struct sock *sk)
2808 inet6_destroy_sock(sk);
2811 static struct inet_protosw mptcp_v6_protosw = {
2812 .type = SOCK_STREAM,
2813 .protocol = IPPROTO_MPTCP,
2814 .prot = &mptcp_v6_prot,
2815 .ops = &mptcp_v6_stream_ops,
2816 .flags = INET_PROTOSW_ICSK,
2819 int __init mptcp_proto_v6_init(void)
2823 mptcp_v6_prot = mptcp_prot;
2824 strcpy(mptcp_v6_prot.name, "MPTCPv6");
2825 mptcp_v6_prot.slab = NULL;
2826 mptcp_v6_prot.destroy = mptcp_v6_destroy;
2827 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
2829 err = proto_register(&mptcp_v6_prot, 1);
2833 err = inet6_register_protosw(&mptcp_v6_protosw);
2835 proto_unregister(&mptcp_v6_prot);