1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2017 - 2019, Intel Corporation.
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/sched/signal.h>
13 #include <linux/atomic.h>
15 #include <net/inet_common.h>
16 #include <net/inet_hashtables.h>
17 #include <net/protocol.h>
19 #include <net/tcp_states.h>
20 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21 #include <net/transp_v6.h>
23 #include <net/mptcp.h>
25 #include <asm/ioctls.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/mptcp.h>
32 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
34 struct mptcp_sock msk;
46 #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
49 MPTCP_CMSG_TS = BIT(0),
50 MPTCP_CMSG_INQ = BIT(1),
53 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
55 static void __mptcp_destroy_sock(struct sock *sk);
56 static void __mptcp_check_send_data_fin(struct sock *sk);
58 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
59 static struct net_device mptcp_napi_dev;
61 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
62 * completed yet or has failed, return the subflow socket.
63 * Otherwise return NULL.
65 struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
67 if (!msk->subflow || READ_ONCE(msk->can_ack))
73 /* Returns end sequence number of the receiver's advertised window */
74 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
76 return READ_ONCE(msk->wnd_end);
79 static bool mptcp_is_tcpsk(struct sock *sk)
81 struct socket *sock = sk->sk_socket;
83 if (unlikely(sk->sk_prot == &tcp_prot)) {
84 /* we are being invoked after mptcp_accept() has
85 * accepted a non-mp-capable flow: sk is a tcp_sk,
88 * Hand the socket over to tcp so all further socket ops
91 sock->ops = &inet_stream_ops;
93 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
94 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
95 sock->ops = &inet6_stream_ops;
103 static int __mptcp_socket_create(struct mptcp_sock *msk)
105 struct mptcp_subflow_context *subflow;
106 struct sock *sk = (struct sock *)msk;
107 struct socket *ssock;
110 err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
114 WRITE_ONCE(msk->first, ssock->sk);
115 WRITE_ONCE(msk->subflow, ssock);
116 subflow = mptcp_subflow_ctx(ssock->sk);
117 list_add(&subflow->node, &msk->conn_list);
118 sock_hold(ssock->sk);
119 subflow->request_mptcp = 1;
121 /* This is the first subflow, always with id 0 */
122 subflow->local_id_valid = 1;
123 mptcp_sock_graft(msk->first, sk->sk_socket);
128 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
130 sk_drops_add(sk, skb);
134 static void mptcp_rmem_charge(struct sock *sk, int size)
136 mptcp_sk(sk)->rmem_fwd_alloc -= size;
139 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
140 struct sk_buff *from)
145 if (MPTCP_SKB_CB(from)->offset ||
146 !skb_try_coalesce(to, from, &fragstolen, &delta))
149 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
150 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
151 to->len, MPTCP_SKB_CB(from)->end_seq);
152 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
154 /* note the fwd memory can reach a negative value after accounting
155 * for the delta, but the later skb free will restore a non
158 atomic_add(delta, &sk->sk_rmem_alloc);
159 mptcp_rmem_charge(sk, delta);
160 kfree_skb_partial(from, fragstolen);
165 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
166 struct sk_buff *from)
168 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
171 return mptcp_try_coalesce((struct sock *)msk, to, from);
174 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
176 amount >>= PAGE_SHIFT;
177 mptcp_sk(sk)->rmem_fwd_alloc -= amount << PAGE_SHIFT;
178 __sk_mem_reduce_allocated(sk, amount);
181 static void mptcp_rmem_uncharge(struct sock *sk, int size)
183 struct mptcp_sock *msk = mptcp_sk(sk);
186 msk->rmem_fwd_alloc += size;
187 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
189 /* see sk_mem_uncharge() for the rationale behind the following schema */
190 if (unlikely(reclaimable >= PAGE_SIZE))
191 __mptcp_rmem_reclaim(sk, reclaimable);
194 static void mptcp_rfree(struct sk_buff *skb)
196 unsigned int len = skb->truesize;
197 struct sock *sk = skb->sk;
199 atomic_sub(len, &sk->sk_rmem_alloc);
200 mptcp_rmem_uncharge(sk, len);
203 static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
207 skb->destructor = mptcp_rfree;
208 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
209 mptcp_rmem_charge(sk, skb->truesize);
212 /* "inspired" by tcp_data_queue_ofo(), main differences:
214 * - don't cope with sacks
216 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
218 struct sock *sk = (struct sock *)msk;
219 struct rb_node **p, *parent;
220 u64 seq, end_seq, max_seq;
221 struct sk_buff *skb1;
223 seq = MPTCP_SKB_CB(skb)->map_seq;
224 end_seq = MPTCP_SKB_CB(skb)->end_seq;
225 max_seq = atomic64_read(&msk->rcv_wnd_sent);
227 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
228 RB_EMPTY_ROOT(&msk->out_of_order_queue));
229 if (after64(end_seq, max_seq)) {
232 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
233 (unsigned long long)end_seq - (unsigned long)max_seq,
234 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
235 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
239 p = &msk->out_of_order_queue.rb_node;
240 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
241 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
242 rb_link_node(&skb->rbnode, NULL, p);
243 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
244 msk->ooo_last_skb = skb;
248 /* with 2 subflows, adding at end of ooo queue is quite likely
249 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
251 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
252 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
253 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
257 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
258 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
259 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
260 parent = &msk->ooo_last_skb->rbnode;
261 p = &parent->rb_right;
265 /* Find place to insert this segment. Handle overlaps on the way. */
269 skb1 = rb_to_skb(parent);
270 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
271 p = &parent->rb_left;
274 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
275 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
276 /* All the bits are present. Drop. */
278 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
281 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
285 * continue traversing
288 /* skb's seq == skb1's seq and skb covers skb1.
289 * Replace skb1 with skb.
291 rb_replace_node(&skb1->rbnode, &skb->rbnode,
292 &msk->out_of_order_queue);
293 mptcp_drop(sk, skb1);
294 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
297 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
298 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
301 p = &parent->rb_right;
305 /* Insert segment into RB tree. */
306 rb_link_node(&skb->rbnode, parent, p);
307 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
310 /* Remove other segments covered by skb. */
311 while ((skb1 = skb_rb_next(skb)) != NULL) {
312 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
314 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
315 mptcp_drop(sk, skb1);
316 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
318 /* If there is no skb after us, we are the last_skb ! */
320 msk->ooo_last_skb = skb;
324 mptcp_set_owner_r(skb, sk);
327 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
329 struct mptcp_sock *msk = mptcp_sk(sk);
332 if (size <= msk->rmem_fwd_alloc)
335 size -= msk->rmem_fwd_alloc;
336 amt = sk_mem_pages(size);
337 amount = amt << PAGE_SHIFT;
338 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
341 msk->rmem_fwd_alloc += amount;
345 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
346 struct sk_buff *skb, unsigned int offset,
349 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
350 struct sock *sk = (struct sock *)msk;
351 struct sk_buff *tail;
354 __skb_unlink(skb, &ssk->sk_receive_queue);
359 /* try to fetch required memory from subflow */
360 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
363 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
365 /* the skb map_seq accounts for the skb offset:
366 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
369 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
370 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
371 MPTCP_SKB_CB(skb)->offset = offset;
372 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
374 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
376 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
377 tail = skb_peek_tail(&sk->sk_receive_queue);
378 if (tail && mptcp_try_coalesce(sk, tail, skb))
381 mptcp_set_owner_r(skb, sk);
382 __skb_queue_tail(&sk->sk_receive_queue, skb);
384 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
385 mptcp_data_queue_ofo(msk, skb);
389 /* old data, keep it simple and drop the whole pkt, sender
390 * will retransmit as needed, if needed.
392 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
398 static void mptcp_stop_timer(struct sock *sk)
400 struct inet_connection_sock *icsk = inet_csk(sk);
402 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
403 mptcp_sk(sk)->timer_ival = 0;
406 static void mptcp_close_wake_up(struct sock *sk)
408 if (sock_flag(sk, SOCK_DEAD))
411 sk->sk_state_change(sk);
412 if (sk->sk_shutdown == SHUTDOWN_MASK ||
413 sk->sk_state == TCP_CLOSE)
414 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
416 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
419 static bool mptcp_pending_data_fin_ack(struct sock *sk)
421 struct mptcp_sock *msk = mptcp_sk(sk);
423 return !__mptcp_check_fallback(msk) &&
424 ((1 << sk->sk_state) &
425 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
426 msk->write_seq == READ_ONCE(msk->snd_una);
429 static void mptcp_check_data_fin_ack(struct sock *sk)
431 struct mptcp_sock *msk = mptcp_sk(sk);
433 /* Look for an acknowledged DATA_FIN */
434 if (mptcp_pending_data_fin_ack(sk)) {
435 WRITE_ONCE(msk->snd_data_fin_enable, 0);
437 switch (sk->sk_state) {
439 inet_sk_state_store(sk, TCP_FIN_WAIT2);
443 inet_sk_state_store(sk, TCP_CLOSE);
447 mptcp_close_wake_up(sk);
451 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
453 struct mptcp_sock *msk = mptcp_sk(sk);
455 if (READ_ONCE(msk->rcv_data_fin) &&
456 ((1 << sk->sk_state) &
457 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
458 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
460 if (msk->ack_seq == rcv_data_fin_seq) {
462 *seq = rcv_data_fin_seq;
471 static void mptcp_set_datafin_timeout(const struct sock *sk)
473 struct inet_connection_sock *icsk = inet_csk(sk);
476 retransmits = min_t(u32, icsk->icsk_retransmits,
477 ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
479 mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
482 static void __mptcp_set_timeout(struct sock *sk, long tout)
484 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
487 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
489 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
491 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
492 inet_csk(ssk)->icsk_timeout - jiffies : 0;
495 static void mptcp_set_timeout(struct sock *sk)
497 struct mptcp_subflow_context *subflow;
500 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
501 tout = max(tout, mptcp_timeout_from_subflow(subflow));
502 __mptcp_set_timeout(sk, tout);
505 static inline bool tcp_can_send_ack(const struct sock *ssk)
507 return !((1 << inet_sk_state_load(ssk)) &
508 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
511 void __mptcp_subflow_send_ack(struct sock *ssk)
513 if (tcp_can_send_ack(ssk))
517 static void mptcp_subflow_send_ack(struct sock *ssk)
521 slow = lock_sock_fast(ssk);
522 __mptcp_subflow_send_ack(ssk);
523 unlock_sock_fast(ssk, slow);
526 static void mptcp_send_ack(struct mptcp_sock *msk)
528 struct mptcp_subflow_context *subflow;
530 mptcp_for_each_subflow(msk, subflow)
531 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
534 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
538 slow = lock_sock_fast(ssk);
539 if (tcp_can_send_ack(ssk))
540 tcp_cleanup_rbuf(ssk, 1);
541 unlock_sock_fast(ssk, slow);
544 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
546 const struct inet_connection_sock *icsk = inet_csk(ssk);
547 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
548 const struct tcp_sock *tp = tcp_sk(ssk);
550 return (ack_pending & ICSK_ACK_SCHED) &&
551 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
552 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
553 (rx_empty && ack_pending &
554 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
557 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
559 int old_space = READ_ONCE(msk->old_wspace);
560 struct mptcp_subflow_context *subflow;
561 struct sock *sk = (struct sock *)msk;
562 int space = __mptcp_space(sk);
563 bool cleanup, rx_empty;
565 cleanup = (space > 0) && (space >= (old_space << 1));
566 rx_empty = !__mptcp_rmem(sk);
568 mptcp_for_each_subflow(msk, subflow) {
569 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
571 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
572 mptcp_subflow_cleanup_rbuf(ssk);
576 static bool mptcp_check_data_fin(struct sock *sk)
578 struct mptcp_sock *msk = mptcp_sk(sk);
579 u64 rcv_data_fin_seq;
582 if (__mptcp_check_fallback(msk))
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 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 */
623 mptcp_close_wake_up(sk);
628 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
632 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
633 struct sock *sk = (struct sock *)msk;
634 unsigned int moved = 0;
635 bool more_data_avail;
640 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
642 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
643 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
645 if (unlikely(ssk_rbuf > sk_rbuf)) {
646 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
651 pr_debug("msk=%p ssk=%p", msk, ssk);
654 u32 map_remaining, offset;
655 u32 seq = tp->copied_seq;
659 /* try to move as much data as available */
660 map_remaining = subflow->map_data_len -
661 mptcp_subflow_get_map_offset(subflow);
663 skb = skb_peek(&ssk->sk_receive_queue);
665 /* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
666 * a different CPU can have already processed the pending
667 * data, stop here or we can enter an infinite loop
674 if (__mptcp_check_fallback(msk)) {
675 /* Under fallback skbs have no MPTCP extension and TCP could
676 * collapse them between the dummy map creation and the
677 * current dequeue. Be sure to adjust the map size.
679 map_remaining = skb->len;
680 subflow->map_data_len = skb->len;
683 offset = seq - TCP_SKB_CB(skb)->seq;
684 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
690 if (offset < skb->len) {
691 size_t len = skb->len - offset;
696 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
700 if (WARN_ON_ONCE(map_remaining < len))
704 sk_eat_skb(ssk, skb);
708 WRITE_ONCE(tp->copied_seq, seq);
709 more_data_avail = mptcp_subflow_data_available(ssk);
711 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
715 } while (more_data_avail);
721 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
723 struct sock *sk = (struct sock *)msk;
724 struct sk_buff *skb, *tail;
729 p = rb_first(&msk->out_of_order_queue);
730 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
733 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
737 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
739 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
742 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
746 end_seq = MPTCP_SKB_CB(skb)->end_seq;
747 tail = skb_peek_tail(&sk->sk_receive_queue);
748 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
749 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
751 /* skip overlapping data, if any */
752 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
753 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
755 MPTCP_SKB_CB(skb)->offset += delta;
756 MPTCP_SKB_CB(skb)->map_seq += delta;
757 __skb_queue_tail(&sk->sk_receive_queue, skb);
759 msk->ack_seq = end_seq;
765 /* In most cases we will be able to lock the mptcp socket. If its already
766 * owned, we need to defer to the work queue to avoid ABBA deadlock.
768 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
770 struct sock *sk = (struct sock *)msk;
771 unsigned int moved = 0;
773 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
774 __mptcp_ofo_queue(msk);
775 if (unlikely(ssk->sk_err)) {
776 if (!sock_owned_by_user(sk))
777 __mptcp_error_report(sk);
779 __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags);
782 /* If the moves have caught up with the DATA_FIN sequence number
783 * it's time to ack the DATA_FIN and change socket state, but
784 * this is not a good place to change state. Let the workqueue
787 if (mptcp_pending_data_fin(sk, NULL))
788 mptcp_schedule_work(sk);
792 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
794 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
795 struct mptcp_sock *msk = mptcp_sk(sk);
796 int sk_rbuf, ssk_rbuf;
798 /* The peer can send data while we are shutting down this
799 * subflow at msk destruction time, but we must avoid enqueuing
800 * more data to the msk receive queue
802 if (unlikely(subflow->disposable))
805 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
806 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
807 if (unlikely(ssk_rbuf > sk_rbuf))
810 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
811 if (__mptcp_rmem(sk) > sk_rbuf) {
812 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
816 /* Wake-up the reader only for in-sequence data */
818 if (move_skbs_to_msk(msk, ssk))
819 sk->sk_data_ready(sk);
821 mptcp_data_unlock(sk);
824 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
826 struct sock *sk = (struct sock *)msk;
828 if (sk->sk_state != TCP_ESTABLISHED)
831 /* attach to msk socket only after we are sure we will deal with it
834 if (sk->sk_socket && !ssk->sk_socket)
835 mptcp_sock_graft(ssk, sk->sk_socket);
837 mptcp_sockopt_sync_locked(msk, ssk);
841 static void __mptcp_flush_join_list(struct sock *sk)
843 struct mptcp_subflow_context *tmp, *subflow;
844 struct mptcp_sock *msk = mptcp_sk(sk);
846 list_for_each_entry_safe(subflow, tmp, &msk->join_list, node) {
847 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
848 bool slow = lock_sock_fast(ssk);
850 list_move_tail(&subflow->node, &msk->conn_list);
851 if (!__mptcp_finish_join(msk, ssk))
852 mptcp_subflow_reset(ssk);
853 unlock_sock_fast(ssk, slow);
857 static bool mptcp_timer_pending(struct sock *sk)
859 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
862 static void mptcp_reset_timer(struct sock *sk)
864 struct inet_connection_sock *icsk = inet_csk(sk);
867 /* prevent rescheduling on close */
868 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
871 tout = mptcp_sk(sk)->timer_ival;
872 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
875 bool mptcp_schedule_work(struct sock *sk)
877 if (inet_sk_state_load(sk) != TCP_CLOSE &&
878 schedule_work(&mptcp_sk(sk)->work)) {
879 /* each subflow already holds a reference to the sk, and the
880 * workqueue is invoked by a subflow, so sk can't go away here.
888 void mptcp_subflow_eof(struct sock *sk)
890 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
891 mptcp_schedule_work(sk);
894 static void mptcp_check_for_eof(struct mptcp_sock *msk)
896 struct mptcp_subflow_context *subflow;
897 struct sock *sk = (struct sock *)msk;
900 mptcp_for_each_subflow(msk, subflow)
901 receivers += !subflow->rx_eof;
905 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
906 /* hopefully temporary hack: propagate shutdown status
907 * to msk, when all subflows agree on it
909 sk->sk_shutdown |= RCV_SHUTDOWN;
911 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
912 sk->sk_data_ready(sk);
915 switch (sk->sk_state) {
916 case TCP_ESTABLISHED:
917 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
920 inet_sk_state_store(sk, TCP_CLOSING);
923 inet_sk_state_store(sk, TCP_CLOSE);
928 mptcp_close_wake_up(sk);
931 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
933 struct mptcp_subflow_context *subflow;
934 struct sock *sk = (struct sock *)msk;
936 sock_owned_by_me(sk);
938 mptcp_for_each_subflow(msk, subflow) {
939 if (READ_ONCE(subflow->data_avail))
940 return mptcp_subflow_tcp_sock(subflow);
946 static bool mptcp_skb_can_collapse_to(u64 write_seq,
947 const struct sk_buff *skb,
948 const struct mptcp_ext *mpext)
950 if (!tcp_skb_can_collapse_to(skb))
953 /* can collapse only if MPTCP level sequence is in order and this
954 * mapping has not been xmitted yet
956 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
960 /* we can append data to the given data frag if:
961 * - there is space available in the backing page_frag
962 * - the data frag tail matches the current page_frag free offset
963 * - the data frag end sequence number matches the current write seq
965 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
966 const struct page_frag *pfrag,
967 const struct mptcp_data_frag *df)
969 return df && pfrag->page == df->page &&
970 pfrag->size - pfrag->offset > 0 &&
971 pfrag->offset == (df->offset + df->data_len) &&
972 df->data_seq + df->data_len == msk->write_seq;
975 static void dfrag_uncharge(struct sock *sk, int len)
977 sk_mem_uncharge(sk, len);
978 sk_wmem_queued_add(sk, -len);
981 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
983 int len = dfrag->data_len + dfrag->overhead;
985 list_del(&dfrag->list);
986 dfrag_uncharge(sk, len);
987 put_page(dfrag->page);
990 static void __mptcp_clean_una(struct sock *sk)
992 struct mptcp_sock *msk = mptcp_sk(sk);
993 struct mptcp_data_frag *dtmp, *dfrag;
996 /* on fallback we just need to ignore snd_una, as this is really
999 if (__mptcp_check_fallback(msk))
1000 msk->snd_una = READ_ONCE(msk->snd_nxt);
1002 snd_una = msk->snd_una;
1003 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1004 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1007 if (unlikely(dfrag == msk->first_pending)) {
1008 /* in recovery mode can see ack after the current snd head */
1009 if (WARN_ON_ONCE(!msk->recovery))
1012 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1015 dfrag_clear(sk, dfrag);
1018 dfrag = mptcp_rtx_head(sk);
1019 if (dfrag && after64(snd_una, dfrag->data_seq)) {
1020 u64 delta = snd_una - dfrag->data_seq;
1022 /* prevent wrap around in recovery mode */
1023 if (unlikely(delta > dfrag->already_sent)) {
1024 if (WARN_ON_ONCE(!msk->recovery))
1026 if (WARN_ON_ONCE(delta > dfrag->data_len))
1028 dfrag->already_sent += delta - dfrag->already_sent;
1031 dfrag->data_seq += delta;
1032 dfrag->offset += delta;
1033 dfrag->data_len -= delta;
1034 dfrag->already_sent -= delta;
1036 dfrag_uncharge(sk, delta);
1039 /* all retransmitted data acked, recovery completed */
1040 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1041 msk->recovery = false;
1044 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1045 snd_una == READ_ONCE(msk->write_seq)) {
1046 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1047 mptcp_stop_timer(sk);
1049 mptcp_reset_timer(sk);
1053 static void __mptcp_clean_una_wakeup(struct sock *sk)
1055 lockdep_assert_held_once(&sk->sk_lock.slock);
1057 __mptcp_clean_una(sk);
1058 mptcp_write_space(sk);
1061 static void mptcp_clean_una_wakeup(struct sock *sk)
1063 mptcp_data_lock(sk);
1064 __mptcp_clean_una_wakeup(sk);
1065 mptcp_data_unlock(sk);
1068 static void mptcp_enter_memory_pressure(struct sock *sk)
1070 struct mptcp_subflow_context *subflow;
1071 struct mptcp_sock *msk = mptcp_sk(sk);
1074 sk_stream_moderate_sndbuf(sk);
1075 mptcp_for_each_subflow(msk, subflow) {
1076 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1079 tcp_enter_memory_pressure(ssk);
1080 sk_stream_moderate_sndbuf(ssk);
1085 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1088 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1090 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1091 pfrag, sk->sk_allocation)))
1094 mptcp_enter_memory_pressure(sk);
1098 static struct mptcp_data_frag *
1099 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1102 int offset = ALIGN(orig_offset, sizeof(long));
1103 struct mptcp_data_frag *dfrag;
1105 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1106 dfrag->data_len = 0;
1107 dfrag->data_seq = msk->write_seq;
1108 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1109 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1110 dfrag->already_sent = 0;
1111 dfrag->page = pfrag->page;
1116 struct mptcp_sendmsg_info {
1122 bool data_lock_held;
1125 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1126 u64 data_seq, int avail_size)
1128 u64 window_end = mptcp_wnd_end(msk);
1131 if (__mptcp_check_fallback(msk))
1134 mptcp_snd_wnd = window_end - data_seq;
1135 avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1137 if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1138 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1139 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1145 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1147 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1151 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1155 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1157 struct sk_buff *skb;
1159 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1161 if (likely(__mptcp_add_ext(skb, gfp))) {
1162 skb_reserve(skb, MAX_TCP_HEADER);
1163 skb->ip_summed = CHECKSUM_PARTIAL;
1164 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1169 mptcp_enter_memory_pressure(sk);
1174 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1176 struct sk_buff *skb;
1178 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1182 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1183 tcp_skb_entail(ssk, skb);
1186 tcp_skb_tsorted_anchor_cleanup(skb);
1191 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1193 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1195 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1198 /* note: this always recompute the csum on the whole skb, even
1199 * if we just appended a single frag. More status info needed
1201 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1203 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1204 __wsum csum = ~csum_unfold(mpext->csum);
1205 int offset = skb->len - added;
1207 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1210 static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1212 struct mptcp_ext *mpext)
1217 mpext->infinite_map = 1;
1218 mpext->data_len = 0;
1220 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1221 mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1223 mptcp_do_fallback(ssk);
1226 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1227 struct mptcp_data_frag *dfrag,
1228 struct mptcp_sendmsg_info *info)
1230 u64 data_seq = dfrag->data_seq + info->sent;
1231 int offset = dfrag->offset + info->sent;
1232 struct mptcp_sock *msk = mptcp_sk(sk);
1233 bool zero_window_probe = false;
1234 struct mptcp_ext *mpext = NULL;
1235 bool can_coalesce = false;
1236 bool reuse_skb = true;
1237 struct sk_buff *skb;
1241 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1242 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1244 if (WARN_ON_ONCE(info->sent > info->limit ||
1245 info->limit > dfrag->data_len))
1248 if (unlikely(!__tcp_can_send(ssk)))
1251 /* compute send limit */
1252 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1253 copy = info->size_goal;
1255 skb = tcp_write_queue_tail(ssk);
1256 if (skb && copy > skb->len) {
1257 /* Limit the write to the size available in the
1258 * current skb, if any, so that we create at most a new skb.
1259 * Explicitly tells TCP internals to avoid collapsing on later
1260 * queue management operation, to avoid breaking the ext <->
1261 * SSN association set here
1263 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1264 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1265 TCP_SKB_CB(skb)->eor = 1;
1269 i = skb_shinfo(skb)->nr_frags;
1270 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1271 if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
1272 tcp_mark_push(tcp_sk(ssk), skb);
1279 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1283 i = skb_shinfo(skb)->nr_frags;
1285 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1288 /* Zero window and all data acked? Probe. */
1289 copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1291 u64 snd_una = READ_ONCE(msk->snd_una);
1293 if (snd_una != msk->snd_nxt) {
1294 tcp_remove_empty_skb(ssk);
1298 zero_window_probe = true;
1299 data_seq = snd_una - 1;
1302 /* all mptcp-level data is acked, no skbs should be present into the
1305 WARN_ON_ONCE(reuse_skb);
1308 copy = min_t(size_t, copy, info->limit - info->sent);
1309 if (!sk_wmem_schedule(ssk, copy)) {
1310 tcp_remove_empty_skb(ssk);
1315 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1317 get_page(dfrag->page);
1318 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1322 skb->data_len += copy;
1323 skb->truesize += copy;
1324 sk_wmem_queued_add(ssk, copy);
1325 sk_mem_charge(ssk, copy);
1326 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1327 TCP_SKB_CB(skb)->end_seq += copy;
1328 tcp_skb_pcount_set(skb, 0);
1330 /* on skb reuse we just need to update the DSS len */
1332 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1333 mpext->data_len += copy;
1334 WARN_ON_ONCE(zero_window_probe);
1338 memset(mpext, 0, sizeof(*mpext));
1339 mpext->data_seq = data_seq;
1340 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1341 mpext->data_len = copy;
1345 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1346 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1349 if (zero_window_probe) {
1350 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1352 if (READ_ONCE(msk->csum_enabled))
1353 mptcp_update_data_checksum(skb, copy);
1354 tcp_push_pending_frames(ssk);
1358 if (READ_ONCE(msk->csum_enabled))
1359 mptcp_update_data_checksum(skb, copy);
1360 if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1361 mptcp_update_infinite_map(msk, ssk, mpext);
1362 trace_mptcp_sendmsg_frag(mpext);
1363 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1367 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1368 sizeof(struct tcphdr) - \
1369 MAX_TCP_OPTION_SPACE - \
1370 sizeof(struct ipv6hdr) - \
1371 sizeof(struct frag_hdr))
1373 struct subflow_send_info {
1378 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1380 if (!subflow->stale)
1384 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1387 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1389 if (unlikely(subflow->stale)) {
1390 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1392 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1395 mptcp_subflow_set_active(subflow);
1397 return __mptcp_subflow_active(subflow);
1400 #define SSK_MODE_ACTIVE 0
1401 #define SSK_MODE_BACKUP 1
1402 #define SSK_MODE_MAX 2
1404 /* implement the mptcp packet scheduler;
1405 * returns the subflow that will transmit the next DSS
1406 * additionally updates the rtx timeout
1408 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1410 struct subflow_send_info send_info[SSK_MODE_MAX];
1411 struct mptcp_subflow_context *subflow;
1412 struct sock *sk = (struct sock *)msk;
1413 u32 pace, burst, wmem;
1414 int i, nr_active = 0;
1419 sock_owned_by_me(sk);
1421 if (__mptcp_check_fallback(msk)) {
1424 return __tcp_can_send(msk->first) &&
1425 sk_stream_memory_free(msk->first) ? msk->first : NULL;
1428 /* re-use last subflow, if the burst allow that */
1429 if (msk->last_snd && msk->snd_burst > 0 &&
1430 sk_stream_memory_free(msk->last_snd) &&
1431 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1432 mptcp_set_timeout(sk);
1433 return msk->last_snd;
1436 /* pick the subflow with the lower wmem/wspace ratio */
1437 for (i = 0; i < SSK_MODE_MAX; ++i) {
1438 send_info[i].ssk = NULL;
1439 send_info[i].linger_time = -1;
1442 mptcp_for_each_subflow(msk, subflow) {
1443 trace_mptcp_subflow_get_send(subflow);
1444 ssk = mptcp_subflow_tcp_sock(subflow);
1445 if (!mptcp_subflow_active(subflow))
1448 tout = max(tout, mptcp_timeout_from_subflow(subflow));
1449 nr_active += !subflow->backup;
1450 pace = subflow->avg_pacing_rate;
1451 if (unlikely(!pace)) {
1452 /* init pacing rate from socket */
1453 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1454 pace = subflow->avg_pacing_rate;
1459 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1460 if (linger_time < send_info[subflow->backup].linger_time) {
1461 send_info[subflow->backup].ssk = ssk;
1462 send_info[subflow->backup].linger_time = linger_time;
1465 __mptcp_set_timeout(sk, tout);
1467 /* pick the best backup if no other subflow is active */
1469 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1471 /* According to the blest algorithm, to avoid HoL blocking for the
1472 * faster flow, we need to:
1473 * - estimate the faster flow linger time
1474 * - use the above to estimate the amount of byte transferred
1475 * by the faster flow
1476 * - check that the amount of queued data is greter than the above,
1477 * otherwise do not use the picked, slower, subflow
1478 * We select the subflow with the shorter estimated time to flush
1479 * the queued mem, which basically ensure the above. We just need
1480 * to check that subflow has a non empty cwin.
1482 ssk = send_info[SSK_MODE_ACTIVE].ssk;
1483 if (!ssk || !sk_stream_memory_free(ssk))
1486 burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1487 wmem = READ_ONCE(ssk->sk_wmem_queued);
1489 msk->last_snd = NULL;
1493 subflow = mptcp_subflow_ctx(ssk);
1494 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1495 READ_ONCE(ssk->sk_pacing_rate) * burst,
1497 msk->last_snd = ssk;
1498 msk->snd_burst = burst;
1502 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1504 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1508 static void mptcp_update_post_push(struct mptcp_sock *msk,
1509 struct mptcp_data_frag *dfrag,
1512 u64 snd_nxt_new = dfrag->data_seq;
1514 dfrag->already_sent += sent;
1516 msk->snd_burst -= sent;
1518 snd_nxt_new += dfrag->already_sent;
1520 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1521 * is recovering after a failover. In that event, this re-sends
1524 * Thus compute snd_nxt_new candidate based on
1525 * the dfrag->data_seq that was sent and the data
1526 * that has been handed to the subflow for transmission
1527 * and skip update in case it was old dfrag.
1529 if (likely(after64(snd_nxt_new, msk->snd_nxt)))
1530 msk->snd_nxt = snd_nxt_new;
1533 void mptcp_check_and_set_pending(struct sock *sk)
1535 if (mptcp_send_head(sk))
1536 mptcp_sk(sk)->push_pending |= BIT(MPTCP_PUSH_PENDING);
1539 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1541 struct sock *prev_ssk = NULL, *ssk = NULL;
1542 struct mptcp_sock *msk = mptcp_sk(sk);
1543 struct mptcp_sendmsg_info info = {
1546 bool do_check_data_fin = false;
1547 struct mptcp_data_frag *dfrag;
1550 while ((dfrag = mptcp_send_head(sk))) {
1551 info.sent = dfrag->already_sent;
1552 info.limit = dfrag->data_len;
1553 len = dfrag->data_len - dfrag->already_sent;
1558 ssk = mptcp_subflow_get_send(msk);
1560 /* First check. If the ssk has changed since
1561 * the last round, release prev_ssk
1563 if (ssk != prev_ssk && prev_ssk)
1564 mptcp_push_release(prev_ssk, &info);
1568 /* Need to lock the new subflow only if different
1569 * from the previous one, otherwise we are still
1570 * helding the relevant lock
1572 if (ssk != prev_ssk)
1575 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1579 mptcp_push_release(ssk, &info);
1583 do_check_data_fin = true;
1587 mptcp_update_post_push(msk, dfrag, ret);
1589 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1592 /* at this point we held the socket lock for the last subflow we used */
1594 mptcp_push_release(ssk, &info);
1597 /* ensure the rtx timer is running */
1598 if (!mptcp_timer_pending(sk))
1599 mptcp_reset_timer(sk);
1600 if (do_check_data_fin)
1601 __mptcp_check_send_data_fin(sk);
1604 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
1606 struct mptcp_sock *msk = mptcp_sk(sk);
1607 struct mptcp_sendmsg_info info = {
1608 .data_lock_held = true,
1610 struct mptcp_data_frag *dfrag;
1611 struct sock *xmit_ssk;
1612 int len, copied = 0;
1616 while ((dfrag = mptcp_send_head(sk))) {
1617 info.sent = dfrag->already_sent;
1618 info.limit = dfrag->data_len;
1619 len = dfrag->data_len - dfrag->already_sent;
1623 /* the caller already invoked the packet scheduler,
1624 * check for a different subflow usage only after
1625 * spooling the first chunk of data
1627 xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
1630 if (xmit_ssk != ssk) {
1631 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk),
1632 MPTCP_DELEGATE_SEND);
1636 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1645 mptcp_update_post_push(msk, dfrag, ret);
1647 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1651 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1652 * not going to flush it via release_sock()
1655 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1657 if (!mptcp_timer_pending(sk))
1658 mptcp_reset_timer(sk);
1660 if (msk->snd_data_fin_enable &&
1661 msk->snd_nxt + 1 == msk->write_seq)
1662 mptcp_schedule_work(sk);
1666 static void mptcp_set_nospace(struct sock *sk)
1668 /* enable autotune */
1669 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1671 /* will be cleared on avail space */
1672 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1675 static int mptcp_disconnect(struct sock *sk, int flags);
1677 static int mptcp_sendmsg_fastopen(struct sock *sk, struct sock *ssk, struct msghdr *msg,
1678 size_t len, int *copied_syn)
1680 unsigned int saved_flags = msg->msg_flags;
1681 struct mptcp_sock *msk = mptcp_sk(sk);
1685 msg->msg_flags |= MSG_DONTWAIT;
1686 msk->connect_flags = O_NONBLOCK;
1687 msk->fastopening = 1;
1688 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1689 msk->fastopening = 0;
1690 msg->msg_flags = saved_flags;
1693 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1694 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1695 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1696 msg->msg_namelen, msg->msg_flags, 1);
1698 /* Keep the same behaviour of plain TCP: zero the copied bytes in
1699 * case of any error, except timeout or signal
1701 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1703 } else if (ret && ret != -EINPROGRESS) {
1704 mptcp_disconnect(sk, 0);
1710 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1712 struct mptcp_sock *msk = mptcp_sk(sk);
1713 struct page_frag *pfrag;
1714 struct socket *ssock;
1719 /* we don't support FASTOPEN yet */
1720 if (msg->msg_flags & MSG_FASTOPEN)
1723 /* silently ignore everything else */
1724 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL;
1728 ssock = __mptcp_nmpc_socket(msk);
1729 if (unlikely(ssock && inet_sk(ssock->sk)->defer_connect)) {
1732 ret = mptcp_sendmsg_fastopen(sk, ssock->sk, msg, len, &copied_syn);
1733 copied += copied_syn;
1734 if (ret == -EINPROGRESS && copied_syn > 0)
1740 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1742 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1743 ret = sk_stream_wait_connect(sk, &timeo);
1749 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1752 pfrag = sk_page_frag(sk);
1754 while (msg_data_left(msg)) {
1755 int total_ts, frag_truesize = 0;
1756 struct mptcp_data_frag *dfrag;
1757 bool dfrag_collapsed;
1758 size_t psize, offset;
1760 /* reuse tail pfrag, if possible, or carve a new one from the
1763 dfrag = mptcp_pending_tail(sk);
1764 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1765 if (!dfrag_collapsed) {
1766 if (!sk_stream_memory_free(sk))
1767 goto wait_for_memory;
1769 if (!mptcp_page_frag_refill(sk, pfrag))
1770 goto wait_for_memory;
1772 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1773 frag_truesize = dfrag->overhead;
1776 /* we do not bound vs wspace, to allow a single packet.
1777 * memory accounting will prevent execessive memory usage
1780 offset = dfrag->offset + dfrag->data_len;
1781 psize = pfrag->size - offset;
1782 psize = min_t(size_t, psize, msg_data_left(msg));
1783 total_ts = psize + frag_truesize;
1785 if (!sk_wmem_schedule(sk, total_ts))
1786 goto wait_for_memory;
1788 if (copy_page_from_iter(dfrag->page, offset, psize,
1789 &msg->msg_iter) != psize) {
1794 /* data successfully copied into the write queue */
1795 sk->sk_forward_alloc -= total_ts;
1797 dfrag->data_len += psize;
1798 frag_truesize += psize;
1799 pfrag->offset += frag_truesize;
1800 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1802 /* charge data on mptcp pending queue to the msk socket
1803 * Note: we charge such data both to sk and ssk
1805 sk_wmem_queued_add(sk, frag_truesize);
1806 if (!dfrag_collapsed) {
1807 get_page(dfrag->page);
1808 list_add_tail(&dfrag->list, &msk->rtx_queue);
1809 if (!msk->first_pending)
1810 WRITE_ONCE(msk->first_pending, dfrag);
1812 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1813 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1819 mptcp_set_nospace(sk);
1820 __mptcp_push_pending(sk, msg->msg_flags);
1821 ret = sk_stream_wait_memory(sk, &timeo);
1827 __mptcp_push_pending(sk, msg->msg_flags);
1837 copied = sk_stream_error(sk, msg->msg_flags, ret);
1841 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1843 size_t len, int flags,
1844 struct scm_timestamping_internal *tss,
1847 struct sk_buff *skb, *tmp;
1850 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1851 u32 offset = MPTCP_SKB_CB(skb)->offset;
1852 u32 data_len = skb->len - offset;
1853 u32 count = min_t(size_t, len - copied, data_len);
1856 if (!(flags & MSG_TRUNC)) {
1857 err = skb_copy_datagram_msg(skb, offset, msg, count);
1858 if (unlikely(err < 0)) {
1865 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1866 tcp_update_recv_tstamps(skb, tss);
1867 *cmsg_flags |= MPTCP_CMSG_TS;
1872 if (count < data_len) {
1873 if (!(flags & MSG_PEEK)) {
1874 MPTCP_SKB_CB(skb)->offset += count;
1875 MPTCP_SKB_CB(skb)->map_seq += count;
1880 if (!(flags & MSG_PEEK)) {
1881 /* we will bulk release the skb memory later */
1882 skb->destructor = NULL;
1883 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1884 __skb_unlink(skb, &msk->receive_queue);
1895 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1897 * Only difference: Use highest rtt estimate of the subflows in use.
1899 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1901 struct mptcp_subflow_context *subflow;
1902 struct sock *sk = (struct sock *)msk;
1903 u32 time, advmss = 1;
1906 sock_owned_by_me(sk);
1911 msk->rcvq_space.copied += copied;
1913 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1914 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1916 rtt_us = msk->rcvq_space.rtt_us;
1917 if (rtt_us && time < (rtt_us >> 3))
1921 mptcp_for_each_subflow(msk, subflow) {
1922 const struct tcp_sock *tp;
1926 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1928 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1929 sf_advmss = READ_ONCE(tp->advmss);
1931 rtt_us = max(sf_rtt_us, rtt_us);
1932 advmss = max(sf_advmss, advmss);
1935 msk->rcvq_space.rtt_us = rtt_us;
1936 if (time < (rtt_us >> 3) || rtt_us == 0)
1939 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1942 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
1943 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1947 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1949 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1951 do_div(grow, msk->rcvq_space.space);
1952 rcvwin += (grow << 1);
1954 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1955 while (tcp_win_from_space(sk, rcvmem) < advmss)
1958 do_div(rcvwin, advmss);
1959 rcvbuf = min_t(u64, rcvwin * rcvmem,
1960 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
1962 if (rcvbuf > sk->sk_rcvbuf) {
1965 window_clamp = tcp_win_from_space(sk, rcvbuf);
1966 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1968 /* Make subflows follow along. If we do not do this, we
1969 * get drops at subflow level if skbs can't be moved to
1970 * the mptcp rx queue fast enough (announced rcv_win can
1971 * exceed ssk->sk_rcvbuf).
1973 mptcp_for_each_subflow(msk, subflow) {
1977 ssk = mptcp_subflow_tcp_sock(subflow);
1978 slow = lock_sock_fast(ssk);
1979 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1980 tcp_sk(ssk)->window_clamp = window_clamp;
1981 tcp_cleanup_rbuf(ssk, 1);
1982 unlock_sock_fast(ssk, slow);
1987 msk->rcvq_space.space = msk->rcvq_space.copied;
1989 msk->rcvq_space.copied = 0;
1990 msk->rcvq_space.time = mstamp;
1993 static void __mptcp_update_rmem(struct sock *sk)
1995 struct mptcp_sock *msk = mptcp_sk(sk);
1997 if (!msk->rmem_released)
2000 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
2001 mptcp_rmem_uncharge(sk, msk->rmem_released);
2002 WRITE_ONCE(msk->rmem_released, 0);
2005 static void __mptcp_splice_receive_queue(struct sock *sk)
2007 struct mptcp_sock *msk = mptcp_sk(sk);
2009 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
2012 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
2014 struct sock *sk = (struct sock *)msk;
2015 unsigned int moved = 0;
2019 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
2022 /* we can have data pending in the subflows only if the msk
2023 * receive buffer was full at subflow_data_ready() time,
2024 * that is an unlikely slow path.
2029 slowpath = lock_sock_fast(ssk);
2030 mptcp_data_lock(sk);
2031 __mptcp_update_rmem(sk);
2032 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
2033 mptcp_data_unlock(sk);
2035 if (unlikely(ssk->sk_err))
2036 __mptcp_error_report(sk);
2037 unlock_sock_fast(ssk, slowpath);
2040 /* acquire the data lock only if some input data is pending */
2042 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
2043 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
2044 mptcp_data_lock(sk);
2045 __mptcp_update_rmem(sk);
2046 ret |= __mptcp_ofo_queue(msk);
2047 __mptcp_splice_receive_queue(sk);
2048 mptcp_data_unlock(sk);
2051 mptcp_check_data_fin((struct sock *)msk);
2052 return !skb_queue_empty(&msk->receive_queue);
2055 static unsigned int mptcp_inq_hint(const struct sock *sk)
2057 const struct mptcp_sock *msk = mptcp_sk(sk);
2058 const struct sk_buff *skb;
2060 skb = skb_peek(&msk->receive_queue);
2062 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
2064 if (hint_val >= INT_MAX)
2067 return (unsigned int)hint_val;
2070 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2076 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2077 int flags, int *addr_len)
2079 struct mptcp_sock *msk = mptcp_sk(sk);
2080 struct scm_timestamping_internal tss;
2081 int copied = 0, cmsg_flags = 0;
2085 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2086 if (unlikely(flags & MSG_ERRQUEUE))
2087 return inet_recv_error(sk, msg, len, addr_len);
2090 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2095 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2097 len = min_t(size_t, len, INT_MAX);
2098 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2100 if (unlikely(msk->recvmsg_inq))
2101 cmsg_flags = MPTCP_CMSG_INQ;
2103 while (copied < len) {
2106 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2107 if (unlikely(bytes_read < 0)) {
2109 copied = bytes_read;
2113 copied += bytes_read;
2115 /* be sure to advertise window change */
2116 mptcp_cleanup_rbuf(msk);
2118 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2121 /* only the master socket status is relevant here. The exit
2122 * conditions mirror closely tcp_recvmsg()
2124 if (copied >= target)
2129 sk->sk_state == TCP_CLOSE ||
2130 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2132 signal_pending(current))
2136 copied = sock_error(sk);
2140 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2141 mptcp_check_for_eof(msk);
2143 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2144 /* race breaker: the shutdown could be after the
2145 * previous receive queue check
2147 if (__mptcp_move_skbs(msk))
2152 if (sk->sk_state == TCP_CLOSE) {
2162 if (signal_pending(current)) {
2163 copied = sock_intr_errno(timeo);
2168 pr_debug("block timeout %ld", timeo);
2169 sk_wait_data(sk, &timeo, NULL);
2173 if (cmsg_flags && copied >= 0) {
2174 if (cmsg_flags & MPTCP_CMSG_TS)
2175 tcp_recv_timestamp(msg, sk, &tss);
2177 if (cmsg_flags & MPTCP_CMSG_INQ) {
2178 unsigned int inq = mptcp_inq_hint(sk);
2180 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2184 pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2185 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2186 skb_queue_empty(&msk->receive_queue), copied);
2187 if (!(flags & MSG_PEEK))
2188 mptcp_rcv_space_adjust(msk, copied);
2194 static void mptcp_retransmit_timer(struct timer_list *t)
2196 struct inet_connection_sock *icsk = from_timer(icsk, t,
2197 icsk_retransmit_timer);
2198 struct sock *sk = &icsk->icsk_inet.sk;
2199 struct mptcp_sock *msk = mptcp_sk(sk);
2202 if (!sock_owned_by_user(sk)) {
2203 /* we need a process context to retransmit */
2204 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2205 mptcp_schedule_work(sk);
2207 /* delegate our work to tcp_release_cb() */
2208 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2214 static void mptcp_timeout_timer(struct timer_list *t)
2216 struct sock *sk = from_timer(sk, t, sk_timer);
2218 mptcp_schedule_work(sk);
2222 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2225 * A backup subflow is returned only if that is the only kind available.
2227 static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2229 struct sock *backup = NULL, *pick = NULL;
2230 struct mptcp_subflow_context *subflow;
2231 int min_stale_count = INT_MAX;
2233 sock_owned_by_me((const struct sock *)msk);
2235 if (__mptcp_check_fallback(msk))
2238 mptcp_for_each_subflow(msk, subflow) {
2239 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2241 if (!__mptcp_subflow_active(subflow))
2244 /* still data outstanding at TCP level? skip this */
2245 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2246 mptcp_pm_subflow_chk_stale(msk, ssk);
2247 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2251 if (subflow->backup) {
2264 /* use backup only if there are no progresses anywhere */
2265 return min_stale_count > 1 ? backup : NULL;
2268 static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2271 iput(SOCK_INODE(msk->subflow));
2272 WRITE_ONCE(msk->subflow, NULL);
2276 bool __mptcp_retransmit_pending_data(struct sock *sk)
2278 struct mptcp_data_frag *cur, *rtx_head;
2279 struct mptcp_sock *msk = mptcp_sk(sk);
2281 if (__mptcp_check_fallback(mptcp_sk(sk)))
2284 if (tcp_rtx_and_write_queues_empty(sk))
2287 /* the closing socket has some data untransmitted and/or unacked:
2288 * some data in the mptcp rtx queue has not really xmitted yet.
2289 * keep it simple and re-inject the whole mptcp level rtx queue
2291 mptcp_data_lock(sk);
2292 __mptcp_clean_una_wakeup(sk);
2293 rtx_head = mptcp_rtx_head(sk);
2295 mptcp_data_unlock(sk);
2299 msk->recovery_snd_nxt = msk->snd_nxt;
2300 msk->recovery = true;
2301 mptcp_data_unlock(sk);
2303 msk->first_pending = rtx_head;
2306 /* be sure to clear the "sent status" on all re-injected fragments */
2307 list_for_each_entry(cur, &msk->rtx_queue, list) {
2308 if (!cur->already_sent)
2310 cur->already_sent = 0;
2316 /* flags for __mptcp_close_ssk() */
2317 #define MPTCP_CF_PUSH BIT(1)
2318 #define MPTCP_CF_FASTCLOSE BIT(2)
2320 /* subflow sockets can be either outgoing (connect) or incoming
2323 * Outgoing subflows use in-kernel sockets.
2324 * Incoming subflows do not have their own 'struct socket' allocated,
2325 * so we need to use tcp_close() after detaching them from the mptcp
2328 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2329 struct mptcp_subflow_context *subflow,
2332 struct mptcp_sock *msk = mptcp_sk(sk);
2333 bool dispose_it, need_push = false;
2335 /* If the first subflow moved to a close state before accept, e.g. due
2336 * to an incoming reset, mptcp either:
2337 * - if either the subflow or the msk are dead, destroy the context
2338 * (the subflow socket is deleted by inet_child_forget) and the msk
2339 * - otherwise do nothing at the moment and take action at accept and/or
2340 * listener shutdown - user-space must be able to accept() the closed
2343 if (msk->in_accept_queue && msk->first == ssk) {
2344 if (!sock_flag(sk, SOCK_DEAD) && !sock_flag(ssk, SOCK_DEAD))
2347 /* ensure later check in mptcp_worker() will dispose the msk */
2348 sock_set_flag(sk, SOCK_DEAD);
2349 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2350 mptcp_subflow_drop_ctx(ssk);
2354 dispose_it = !msk->subflow || ssk != msk->subflow->sk;
2356 list_del(&subflow->node);
2358 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2360 if (flags & MPTCP_CF_FASTCLOSE) {
2361 /* be sure to force the tcp_disconnect() path,
2362 * to generate the egress reset
2364 ssk->sk_lingertime = 0;
2365 sock_set_flag(ssk, SOCK_LINGER);
2366 subflow->send_fastclose = 1;
2369 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2371 tcp_disconnect(ssk, 0);
2372 msk->subflow->state = SS_UNCONNECTED;
2373 mptcp_subflow_ctx_reset(subflow);
2379 subflow->disposable = 1;
2381 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2382 * the ssk has been already destroyed, we just need to release the
2383 * reference owned by msk;
2385 if (!inet_csk(ssk)->icsk_ulp_ops) {
2386 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2387 kfree_rcu(subflow, rcu);
2389 /* otherwise tcp will dispose of the ssk and subflow ctx */
2390 if (ssk->sk_state == TCP_LISTEN) {
2391 tcp_set_state(ssk, TCP_CLOSE);
2392 mptcp_subflow_queue_clean(sk, ssk);
2393 inet_csk_listen_stop(ssk);
2396 __tcp_close(ssk, 0);
2398 /* close acquired an extra ref */
2407 if (ssk == msk->first)
2408 WRITE_ONCE(msk->first, NULL);
2411 if (ssk == msk->last_snd)
2412 msk->last_snd = NULL;
2415 __mptcp_push_pending(sk, 0);
2418 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2419 struct mptcp_subflow_context *subflow)
2421 if (sk->sk_state == TCP_ESTABLISHED)
2422 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2424 /* subflow aborted before reaching the fully_established status
2425 * attempt the creation of the next subflow
2427 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2429 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2432 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2437 static void __mptcp_close_subflow(struct sock *sk)
2439 struct mptcp_subflow_context *subflow, *tmp;
2440 struct mptcp_sock *msk = mptcp_sk(sk);
2444 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2445 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2447 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2450 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2451 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2454 mptcp_close_ssk(sk, ssk, subflow);
2459 static bool mptcp_should_close(const struct sock *sk)
2461 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2462 struct mptcp_subflow_context *subflow;
2464 if (delta >= TCP_TIMEWAIT_LEN || mptcp_sk(sk)->in_accept_queue)
2467 /* if all subflows are in closed status don't bother with additional
2470 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2471 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2478 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2480 struct mptcp_subflow_context *subflow, *tmp;
2481 struct sock *sk = &msk->sk.icsk_inet.sk;
2483 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2486 mptcp_token_destroy(msk);
2488 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2489 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2492 slow = lock_sock_fast(tcp_sk);
2493 if (tcp_sk->sk_state != TCP_CLOSE) {
2494 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2495 tcp_set_state(tcp_sk, TCP_CLOSE);
2497 unlock_sock_fast(tcp_sk, slow);
2500 /* Mirror the tcp_reset() error propagation */
2501 switch (sk->sk_state) {
2503 sk->sk_err = ECONNREFUSED;
2505 case TCP_CLOSE_WAIT:
2511 sk->sk_err = ECONNRESET;
2514 inet_sk_state_store(sk, TCP_CLOSE);
2515 sk->sk_shutdown = SHUTDOWN_MASK;
2516 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2517 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2519 /* the calling mptcp_worker will properly destroy the socket */
2520 if (sock_flag(sk, SOCK_DEAD))
2523 sk->sk_state_change(sk);
2524 sk_error_report(sk);
2527 static void __mptcp_retrans(struct sock *sk)
2529 struct mptcp_sock *msk = mptcp_sk(sk);
2530 struct mptcp_sendmsg_info info = {};
2531 struct mptcp_data_frag *dfrag;
2536 mptcp_clean_una_wakeup(sk);
2538 /* first check ssk: need to kick "stale" logic */
2539 ssk = mptcp_subflow_get_retrans(msk);
2540 dfrag = mptcp_rtx_head(sk);
2542 if (mptcp_data_fin_enabled(msk)) {
2543 struct inet_connection_sock *icsk = inet_csk(sk);
2545 icsk->icsk_retransmits++;
2546 mptcp_set_datafin_timeout(sk);
2547 mptcp_send_ack(msk);
2552 if (!mptcp_send_head(sk))
2563 /* limit retransmission to the bytes already sent on some subflows */
2565 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
2566 while (info.sent < info.limit) {
2567 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2571 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2576 dfrag->already_sent = max(dfrag->already_sent, info.sent);
2577 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2579 WRITE_ONCE(msk->allow_infinite_fallback, false);
2585 mptcp_check_and_set_pending(sk);
2587 if (!mptcp_timer_pending(sk))
2588 mptcp_reset_timer(sk);
2591 /* schedule the timeout timer for the relevant event: either close timeout
2592 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2594 void mptcp_reset_timeout(struct mptcp_sock *msk, unsigned long fail_tout)
2596 struct sock *sk = (struct sock *)msk;
2597 unsigned long timeout, close_timeout;
2599 if (!fail_tout && !sock_flag(sk, SOCK_DEAD))
2602 close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies + TCP_TIMEWAIT_LEN;
2604 /* the close timeout takes precedence on the fail one, and here at least one of
2607 timeout = sock_flag(sk, SOCK_DEAD) ? close_timeout : fail_tout;
2609 sk_reset_timer(sk, &sk->sk_timer, timeout);
2612 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2614 struct sock *ssk = msk->first;
2620 pr_debug("MP_FAIL doesn't respond, reset the subflow");
2622 slow = lock_sock_fast(ssk);
2623 mptcp_subflow_reset(ssk);
2624 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2625 unlock_sock_fast(ssk, slow);
2627 mptcp_reset_timeout(msk, 0);
2630 static void mptcp_do_fastclose(struct sock *sk)
2632 struct mptcp_subflow_context *subflow, *tmp;
2633 struct mptcp_sock *msk = mptcp_sk(sk);
2635 mptcp_for_each_subflow_safe(msk, subflow, tmp)
2636 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2637 subflow, MPTCP_CF_FASTCLOSE);
2640 static void mptcp_worker(struct work_struct *work)
2642 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2643 struct sock *sk = &msk->sk.icsk_inet.sk;
2644 unsigned long fail_tout;
2648 state = sk->sk_state;
2649 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2652 mptcp_check_data_fin_ack(sk);
2654 mptcp_check_fastclose(msk);
2656 mptcp_pm_nl_work(msk);
2658 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2659 mptcp_check_for_eof(msk);
2661 __mptcp_check_send_data_fin(sk);
2662 mptcp_check_data_fin(sk);
2664 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2665 __mptcp_close_subflow(sk);
2667 /* There is no point in keeping around an orphaned sk timedout or
2668 * closed, but we need the msk around to reply to incoming DATA_FIN,
2669 * even if it is orphaned and in FIN_WAIT2 state
2671 if (sock_flag(sk, SOCK_DEAD)) {
2672 if (mptcp_should_close(sk)) {
2673 inet_sk_state_store(sk, TCP_CLOSE);
2674 mptcp_do_fastclose(sk);
2676 if (sk->sk_state == TCP_CLOSE) {
2677 __mptcp_destroy_sock(sk);
2682 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2683 __mptcp_retrans(sk);
2685 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2686 if (fail_tout && time_after(jiffies, fail_tout))
2687 mptcp_mp_fail_no_response(msk);
2694 static int __mptcp_init_sock(struct sock *sk)
2696 struct mptcp_sock *msk = mptcp_sk(sk);
2698 INIT_LIST_HEAD(&msk->conn_list);
2699 INIT_LIST_HEAD(&msk->join_list);
2700 INIT_LIST_HEAD(&msk->rtx_queue);
2701 INIT_WORK(&msk->work, mptcp_worker);
2702 __skb_queue_head_init(&msk->receive_queue);
2703 msk->out_of_order_queue = RB_ROOT;
2704 msk->first_pending = NULL;
2705 msk->rmem_fwd_alloc = 0;
2706 WRITE_ONCE(msk->rmem_released, 0);
2707 msk->timer_ival = TCP_RTO_MIN;
2709 WRITE_ONCE(msk->first, NULL);
2710 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2711 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2712 WRITE_ONCE(msk->allow_infinite_fallback, true);
2713 msk->recovery = false;
2715 mptcp_pm_data_init(msk);
2717 /* re-use the csk retrans timer for MPTCP-level retrans */
2718 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2719 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2724 static void mptcp_ca_reset(struct sock *sk)
2726 struct inet_connection_sock *icsk = inet_csk(sk);
2728 tcp_assign_congestion_control(sk);
2729 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2731 /* no need to keep a reference to the ops, the name will suffice */
2732 tcp_cleanup_congestion_control(sk);
2733 icsk->icsk_ca_ops = NULL;
2736 static int mptcp_init_sock(struct sock *sk)
2738 struct net *net = sock_net(sk);
2741 ret = __mptcp_init_sock(sk);
2745 if (!mptcp_is_enabled(net))
2746 return -ENOPROTOOPT;
2748 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2751 ret = __mptcp_socket_create(mptcp_sk(sk));
2755 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2756 * propagate the correct value
2760 sk_sockets_allocated_inc(sk);
2761 sk->sk_rcvbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[1]);
2762 sk->sk_sndbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[1]);
2767 static void __mptcp_clear_xmit(struct sock *sk)
2769 struct mptcp_sock *msk = mptcp_sk(sk);
2770 struct mptcp_data_frag *dtmp, *dfrag;
2772 WRITE_ONCE(msk->first_pending, NULL);
2773 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2774 dfrag_clear(sk, dfrag);
2777 void mptcp_cancel_work(struct sock *sk)
2779 struct mptcp_sock *msk = mptcp_sk(sk);
2781 if (cancel_work_sync(&msk->work))
2785 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2789 switch (ssk->sk_state) {
2791 if (!(how & RCV_SHUTDOWN))
2795 tcp_disconnect(ssk, O_NONBLOCK);
2798 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2799 pr_debug("Fallback");
2800 ssk->sk_shutdown |= how;
2801 tcp_shutdown(ssk, how);
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 /* fallback socket will not get data_fin/ack, can move to the next
2862 if (__mptcp_check_fallback(msk)) {
2863 WRITE_ONCE(msk->snd_una, msk->write_seq);
2864 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2865 inet_sk_state_store(sk, TCP_CLOSE);
2866 mptcp_close_wake_up(sk);
2867 } else if (sk->sk_state == TCP_FIN_WAIT1) {
2868 inet_sk_state_store(sk, TCP_FIN_WAIT2);
2872 mptcp_for_each_subflow(msk, subflow) {
2873 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2875 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2879 static void __mptcp_wr_shutdown(struct sock *sk)
2881 struct mptcp_sock *msk = mptcp_sk(sk);
2883 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2884 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2885 !!mptcp_send_head(sk));
2887 /* will be ignored by fallback sockets */
2888 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2889 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2891 __mptcp_check_send_data_fin(sk);
2894 static void __mptcp_destroy_sock(struct sock *sk)
2896 struct mptcp_sock *msk = mptcp_sk(sk);
2898 pr_debug("msk=%p", msk);
2902 mptcp_stop_timer(sk);
2903 sk_stop_timer(sk, &sk->sk_timer);
2906 sk->sk_prot->destroy(sk);
2908 WARN_ON_ONCE(msk->rmem_fwd_alloc);
2909 WARN_ON_ONCE(msk->rmem_released);
2910 sk_stream_kill_queues(sk);
2911 xfrm_sk_free_policy(sk);
2913 sk_refcnt_debug_release(sk);
2917 void __mptcp_unaccepted_force_close(struct sock *sk)
2919 sock_set_flag(sk, SOCK_DEAD);
2920 inet_sk_state_store(sk, TCP_CLOSE);
2921 mptcp_do_fastclose(sk);
2922 __mptcp_destroy_sock(sk);
2925 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
2927 /* Concurrent splices from sk_receive_queue into receive_queue will
2928 * always show at least one non-empty queue when checked in this order.
2930 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
2931 skb_queue_empty_lockless(&msk->receive_queue))
2934 return EPOLLIN | EPOLLRDNORM;
2937 bool __mptcp_close(struct sock *sk, long timeout)
2939 struct mptcp_subflow_context *subflow;
2940 struct mptcp_sock *msk = mptcp_sk(sk);
2941 bool do_cancel_work = false;
2942 int subflows_alive = 0;
2944 sk->sk_shutdown = SHUTDOWN_MASK;
2946 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2947 inet_sk_state_store(sk, TCP_CLOSE);
2951 if (mptcp_check_readable(msk)) {
2952 /* the msk has read data, do the MPTCP equivalent of TCP reset */
2953 inet_sk_state_store(sk, TCP_CLOSE);
2954 mptcp_do_fastclose(sk);
2955 } else if (mptcp_close_state(sk)) {
2956 __mptcp_wr_shutdown(sk);
2959 sk_stream_wait_close(sk, timeout);
2962 /* orphan all the subflows */
2963 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
2964 mptcp_for_each_subflow(msk, subflow) {
2965 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2966 bool slow = lock_sock_fast_nested(ssk);
2968 subflows_alive += ssk->sk_state != TCP_CLOSE;
2970 /* since the close timeout takes precedence on the fail one,
2973 if (ssk == msk->first)
2974 subflow->fail_tout = 0;
2976 /* detach from the parent socket, but allow data_ready to
2977 * push incoming data into the mptcp stack, to properly ack it
2979 ssk->sk_socket = NULL;
2981 unlock_sock_fast(ssk, slow);
2985 /* all the subflows are closed, only timeout can change the msk
2986 * state, let's not keep resources busy for no reasons
2988 if (subflows_alive == 0)
2989 inet_sk_state_store(sk, TCP_CLOSE);
2992 pr_debug("msk=%p state=%d", sk, sk->sk_state);
2993 if (mptcp_sk(sk)->token)
2994 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
2996 if (sk->sk_state == TCP_CLOSE) {
2997 __mptcp_destroy_sock(sk);
2998 do_cancel_work = true;
3000 mptcp_reset_timeout(msk, 0);
3003 return do_cancel_work;
3006 static void mptcp_close(struct sock *sk, long timeout)
3008 bool do_cancel_work;
3012 do_cancel_work = __mptcp_close(sk, timeout);
3015 mptcp_cancel_work(sk);
3020 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3022 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3023 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3024 struct ipv6_pinfo *msk6 = inet6_sk(msk);
3026 msk->sk_v6_daddr = ssk->sk_v6_daddr;
3027 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3030 msk6->saddr = ssk6->saddr;
3031 msk6->flow_label = ssk6->flow_label;
3035 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3036 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3037 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3038 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3039 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3040 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3043 static int mptcp_disconnect(struct sock *sk, int flags)
3045 struct mptcp_sock *msk = mptcp_sk(sk);
3047 /* We are on the fastopen error path. We can't call straight into the
3048 * subflows cleanup code due to lock nesting (we are already under
3049 * msk->firstsocket lock). Do nothing and leave the cleanup to the
3052 if (msk->fastopening)
3055 inet_sk_state_store(sk, TCP_CLOSE);
3057 mptcp_stop_timer(sk);
3058 sk_stop_timer(sk, &sk->sk_timer);
3060 if (mptcp_sk(sk)->token)
3061 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
3063 /* msk->subflow is still intact, the following will not free the first
3066 mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3067 msk->last_snd = NULL;
3068 WRITE_ONCE(msk->flags, 0);
3070 msk->push_pending = 0;
3071 msk->recovery = false;
3072 msk->can_ack = false;
3073 msk->fully_established = false;
3074 msk->rcv_data_fin = false;
3075 msk->snd_data_fin_enable = false;
3076 msk->rcv_fastclose = false;
3077 msk->use_64bit_ack = false;
3078 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3079 mptcp_pm_data_reset(msk);
3082 sk->sk_shutdown = 0;
3083 sk_error_report(sk);
3087 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3088 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3090 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3092 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3096 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3097 const struct mptcp_options_received *mp_opt,
3099 struct request_sock *req)
3101 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3102 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3103 struct mptcp_sock *msk;
3109 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3110 if (nsk->sk_family == AF_INET6)
3111 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3114 __mptcp_init_sock(nsk);
3116 msk = mptcp_sk(nsk);
3117 msk->local_key = subflow_req->local_key;
3118 msk->token = subflow_req->token;
3119 WRITE_ONCE(msk->subflow, NULL);
3120 msk->in_accept_queue = 1;
3121 WRITE_ONCE(msk->fully_established, false);
3122 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3123 WRITE_ONCE(msk->csum_enabled, true);
3125 msk->write_seq = subflow_req->idsn + 1;
3126 msk->snd_nxt = msk->write_seq;
3127 msk->snd_una = msk->write_seq;
3128 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
3129 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3131 if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) {
3132 msk->can_ack = true;
3133 msk->remote_key = mp_opt->sndr_key;
3134 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
3136 WRITE_ONCE(msk->ack_seq, ack_seq);
3137 atomic64_set(&msk->rcv_wnd_sent, ack_seq);
3140 sock_reset_flag(nsk, SOCK_RCU_FREE);
3141 security_inet_csk_clone(nsk, req);
3143 /* this can't race with mptcp_close(), as the msk is
3144 * not yet exposted to user-space
3146 inet_sk_state_store(nsk, TCP_ESTABLISHED);
3148 /* The msk maintain a ref to each subflow in the connections list */
3149 WRITE_ONCE(msk->first, ssk);
3150 list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list);
3153 /* new mpc subflow takes ownership of the newly
3154 * created mptcp socket
3156 mptcp_token_accept(subflow_req, msk);
3158 /* set msk addresses early to ensure mptcp_pm_get_local_id()
3159 * uses the correct data
3161 mptcp_copy_inaddrs(nsk, ssk);
3162 mptcp_propagate_sndbuf(nsk, ssk);
3164 mptcp_rcv_space_init(msk, ssk);
3165 bh_unlock_sock(nsk);
3167 /* note: the newly allocated socket refcount is 2 now */
3171 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3173 const struct tcp_sock *tp = tcp_sk(ssk);
3175 msk->rcvq_space.copied = 0;
3176 msk->rcvq_space.rtt_us = 0;
3178 msk->rcvq_space.time = tp->tcp_mstamp;
3180 /* initial rcv_space offering made to peer */
3181 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3182 TCP_INIT_CWND * tp->advmss);
3183 if (msk->rcvq_space.space == 0)
3184 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3186 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3189 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
3192 struct mptcp_sock *msk = mptcp_sk(sk);
3193 struct socket *listener;
3196 listener = READ_ONCE(msk->subflow);
3197 if (WARN_ON_ONCE(!listener)) {
3202 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
3203 newsk = inet_csk_accept(listener->sk, flags, err, kern);
3207 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
3208 if (sk_is_mptcp(newsk)) {
3209 struct mptcp_subflow_context *subflow;
3210 struct sock *new_mptcp_sock;
3212 subflow = mptcp_subflow_ctx(newsk);
3213 new_mptcp_sock = subflow->conn;
3215 /* is_mptcp should be false if subflow->conn is missing, see
3216 * subflow_syn_recv_sock()
3218 if (WARN_ON_ONCE(!new_mptcp_sock)) {
3219 tcp_sk(newsk)->is_mptcp = 0;
3223 newsk = new_mptcp_sock;
3224 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3226 MPTCP_INC_STATS(sock_net(sk),
3227 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3231 newsk->sk_kern_sock = kern;
3235 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
3237 struct mptcp_subflow_context *subflow, *tmp;
3238 struct sock *sk = (struct sock *)msk;
3240 __mptcp_clear_xmit(sk);
3242 /* join list will be eventually flushed (with rst) at sock lock release time */
3243 mptcp_for_each_subflow_safe(msk, subflow, tmp)
3244 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3246 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3247 mptcp_data_lock(sk);
3248 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
3249 __skb_queue_purge(&sk->sk_receive_queue);
3250 skb_rbtree_purge(&msk->out_of_order_queue);
3251 mptcp_data_unlock(sk);
3253 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3254 * inet_sock_destruct() will dispose it
3256 sk->sk_forward_alloc += msk->rmem_fwd_alloc;
3257 msk->rmem_fwd_alloc = 0;
3258 mptcp_token_destroy(msk);
3259 mptcp_pm_free_anno_list(msk);
3260 mptcp_free_local_addr_list(msk);
3263 static void mptcp_destroy(struct sock *sk)
3265 struct mptcp_sock *msk = mptcp_sk(sk);
3267 /* clears msk->subflow, allowing the following to close
3268 * even the initial subflow
3270 mptcp_dispose_initial_subflow(msk);
3271 mptcp_destroy_common(msk, 0);
3272 sk_sockets_allocated_dec(sk);
3275 void __mptcp_data_acked(struct sock *sk)
3277 if (!sock_owned_by_user(sk))
3278 __mptcp_clean_una(sk);
3280 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3282 if (mptcp_pending_data_fin_ack(sk))
3283 mptcp_schedule_work(sk);
3286 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3288 if (!mptcp_send_head(sk))
3291 if (!sock_owned_by_user(sk)) {
3292 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
3294 if (xmit_ssk == ssk)
3295 __mptcp_subflow_push_pending(sk, ssk);
3297 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND);
3299 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3303 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3304 BIT(MPTCP_RETRANSMIT) | \
3305 BIT(MPTCP_FLUSH_JOIN_LIST))
3307 /* processes deferred events and flush wmem */
3308 static void mptcp_release_cb(struct sock *sk)
3309 __must_hold(&sk->sk_lock.slock)
3311 struct mptcp_sock *msk = mptcp_sk(sk);
3314 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED) |
3319 /* the following actions acquire the subflow socket lock
3321 * 1) can't be invoked in atomic scope
3322 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3323 * datapath acquires the msk socket spinlock while helding
3324 * the subflow socket lock
3326 msk->push_pending = 0;
3327 msk->cb_flags &= ~flags;
3328 spin_unlock_bh(&sk->sk_lock.slock);
3329 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3330 __mptcp_flush_join_list(sk);
3331 if (flags & BIT(MPTCP_PUSH_PENDING))
3332 __mptcp_push_pending(sk, 0);
3333 if (flags & BIT(MPTCP_RETRANSMIT))
3334 __mptcp_retrans(sk);
3337 spin_lock_bh(&sk->sk_lock.slock);
3340 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3341 __mptcp_clean_una_wakeup(sk);
3342 if (unlikely(&msk->cb_flags)) {
3343 /* be sure to set the current sk state before tacking actions
3344 * depending on sk_state, that is processing MPTCP_ERROR_REPORT
3346 if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags))
3347 __mptcp_set_connected(sk);
3348 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3349 __mptcp_error_report(sk);
3350 if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags))
3351 msk->last_snd = NULL;
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);
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);
3420 struct socket *ssock;
3422 ssock = msk->subflow;
3423 pr_debug("msk=%p, subflow=%p", msk, ssock);
3424 if (WARN_ON_ONCE(!ssock))
3427 return inet_csk_get_port(ssock->sk, snum);
3430 void mptcp_finish_connect(struct sock *ssk)
3432 struct mptcp_subflow_context *subflow;
3433 struct mptcp_sock *msk;
3437 subflow = mptcp_subflow_ctx(ssk);
3441 pr_debug("msk=%p, token=%u", sk, subflow->token);
3443 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
3445 subflow->map_seq = ack_seq;
3446 subflow->map_subflow_seq = 1;
3448 /* the socket is not connected yet, no msk/subflow ops can access/race
3449 * accessing the field below
3451 WRITE_ONCE(msk->remote_key, subflow->remote_key);
3452 WRITE_ONCE(msk->local_key, subflow->local_key);
3453 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3454 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3455 WRITE_ONCE(msk->ack_seq, ack_seq);
3456 WRITE_ONCE(msk->can_ack, 1);
3457 WRITE_ONCE(msk->snd_una, msk->write_seq);
3458 atomic64_set(&msk->rcv_wnd_sent, ack_seq);
3460 mptcp_pm_new_connection(msk, ssk, 0);
3462 mptcp_rcv_space_init(msk, ssk);
3465 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3467 write_lock_bh(&sk->sk_callback_lock);
3468 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3469 sk_set_socket(sk, parent);
3470 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3471 write_unlock_bh(&sk->sk_callback_lock);
3474 bool mptcp_finish_join(struct sock *ssk)
3476 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3477 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3478 struct sock *parent = (void *)msk;
3481 pr_debug("msk=%p, subflow=%p", msk, subflow);
3483 /* mptcp socket already closing? */
3484 if (!mptcp_is_fully_established(parent)) {
3485 subflow->reset_reason = MPTCP_RST_EMPTCP;
3489 if (!list_empty(&subflow->node))
3492 if (!mptcp_pm_allow_new_subflow(msk))
3493 goto err_prohibited;
3495 /* active connections are already on conn_list.
3496 * If we can't acquire msk socket lock here, let the release callback
3499 mptcp_data_lock(parent);
3500 if (!sock_owned_by_user(parent)) {
3501 ret = __mptcp_finish_join(msk, ssk);
3504 list_add_tail(&subflow->node, &msk->conn_list);
3508 list_add_tail(&subflow->node, &msk->join_list);
3509 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3511 mptcp_data_unlock(parent);
3515 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3519 subflow->map_seq = READ_ONCE(msk->ack_seq);
3520 WRITE_ONCE(msk->allow_infinite_fallback, false);
3523 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
3527 static void mptcp_shutdown(struct sock *sk, int how)
3529 pr_debug("sk=%p, how=%d", sk, how);
3531 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3532 __mptcp_wr_shutdown(sk);
3535 static int mptcp_forward_alloc_get(const struct sock *sk)
3537 return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3540 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3542 const struct sock *sk = (void *)msk;
3545 if (sk->sk_state == TCP_LISTEN)
3548 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3551 delta = msk->write_seq - v;
3552 if (__mptcp_check_fallback(msk) && msk->first) {
3553 struct tcp_sock *tp = tcp_sk(msk->first);
3555 /* the first subflow is disconnected after close - see
3556 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3557 * so ignore that status, too.
3559 if (!((1 << msk->first->sk_state) &
3560 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3561 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3563 if (delta > INT_MAX)
3569 static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3571 struct mptcp_sock *msk = mptcp_sk(sk);
3577 if (sk->sk_state == TCP_LISTEN)
3581 __mptcp_move_skbs(msk);
3582 answ = mptcp_inq_hint(sk);
3586 slow = lock_sock_fast(sk);
3587 answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3588 unlock_sock_fast(sk, slow);
3591 slow = lock_sock_fast(sk);
3592 answ = mptcp_ioctl_outq(msk, msk->snd_nxt);
3593 unlock_sock_fast(sk, slow);
3596 return -ENOIOCTLCMD;
3599 return put_user(answ, (int __user *)arg);
3602 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3603 struct mptcp_subflow_context *subflow)
3605 subflow->request_mptcp = 0;
3606 __mptcp_do_fallback(msk);
3609 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3611 struct mptcp_subflow_context *subflow;
3612 struct mptcp_sock *msk = mptcp_sk(sk);
3613 struct socket *ssock;
3616 ssock = __mptcp_nmpc_socket(msk);
3620 mptcp_token_destroy(msk);
3621 inet_sk_state_store(sk, TCP_SYN_SENT);
3622 subflow = mptcp_subflow_ctx(ssock->sk);
3623 #ifdef CONFIG_TCP_MD5SIG
3624 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3627 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
3628 mptcp_subflow_early_fallback(msk, subflow);
3630 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3631 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
3632 mptcp_subflow_early_fallback(msk, subflow);
3634 if (likely(!__mptcp_check_fallback(msk)))
3635 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3637 /* if reaching here via the fastopen/sendmsg path, the caller already
3638 * acquired the subflow socket lock, too.
3640 if (msk->fastopening)
3641 err = __inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags, 1);
3643 err = inet_stream_connect(ssock, uaddr, addr_len, msk->connect_flags);
3644 inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect;
3646 /* on successful connect, the msk state will be moved to established by
3647 * subflow_finish_connect()
3649 if (unlikely(err && err != -EINPROGRESS)) {
3650 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk));
3654 mptcp_copy_inaddrs(sk, ssock->sk);
3656 /* unblocking connect, mptcp-level inet_stream_connect will error out
3657 * without changing the socket state, update it here.
3659 if (err == -EINPROGRESS)
3660 sk->sk_socket->state = ssock->state;
3664 static struct proto mptcp_prot = {
3666 .owner = THIS_MODULE,
3667 .init = mptcp_init_sock,
3668 .connect = mptcp_connect,
3669 .disconnect = mptcp_disconnect,
3670 .close = mptcp_close,
3671 .accept = mptcp_accept,
3672 .setsockopt = mptcp_setsockopt,
3673 .getsockopt = mptcp_getsockopt,
3674 .shutdown = mptcp_shutdown,
3675 .destroy = mptcp_destroy,
3676 .sendmsg = mptcp_sendmsg,
3677 .ioctl = mptcp_ioctl,
3678 .recvmsg = mptcp_recvmsg,
3679 .release_cb = mptcp_release_cb,
3681 .unhash = mptcp_unhash,
3682 .get_port = mptcp_get_port,
3683 .forward_alloc_get = mptcp_forward_alloc_get,
3684 .sockets_allocated = &mptcp_sockets_allocated,
3686 .memory_allocated = &tcp_memory_allocated,
3687 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3689 .memory_pressure = &tcp_memory_pressure,
3690 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3691 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3692 .sysctl_mem = sysctl_tcp_mem,
3693 .obj_size = sizeof(struct mptcp_sock),
3694 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3695 .no_autobind = true,
3698 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3700 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3701 struct socket *ssock;
3704 lock_sock(sock->sk);
3705 ssock = __mptcp_nmpc_socket(msk);
3711 err = ssock->ops->bind(ssock, uaddr, addr_len);
3713 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3716 release_sock(sock->sk);
3720 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
3721 int addr_len, int flags)
3725 lock_sock(sock->sk);
3726 mptcp_sk(sock->sk)->connect_flags = flags;
3727 ret = __inet_stream_connect(sock, uaddr, addr_len, flags, 0);
3728 release_sock(sock->sk);
3732 static int mptcp_listen(struct socket *sock, int backlog)
3734 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3735 struct socket *ssock;
3738 pr_debug("msk=%p", msk);
3740 lock_sock(sock->sk);
3741 ssock = __mptcp_nmpc_socket(msk);
3747 mptcp_token_destroy(msk);
3748 inet_sk_state_store(sock->sk, TCP_LISTEN);
3749 sock_set_flag(sock->sk, SOCK_RCU_FREE);
3751 err = ssock->ops->listen(ssock, backlog);
3752 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3754 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3757 release_sock(sock->sk);
3761 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3762 int flags, bool kern)
3764 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3765 struct socket *ssock;
3768 pr_debug("msk=%p", msk);
3770 /* Buggy applications can call accept on socket states other then LISTEN
3771 * but no need to allocate the first subflow just to error out.
3773 ssock = READ_ONCE(msk->subflow);
3777 err = ssock->ops->accept(sock, newsock, flags, kern);
3778 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
3779 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3780 struct mptcp_subflow_context *subflow;
3781 struct sock *newsk = newsock->sk;
3783 msk->in_accept_queue = 0;
3787 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3788 * This is needed so NOSPACE flag can be set from tcp stack.
3790 mptcp_for_each_subflow(msk, subflow) {
3791 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3793 if (!ssk->sk_socket)
3794 mptcp_sock_graft(ssk, newsock);
3797 /* Do late cleanup for the first subflow as necessary. Also
3798 * deal with bad peers not doing a complete shutdown.
3801 unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
3802 __mptcp_close_ssk(newsk, msk->first,
3803 mptcp_subflow_ctx(msk->first), 0);
3804 if (unlikely(list_empty(&msk->conn_list)))
3805 inet_sk_state_store(newsk, TCP_CLOSE);
3808 release_sock(newsk);
3814 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3816 struct sock *sk = (struct sock *)msk;
3818 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
3819 return EPOLLOUT | EPOLLWRNORM;
3821 if (sk_stream_is_writeable(sk))
3822 return EPOLLOUT | EPOLLWRNORM;
3824 mptcp_set_nospace(sk);
3825 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3826 if (sk_stream_is_writeable(sk))
3827 return EPOLLOUT | EPOLLWRNORM;
3832 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3833 struct poll_table_struct *wait)
3835 struct sock *sk = sock->sk;
3836 struct mptcp_sock *msk;
3841 sock_poll_wait(file, sock, wait);
3843 state = inet_sk_state_load(sk);
3844 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
3845 if (state == TCP_LISTEN) {
3846 struct socket *ssock = READ_ONCE(msk->subflow);
3848 if (WARN_ON_ONCE(!ssock || !ssock->sk))
3851 return inet_csk_listen_poll(ssock->sk);
3854 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3855 mask |= mptcp_check_readable(msk);
3856 mask |= mptcp_check_writeable(msk);
3857 } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) {
3858 /* cf tcp_poll() note about TFO */
3859 mask |= EPOLLOUT | EPOLLWRNORM;
3861 if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3863 if (sk->sk_shutdown & RCV_SHUTDOWN)
3864 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3866 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
3874 static const struct proto_ops mptcp_stream_ops = {
3876 .owner = THIS_MODULE,
3877 .release = inet_release,
3879 .connect = mptcp_stream_connect,
3880 .socketpair = sock_no_socketpair,
3881 .accept = mptcp_stream_accept,
3882 .getname = inet_getname,
3884 .ioctl = inet_ioctl,
3885 .gettstamp = sock_gettstamp,
3886 .listen = mptcp_listen,
3887 .shutdown = inet_shutdown,
3888 .setsockopt = sock_common_setsockopt,
3889 .getsockopt = sock_common_getsockopt,
3890 .sendmsg = inet_sendmsg,
3891 .recvmsg = inet_recvmsg,
3892 .mmap = sock_no_mmap,
3893 .sendpage = inet_sendpage,
3896 static struct inet_protosw mptcp_protosw = {
3897 .type = SOCK_STREAM,
3898 .protocol = IPPROTO_MPTCP,
3899 .prot = &mptcp_prot,
3900 .ops = &mptcp_stream_ops,
3901 .flags = INET_PROTOSW_ICSK,
3904 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3906 struct mptcp_delegated_action *delegated;
3907 struct mptcp_subflow_context *subflow;
3910 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3911 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3912 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3914 bh_lock_sock_nested(ssk);
3915 if (!sock_owned_by_user(ssk) &&
3916 mptcp_subflow_has_delegated_action(subflow))
3917 mptcp_subflow_process_delegated(ssk);
3918 /* ... elsewhere tcp_release_cb_override already processed
3919 * the action or will do at next release_sock().
3920 * In both case must dequeue the subflow here - on the same
3921 * CPU that scheduled it.
3923 bh_unlock_sock(ssk);
3926 if (++work_done == budget)
3930 /* always provide a 0 'work_done' argument, so that napi_complete_done
3931 * will not try accessing the NULL napi->dev ptr
3933 napi_complete_done(napi, 0);
3937 void __init mptcp_proto_init(void)
3939 struct mptcp_delegated_action *delegated;
3942 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
3944 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3945 panic("Failed to allocate MPTCP pcpu counter\n");
3947 init_dummy_netdev(&mptcp_napi_dev);
3948 for_each_possible_cpu(cpu) {
3949 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3950 INIT_LIST_HEAD(&delegated->head);
3951 netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
3953 napi_enable(&delegated->napi);
3956 mptcp_subflow_init();
3960 if (proto_register(&mptcp_prot, 1) != 0)
3961 panic("Failed to register MPTCP proto.\n");
3963 inet_register_protosw(&mptcp_protosw);
3965 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
3968 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3969 static const struct proto_ops mptcp_v6_stream_ops = {
3971 .owner = THIS_MODULE,
3972 .release = inet6_release,
3974 .connect = mptcp_stream_connect,
3975 .socketpair = sock_no_socketpair,
3976 .accept = mptcp_stream_accept,
3977 .getname = inet6_getname,
3979 .ioctl = inet6_ioctl,
3980 .gettstamp = sock_gettstamp,
3981 .listen = mptcp_listen,
3982 .shutdown = inet_shutdown,
3983 .setsockopt = sock_common_setsockopt,
3984 .getsockopt = sock_common_getsockopt,
3985 .sendmsg = inet6_sendmsg,
3986 .recvmsg = inet6_recvmsg,
3987 .mmap = sock_no_mmap,
3988 .sendpage = inet_sendpage,
3989 #ifdef CONFIG_COMPAT
3990 .compat_ioctl = inet6_compat_ioctl,
3994 static struct proto mptcp_v6_prot;
3996 static struct inet_protosw mptcp_v6_protosw = {
3997 .type = SOCK_STREAM,
3998 .protocol = IPPROTO_MPTCP,
3999 .prot = &mptcp_v6_prot,
4000 .ops = &mptcp_v6_stream_ops,
4001 .flags = INET_PROTOSW_ICSK,
4004 int __init mptcp_proto_v6_init(void)
4008 mptcp_v6_prot = mptcp_prot;
4009 strcpy(mptcp_v6_prot.name, "MPTCPv6");
4010 mptcp_v6_prot.slab = NULL;
4011 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4013 err = proto_register(&mptcp_v6_prot, 1);
4017 err = inet6_register_protosw(&mptcp_v6_protosw);
4019 proto_unregister(&mptcp_v6_prot);