1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2017 - 2019, Intel Corporation.
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/sched/signal.h>
13 #include <linux/atomic.h>
15 #include <net/inet_common.h>
16 #include <net/inet_hashtables.h>
17 #include <net/protocol.h>
19 #include <net/tcp_states.h>
20 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21 #include <net/transp_v6.h>
23 #include <net/mptcp.h>
25 #include <asm/ioctls.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/mptcp.h>
32 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
34 struct mptcp_sock msk;
40 MPTCP_CMSG_TS = BIT(0),
41 MPTCP_CMSG_INQ = BIT(1),
44 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
46 static void __mptcp_destroy_sock(struct sock *sk);
47 static void mptcp_check_send_data_fin(struct sock *sk);
49 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
50 static struct net_device mptcp_napi_dev;
52 /* Returns end sequence number of the receiver's advertised window */
53 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
55 return READ_ONCE(msk->wnd_end);
58 static bool mptcp_is_tcpsk(struct sock *sk)
60 struct socket *sock = sk->sk_socket;
62 if (unlikely(sk->sk_prot == &tcp_prot)) {
63 /* we are being invoked after mptcp_accept() has
64 * accepted a non-mp-capable flow: sk is a tcp_sk,
67 * Hand the socket over to tcp so all further socket ops
70 WRITE_ONCE(sock->ops, &inet_stream_ops);
72 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
73 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
74 WRITE_ONCE(sock->ops, &inet6_stream_ops);
82 static int __mptcp_socket_create(struct mptcp_sock *msk)
84 struct mptcp_subflow_context *subflow;
85 struct sock *sk = (struct sock *)msk;
89 err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
93 msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio;
94 WRITE_ONCE(msk->first, ssock->sk);
95 subflow = mptcp_subflow_ctx(ssock->sk);
96 list_add(&subflow->node, &msk->conn_list);
98 subflow->request_mptcp = 1;
99 subflow->subflow_id = msk->subflow_id++;
101 /* This is the first subflow, always with id 0 */
102 subflow->local_id_valid = 1;
103 mptcp_sock_graft(msk->first, sk->sk_socket);
104 iput(SOCK_INODE(ssock));
109 /* If the MPC handshake is not started, returns the first subflow,
110 * eventually allocating it.
112 struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk)
114 struct sock *sk = (struct sock *)msk;
117 if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
118 return ERR_PTR(-EINVAL);
121 ret = __mptcp_socket_create(msk);
125 mptcp_sockopt_sync(msk, msk->first);
131 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
133 sk_drops_add(sk, skb);
137 static void mptcp_rmem_charge(struct sock *sk, int size)
139 mptcp_sk(sk)->rmem_fwd_alloc -= size;
142 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
143 struct sk_buff *from)
148 if (MPTCP_SKB_CB(from)->offset ||
149 !skb_try_coalesce(to, from, &fragstolen, &delta))
152 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
153 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
154 to->len, MPTCP_SKB_CB(from)->end_seq);
155 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
157 /* note the fwd memory can reach a negative value after accounting
158 * for the delta, but the later skb free will restore a non
161 atomic_add(delta, &sk->sk_rmem_alloc);
162 mptcp_rmem_charge(sk, delta);
163 kfree_skb_partial(from, fragstolen);
168 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
169 struct sk_buff *from)
171 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
174 return mptcp_try_coalesce((struct sock *)msk, to, from);
177 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
179 amount >>= PAGE_SHIFT;
180 mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT;
181 __sk_mem_reduce_allocated(sk, amount);
184 static void mptcp_rmem_uncharge(struct sock *sk, int size)
186 struct mptcp_sock *msk = mptcp_sk(sk);
189 msk->rmem_fwd_alloc += size;
190 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
192 /* see sk_mem_uncharge() for the rationale behind the following schema */
193 if (unlikely(reclaimable >= PAGE_SIZE))
194 __mptcp_rmem_reclaim(sk, reclaimable);
197 static void mptcp_rfree(struct sk_buff *skb)
199 unsigned int len = skb->truesize;
200 struct sock *sk = skb->sk;
202 atomic_sub(len, &sk->sk_rmem_alloc);
203 mptcp_rmem_uncharge(sk, len);
206 void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
210 skb->destructor = mptcp_rfree;
211 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
212 mptcp_rmem_charge(sk, skb->truesize);
215 /* "inspired" by tcp_data_queue_ofo(), main differences:
217 * - don't cope with sacks
219 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
221 struct sock *sk = (struct sock *)msk;
222 struct rb_node **p, *parent;
223 u64 seq, end_seq, max_seq;
224 struct sk_buff *skb1;
226 seq = MPTCP_SKB_CB(skb)->map_seq;
227 end_seq = MPTCP_SKB_CB(skb)->end_seq;
228 max_seq = atomic64_read(&msk->rcv_wnd_sent);
230 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
231 RB_EMPTY_ROOT(&msk->out_of_order_queue));
232 if (after64(end_seq, max_seq)) {
235 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
236 (unsigned long long)end_seq - (unsigned long)max_seq,
237 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
238 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
242 p = &msk->out_of_order_queue.rb_node;
243 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
244 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
245 rb_link_node(&skb->rbnode, NULL, p);
246 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
247 msk->ooo_last_skb = skb;
251 /* with 2 subflows, adding at end of ooo queue is quite likely
252 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
254 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
255 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
256 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
260 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
261 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
262 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
263 parent = &msk->ooo_last_skb->rbnode;
264 p = &parent->rb_right;
268 /* Find place to insert this segment. Handle overlaps on the way. */
272 skb1 = rb_to_skb(parent);
273 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
274 p = &parent->rb_left;
277 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
278 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
279 /* All the bits are present. Drop. */
281 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
284 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
288 * continue traversing
291 /* skb's seq == skb1's seq and skb covers skb1.
292 * Replace skb1 with skb.
294 rb_replace_node(&skb1->rbnode, &skb->rbnode,
295 &msk->out_of_order_queue);
296 mptcp_drop(sk, skb1);
297 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
300 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
301 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
304 p = &parent->rb_right;
308 /* Insert segment into RB tree. */
309 rb_link_node(&skb->rbnode, parent, p);
310 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
313 /* Remove other segments covered by skb. */
314 while ((skb1 = skb_rb_next(skb)) != NULL) {
315 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
317 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
318 mptcp_drop(sk, skb1);
319 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
321 /* If there is no skb after us, we are the last_skb ! */
323 msk->ooo_last_skb = skb;
327 mptcp_set_owner_r(skb, sk);
330 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
332 struct mptcp_sock *msk = mptcp_sk(sk);
335 if (size <= msk->rmem_fwd_alloc)
338 size -= msk->rmem_fwd_alloc;
339 amt = sk_mem_pages(size);
340 amount = amt << PAGE_SHIFT;
341 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
344 msk->rmem_fwd_alloc += amount;
348 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
349 struct sk_buff *skb, unsigned int offset,
352 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
353 struct sock *sk = (struct sock *)msk;
354 struct sk_buff *tail;
357 __skb_unlink(skb, &ssk->sk_receive_queue);
362 /* try to fetch required memory from subflow */
363 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
366 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
368 /* the skb map_seq accounts for the skb offset:
369 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
372 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
373 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
374 MPTCP_SKB_CB(skb)->offset = offset;
375 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
377 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
379 msk->bytes_received += copy_len;
380 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
381 tail = skb_peek_tail(&sk->sk_receive_queue);
382 if (tail && mptcp_try_coalesce(sk, tail, skb))
385 mptcp_set_owner_r(skb, sk);
386 __skb_queue_tail(&sk->sk_receive_queue, skb);
388 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
389 mptcp_data_queue_ofo(msk, skb);
393 /* old data, keep it simple and drop the whole pkt, sender
394 * will retransmit as needed, if needed.
396 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
402 static void mptcp_stop_timer(struct sock *sk)
404 struct inet_connection_sock *icsk = inet_csk(sk);
406 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
407 mptcp_sk(sk)->timer_ival = 0;
410 static void mptcp_close_wake_up(struct sock *sk)
412 if (sock_flag(sk, SOCK_DEAD))
415 sk->sk_state_change(sk);
416 if (sk->sk_shutdown == SHUTDOWN_MASK ||
417 sk->sk_state == TCP_CLOSE)
418 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
420 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
423 static bool mptcp_pending_data_fin_ack(struct sock *sk)
425 struct mptcp_sock *msk = mptcp_sk(sk);
427 return ((1 << sk->sk_state) &
428 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
429 msk->write_seq == READ_ONCE(msk->snd_una);
432 static void mptcp_check_data_fin_ack(struct sock *sk)
434 struct mptcp_sock *msk = mptcp_sk(sk);
436 /* Look for an acknowledged DATA_FIN */
437 if (mptcp_pending_data_fin_ack(sk)) {
438 WRITE_ONCE(msk->snd_data_fin_enable, 0);
440 switch (sk->sk_state) {
442 inet_sk_state_store(sk, TCP_FIN_WAIT2);
446 inet_sk_state_store(sk, TCP_CLOSE);
450 mptcp_close_wake_up(sk);
454 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
456 struct mptcp_sock *msk = mptcp_sk(sk);
458 if (READ_ONCE(msk->rcv_data_fin) &&
459 ((1 << sk->sk_state) &
460 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
461 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
463 if (msk->ack_seq == rcv_data_fin_seq) {
465 *seq = rcv_data_fin_seq;
474 static void mptcp_set_datafin_timeout(struct sock *sk)
476 struct inet_connection_sock *icsk = inet_csk(sk);
479 retransmits = min_t(u32, icsk->icsk_retransmits,
480 ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
482 mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
485 static void __mptcp_set_timeout(struct sock *sk, long tout)
487 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
490 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
492 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
494 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
495 inet_csk(ssk)->icsk_timeout - jiffies : 0;
498 static void mptcp_set_timeout(struct sock *sk)
500 struct mptcp_subflow_context *subflow;
503 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
504 tout = max(tout, mptcp_timeout_from_subflow(subflow));
505 __mptcp_set_timeout(sk, tout);
508 static inline bool tcp_can_send_ack(const struct sock *ssk)
510 return !((1 << inet_sk_state_load(ssk)) &
511 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
514 void __mptcp_subflow_send_ack(struct sock *ssk)
516 if (tcp_can_send_ack(ssk))
520 static void mptcp_subflow_send_ack(struct sock *ssk)
524 slow = lock_sock_fast(ssk);
525 __mptcp_subflow_send_ack(ssk);
526 unlock_sock_fast(ssk, slow);
529 static void mptcp_send_ack(struct mptcp_sock *msk)
531 struct mptcp_subflow_context *subflow;
533 mptcp_for_each_subflow(msk, subflow)
534 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
537 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
541 slow = lock_sock_fast(ssk);
542 if (tcp_can_send_ack(ssk))
543 tcp_cleanup_rbuf(ssk, 1);
544 unlock_sock_fast(ssk, slow);
547 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
549 const struct inet_connection_sock *icsk = inet_csk(ssk);
550 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
551 const struct tcp_sock *tp = tcp_sk(ssk);
553 return (ack_pending & ICSK_ACK_SCHED) &&
554 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
555 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
556 (rx_empty && ack_pending &
557 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
560 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
562 int old_space = READ_ONCE(msk->old_wspace);
563 struct mptcp_subflow_context *subflow;
564 struct sock *sk = (struct sock *)msk;
565 int space = __mptcp_space(sk);
566 bool cleanup, rx_empty;
568 cleanup = (space > 0) && (space >= (old_space << 1));
569 rx_empty = !__mptcp_rmem(sk);
571 mptcp_for_each_subflow(msk, subflow) {
572 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
574 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
575 mptcp_subflow_cleanup_rbuf(ssk);
579 static bool mptcp_check_data_fin(struct sock *sk)
581 struct mptcp_sock *msk = mptcp_sk(sk);
582 u64 rcv_data_fin_seq;
585 /* Need to ack a DATA_FIN received from a peer while this side
586 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
587 * msk->rcv_data_fin was set when parsing the incoming options
588 * at the subflow level and the msk lock was not held, so this
589 * is the first opportunity to act on the DATA_FIN and change
592 * If we are caught up to the sequence number of the incoming
593 * DATA_FIN, send the DATA_ACK now and do state transition. If
594 * not caught up, do nothing and let the recv code send DATA_ACK
598 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
599 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
600 WRITE_ONCE(msk->rcv_data_fin, 0);
602 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
603 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
605 switch (sk->sk_state) {
606 case TCP_ESTABLISHED:
607 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
610 inet_sk_state_store(sk, TCP_CLOSING);
613 inet_sk_state_store(sk, TCP_CLOSE);
616 /* Other states not expected */
622 if (!__mptcp_check_fallback(msk))
624 mptcp_close_wake_up(sk);
629 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
633 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
634 struct sock *sk = (struct sock *)msk;
635 unsigned int moved = 0;
636 bool more_data_avail;
641 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
643 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
644 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
646 if (unlikely(ssk_rbuf > sk_rbuf)) {
647 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
652 pr_debug("msk=%p ssk=%p", msk, ssk);
655 u32 map_remaining, offset;
656 u32 seq = tp->copied_seq;
660 /* try to move as much data as available */
661 map_remaining = subflow->map_data_len -
662 mptcp_subflow_get_map_offset(subflow);
664 skb = skb_peek(&ssk->sk_receive_queue);
666 /* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
667 * a different CPU can have already processed the pending
668 * data, stop here or we can enter an infinite loop
675 if (__mptcp_check_fallback(msk)) {
676 /* Under fallback skbs have no MPTCP extension and TCP could
677 * collapse them between the dummy map creation and the
678 * current dequeue. Be sure to adjust the map size.
680 map_remaining = skb->len;
681 subflow->map_data_len = skb->len;
684 offset = seq - TCP_SKB_CB(skb)->seq;
685 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
691 if (offset < skb->len) {
692 size_t len = skb->len - offset;
697 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
701 if (WARN_ON_ONCE(map_remaining < len))
705 sk_eat_skb(ssk, skb);
709 WRITE_ONCE(tp->copied_seq, seq);
710 more_data_avail = mptcp_subflow_data_available(ssk);
712 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
716 } while (more_data_avail);
722 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
724 struct sock *sk = (struct sock *)msk;
725 struct sk_buff *skb, *tail;
730 p = rb_first(&msk->out_of_order_queue);
731 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
734 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
738 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
740 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
743 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
747 end_seq = MPTCP_SKB_CB(skb)->end_seq;
748 tail = skb_peek_tail(&sk->sk_receive_queue);
749 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
750 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
752 /* skip overlapping data, if any */
753 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
754 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
756 MPTCP_SKB_CB(skb)->offset += delta;
757 MPTCP_SKB_CB(skb)->map_seq += delta;
758 __skb_queue_tail(&sk->sk_receive_queue, skb);
760 msk->bytes_received += end_seq - msk->ack_seq;
761 msk->ack_seq = end_seq;
767 /* In most cases we will be able to lock the mptcp socket. If its already
768 * owned, we need to defer to the work queue to avoid ABBA deadlock.
770 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
772 struct sock *sk = (struct sock *)msk;
773 unsigned int moved = 0;
775 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
776 __mptcp_ofo_queue(msk);
777 if (unlikely(ssk->sk_err)) {
778 if (!sock_owned_by_user(sk))
779 __mptcp_error_report(sk);
781 __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags);
784 /* If the moves have caught up with the DATA_FIN sequence number
785 * it's time to ack the DATA_FIN and change socket state, but
786 * this is not a good place to change state. Let the workqueue
789 if (mptcp_pending_data_fin(sk, NULL))
790 mptcp_schedule_work(sk);
794 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
796 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
797 struct mptcp_sock *msk = mptcp_sk(sk);
798 int sk_rbuf, ssk_rbuf;
800 /* The peer can send data while we are shutting down this
801 * subflow at msk destruction time, but we must avoid enqueuing
802 * more data to the msk receive queue
804 if (unlikely(subflow->disposable))
807 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
808 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
809 if (unlikely(ssk_rbuf > sk_rbuf))
812 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
813 if (__mptcp_rmem(sk) > sk_rbuf) {
814 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
818 /* Wake-up the reader only for in-sequence data */
820 if (move_skbs_to_msk(msk, ssk))
821 sk->sk_data_ready(sk);
823 mptcp_data_unlock(sk);
826 static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
828 mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
829 WRITE_ONCE(msk->allow_infinite_fallback, false);
830 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
833 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
835 struct sock *sk = (struct sock *)msk;
837 if (sk->sk_state != TCP_ESTABLISHED)
840 /* attach to msk socket only after we are sure we will deal with it
843 if (sk->sk_socket && !ssk->sk_socket)
844 mptcp_sock_graft(ssk, sk->sk_socket);
846 mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
847 mptcp_sockopt_sync_locked(msk, ssk);
848 mptcp_subflow_joined(msk, ssk);
852 static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
854 struct mptcp_subflow_context *tmp, *subflow;
855 struct mptcp_sock *msk = mptcp_sk(sk);
857 list_for_each_entry_safe(subflow, tmp, join_list, node) {
858 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
859 bool slow = lock_sock_fast(ssk);
861 list_move_tail(&subflow->node, &msk->conn_list);
862 if (!__mptcp_finish_join(msk, ssk))
863 mptcp_subflow_reset(ssk);
864 unlock_sock_fast(ssk, slow);
868 static bool mptcp_timer_pending(struct sock *sk)
870 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
873 static void mptcp_reset_timer(struct sock *sk)
875 struct inet_connection_sock *icsk = inet_csk(sk);
878 /* prevent rescheduling on close */
879 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
882 tout = mptcp_sk(sk)->timer_ival;
883 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
886 bool mptcp_schedule_work(struct sock *sk)
888 if (inet_sk_state_load(sk) != TCP_CLOSE &&
889 schedule_work(&mptcp_sk(sk)->work)) {
890 /* each subflow already holds a reference to the sk, and the
891 * workqueue is invoked by a subflow, so sk can't go away here.
899 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
901 struct mptcp_subflow_context *subflow;
903 msk_owned_by_me(msk);
905 mptcp_for_each_subflow(msk, subflow) {
906 if (READ_ONCE(subflow->data_avail))
907 return mptcp_subflow_tcp_sock(subflow);
913 static bool mptcp_skb_can_collapse_to(u64 write_seq,
914 const struct sk_buff *skb,
915 const struct mptcp_ext *mpext)
917 if (!tcp_skb_can_collapse_to(skb))
920 /* can collapse only if MPTCP level sequence is in order and this
921 * mapping has not been xmitted yet
923 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
927 /* we can append data to the given data frag if:
928 * - there is space available in the backing page_frag
929 * - the data frag tail matches the current page_frag free offset
930 * - the data frag end sequence number matches the current write seq
932 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
933 const struct page_frag *pfrag,
934 const struct mptcp_data_frag *df)
936 return df && pfrag->page == df->page &&
937 pfrag->size - pfrag->offset > 0 &&
938 pfrag->offset == (df->offset + df->data_len) &&
939 df->data_seq + df->data_len == msk->write_seq;
942 static void dfrag_uncharge(struct sock *sk, int len)
944 sk_mem_uncharge(sk, len);
945 sk_wmem_queued_add(sk, -len);
948 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
950 int len = dfrag->data_len + dfrag->overhead;
952 list_del(&dfrag->list);
953 dfrag_uncharge(sk, len);
954 put_page(dfrag->page);
957 static void __mptcp_clean_una(struct sock *sk)
959 struct mptcp_sock *msk = mptcp_sk(sk);
960 struct mptcp_data_frag *dtmp, *dfrag;
963 snd_una = msk->snd_una;
964 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
965 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
968 if (unlikely(dfrag == msk->first_pending)) {
969 /* in recovery mode can see ack after the current snd head */
970 if (WARN_ON_ONCE(!msk->recovery))
973 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
976 dfrag_clear(sk, dfrag);
979 dfrag = mptcp_rtx_head(sk);
980 if (dfrag && after64(snd_una, dfrag->data_seq)) {
981 u64 delta = snd_una - dfrag->data_seq;
983 /* prevent wrap around in recovery mode */
984 if (unlikely(delta > dfrag->already_sent)) {
985 if (WARN_ON_ONCE(!msk->recovery))
987 if (WARN_ON_ONCE(delta > dfrag->data_len))
989 dfrag->already_sent += delta - dfrag->already_sent;
992 dfrag->data_seq += delta;
993 dfrag->offset += delta;
994 dfrag->data_len -= delta;
995 dfrag->already_sent -= delta;
997 dfrag_uncharge(sk, delta);
1000 /* all retransmitted data acked, recovery completed */
1001 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1002 msk->recovery = false;
1005 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1006 snd_una == READ_ONCE(msk->write_seq)) {
1007 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1008 mptcp_stop_timer(sk);
1010 mptcp_reset_timer(sk);
1014 static void __mptcp_clean_una_wakeup(struct sock *sk)
1016 lockdep_assert_held_once(&sk->sk_lock.slock);
1018 __mptcp_clean_una(sk);
1019 mptcp_write_space(sk);
1022 static void mptcp_clean_una_wakeup(struct sock *sk)
1024 mptcp_data_lock(sk);
1025 __mptcp_clean_una_wakeup(sk);
1026 mptcp_data_unlock(sk);
1029 static void mptcp_enter_memory_pressure(struct sock *sk)
1031 struct mptcp_subflow_context *subflow;
1032 struct mptcp_sock *msk = mptcp_sk(sk);
1035 sk_stream_moderate_sndbuf(sk);
1036 mptcp_for_each_subflow(msk, subflow) {
1037 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1040 tcp_enter_memory_pressure(ssk);
1041 sk_stream_moderate_sndbuf(ssk);
1046 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1049 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1051 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1052 pfrag, sk->sk_allocation)))
1055 mptcp_enter_memory_pressure(sk);
1059 static struct mptcp_data_frag *
1060 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1063 int offset = ALIGN(orig_offset, sizeof(long));
1064 struct mptcp_data_frag *dfrag;
1066 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1067 dfrag->data_len = 0;
1068 dfrag->data_seq = msk->write_seq;
1069 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1070 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1071 dfrag->already_sent = 0;
1072 dfrag->page = pfrag->page;
1077 struct mptcp_sendmsg_info {
1083 bool data_lock_held;
1086 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1087 u64 data_seq, int avail_size)
1089 u64 window_end = mptcp_wnd_end(msk);
1092 if (__mptcp_check_fallback(msk))
1095 mptcp_snd_wnd = window_end - data_seq;
1096 avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1098 if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1099 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1100 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1106 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1108 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1112 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1116 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1118 struct sk_buff *skb;
1120 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1122 if (likely(__mptcp_add_ext(skb, gfp))) {
1123 skb_reserve(skb, MAX_TCP_HEADER);
1124 skb->ip_summed = CHECKSUM_PARTIAL;
1125 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1130 mptcp_enter_memory_pressure(sk);
1135 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1137 struct sk_buff *skb;
1139 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1143 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1144 tcp_skb_entail(ssk, skb);
1147 tcp_skb_tsorted_anchor_cleanup(skb);
1152 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1154 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1156 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1159 /* note: this always recompute the csum on the whole skb, even
1160 * if we just appended a single frag. More status info needed
1162 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1164 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1165 __wsum csum = ~csum_unfold(mpext->csum);
1166 int offset = skb->len - added;
1168 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1171 static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1173 struct mptcp_ext *mpext)
1178 mpext->infinite_map = 1;
1179 mpext->data_len = 0;
1181 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1182 mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1184 mptcp_do_fallback(ssk);
1187 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1188 struct mptcp_data_frag *dfrag,
1189 struct mptcp_sendmsg_info *info)
1191 u64 data_seq = dfrag->data_seq + info->sent;
1192 int offset = dfrag->offset + info->sent;
1193 struct mptcp_sock *msk = mptcp_sk(sk);
1194 bool zero_window_probe = false;
1195 struct mptcp_ext *mpext = NULL;
1196 bool can_coalesce = false;
1197 bool reuse_skb = true;
1198 struct sk_buff *skb;
1202 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1203 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1205 if (WARN_ON_ONCE(info->sent > info->limit ||
1206 info->limit > dfrag->data_len))
1209 if (unlikely(!__tcp_can_send(ssk)))
1212 /* compute send limit */
1213 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1214 copy = info->size_goal;
1216 skb = tcp_write_queue_tail(ssk);
1217 if (skb && copy > skb->len) {
1218 /* Limit the write to the size available in the
1219 * current skb, if any, so that we create at most a new skb.
1220 * Explicitly tells TCP internals to avoid collapsing on later
1221 * queue management operation, to avoid breaking the ext <->
1222 * SSN association set here
1224 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1225 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1226 TCP_SKB_CB(skb)->eor = 1;
1230 i = skb_shinfo(skb)->nr_frags;
1231 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1232 if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
1233 tcp_mark_push(tcp_sk(ssk), skb);
1240 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1244 i = skb_shinfo(skb)->nr_frags;
1246 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1249 /* Zero window and all data acked? Probe. */
1250 copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1252 u64 snd_una = READ_ONCE(msk->snd_una);
1254 if (snd_una != msk->snd_nxt) {
1255 tcp_remove_empty_skb(ssk);
1259 zero_window_probe = true;
1260 data_seq = snd_una - 1;
1263 /* all mptcp-level data is acked, no skbs should be present into the
1266 WARN_ON_ONCE(reuse_skb);
1269 copy = min_t(size_t, copy, info->limit - info->sent);
1270 if (!sk_wmem_schedule(ssk, copy)) {
1271 tcp_remove_empty_skb(ssk);
1276 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1278 get_page(dfrag->page);
1279 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1283 skb->data_len += copy;
1284 skb->truesize += copy;
1285 sk_wmem_queued_add(ssk, copy);
1286 sk_mem_charge(ssk, copy);
1287 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1288 TCP_SKB_CB(skb)->end_seq += copy;
1289 tcp_skb_pcount_set(skb, 0);
1291 /* on skb reuse we just need to update the DSS len */
1293 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1294 mpext->data_len += copy;
1295 WARN_ON_ONCE(zero_window_probe);
1299 memset(mpext, 0, sizeof(*mpext));
1300 mpext->data_seq = data_seq;
1301 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1302 mpext->data_len = copy;
1306 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1307 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1310 if (zero_window_probe) {
1311 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1313 if (READ_ONCE(msk->csum_enabled))
1314 mptcp_update_data_checksum(skb, copy);
1315 tcp_push_pending_frames(ssk);
1319 if (READ_ONCE(msk->csum_enabled))
1320 mptcp_update_data_checksum(skb, copy);
1321 if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1322 mptcp_update_infinite_map(msk, ssk, mpext);
1323 trace_mptcp_sendmsg_frag(mpext);
1324 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1328 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1329 sizeof(struct tcphdr) - \
1330 MAX_TCP_OPTION_SPACE - \
1331 sizeof(struct ipv6hdr) - \
1332 sizeof(struct frag_hdr))
1334 struct subflow_send_info {
1339 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1341 if (!subflow->stale)
1345 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1348 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1350 if (unlikely(subflow->stale)) {
1351 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1353 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1356 mptcp_subflow_set_active(subflow);
1358 return __mptcp_subflow_active(subflow);
1361 #define SSK_MODE_ACTIVE 0
1362 #define SSK_MODE_BACKUP 1
1363 #define SSK_MODE_MAX 2
1365 /* implement the mptcp packet scheduler;
1366 * returns the subflow that will transmit the next DSS
1367 * additionally updates the rtx timeout
1369 struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1371 struct subflow_send_info send_info[SSK_MODE_MAX];
1372 struct mptcp_subflow_context *subflow;
1373 struct sock *sk = (struct sock *)msk;
1374 u32 pace, burst, wmem;
1375 int i, nr_active = 0;
1380 /* pick the subflow with the lower wmem/wspace ratio */
1381 for (i = 0; i < SSK_MODE_MAX; ++i) {
1382 send_info[i].ssk = NULL;
1383 send_info[i].linger_time = -1;
1386 mptcp_for_each_subflow(msk, subflow) {
1387 trace_mptcp_subflow_get_send(subflow);
1388 ssk = mptcp_subflow_tcp_sock(subflow);
1389 if (!mptcp_subflow_active(subflow))
1392 tout = max(tout, mptcp_timeout_from_subflow(subflow));
1393 nr_active += !subflow->backup;
1394 pace = subflow->avg_pacing_rate;
1395 if (unlikely(!pace)) {
1396 /* init pacing rate from socket */
1397 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1398 pace = subflow->avg_pacing_rate;
1403 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1404 if (linger_time < send_info[subflow->backup].linger_time) {
1405 send_info[subflow->backup].ssk = ssk;
1406 send_info[subflow->backup].linger_time = linger_time;
1409 __mptcp_set_timeout(sk, tout);
1411 /* pick the best backup if no other subflow is active */
1413 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1415 /* According to the blest algorithm, to avoid HoL blocking for the
1416 * faster flow, we need to:
1417 * - estimate the faster flow linger time
1418 * - use the above to estimate the amount of byte transferred
1419 * by the faster flow
1420 * - check that the amount of queued data is greter than the above,
1421 * otherwise do not use the picked, slower, subflow
1422 * We select the subflow with the shorter estimated time to flush
1423 * the queued mem, which basically ensure the above. We just need
1424 * to check that subflow has a non empty cwin.
1426 ssk = send_info[SSK_MODE_ACTIVE].ssk;
1427 if (!ssk || !sk_stream_memory_free(ssk))
1430 burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1431 wmem = READ_ONCE(ssk->sk_wmem_queued);
1435 subflow = mptcp_subflow_ctx(ssk);
1436 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1437 READ_ONCE(ssk->sk_pacing_rate) * burst,
1439 msk->snd_burst = burst;
1443 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1445 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1449 static void mptcp_update_post_push(struct mptcp_sock *msk,
1450 struct mptcp_data_frag *dfrag,
1453 u64 snd_nxt_new = dfrag->data_seq;
1455 dfrag->already_sent += sent;
1457 msk->snd_burst -= sent;
1459 snd_nxt_new += dfrag->already_sent;
1461 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1462 * is recovering after a failover. In that event, this re-sends
1465 * Thus compute snd_nxt_new candidate based on
1466 * the dfrag->data_seq that was sent and the data
1467 * that has been handed to the subflow for transmission
1468 * and skip update in case it was old dfrag.
1470 if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
1471 msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
1472 msk->snd_nxt = snd_nxt_new;
1476 void mptcp_check_and_set_pending(struct sock *sk)
1478 if (mptcp_send_head(sk))
1479 mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING);
1482 static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
1483 struct mptcp_sendmsg_info *info)
1485 struct mptcp_sock *msk = mptcp_sk(sk);
1486 struct mptcp_data_frag *dfrag;
1487 int len, copied = 0, err = 0;
1489 while ((dfrag = mptcp_send_head(sk))) {
1490 info->sent = dfrag->already_sent;
1491 info->limit = dfrag->data_len;
1492 len = dfrag->data_len - dfrag->already_sent;
1496 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
1498 err = copied ? : ret;
1506 mptcp_update_post_push(msk, dfrag, ret);
1508 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1510 if (msk->snd_burst <= 0 ||
1511 !sk_stream_memory_free(ssk) ||
1512 !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
1516 mptcp_set_timeout(sk);
1524 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1526 struct sock *prev_ssk = NULL, *ssk = NULL;
1527 struct mptcp_sock *msk = mptcp_sk(sk);
1528 struct mptcp_sendmsg_info info = {
1531 bool do_check_data_fin = false;
1534 while (mptcp_send_head(sk) && (push_count > 0)) {
1535 struct mptcp_subflow_context *subflow;
1538 if (mptcp_sched_get_send(msk))
1543 mptcp_for_each_subflow(msk, subflow) {
1544 if (READ_ONCE(subflow->scheduled)) {
1545 mptcp_subflow_set_scheduled(subflow, false);
1548 ssk = mptcp_subflow_tcp_sock(subflow);
1549 if (ssk != prev_ssk) {
1550 /* First check. If the ssk has changed since
1551 * the last round, release prev_ssk
1554 mptcp_push_release(prev_ssk, &info);
1556 /* Need to lock the new subflow only if different
1557 * from the previous one, otherwise we are still
1558 * helding the relevant lock
1565 ret = __subflow_push_pending(sk, ssk, &info);
1567 if (ret != -EAGAIN ||
1568 (1 << ssk->sk_state) &
1569 (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
1573 do_check_data_fin = true;
1578 /* at this point we held the socket lock for the last subflow we used */
1580 mptcp_push_release(ssk, &info);
1582 /* ensure the rtx timer is running */
1583 if (!mptcp_timer_pending(sk))
1584 mptcp_reset_timer(sk);
1585 if (do_check_data_fin)
1586 mptcp_check_send_data_fin(sk);
1589 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
1591 struct mptcp_sock *msk = mptcp_sk(sk);
1592 struct mptcp_sendmsg_info info = {
1593 .data_lock_held = true,
1595 bool keep_pushing = true;
1596 struct sock *xmit_ssk;
1600 while (mptcp_send_head(sk) && keep_pushing) {
1601 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1604 /* check for a different subflow usage only after
1605 * spooling the first chunk of data
1608 mptcp_subflow_set_scheduled(subflow, false);
1609 ret = __subflow_push_pending(sk, ssk, &info);
1617 if (mptcp_sched_get_send(msk))
1620 if (READ_ONCE(subflow->scheduled)) {
1621 mptcp_subflow_set_scheduled(subflow, false);
1622 ret = __subflow_push_pending(sk, ssk, &info);
1624 keep_pushing = false;
1628 mptcp_for_each_subflow(msk, subflow) {
1629 if (READ_ONCE(subflow->scheduled)) {
1630 xmit_ssk = mptcp_subflow_tcp_sock(subflow);
1631 if (xmit_ssk != ssk) {
1632 mptcp_subflow_delegate(subflow,
1633 MPTCP_DELEGATE_SEND);
1634 keep_pushing = false;
1641 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1642 * not going to flush it via release_sock()
1645 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1647 if (!mptcp_timer_pending(sk))
1648 mptcp_reset_timer(sk);
1650 if (msk->snd_data_fin_enable &&
1651 msk->snd_nxt + 1 == msk->write_seq)
1652 mptcp_schedule_work(sk);
1656 static void mptcp_set_nospace(struct sock *sk)
1658 /* enable autotune */
1659 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1661 /* will be cleared on avail space */
1662 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1665 static int mptcp_disconnect(struct sock *sk, int flags);
1667 static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1668 size_t len, int *copied_syn)
1670 unsigned int saved_flags = msg->msg_flags;
1671 struct mptcp_sock *msk = mptcp_sk(sk);
1675 /* on flags based fastopen the mptcp is supposed to create the
1676 * first subflow right now. Otherwise we are in the defer_connect
1677 * path, and the first subflow must be already present.
1678 * Since the defer_connect flag is cleared after the first succsful
1679 * fastopen attempt, no need to check for additional subflow status.
1681 if (msg->msg_flags & MSG_FASTOPEN) {
1682 ssk = __mptcp_nmpc_sk(msk);
1684 return PTR_ERR(ssk);
1692 msg->msg_flags |= MSG_DONTWAIT;
1693 msk->fastopening = 1;
1694 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1695 msk->fastopening = 0;
1696 msg->msg_flags = saved_flags;
1699 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1700 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1701 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1702 msg->msg_namelen, msg->msg_flags, 1);
1704 /* Keep the same behaviour of plain TCP: zero the copied bytes in
1705 * case of any error, except timeout or signal
1707 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1709 } else if (ret && ret != -EINPROGRESS) {
1710 /* The disconnect() op called by tcp_sendmsg_fastopen()/
1711 * __inet_stream_connect() can fail, due to looking check,
1712 * see mptcp_disconnect().
1713 * Attempt it again outside the problematic scope.
1715 if (!mptcp_disconnect(sk, 0))
1716 sk->sk_socket->state = SS_UNCONNECTED;
1718 inet_clear_bit(DEFER_CONNECT, sk);
1723 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1725 struct mptcp_sock *msk = mptcp_sk(sk);
1726 struct page_frag *pfrag;
1731 /* silently ignore everything else */
1732 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1736 if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
1737 msg->msg_flags & MSG_FASTOPEN)) {
1740 ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1741 copied += copied_syn;
1742 if (ret == -EINPROGRESS && copied_syn > 0)
1748 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1750 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1751 ret = sk_stream_wait_connect(sk, &timeo);
1757 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1760 pfrag = sk_page_frag(sk);
1762 while (msg_data_left(msg)) {
1763 int total_ts, frag_truesize = 0;
1764 struct mptcp_data_frag *dfrag;
1765 bool dfrag_collapsed;
1766 size_t psize, offset;
1768 /* reuse tail pfrag, if possible, or carve a new one from the
1771 dfrag = mptcp_pending_tail(sk);
1772 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1773 if (!dfrag_collapsed) {
1774 if (!sk_stream_memory_free(sk))
1775 goto wait_for_memory;
1777 if (!mptcp_page_frag_refill(sk, pfrag))
1778 goto wait_for_memory;
1780 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1781 frag_truesize = dfrag->overhead;
1784 /* we do not bound vs wspace, to allow a single packet.
1785 * memory accounting will prevent execessive memory usage
1788 offset = dfrag->offset + dfrag->data_len;
1789 psize = pfrag->size - offset;
1790 psize = min_t(size_t, psize, msg_data_left(msg));
1791 total_ts = psize + frag_truesize;
1793 if (!sk_wmem_schedule(sk, total_ts))
1794 goto wait_for_memory;
1796 if (copy_page_from_iter(dfrag->page, offset, psize,
1797 &msg->msg_iter) != psize) {
1802 /* data successfully copied into the write queue */
1803 sk->sk_forward_alloc -= total_ts;
1805 dfrag->data_len += psize;
1806 frag_truesize += psize;
1807 pfrag->offset += frag_truesize;
1808 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1810 /* charge data on mptcp pending queue to the msk socket
1811 * Note: we charge such data both to sk and ssk
1813 sk_wmem_queued_add(sk, frag_truesize);
1814 if (!dfrag_collapsed) {
1815 get_page(dfrag->page);
1816 list_add_tail(&dfrag->list, &msk->rtx_queue);
1817 if (!msk->first_pending)
1818 WRITE_ONCE(msk->first_pending, dfrag);
1820 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1821 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1827 mptcp_set_nospace(sk);
1828 __mptcp_push_pending(sk, msg->msg_flags);
1829 ret = sk_stream_wait_memory(sk, &timeo);
1835 __mptcp_push_pending(sk, msg->msg_flags);
1845 copied = sk_stream_error(sk, msg->msg_flags, ret);
1849 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1851 size_t len, int flags,
1852 struct scm_timestamping_internal *tss,
1855 struct sk_buff *skb, *tmp;
1858 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1859 u32 offset = MPTCP_SKB_CB(skb)->offset;
1860 u32 data_len = skb->len - offset;
1861 u32 count = min_t(size_t, len - copied, data_len);
1864 if (!(flags & MSG_TRUNC)) {
1865 err = skb_copy_datagram_msg(skb, offset, msg, count);
1866 if (unlikely(err < 0)) {
1873 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1874 tcp_update_recv_tstamps(skb, tss);
1875 *cmsg_flags |= MPTCP_CMSG_TS;
1880 if (count < data_len) {
1881 if (!(flags & MSG_PEEK)) {
1882 MPTCP_SKB_CB(skb)->offset += count;
1883 MPTCP_SKB_CB(skb)->map_seq += count;
1888 if (!(flags & MSG_PEEK)) {
1889 /* we will bulk release the skb memory later */
1890 skb->destructor = NULL;
1891 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1892 __skb_unlink(skb, &msk->receive_queue);
1903 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1905 * Only difference: Use highest rtt estimate of the subflows in use.
1907 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1909 struct mptcp_subflow_context *subflow;
1910 struct sock *sk = (struct sock *)msk;
1911 u8 scaling_ratio = U8_MAX;
1912 u32 time, advmss = 1;
1915 msk_owned_by_me(msk);
1920 msk->rcvq_space.copied += copied;
1922 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1923 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1925 rtt_us = msk->rcvq_space.rtt_us;
1926 if (rtt_us && time < (rtt_us >> 3))
1930 mptcp_for_each_subflow(msk, subflow) {
1931 const struct tcp_sock *tp;
1935 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1937 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1938 sf_advmss = READ_ONCE(tp->advmss);
1940 rtt_us = max(sf_rtt_us, rtt_us);
1941 advmss = max(sf_advmss, advmss);
1942 scaling_ratio = min(tp->scaling_ratio, scaling_ratio);
1945 msk->rcvq_space.rtt_us = rtt_us;
1946 msk->scaling_ratio = scaling_ratio;
1947 if (time < (rtt_us >> 3) || rtt_us == 0)
1950 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1953 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
1954 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1958 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1960 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1962 do_div(grow, msk->rcvq_space.space);
1963 rcvwin += (grow << 1);
1965 rcvbuf = min_t(u64, __tcp_space_from_win(scaling_ratio, rcvwin),
1966 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
1968 if (rcvbuf > sk->sk_rcvbuf) {
1971 window_clamp = __tcp_win_from_space(scaling_ratio, rcvbuf);
1972 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1974 /* Make subflows follow along. If we do not do this, we
1975 * get drops at subflow level if skbs can't be moved to
1976 * the mptcp rx queue fast enough (announced rcv_win can
1977 * exceed ssk->sk_rcvbuf).
1979 mptcp_for_each_subflow(msk, subflow) {
1983 ssk = mptcp_subflow_tcp_sock(subflow);
1984 slow = lock_sock_fast(ssk);
1985 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1986 tcp_sk(ssk)->window_clamp = window_clamp;
1987 tcp_cleanup_rbuf(ssk, 1);
1988 unlock_sock_fast(ssk, slow);
1993 msk->rcvq_space.space = msk->rcvq_space.copied;
1995 msk->rcvq_space.copied = 0;
1996 msk->rcvq_space.time = mstamp;
1999 static void __mptcp_update_rmem(struct sock *sk)
2001 struct mptcp_sock *msk = mptcp_sk(sk);
2003 if (!msk->rmem_released)
2006 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
2007 mptcp_rmem_uncharge(sk, msk->rmem_released);
2008 WRITE_ONCE(msk->rmem_released, 0);
2011 static void __mptcp_splice_receive_queue(struct sock *sk)
2013 struct mptcp_sock *msk = mptcp_sk(sk);
2015 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
2018 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
2020 struct sock *sk = (struct sock *)msk;
2021 unsigned int moved = 0;
2025 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
2028 /* we can have data pending in the subflows only if the msk
2029 * receive buffer was full at subflow_data_ready() time,
2030 * that is an unlikely slow path.
2035 slowpath = lock_sock_fast(ssk);
2036 mptcp_data_lock(sk);
2037 __mptcp_update_rmem(sk);
2038 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
2039 mptcp_data_unlock(sk);
2041 if (unlikely(ssk->sk_err))
2042 __mptcp_error_report(sk);
2043 unlock_sock_fast(ssk, slowpath);
2046 /* acquire the data lock only if some input data is pending */
2048 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
2049 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
2050 mptcp_data_lock(sk);
2051 __mptcp_update_rmem(sk);
2052 ret |= __mptcp_ofo_queue(msk);
2053 __mptcp_splice_receive_queue(sk);
2054 mptcp_data_unlock(sk);
2057 mptcp_check_data_fin((struct sock *)msk);
2058 return !skb_queue_empty(&msk->receive_queue);
2061 static unsigned int mptcp_inq_hint(const struct sock *sk)
2063 const struct mptcp_sock *msk = mptcp_sk(sk);
2064 const struct sk_buff *skb;
2066 skb = skb_peek(&msk->receive_queue);
2068 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
2070 if (hint_val >= INT_MAX)
2073 return (unsigned int)hint_val;
2076 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2082 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2083 int flags, int *addr_len)
2085 struct mptcp_sock *msk = mptcp_sk(sk);
2086 struct scm_timestamping_internal tss;
2087 int copied = 0, cmsg_flags = 0;
2091 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2092 if (unlikely(flags & MSG_ERRQUEUE))
2093 return inet_recv_error(sk, msg, len, addr_len);
2096 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2101 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2103 len = min_t(size_t, len, INT_MAX);
2104 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2106 if (unlikely(msk->recvmsg_inq))
2107 cmsg_flags = MPTCP_CMSG_INQ;
2109 while (copied < len) {
2112 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2113 if (unlikely(bytes_read < 0)) {
2115 copied = bytes_read;
2119 copied += bytes_read;
2121 /* be sure to advertise window change */
2122 mptcp_cleanup_rbuf(msk);
2124 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2127 /* only the master socket status is relevant here. The exit
2128 * conditions mirror closely tcp_recvmsg()
2130 if (copied >= target)
2135 sk->sk_state == TCP_CLOSE ||
2136 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2138 signal_pending(current))
2142 copied = sock_error(sk);
2146 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2147 /* race breaker: the shutdown could be after the
2148 * previous receive queue check
2150 if (__mptcp_move_skbs(msk))
2155 if (sk->sk_state == TCP_CLOSE) {
2165 if (signal_pending(current)) {
2166 copied = sock_intr_errno(timeo);
2171 pr_debug("block timeout %ld", timeo);
2172 sk_wait_data(sk, &timeo, NULL);
2176 if (cmsg_flags && copied >= 0) {
2177 if (cmsg_flags & MPTCP_CMSG_TS)
2178 tcp_recv_timestamp(msg, sk, &tss);
2180 if (cmsg_flags & MPTCP_CMSG_INQ) {
2181 unsigned int inq = mptcp_inq_hint(sk);
2183 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2187 pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2188 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2189 skb_queue_empty(&msk->receive_queue), copied);
2190 if (!(flags & MSG_PEEK))
2191 mptcp_rcv_space_adjust(msk, copied);
2197 static void mptcp_retransmit_timer(struct timer_list *t)
2199 struct inet_connection_sock *icsk = from_timer(icsk, t,
2200 icsk_retransmit_timer);
2201 struct sock *sk = &icsk->icsk_inet.sk;
2202 struct mptcp_sock *msk = mptcp_sk(sk);
2205 if (!sock_owned_by_user(sk)) {
2206 /* we need a process context to retransmit */
2207 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2208 mptcp_schedule_work(sk);
2210 /* delegate our work to tcp_release_cb() */
2211 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2217 static void mptcp_timeout_timer(struct timer_list *t)
2219 struct sock *sk = from_timer(sk, t, sk_timer);
2221 mptcp_schedule_work(sk);
2225 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2228 * A backup subflow is returned only if that is the only kind available.
2230 struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2232 struct sock *backup = NULL, *pick = NULL;
2233 struct mptcp_subflow_context *subflow;
2234 int min_stale_count = INT_MAX;
2236 mptcp_for_each_subflow(msk, subflow) {
2237 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2239 if (!__mptcp_subflow_active(subflow))
2242 /* still data outstanding at TCP level? skip this */
2243 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2244 mptcp_pm_subflow_chk_stale(msk, ssk);
2245 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2249 if (subflow->backup) {
2262 /* use backup only if there are no progresses anywhere */
2263 return min_stale_count > 1 ? backup : NULL;
2266 bool __mptcp_retransmit_pending_data(struct sock *sk)
2268 struct mptcp_data_frag *cur, *rtx_head;
2269 struct mptcp_sock *msk = mptcp_sk(sk);
2271 if (__mptcp_check_fallback(msk))
2274 if (tcp_rtx_and_write_queues_empty(sk))
2277 /* the closing socket has some data untransmitted and/or unacked:
2278 * some data in the mptcp rtx queue has not really xmitted yet.
2279 * keep it simple and re-inject the whole mptcp level rtx queue
2281 mptcp_data_lock(sk);
2282 __mptcp_clean_una_wakeup(sk);
2283 rtx_head = mptcp_rtx_head(sk);
2285 mptcp_data_unlock(sk);
2289 msk->recovery_snd_nxt = msk->snd_nxt;
2290 msk->recovery = true;
2291 mptcp_data_unlock(sk);
2293 msk->first_pending = rtx_head;
2296 /* be sure to clear the "sent status" on all re-injected fragments */
2297 list_for_each_entry(cur, &msk->rtx_queue, list) {
2298 if (!cur->already_sent)
2300 cur->already_sent = 0;
2306 /* flags for __mptcp_close_ssk() */
2307 #define MPTCP_CF_PUSH BIT(1)
2308 #define MPTCP_CF_FASTCLOSE BIT(2)
2310 /* subflow sockets can be either outgoing (connect) or incoming
2313 * Outgoing subflows use in-kernel sockets.
2314 * Incoming subflows do not have their own 'struct socket' allocated,
2315 * so we need to use tcp_close() after detaching them from the mptcp
2318 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2319 struct mptcp_subflow_context *subflow,
2322 struct mptcp_sock *msk = mptcp_sk(sk);
2323 bool dispose_it, need_push = false;
2325 /* If the first subflow moved to a close state before accept, e.g. due
2326 * to an incoming reset, mptcp either:
2327 * - if either the subflow or the msk are dead, destroy the context
2328 * (the subflow socket is deleted by inet_child_forget) and the msk
2329 * - otherwise do nothing at the moment and take action at accept and/or
2330 * listener shutdown - user-space must be able to accept() the closed
2333 if (msk->in_accept_queue && msk->first == ssk) {
2334 if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD))
2337 /* ensure later check in mptcp_worker() will dispose the msk */
2338 sock_set_flag(sk, SOCK_DEAD);
2339 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2340 mptcp_subflow_drop_ctx(ssk);
2344 dispose_it = msk->free_first || ssk != msk->first;
2346 list_del(&subflow->node);
2348 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2350 if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) {
2351 /* be sure to force the tcp_disconnect() path,
2352 * to generate the egress reset
2354 ssk->sk_lingertime = 0;
2355 sock_set_flag(ssk, SOCK_LINGER);
2356 subflow->send_fastclose = 1;
2359 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2361 /* The MPTCP code never wait on the subflow sockets, TCP-level
2362 * disconnect should never fail
2364 WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2365 mptcp_subflow_ctx_reset(subflow);
2371 subflow->disposable = 1;
2373 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2374 * the ssk has been already destroyed, we just need to release the
2375 * reference owned by msk;
2377 if (!inet_csk(ssk)->icsk_ulp_ops) {
2378 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2379 kfree_rcu(subflow, rcu);
2381 /* otherwise tcp will dispose of the ssk and subflow ctx */
2382 __tcp_close(ssk, 0);
2384 /* close acquired an extra ref */
2393 if (ssk == msk->first)
2394 WRITE_ONCE(msk->first, NULL);
2398 __mptcp_push_pending(sk, 0);
2401 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2402 struct mptcp_subflow_context *subflow)
2404 if (sk->sk_state == TCP_ESTABLISHED)
2405 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2407 /* subflow aborted before reaching the fully_established status
2408 * attempt the creation of the next subflow
2410 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2412 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2415 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2420 static void __mptcp_close_subflow(struct sock *sk)
2422 struct mptcp_subflow_context *subflow, *tmp;
2423 struct mptcp_sock *msk = mptcp_sk(sk);
2427 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2428 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2430 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2433 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2434 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2437 mptcp_close_ssk(sk, ssk, subflow);
2442 static bool mptcp_should_close(const struct sock *sk)
2444 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2445 struct mptcp_subflow_context *subflow;
2447 if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue)
2450 /* if all subflows are in closed status don't bother with additional
2453 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2454 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2461 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2463 struct mptcp_subflow_context *subflow, *tmp;
2464 struct sock *sk = (struct sock *)msk;
2466 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2469 mptcp_token_destroy(msk);
2471 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2472 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2475 slow = lock_sock_fast(tcp_sk);
2476 if (tcp_sk->sk_state != TCP_CLOSE) {
2477 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2478 tcp_set_state(tcp_sk, TCP_CLOSE);
2480 unlock_sock_fast(tcp_sk, slow);
2483 /* Mirror the tcp_reset() error propagation */
2484 switch (sk->sk_state) {
2486 WRITE_ONCE(sk->sk_err, ECONNREFUSED);
2488 case TCP_CLOSE_WAIT:
2489 WRITE_ONCE(sk->sk_err, EPIPE);
2494 WRITE_ONCE(sk->sk_err, ECONNRESET);
2497 inet_sk_state_store(sk, TCP_CLOSE);
2498 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2499 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2500 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2502 /* the calling mptcp_worker will properly destroy the socket */
2503 if (sock_flag(sk, SOCK_DEAD))
2506 sk->sk_state_change(sk);
2507 sk_error_report(sk);
2510 static void __mptcp_retrans(struct sock *sk)
2512 struct mptcp_sock *msk = mptcp_sk(sk);
2513 struct mptcp_subflow_context *subflow;
2514 struct mptcp_sendmsg_info info = {};
2515 struct mptcp_data_frag *dfrag;
2520 mptcp_clean_una_wakeup(sk);
2522 /* first check ssk: need to kick "stale" logic */
2523 err = mptcp_sched_get_retrans(msk);
2524 dfrag = mptcp_rtx_head(sk);
2526 if (mptcp_data_fin_enabled(msk)) {
2527 struct inet_connection_sock *icsk = inet_csk(sk);
2529 icsk->icsk_retransmits++;
2530 mptcp_set_datafin_timeout(sk);
2531 mptcp_send_ack(msk);
2536 if (!mptcp_send_head(sk))
2545 mptcp_for_each_subflow(msk, subflow) {
2546 if (READ_ONCE(subflow->scheduled)) {
2549 mptcp_subflow_set_scheduled(subflow, false);
2551 ssk = mptcp_subflow_tcp_sock(subflow);
2555 /* limit retransmission to the bytes already sent on some subflows */
2557 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
2558 dfrag->already_sent;
2559 while (info.sent < info.limit) {
2560 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2564 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2569 len = max(copied, len);
2570 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2572 WRITE_ONCE(msk->allow_infinite_fallback, false);
2579 msk->bytes_retrans += len;
2580 dfrag->already_sent = max(dfrag->already_sent, len);
2583 mptcp_check_and_set_pending(sk);
2585 if (!mptcp_timer_pending(sk))
2586 mptcp_reset_timer(sk);
2589 /* schedule the timeout timer for the relevant event: either close timeout
2590 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2592 void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout)
2594 struct sock *sk = (struct sock *)msk;
2595 unsigned long timeout, close_timeout;
2597 if (!fail_tout && !sock_flag(sk, SOCK_DEAD))
2600 close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN;
2602 /* the close timeout takes precedence on the fail one, and here at least one of
2605 timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout;
2607 sk_reset_timer(sk, &sk->sk_timer, timeout);
2610 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2612 struct sock *ssk = msk->first;
2618 pr_debug("MP_FAIL doesn't respond, reset the subflow");
2620 slow = lock_sock_fast(ssk);
2621 mptcp_subflow_reset(ssk);
2622 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2623 unlock_sock_fast(ssk, slow);
2625 mptcp_reset_timeout(msk, 0);
2628 static void mptcp_do_fastclose(struct sock *sk)
2630 struct mptcp_subflow_context *subflow, *tmp;
2631 struct mptcp_sock *msk = mptcp_sk(sk);
2633 inet_sk_state_store(sk, TCP_CLOSE);
2634 mptcp_for_each_subflow_safe(msk, subflow, tmp)
2635 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2636 subflow, MPTCP_CF_FASTCLOSE);
2639 static void mptcp_worker(struct work_struct *work)
2641 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2642 struct sock *sk = (struct sock *)msk;
2643 unsigned long fail_tout;
2647 state = sk->sk_state;
2648 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2651 mptcp_check_fastclose(msk);
2653 mptcp_pm_nl_work(msk);
2655 mptcp_check_send_data_fin(sk);
2656 mptcp_check_data_fin_ack(sk);
2657 mptcp_check_data_fin(sk);
2659 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2660 __mptcp_close_subflow(sk);
2662 /* There is no point in keeping around an orphaned sk timedout or
2663 * closed, but we need the msk around to reply to incoming DATA_FIN,
2664 * even if it is orphaned and in FIN_WAIT2 state
2666 if (sock_flag(sk, SOCK_DEAD)) {
2667 if (mptcp_should_close(sk))
2668 mptcp_do_fastclose(sk);
2670 if (sk->sk_state == TCP_CLOSE) {
2671 __mptcp_destroy_sock(sk);
2676 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2677 __mptcp_retrans(sk);
2679 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2680 if (fail_tout && time_after(jiffies, fail_tout))
2681 mptcp_mp_fail_no_response(msk);
2688 static void __mptcp_init_sock(struct sock *sk)
2690 struct mptcp_sock *msk = mptcp_sk(sk);
2692 INIT_LIST_HEAD(&msk->conn_list);
2693 INIT_LIST_HEAD(&msk->join_list);
2694 INIT_LIST_HEAD(&msk->rtx_queue);
2695 INIT_WORK(&msk->work, mptcp_worker);
2696 __skb_queue_head_init(&msk->receive_queue);
2697 msk->out_of_order_queue = RB_ROOT;
2698 msk->first_pending = NULL;
2699 msk->rmem_fwd_alloc = 0;
2700 WRITE_ONCE(msk->rmem_released, 0);
2701 msk->timer_ival = TCP_RTO_MIN;
2703 WRITE_ONCE(msk->first, NULL);
2704 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2705 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2706 WRITE_ONCE(msk->allow_infinite_fallback, true);
2707 msk->recovery = false;
2708 msk->subflow_id = 1;
2710 mptcp_pm_data_init(msk);
2712 /* re-use the csk retrans timer for MPTCP-level retrans */
2713 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2714 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2717 static void mptcp_ca_reset(struct sock *sk)
2719 struct inet_connection_sock *icsk = inet_csk(sk);
2721 tcp_assign_congestion_control(sk);
2722 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2724 /* no need to keep a reference to the ops, the name will suffice */
2725 tcp_cleanup_congestion_control(sk);
2726 icsk->icsk_ca_ops = NULL;
2729 static int mptcp_init_sock(struct sock *sk)
2731 struct net *net = sock_net(sk);
2734 __mptcp_init_sock(sk);
2736 if (!mptcp_is_enabled(net))
2737 return -ENOPROTOOPT;
2739 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2742 ret = mptcp_init_sched(mptcp_sk(sk),
2743 mptcp_sched_find(mptcp_get_scheduler(net)));
2747 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2749 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2750 * propagate the correct value
2754 sk_sockets_allocated_inc(sk);
2755 sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2756 sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2761 static void __mptcp_clear_xmit(struct sock *sk)
2763 struct mptcp_sock *msk = mptcp_sk(sk);
2764 struct mptcp_data_frag *dtmp, *dfrag;
2766 WRITE_ONCE(msk->first_pending, NULL);
2767 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2768 dfrag_clear(sk, dfrag);
2771 void mptcp_cancel_work(struct sock *sk)
2773 struct mptcp_sock *msk = mptcp_sk(sk);
2775 if (cancel_work_sync(&msk->work))
2779 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2783 switch (ssk->sk_state) {
2785 if (!(how & RCV_SHUTDOWN))
2789 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2792 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2793 pr_debug("Fallback");
2794 ssk->sk_shutdown |= how;
2795 tcp_shutdown(ssk, how);
2797 /* simulate the data_fin ack reception to let the state
2798 * machine move forward
2800 WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2801 mptcp_schedule_work(sk);
2803 pr_debug("Sending DATA_FIN on subflow %p", ssk);
2805 if (!mptcp_timer_pending(sk))
2806 mptcp_reset_timer(sk);
2814 static const unsigned char new_state[16] = {
2815 /* current state: new state: action: */
2816 [0 /* (Invalid) */] = TCP_CLOSE,
2817 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2818 [TCP_SYN_SENT] = TCP_CLOSE,
2819 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2820 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2821 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2822 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2823 [TCP_CLOSE] = TCP_CLOSE,
2824 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2825 [TCP_LAST_ACK] = TCP_LAST_ACK,
2826 [TCP_LISTEN] = TCP_CLOSE,
2827 [TCP_CLOSING] = TCP_CLOSING,
2828 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2831 static int mptcp_close_state(struct sock *sk)
2833 int next = (int)new_state[sk->sk_state];
2834 int ns = next & TCP_STATE_MASK;
2836 inet_sk_state_store(sk, ns);
2838 return next & TCP_ACTION_FIN;
2841 static void mptcp_check_send_data_fin(struct sock *sk)
2843 struct mptcp_subflow_context *subflow;
2844 struct mptcp_sock *msk = mptcp_sk(sk);
2846 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2847 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2848 msk->snd_nxt, msk->write_seq);
2850 /* we still need to enqueue subflows or not really shutting down,
2853 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2854 mptcp_send_head(sk))
2857 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2859 mptcp_for_each_subflow(msk, subflow) {
2860 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2862 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2866 static void __mptcp_wr_shutdown(struct sock *sk)
2868 struct mptcp_sock *msk = mptcp_sk(sk);
2870 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2871 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2872 !!mptcp_send_head(sk));
2874 /* will be ignored by fallback sockets */
2875 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2876 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2878 mptcp_check_send_data_fin(sk);
2881 static void __mptcp_destroy_sock(struct sock *sk)
2883 struct mptcp_sock *msk = mptcp_sk(sk);
2885 pr_debug("msk=%p", msk);
2889 mptcp_stop_timer(sk);
2890 sk_stop_timer(sk, &sk->sk_timer);
2892 mptcp_release_sched(msk);
2894 sk->sk_prot->destroy(sk);
2896 WARN_ON_ONCE(msk->rmem_fwd_alloc);
2897 WARN_ON_ONCE(msk->rmem_released);
2898 sk_stream_kill_queues(sk);
2899 xfrm_sk_free_policy(sk);
2904 void __mptcp_unaccepted_force_close(struct sock *sk)
2906 sock_set_flag(sk, SOCK_DEAD);
2907 mptcp_do_fastclose(sk);
2908 __mptcp_destroy_sock(sk);
2911 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
2913 /* Concurrent splices from sk_receive_queue into receive_queue will
2914 * always show at least one non-empty queue when checked in this order.
2916 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
2917 skb_queue_empty_lockless(&msk->receive_queue))
2920 return EPOLLIN | EPOLLRDNORM;
2923 static void mptcp_check_listen_stop(struct sock *sk)
2927 if (inet_sk_state_load(sk) != TCP_LISTEN)
2930 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
2931 ssk = mptcp_sk(sk)->first;
2932 if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
2935 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2936 tcp_set_state(ssk, TCP_CLOSE);
2937 mptcp_subflow_queue_clean(sk, ssk);
2938 inet_csk_listen_stop(ssk);
2939 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
2943 bool __mptcp_close(struct sock *sk, long timeout)
2945 struct mptcp_subflow_context *subflow;
2946 struct mptcp_sock *msk = mptcp_sk(sk);
2947 bool do_cancel_work = false;
2948 int subflows_alive = 0;
2950 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2952 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2953 mptcp_check_listen_stop(sk);
2954 inet_sk_state_store(sk, TCP_CLOSE);
2958 if (mptcp_check_readable(msk) || timeout < 0) {
2959 /* If the msk has read data, or the caller explicitly ask it,
2960 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
2962 mptcp_do_fastclose(sk);
2964 } else if (mptcp_close_state(sk)) {
2965 __mptcp_wr_shutdown(sk);
2968 sk_stream_wait_close(sk, timeout);
2971 /* orphan all the subflows */
2972 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
2973 mptcp_for_each_subflow(msk, subflow) {
2974 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2975 bool slow = lock_sock_fast_nested(ssk);
2977 subflows_alive += ssk->sk_state != TCP_CLOSE;
2979 /* since the close timeout takes precedence on the fail one,
2982 if (ssk == msk->first)
2983 subflow->fail_tout = 0;
2985 /* detach from the parent socket, but allow data_ready to
2986 * push incoming data into the mptcp stack, to properly ack it
2988 ssk->sk_socket = NULL;
2990 unlock_sock_fast(ssk, slow);
2994 /* all the subflows are closed, only timeout can change the msk
2995 * state, let's not keep resources busy for no reasons
2997 if (subflows_alive == 0)
2998 inet_sk_state_store(sk, TCP_CLOSE);
3001 pr_debug("msk=%p state=%d", sk, sk->sk_state);
3003 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3005 if (sk->sk_state == TCP_CLOSE) {
3006 __mptcp_destroy_sock(sk);
3007 do_cancel_work = true;
3009 mptcp_reset_timeout(msk, 0);
3012 return do_cancel_work;
3015 static void mptcp_close(struct sock *sk, long timeout)
3017 bool do_cancel_work;
3021 do_cancel_work = __mptcp_close(sk, timeout);
3024 mptcp_cancel_work(sk);
3029 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3031 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3032 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3033 struct ipv6_pinfo *msk6 = inet6_sk(msk);
3035 msk->sk_v6_daddr = ssk->sk_v6_daddr;
3036 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3039 msk6->saddr = ssk6->saddr;
3040 msk6->flow_label = ssk6->flow_label;
3044 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3045 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3046 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3047 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3048 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3049 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3052 static int mptcp_disconnect(struct sock *sk, int flags)
3054 struct mptcp_sock *msk = mptcp_sk(sk);
3056 /* Deny disconnect if other threads are blocked in sk_wait_event()
3057 * or inet_wait_for_connect().
3059 if (sk->sk_wait_pending)
3062 /* We are on the fastopen error path. We can't call straight into the
3063 * subflows cleanup code due to lock nesting (we are already under
3064 * msk->firstsocket lock).
3066 if (msk->fastopening)
3069 mptcp_check_listen_stop(sk);
3070 inet_sk_state_store(sk, TCP_CLOSE);
3072 mptcp_stop_timer(sk);
3073 sk_stop_timer(sk, &sk->sk_timer);
3076 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3078 /* msk->subflow is still intact, the following will not free the first
3081 mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3082 WRITE_ONCE(msk->flags, 0);
3084 msk->push_pending = 0;
3085 msk->recovery = false;
3086 msk->can_ack = false;
3087 msk->fully_established = false;
3088 msk->rcv_data_fin = false;
3089 msk->snd_data_fin_enable = false;
3090 msk->rcv_fastclose = false;
3091 msk->use_64bit_ack = false;
3092 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3093 mptcp_pm_data_reset(msk);
3095 msk->bytes_acked = 0;
3096 msk->bytes_received = 0;
3097 msk->bytes_sent = 0;
3098 msk->bytes_retrans = 0;
3100 WRITE_ONCE(sk->sk_shutdown, 0);
3101 sk_error_report(sk);
3105 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3106 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3108 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3110 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3114 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3115 const struct mptcp_options_received *mp_opt,
3117 struct request_sock *req)
3119 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3120 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3121 struct mptcp_sock *msk;
3126 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3127 if (nsk->sk_family == AF_INET6)
3128 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3131 nsk->sk_wait_pending = 0;
3132 __mptcp_init_sock(nsk);
3134 msk = mptcp_sk(nsk);
3135 msk->local_key = subflow_req->local_key;
3136 msk->token = subflow_req->token;
3137 msk->in_accept_queue = 1;
3138 WRITE_ONCE(msk->fully_established, false);
3139 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3140 WRITE_ONCE(msk->csum_enabled, true);
3142 msk->write_seq = subflow_req->idsn + 1;
3143 msk->snd_nxt = msk->write_seq;
3144 msk->snd_una = msk->write_seq;
3145 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
3146 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3147 mptcp_init_sched(msk, mptcp_sk(sk)->sched);
3149 /* passive msk is created after the first/MPC subflow */
3150 msk->subflow_id = 2;
3152 sock_reset_flag(nsk, SOCK_RCU_FREE);
3153 security_inet_csk_clone(nsk, req);
3155 /* this can't race with mptcp_close(), as the msk is
3156 * not yet exposted to user-space
3158 inet_sk_state_store(nsk, TCP_ESTABLISHED);
3160 /* The msk maintain a ref to each subflow in the connections list */
3161 WRITE_ONCE(msk->first, ssk);
3162 list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list);
3165 /* new mpc subflow takes ownership of the newly
3166 * created mptcp socket
3168 mptcp_token_accept(subflow_req, msk);
3170 /* set msk addresses early to ensure mptcp_pm_get_local_id()
3171 * uses the correct data
3173 mptcp_copy_inaddrs(nsk, ssk);
3174 mptcp_propagate_sndbuf(nsk, ssk);
3176 mptcp_rcv_space_init(msk, ssk);
3177 bh_unlock_sock(nsk);
3179 /* note: the newly allocated socket refcount is 2 now */
3183 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3185 const struct tcp_sock *tp = tcp_sk(ssk);
3187 msk->rcvq_space.copied = 0;
3188 msk->rcvq_space.rtt_us = 0;
3190 msk->rcvq_space.time = tp->tcp_mstamp;
3192 /* initial rcv_space offering made to peer */
3193 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3194 TCP_INIT_CWND * tp->advmss);
3195 if (msk->rcvq_space.space == 0)
3196 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3198 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3201 static struct sock *mptcp_accept(struct sock *ssk, int flags, int *err,
3206 pr_debug("ssk=%p, listener=%p", ssk, mptcp_subflow_ctx(ssk));
3207 newsk = inet_csk_accept(ssk, flags, err, kern);
3211 pr_debug("newsk=%p, subflow is mptcp=%d", newsk, sk_is_mptcp(newsk));
3212 if (sk_is_mptcp(newsk)) {
3213 struct mptcp_subflow_context *subflow;
3214 struct sock *new_mptcp_sock;
3216 subflow = mptcp_subflow_ctx(newsk);
3217 new_mptcp_sock = subflow->conn;
3219 /* is_mptcp should be false if subflow->conn is missing, see
3220 * subflow_syn_recv_sock()
3222 if (WARN_ON_ONCE(!new_mptcp_sock)) {
3223 tcp_sk(newsk)->is_mptcp = 0;
3227 newsk = new_mptcp_sock;
3228 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3230 MPTCP_INC_STATS(sock_net(ssk),
3231 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3235 newsk->sk_kern_sock = kern;
3239 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
3241 struct mptcp_subflow_context *subflow, *tmp;
3242 struct sock *sk = (struct sock *)msk;
3244 __mptcp_clear_xmit(sk);
3246 /* join list will be eventually flushed (with rst) at sock lock release time */
3247 mptcp_for_each_subflow_safe(msk, subflow, tmp)
3248 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3250 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3251 mptcp_data_lock(sk);
3252 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
3253 __skb_queue_purge(&sk->sk_receive_queue);
3254 skb_rbtree_purge(&msk->out_of_order_queue);
3255 mptcp_data_unlock(sk);
3257 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3258 * inet_sock_destruct() will dispose it
3260 sk->sk_forward_alloc += msk->rmem_fwd_alloc;
3261 msk->rmem_fwd_alloc = 0;
3262 mptcp_token_destroy(msk);
3263 mptcp_pm_free_anno_list(msk);
3264 mptcp_free_local_addr_list(msk);
3267 static void mptcp_destroy(struct sock *sk)
3269 struct mptcp_sock *msk = mptcp_sk(sk);
3271 /* allow the following to close even the initial subflow */
3272 msk->free_first = 1;
3273 mptcp_destroy_common(msk, 0);
3274 sk_sockets_allocated_dec(sk);
3277 void __mptcp_data_acked(struct sock *sk)
3279 if (!sock_owned_by_user(sk))
3280 __mptcp_clean_una(sk);
3282 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3284 if (mptcp_pending_data_fin_ack(sk))
3285 mptcp_schedule_work(sk);
3288 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3290 if (!mptcp_send_head(sk))
3293 if (!sock_owned_by_user(sk))
3294 __mptcp_subflow_push_pending(sk, ssk, false);
3296 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3299 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3300 BIT(MPTCP_RETRANSMIT) | \
3301 BIT(MPTCP_FLUSH_JOIN_LIST))
3303 /* processes deferred events and flush wmem */
3304 static void mptcp_release_cb(struct sock *sk)
3305 __must_hold(&sk->sk_lock.slock)
3307 struct mptcp_sock *msk = mptcp_sk(sk);
3310 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) |
3312 struct list_head join_list;
3317 INIT_LIST_HEAD(&join_list);
3318 list_splice_init(&msk->join_list, &join_list);
3320 /* the following actions acquire the subflow socket lock
3322 * 1) can't be invoked in atomic scope
3323 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3324 * datapath acquires the msk socket spinlock while helding
3325 * the subflow socket lock
3327 msk->push_pending = 0;
3328 msk->cb_flags &= ~flags;
3329 spin_unlock_bh(&sk->sk_lock.slock);
3331 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3332 __mptcp_flush_join_list(sk, &join_list);
3333 if (flags & BIT(MPTCP_PUSH_PENDING))
3334 __mptcp_push_pending(sk, 0);
3335 if (flags & BIT(MPTCP_RETRANSMIT))
3336 __mptcp_retrans(sk);
3339 spin_lock_bh(&sk->sk_lock.slock);
3342 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3343 __mptcp_clean_una_wakeup(sk);
3344 if (unlikely(msk->cb_flags)) {
3345 /* be sure to set the current sk state before tacking actions
3346 * depending on sk_state, that is processing MPTCP_ERROR_REPORT
3348 if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags))
3349 __mptcp_set_connected(sk);
3350 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3351 __mptcp_error_report(sk);
3354 __mptcp_update_rmem(sk);
3357 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3358 * TCP can't schedule delack timer before the subflow is fully established.
3359 * MPTCP uses the delack timer to do 3rd ack retransmissions
3361 static void schedule_3rdack_retransmission(struct sock *ssk)
3363 struct inet_connection_sock *icsk = inet_csk(ssk);
3364 struct tcp_sock *tp = tcp_sk(ssk);
3365 unsigned long timeout;
3367 if (mptcp_subflow_ctx(ssk)->fully_established)
3370 /* reschedule with a timeout above RTT, as we must look only for drop */
3372 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3374 timeout = TCP_TIMEOUT_INIT;
3377 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3378 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3379 icsk->icsk_ack.timeout = timeout;
3380 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3383 void mptcp_subflow_process_delegated(struct sock *ssk)
3385 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3386 struct sock *sk = subflow->conn;
3388 if (test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) {
3389 mptcp_data_lock(sk);
3390 if (!sock_owned_by_user(sk))
3391 __mptcp_subflow_push_pending(sk, ssk, true);
3393 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3394 mptcp_data_unlock(sk);
3395 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_SEND);
3397 if (test_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status)) {
3398 schedule_3rdack_retransmission(ssk);
3399 mptcp_subflow_delegated_done(subflow, MPTCP_DELEGATE_ACK);
3403 static int mptcp_hash(struct sock *sk)
3405 /* should never be called,
3406 * we hash the TCP subflows not the master socket
3412 static void mptcp_unhash(struct sock *sk)
3414 /* called from sk_common_release(), but nothing to do here */
3417 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3419 struct mptcp_sock *msk = mptcp_sk(sk);
3421 pr_debug("msk=%p, ssk=%p", msk, msk->first);
3422 if (WARN_ON_ONCE(!msk->first))
3425 return inet_csk_get_port(msk->first, snum);
3428 void mptcp_finish_connect(struct sock *ssk)
3430 struct mptcp_subflow_context *subflow;
3431 struct mptcp_sock *msk;
3434 subflow = mptcp_subflow_ctx(ssk);
3438 pr_debug("msk=%p, token=%u", sk, subflow->token);
3440 subflow->map_seq = subflow->iasn;
3441 subflow->map_subflow_seq = 1;
3443 /* the socket is not connected yet, no msk/subflow ops can access/race
3444 * accessing the field below
3446 WRITE_ONCE(msk->local_key, subflow->local_key);
3447 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3448 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3449 WRITE_ONCE(msk->snd_una, msk->write_seq);
3451 mptcp_pm_new_connection(msk, ssk, 0);
3453 mptcp_rcv_space_init(msk, ssk);
3456 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3458 write_lock_bh(&sk->sk_callback_lock);
3459 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3460 sk_set_socket(sk, parent);
3461 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3462 write_unlock_bh(&sk->sk_callback_lock);
3465 bool mptcp_finish_join(struct sock *ssk)
3467 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3468 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3469 struct sock *parent = (void *)msk;
3472 pr_debug("msk=%p, subflow=%p", msk, subflow);
3474 /* mptcp socket already closing? */
3475 if (!mptcp_is_fully_established(parent)) {
3476 subflow->reset_reason = MPTCP_RST_EMPTCP;
3480 /* active subflow, already present inside the conn_list */
3481 if (!list_empty(&subflow->node)) {
3482 mptcp_subflow_joined(msk, ssk);
3486 if (!mptcp_pm_allow_new_subflow(msk))
3487 goto err_prohibited;
3489 /* If we can't acquire msk socket lock here, let the release callback
3492 mptcp_data_lock(parent);
3493 if (!sock_owned_by_user(parent)) {
3494 ret = __mptcp_finish_join(msk, ssk);
3497 list_add_tail(&subflow->node, &msk->conn_list);
3501 list_add_tail(&subflow->node, &msk->join_list);
3502 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3504 mptcp_data_unlock(parent);
3508 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3515 static void mptcp_shutdown(struct sock *sk, int how)
3517 pr_debug("sk=%p, how=%d", sk, how);
3519 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3520 __mptcp_wr_shutdown(sk);
3523 static int mptcp_forward_alloc_get(const struct sock *sk)
3525 return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3528 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3530 const struct sock *sk = (void *)msk;
3533 if (sk->sk_state == TCP_LISTEN)
3536 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3539 delta = msk->write_seq - v;
3540 if (__mptcp_check_fallback(msk) && msk->first) {
3541 struct tcp_sock *tp = tcp_sk(msk->first);
3543 /* the first subflow is disconnected after close - see
3544 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3545 * so ignore that status, too.
3547 if (!((1 << msk->first->sk_state) &
3548 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3549 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3551 if (delta > INT_MAX)
3557 static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3559 struct mptcp_sock *msk = mptcp_sk(sk);
3564 if (sk->sk_state == TCP_LISTEN)
3568 __mptcp_move_skbs(msk);
3569 *karg = mptcp_inq_hint(sk);
3573 slow = lock_sock_fast(sk);
3574 *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3575 unlock_sock_fast(sk, slow);
3578 slow = lock_sock_fast(sk);
3579 *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3580 unlock_sock_fast(sk, slow);
3583 return -ENOIOCTLCMD;
3589 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3590 struct mptcp_subflow_context *subflow)
3592 subflow->request_mptcp = 0;
3593 __mptcp_do_fallback(msk);
3596 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3598 struct mptcp_subflow_context *subflow;
3599 struct mptcp_sock *msk = mptcp_sk(sk);
3603 ssk = __mptcp_nmpc_sk(msk);
3605 return PTR_ERR(ssk);
3607 inet_sk_state_store(sk, TCP_SYN_SENT);
3608 subflow = mptcp_subflow_ctx(ssk);
3609 #ifdef CONFIG_TCP_MD5SIG
3610 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3613 if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
3614 mptcp_subflow_early_fallback(msk, subflow);
3616 if (subflow->request_mptcp && mptcp_token_new_connect(ssk)) {
3617 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT);
3618 mptcp_subflow_early_fallback(msk, subflow);
3620 if (likely(!__mptcp_check_fallback(msk)))
3621 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3623 /* if reaching here via the fastopen/sendmsg path, the caller already
3624 * acquired the subflow socket lock, too.
3626 if (!msk->fastopening)
3629 /* the following mirrors closely a very small chunk of code from
3630 * __inet_stream_connect()
3632 if (ssk->sk_state != TCP_CLOSE)
3635 if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
3636 err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3641 err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3645 inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3648 if (!msk->fastopening)
3651 /* on successful connect, the msk state will be moved to established by
3652 * subflow_finish_connect()
3654 if (unlikely(err)) {
3655 /* avoid leaving a dangling token in an unconnected socket */
3656 mptcp_token_destroy(msk);
3657 inet_sk_state_store(sk, TCP_CLOSE);
3661 mptcp_copy_inaddrs(sk, ssk);
3665 static struct proto mptcp_prot = {
3667 .owner = THIS_MODULE,
3668 .init = mptcp_init_sock,
3669 .connect = mptcp_connect,
3670 .disconnect = mptcp_disconnect,
3671 .close = mptcp_close,
3672 .accept = mptcp_accept,
3673 .setsockopt = mptcp_setsockopt,
3674 .getsockopt = mptcp_getsockopt,
3675 .shutdown = mptcp_shutdown,
3676 .destroy = mptcp_destroy,
3677 .sendmsg = mptcp_sendmsg,
3678 .ioctl = mptcp_ioctl,
3679 .recvmsg = mptcp_recvmsg,
3680 .release_cb = mptcp_release_cb,
3682 .unhash = mptcp_unhash,
3683 .get_port = mptcp_get_port,
3684 .forward_alloc_get = mptcp_forward_alloc_get,
3685 .sockets_allocated = &mptcp_sockets_allocated,
3687 .memory_allocated = &tcp_memory_allocated,
3688 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3690 .memory_pressure = &tcp_memory_pressure,
3691 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3692 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3693 .sysctl_mem = sysctl_tcp_mem,
3694 .obj_size = sizeof(struct mptcp_sock),
3695 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3696 .no_autobind = true,
3699 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3701 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3702 struct sock *ssk, *sk = sock->sk;
3706 ssk = __mptcp_nmpc_sk(msk);
3712 if (sk->sk_family == AF_INET)
3713 err = inet_bind_sk(ssk, uaddr, addr_len);
3714 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3715 else if (sk->sk_family == AF_INET6)
3716 err = inet6_bind_sk(ssk, uaddr, addr_len);
3719 mptcp_copy_inaddrs(sk, ssk);
3726 static int mptcp_listen(struct socket *sock, int backlog)
3728 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3729 struct sock *sk = sock->sk;
3733 pr_debug("msk=%p", msk);
3738 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
3741 ssk = __mptcp_nmpc_sk(msk);
3747 inet_sk_state_store(sk, TCP_LISTEN);
3748 sock_set_flag(sk, SOCK_RCU_FREE);
3751 err = __inet_listen_sk(ssk, backlog);
3753 inet_sk_state_store(sk, inet_sk_state_load(ssk));
3756 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3757 mptcp_copy_inaddrs(sk, ssk);
3758 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
3766 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3767 int flags, bool kern)
3769 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3770 struct sock *ssk, *newsk;
3773 pr_debug("msk=%p", msk);
3775 /* Buggy applications can call accept on socket states other then LISTEN
3776 * but no need to allocate the first subflow just to error out.
3778 ssk = READ_ONCE(msk->first);
3782 newsk = mptcp_accept(ssk, flags, &err, kern);
3788 __inet_accept(sock, newsock, newsk);
3789 if (!mptcp_is_tcpsk(newsock->sk)) {
3790 struct mptcp_sock *msk = mptcp_sk(newsk);
3791 struct mptcp_subflow_context *subflow;
3793 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3794 msk->in_accept_queue = 0;
3796 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3797 * This is needed so NOSPACE flag can be set from tcp stack.
3799 mptcp_for_each_subflow(msk, subflow) {
3800 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3802 if (!ssk->sk_socket)
3803 mptcp_sock_graft(ssk, newsock);
3806 /* Do late cleanup for the first subflow as necessary. Also
3807 * deal with bad peers not doing a complete shutdown.
3809 if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
3810 __mptcp_close_ssk(newsk, msk->first,
3811 mptcp_subflow_ctx(msk->first), 0);
3812 if (unlikely(list_is_singular(&msk->conn_list)))
3813 inet_sk_state_store(newsk, TCP_CLOSE);
3816 release_sock(newsk);
3821 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3823 struct sock *sk = (struct sock *)msk;
3825 if (sk_stream_is_writeable(sk))
3826 return EPOLLOUT | EPOLLWRNORM;
3828 mptcp_set_nospace(sk);
3829 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3830 if (sk_stream_is_writeable(sk))
3831 return EPOLLOUT | EPOLLWRNORM;
3836 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3837 struct poll_table_struct *wait)
3839 struct sock *sk = sock->sk;
3840 struct mptcp_sock *msk;
3846 sock_poll_wait(file, sock, wait);
3848 state = inet_sk_state_load(sk);
3849 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
3850 if (state == TCP_LISTEN) {
3851 struct sock *ssk = READ_ONCE(msk->first);
3853 if (WARN_ON_ONCE(!ssk))
3856 return inet_csk_listen_poll(ssk);
3859 shutdown = READ_ONCE(sk->sk_shutdown);
3860 if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3862 if (shutdown & RCV_SHUTDOWN)
3863 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3865 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3866 mask |= mptcp_check_readable(msk);
3867 if (shutdown & SEND_SHUTDOWN)
3868 mask |= EPOLLOUT | EPOLLWRNORM;
3870 mask |= mptcp_check_writeable(msk);
3871 } else if (state == TCP_SYN_SENT &&
3872 inet_test_bit(DEFER_CONNECT, sk)) {
3873 /* cf tcp_poll() note about TFO */
3874 mask |= EPOLLOUT | EPOLLWRNORM;
3877 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
3879 if (READ_ONCE(sk->sk_err))
3885 static const struct proto_ops mptcp_stream_ops = {
3887 .owner = THIS_MODULE,
3888 .release = inet_release,
3890 .connect = inet_stream_connect,
3891 .socketpair = sock_no_socketpair,
3892 .accept = mptcp_stream_accept,
3893 .getname = inet_getname,
3895 .ioctl = inet_ioctl,
3896 .gettstamp = sock_gettstamp,
3897 .listen = mptcp_listen,
3898 .shutdown = inet_shutdown,
3899 .setsockopt = sock_common_setsockopt,
3900 .getsockopt = sock_common_getsockopt,
3901 .sendmsg = inet_sendmsg,
3902 .recvmsg = inet_recvmsg,
3903 .mmap = sock_no_mmap,
3906 static struct inet_protosw mptcp_protosw = {
3907 .type = SOCK_STREAM,
3908 .protocol = IPPROTO_MPTCP,
3909 .prot = &mptcp_prot,
3910 .ops = &mptcp_stream_ops,
3911 .flags = INET_PROTOSW_ICSK,
3914 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3916 struct mptcp_delegated_action *delegated;
3917 struct mptcp_subflow_context *subflow;
3920 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3921 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3922 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3924 bh_lock_sock_nested(ssk);
3925 if (!sock_owned_by_user(ssk) &&
3926 mptcp_subflow_has_delegated_action(subflow))
3927 mptcp_subflow_process_delegated(ssk);
3928 /* ... elsewhere tcp_release_cb_override already processed
3929 * the action or will do at next release_sock().
3930 * In both case must dequeue the subflow here - on the same
3931 * CPU that scheduled it.
3933 bh_unlock_sock(ssk);
3936 if (++work_done == budget)
3940 /* always provide a 0 'work_done' argument, so that napi_complete_done
3941 * will not try accessing the NULL napi->dev ptr
3943 napi_complete_done(napi, 0);
3947 void __init mptcp_proto_init(void)
3949 struct mptcp_delegated_action *delegated;
3952 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
3954 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3955 panic("Failed to allocate MPTCP pcpu counter\n");
3957 init_dummy_netdev(&mptcp_napi_dev);
3958 for_each_possible_cpu(cpu) {
3959 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3960 INIT_LIST_HEAD(&delegated->head);
3961 netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
3963 napi_enable(&delegated->napi);
3966 mptcp_subflow_init();
3971 if (proto_register(&mptcp_prot, 1) != 0)
3972 panic("Failed to register MPTCP proto.\n");
3974 inet_register_protosw(&mptcp_protosw);
3976 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
3979 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3980 static const struct proto_ops mptcp_v6_stream_ops = {
3982 .owner = THIS_MODULE,
3983 .release = inet6_release,
3985 .connect = inet_stream_connect,
3986 .socketpair = sock_no_socketpair,
3987 .accept = mptcp_stream_accept,
3988 .getname = inet6_getname,
3990 .ioctl = inet6_ioctl,
3991 .gettstamp = sock_gettstamp,
3992 .listen = mptcp_listen,
3993 .shutdown = inet_shutdown,
3994 .setsockopt = sock_common_setsockopt,
3995 .getsockopt = sock_common_getsockopt,
3996 .sendmsg = inet6_sendmsg,
3997 .recvmsg = inet6_recvmsg,
3998 .mmap = sock_no_mmap,
3999 #ifdef CONFIG_COMPAT
4000 .compat_ioctl = inet6_compat_ioctl,
4004 static struct proto mptcp_v6_prot;
4006 static struct inet_protosw mptcp_v6_protosw = {
4007 .type = SOCK_STREAM,
4008 .protocol = IPPROTO_MPTCP,
4009 .prot = &mptcp_v6_prot,
4010 .ops = &mptcp_v6_stream_ops,
4011 .flags = INET_PROTOSW_ICSK,
4014 int __init mptcp_proto_v6_init(void)
4018 mptcp_v6_prot = mptcp_prot;
4019 strcpy(mptcp_v6_prot.name, "MPTCPv6");
4020 mptcp_v6_prot.slab = NULL;
4021 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4022 mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4024 err = proto_register(&mptcp_v6_prot, 1);
4028 err = inet6_register_protosw(&mptcp_v6_protosw);
4030 proto_unregister(&mptcp_v6_prot);