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;
46 #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
49 MPTCP_CMSG_TS = BIT(0),
50 MPTCP_CMSG_INQ = BIT(1),
53 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
55 static void __mptcp_destroy_sock(struct sock *sk);
56 static void __mptcp_check_send_data_fin(struct sock *sk);
58 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
59 static struct net_device mptcp_napi_dev;
61 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
62 * completed yet or has failed, return the subflow socket.
63 * Otherwise return NULL.
65 struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
67 if (!msk->subflow || READ_ONCE(msk->can_ack))
73 /* Returns end sequence number of the receiver's advertised window */
74 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
76 return READ_ONCE(msk->wnd_end);
79 static bool mptcp_is_tcpsk(struct sock *sk)
81 struct socket *sock = sk->sk_socket;
83 if (unlikely(sk->sk_prot == &tcp_prot)) {
84 /* we are being invoked after mptcp_accept() has
85 * accepted a non-mp-capable flow: sk is a tcp_sk,
88 * Hand the socket over to tcp so all further socket ops
91 sock->ops = &inet_stream_ops;
93 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
94 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
95 sock->ops = &inet6_stream_ops;
103 static int __mptcp_socket_create(struct mptcp_sock *msk)
105 struct mptcp_subflow_context *subflow;
106 struct sock *sk = (struct sock *)msk;
107 struct socket *ssock;
110 err = mptcp_subflow_create_socket(sk, &ssock);
114 msk->first = ssock->sk;
115 msk->subflow = ssock;
116 subflow = mptcp_subflow_ctx(ssock->sk);
117 list_add(&subflow->node, &msk->conn_list);
118 sock_hold(ssock->sk);
119 subflow->request_mptcp = 1;
121 /* This is the first subflow, always with id 0 */
122 subflow->local_id_valid = 1;
123 mptcp_sock_graft(msk->first, sk->sk_socket);
128 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
130 sk_drops_add(sk, skb);
134 static void mptcp_rmem_charge(struct sock *sk, int size)
136 mptcp_sk(sk)->rmem_fwd_alloc -= size;
139 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
140 struct sk_buff *from)
145 if (MPTCP_SKB_CB(from)->offset ||
146 !skb_try_coalesce(to, from, &fragstolen, &delta))
149 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
150 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
151 to->len, MPTCP_SKB_CB(from)->end_seq);
152 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
153 kfree_skb_partial(from, fragstolen);
154 atomic_add(delta, &sk->sk_rmem_alloc);
155 mptcp_rmem_charge(sk, delta);
159 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
160 struct sk_buff *from)
162 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
165 return mptcp_try_coalesce((struct sock *)msk, to, from);
168 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
170 amount >>= SK_MEM_QUANTUM_SHIFT;
171 mptcp_sk(sk)->rmem_fwd_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
172 __sk_mem_reduce_allocated(sk, amount);
175 static void mptcp_rmem_uncharge(struct sock *sk, int size)
177 struct mptcp_sock *msk = mptcp_sk(sk);
180 msk->rmem_fwd_alloc += size;
181 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
183 /* see sk_mem_uncharge() for the rationale behind the following schema */
184 if (unlikely(reclaimable >= SK_RECLAIM_THRESHOLD))
185 __mptcp_rmem_reclaim(sk, SK_RECLAIM_CHUNK);
188 static void mptcp_rfree(struct sk_buff *skb)
190 unsigned int len = skb->truesize;
191 struct sock *sk = skb->sk;
193 atomic_sub(len, &sk->sk_rmem_alloc);
194 mptcp_rmem_uncharge(sk, len);
197 static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
201 skb->destructor = mptcp_rfree;
202 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
203 mptcp_rmem_charge(sk, skb->truesize);
206 /* "inspired" by tcp_data_queue_ofo(), main differences:
208 * - don't cope with sacks
210 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
212 struct sock *sk = (struct sock *)msk;
213 struct rb_node **p, *parent;
214 u64 seq, end_seq, max_seq;
215 struct sk_buff *skb1;
217 seq = MPTCP_SKB_CB(skb)->map_seq;
218 end_seq = MPTCP_SKB_CB(skb)->end_seq;
219 max_seq = READ_ONCE(msk->rcv_wnd_sent);
221 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
222 RB_EMPTY_ROOT(&msk->out_of_order_queue));
223 if (after64(end_seq, max_seq)) {
226 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
227 (unsigned long long)end_seq - (unsigned long)max_seq,
228 (unsigned long long)msk->rcv_wnd_sent);
229 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
233 p = &msk->out_of_order_queue.rb_node;
234 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
235 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
236 rb_link_node(&skb->rbnode, NULL, p);
237 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
238 msk->ooo_last_skb = skb;
242 /* with 2 subflows, adding at end of ooo queue is quite likely
243 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
245 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
246 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
247 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
251 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
252 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
253 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
254 parent = &msk->ooo_last_skb->rbnode;
255 p = &parent->rb_right;
259 /* Find place to insert this segment. Handle overlaps on the way. */
263 skb1 = rb_to_skb(parent);
264 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
265 p = &parent->rb_left;
268 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
269 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
270 /* All the bits are present. Drop. */
272 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
275 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
279 * continue traversing
282 /* skb's seq == skb1's seq and skb covers skb1.
283 * Replace skb1 with skb.
285 rb_replace_node(&skb1->rbnode, &skb->rbnode,
286 &msk->out_of_order_queue);
287 mptcp_drop(sk, skb1);
288 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
291 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
292 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
295 p = &parent->rb_right;
299 /* Insert segment into RB tree. */
300 rb_link_node(&skb->rbnode, parent, p);
301 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
304 /* Remove other segments covered by skb. */
305 while ((skb1 = skb_rb_next(skb)) != NULL) {
306 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
308 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
309 mptcp_drop(sk, skb1);
310 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
312 /* If there is no skb after us, we are the last_skb ! */
314 msk->ooo_last_skb = skb;
318 mptcp_set_owner_r(skb, sk);
321 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
323 struct mptcp_sock *msk = mptcp_sk(sk);
326 if (size < msk->rmem_fwd_alloc)
329 amt = sk_mem_pages(size);
330 amount = amt << SK_MEM_QUANTUM_SHIFT;
331 msk->rmem_fwd_alloc += amount;
332 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV)) {
333 if (ssk->sk_forward_alloc < amount) {
334 msk->rmem_fwd_alloc -= amount;
338 ssk->sk_forward_alloc -= amount;
343 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
344 struct sk_buff *skb, unsigned int offset,
347 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
348 struct sock *sk = (struct sock *)msk;
349 struct sk_buff *tail;
352 __skb_unlink(skb, &ssk->sk_receive_queue);
357 /* try to fetch required memory from subflow */
358 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
361 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
363 /* the skb map_seq accounts for the skb offset:
364 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
367 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
368 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
369 MPTCP_SKB_CB(skb)->offset = offset;
370 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
372 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
374 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
375 tail = skb_peek_tail(&sk->sk_receive_queue);
376 if (tail && mptcp_try_coalesce(sk, tail, skb))
379 mptcp_set_owner_r(skb, sk);
380 __skb_queue_tail(&sk->sk_receive_queue, skb);
382 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
383 mptcp_data_queue_ofo(msk, skb);
387 /* old data, keep it simple and drop the whole pkt, sender
388 * will retransmit as needed, if needed.
390 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
396 static void mptcp_stop_timer(struct sock *sk)
398 struct inet_connection_sock *icsk = inet_csk(sk);
400 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
401 mptcp_sk(sk)->timer_ival = 0;
404 static void mptcp_close_wake_up(struct sock *sk)
406 if (sock_flag(sk, SOCK_DEAD))
409 sk->sk_state_change(sk);
410 if (sk->sk_shutdown == SHUTDOWN_MASK ||
411 sk->sk_state == TCP_CLOSE)
412 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
414 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
417 static bool mptcp_pending_data_fin_ack(struct sock *sk)
419 struct mptcp_sock *msk = mptcp_sk(sk);
421 return !__mptcp_check_fallback(msk) &&
422 ((1 << sk->sk_state) &
423 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
424 msk->write_seq == READ_ONCE(msk->snd_una);
427 static void mptcp_check_data_fin_ack(struct sock *sk)
429 struct mptcp_sock *msk = mptcp_sk(sk);
431 /* Look for an acknowledged DATA_FIN */
432 if (mptcp_pending_data_fin_ack(sk)) {
433 WRITE_ONCE(msk->snd_data_fin_enable, 0);
435 switch (sk->sk_state) {
437 inet_sk_state_store(sk, TCP_FIN_WAIT2);
441 inet_sk_state_store(sk, TCP_CLOSE);
445 mptcp_close_wake_up(sk);
449 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
451 struct mptcp_sock *msk = mptcp_sk(sk);
453 if (READ_ONCE(msk->rcv_data_fin) &&
454 ((1 << sk->sk_state) &
455 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
456 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
458 if (msk->ack_seq == rcv_data_fin_seq) {
460 *seq = rcv_data_fin_seq;
469 static void mptcp_set_datafin_timeout(const struct sock *sk)
471 struct inet_connection_sock *icsk = inet_csk(sk);
474 retransmits = min_t(u32, icsk->icsk_retransmits,
475 ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
477 mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
480 static void __mptcp_set_timeout(struct sock *sk, long tout)
482 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
485 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
487 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
489 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
490 inet_csk(ssk)->icsk_timeout - jiffies : 0;
493 static void mptcp_set_timeout(struct sock *sk)
495 struct mptcp_subflow_context *subflow;
498 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
499 tout = max(tout, mptcp_timeout_from_subflow(subflow));
500 __mptcp_set_timeout(sk, tout);
503 static bool tcp_can_send_ack(const struct sock *ssk)
505 return !((1 << inet_sk_state_load(ssk)) &
506 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
509 void mptcp_subflow_send_ack(struct sock *ssk)
513 slow = lock_sock_fast(ssk);
514 if (tcp_can_send_ack(ssk))
516 unlock_sock_fast(ssk, slow);
519 static void mptcp_send_ack(struct mptcp_sock *msk)
521 struct mptcp_subflow_context *subflow;
523 mptcp_for_each_subflow(msk, subflow)
524 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
527 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
531 slow = lock_sock_fast(ssk);
532 if (tcp_can_send_ack(ssk))
533 tcp_cleanup_rbuf(ssk, 1);
534 unlock_sock_fast(ssk, slow);
537 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
539 const struct inet_connection_sock *icsk = inet_csk(ssk);
540 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
541 const struct tcp_sock *tp = tcp_sk(ssk);
543 return (ack_pending & ICSK_ACK_SCHED) &&
544 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
545 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
546 (rx_empty && ack_pending &
547 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
550 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
552 int old_space = READ_ONCE(msk->old_wspace);
553 struct mptcp_subflow_context *subflow;
554 struct sock *sk = (struct sock *)msk;
555 int space = __mptcp_space(sk);
556 bool cleanup, rx_empty;
558 cleanup = (space > 0) && (space >= (old_space << 1));
559 rx_empty = !__mptcp_rmem(sk);
561 mptcp_for_each_subflow(msk, subflow) {
562 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
564 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
565 mptcp_subflow_cleanup_rbuf(ssk);
569 static bool mptcp_check_data_fin(struct sock *sk)
571 struct mptcp_sock *msk = mptcp_sk(sk);
572 u64 rcv_data_fin_seq;
575 if (__mptcp_check_fallback(msk))
578 /* Need to ack a DATA_FIN received from a peer while this side
579 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
580 * msk->rcv_data_fin was set when parsing the incoming options
581 * at the subflow level and the msk lock was not held, so this
582 * is the first opportunity to act on the DATA_FIN and change
585 * If we are caught up to the sequence number of the incoming
586 * DATA_FIN, send the DATA_ACK now and do state transition. If
587 * not caught up, do nothing and let the recv code send DATA_ACK
591 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
592 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
593 WRITE_ONCE(msk->rcv_data_fin, 0);
595 sk->sk_shutdown |= RCV_SHUTDOWN;
596 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
598 switch (sk->sk_state) {
599 case TCP_ESTABLISHED:
600 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
603 inet_sk_state_store(sk, TCP_CLOSING);
606 inet_sk_state_store(sk, TCP_CLOSE);
609 /* Other states not expected */
616 mptcp_close_wake_up(sk);
621 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
625 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
626 struct sock *sk = (struct sock *)msk;
627 unsigned int moved = 0;
628 bool more_data_avail;
633 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
635 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
636 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
638 if (unlikely(ssk_rbuf > sk_rbuf)) {
639 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
644 pr_debug("msk=%p ssk=%p", msk, ssk);
647 u32 map_remaining, offset;
648 u32 seq = tp->copied_seq;
652 /* try to move as much data as available */
653 map_remaining = subflow->map_data_len -
654 mptcp_subflow_get_map_offset(subflow);
656 skb = skb_peek(&ssk->sk_receive_queue);
658 /* if no data is found, a racing workqueue/recvmsg
659 * already processed the new data, stop here or we
660 * can enter an infinite loop
667 if (__mptcp_check_fallback(msk)) {
668 /* if we are running under the workqueue, TCP could have
669 * collapsed skbs between dummy map creation and now
670 * be sure to adjust the size
672 map_remaining = skb->len;
673 subflow->map_data_len = skb->len;
676 offset = seq - TCP_SKB_CB(skb)->seq;
677 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
683 if (offset < skb->len) {
684 size_t len = skb->len - offset;
689 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
693 if (WARN_ON_ONCE(map_remaining < len))
697 sk_eat_skb(ssk, skb);
701 WRITE_ONCE(tp->copied_seq, seq);
702 more_data_avail = mptcp_subflow_data_available(ssk);
704 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
708 } while (more_data_avail);
714 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
716 struct sock *sk = (struct sock *)msk;
717 struct sk_buff *skb, *tail;
722 p = rb_first(&msk->out_of_order_queue);
723 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
726 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
730 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
732 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
735 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
739 end_seq = MPTCP_SKB_CB(skb)->end_seq;
740 tail = skb_peek_tail(&sk->sk_receive_queue);
741 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
742 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
744 /* skip overlapping data, if any */
745 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
746 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
748 MPTCP_SKB_CB(skb)->offset += delta;
749 MPTCP_SKB_CB(skb)->map_seq += delta;
750 __skb_queue_tail(&sk->sk_receive_queue, skb);
752 msk->ack_seq = end_seq;
758 /* In most cases we will be able to lock the mptcp socket. If its already
759 * owned, we need to defer to the work queue to avoid ABBA deadlock.
761 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
763 struct sock *sk = (struct sock *)msk;
764 unsigned int moved = 0;
766 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
767 __mptcp_ofo_queue(msk);
768 if (unlikely(ssk->sk_err)) {
769 if (!sock_owned_by_user(sk))
770 __mptcp_error_report(sk);
772 __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags);
775 /* If the moves have caught up with the DATA_FIN sequence number
776 * it's time to ack the DATA_FIN and change socket state, but
777 * this is not a good place to change state. Let the workqueue
780 if (mptcp_pending_data_fin(sk, NULL))
781 mptcp_schedule_work(sk);
785 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
787 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
788 struct mptcp_sock *msk = mptcp_sk(sk);
789 int sk_rbuf, ssk_rbuf;
791 /* The peer can send data while we are shutting down this
792 * subflow at msk destruction time, but we must avoid enqueuing
793 * more data to the msk receive queue
795 if (unlikely(subflow->disposable))
798 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
799 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
800 if (unlikely(ssk_rbuf > sk_rbuf))
803 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
804 if (__mptcp_rmem(sk) > sk_rbuf) {
805 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
809 /* Wake-up the reader only for in-sequence data */
811 if (move_skbs_to_msk(msk, ssk))
812 sk->sk_data_ready(sk);
814 mptcp_data_unlock(sk);
817 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
819 struct sock *sk = (struct sock *)msk;
821 if (sk->sk_state != TCP_ESTABLISHED)
824 /* attach to msk socket only after we are sure we will deal with it
827 if (sk->sk_socket && !ssk->sk_socket)
828 mptcp_sock_graft(ssk, sk->sk_socket);
830 mptcp_propagate_sndbuf((struct sock *)msk, ssk);
831 mptcp_sockopt_sync_locked(msk, ssk);
835 static void __mptcp_flush_join_list(struct sock *sk)
837 struct mptcp_subflow_context *tmp, *subflow;
838 struct mptcp_sock *msk = mptcp_sk(sk);
840 list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) {
841 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
842 bool slow = lock_sock_fast(ssk);
844 list_move_tail(&subflow->node, &msk->conn_list);
845 if (!__mptcp_finish_join(msk, ssk))
846 mptcp_subflow_reset(ssk);
847 unlock_sock_fast(ssk, slow);
851 static bool mptcp_timer_pending(struct sock *sk)
853 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
856 static void mptcp_reset_timer(struct sock *sk)
858 struct inet_connection_sock *icsk = inet_csk(sk);
861 /* prevent rescheduling on close */
862 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
865 tout = mptcp_sk(sk)->timer_ival;
866 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
869 bool mptcp_schedule_work(struct sock *sk)
871 if (inet_sk_state_load(sk) != TCP_CLOSE &&
872 schedule_work(&mptcp_sk(sk)->work)) {
873 /* each subflow already holds a reference to the sk, and the
874 * workqueue is invoked by a subflow, so sk can't go away here.
882 void mptcp_subflow_eof(struct sock *sk)
884 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
885 mptcp_schedule_work(sk);
888 static void mptcp_check_for_eof(struct mptcp_sock *msk)
890 struct mptcp_subflow_context *subflow;
891 struct sock *sk = (struct sock *)msk;
894 mptcp_for_each_subflow(msk, subflow)
895 receivers += !subflow->rx_eof;
899 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
900 /* hopefully temporary hack: propagate shutdown status
901 * to msk, when all subflows agree on it
903 sk->sk_shutdown |= RCV_SHUTDOWN;
905 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
906 sk->sk_data_ready(sk);
909 switch (sk->sk_state) {
910 case TCP_ESTABLISHED:
911 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
914 inet_sk_state_store(sk, TCP_CLOSING);
917 inet_sk_state_store(sk, TCP_CLOSE);
922 mptcp_close_wake_up(sk);
925 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
927 struct mptcp_subflow_context *subflow;
928 struct sock *sk = (struct sock *)msk;
930 sock_owned_by_me(sk);
932 mptcp_for_each_subflow(msk, subflow) {
933 if (READ_ONCE(subflow->data_avail))
934 return mptcp_subflow_tcp_sock(subflow);
940 static bool mptcp_skb_can_collapse_to(u64 write_seq,
941 const struct sk_buff *skb,
942 const struct mptcp_ext *mpext)
944 if (!tcp_skb_can_collapse_to(skb))
947 /* can collapse only if MPTCP level sequence is in order and this
948 * mapping has not been xmitted yet
950 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
954 /* we can append data to the given data frag if:
955 * - there is space available in the backing page_frag
956 * - the data frag tail matches the current page_frag free offset
957 * - the data frag end sequence number matches the current write seq
959 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
960 const struct page_frag *pfrag,
961 const struct mptcp_data_frag *df)
963 return df && pfrag->page == df->page &&
964 pfrag->size - pfrag->offset > 0 &&
965 pfrag->offset == (df->offset + df->data_len) &&
966 df->data_seq + df->data_len == msk->write_seq;
969 static void __mptcp_mem_reclaim_partial(struct sock *sk)
971 int reclaimable = mptcp_sk(sk)->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
973 lockdep_assert_held_once(&sk->sk_lock.slock);
975 if (reclaimable > SK_MEM_QUANTUM)
976 __mptcp_rmem_reclaim(sk, reclaimable - 1);
978 sk_mem_reclaim_partial(sk);
981 static void mptcp_mem_reclaim_partial(struct sock *sk)
984 __mptcp_mem_reclaim_partial(sk);
985 mptcp_data_unlock(sk);
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;
1007 bool cleaned = false;
1010 /* on fallback we just need to ignore snd_una, as this is really
1013 if (__mptcp_check_fallback(msk))
1014 msk->snd_una = READ_ONCE(msk->snd_nxt);
1016 snd_una = msk->snd_una;
1017 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1018 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1021 if (unlikely(dfrag == msk->first_pending)) {
1022 /* in recovery mode can see ack after the current snd head */
1023 if (WARN_ON_ONCE(!msk->recovery))
1026 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1029 dfrag_clear(sk, dfrag);
1033 dfrag = mptcp_rtx_head(sk);
1034 if (dfrag && after64(snd_una, dfrag->data_seq)) {
1035 u64 delta = snd_una - dfrag->data_seq;
1037 /* prevent wrap around in recovery mode */
1038 if (unlikely(delta > dfrag->already_sent)) {
1039 if (WARN_ON_ONCE(!msk->recovery))
1041 if (WARN_ON_ONCE(delta > dfrag->data_len))
1043 dfrag->already_sent += delta - dfrag->already_sent;
1046 dfrag->data_seq += delta;
1047 dfrag->offset += delta;
1048 dfrag->data_len -= delta;
1049 dfrag->already_sent -= delta;
1051 dfrag_uncharge(sk, delta);
1055 /* all retransmitted data acked, recovery completed */
1056 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1057 msk->recovery = false;
1060 if (cleaned && tcp_under_memory_pressure(sk))
1061 __mptcp_mem_reclaim_partial(sk);
1063 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1064 snd_una == READ_ONCE(msk->write_seq)) {
1065 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1066 mptcp_stop_timer(sk);
1068 mptcp_reset_timer(sk);
1072 static void __mptcp_clean_una_wakeup(struct sock *sk)
1074 lockdep_assert_held_once(&sk->sk_lock.slock);
1076 __mptcp_clean_una(sk);
1077 mptcp_write_space(sk);
1080 static void mptcp_clean_una_wakeup(struct sock *sk)
1082 mptcp_data_lock(sk);
1083 __mptcp_clean_una_wakeup(sk);
1084 mptcp_data_unlock(sk);
1087 static void mptcp_enter_memory_pressure(struct sock *sk)
1089 struct mptcp_subflow_context *subflow;
1090 struct mptcp_sock *msk = mptcp_sk(sk);
1093 sk_stream_moderate_sndbuf(sk);
1094 mptcp_for_each_subflow(msk, subflow) {
1095 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1098 tcp_enter_memory_pressure(ssk);
1099 sk_stream_moderate_sndbuf(ssk);
1104 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1107 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1109 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1110 pfrag, sk->sk_allocation)))
1113 mptcp_enter_memory_pressure(sk);
1117 static struct mptcp_data_frag *
1118 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1121 int offset = ALIGN(orig_offset, sizeof(long));
1122 struct mptcp_data_frag *dfrag;
1124 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1125 dfrag->data_len = 0;
1126 dfrag->data_seq = msk->write_seq;
1127 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1128 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1129 dfrag->already_sent = 0;
1130 dfrag->page = pfrag->page;
1135 struct mptcp_sendmsg_info {
1141 bool data_lock_held;
1144 static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq,
1147 u64 window_end = mptcp_wnd_end(msk);
1149 if (__mptcp_check_fallback(msk))
1152 if (!before64(data_seq + avail_size, window_end)) {
1153 u64 allowed_size = window_end - data_seq;
1155 return min_t(unsigned int, allowed_size, avail_size);
1161 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1163 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1167 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1171 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1173 struct sk_buff *skb;
1175 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1177 if (likely(__mptcp_add_ext(skb, gfp))) {
1178 skb_reserve(skb, MAX_TCP_HEADER);
1179 skb->ip_summed = CHECKSUM_PARTIAL;
1180 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1185 mptcp_enter_memory_pressure(sk);
1190 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1192 struct sk_buff *skb;
1194 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1198 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1199 tcp_skb_entail(ssk, skb);
1202 tcp_skb_tsorted_anchor_cleanup(skb);
1207 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1209 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1211 if (unlikely(tcp_under_memory_pressure(sk))) {
1213 __mptcp_mem_reclaim_partial(sk);
1215 mptcp_mem_reclaim_partial(sk);
1217 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1220 /* note: this always recompute the csum on the whole skb, even
1221 * if we just appended a single frag. More status info needed
1223 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1225 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1226 __wsum csum = ~csum_unfold(mpext->csum);
1227 int offset = skb->len - added;
1229 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1232 static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1234 struct mptcp_ext *mpext)
1239 mpext->infinite_map = 1;
1240 mpext->data_len = 0;
1242 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1243 mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1245 __mptcp_do_fallback(msk);
1248 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1249 struct mptcp_data_frag *dfrag,
1250 struct mptcp_sendmsg_info *info)
1252 u64 data_seq = dfrag->data_seq + info->sent;
1253 int offset = dfrag->offset + info->sent;
1254 struct mptcp_sock *msk = mptcp_sk(sk);
1255 bool zero_window_probe = false;
1256 struct mptcp_ext *mpext = NULL;
1257 bool can_coalesce = false;
1258 bool reuse_skb = true;
1259 struct sk_buff *skb;
1263 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1264 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1266 if (WARN_ON_ONCE(info->sent > info->limit ||
1267 info->limit > dfrag->data_len))
1270 /* compute send limit */
1271 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1272 copy = info->size_goal;
1274 skb = tcp_write_queue_tail(ssk);
1275 if (skb && copy > skb->len) {
1276 /* Limit the write to the size available in the
1277 * current skb, if any, so that we create at most a new skb.
1278 * Explicitly tells TCP internals to avoid collapsing on later
1279 * queue management operation, to avoid breaking the ext <->
1280 * SSN association set here
1282 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1283 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1284 TCP_SKB_CB(skb)->eor = 1;
1288 i = skb_shinfo(skb)->nr_frags;
1289 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1290 if (!can_coalesce && i >= sysctl_max_skb_frags) {
1291 tcp_mark_push(tcp_sk(ssk), skb);
1298 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1302 i = skb_shinfo(skb)->nr_frags;
1304 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1307 /* Zero window and all data acked? Probe. */
1308 copy = mptcp_check_allowed_size(msk, data_seq, copy);
1310 u64 snd_una = READ_ONCE(msk->snd_una);
1312 if (snd_una != msk->snd_nxt) {
1313 tcp_remove_empty_skb(ssk);
1317 zero_window_probe = true;
1318 data_seq = snd_una - 1;
1321 /* all mptcp-level data is acked, no skbs should be present into the
1324 WARN_ON_ONCE(reuse_skb);
1327 copy = min_t(size_t, copy, info->limit - info->sent);
1328 if (!sk_wmem_schedule(ssk, copy)) {
1329 tcp_remove_empty_skb(ssk);
1334 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1336 get_page(dfrag->page);
1337 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1341 skb->data_len += copy;
1342 skb->truesize += copy;
1343 sk_wmem_queued_add(ssk, copy);
1344 sk_mem_charge(ssk, copy);
1345 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1346 TCP_SKB_CB(skb)->end_seq += copy;
1347 tcp_skb_pcount_set(skb, 0);
1349 /* on skb reuse we just need to update the DSS len */
1351 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1352 mpext->data_len += copy;
1353 WARN_ON_ONCE(zero_window_probe);
1357 memset(mpext, 0, sizeof(*mpext));
1358 mpext->data_seq = data_seq;
1359 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1360 mpext->data_len = copy;
1364 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1365 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1368 if (zero_window_probe) {
1369 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1371 if (READ_ONCE(msk->csum_enabled))
1372 mptcp_update_data_checksum(skb, copy);
1373 tcp_push_pending_frames(ssk);
1377 if (READ_ONCE(msk->csum_enabled))
1378 mptcp_update_data_checksum(skb, copy);
1379 if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1380 mptcp_update_infinite_map(msk, ssk, mpext);
1381 trace_mptcp_sendmsg_frag(mpext);
1382 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1386 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1387 sizeof(struct tcphdr) - \
1388 MAX_TCP_OPTION_SPACE - \
1389 sizeof(struct ipv6hdr) - \
1390 sizeof(struct frag_hdr))
1392 struct subflow_send_info {
1397 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1399 if (!subflow->stale)
1403 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1406 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1408 if (unlikely(subflow->stale)) {
1409 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1411 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1414 mptcp_subflow_set_active(subflow);
1416 return __mptcp_subflow_active(subflow);
1419 #define SSK_MODE_ACTIVE 0
1420 #define SSK_MODE_BACKUP 1
1421 #define SSK_MODE_MAX 2
1423 /* implement the mptcp packet scheduler;
1424 * returns the subflow that will transmit the next DSS
1425 * additionally updates the rtx timeout
1427 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1429 struct subflow_send_info send_info[SSK_MODE_MAX];
1430 struct mptcp_subflow_context *subflow;
1431 struct sock *sk = (struct sock *)msk;
1432 u32 pace, burst, wmem;
1433 int i, nr_active = 0;
1438 sock_owned_by_me(sk);
1440 if (__mptcp_check_fallback(msk)) {
1443 return sk_stream_memory_free(msk->first) ? msk->first : NULL;
1446 /* re-use last subflow, if the burst allow that */
1447 if (msk->last_snd && msk->snd_burst > 0 &&
1448 sk_stream_memory_free(msk->last_snd) &&
1449 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1450 mptcp_set_timeout(sk);
1451 return msk->last_snd;
1454 /* pick the subflow with the lower wmem/wspace ratio */
1455 for (i = 0; i < SSK_MODE_MAX; ++i) {
1456 send_info[i].ssk = NULL;
1457 send_info[i].linger_time = -1;
1460 mptcp_for_each_subflow(msk, subflow) {
1461 trace_mptcp_subflow_get_send(subflow);
1462 ssk = mptcp_subflow_tcp_sock(subflow);
1463 if (!mptcp_subflow_active(subflow))
1466 tout = max(tout, mptcp_timeout_from_subflow(subflow));
1467 nr_active += !subflow->backup;
1468 pace = subflow->avg_pacing_rate;
1469 if (unlikely(!pace)) {
1470 /* init pacing rate from socket */
1471 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1472 pace = subflow->avg_pacing_rate;
1477 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1478 if (linger_time < send_info[subflow->backup].linger_time) {
1479 send_info[subflow->backup].ssk = ssk;
1480 send_info[subflow->backup].linger_time = linger_time;
1483 __mptcp_set_timeout(sk, tout);
1485 /* pick the best backup if no other subflow is active */
1487 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1489 /* According to the blest algorithm, to avoid HoL blocking for the
1490 * faster flow, we need to:
1491 * - estimate the faster flow linger time
1492 * - use the above to estimate the amount of byte transferred
1493 * by the faster flow
1494 * - check that the amount of queued data is greter than the above,
1495 * otherwise do not use the picked, slower, subflow
1496 * We select the subflow with the shorter estimated time to flush
1497 * the queued mem, which basically ensure the above. We just need
1498 * to check that subflow has a non empty cwin.
1500 ssk = send_info[SSK_MODE_ACTIVE].ssk;
1501 if (!ssk || !sk_stream_memory_free(ssk) || !tcp_sk(ssk)->snd_wnd)
1504 burst = min_t(int, MPTCP_SEND_BURST_SIZE, tcp_sk(ssk)->snd_wnd);
1505 wmem = READ_ONCE(ssk->sk_wmem_queued);
1506 subflow = mptcp_subflow_ctx(ssk);
1507 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1508 READ_ONCE(ssk->sk_pacing_rate) * burst,
1510 msk->last_snd = ssk;
1511 msk->snd_burst = burst;
1515 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1517 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1521 static void mptcp_update_post_push(struct mptcp_sock *msk,
1522 struct mptcp_data_frag *dfrag,
1525 u64 snd_nxt_new = dfrag->data_seq;
1527 dfrag->already_sent += sent;
1529 msk->snd_burst -= sent;
1531 snd_nxt_new += dfrag->already_sent;
1533 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1534 * is recovering after a failover. In that event, this re-sends
1537 * Thus compute snd_nxt_new candidate based on
1538 * the dfrag->data_seq that was sent and the data
1539 * that has been handed to the subflow for transmission
1540 * and skip update in case it was old dfrag.
1542 if (likely(after64(snd_nxt_new, msk->snd_nxt)))
1543 msk->snd_nxt = snd_nxt_new;
1546 void mptcp_check_and_set_pending(struct sock *sk)
1548 if (mptcp_send_head(sk))
1549 mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING);
1552 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1554 struct sock *prev_ssk = NULL, *ssk = NULL;
1555 struct mptcp_sock *msk = mptcp_sk(sk);
1556 struct mptcp_sendmsg_info info = {
1559 struct mptcp_data_frag *dfrag;
1560 int len, copied = 0;
1562 while ((dfrag = mptcp_send_head(sk))) {
1563 info.sent = dfrag->already_sent;
1564 info.limit = dfrag->data_len;
1565 len = dfrag->data_len - dfrag->already_sent;
1570 ssk = mptcp_subflow_get_send(msk);
1572 /* First check. If the ssk has changed since
1573 * the last round, release prev_ssk
1575 if (ssk != prev_ssk && prev_ssk)
1576 mptcp_push_release(prev_ssk, &info);
1580 /* Need to lock the new subflow only if different
1581 * from the previous one, otherwise we are still
1582 * helding the relevant lock
1584 if (ssk != prev_ssk)
1587 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1589 mptcp_push_release(ssk, &info);
1597 mptcp_update_post_push(msk, dfrag, ret);
1599 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1602 /* at this point we held the socket lock for the last subflow we used */
1604 mptcp_push_release(ssk, &info);
1607 /* ensure the rtx timer is running */
1608 if (!mptcp_timer_pending(sk))
1609 mptcp_reset_timer(sk);
1611 __mptcp_check_send_data_fin(sk);
1614 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
1616 struct mptcp_sock *msk = mptcp_sk(sk);
1617 struct mptcp_sendmsg_info info = {
1618 .data_lock_held = true,
1620 struct mptcp_data_frag *dfrag;
1621 struct sock *xmit_ssk;
1622 int len, copied = 0;
1626 while ((dfrag = mptcp_send_head(sk))) {
1627 info.sent = dfrag->already_sent;
1628 info.limit = dfrag->data_len;
1629 len = dfrag->data_len - dfrag->already_sent;
1633 /* the caller already invoked the packet scheduler,
1634 * check for a different subflow usage only after
1635 * spooling the first chunk of data
1637 xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
1640 if (xmit_ssk != ssk) {
1641 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk),
1642 MPTCP_DELEGATE_SEND);
1646 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1655 mptcp_update_post_push(msk, dfrag, ret);
1657 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1661 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1662 * not going to flush it via release_sock()
1665 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1667 if (!mptcp_timer_pending(sk))
1668 mptcp_reset_timer(sk);
1670 if (msk->snd_data_fin_enable &&
1671 msk->snd_nxt + 1 == msk->write_seq)
1672 mptcp_schedule_work(sk);
1676 static void mptcp_set_nospace(struct sock *sk)
1678 /* enable autotune */
1679 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1681 /* will be cleared on avail space */
1682 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1685 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1687 struct mptcp_sock *msk = mptcp_sk(sk);
1688 struct page_frag *pfrag;
1693 /* we don't support FASTOPEN yet */
1694 if (msg->msg_flags & MSG_FASTOPEN)
1697 /* silently ignore everything else */
1698 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL;
1702 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1704 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1705 ret = sk_stream_wait_connect(sk, &timeo);
1710 pfrag = sk_page_frag(sk);
1712 while (msg_data_left(msg)) {
1713 int total_ts, frag_truesize = 0;
1714 struct mptcp_data_frag *dfrag;
1715 bool dfrag_collapsed;
1716 size_t psize, offset;
1718 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
1723 /* reuse tail pfrag, if possible, or carve a new one from the
1726 dfrag = mptcp_pending_tail(sk);
1727 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1728 if (!dfrag_collapsed) {
1729 if (!sk_stream_memory_free(sk))
1730 goto wait_for_memory;
1732 if (!mptcp_page_frag_refill(sk, pfrag))
1733 goto wait_for_memory;
1735 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1736 frag_truesize = dfrag->overhead;
1739 /* we do not bound vs wspace, to allow a single packet.
1740 * memory accounting will prevent execessive memory usage
1743 offset = dfrag->offset + dfrag->data_len;
1744 psize = pfrag->size - offset;
1745 psize = min_t(size_t, psize, msg_data_left(msg));
1746 total_ts = psize + frag_truesize;
1748 if (!sk_wmem_schedule(sk, total_ts))
1749 goto wait_for_memory;
1751 if (copy_page_from_iter(dfrag->page, offset, psize,
1752 &msg->msg_iter) != psize) {
1757 /* data successfully copied into the write queue */
1758 sk->sk_forward_alloc -= total_ts;
1760 dfrag->data_len += psize;
1761 frag_truesize += psize;
1762 pfrag->offset += frag_truesize;
1763 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1765 /* charge data on mptcp pending queue to the msk socket
1766 * Note: we charge such data both to sk and ssk
1768 sk_wmem_queued_add(sk, frag_truesize);
1769 if (!dfrag_collapsed) {
1770 get_page(dfrag->page);
1771 list_add_tail(&dfrag->list, &msk->rtx_queue);
1772 if (!msk->first_pending)
1773 WRITE_ONCE(msk->first_pending, dfrag);
1775 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1776 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1782 mptcp_set_nospace(sk);
1783 __mptcp_push_pending(sk, msg->msg_flags);
1784 ret = sk_stream_wait_memory(sk, &timeo);
1790 __mptcp_push_pending(sk, msg->msg_flags);
1794 return copied ? : ret;
1797 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1799 size_t len, int flags,
1800 struct scm_timestamping_internal *tss,
1803 struct sk_buff *skb, *tmp;
1806 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1807 u32 offset = MPTCP_SKB_CB(skb)->offset;
1808 u32 data_len = skb->len - offset;
1809 u32 count = min_t(size_t, len - copied, data_len);
1812 if (!(flags & MSG_TRUNC)) {
1813 err = skb_copy_datagram_msg(skb, offset, msg, count);
1814 if (unlikely(err < 0)) {
1821 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1822 tcp_update_recv_tstamps(skb, tss);
1823 *cmsg_flags |= MPTCP_CMSG_TS;
1828 if (count < data_len) {
1829 if (!(flags & MSG_PEEK)) {
1830 MPTCP_SKB_CB(skb)->offset += count;
1831 MPTCP_SKB_CB(skb)->map_seq += count;
1836 if (!(flags & MSG_PEEK)) {
1837 /* we will bulk release the skb memory later */
1838 skb->destructor = NULL;
1839 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1840 __skb_unlink(skb, &msk->receive_queue);
1851 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1853 * Only difference: Use highest rtt estimate of the subflows in use.
1855 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1857 struct mptcp_subflow_context *subflow;
1858 struct sock *sk = (struct sock *)msk;
1859 u32 time, advmss = 1;
1862 sock_owned_by_me(sk);
1867 msk->rcvq_space.copied += copied;
1869 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1870 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1872 rtt_us = msk->rcvq_space.rtt_us;
1873 if (rtt_us && time < (rtt_us >> 3))
1877 mptcp_for_each_subflow(msk, subflow) {
1878 const struct tcp_sock *tp;
1882 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1884 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1885 sf_advmss = READ_ONCE(tp->advmss);
1887 rtt_us = max(sf_rtt_us, rtt_us);
1888 advmss = max(sf_advmss, advmss);
1891 msk->rcvq_space.rtt_us = rtt_us;
1892 if (time < (rtt_us >> 3) || rtt_us == 0)
1895 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1898 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
1899 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1903 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1905 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1907 do_div(grow, msk->rcvq_space.space);
1908 rcvwin += (grow << 1);
1910 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1911 while (tcp_win_from_space(sk, rcvmem) < advmss)
1914 do_div(rcvwin, advmss);
1915 rcvbuf = min_t(u64, rcvwin * rcvmem,
1916 sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
1918 if (rcvbuf > sk->sk_rcvbuf) {
1921 window_clamp = tcp_win_from_space(sk, rcvbuf);
1922 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1924 /* Make subflows follow along. If we do not do this, we
1925 * get drops at subflow level if skbs can't be moved to
1926 * the mptcp rx queue fast enough (announced rcv_win can
1927 * exceed ssk->sk_rcvbuf).
1929 mptcp_for_each_subflow(msk, subflow) {
1933 ssk = mptcp_subflow_tcp_sock(subflow);
1934 slow = lock_sock_fast(ssk);
1935 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1936 tcp_sk(ssk)->window_clamp = window_clamp;
1937 tcp_cleanup_rbuf(ssk, 1);
1938 unlock_sock_fast(ssk, slow);
1943 msk->rcvq_space.space = msk->rcvq_space.copied;
1945 msk->rcvq_space.copied = 0;
1946 msk->rcvq_space.time = mstamp;
1949 static void __mptcp_update_rmem(struct sock *sk)
1951 struct mptcp_sock *msk = mptcp_sk(sk);
1953 if (!msk->rmem_released)
1956 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
1957 mptcp_rmem_uncharge(sk, msk->rmem_released);
1958 WRITE_ONCE(msk->rmem_released, 0);
1961 static void __mptcp_splice_receive_queue(struct sock *sk)
1963 struct mptcp_sock *msk = mptcp_sk(sk);
1965 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
1968 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
1970 struct sock *sk = (struct sock *)msk;
1971 unsigned int moved = 0;
1975 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
1978 /* we can have data pending in the subflows only if the msk
1979 * receive buffer was full at subflow_data_ready() time,
1980 * that is an unlikely slow path.
1985 slowpath = lock_sock_fast(ssk);
1986 mptcp_data_lock(sk);
1987 __mptcp_update_rmem(sk);
1988 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
1989 mptcp_data_unlock(sk);
1991 if (unlikely(ssk->sk_err))
1992 __mptcp_error_report(sk);
1993 unlock_sock_fast(ssk, slowpath);
1996 /* acquire the data lock only if some input data is pending */
1998 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
1999 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
2000 mptcp_data_lock(sk);
2001 __mptcp_update_rmem(sk);
2002 ret |= __mptcp_ofo_queue(msk);
2003 __mptcp_splice_receive_queue(sk);
2004 mptcp_data_unlock(sk);
2007 mptcp_check_data_fin((struct sock *)msk);
2008 return !skb_queue_empty(&msk->receive_queue);
2011 static unsigned int mptcp_inq_hint(const struct sock *sk)
2013 const struct mptcp_sock *msk = mptcp_sk(sk);
2014 const struct sk_buff *skb;
2016 skb = skb_peek(&msk->receive_queue);
2018 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
2020 if (hint_val >= INT_MAX)
2023 return (unsigned int)hint_val;
2026 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2032 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2033 int flags, int *addr_len)
2035 struct mptcp_sock *msk = mptcp_sk(sk);
2036 struct scm_timestamping_internal tss;
2037 int copied = 0, cmsg_flags = 0;
2041 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2042 if (unlikely(flags & MSG_ERRQUEUE))
2043 return inet_recv_error(sk, msg, len, addr_len);
2046 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2051 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2053 len = min_t(size_t, len, INT_MAX);
2054 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2056 if (unlikely(msk->recvmsg_inq))
2057 cmsg_flags = MPTCP_CMSG_INQ;
2059 while (copied < len) {
2062 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2063 if (unlikely(bytes_read < 0)) {
2065 copied = bytes_read;
2069 copied += bytes_read;
2071 /* be sure to advertise window change */
2072 mptcp_cleanup_rbuf(msk);
2074 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2077 /* only the master socket status is relevant here. The exit
2078 * conditions mirror closely tcp_recvmsg()
2080 if (copied >= target)
2085 sk->sk_state == TCP_CLOSE ||
2086 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2088 signal_pending(current))
2092 copied = sock_error(sk);
2096 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2097 mptcp_check_for_eof(msk);
2099 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2100 /* race breaker: the shutdown could be after the
2101 * previous receive queue check
2103 if (__mptcp_move_skbs(msk))
2108 if (sk->sk_state == TCP_CLOSE) {
2118 if (signal_pending(current)) {
2119 copied = sock_intr_errno(timeo);
2124 pr_debug("block timeout %ld", timeo);
2125 sk_wait_data(sk, &timeo, NULL);
2129 if (cmsg_flags && copied >= 0) {
2130 if (cmsg_flags & MPTCP_CMSG_TS)
2131 tcp_recv_timestamp(msg, sk, &tss);
2133 if (cmsg_flags & MPTCP_CMSG_INQ) {
2134 unsigned int inq = mptcp_inq_hint(sk);
2136 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2140 pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2141 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2142 skb_queue_empty(&msk->receive_queue), copied);
2143 if (!(flags & MSG_PEEK))
2144 mptcp_rcv_space_adjust(msk, copied);
2150 static void mptcp_retransmit_timer(struct timer_list *t)
2152 struct inet_connection_sock *icsk = from_timer(icsk, t,
2153 icsk_retransmit_timer);
2154 struct sock *sk = &icsk->icsk_inet.sk;
2155 struct mptcp_sock *msk = mptcp_sk(sk);
2158 if (!sock_owned_by_user(sk)) {
2159 /* we need a process context to retransmit */
2160 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2161 mptcp_schedule_work(sk);
2163 /* delegate our work to tcp_release_cb() */
2164 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2170 static void mptcp_timeout_timer(struct timer_list *t)
2172 struct sock *sk = from_timer(sk, t, sk_timer);
2174 mptcp_schedule_work(sk);
2178 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2181 * A backup subflow is returned only if that is the only kind available.
2183 static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2185 struct sock *backup = NULL, *pick = NULL;
2186 struct mptcp_subflow_context *subflow;
2187 int min_stale_count = INT_MAX;
2189 sock_owned_by_me((const struct sock *)msk);
2191 if (__mptcp_check_fallback(msk))
2194 mptcp_for_each_subflow(msk, subflow) {
2195 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2197 if (!__mptcp_subflow_active(subflow))
2200 /* still data outstanding at TCP level? skip this */
2201 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2202 mptcp_pm_subflow_chk_stale(msk, ssk);
2203 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2207 if (subflow->backup) {
2220 /* use backup only if there are no progresses anywhere */
2221 return min_stale_count > 1 ? backup : NULL;
2224 static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2227 iput(SOCK_INODE(msk->subflow));
2228 msk->subflow = NULL;
2232 bool __mptcp_retransmit_pending_data(struct sock *sk)
2234 struct mptcp_data_frag *cur, *rtx_head;
2235 struct mptcp_sock *msk = mptcp_sk(sk);
2237 if (__mptcp_check_fallback(mptcp_sk(sk)))
2240 if (tcp_rtx_and_write_queues_empty(sk))
2243 /* the closing socket has some data untransmitted and/or unacked:
2244 * some data in the mptcp rtx queue has not really xmitted yet.
2245 * keep it simple and re-inject the whole mptcp level rtx queue
2247 mptcp_data_lock(sk);
2248 __mptcp_clean_una_wakeup(sk);
2249 rtx_head = mptcp_rtx_head(sk);
2251 mptcp_data_unlock(sk);
2255 msk->recovery_snd_nxt = msk->snd_nxt;
2256 msk->recovery = true;
2257 mptcp_data_unlock(sk);
2259 msk->first_pending = rtx_head;
2262 /* be sure to clear the "sent status" on all re-injected fragments */
2263 list_for_each_entry(cur, &msk->rtx_queue, list) {
2264 if (!cur->already_sent)
2266 cur->already_sent = 0;
2272 /* flags for __mptcp_close_ssk() */
2273 #define MPTCP_CF_PUSH BIT(1)
2274 #define MPTCP_CF_FASTCLOSE BIT(2)
2276 /* subflow sockets can be either outgoing (connect) or incoming
2279 * Outgoing subflows use in-kernel sockets.
2280 * Incoming subflows do not have their own 'struct socket' allocated,
2281 * so we need to use tcp_close() after detaching them from the mptcp
2284 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2285 struct mptcp_subflow_context *subflow,
2288 struct mptcp_sock *msk = mptcp_sk(sk);
2289 bool need_push, dispose_it;
2291 dispose_it = !msk->subflow || ssk != msk->subflow->sk;
2293 list_del(&subflow->node);
2295 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2297 if (flags & MPTCP_CF_FASTCLOSE)
2298 subflow->send_fastclose = 1;
2300 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2302 tcp_disconnect(ssk, 0);
2303 msk->subflow->state = SS_UNCONNECTED;
2304 mptcp_subflow_ctx_reset(subflow);
2310 /* if we are invoked by the msk cleanup code, the subflow is
2316 subflow->disposable = 1;
2318 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2319 * the ssk has been already destroyed, we just need to release the
2320 * reference owned by msk;
2322 if (!inet_csk(ssk)->icsk_ulp_ops) {
2323 kfree_rcu(subflow, rcu);
2325 /* otherwise tcp will dispose of the ssk and subflow ctx */
2326 __tcp_close(ssk, 0);
2328 /* close acquired an extra ref */
2335 if (ssk == msk->first)
2339 if (ssk == msk->last_snd)
2340 msk->last_snd = NULL;
2343 __mptcp_push_pending(sk, 0);
2346 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2347 struct mptcp_subflow_context *subflow)
2349 if (sk->sk_state == TCP_ESTABLISHED)
2350 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2352 /* subflow aborted before reaching the fully_established status
2353 * attempt the creation of the next subflow
2355 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2357 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2360 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2365 static void __mptcp_close_subflow(struct mptcp_sock *msk)
2367 struct mptcp_subflow_context *subflow, *tmp;
2371 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2372 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2374 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2377 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2378 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2381 mptcp_close_ssk((struct sock *)msk, ssk, subflow);
2385 static bool mptcp_check_close_timeout(const struct sock *sk)
2387 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2388 struct mptcp_subflow_context *subflow;
2390 if (delta >= TCP_TIMEWAIT_LEN)
2393 /* if all subflows are in closed status don't bother with additional
2396 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2397 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2404 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2406 struct mptcp_subflow_context *subflow, *tmp;
2407 struct sock *sk = &msk->sk.icsk_inet.sk;
2409 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2412 mptcp_token_destroy(msk);
2414 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2415 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2418 slow = lock_sock_fast(tcp_sk);
2419 if (tcp_sk->sk_state != TCP_CLOSE) {
2420 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2421 tcp_set_state(tcp_sk, TCP_CLOSE);
2423 unlock_sock_fast(tcp_sk, slow);
2426 inet_sk_state_store(sk, TCP_CLOSE);
2427 sk->sk_shutdown = SHUTDOWN_MASK;
2428 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2429 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2431 mptcp_close_wake_up(sk);
2434 static void __mptcp_retrans(struct sock *sk)
2436 struct mptcp_sock *msk = mptcp_sk(sk);
2437 struct mptcp_sendmsg_info info = {};
2438 struct mptcp_data_frag *dfrag;
2443 mptcp_clean_una_wakeup(sk);
2445 /* first check ssk: need to kick "stale" logic */
2446 ssk = mptcp_subflow_get_retrans(msk);
2447 dfrag = mptcp_rtx_head(sk);
2449 if (mptcp_data_fin_enabled(msk)) {
2450 struct inet_connection_sock *icsk = inet_csk(sk);
2452 icsk->icsk_retransmits++;
2453 mptcp_set_datafin_timeout(sk);
2454 mptcp_send_ack(msk);
2459 if (!mptcp_send_head(sk))
2470 /* limit retransmission to the bytes already sent on some subflows */
2472 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
2473 while (info.sent < info.limit) {
2474 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2478 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2483 dfrag->already_sent = max(dfrag->already_sent, info.sent);
2484 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2486 WRITE_ONCE(msk->allow_infinite_fallback, false);
2492 mptcp_check_and_set_pending(sk);
2494 if (!mptcp_timer_pending(sk))
2495 mptcp_reset_timer(sk);
2498 static void mptcp_worker(struct work_struct *work)
2500 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2501 struct sock *sk = &msk->sk.icsk_inet.sk;
2505 state = sk->sk_state;
2506 if (unlikely(state == TCP_CLOSE))
2509 mptcp_check_data_fin_ack(sk);
2511 mptcp_check_fastclose(msk);
2513 mptcp_pm_nl_work(msk);
2515 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2516 mptcp_check_for_eof(msk);
2518 __mptcp_check_send_data_fin(sk);
2519 mptcp_check_data_fin(sk);
2521 /* There is no point in keeping around an orphaned sk timedout or
2522 * closed, but we need the msk around to reply to incoming DATA_FIN,
2523 * even if it is orphaned and in FIN_WAIT2 state
2525 if (sock_flag(sk, SOCK_DEAD) &&
2526 (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) {
2527 inet_sk_state_store(sk, TCP_CLOSE);
2528 __mptcp_destroy_sock(sk);
2532 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2533 __mptcp_close_subflow(msk);
2535 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2536 __mptcp_retrans(sk);
2543 static int __mptcp_init_sock(struct sock *sk)
2545 struct mptcp_sock *msk = mptcp_sk(sk);
2547 INIT_LIST_HEAD(&msk->conn_list);
2548 INIT_LIST_HEAD(&msk->join_list);
2549 INIT_LIST_HEAD(&msk->rtx_queue);
2550 INIT_WORK(&msk->work, mptcp_worker);
2551 __skb_queue_head_init(&msk->receive_queue);
2552 msk->out_of_order_queue = RB_ROOT;
2553 msk->first_pending = NULL;
2554 msk->rmem_fwd_alloc = 0;
2555 WRITE_ONCE(msk->rmem_released, 0);
2556 msk->timer_ival = TCP_RTO_MIN;
2559 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2560 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2561 WRITE_ONCE(msk->allow_infinite_fallback, true);
2562 msk->recovery = false;
2564 mptcp_pm_data_init(msk);
2566 /* re-use the csk retrans timer for MPTCP-level retrans */
2567 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2568 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2573 static void mptcp_ca_reset(struct sock *sk)
2575 struct inet_connection_sock *icsk = inet_csk(sk);
2577 tcp_assign_congestion_control(sk);
2578 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2580 /* no need to keep a reference to the ops, the name will suffice */
2581 tcp_cleanup_congestion_control(sk);
2582 icsk->icsk_ca_ops = NULL;
2585 static int mptcp_init_sock(struct sock *sk)
2587 struct net *net = sock_net(sk);
2590 ret = __mptcp_init_sock(sk);
2594 if (!mptcp_is_enabled(net))
2595 return -ENOPROTOOPT;
2597 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2600 ret = __mptcp_socket_create(mptcp_sk(sk));
2604 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2605 * propagate the correct value
2609 sk_sockets_allocated_inc(sk);
2610 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
2611 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
2616 static void __mptcp_clear_xmit(struct sock *sk)
2618 struct mptcp_sock *msk = mptcp_sk(sk);
2619 struct mptcp_data_frag *dtmp, *dfrag;
2621 WRITE_ONCE(msk->first_pending, NULL);
2622 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2623 dfrag_clear(sk, dfrag);
2626 static void mptcp_cancel_work(struct sock *sk)
2628 struct mptcp_sock *msk = mptcp_sk(sk);
2630 if (cancel_work_sync(&msk->work))
2634 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2638 switch (ssk->sk_state) {
2640 if (!(how & RCV_SHUTDOWN))
2644 tcp_disconnect(ssk, O_NONBLOCK);
2647 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2648 pr_debug("Fallback");
2649 ssk->sk_shutdown |= how;
2650 tcp_shutdown(ssk, how);
2652 pr_debug("Sending DATA_FIN on subflow %p", ssk);
2654 if (!mptcp_timer_pending(sk))
2655 mptcp_reset_timer(sk);
2663 static const unsigned char new_state[16] = {
2664 /* current state: new state: action: */
2665 [0 /* (Invalid) */] = TCP_CLOSE,
2666 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2667 [TCP_SYN_SENT] = TCP_CLOSE,
2668 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2669 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2670 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2671 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2672 [TCP_CLOSE] = TCP_CLOSE,
2673 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2674 [TCP_LAST_ACK] = TCP_LAST_ACK,
2675 [TCP_LISTEN] = TCP_CLOSE,
2676 [TCP_CLOSING] = TCP_CLOSING,
2677 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2680 static int mptcp_close_state(struct sock *sk)
2682 int next = (int)new_state[sk->sk_state];
2683 int ns = next & TCP_STATE_MASK;
2685 inet_sk_state_store(sk, ns);
2687 return next & TCP_ACTION_FIN;
2690 static void __mptcp_check_send_data_fin(struct sock *sk)
2692 struct mptcp_subflow_context *subflow;
2693 struct mptcp_sock *msk = mptcp_sk(sk);
2695 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2696 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2697 msk->snd_nxt, msk->write_seq);
2699 /* we still need to enqueue subflows or not really shutting down,
2702 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2703 mptcp_send_head(sk))
2706 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2708 /* fallback socket will not get data_fin/ack, can move to the next
2711 if (__mptcp_check_fallback(msk)) {
2712 WRITE_ONCE(msk->snd_una, msk->write_seq);
2713 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2714 inet_sk_state_store(sk, TCP_CLOSE);
2715 mptcp_close_wake_up(sk);
2716 } else if (sk->sk_state == TCP_FIN_WAIT1) {
2717 inet_sk_state_store(sk, TCP_FIN_WAIT2);
2721 mptcp_for_each_subflow(msk, subflow) {
2722 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2724 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2728 static void __mptcp_wr_shutdown(struct sock *sk)
2730 struct mptcp_sock *msk = mptcp_sk(sk);
2732 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2733 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2734 !!mptcp_send_head(sk));
2736 /* will be ignored by fallback sockets */
2737 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2738 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2740 __mptcp_check_send_data_fin(sk);
2743 static void __mptcp_destroy_sock(struct sock *sk)
2745 struct mptcp_subflow_context *subflow, *tmp;
2746 struct mptcp_sock *msk = mptcp_sk(sk);
2747 LIST_HEAD(conn_list);
2749 pr_debug("msk=%p", msk);
2753 /* join list will be eventually flushed (with rst) at sock lock release time*/
2754 list_splice_init(&msk->conn_list, &conn_list);
2756 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
2757 sk_stop_timer(sk, &sk->sk_timer);
2760 /* clears msk->subflow, allowing the following loop to close
2761 * even the initial subflow
2763 mptcp_dispose_initial_subflow(msk);
2764 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
2765 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2766 __mptcp_close_ssk(sk, ssk, subflow, 0);
2769 sk->sk_prot->destroy(sk);
2771 WARN_ON_ONCE(msk->rmem_fwd_alloc);
2772 WARN_ON_ONCE(msk->rmem_released);
2773 sk_stream_kill_queues(sk);
2774 xfrm_sk_free_policy(sk);
2776 sk_refcnt_debug_release(sk);
2780 static void mptcp_close(struct sock *sk, long timeout)
2782 struct mptcp_subflow_context *subflow;
2783 bool do_cancel_work = false;
2786 sk->sk_shutdown = SHUTDOWN_MASK;
2788 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2789 inet_sk_state_store(sk, TCP_CLOSE);
2793 if (mptcp_close_state(sk))
2794 __mptcp_wr_shutdown(sk);
2796 sk_stream_wait_close(sk, timeout);
2799 /* orphan all the subflows */
2800 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
2801 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2802 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2803 bool slow = lock_sock_fast_nested(ssk);
2806 unlock_sock_fast(ssk, slow);
2811 pr_debug("msk=%p state=%d", sk, sk->sk_state);
2812 if (mptcp_sk(sk)->token)
2813 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
2815 if (sk->sk_state == TCP_CLOSE) {
2816 __mptcp_destroy_sock(sk);
2817 do_cancel_work = true;
2819 sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN);
2823 mptcp_cancel_work(sk);
2828 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
2830 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2831 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
2832 struct ipv6_pinfo *msk6 = inet6_sk(msk);
2834 msk->sk_v6_daddr = ssk->sk_v6_daddr;
2835 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
2838 msk6->saddr = ssk6->saddr;
2839 msk6->flow_label = ssk6->flow_label;
2843 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
2844 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
2845 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
2846 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
2847 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
2848 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
2851 static int mptcp_disconnect(struct sock *sk, int flags)
2853 struct mptcp_subflow_context *subflow;
2854 struct mptcp_sock *msk = mptcp_sk(sk);
2856 inet_sk_state_store(sk, TCP_CLOSE);
2858 mptcp_for_each_subflow(msk, subflow) {
2859 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2861 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_FASTCLOSE);
2864 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
2865 sk_stop_timer(sk, &sk->sk_timer);
2867 if (mptcp_sk(sk)->token)
2868 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
2870 mptcp_destroy_common(msk);
2871 msk->last_snd = NULL;
2872 WRITE_ONCE(msk->flags, 0);
2874 msk->push_pending = 0;
2875 msk->recovery = false;
2876 msk->can_ack = false;
2877 msk->fully_established = false;
2878 msk->rcv_data_fin = false;
2879 msk->snd_data_fin_enable = false;
2880 msk->rcv_fastclose = false;
2881 msk->use_64bit_ack = false;
2882 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2883 mptcp_pm_data_reset(msk);
2886 sk->sk_shutdown = 0;
2887 sk_error_report(sk);
2891 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2892 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
2894 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
2896 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
2900 struct sock *mptcp_sk_clone(const struct sock *sk,
2901 const struct mptcp_options_received *mp_opt,
2902 struct request_sock *req)
2904 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
2905 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
2906 struct mptcp_sock *msk;
2912 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2913 if (nsk->sk_family == AF_INET6)
2914 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
2917 __mptcp_init_sock(nsk);
2919 msk = mptcp_sk(nsk);
2920 msk->local_key = subflow_req->local_key;
2921 msk->token = subflow_req->token;
2922 msk->subflow = NULL;
2923 WRITE_ONCE(msk->fully_established, false);
2924 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
2925 WRITE_ONCE(msk->csum_enabled, true);
2927 msk->write_seq = subflow_req->idsn + 1;
2928 msk->snd_nxt = msk->write_seq;
2929 msk->snd_una = msk->write_seq;
2930 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
2931 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
2933 if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) {
2934 msk->can_ack = true;
2935 msk->remote_key = mp_opt->sndr_key;
2936 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
2938 WRITE_ONCE(msk->ack_seq, ack_seq);
2939 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
2942 sock_reset_flag(nsk, SOCK_RCU_FREE);
2943 /* will be fully established after successful MPC subflow creation */
2944 inet_sk_state_store(nsk, TCP_SYN_RECV);
2946 security_inet_csk_clone(nsk, req);
2947 bh_unlock_sock(nsk);
2949 /* keep a single reference */
2954 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
2956 const struct tcp_sock *tp = tcp_sk(ssk);
2958 msk->rcvq_space.copied = 0;
2959 msk->rcvq_space.rtt_us = 0;
2961 msk->rcvq_space.time = tp->tcp_mstamp;
2963 /* initial rcv_space offering made to peer */
2964 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
2965 TCP_INIT_CWND * tp->advmss);
2966 if (msk->rcvq_space.space == 0)
2967 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
2969 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
2972 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
2975 struct mptcp_sock *msk = mptcp_sk(sk);
2976 struct socket *listener;
2979 listener = __mptcp_nmpc_socket(msk);
2980 if (WARN_ON_ONCE(!listener)) {
2985 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
2986 newsk = inet_csk_accept(listener->sk, flags, err, kern);
2990 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
2991 if (sk_is_mptcp(newsk)) {
2992 struct mptcp_subflow_context *subflow;
2993 struct sock *new_mptcp_sock;
2995 subflow = mptcp_subflow_ctx(newsk);
2996 new_mptcp_sock = subflow->conn;
2998 /* is_mptcp should be false if subflow->conn is missing, see
2999 * subflow_syn_recv_sock()
3001 if (WARN_ON_ONCE(!new_mptcp_sock)) {
3002 tcp_sk(newsk)->is_mptcp = 0;
3006 /* acquire the 2nd reference for the owning socket */
3007 sock_hold(new_mptcp_sock);
3008 newsk = new_mptcp_sock;
3009 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3011 MPTCP_INC_STATS(sock_net(sk),
3012 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3016 newsk->sk_kern_sock = kern;
3020 void mptcp_destroy_common(struct mptcp_sock *msk)
3022 struct sock *sk = (struct sock *)msk;
3024 __mptcp_clear_xmit(sk);
3026 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3027 mptcp_data_lock(sk);
3028 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
3029 __skb_queue_purge(&sk->sk_receive_queue);
3030 skb_rbtree_purge(&msk->out_of_order_queue);
3031 mptcp_data_unlock(sk);
3033 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3034 * inet_sock_destruct() will dispose it
3036 sk->sk_forward_alloc += msk->rmem_fwd_alloc;
3037 msk->rmem_fwd_alloc = 0;
3038 mptcp_token_destroy(msk);
3039 mptcp_pm_free_anno_list(msk);
3042 static void mptcp_destroy(struct sock *sk)
3044 struct mptcp_sock *msk = mptcp_sk(sk);
3046 mptcp_destroy_common(msk);
3047 sk_sockets_allocated_dec(sk);
3050 void __mptcp_data_acked(struct sock *sk)
3052 if (!sock_owned_by_user(sk))
3053 __mptcp_clean_una(sk);
3055 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3057 if (mptcp_pending_data_fin_ack(sk))
3058 mptcp_schedule_work(sk);
3061 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3063 if (!mptcp_send_head(sk))
3066 if (!sock_owned_by_user(sk)) {
3067 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
3069 if (xmit_ssk == ssk)
3070 __mptcp_subflow_push_pending(sk, ssk);
3072 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND);
3074 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3078 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3079 BIT(MPTCP_RETRANSMIT) | \
3080 BIT(MPTCP_FLUSH_JOIN_LIST))
3082 /* processes deferred events and flush wmem */
3083 static void mptcp_release_cb(struct sock *sk)
3084 __must_hold(&sk->sk_lock.slock)
3086 struct mptcp_sock *msk = mptcp_sk(sk);
3089 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) |
3094 /* the following actions acquire the subflow socket lock
3096 * 1) can't be invoked in atomic scope
3097 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3098 * datapath acquires the msk socket spinlock while helding
3099 * the subflow socket lock
3101 msk->push_pending = 0;
3102 msk->cb_flags &= ~flags;
3103 spin_unlock_bh(&sk->sk_lock.slock);
3104 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3105 __mptcp_flush_join_list(sk);
3106 if (flags & BIT(MPTCP_PUSH_PENDING))
3107 __mptcp_push_pending(sk, 0);
3108 if (flags & BIT(MPTCP_RETRANSMIT))
3109 __mptcp_retrans(sk);
3112 spin_lock_bh(&sk->sk_lock.slock);
3115 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3116 __mptcp_clean_una_wakeup(sk);
3117 if (unlikely(&msk->cb_flags)) {
3118 /* be sure to set the current sk state before tacking actions
3119 * depending on sk_state, that is processing MPTCP_ERROR_REPORT
3121 if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags))
3122 __mptcp_set_connected(sk);
3123 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3124 __mptcp_error_report(sk);
3125 if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags))
3126 msk->last_snd = NULL;
3129 __mptcp_update_rmem(sk);
3132 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3133 * TCP can't schedule delack timer before the subflow is fully established.
3134 * MPTCP uses the delack timer to do 3rd ack retransmissions
3136 static void schedule_3rdack_retransmission(struct sock *ssk)
3138 struct inet_connection_sock *icsk = inet_csk(ssk);
3139 struct tcp_sock *tp = tcp_sk(ssk);
3140 unsigned long timeout;
3142 if (mptcp_subflow_ctx(ssk)->fully_established)
3145 /* reschedule with a timeout above RTT, as we must look only for drop */
3147 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3149 timeout = TCP_TIMEOUT_INIT;
3152 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3153 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3154 icsk->icsk_ack.timeout = timeout;
3155 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3158 void mptcp_subflow_process_delegated(struct sock *ssk)
3160 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3161 struct sock *sk = subflow->conn;
3163 if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) {
3164 mptcp_data_lock(sk);
3165 if (!sock_owned_by_user(sk))
3166 __mptcp_subflow_push_pending(sk, ssk);
3168 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3169 mptcp_data_unlock(sk);
3170 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND);
3172 if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) {
3173 schedule_3rdack_retransmission(ssk);
3174 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK);
3178 static int mptcp_hash(struct sock *sk)
3180 /* should never be called,
3181 * we hash the TCP subflows not the master socket
3187 static void mptcp_unhash(struct sock *sk)
3189 /* called from sk_common_release(), but nothing to do here */
3192 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3194 struct mptcp_sock *msk = mptcp_sk(sk);
3195 struct socket *ssock;
3197 ssock = __mptcp_nmpc_socket(msk);
3198 pr_debug("msk=%p, subflow=%p", msk, ssock);
3199 if (WARN_ON_ONCE(!ssock))
3202 return inet_csk_get_port(ssock->sk, snum);
3205 void mptcp_finish_connect(struct sock *ssk)
3207 struct mptcp_subflow_context *subflow;
3208 struct mptcp_sock *msk;
3212 subflow = mptcp_subflow_ctx(ssk);
3216 pr_debug("msk=%p, token=%u", sk, subflow->token);
3218 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
3220 subflow->map_seq = ack_seq;
3221 subflow->map_subflow_seq = 1;
3223 /* the socket is not connected yet, no msk/subflow ops can access/race
3224 * accessing the field below
3226 WRITE_ONCE(msk->remote_key, subflow->remote_key);
3227 WRITE_ONCE(msk->local_key, subflow->local_key);
3228 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3229 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3230 WRITE_ONCE(msk->ack_seq, ack_seq);
3231 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
3232 WRITE_ONCE(msk->can_ack, 1);
3233 WRITE_ONCE(msk->snd_una, msk->write_seq);
3235 mptcp_pm_new_connection(msk, ssk, 0);
3237 mptcp_rcv_space_init(msk, ssk);
3240 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3242 write_lock_bh(&sk->sk_callback_lock);
3243 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3244 sk_set_socket(sk, parent);
3245 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3246 write_unlock_bh(&sk->sk_callback_lock);
3249 bool mptcp_finish_join(struct sock *ssk)
3251 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3252 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3253 struct sock *parent = (void *)msk;
3256 pr_debug("msk=%p, subflow=%p", msk, subflow);
3258 /* mptcp socket already closing? */
3259 if (!mptcp_is_fully_established(parent)) {
3260 subflow->reset_reason = MPTCP_RST_EMPTCP;
3264 if (!msk->pm.server_side)
3267 if (!mptcp_pm_allow_new_subflow(msk))
3268 goto err_prohibited;
3270 if (WARN_ON_ONCE(!list_empty(&subflow->node)))
3271 goto err_prohibited;
3273 /* active connections are already on conn_list.
3274 * If we can't acquire msk socket lock here, let the release callback
3277 mptcp_data_lock(parent);
3278 if (!sock_owned_by_user(parent)) {
3279 ret = __mptcp_finish_join(msk, ssk);
3282 list_add_tail(&subflow->node, &msk->conn_list);
3286 list_add_tail(&subflow->node, &msk->join_list);
3287 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3289 mptcp_data_unlock(parent);
3293 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3297 subflow->map_seq = READ_ONCE(msk->ack_seq);
3298 WRITE_ONCE(msk->allow_infinite_fallback, false);
3301 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
3305 static void mptcp_shutdown(struct sock *sk, int how)
3307 pr_debug("sk=%p, how=%d", sk, how);
3309 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3310 __mptcp_wr_shutdown(sk);
3313 static int mptcp_forward_alloc_get(const struct sock *sk)
3315 return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3318 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3320 const struct sock *sk = (void *)msk;
3323 if (sk->sk_state == TCP_LISTEN)
3326 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3329 delta = msk->write_seq - v;
3330 if (__mptcp_check_fallback(msk) && msk->first) {
3331 struct tcp_sock *tp = tcp_sk(msk->first);
3333 /* the first subflow is disconnected after close - see
3334 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3335 * so ignore that status, too.
3337 if (!((1 << msk->first->sk_state) &
3338 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3339 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3341 if (delta > INT_MAX)
3347 static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3349 struct mptcp_sock *msk = mptcp_sk(sk);
3355 if (sk->sk_state == TCP_LISTEN)
3359 __mptcp_move_skbs(msk);
3360 answ = mptcp_inq_hint(sk);
3364 slow = lock_sock_fast(sk);
3365 answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3366 unlock_sock_fast(sk, slow);
3369 slow = lock_sock_fast(sk);
3370 answ = mptcp_ioctl_outq(msk, msk->snd_nxt);
3371 unlock_sock_fast(sk, slow);
3374 return -ENOIOCTLCMD;
3377 return put_user(answ, (int __user *)arg);
3380 static struct proto mptcp_prot = {
3382 .owner = THIS_MODULE,
3383 .init = mptcp_init_sock,
3384 .disconnect = mptcp_disconnect,
3385 .close = mptcp_close,
3386 .accept = mptcp_accept,
3387 .setsockopt = mptcp_setsockopt,
3388 .getsockopt = mptcp_getsockopt,
3389 .shutdown = mptcp_shutdown,
3390 .destroy = mptcp_destroy,
3391 .sendmsg = mptcp_sendmsg,
3392 .ioctl = mptcp_ioctl,
3393 .recvmsg = mptcp_recvmsg,
3394 .release_cb = mptcp_release_cb,
3396 .unhash = mptcp_unhash,
3397 .get_port = mptcp_get_port,
3398 .forward_alloc_get = mptcp_forward_alloc_get,
3399 .sockets_allocated = &mptcp_sockets_allocated,
3400 .memory_allocated = &tcp_memory_allocated,
3401 .memory_pressure = &tcp_memory_pressure,
3402 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3403 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3404 .sysctl_mem = sysctl_tcp_mem,
3405 .obj_size = sizeof(struct mptcp_sock),
3406 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3407 .no_autobind = true,
3410 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3412 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3413 struct socket *ssock;
3416 lock_sock(sock->sk);
3417 ssock = __mptcp_nmpc_socket(msk);
3423 err = ssock->ops->bind(ssock, uaddr, addr_len);
3425 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3428 release_sock(sock->sk);
3432 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3433 struct mptcp_subflow_context *subflow)
3435 subflow->request_mptcp = 0;
3436 __mptcp_do_fallback(msk);
3439 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
3440 int addr_len, int flags)
3442 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3443 struct mptcp_subflow_context *subflow;
3444 struct socket *ssock;
3447 lock_sock(sock->sk);
3449 if (addr_len < sizeof(uaddr->sa_family))
3452 if (uaddr->sa_family == AF_UNSPEC) {
3453 err = mptcp_disconnect(sock->sk, flags);
3454 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
3459 if (sock->state != SS_UNCONNECTED && msk->subflow) {
3460 /* pending connection or invalid state, let existing subflow
3463 ssock = msk->subflow;
3467 ssock = __mptcp_nmpc_socket(msk);
3471 mptcp_token_destroy(msk);
3472 inet_sk_state_store(sock->sk, TCP_SYN_SENT);
3473 subflow = mptcp_subflow_ctx(ssock->sk);
3474 #ifdef CONFIG_TCP_MD5SIG
3475 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3478 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
3479 mptcp_subflow_early_fallback(msk, subflow);
3481 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3482 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
3483 mptcp_subflow_early_fallback(msk, subflow);
3485 if (likely(!__mptcp_check_fallback(msk)))
3486 MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE);
3489 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
3490 sock->state = ssock->state;
3492 /* on successful connect, the msk state will be moved to established by
3493 * subflow_finish_connect()
3495 if (!err || err == -EINPROGRESS)
3496 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3498 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3501 release_sock(sock->sk);
3505 static int mptcp_listen(struct socket *sock, int backlog)
3507 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3508 struct socket *ssock;
3511 pr_debug("msk=%p", msk);
3513 lock_sock(sock->sk);
3514 ssock = __mptcp_nmpc_socket(msk);
3520 mptcp_token_destroy(msk);
3521 inet_sk_state_store(sock->sk, TCP_LISTEN);
3522 sock_set_flag(sock->sk, SOCK_RCU_FREE);
3524 err = ssock->ops->listen(ssock, backlog);
3525 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3527 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3530 release_sock(sock->sk);
3534 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3535 int flags, bool kern)
3537 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3538 struct socket *ssock;
3541 pr_debug("msk=%p", msk);
3543 ssock = __mptcp_nmpc_socket(msk);
3547 err = ssock->ops->accept(sock, newsock, flags, kern);
3548 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
3549 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3550 struct mptcp_subflow_context *subflow;
3551 struct sock *newsk = newsock->sk;
3555 /* PM/worker can now acquire the first subflow socket
3556 * lock without racing with listener queue cleanup,
3557 * we can notify it, if needed.
3559 * Even if remote has reset the initial subflow by now
3560 * the refcnt is still at least one.
3562 subflow = mptcp_subflow_ctx(msk->first);
3563 list_add(&subflow->node, &msk->conn_list);
3564 sock_hold(msk->first);
3565 if (mptcp_is_fully_established(newsk))
3566 mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL);
3568 mptcp_copy_inaddrs(newsk, msk->first);
3569 mptcp_rcv_space_init(msk, msk->first);
3570 mptcp_propagate_sndbuf(newsk, msk->first);
3572 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3573 * This is needed so NOSPACE flag can be set from tcp stack.
3575 mptcp_for_each_subflow(msk, subflow) {
3576 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3578 if (!ssk->sk_socket)
3579 mptcp_sock_graft(ssk, newsock);
3581 release_sock(newsk);
3587 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
3589 /* Concurrent splices from sk_receive_queue into receive_queue will
3590 * always show at least one non-empty queue when checked in this order.
3592 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
3593 skb_queue_empty_lockless(&msk->receive_queue))
3596 return EPOLLIN | EPOLLRDNORM;
3599 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3601 struct sock *sk = (struct sock *)msk;
3603 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
3604 return EPOLLOUT | EPOLLWRNORM;
3606 if (sk_stream_is_writeable(sk))
3607 return EPOLLOUT | EPOLLWRNORM;
3609 mptcp_set_nospace(sk);
3610 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3611 if (sk_stream_is_writeable(sk))
3612 return EPOLLOUT | EPOLLWRNORM;
3617 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3618 struct poll_table_struct *wait)
3620 struct sock *sk = sock->sk;
3621 struct mptcp_sock *msk;
3626 sock_poll_wait(file, sock, wait);
3628 state = inet_sk_state_load(sk);
3629 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
3630 if (state == TCP_LISTEN) {
3631 if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk))
3634 return inet_csk_listen_poll(msk->subflow->sk);
3637 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3638 mask |= mptcp_check_readable(msk);
3639 mask |= mptcp_check_writeable(msk);
3641 if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3643 if (sk->sk_shutdown & RCV_SHUTDOWN)
3644 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3646 /* This barrier is coupled with smp_wmb() in tcp_reset() */
3654 static const struct proto_ops mptcp_stream_ops = {
3656 .owner = THIS_MODULE,
3657 .release = inet_release,
3659 .connect = mptcp_stream_connect,
3660 .socketpair = sock_no_socketpair,
3661 .accept = mptcp_stream_accept,
3662 .getname = inet_getname,
3664 .ioctl = inet_ioctl,
3665 .gettstamp = sock_gettstamp,
3666 .listen = mptcp_listen,
3667 .shutdown = inet_shutdown,
3668 .setsockopt = sock_common_setsockopt,
3669 .getsockopt = sock_common_getsockopt,
3670 .sendmsg = inet_sendmsg,
3671 .recvmsg = inet_recvmsg,
3672 .mmap = sock_no_mmap,
3673 .sendpage = inet_sendpage,
3676 static struct inet_protosw mptcp_protosw = {
3677 .type = SOCK_STREAM,
3678 .protocol = IPPROTO_MPTCP,
3679 .prot = &mptcp_prot,
3680 .ops = &mptcp_stream_ops,
3681 .flags = INET_PROTOSW_ICSK,
3684 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3686 struct mptcp_delegated_action *delegated;
3687 struct mptcp_subflow_context *subflow;
3690 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3691 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3692 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3694 bh_lock_sock_nested(ssk);
3695 if (!sock_owned_by_user(ssk) &&
3696 mptcp_subflow_has_delegated_action(subflow))
3697 mptcp_subflow_process_delegated(ssk);
3698 /* ... elsewhere tcp_release_cb_override already processed
3699 * the action or will do at next release_sock().
3700 * In both case must dequeue the subflow here - on the same
3701 * CPU that scheduled it.
3703 bh_unlock_sock(ssk);
3706 if (++work_done == budget)
3710 /* always provide a 0 'work_done' argument, so that napi_complete_done
3711 * will not try accessing the NULL napi->dev ptr
3713 napi_complete_done(napi, 0);
3717 void __init mptcp_proto_init(void)
3719 struct mptcp_delegated_action *delegated;
3722 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
3724 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3725 panic("Failed to allocate MPTCP pcpu counter\n");
3727 init_dummy_netdev(&mptcp_napi_dev);
3728 for_each_possible_cpu(cpu) {
3729 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3730 INIT_LIST_HEAD(&delegated->head);
3731 netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll,
3733 napi_enable(&delegated->napi);
3736 mptcp_subflow_init();
3740 if (proto_register(&mptcp_prot, 1) != 0)
3741 panic("Failed to register MPTCP proto.\n");
3743 inet_register_protosw(&mptcp_protosw);
3745 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
3748 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3749 static const struct proto_ops mptcp_v6_stream_ops = {
3751 .owner = THIS_MODULE,
3752 .release = inet6_release,
3754 .connect = mptcp_stream_connect,
3755 .socketpair = sock_no_socketpair,
3756 .accept = mptcp_stream_accept,
3757 .getname = inet6_getname,
3759 .ioctl = inet6_ioctl,
3760 .gettstamp = sock_gettstamp,
3761 .listen = mptcp_listen,
3762 .shutdown = inet_shutdown,
3763 .setsockopt = sock_common_setsockopt,
3764 .getsockopt = sock_common_getsockopt,
3765 .sendmsg = inet6_sendmsg,
3766 .recvmsg = inet6_recvmsg,
3767 .mmap = sock_no_mmap,
3768 .sendpage = inet_sendpage,
3769 #ifdef CONFIG_COMPAT
3770 .compat_ioctl = inet6_compat_ioctl,
3774 static struct proto mptcp_v6_prot;
3776 static void mptcp_v6_destroy(struct sock *sk)
3779 inet6_destroy_sock(sk);
3782 static struct inet_protosw mptcp_v6_protosw = {
3783 .type = SOCK_STREAM,
3784 .protocol = IPPROTO_MPTCP,
3785 .prot = &mptcp_v6_prot,
3786 .ops = &mptcp_v6_stream_ops,
3787 .flags = INET_PROTOSW_ICSK,
3790 int __init mptcp_proto_v6_init(void)
3794 mptcp_v6_prot = mptcp_prot;
3795 strcpy(mptcp_v6_prot.name, "MPTCPv6");
3796 mptcp_v6_prot.slab = NULL;
3797 mptcp_v6_prot.destroy = mptcp_v6_destroy;
3798 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
3800 err = proto_register(&mptcp_v6_prot, 1);
3804 err = inet6_register_protosw(&mptcp_v6_protosw);
3806 proto_unregister(&mptcp_v6_prot);