1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2017 - 2019, Intel Corporation.
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/sched/signal.h>
13 #include <linux/atomic.h>
15 #include <net/inet_common.h>
16 #include <net/inet_hashtables.h>
17 #include <net/protocol.h>
19 #include <net/tcp_states.h>
20 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21 #include <net/transp_v6.h>
23 #include <net/mptcp.h>
25 #include <asm/ioctls.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/mptcp.h>
32 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
34 struct mptcp_sock msk;
40 MPTCP_CMSG_TS = BIT(0),
41 MPTCP_CMSG_INQ = BIT(1),
44 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
46 static void __mptcp_destroy_sock(struct sock *sk);
47 static void mptcp_check_send_data_fin(struct sock *sk);
49 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
50 static struct net_device mptcp_napi_dev;
52 /* Returns end sequence number of the receiver's advertised window */
53 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
55 return READ_ONCE(msk->wnd_end);
58 static bool mptcp_is_tcpsk(struct sock *sk)
60 struct socket *sock = sk->sk_socket;
62 if (unlikely(sk->sk_prot == &tcp_prot)) {
63 /* we are being invoked after mptcp_accept() has
64 * accepted a non-mp-capable flow: sk is a tcp_sk,
67 * Hand the socket over to tcp so all further socket ops
70 sock->ops = &inet_stream_ops;
72 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
73 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
74 sock->ops = &inet6_stream_ops;
82 static int __mptcp_socket_create(struct mptcp_sock *msk)
84 struct mptcp_subflow_context *subflow;
85 struct sock *sk = (struct sock *)msk;
89 err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
93 WRITE_ONCE(msk->first, ssock->sk);
94 WRITE_ONCE(msk->subflow, ssock);
95 subflow = mptcp_subflow_ctx(ssock->sk);
96 list_add(&subflow->node, &msk->conn_list);
98 subflow->request_mptcp = 1;
99 subflow->subflow_id = msk->subflow_id++;
101 /* This is the first subflow, always with id 0 */
102 subflow->local_id_valid = 1;
103 mptcp_sock_graft(msk->first, sk->sk_socket);
108 /* If the MPC handshake is not started, returns the first subflow,
109 * eventually allocating it.
111 struct socket *__mptcp_nmpc_socket(struct mptcp_sock *msk)
113 struct sock *sk = (struct sock *)msk;
116 if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
117 return ERR_PTR(-EINVAL);
121 return ERR_PTR(-EINVAL);
123 ret = __mptcp_socket_create(msk);
127 mptcp_sockopt_sync(msk, msk->first);
133 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
135 sk_drops_add(sk, skb);
139 static void mptcp_rmem_charge(struct sock *sk, int size)
141 mptcp_sk(sk)->rmem_fwd_alloc -= size;
144 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
145 struct sk_buff *from)
150 if (MPTCP_SKB_CB(from)->offset ||
151 !skb_try_coalesce(to, from, &fragstolen, &delta))
154 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
155 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
156 to->len, MPTCP_SKB_CB(from)->end_seq);
157 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
159 /* note the fwd memory can reach a negative value after accounting
160 * for the delta, but the later skb free will restore a non
163 atomic_add(delta, &sk->sk_rmem_alloc);
164 mptcp_rmem_charge(sk, delta);
165 kfree_skb_partial(from, fragstolen);
170 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
171 struct sk_buff *from)
173 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
176 return mptcp_try_coalesce((struct sock *)msk, to, from);
179 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
181 amount >>= PAGE_SHIFT;
182 mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT;
183 __sk_mem_reduce_allocated(sk, amount);
186 static void mptcp_rmem_uncharge(struct sock *sk, int size)
188 struct mptcp_sock *msk = mptcp_sk(sk);
191 msk->rmem_fwd_alloc += size;
192 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
194 /* see sk_mem_uncharge() for the rationale behind the following schema */
195 if (unlikely(reclaimable >= PAGE_SIZE))
196 __mptcp_rmem_reclaim(sk, reclaimable);
199 static void mptcp_rfree(struct sk_buff *skb)
201 unsigned int len = skb->truesize;
202 struct sock *sk = skb->sk;
204 atomic_sub(len, &sk->sk_rmem_alloc);
205 mptcp_rmem_uncharge(sk, len);
208 void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
212 skb->destructor = mptcp_rfree;
213 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
214 mptcp_rmem_charge(sk, skb->truesize);
217 /* "inspired" by tcp_data_queue_ofo(), main differences:
219 * - don't cope with sacks
221 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
223 struct sock *sk = (struct sock *)msk;
224 struct rb_node **p, *parent;
225 u64 seq, end_seq, max_seq;
226 struct sk_buff *skb1;
228 seq = MPTCP_SKB_CB(skb)->map_seq;
229 end_seq = MPTCP_SKB_CB(skb)->end_seq;
230 max_seq = atomic64_read(&msk->rcv_wnd_sent);
232 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
233 RB_EMPTY_ROOT(&msk->out_of_order_queue));
234 if (after64(end_seq, max_seq)) {
237 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
238 (unsigned long long)end_seq - (unsigned long)max_seq,
239 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
240 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
244 p = &msk->out_of_order_queue.rb_node;
245 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
246 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
247 rb_link_node(&skb->rbnode, NULL, p);
248 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
249 msk->ooo_last_skb = skb;
253 /* with 2 subflows, adding at end of ooo queue is quite likely
254 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
256 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
257 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
258 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
262 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
263 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
264 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
265 parent = &msk->ooo_last_skb->rbnode;
266 p = &parent->rb_right;
270 /* Find place to insert this segment. Handle overlaps on the way. */
274 skb1 = rb_to_skb(parent);
275 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
276 p = &parent->rb_left;
279 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
280 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
281 /* All the bits are present. Drop. */
283 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
286 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
290 * continue traversing
293 /* skb's seq == skb1's seq and skb covers skb1.
294 * Replace skb1 with skb.
296 rb_replace_node(&skb1->rbnode, &skb->rbnode,
297 &msk->out_of_order_queue);
298 mptcp_drop(sk, skb1);
299 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
302 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
303 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
306 p = &parent->rb_right;
310 /* Insert segment into RB tree. */
311 rb_link_node(&skb->rbnode, parent, p);
312 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
315 /* Remove other segments covered by skb. */
316 while ((skb1 = skb_rb_next(skb)) != NULL) {
317 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
319 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
320 mptcp_drop(sk, skb1);
321 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
323 /* If there is no skb after us, we are the last_skb ! */
325 msk->ooo_last_skb = skb;
329 mptcp_set_owner_r(skb, sk);
332 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
334 struct mptcp_sock *msk = mptcp_sk(sk);
337 if (size <= msk->rmem_fwd_alloc)
340 size -= msk->rmem_fwd_alloc;
341 amt = sk_mem_pages(size);
342 amount = amt << PAGE_SHIFT;
343 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
346 msk->rmem_fwd_alloc += amount;
350 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
351 struct sk_buff *skb, unsigned int offset,
354 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
355 struct sock *sk = (struct sock *)msk;
356 struct sk_buff *tail;
359 __skb_unlink(skb, &ssk->sk_receive_queue);
364 /* try to fetch required memory from subflow */
365 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
368 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
370 /* the skb map_seq accounts for the skb offset:
371 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
374 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
375 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
376 MPTCP_SKB_CB(skb)->offset = offset;
377 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
379 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
381 msk->bytes_received += copy_len;
382 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
383 tail = skb_peek_tail(&sk->sk_receive_queue);
384 if (tail && mptcp_try_coalesce(sk, tail, skb))
387 mptcp_set_owner_r(skb, sk);
388 __skb_queue_tail(&sk->sk_receive_queue, skb);
390 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
391 mptcp_data_queue_ofo(msk, skb);
395 /* old data, keep it simple and drop the whole pkt, sender
396 * will retransmit as needed, if needed.
398 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
404 static void mptcp_stop_timer(struct sock *sk)
406 struct inet_connection_sock *icsk = inet_csk(sk);
408 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
409 mptcp_sk(sk)->timer_ival = 0;
412 static void mptcp_close_wake_up(struct sock *sk)
414 if (sock_flag(sk, SOCK_DEAD))
417 sk->sk_state_change(sk);
418 if (sk->sk_shutdown == SHUTDOWN_MASK ||
419 sk->sk_state == TCP_CLOSE)
420 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
422 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
425 static bool mptcp_pending_data_fin_ack(struct sock *sk)
427 struct mptcp_sock *msk = mptcp_sk(sk);
429 return ((1 << sk->sk_state) &
430 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
431 msk->write_seq == READ_ONCE(msk->snd_una);
434 static void mptcp_check_data_fin_ack(struct sock *sk)
436 struct mptcp_sock *msk = mptcp_sk(sk);
438 /* Look for an acknowledged DATA_FIN */
439 if (mptcp_pending_data_fin_ack(sk)) {
440 WRITE_ONCE(msk->snd_data_fin_enable, 0);
442 switch (sk->sk_state) {
444 inet_sk_state_store(sk, TCP_FIN_WAIT2);
448 inet_sk_state_store(sk, TCP_CLOSE);
452 mptcp_close_wake_up(sk);
456 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
458 struct mptcp_sock *msk = mptcp_sk(sk);
460 if (READ_ONCE(msk->rcv_data_fin) &&
461 ((1 << sk->sk_state) &
462 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
463 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
465 if (msk->ack_seq == rcv_data_fin_seq) {
467 *seq = rcv_data_fin_seq;
476 static void mptcp_set_datafin_timeout(struct sock *sk)
478 struct inet_connection_sock *icsk = inet_csk(sk);
481 retransmits = min_t(u32, icsk->icsk_retransmits,
482 ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
484 mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
487 static void __mptcp_set_timeout(struct sock *sk, long tout)
489 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
492 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
494 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
496 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
497 inet_csk(ssk)->icsk_timeout - jiffies : 0;
500 static void mptcp_set_timeout(struct sock *sk)
502 struct mptcp_subflow_context *subflow;
505 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
506 tout = max(tout, mptcp_timeout_from_subflow(subflow));
507 __mptcp_set_timeout(sk, tout);
510 static inline bool tcp_can_send_ack(const struct sock *ssk)
512 return !((1 << inet_sk_state_load(ssk)) &
513 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
516 void __mptcp_subflow_send_ack(struct sock *ssk)
518 if (tcp_can_send_ack(ssk))
522 static void mptcp_subflow_send_ack(struct sock *ssk)
526 slow = lock_sock_fast(ssk);
527 __mptcp_subflow_send_ack(ssk);
528 unlock_sock_fast(ssk, slow);
531 static void mptcp_send_ack(struct mptcp_sock *msk)
533 struct mptcp_subflow_context *subflow;
535 mptcp_for_each_subflow(msk, subflow)
536 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
539 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
543 slow = lock_sock_fast(ssk);
544 if (tcp_can_send_ack(ssk))
545 tcp_cleanup_rbuf(ssk, 1);
546 unlock_sock_fast(ssk, slow);
549 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
551 const struct inet_connection_sock *icsk = inet_csk(ssk);
552 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
553 const struct tcp_sock *tp = tcp_sk(ssk);
555 return (ack_pending & ICSK_ACK_SCHED) &&
556 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
557 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
558 (rx_empty && ack_pending &
559 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
562 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
564 int old_space = READ_ONCE(msk->old_wspace);
565 struct mptcp_subflow_context *subflow;
566 struct sock *sk = (struct sock *)msk;
567 int space = __mptcp_space(sk);
568 bool cleanup, rx_empty;
570 cleanup = (space > 0) && (space >= (old_space << 1));
571 rx_empty = !__mptcp_rmem(sk);
573 mptcp_for_each_subflow(msk, subflow) {
574 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
576 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
577 mptcp_subflow_cleanup_rbuf(ssk);
581 static bool mptcp_check_data_fin(struct sock *sk)
583 struct mptcp_sock *msk = mptcp_sk(sk);
584 u64 rcv_data_fin_seq;
587 /* Need to ack a DATA_FIN received from a peer while this side
588 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
589 * msk->rcv_data_fin was set when parsing the incoming options
590 * at the subflow level and the msk lock was not held, so this
591 * is the first opportunity to act on the DATA_FIN and change
594 * If we are caught up to the sequence number of the incoming
595 * DATA_FIN, send the DATA_ACK now and do state transition. If
596 * not caught up, do nothing and let the recv code send DATA_ACK
600 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
601 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
602 WRITE_ONCE(msk->rcv_data_fin, 0);
604 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
605 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
607 switch (sk->sk_state) {
608 case TCP_ESTABLISHED:
609 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
612 inet_sk_state_store(sk, TCP_CLOSING);
615 inet_sk_state_store(sk, TCP_CLOSE);
618 /* Other states not expected */
624 if (!__mptcp_check_fallback(msk))
626 mptcp_close_wake_up(sk);
631 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
635 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
636 struct sock *sk = (struct sock *)msk;
637 unsigned int moved = 0;
638 bool more_data_avail;
643 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
645 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
646 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
648 if (unlikely(ssk_rbuf > sk_rbuf)) {
649 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
654 pr_debug("msk=%p ssk=%p", msk, ssk);
657 u32 map_remaining, offset;
658 u32 seq = tp->copied_seq;
662 /* try to move as much data as available */
663 map_remaining = subflow->map_data_len -
664 mptcp_subflow_get_map_offset(subflow);
666 skb = skb_peek(&ssk->sk_receive_queue);
668 /* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
669 * a different CPU can have already processed the pending
670 * data, stop here or we can enter an infinite loop
677 if (__mptcp_check_fallback(msk)) {
678 /* Under fallback skbs have no MPTCP extension and TCP could
679 * collapse them between the dummy map creation and the
680 * current dequeue. Be sure to adjust the map size.
682 map_remaining = skb->len;
683 subflow->map_data_len = skb->len;
686 offset = seq - TCP_SKB_CB(skb)->seq;
687 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
693 if (offset < skb->len) {
694 size_t len = skb->len - offset;
699 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
703 if (WARN_ON_ONCE(map_remaining < len))
707 sk_eat_skb(ssk, skb);
711 WRITE_ONCE(tp->copied_seq, seq);
712 more_data_avail = mptcp_subflow_data_available(ssk);
714 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
718 } while (more_data_avail);
724 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
726 struct sock *sk = (struct sock *)msk;
727 struct sk_buff *skb, *tail;
732 p = rb_first(&msk->out_of_order_queue);
733 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
736 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
740 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
742 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
745 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
749 end_seq = MPTCP_SKB_CB(skb)->end_seq;
750 tail = skb_peek_tail(&sk->sk_receive_queue);
751 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
752 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
754 /* skip overlapping data, if any */
755 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
756 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
758 MPTCP_SKB_CB(skb)->offset += delta;
759 MPTCP_SKB_CB(skb)->map_seq += delta;
760 __skb_queue_tail(&sk->sk_receive_queue, skb);
762 msk->bytes_received += end_seq - msk->ack_seq;
763 msk->ack_seq = end_seq;
769 /* In most cases we will be able to lock the mptcp socket. If its already
770 * owned, we need to defer to the work queue to avoid ABBA deadlock.
772 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
774 struct sock *sk = (struct sock *)msk;
775 unsigned int moved = 0;
777 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
778 __mptcp_ofo_queue(msk);
779 if (unlikely(ssk->sk_err)) {
780 if (!sock_owned_by_user(sk))
781 __mptcp_error_report(sk);
783 __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags);
786 /* If the moves have caught up with the DATA_FIN sequence number
787 * it's time to ack the DATA_FIN and change socket state, but
788 * this is not a good place to change state. Let the workqueue
791 if (mptcp_pending_data_fin(sk, NULL))
792 mptcp_schedule_work(sk);
796 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
798 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
799 struct mptcp_sock *msk = mptcp_sk(sk);
800 int sk_rbuf, ssk_rbuf;
802 /* The peer can send data while we are shutting down this
803 * subflow at msk destruction time, but we must avoid enqueuing
804 * more data to the msk receive queue
806 if (unlikely(subflow->disposable))
809 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
810 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
811 if (unlikely(ssk_rbuf > sk_rbuf))
814 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
815 if (__mptcp_rmem(sk) > sk_rbuf) {
816 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
820 /* Wake-up the reader only for in-sequence data */
822 if (move_skbs_to_msk(msk, ssk))
823 sk->sk_data_ready(sk);
825 mptcp_data_unlock(sk);
828 static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
830 mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
831 WRITE_ONCE(msk->allow_infinite_fallback, false);
832 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
835 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
837 struct sock *sk = (struct sock *)msk;
839 if (sk->sk_state != TCP_ESTABLISHED)
842 /* attach to msk socket only after we are sure we will deal with it
845 if (sk->sk_socket && !ssk->sk_socket)
846 mptcp_sock_graft(ssk, sk->sk_socket);
848 mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
849 mptcp_sockopt_sync_locked(msk, ssk);
850 mptcp_subflow_joined(msk, ssk);
854 static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
856 struct mptcp_subflow_context *tmp, *subflow;
857 struct mptcp_sock *msk = mptcp_sk(sk);
859 list_for_each_entry_safe(subflow, tmp, join_list, node) {
860 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
861 bool slow = lock_sock_fast(ssk);
863 list_move_tail(&subflow->node, &msk->conn_list);
864 if (!__mptcp_finish_join(msk, ssk))
865 mptcp_subflow_reset(ssk);
866 unlock_sock_fast(ssk, slow);
870 static bool mptcp_timer_pending(struct sock *sk)
872 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
875 static void mptcp_reset_timer(struct sock *sk)
877 struct inet_connection_sock *icsk = inet_csk(sk);
880 /* prevent rescheduling on close */
881 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
884 tout = mptcp_sk(sk)->timer_ival;
885 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
888 bool mptcp_schedule_work(struct sock *sk)
890 if (inet_sk_state_load(sk) != TCP_CLOSE &&
891 schedule_work(&mptcp_sk(sk)->work)) {
892 /* each subflow already holds a reference to the sk, and the
893 * workqueue is invoked by a subflow, so sk can't go away here.
901 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
903 struct mptcp_subflow_context *subflow;
905 msk_owned_by_me(msk);
907 mptcp_for_each_subflow(msk, subflow) {
908 if (READ_ONCE(subflow->data_avail))
909 return mptcp_subflow_tcp_sock(subflow);
915 static bool mptcp_skb_can_collapse_to(u64 write_seq,
916 const struct sk_buff *skb,
917 const struct mptcp_ext *mpext)
919 if (!tcp_skb_can_collapse_to(skb))
922 /* can collapse only if MPTCP level sequence is in order and this
923 * mapping has not been xmitted yet
925 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
929 /* we can append data to the given data frag if:
930 * - there is space available in the backing page_frag
931 * - the data frag tail matches the current page_frag free offset
932 * - the data frag end sequence number matches the current write seq
934 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
935 const struct page_frag *pfrag,
936 const struct mptcp_data_frag *df)
938 return df && pfrag->page == df->page &&
939 pfrag->size - pfrag->offset > 0 &&
940 pfrag->offset == (df->offset + df->data_len) &&
941 df->data_seq + df->data_len == msk->write_seq;
944 static void dfrag_uncharge(struct sock *sk, int len)
946 sk_mem_uncharge(sk, len);
947 sk_wmem_queued_add(sk, -len);
950 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
952 int len = dfrag->data_len + dfrag->overhead;
954 list_del(&dfrag->list);
955 dfrag_uncharge(sk, len);
956 put_page(dfrag->page);
959 static void __mptcp_clean_una(struct sock *sk)
961 struct mptcp_sock *msk = mptcp_sk(sk);
962 struct mptcp_data_frag *dtmp, *dfrag;
965 snd_una = msk->snd_una;
966 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
967 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
970 if (unlikely(dfrag == msk->first_pending)) {
971 /* in recovery mode can see ack after the current snd head */
972 if (WARN_ON_ONCE(!msk->recovery))
975 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
978 dfrag_clear(sk, dfrag);
981 dfrag = mptcp_rtx_head(sk);
982 if (dfrag && after64(snd_una, dfrag->data_seq)) {
983 u64 delta = snd_una - dfrag->data_seq;
985 /* prevent wrap around in recovery mode */
986 if (unlikely(delta > dfrag->already_sent)) {
987 if (WARN_ON_ONCE(!msk->recovery))
989 if (WARN_ON_ONCE(delta > dfrag->data_len))
991 dfrag->already_sent += delta - dfrag->already_sent;
994 dfrag->data_seq += delta;
995 dfrag->offset += delta;
996 dfrag->data_len -= delta;
997 dfrag->already_sent -= delta;
999 dfrag_uncharge(sk, delta);
1002 /* all retransmitted data acked, recovery completed */
1003 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1004 msk->recovery = false;
1007 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1008 snd_una == READ_ONCE(msk->write_seq)) {
1009 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1010 mptcp_stop_timer(sk);
1012 mptcp_reset_timer(sk);
1016 static void __mptcp_clean_una_wakeup(struct sock *sk)
1018 lockdep_assert_held_once(&sk->sk_lock.slock);
1020 __mptcp_clean_una(sk);
1021 mptcp_write_space(sk);
1024 static void mptcp_clean_una_wakeup(struct sock *sk)
1026 mptcp_data_lock(sk);
1027 __mptcp_clean_una_wakeup(sk);
1028 mptcp_data_unlock(sk);
1031 static void mptcp_enter_memory_pressure(struct sock *sk)
1033 struct mptcp_subflow_context *subflow;
1034 struct mptcp_sock *msk = mptcp_sk(sk);
1037 sk_stream_moderate_sndbuf(sk);
1038 mptcp_for_each_subflow(msk, subflow) {
1039 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1042 tcp_enter_memory_pressure(ssk);
1043 sk_stream_moderate_sndbuf(ssk);
1048 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1051 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1053 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1054 pfrag, sk->sk_allocation)))
1057 mptcp_enter_memory_pressure(sk);
1061 static struct mptcp_data_frag *
1062 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1065 int offset = ALIGN(orig_offset, sizeof(long));
1066 struct mptcp_data_frag *dfrag;
1068 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1069 dfrag->data_len = 0;
1070 dfrag->data_seq = msk->write_seq;
1071 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1072 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1073 dfrag->already_sent = 0;
1074 dfrag->page = pfrag->page;
1079 struct mptcp_sendmsg_info {
1085 bool data_lock_held;
1088 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1089 u64 data_seq, int avail_size)
1091 u64 window_end = mptcp_wnd_end(msk);
1094 if (__mptcp_check_fallback(msk))
1097 mptcp_snd_wnd = window_end - data_seq;
1098 avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1100 if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1101 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1102 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1108 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1110 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1114 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1118 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1120 struct sk_buff *skb;
1122 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1124 if (likely(__mptcp_add_ext(skb, gfp))) {
1125 skb_reserve(skb, MAX_TCP_HEADER);
1126 skb->ip_summed = CHECKSUM_PARTIAL;
1127 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1132 mptcp_enter_memory_pressure(sk);
1137 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1139 struct sk_buff *skb;
1141 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1145 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1146 tcp_skb_entail(ssk, skb);
1149 tcp_skb_tsorted_anchor_cleanup(skb);
1154 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1156 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1158 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1161 /* note: this always recompute the csum on the whole skb, even
1162 * if we just appended a single frag. More status info needed
1164 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1166 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1167 __wsum csum = ~csum_unfold(mpext->csum);
1168 int offset = skb->len - added;
1170 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1173 static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1175 struct mptcp_ext *mpext)
1180 mpext->infinite_map = 1;
1181 mpext->data_len = 0;
1183 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1184 mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1186 mptcp_do_fallback(ssk);
1189 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1190 struct mptcp_data_frag *dfrag,
1191 struct mptcp_sendmsg_info *info)
1193 u64 data_seq = dfrag->data_seq + info->sent;
1194 int offset = dfrag->offset + info->sent;
1195 struct mptcp_sock *msk = mptcp_sk(sk);
1196 bool zero_window_probe = false;
1197 struct mptcp_ext *mpext = NULL;
1198 bool can_coalesce = false;
1199 bool reuse_skb = true;
1200 struct sk_buff *skb;
1204 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1205 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1207 if (WARN_ON_ONCE(info->sent > info->limit ||
1208 info->limit > dfrag->data_len))
1211 if (unlikely(!__tcp_can_send(ssk)))
1214 /* compute send limit */
1215 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1216 copy = info->size_goal;
1218 skb = tcp_write_queue_tail(ssk);
1219 if (skb && copy > skb->len) {
1220 /* Limit the write to the size available in the
1221 * current skb, if any, so that we create at most a new skb.
1222 * Explicitly tells TCP internals to avoid collapsing on later
1223 * queue management operation, to avoid breaking the ext <->
1224 * SSN association set here
1226 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1227 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1228 TCP_SKB_CB(skb)->eor = 1;
1232 i = skb_shinfo(skb)->nr_frags;
1233 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1234 if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
1235 tcp_mark_push(tcp_sk(ssk), skb);
1242 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1246 i = skb_shinfo(skb)->nr_frags;
1248 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1251 /* Zero window and all data acked? Probe. */
1252 copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1254 u64 snd_una = READ_ONCE(msk->snd_una);
1256 if (snd_una != msk->snd_nxt) {
1257 tcp_remove_empty_skb(ssk);
1261 zero_window_probe = true;
1262 data_seq = snd_una - 1;
1265 /* all mptcp-level data is acked, no skbs should be present into the
1268 WARN_ON_ONCE(reuse_skb);
1271 copy = min_t(size_t, copy, info->limit - info->sent);
1272 if (!sk_wmem_schedule(ssk, copy)) {
1273 tcp_remove_empty_skb(ssk);
1278 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1280 get_page(dfrag->page);
1281 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1285 skb->data_len += copy;
1286 skb->truesize += copy;
1287 sk_wmem_queued_add(ssk, copy);
1288 sk_mem_charge(ssk, copy);
1289 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1290 TCP_SKB_CB(skb)->end_seq += copy;
1291 tcp_skb_pcount_set(skb, 0);
1293 /* on skb reuse we just need to update the DSS len */
1295 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1296 mpext->data_len += copy;
1297 WARN_ON_ONCE(zero_window_probe);
1301 memset(mpext, 0, sizeof(*mpext));
1302 mpext->data_seq = data_seq;
1303 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1304 mpext->data_len = copy;
1308 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1309 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1312 if (zero_window_probe) {
1313 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1315 if (READ_ONCE(msk->csum_enabled))
1316 mptcp_update_data_checksum(skb, copy);
1317 tcp_push_pending_frames(ssk);
1321 if (READ_ONCE(msk->csum_enabled))
1322 mptcp_update_data_checksum(skb, copy);
1323 if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1324 mptcp_update_infinite_map(msk, ssk, mpext);
1325 trace_mptcp_sendmsg_frag(mpext);
1326 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1330 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1331 sizeof(struct tcphdr) - \
1332 MAX_TCP_OPTION_SPACE - \
1333 sizeof(struct ipv6hdr) - \
1334 sizeof(struct frag_hdr))
1336 struct subflow_send_info {
1341 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1343 if (!subflow->stale)
1347 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1350 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1352 if (unlikely(subflow->stale)) {
1353 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1355 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1358 mptcp_subflow_set_active(subflow);
1360 return __mptcp_subflow_active(subflow);
1363 #define SSK_MODE_ACTIVE 0
1364 #define SSK_MODE_BACKUP 1
1365 #define SSK_MODE_MAX 2
1367 /* implement the mptcp packet scheduler;
1368 * returns the subflow that will transmit the next DSS
1369 * additionally updates the rtx timeout
1371 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1373 struct subflow_send_info send_info[SSK_MODE_MAX];
1374 struct mptcp_subflow_context *subflow;
1375 struct sock *sk = (struct sock *)msk;
1376 u32 pace, burst, wmem;
1377 int i, nr_active = 0;
1382 msk_owned_by_me(msk);
1384 if (__mptcp_check_fallback(msk)) {
1387 return __tcp_can_send(msk->first) &&
1388 sk_stream_memory_free(msk->first) ? msk->first : NULL;
1391 /* re-use last subflow, if the burst allow that */
1392 if (msk->last_snd && msk->snd_burst > 0 &&
1393 sk_stream_memory_free(msk->last_snd) &&
1394 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1395 mptcp_set_timeout(sk);
1396 return msk->last_snd;
1399 /* pick the subflow with the lower wmem/wspace ratio */
1400 for (i = 0; i < SSK_MODE_MAX; ++i) {
1401 send_info[i].ssk = NULL;
1402 send_info[i].linger_time = -1;
1405 mptcp_for_each_subflow(msk, subflow) {
1406 trace_mptcp_subflow_get_send(subflow);
1407 ssk = mptcp_subflow_tcp_sock(subflow);
1408 if (!mptcp_subflow_active(subflow))
1411 tout = max(tout, mptcp_timeout_from_subflow(subflow));
1412 nr_active += !subflow->backup;
1413 pace = subflow->avg_pacing_rate;
1414 if (unlikely(!pace)) {
1415 /* init pacing rate from socket */
1416 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1417 pace = subflow->avg_pacing_rate;
1422 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1423 if (linger_time < send_info[subflow->backup].linger_time) {
1424 send_info[subflow->backup].ssk = ssk;
1425 send_info[subflow->backup].linger_time = linger_time;
1428 __mptcp_set_timeout(sk, tout);
1430 /* pick the best backup if no other subflow is active */
1432 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1434 /* According to the blest algorithm, to avoid HoL blocking for the
1435 * faster flow, we need to:
1436 * - estimate the faster flow linger time
1437 * - use the above to estimate the amount of byte transferred
1438 * by the faster flow
1439 * - check that the amount of queued data is greter than the above,
1440 * otherwise do not use the picked, slower, subflow
1441 * We select the subflow with the shorter estimated time to flush
1442 * the queued mem, which basically ensure the above. We just need
1443 * to check that subflow has a non empty cwin.
1445 ssk = send_info[SSK_MODE_ACTIVE].ssk;
1446 if (!ssk || !sk_stream_memory_free(ssk))
1449 burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1450 wmem = READ_ONCE(ssk->sk_wmem_queued);
1452 msk->last_snd = NULL;
1456 subflow = mptcp_subflow_ctx(ssk);
1457 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1458 READ_ONCE(ssk->sk_pacing_rate) * burst,
1460 msk->last_snd = ssk;
1461 msk->snd_burst = burst;
1465 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1467 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1471 static void mptcp_update_post_push(struct mptcp_sock *msk,
1472 struct mptcp_data_frag *dfrag,
1475 u64 snd_nxt_new = dfrag->data_seq;
1477 dfrag->already_sent += sent;
1479 msk->snd_burst -= sent;
1481 snd_nxt_new += dfrag->already_sent;
1483 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1484 * is recovering after a failover. In that event, this re-sends
1487 * Thus compute snd_nxt_new candidate based on
1488 * the dfrag->data_seq that was sent and the data
1489 * that has been handed to the subflow for transmission
1490 * and skip update in case it was old dfrag.
1492 if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
1493 msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
1494 msk->snd_nxt = snd_nxt_new;
1498 void mptcp_check_and_set_pending(struct sock *sk)
1500 if (mptcp_send_head(sk))
1501 mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING);
1504 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1506 struct sock *prev_ssk = NULL, *ssk = NULL;
1507 struct mptcp_sock *msk = mptcp_sk(sk);
1508 struct mptcp_sendmsg_info info = {
1511 bool do_check_data_fin = false;
1512 struct mptcp_data_frag *dfrag;
1515 while ((dfrag = mptcp_send_head(sk))) {
1516 info.sent = dfrag->already_sent;
1517 info.limit = dfrag->data_len;
1518 len = dfrag->data_len - dfrag->already_sent;
1523 ssk = mptcp_subflow_get_send(msk);
1525 /* First check. If the ssk has changed since
1526 * the last round, release prev_ssk
1528 if (ssk != prev_ssk && prev_ssk)
1529 mptcp_push_release(prev_ssk, &info);
1533 /* Need to lock the new subflow only if different
1534 * from the previous one, otherwise we are still
1535 * helding the relevant lock
1537 if (ssk != prev_ssk)
1540 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1544 mptcp_push_release(ssk, &info);
1548 do_check_data_fin = true;
1552 mptcp_update_post_push(msk, dfrag, ret);
1554 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1557 /* at this point we held the socket lock for the last subflow we used */
1559 mptcp_push_release(ssk, &info);
1562 /* ensure the rtx timer is running */
1563 if (!mptcp_timer_pending(sk))
1564 mptcp_reset_timer(sk);
1565 if (do_check_data_fin)
1566 mptcp_check_send_data_fin(sk);
1569 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
1571 struct mptcp_sock *msk = mptcp_sk(sk);
1572 struct mptcp_sendmsg_info info = {
1573 .data_lock_held = true,
1575 struct mptcp_data_frag *dfrag;
1576 struct sock *xmit_ssk;
1577 int len, copied = 0;
1580 while ((dfrag = mptcp_send_head(sk))) {
1581 info.sent = dfrag->already_sent;
1582 info.limit = dfrag->data_len;
1583 len = dfrag->data_len - dfrag->already_sent;
1587 /* check for a different subflow usage only after
1588 * spooling the first chunk of data
1590 xmit_ssk = first ? ssk : mptcp_subflow_get_send(msk);
1593 if (xmit_ssk != ssk) {
1594 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk),
1595 MPTCP_DELEGATE_SEND);
1599 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1608 mptcp_update_post_push(msk, dfrag, ret);
1610 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1614 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1615 * not going to flush it via release_sock()
1618 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1620 if (!mptcp_timer_pending(sk))
1621 mptcp_reset_timer(sk);
1623 if (msk->snd_data_fin_enable &&
1624 msk->snd_nxt + 1 == msk->write_seq)
1625 mptcp_schedule_work(sk);
1629 static void mptcp_set_nospace(struct sock *sk)
1631 /* enable autotune */
1632 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1634 /* will be cleared on avail space */
1635 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1638 static int mptcp_disconnect(struct sock *sk, int flags);
1640 static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1641 size_t len, int *copied_syn)
1643 unsigned int saved_flags = msg->msg_flags;
1644 struct mptcp_sock *msk = mptcp_sk(sk);
1645 struct socket *ssock;
1649 /* on flags based fastopen the mptcp is supposed to create the
1650 * first subflow right now. Otherwise we are in the defer_connect
1651 * path, and the first subflow must be already present.
1652 * Since the defer_connect flag is cleared after the first succsful
1653 * fastopen attempt, no need to check for additional subflow status.
1655 if (msg->msg_flags & MSG_FASTOPEN) {
1656 ssock = __mptcp_nmpc_socket(msk);
1658 return PTR_ERR(ssock);
1666 msg->msg_flags |= MSG_DONTWAIT;
1667 msk->fastopening = 1;
1668 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1669 msk->fastopening = 0;
1670 msg->msg_flags = saved_flags;
1673 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1674 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1675 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1676 msg->msg_namelen, msg->msg_flags, 1);
1678 /* Keep the same behaviour of plain TCP: zero the copied bytes in
1679 * case of any error, except timeout or signal
1681 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1683 } else if (ret && ret != -EINPROGRESS) {
1684 /* The disconnect() op called by tcp_sendmsg_fastopen()/
1685 * __inet_stream_connect() can fail, due to looking check,
1686 * see mptcp_disconnect().
1687 * Attempt it again outside the problematic scope.
1689 if (!mptcp_disconnect(sk, 0))
1690 sk->sk_socket->state = SS_UNCONNECTED;
1692 inet_sk(sk)->defer_connect = 0;
1697 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1699 struct mptcp_sock *msk = mptcp_sk(sk);
1700 struct page_frag *pfrag;
1705 /* silently ignore everything else */
1706 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1710 if (unlikely(inet_sk(sk)->defer_connect || msg->msg_flags & MSG_FASTOPEN)) {
1713 ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1714 copied += copied_syn;
1715 if (ret == -EINPROGRESS && copied_syn > 0)
1721 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1723 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1724 ret = sk_stream_wait_connect(sk, &timeo);
1730 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1733 pfrag = sk_page_frag(sk);
1735 while (msg_data_left(msg)) {
1736 int total_ts, frag_truesize = 0;
1737 struct mptcp_data_frag *dfrag;
1738 bool dfrag_collapsed;
1739 size_t psize, offset;
1741 /* reuse tail pfrag, if possible, or carve a new one from the
1744 dfrag = mptcp_pending_tail(sk);
1745 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1746 if (!dfrag_collapsed) {
1747 if (!sk_stream_memory_free(sk))
1748 goto wait_for_memory;
1750 if (!mptcp_page_frag_refill(sk, pfrag))
1751 goto wait_for_memory;
1753 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1754 frag_truesize = dfrag->overhead;
1757 /* we do not bound vs wspace, to allow a single packet.
1758 * memory accounting will prevent execessive memory usage
1761 offset = dfrag->offset + dfrag->data_len;
1762 psize = pfrag->size - offset;
1763 psize = min_t(size_t, psize, msg_data_left(msg));
1764 total_ts = psize + frag_truesize;
1766 if (!sk_wmem_schedule(sk, total_ts))
1767 goto wait_for_memory;
1769 if (copy_page_from_iter(dfrag->page, offset, psize,
1770 &msg->msg_iter) != psize) {
1775 /* data successfully copied into the write queue */
1776 sk->sk_forward_alloc -= total_ts;
1778 dfrag->data_len += psize;
1779 frag_truesize += psize;
1780 pfrag->offset += frag_truesize;
1781 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1783 /* charge data on mptcp pending queue to the msk socket
1784 * Note: we charge such data both to sk and ssk
1786 sk_wmem_queued_add(sk, frag_truesize);
1787 if (!dfrag_collapsed) {
1788 get_page(dfrag->page);
1789 list_add_tail(&dfrag->list, &msk->rtx_queue);
1790 if (!msk->first_pending)
1791 WRITE_ONCE(msk->first_pending, dfrag);
1793 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1794 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1800 mptcp_set_nospace(sk);
1801 __mptcp_push_pending(sk, msg->msg_flags);
1802 ret = sk_stream_wait_memory(sk, &timeo);
1808 __mptcp_push_pending(sk, msg->msg_flags);
1818 copied = sk_stream_error(sk, msg->msg_flags, ret);
1822 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1824 size_t len, int flags,
1825 struct scm_timestamping_internal *tss,
1828 struct sk_buff *skb, *tmp;
1831 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1832 u32 offset = MPTCP_SKB_CB(skb)->offset;
1833 u32 data_len = skb->len - offset;
1834 u32 count = min_t(size_t, len - copied, data_len);
1837 if (!(flags & MSG_TRUNC)) {
1838 err = skb_copy_datagram_msg(skb, offset, msg, count);
1839 if (unlikely(err < 0)) {
1846 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1847 tcp_update_recv_tstamps(skb, tss);
1848 *cmsg_flags |= MPTCP_CMSG_TS;
1853 if (count < data_len) {
1854 if (!(flags & MSG_PEEK)) {
1855 MPTCP_SKB_CB(skb)->offset += count;
1856 MPTCP_SKB_CB(skb)->map_seq += count;
1861 if (!(flags & MSG_PEEK)) {
1862 /* we will bulk release the skb memory later */
1863 skb->destructor = NULL;
1864 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1865 __skb_unlink(skb, &msk->receive_queue);
1876 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1878 * Only difference: Use highest rtt estimate of the subflows in use.
1880 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1882 struct mptcp_subflow_context *subflow;
1883 struct sock *sk = (struct sock *)msk;
1884 u32 time, advmss = 1;
1887 msk_owned_by_me(msk);
1892 msk->rcvq_space.copied += copied;
1894 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1895 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1897 rtt_us = msk->rcvq_space.rtt_us;
1898 if (rtt_us && time < (rtt_us >> 3))
1902 mptcp_for_each_subflow(msk, subflow) {
1903 const struct tcp_sock *tp;
1907 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1909 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1910 sf_advmss = READ_ONCE(tp->advmss);
1912 rtt_us = max(sf_rtt_us, rtt_us);
1913 advmss = max(sf_advmss, advmss);
1916 msk->rcvq_space.rtt_us = rtt_us;
1917 if (time < (rtt_us >> 3) || rtt_us == 0)
1920 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1923 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
1924 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1928 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1930 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1932 do_div(grow, msk->rcvq_space.space);
1933 rcvwin += (grow << 1);
1935 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1936 while (tcp_win_from_space(sk, rcvmem) < advmss)
1939 do_div(rcvwin, advmss);
1940 rcvbuf = min_t(u64, rcvwin * rcvmem,
1941 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
1943 if (rcvbuf > sk->sk_rcvbuf) {
1946 window_clamp = tcp_win_from_space(sk, rcvbuf);
1947 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1949 /* Make subflows follow along. If we do not do this, we
1950 * get drops at subflow level if skbs can't be moved to
1951 * the mptcp rx queue fast enough (announced rcv_win can
1952 * exceed ssk->sk_rcvbuf).
1954 mptcp_for_each_subflow(msk, subflow) {
1958 ssk = mptcp_subflow_tcp_sock(subflow);
1959 slow = lock_sock_fast(ssk);
1960 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1961 tcp_sk(ssk)->window_clamp = window_clamp;
1962 tcp_cleanup_rbuf(ssk, 1);
1963 unlock_sock_fast(ssk, slow);
1968 msk->rcvq_space.space = msk->rcvq_space.copied;
1970 msk->rcvq_space.copied = 0;
1971 msk->rcvq_space.time = mstamp;
1974 static void __mptcp_update_rmem(struct sock *sk)
1976 struct mptcp_sock *msk = mptcp_sk(sk);
1978 if (!msk->rmem_released)
1981 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
1982 mptcp_rmem_uncharge(sk, msk->rmem_released);
1983 WRITE_ONCE(msk->rmem_released, 0);
1986 static void __mptcp_splice_receive_queue(struct sock *sk)
1988 struct mptcp_sock *msk = mptcp_sk(sk);
1990 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
1993 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
1995 struct sock *sk = (struct sock *)msk;
1996 unsigned int moved = 0;
2000 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
2003 /* we can have data pending in the subflows only if the msk
2004 * receive buffer was full at subflow_data_ready() time,
2005 * that is an unlikely slow path.
2010 slowpath = lock_sock_fast(ssk);
2011 mptcp_data_lock(sk);
2012 __mptcp_update_rmem(sk);
2013 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
2014 mptcp_data_unlock(sk);
2016 if (unlikely(ssk->sk_err))
2017 __mptcp_error_report(sk);
2018 unlock_sock_fast(ssk, slowpath);
2021 /* acquire the data lock only if some input data is pending */
2023 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
2024 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
2025 mptcp_data_lock(sk);
2026 __mptcp_update_rmem(sk);
2027 ret |= __mptcp_ofo_queue(msk);
2028 __mptcp_splice_receive_queue(sk);
2029 mptcp_data_unlock(sk);
2032 mptcp_check_data_fin((struct sock *)msk);
2033 return !skb_queue_empty(&msk->receive_queue);
2036 static unsigned int mptcp_inq_hint(const struct sock *sk)
2038 const struct mptcp_sock *msk = mptcp_sk(sk);
2039 const struct sk_buff *skb;
2041 skb = skb_peek(&msk->receive_queue);
2043 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
2045 if (hint_val >= INT_MAX)
2048 return (unsigned int)hint_val;
2051 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2057 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2058 int flags, int *addr_len)
2060 struct mptcp_sock *msk = mptcp_sk(sk);
2061 struct scm_timestamping_internal tss;
2062 int copied = 0, cmsg_flags = 0;
2066 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2067 if (unlikely(flags & MSG_ERRQUEUE))
2068 return inet_recv_error(sk, msg, len, addr_len);
2071 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2076 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2078 len = min_t(size_t, len, INT_MAX);
2079 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2081 if (unlikely(msk->recvmsg_inq))
2082 cmsg_flags = MPTCP_CMSG_INQ;
2084 while (copied < len) {
2087 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2088 if (unlikely(bytes_read < 0)) {
2090 copied = bytes_read;
2094 copied += bytes_read;
2096 /* be sure to advertise window change */
2097 mptcp_cleanup_rbuf(msk);
2099 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2102 /* only the master socket status is relevant here. The exit
2103 * conditions mirror closely tcp_recvmsg()
2105 if (copied >= target)
2110 sk->sk_state == TCP_CLOSE ||
2111 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2113 signal_pending(current))
2117 copied = sock_error(sk);
2121 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2122 /* race breaker: the shutdown could be after the
2123 * previous receive queue check
2125 if (__mptcp_move_skbs(msk))
2130 if (sk->sk_state == TCP_CLOSE) {
2140 if (signal_pending(current)) {
2141 copied = sock_intr_errno(timeo);
2146 pr_debug("block timeout %ld", timeo);
2147 sk_wait_data(sk, &timeo, NULL);
2151 if (cmsg_flags && copied >= 0) {
2152 if (cmsg_flags & MPTCP_CMSG_TS)
2153 tcp_recv_timestamp(msg, sk, &tss);
2155 if (cmsg_flags & MPTCP_CMSG_INQ) {
2156 unsigned int inq = mptcp_inq_hint(sk);
2158 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2162 pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2163 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2164 skb_queue_empty(&msk->receive_queue), copied);
2165 if (!(flags & MSG_PEEK))
2166 mptcp_rcv_space_adjust(msk, copied);
2172 static void mptcp_retransmit_timer(struct timer_list *t)
2174 struct inet_connection_sock *icsk = from_timer(icsk, t,
2175 icsk_retransmit_timer);
2176 struct sock *sk = &icsk->icsk_inet.sk;
2177 struct mptcp_sock *msk = mptcp_sk(sk);
2180 if (!sock_owned_by_user(sk)) {
2181 /* we need a process context to retransmit */
2182 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2183 mptcp_schedule_work(sk);
2185 /* delegate our work to tcp_release_cb() */
2186 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2192 static void mptcp_timeout_timer(struct timer_list *t)
2194 struct sock *sk = from_timer(sk, t, sk_timer);
2196 mptcp_schedule_work(sk);
2200 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2203 * A backup subflow is returned only if that is the only kind available.
2205 static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2207 struct sock *backup = NULL, *pick = NULL;
2208 struct mptcp_subflow_context *subflow;
2209 int min_stale_count = INT_MAX;
2211 msk_owned_by_me(msk);
2213 if (__mptcp_check_fallback(msk))
2216 mptcp_for_each_subflow(msk, subflow) {
2217 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2219 if (!__mptcp_subflow_active(subflow))
2222 /* still data outstanding at TCP level? skip this */
2223 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2224 mptcp_pm_subflow_chk_stale(msk, ssk);
2225 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2229 if (subflow->backup) {
2242 /* use backup only if there are no progresses anywhere */
2243 return min_stale_count > 1 ? backup : NULL;
2246 static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2249 iput(SOCK_INODE(msk->subflow));
2250 WRITE_ONCE(msk->subflow, NULL);
2254 bool __mptcp_retransmit_pending_data(struct sock *sk)
2256 struct mptcp_data_frag *cur, *rtx_head;
2257 struct mptcp_sock *msk = mptcp_sk(sk);
2259 if (__mptcp_check_fallback(msk))
2262 if (tcp_rtx_and_write_queues_empty(sk))
2265 /* the closing socket has some data untransmitted and/or unacked:
2266 * some data in the mptcp rtx queue has not really xmitted yet.
2267 * keep it simple and re-inject the whole mptcp level rtx queue
2269 mptcp_data_lock(sk);
2270 __mptcp_clean_una_wakeup(sk);
2271 rtx_head = mptcp_rtx_head(sk);
2273 mptcp_data_unlock(sk);
2277 msk->recovery_snd_nxt = msk->snd_nxt;
2278 msk->recovery = true;
2279 mptcp_data_unlock(sk);
2281 msk->first_pending = rtx_head;
2284 /* be sure to clear the "sent status" on all re-injected fragments */
2285 list_for_each_entry(cur, &msk->rtx_queue, list) {
2286 if (!cur->already_sent)
2288 cur->already_sent = 0;
2294 /* flags for __mptcp_close_ssk() */
2295 #define MPTCP_CF_PUSH BIT(1)
2296 #define MPTCP_CF_FASTCLOSE BIT(2)
2298 /* subflow sockets can be either outgoing (connect) or incoming
2301 * Outgoing subflows use in-kernel sockets.
2302 * Incoming subflows do not have their own 'struct socket' allocated,
2303 * so we need to use tcp_close() after detaching them from the mptcp
2306 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2307 struct mptcp_subflow_context *subflow,
2310 struct mptcp_sock *msk = mptcp_sk(sk);
2311 bool dispose_it, need_push = false;
2313 /* If the first subflow moved to a close state before accept, e.g. due
2314 * to an incoming reset, mptcp either:
2315 * - if either the subflow or the msk are dead, destroy the context
2316 * (the subflow socket is deleted by inet_child_forget) and the msk
2317 * - otherwise do nothing at the moment and take action at accept and/or
2318 * listener shutdown - user-space must be able to accept() the closed
2321 if (msk->in_accept_queue && msk->first == ssk) {
2322 if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD))
2325 /* ensure later check in mptcp_worker() will dispose the msk */
2326 sock_set_flag(sk, SOCK_DEAD);
2327 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2328 mptcp_subflow_drop_ctx(ssk);
2332 dispose_it = !msk->subflow || ssk != msk->subflow->sk;
2334 list_del(&subflow->node);
2336 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2338 if (flags & MPTCP_CF_FASTCLOSE) {
2339 /* be sure to force the tcp_disconnect() path,
2340 * to generate the egress reset
2342 ssk->sk_lingertime = 0;
2343 sock_set_flag(ssk, SOCK_LINGER);
2344 subflow->send_fastclose = 1;
2347 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2349 /* The MPTCP code never wait on the subflow sockets, TCP-level
2350 * disconnect should never fail
2352 WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2353 msk->subflow->state = SS_UNCONNECTED;
2354 mptcp_subflow_ctx_reset(subflow);
2360 subflow->disposable = 1;
2362 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2363 * the ssk has been already destroyed, we just need to release the
2364 * reference owned by msk;
2366 if (!inet_csk(ssk)->icsk_ulp_ops) {
2367 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2368 kfree_rcu(subflow, rcu);
2370 /* otherwise tcp will dispose of the ssk and subflow ctx */
2371 __tcp_close(ssk, 0);
2373 /* close acquired an extra ref */
2382 if (ssk == msk->first)
2383 WRITE_ONCE(msk->first, NULL);
2386 if (ssk == msk->last_snd)
2387 msk->last_snd = NULL;
2390 __mptcp_push_pending(sk, 0);
2393 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2394 struct mptcp_subflow_context *subflow)
2396 if (sk->sk_state == TCP_ESTABLISHED)
2397 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2399 /* subflow aborted before reaching the fully_established status
2400 * attempt the creation of the next subflow
2402 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2404 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2407 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2412 static void __mptcp_close_subflow(struct sock *sk)
2414 struct mptcp_subflow_context *subflow, *tmp;
2415 struct mptcp_sock *msk = mptcp_sk(sk);
2419 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2420 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2422 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2425 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2426 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2429 mptcp_close_ssk(sk, ssk, subflow);
2434 static bool mptcp_should_close(const struct sock *sk)
2436 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2437 struct mptcp_subflow_context *subflow;
2439 if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue)
2442 /* if all subflows are in closed status don't bother with additional
2445 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2446 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2453 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2455 struct mptcp_subflow_context *subflow, *tmp;
2456 struct sock *sk = (struct sock *)msk;
2458 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2461 mptcp_token_destroy(msk);
2463 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2464 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2467 slow = lock_sock_fast(tcp_sk);
2468 if (tcp_sk->sk_state != TCP_CLOSE) {
2469 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2470 tcp_set_state(tcp_sk, TCP_CLOSE);
2472 unlock_sock_fast(tcp_sk, slow);
2475 /* Mirror the tcp_reset() error propagation */
2476 switch (sk->sk_state) {
2478 WRITE_ONCE(sk->sk_err, ECONNREFUSED);
2480 case TCP_CLOSE_WAIT:
2481 WRITE_ONCE(sk->sk_err, EPIPE);
2486 WRITE_ONCE(sk->sk_err, ECONNRESET);
2489 inet_sk_state_store(sk, TCP_CLOSE);
2490 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2491 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2492 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2494 /* the calling mptcp_worker will properly destroy the socket */
2495 if (sock_flag(sk, SOCK_DEAD))
2498 sk->sk_state_change(sk);
2499 sk_error_report(sk);
2502 static void __mptcp_retrans(struct sock *sk)
2504 struct mptcp_sock *msk = mptcp_sk(sk);
2505 struct mptcp_sendmsg_info info = {};
2506 struct mptcp_data_frag *dfrag;
2511 mptcp_clean_una_wakeup(sk);
2513 /* first check ssk: need to kick "stale" logic */
2514 ssk = mptcp_subflow_get_retrans(msk);
2515 dfrag = mptcp_rtx_head(sk);
2517 if (mptcp_data_fin_enabled(msk)) {
2518 struct inet_connection_sock *icsk = inet_csk(sk);
2520 icsk->icsk_retransmits++;
2521 mptcp_set_datafin_timeout(sk);
2522 mptcp_send_ack(msk);
2527 if (!mptcp_send_head(sk))
2538 /* limit retransmission to the bytes already sent on some subflows */
2540 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
2541 while (info.sent < info.limit) {
2542 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2546 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2551 dfrag->already_sent = max(dfrag->already_sent, info.sent);
2552 msk->bytes_retrans += copied;
2553 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2555 WRITE_ONCE(msk->allow_infinite_fallback, false);
2561 mptcp_check_and_set_pending(sk);
2563 if (!mptcp_timer_pending(sk))
2564 mptcp_reset_timer(sk);
2567 /* schedule the timeout timer for the relevant event: either close timeout
2568 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2570 void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout)
2572 struct sock *sk = (struct sock *)msk;
2573 unsigned long timeout, close_timeout;
2575 if (!fail_tout && !sock_flag(sk, SOCK_DEAD))
2578 close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN;
2580 /* the close timeout takes precedence on the fail one, and here at least one of
2583 timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout;
2585 sk_reset_timer(sk, &sk->sk_timer, timeout);
2588 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2590 struct sock *ssk = msk->first;
2596 pr_debug("MP_FAIL doesn't respond, reset the subflow");
2598 slow = lock_sock_fast(ssk);
2599 mptcp_subflow_reset(ssk);
2600 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2601 unlock_sock_fast(ssk, slow);
2603 mptcp_reset_timeout(msk, 0);
2606 static void mptcp_do_fastclose(struct sock *sk)
2608 struct mptcp_subflow_context *subflow, *tmp;
2609 struct mptcp_sock *msk = mptcp_sk(sk);
2611 inet_sk_state_store(sk, TCP_CLOSE);
2612 mptcp_for_each_subflow_safe(msk, subflow, tmp)
2613 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2614 subflow, MPTCP_CF_FASTCLOSE);
2617 static void mptcp_worker(struct work_struct *work)
2619 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2620 struct sock *sk = (struct sock *)msk;
2621 unsigned long fail_tout;
2625 state = sk->sk_state;
2626 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2629 mptcp_check_fastclose(msk);
2631 mptcp_pm_nl_work(msk);
2633 mptcp_check_send_data_fin(sk);
2634 mptcp_check_data_fin_ack(sk);
2635 mptcp_check_data_fin(sk);
2637 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2638 __mptcp_close_subflow(sk);
2640 /* There is no point in keeping around an orphaned sk timedout or
2641 * closed, but we need the msk around to reply to incoming DATA_FIN,
2642 * even if it is orphaned and in FIN_WAIT2 state
2644 if (sock_flag(sk, SOCK_DEAD)) {
2645 if (mptcp_should_close(sk))
2646 mptcp_do_fastclose(sk);
2648 if (sk->sk_state == TCP_CLOSE) {
2649 __mptcp_destroy_sock(sk);
2654 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2655 __mptcp_retrans(sk);
2657 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2658 if (fail_tout && time_after(jiffies, fail_tout))
2659 mptcp_mp_fail_no_response(msk);
2666 static int __mptcp_init_sock(struct sock *sk)
2668 struct mptcp_sock *msk = mptcp_sk(sk);
2670 INIT_LIST_HEAD(&msk->conn_list);
2671 INIT_LIST_HEAD(&msk->join_list);
2672 INIT_LIST_HEAD(&msk->rtx_queue);
2673 INIT_WORK(&msk->work, mptcp_worker);
2674 __skb_queue_head_init(&msk->receive_queue);
2675 msk->out_of_order_queue = RB_ROOT;
2676 msk->first_pending = NULL;
2677 msk->rmem_fwd_alloc = 0;
2678 WRITE_ONCE(msk->rmem_released, 0);
2679 msk->timer_ival = TCP_RTO_MIN;
2681 WRITE_ONCE(msk->first, NULL);
2682 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2683 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2684 WRITE_ONCE(msk->allow_infinite_fallback, true);
2685 msk->recovery = false;
2686 msk->subflow_id = 1;
2688 mptcp_pm_data_init(msk);
2690 /* re-use the csk retrans timer for MPTCP-level retrans */
2691 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2692 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2697 static void mptcp_ca_reset(struct sock *sk)
2699 struct inet_connection_sock *icsk = inet_csk(sk);
2701 tcp_assign_congestion_control(sk);
2702 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2704 /* no need to keep a reference to the ops, the name will suffice */
2705 tcp_cleanup_congestion_control(sk);
2706 icsk->icsk_ca_ops = NULL;
2709 static int mptcp_init_sock(struct sock *sk)
2711 struct net *net = sock_net(sk);
2714 ret = __mptcp_init_sock(sk);
2718 if (!mptcp_is_enabled(net))
2719 return -ENOPROTOOPT;
2721 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2724 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2726 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2727 * propagate the correct value
2731 sk_sockets_allocated_inc(sk);
2732 sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2733 sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2738 static void __mptcp_clear_xmit(struct sock *sk)
2740 struct mptcp_sock *msk = mptcp_sk(sk);
2741 struct mptcp_data_frag *dtmp, *dfrag;
2743 WRITE_ONCE(msk->first_pending, NULL);
2744 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2745 dfrag_clear(sk, dfrag);
2748 void mptcp_cancel_work(struct sock *sk)
2750 struct mptcp_sock *msk = mptcp_sk(sk);
2752 if (cancel_work_sync(&msk->work))
2756 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2760 switch (ssk->sk_state) {
2762 if (!(how & RCV_SHUTDOWN))
2766 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2769 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2770 pr_debug("Fallback");
2771 ssk->sk_shutdown |= how;
2772 tcp_shutdown(ssk, how);
2774 /* simulate the data_fin ack reception to let the state
2775 * machine move forward
2777 WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2778 mptcp_schedule_work(sk);
2780 pr_debug("Sending DATA_FIN on subflow %p", ssk);
2782 if (!mptcp_timer_pending(sk))
2783 mptcp_reset_timer(sk);
2791 static const unsigned char new_state[16] = {
2792 /* current state: new state: action: */
2793 [0 /* (Invalid) */] = TCP_CLOSE,
2794 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2795 [TCP_SYN_SENT] = TCP_CLOSE,
2796 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2797 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2798 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2799 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2800 [TCP_CLOSE] = TCP_CLOSE,
2801 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2802 [TCP_LAST_ACK] = TCP_LAST_ACK,
2803 [TCP_LISTEN] = TCP_CLOSE,
2804 [TCP_CLOSING] = TCP_CLOSING,
2805 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2808 static int mptcp_close_state(struct sock *sk)
2810 int next = (int)new_state[sk->sk_state];
2811 int ns = next & TCP_STATE_MASK;
2813 inet_sk_state_store(sk, ns);
2815 return next & TCP_ACTION_FIN;
2818 static void mptcp_check_send_data_fin(struct sock *sk)
2820 struct mptcp_subflow_context *subflow;
2821 struct mptcp_sock *msk = mptcp_sk(sk);
2823 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2824 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2825 msk->snd_nxt, msk->write_seq);
2827 /* we still need to enqueue subflows or not really shutting down,
2830 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2831 mptcp_send_head(sk))
2834 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2836 mptcp_for_each_subflow(msk, subflow) {
2837 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2839 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2843 static void __mptcp_wr_shutdown(struct sock *sk)
2845 struct mptcp_sock *msk = mptcp_sk(sk);
2847 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2848 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2849 !!mptcp_send_head(sk));
2851 /* will be ignored by fallback sockets */
2852 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2853 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2855 mptcp_check_send_data_fin(sk);
2858 static void __mptcp_destroy_sock(struct sock *sk)
2860 struct mptcp_sock *msk = mptcp_sk(sk);
2862 pr_debug("msk=%p", msk);
2866 mptcp_stop_timer(sk);
2867 sk_stop_timer(sk, &sk->sk_timer);
2870 sk->sk_prot->destroy(sk);
2872 WARN_ON_ONCE(msk->rmem_fwd_alloc);
2873 WARN_ON_ONCE(msk->rmem_released);
2874 sk_stream_kill_queues(sk);
2875 xfrm_sk_free_policy(sk);
2880 void __mptcp_unaccepted_force_close(struct sock *sk)
2882 sock_set_flag(sk, SOCK_DEAD);
2883 mptcp_do_fastclose(sk);
2884 __mptcp_destroy_sock(sk);
2887 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
2889 /* Concurrent splices from sk_receive_queue into receive_queue will
2890 * always show at least one non-empty queue when checked in this order.
2892 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
2893 skb_queue_empty_lockless(&msk->receive_queue))
2896 return EPOLLIN | EPOLLRDNORM;
2899 static void mptcp_check_listen_stop(struct sock *sk)
2903 if (inet_sk_state_load(sk) != TCP_LISTEN)
2906 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
2907 ssk = mptcp_sk(sk)->first;
2908 if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
2911 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2912 mptcp_subflow_queue_clean(sk, ssk);
2913 inet_csk_listen_stop(ssk);
2914 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
2915 tcp_set_state(ssk, TCP_CLOSE);
2919 bool __mptcp_close(struct sock *sk, long timeout)
2921 struct mptcp_subflow_context *subflow;
2922 struct mptcp_sock *msk = mptcp_sk(sk);
2923 bool do_cancel_work = false;
2924 int subflows_alive = 0;
2926 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2928 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2929 mptcp_check_listen_stop(sk);
2930 inet_sk_state_store(sk, TCP_CLOSE);
2934 if (mptcp_check_readable(msk) || timeout < 0) {
2935 /* If the msk has read data, or the caller explicitly ask it,
2936 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
2938 mptcp_do_fastclose(sk);
2940 } else if (mptcp_close_state(sk)) {
2941 __mptcp_wr_shutdown(sk);
2944 sk_stream_wait_close(sk, timeout);
2947 /* orphan all the subflows */
2948 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
2949 mptcp_for_each_subflow(msk, subflow) {
2950 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2951 bool slow = lock_sock_fast_nested(ssk);
2953 subflows_alive += ssk->sk_state != TCP_CLOSE;
2955 /* since the close timeout takes precedence on the fail one,
2958 if (ssk == msk->first)
2959 subflow->fail_tout = 0;
2961 /* detach from the parent socket, but allow data_ready to
2962 * push incoming data into the mptcp stack, to properly ack it
2964 ssk->sk_socket = NULL;
2966 unlock_sock_fast(ssk, slow);
2970 /* all the subflows are closed, only timeout can change the msk
2971 * state, let's not keep resources busy for no reasons
2973 if (subflows_alive == 0)
2974 inet_sk_state_store(sk, TCP_CLOSE);
2977 pr_debug("msk=%p state=%d", sk, sk->sk_state);
2979 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
2981 if (sk->sk_state == TCP_CLOSE) {
2982 __mptcp_destroy_sock(sk);
2983 do_cancel_work = true;
2985 mptcp_reset_timeout(msk, 0);
2988 return do_cancel_work;
2991 static void mptcp_close(struct sock *sk, long timeout)
2993 bool do_cancel_work;
2997 do_cancel_work = __mptcp_close(sk, timeout);
3000 mptcp_cancel_work(sk);
3005 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3007 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3008 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3009 struct ipv6_pinfo *msk6 = inet6_sk(msk);
3011 msk->sk_v6_daddr = ssk->sk_v6_daddr;
3012 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3015 msk6->saddr = ssk6->saddr;
3016 msk6->flow_label = ssk6->flow_label;
3020 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3021 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3022 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3023 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3024 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3025 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3028 static int mptcp_disconnect(struct sock *sk, int flags)
3030 struct mptcp_sock *msk = mptcp_sk(sk);
3032 /* Deny disconnect if other threads are blocked in sk_wait_event()
3033 * or inet_wait_for_connect().
3035 if (sk->sk_wait_pending)
3038 /* We are on the fastopen error path. We can't call straight into the
3039 * subflows cleanup code due to lock nesting (we are already under
3040 * msk->firstsocket lock).
3042 if (msk->fastopening)
3045 mptcp_check_listen_stop(sk);
3046 inet_sk_state_store(sk, TCP_CLOSE);
3048 mptcp_stop_timer(sk);
3049 sk_stop_timer(sk, &sk->sk_timer);
3052 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3054 /* msk->subflow is still intact, the following will not free the first
3057 mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3058 msk->last_snd = NULL;
3059 WRITE_ONCE(msk->flags, 0);
3061 msk->push_pending = 0;
3062 msk->recovery = false;
3063 msk->can_ack = false;
3064 msk->fully_established = false;
3065 msk->rcv_data_fin = false;
3066 msk->snd_data_fin_enable = false;
3067 msk->rcv_fastclose = false;
3068 msk->use_64bit_ack = false;
3069 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3070 mptcp_pm_data_reset(msk);
3072 msk->bytes_acked = 0;
3073 msk->bytes_received = 0;
3074 msk->bytes_sent = 0;
3075 msk->bytes_retrans = 0;
3077 WRITE_ONCE(sk->sk_shutdown, 0);
3078 sk_error_report(sk);
3082 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3083 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3085 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3087 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3091 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3092 const struct mptcp_options_received *mp_opt,
3094 struct request_sock *req)
3096 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3097 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3098 struct mptcp_sock *msk;
3103 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3104 if (nsk->sk_family == AF_INET6)
3105 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3108 nsk->sk_wait_pending = 0;
3109 __mptcp_init_sock(nsk);
3111 msk = mptcp_sk(nsk);
3112 msk->local_key = subflow_req->local_key;
3113 msk->token = subflow_req->token;
3114 WRITE_ONCE(msk->subflow, NULL);
3115 msk->in_accept_queue = 1;
3116 WRITE_ONCE(msk->fully_established, false);
3117 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3118 WRITE_ONCE(msk->csum_enabled, true);
3120 msk->write_seq = subflow_req->idsn + 1;
3121 msk->snd_nxt = msk->write_seq;
3122 msk->snd_una = msk->write_seq;
3123 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
3124 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3126 /* passive msk is created after the first/MPC subflow */
3127 msk->subflow_id = 2;
3129 sock_reset_flag(nsk, SOCK_RCU_FREE);
3130 security_inet_csk_clone(nsk, req);
3132 /* this can't race with mptcp_close(), as the msk is
3133 * not yet exposted to user-space
3135 inet_sk_state_store(nsk, TCP_ESTABLISHED);
3137 /* The msk maintain a ref to each subflow in the connections list */
3138 WRITE_ONCE(msk->first, ssk);
3139 list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list);
3142 /* new mpc subflow takes ownership of the newly
3143 * created mptcp socket
3145 mptcp_token_accept(subflow_req, msk);
3147 /* set msk addresses early to ensure mptcp_pm_get_local_id()
3148 * uses the correct data
3150 mptcp_copy_inaddrs(nsk, ssk);
3151 mptcp_propagate_sndbuf(nsk, ssk);
3153 mptcp_rcv_space_init(msk, ssk);
3154 bh_unlock_sock(nsk);
3156 /* note: the newly allocated socket refcount is 2 now */
3160 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3162 const struct tcp_sock *tp = tcp_sk(ssk);
3164 msk->rcvq_space.copied = 0;
3165 msk->rcvq_space.rtt_us = 0;
3167 msk->rcvq_space.time = tp->tcp_mstamp;
3169 /* initial rcv_space offering made to peer */
3170 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3171 TCP_INIT_CWND * tp->advmss);
3172 if (msk->rcvq_space.space == 0)
3173 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3175 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3178 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
3181 struct mptcp_sock *msk = mptcp_sk(sk);
3182 struct socket *listener;
3185 listener = READ_ONCE(msk->subflow);
3186 if (WARN_ON_ONCE(!listener)) {
3191 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
3192 newsk = inet_csk_accept(listener->sk, flags, err, kern);
3196 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
3197 if (sk_is_mptcp(newsk)) {
3198 struct mptcp_subflow_context *subflow;
3199 struct sock *new_mptcp_sock;
3201 subflow = mptcp_subflow_ctx(newsk);
3202 new_mptcp_sock = subflow->conn;
3204 /* is_mptcp should be false if subflow->conn is missing, see
3205 * subflow_syn_recv_sock()
3207 if (WARN_ON_ONCE(!new_mptcp_sock)) {
3208 tcp_sk(newsk)->is_mptcp = 0;
3212 newsk = new_mptcp_sock;
3213 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3215 MPTCP_INC_STATS(sock_net(sk),
3216 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3220 newsk->sk_kern_sock = kern;
3224 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
3226 struct mptcp_subflow_context *subflow, *tmp;
3227 struct sock *sk = (struct sock *)msk;
3229 __mptcp_clear_xmit(sk);
3231 /* join list will be eventually flushed (with rst) at sock lock release time */
3232 mptcp_for_each_subflow_safe(msk, subflow, tmp)
3233 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3235 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3236 mptcp_data_lock(sk);
3237 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
3238 __skb_queue_purge(&sk->sk_receive_queue);
3239 skb_rbtree_purge(&msk->out_of_order_queue);
3240 mptcp_data_unlock(sk);
3242 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3243 * inet_sock_destruct() will dispose it
3245 sk->sk_forward_alloc += msk->rmem_fwd_alloc;
3246 msk->rmem_fwd_alloc = 0;
3247 mptcp_token_destroy(msk);
3248 mptcp_pm_free_anno_list(msk);
3249 mptcp_free_local_addr_list(msk);
3252 static void mptcp_destroy(struct sock *sk)
3254 struct mptcp_sock *msk = mptcp_sk(sk);
3256 /* clears msk->subflow, allowing the following to close
3257 * even the initial subflow
3259 mptcp_dispose_initial_subflow(msk);
3260 mptcp_destroy_common(msk, 0);
3261 sk_sockets_allocated_dec(sk);
3264 void __mptcp_data_acked(struct sock *sk)
3266 if (!sock_owned_by_user(sk))
3267 __mptcp_clean_una(sk);
3269 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3271 if (mptcp_pending_data_fin_ack(sk))
3272 mptcp_schedule_work(sk);
3275 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3277 if (!mptcp_send_head(sk))
3280 if (!sock_owned_by_user(sk))
3281 __mptcp_subflow_push_pending(sk, ssk, false);
3283 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3286 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3287 BIT(MPTCP_RETRANSMIT) | \
3288 BIT(MPTCP_FLUSH_JOIN_LIST))
3290 /* processes deferred events and flush wmem */
3291 static void mptcp_release_cb(struct sock *sk)
3292 __must_hold(&sk->sk_lock.slock)
3294 struct mptcp_sock *msk = mptcp_sk(sk);
3297 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) |
3299 struct list_head join_list;
3304 INIT_LIST_HEAD(&join_list);
3305 list_splice_init(&msk->join_list, &join_list);
3307 /* the following actions acquire the subflow socket lock
3309 * 1) can't be invoked in atomic scope
3310 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3311 * datapath acquires the msk socket spinlock while helding
3312 * the subflow socket lock
3314 msk->push_pending = 0;
3315 msk->cb_flags &= ~flags;
3316 spin_unlock_bh(&sk->sk_lock.slock);
3318 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3319 __mptcp_flush_join_list(sk, &join_list);
3320 if (flags & BIT(MPTCP_PUSH_PENDING))
3321 __mptcp_push_pending(sk, 0);
3322 if (flags & BIT(MPTCP_RETRANSMIT))
3323 __mptcp_retrans(sk);
3326 spin_lock_bh(&sk->sk_lock.slock);
3329 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3330 __mptcp_clean_una_wakeup(sk);
3331 if (unlikely(&msk->cb_flags)) {
3332 /* be sure to set the current sk state before tacking actions
3333 * depending on sk_state, that is processing MPTCP_ERROR_REPORT
3335 if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags))
3336 __mptcp_set_connected(sk);
3337 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3338 __mptcp_error_report(sk);
3339 if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags))
3340 msk->last_snd = NULL;
3343 __mptcp_update_rmem(sk);
3346 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3347 * TCP can't schedule delack timer before the subflow is fully established.
3348 * MPTCP uses the delack timer to do 3rd ack retransmissions
3350 static void schedule_3rdack_retransmission(struct sock *ssk)
3352 struct inet_connection_sock *icsk = inet_csk(ssk);
3353 struct tcp_sock *tp = tcp_sk(ssk);
3354 unsigned long timeout;
3356 if (mptcp_subflow_ctx(ssk)->fully_established)
3359 /* reschedule with a timeout above RTT, as we must look only for drop */
3361 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3363 timeout = TCP_TIMEOUT_INIT;
3366 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3367 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3368 icsk->icsk_ack.timeout = timeout;
3369 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3372 void mptcp_subflow_process_delegated(struct sock *ssk)
3374 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3375 struct sock *sk = subflow->conn;
3377 if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) {
3378 mptcp_data_lock(sk);
3379 if (!sock_owned_by_user(sk))
3380 __mptcp_subflow_push_pending(sk, ssk, true);
3382 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3383 mptcp_data_unlock(sk);
3384 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND);
3386 if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) {
3387 schedule_3rdack_retransmission(ssk);
3388 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK);
3392 static int mptcp_hash(struct sock *sk)
3394 /* should never be called,
3395 * we hash the TCP subflows not the master socket
3401 static void mptcp_unhash(struct sock *sk)
3403 /* called from sk_common_release(), but nothing to do here */
3406 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3408 struct mptcp_sock *msk = mptcp_sk(sk);
3409 struct socket *ssock;
3411 ssock = msk->subflow;
3412 pr_debug("msk=%p, subflow=%p", msk, ssock);
3413 if (WARN_ON_ONCE(!ssock))
3416 return inet_csk_get_port(ssock->sk, snum);
3419 void mptcp_finish_connect(struct sock *ssk)
3421 struct mptcp_subflow_context *subflow;
3422 struct mptcp_sock *msk;
3425 subflow = mptcp_subflow_ctx(ssk);
3429 pr_debug("msk=%p, token=%u", sk, subflow->token);
3431 subflow->map_seq = subflow->iasn;
3432 subflow->map_subflow_seq = 1;
3434 /* the socket is not connected yet, no msk/subflow ops can access/race
3435 * accessing the field below
3437 WRITE_ONCE(msk->local_key, subflow->local_key);
3438 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3439 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3440 WRITE_ONCE(msk->snd_una, msk->write_seq);
3442 mptcp_pm_new_connection(msk, ssk, 0);
3444 mptcp_rcv_space_init(msk, ssk);
3447 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3449 write_lock_bh(&sk->sk_callback_lock);
3450 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3451 sk_set_socket(sk, parent);
3452 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3453 write_unlock_bh(&sk->sk_callback_lock);
3456 bool mptcp_finish_join(struct sock *ssk)
3458 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3459 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3460 struct sock *parent = (void *)msk;
3463 pr_debug("msk=%p, subflow=%p", msk, subflow);
3465 /* mptcp socket already closing? */
3466 if (!mptcp_is_fully_established(parent)) {
3467 subflow->reset_reason = MPTCP_RST_EMPTCP;
3471 /* active subflow, already present inside the conn_list */
3472 if (!list_empty(&subflow->node)) {
3473 mptcp_subflow_joined(msk, ssk);
3477 if (!mptcp_pm_allow_new_subflow(msk))
3478 goto err_prohibited;
3480 /* If we can't acquire msk socket lock here, let the release callback
3483 mptcp_data_lock(parent);
3484 if (!sock_owned_by_user(parent)) {
3485 ret = __mptcp_finish_join(msk, ssk);
3488 list_add_tail(&subflow->node, &msk->conn_list);
3492 list_add_tail(&subflow->node, &msk->join_list);
3493 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3495 mptcp_data_unlock(parent);
3499 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3506 static void mptcp_shutdown(struct sock *sk, int how)
3508 pr_debug("sk=%p, how=%d", sk, how);
3510 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3511 __mptcp_wr_shutdown(sk);
3514 static int mptcp_forward_alloc_get(const struct sock *sk)
3516 return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3519 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3521 const struct sock *sk = (void *)msk;
3524 if (sk->sk_state == TCP_LISTEN)
3527 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3530 delta = msk->write_seq - v;
3531 if (__mptcp_check_fallback(msk) && msk->first) {
3532 struct tcp_sock *tp = tcp_sk(msk->first);
3534 /* the first subflow is disconnected after close - see
3535 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3536 * so ignore that status, too.
3538 if (!((1 << msk->first->sk_state) &
3539 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3540 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3542 if (delta > INT_MAX)
3548 static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3550 struct mptcp_sock *msk = mptcp_sk(sk);
3555 if (sk->sk_state == TCP_LISTEN)
3559 __mptcp_move_skbs(msk);
3560 *karg = mptcp_inq_hint(sk);
3564 slow = lock_sock_fast(sk);
3565 *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3566 unlock_sock_fast(sk, slow);
3569 slow = lock_sock_fast(sk);
3570 *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3571 unlock_sock_fast(sk, slow);
3574 return -ENOIOCTLCMD;
3580 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3581 struct mptcp_subflow_context *subflow)
3583 subflow->request_mptcp = 0;
3584 __mptcp_do_fallback(msk);
3587 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3589 struct mptcp_subflow_context *subflow;
3590 struct mptcp_sock *msk = mptcp_sk(sk);
3591 struct socket *ssock;
3594 ssock = __mptcp_nmpc_socket(msk);
3596 return PTR_ERR(ssock);
3598 mptcp_token_destroy(msk);
3599 inet_sk_state_store(sk, TCP_SYN_SENT);
3600 subflow = mptcp_subflow_ctx(ssock->sk);
3601 #ifdef CONFIG_TCP_MD5SIG
3602 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3605 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
3606 mptcp_subflow_early_fallback(msk, subflow);
3608 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3609 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
3610 mptcp_subflow_early_fallback(msk, subflow);
3612 if (likely(!__mptcp_check_fallback(msk)))
3613 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3615 /* if reaching here via the fastopen/sendmsg path, the caller already
3616 * acquired the subflow socket lock, too.
3618 if (msk->fastopening)
3619 err = __inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK, 1);
3621 err = inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK);
3622 inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect;
3624 /* on successful connect, the msk state will be moved to established by
3625 * subflow_finish_connect()
3627 if (unlikely(err && err != -EINPROGRESS)) {
3628 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk));
3632 mptcp_copy_inaddrs(sk, ssock->sk);
3634 /* silence EINPROGRESS and let the caller inet_stream_connect
3635 * handle the connection in progress
3640 static struct proto mptcp_prot = {
3642 .owner = THIS_MODULE,
3643 .init = mptcp_init_sock,
3644 .connect = mptcp_connect,
3645 .disconnect = mptcp_disconnect,
3646 .close = mptcp_close,
3647 .accept = mptcp_accept,
3648 .setsockopt = mptcp_setsockopt,
3649 .getsockopt = mptcp_getsockopt,
3650 .shutdown = mptcp_shutdown,
3651 .destroy = mptcp_destroy,
3652 .sendmsg = mptcp_sendmsg,
3653 .ioctl = mptcp_ioctl,
3654 .recvmsg = mptcp_recvmsg,
3655 .release_cb = mptcp_release_cb,
3657 .unhash = mptcp_unhash,
3658 .get_port = mptcp_get_port,
3659 .forward_alloc_get = mptcp_forward_alloc_get,
3660 .sockets_allocated = &mptcp_sockets_allocated,
3662 .memory_allocated = &tcp_memory_allocated,
3663 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3665 .memory_pressure = &tcp_memory_pressure,
3666 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3667 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3668 .sysctl_mem = sysctl_tcp_mem,
3669 .obj_size = sizeof(struct mptcp_sock),
3670 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3671 .no_autobind = true,
3674 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3676 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3677 struct socket *ssock;
3680 lock_sock(sock->sk);
3681 ssock = __mptcp_nmpc_socket(msk);
3682 if (IS_ERR(ssock)) {
3683 err = PTR_ERR(ssock);
3687 err = ssock->ops->bind(ssock, uaddr, addr_len);
3689 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3692 release_sock(sock->sk);
3696 static int mptcp_listen(struct socket *sock, int backlog)
3698 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3699 struct sock *sk = sock->sk;
3700 struct socket *ssock;
3703 pr_debug("msk=%p", msk);
3706 ssock = __mptcp_nmpc_socket(msk);
3707 if (IS_ERR(ssock)) {
3708 err = PTR_ERR(ssock);
3712 mptcp_token_destroy(msk);
3713 inet_sk_state_store(sk, TCP_LISTEN);
3714 sock_set_flag(sk, SOCK_RCU_FREE);
3716 err = ssock->ops->listen(ssock, backlog);
3717 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk));
3719 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3720 mptcp_copy_inaddrs(sk, ssock->sk);
3723 mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED);
3730 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3731 int flags, bool kern)
3733 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3734 struct socket *ssock;
3738 pr_debug("msk=%p", msk);
3740 /* Buggy applications can call accept on socket states other then LISTEN
3741 * but no need to allocate the first subflow just to error out.
3743 ssock = READ_ONCE(msk->subflow);
3747 newsk = mptcp_accept(sock->sk, flags, &err, kern);
3753 __inet_accept(sock, newsock, newsk);
3754 if (!mptcp_is_tcpsk(newsock->sk)) {
3755 struct mptcp_sock *msk = mptcp_sk(newsk);
3756 struct mptcp_subflow_context *subflow;
3758 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3759 msk->in_accept_queue = 0;
3761 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3762 * This is needed so NOSPACE flag can be set from tcp stack.
3764 mptcp_for_each_subflow(msk, subflow) {
3765 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3767 if (!ssk->sk_socket)
3768 mptcp_sock_graft(ssk, newsock);
3771 /* Do late cleanup for the first subflow as necessary. Also
3772 * deal with bad peers not doing a complete shutdown.
3775 unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
3776 __mptcp_close_ssk(newsk, msk->first,
3777 mptcp_subflow_ctx(msk->first), 0);
3778 if (unlikely(list_empty(&msk->conn_list)))
3779 inet_sk_state_store(newsk, TCP_CLOSE);
3782 release_sock(newsk);
3787 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3789 struct sock *sk = (struct sock *)msk;
3791 if (sk_stream_is_writeable(sk))
3792 return EPOLLOUT | EPOLLWRNORM;
3794 mptcp_set_nospace(sk);
3795 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3796 if (sk_stream_is_writeable(sk))
3797 return EPOLLOUT | EPOLLWRNORM;
3802 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3803 struct poll_table_struct *wait)
3805 struct sock *sk = sock->sk;
3806 struct mptcp_sock *msk;
3812 sock_poll_wait(file, sock, wait);
3814 state = inet_sk_state_load(sk);
3815 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
3816 if (state == TCP_LISTEN) {
3817 struct socket *ssock = READ_ONCE(msk->subflow);
3819 if (WARN_ON_ONCE(!ssock || !ssock->sk))
3822 return inet_csk_listen_poll(ssock->sk);
3825 shutdown = READ_ONCE(sk->sk_shutdown);
3826 if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3828 if (shutdown & RCV_SHUTDOWN)
3829 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3831 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3832 mask |= mptcp_check_readable(msk);
3833 if (shutdown & SEND_SHUTDOWN)
3834 mask |= EPOLLOUT | EPOLLWRNORM;
3836 mask |= mptcp_check_writeable(msk);
3837 } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) {
3838 /* cf tcp_poll() note about TFO */
3839 mask |= EPOLLOUT | EPOLLWRNORM;
3842 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
3844 if (READ_ONCE(sk->sk_err))
3850 static const struct proto_ops mptcp_stream_ops = {
3852 .owner = THIS_MODULE,
3853 .release = inet_release,
3855 .connect = inet_stream_connect,
3856 .socketpair = sock_no_socketpair,
3857 .accept = mptcp_stream_accept,
3858 .getname = inet_getname,
3860 .ioctl = inet_ioctl,
3861 .gettstamp = sock_gettstamp,
3862 .listen = mptcp_listen,
3863 .shutdown = inet_shutdown,
3864 .setsockopt = sock_common_setsockopt,
3865 .getsockopt = sock_common_getsockopt,
3866 .sendmsg = inet_sendmsg,
3867 .recvmsg = inet_recvmsg,
3868 .mmap = sock_no_mmap,
3871 static struct inet_protosw mptcp_protosw = {
3872 .type = SOCK_STREAM,
3873 .protocol = IPPROTO_MPTCP,
3874 .prot = &mptcp_prot,
3875 .ops = &mptcp_stream_ops,
3876 .flags = INET_PROTOSW_ICSK,
3879 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3881 struct mptcp_delegated_action *delegated;
3882 struct mptcp_subflow_context *subflow;
3885 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3886 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3887 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3889 bh_lock_sock_nested(ssk);
3890 if (!sock_owned_by_user(ssk) &&
3891 mptcp_subflow_has_delegated_action(subflow))
3892 mptcp_subflow_process_delegated(ssk);
3893 /* ... elsewhere tcp_release_cb_override already processed
3894 * the action or will do at next release_sock().
3895 * In both case must dequeue the subflow here - on the same
3896 * CPU that scheduled it.
3898 bh_unlock_sock(ssk);
3901 if (++work_done == budget)
3905 /* always provide a 0 'work_done' argument, so that napi_complete_done
3906 * will not try accessing the NULL napi->dev ptr
3908 napi_complete_done(napi, 0);
3912 void __init mptcp_proto_init(void)
3914 struct mptcp_delegated_action *delegated;
3917 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
3919 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3920 panic("Failed to allocate MPTCP pcpu counter\n");
3922 init_dummy_netdev(&mptcp_napi_dev);
3923 for_each_possible_cpu(cpu) {
3924 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3925 INIT_LIST_HEAD(&delegated->head);
3926 netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
3928 napi_enable(&delegated->napi);
3931 mptcp_subflow_init();
3935 if (proto_register(&mptcp_prot, 1) != 0)
3936 panic("Failed to register MPTCP proto.\n");
3938 inet_register_protosw(&mptcp_protosw);
3940 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
3943 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3944 static const struct proto_ops mptcp_v6_stream_ops = {
3946 .owner = THIS_MODULE,
3947 .release = inet6_release,
3949 .connect = inet_stream_connect,
3950 .socketpair = sock_no_socketpair,
3951 .accept = mptcp_stream_accept,
3952 .getname = inet6_getname,
3954 .ioctl = inet6_ioctl,
3955 .gettstamp = sock_gettstamp,
3956 .listen = mptcp_listen,
3957 .shutdown = inet_shutdown,
3958 .setsockopt = sock_common_setsockopt,
3959 .getsockopt = sock_common_getsockopt,
3960 .sendmsg = inet6_sendmsg,
3961 .recvmsg = inet6_recvmsg,
3962 .mmap = sock_no_mmap,
3963 #ifdef CONFIG_COMPAT
3964 .compat_ioctl = inet6_compat_ioctl,
3968 static struct proto mptcp_v6_prot;
3970 static struct inet_protosw mptcp_v6_protosw = {
3971 .type = SOCK_STREAM,
3972 .protocol = IPPROTO_MPTCP,
3973 .prot = &mptcp_v6_prot,
3974 .ops = &mptcp_v6_stream_ops,
3975 .flags = INET_PROTOSW_ICSK,
3978 int __init mptcp_proto_v6_init(void)
3982 mptcp_v6_prot = mptcp_prot;
3983 strcpy(mptcp_v6_prot.name, "MPTCPv6");
3984 mptcp_v6_prot.slab = NULL;
3985 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
3987 err = proto_register(&mptcp_v6_prot, 1);
3991 err = inet6_register_protosw(&mptcp_v6_protosw);
3993 proto_unregister(&mptcp_v6_prot);