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>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/mptcp.h>
31 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
33 struct mptcp_sock msk;
45 #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
48 MPTCP_CMSG_TS = BIT(0),
51 static struct percpu_counter mptcp_sockets_allocated;
53 static void __mptcp_destroy_sock(struct sock *sk);
54 static void __mptcp_check_send_data_fin(struct sock *sk);
56 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
57 static struct net_device mptcp_napi_dev;
59 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
60 * completed yet or has failed, return the subflow socket.
61 * Otherwise return NULL.
63 struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
65 if (!msk->subflow || READ_ONCE(msk->can_ack))
71 /* Returns end sequence number of the receiver's advertised window */
72 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
74 return READ_ONCE(msk->wnd_end);
77 static bool mptcp_is_tcpsk(struct sock *sk)
79 struct socket *sock = sk->sk_socket;
81 if (unlikely(sk->sk_prot == &tcp_prot)) {
82 /* we are being invoked after mptcp_accept() has
83 * accepted a non-mp-capable flow: sk is a tcp_sk,
86 * Hand the socket over to tcp so all further socket ops
89 sock->ops = &inet_stream_ops;
91 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
92 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
93 sock->ops = &inet6_stream_ops;
101 static int __mptcp_socket_create(struct mptcp_sock *msk)
103 struct mptcp_subflow_context *subflow;
104 struct sock *sk = (struct sock *)msk;
105 struct socket *ssock;
108 err = mptcp_subflow_create_socket(sk, &ssock);
112 msk->first = ssock->sk;
113 msk->subflow = ssock;
114 subflow = mptcp_subflow_ctx(ssock->sk);
115 list_add(&subflow->node, &msk->conn_list);
116 sock_hold(ssock->sk);
117 subflow->request_mptcp = 1;
118 mptcp_sock_graft(msk->first, sk->sk_socket);
123 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
125 sk_drops_add(sk, skb);
129 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
130 struct sk_buff *from)
135 if (MPTCP_SKB_CB(from)->offset ||
136 !skb_try_coalesce(to, from, &fragstolen, &delta))
139 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
140 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
141 to->len, MPTCP_SKB_CB(from)->end_seq);
142 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
143 kfree_skb_partial(from, fragstolen);
144 atomic_add(delta, &sk->sk_rmem_alloc);
145 sk_mem_charge(sk, delta);
149 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
150 struct sk_buff *from)
152 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
155 return mptcp_try_coalesce((struct sock *)msk, to, from);
158 /* "inspired" by tcp_data_queue_ofo(), main differences:
160 * - don't cope with sacks
162 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
164 struct sock *sk = (struct sock *)msk;
165 struct rb_node **p, *parent;
166 u64 seq, end_seq, max_seq;
167 struct sk_buff *skb1;
169 seq = MPTCP_SKB_CB(skb)->map_seq;
170 end_seq = MPTCP_SKB_CB(skb)->end_seq;
171 max_seq = READ_ONCE(msk->rcv_wnd_sent);
173 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
174 RB_EMPTY_ROOT(&msk->out_of_order_queue));
175 if (after64(end_seq, max_seq)) {
178 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
179 (unsigned long long)end_seq - (unsigned long)max_seq,
180 (unsigned long long)msk->rcv_wnd_sent);
181 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
185 p = &msk->out_of_order_queue.rb_node;
186 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
187 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
188 rb_link_node(&skb->rbnode, NULL, p);
189 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
190 msk->ooo_last_skb = skb;
194 /* with 2 subflows, adding at end of ooo queue is quite likely
195 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
197 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
198 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
199 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
203 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
204 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
205 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
206 parent = &msk->ooo_last_skb->rbnode;
207 p = &parent->rb_right;
211 /* Find place to insert this segment. Handle overlaps on the way. */
215 skb1 = rb_to_skb(parent);
216 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
217 p = &parent->rb_left;
220 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
221 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
222 /* All the bits are present. Drop. */
224 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
227 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
231 * continue traversing
234 /* skb's seq == skb1's seq and skb covers skb1.
235 * Replace skb1 with skb.
237 rb_replace_node(&skb1->rbnode, &skb->rbnode,
238 &msk->out_of_order_queue);
239 mptcp_drop(sk, skb1);
240 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
243 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
244 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
247 p = &parent->rb_right;
251 /* Insert segment into RB tree. */
252 rb_link_node(&skb->rbnode, parent, p);
253 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
256 /* Remove other segments covered by skb. */
257 while ((skb1 = skb_rb_next(skb)) != NULL) {
258 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
260 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
261 mptcp_drop(sk, skb1);
262 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
264 /* If there is no skb after us, we are the last_skb ! */
266 msk->ooo_last_skb = skb;
270 skb_set_owner_r(skb, sk);
273 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
274 struct sk_buff *skb, unsigned int offset,
277 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
278 struct sock *sk = (struct sock *)msk;
279 struct sk_buff *tail;
282 __skb_unlink(skb, &ssk->sk_receive_queue);
287 /* try to fetch required memory from subflow */
288 if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
289 int amount = sk_mem_pages(skb->truesize) << SK_MEM_QUANTUM_SHIFT;
291 if (ssk->sk_forward_alloc < amount)
294 ssk->sk_forward_alloc -= amount;
295 sk->sk_forward_alloc += amount;
298 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
300 /* the skb map_seq accounts for the skb offset:
301 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
304 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
305 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
306 MPTCP_SKB_CB(skb)->offset = offset;
307 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
309 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
311 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
312 tail = skb_peek_tail(&sk->sk_receive_queue);
313 if (tail && mptcp_try_coalesce(sk, tail, skb))
316 skb_set_owner_r(skb, sk);
317 __skb_queue_tail(&sk->sk_receive_queue, skb);
319 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
320 mptcp_data_queue_ofo(msk, skb);
324 /* old data, keep it simple and drop the whole pkt, sender
325 * will retransmit as needed, if needed.
327 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
333 static void mptcp_stop_timer(struct sock *sk)
335 struct inet_connection_sock *icsk = inet_csk(sk);
337 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
338 mptcp_sk(sk)->timer_ival = 0;
341 static void mptcp_close_wake_up(struct sock *sk)
343 if (sock_flag(sk, SOCK_DEAD))
346 sk->sk_state_change(sk);
347 if (sk->sk_shutdown == SHUTDOWN_MASK ||
348 sk->sk_state == TCP_CLOSE)
349 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
351 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
354 static bool mptcp_pending_data_fin_ack(struct sock *sk)
356 struct mptcp_sock *msk = mptcp_sk(sk);
358 return !__mptcp_check_fallback(msk) &&
359 ((1 << sk->sk_state) &
360 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
361 msk->write_seq == READ_ONCE(msk->snd_una);
364 static void mptcp_check_data_fin_ack(struct sock *sk)
366 struct mptcp_sock *msk = mptcp_sk(sk);
368 /* Look for an acknowledged DATA_FIN */
369 if (mptcp_pending_data_fin_ack(sk)) {
370 WRITE_ONCE(msk->snd_data_fin_enable, 0);
372 switch (sk->sk_state) {
374 inet_sk_state_store(sk, TCP_FIN_WAIT2);
378 inet_sk_state_store(sk, TCP_CLOSE);
382 mptcp_close_wake_up(sk);
386 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
388 struct mptcp_sock *msk = mptcp_sk(sk);
390 if (READ_ONCE(msk->rcv_data_fin) &&
391 ((1 << sk->sk_state) &
392 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
393 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
395 if (msk->ack_seq == rcv_data_fin_seq) {
397 *seq = rcv_data_fin_seq;
406 static void mptcp_set_datafin_timeout(const struct sock *sk)
408 struct inet_connection_sock *icsk = inet_csk(sk);
410 mptcp_sk(sk)->timer_ival = min(TCP_RTO_MAX,
411 TCP_RTO_MIN << icsk->icsk_retransmits);
414 static void __mptcp_set_timeout(struct sock *sk, long tout)
416 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
419 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
421 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
423 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
424 inet_csk(ssk)->icsk_timeout - jiffies : 0;
427 static void mptcp_set_timeout(struct sock *sk)
429 struct mptcp_subflow_context *subflow;
432 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
433 tout = max(tout, mptcp_timeout_from_subflow(subflow));
434 __mptcp_set_timeout(sk, tout);
437 static bool tcp_can_send_ack(const struct sock *ssk)
439 return !((1 << inet_sk_state_load(ssk)) &
440 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
443 void mptcp_subflow_send_ack(struct sock *ssk)
447 slow = lock_sock_fast(ssk);
448 if (tcp_can_send_ack(ssk))
450 unlock_sock_fast(ssk, slow);
453 static void mptcp_send_ack(struct mptcp_sock *msk)
455 struct mptcp_subflow_context *subflow;
457 mptcp_for_each_subflow(msk, subflow)
458 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
461 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
465 slow = lock_sock_fast(ssk);
466 if (tcp_can_send_ack(ssk))
467 tcp_cleanup_rbuf(ssk, 1);
468 unlock_sock_fast(ssk, slow);
471 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
473 const struct inet_connection_sock *icsk = inet_csk(ssk);
474 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
475 const struct tcp_sock *tp = tcp_sk(ssk);
477 return (ack_pending & ICSK_ACK_SCHED) &&
478 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
479 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
480 (rx_empty && ack_pending &
481 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
484 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
486 int old_space = READ_ONCE(msk->old_wspace);
487 struct mptcp_subflow_context *subflow;
488 struct sock *sk = (struct sock *)msk;
489 int space = __mptcp_space(sk);
490 bool cleanup, rx_empty;
492 cleanup = (space > 0) && (space >= (old_space << 1));
493 rx_empty = !__mptcp_rmem(sk);
495 mptcp_for_each_subflow(msk, subflow) {
496 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
498 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
499 mptcp_subflow_cleanup_rbuf(ssk);
503 static bool mptcp_check_data_fin(struct sock *sk)
505 struct mptcp_sock *msk = mptcp_sk(sk);
506 u64 rcv_data_fin_seq;
509 if (__mptcp_check_fallback(msk))
512 /* Need to ack a DATA_FIN received from a peer while this side
513 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
514 * msk->rcv_data_fin was set when parsing the incoming options
515 * at the subflow level and the msk lock was not held, so this
516 * is the first opportunity to act on the DATA_FIN and change
519 * If we are caught up to the sequence number of the incoming
520 * DATA_FIN, send the DATA_ACK now and do state transition. If
521 * not caught up, do nothing and let the recv code send DATA_ACK
525 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
526 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
527 WRITE_ONCE(msk->rcv_data_fin, 0);
529 sk->sk_shutdown |= RCV_SHUTDOWN;
530 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
531 set_bit(MPTCP_DATA_READY, &msk->flags);
533 switch (sk->sk_state) {
534 case TCP_ESTABLISHED:
535 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
538 inet_sk_state_store(sk, TCP_CLOSING);
541 inet_sk_state_store(sk, TCP_CLOSE);
544 /* Other states not expected */
551 mptcp_close_wake_up(sk);
556 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
560 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
561 struct sock *sk = (struct sock *)msk;
562 unsigned int moved = 0;
563 bool more_data_avail;
568 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
570 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
571 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
573 if (unlikely(ssk_rbuf > sk_rbuf)) {
574 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
579 pr_debug("msk=%p ssk=%p", msk, ssk);
582 u32 map_remaining, offset;
583 u32 seq = tp->copied_seq;
587 /* try to move as much data as available */
588 map_remaining = subflow->map_data_len -
589 mptcp_subflow_get_map_offset(subflow);
591 skb = skb_peek(&ssk->sk_receive_queue);
593 /* if no data is found, a racing workqueue/recvmsg
594 * already processed the new data, stop here or we
595 * can enter an infinite loop
602 if (__mptcp_check_fallback(msk)) {
603 /* if we are running under the workqueue, TCP could have
604 * collapsed skbs between dummy map creation and now
605 * be sure to adjust the size
607 map_remaining = skb->len;
608 subflow->map_data_len = skb->len;
611 offset = seq - TCP_SKB_CB(skb)->seq;
612 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
618 if (offset < skb->len) {
619 size_t len = skb->len - offset;
624 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
628 if (WARN_ON_ONCE(map_remaining < len))
632 sk_eat_skb(ssk, skb);
636 WRITE_ONCE(tp->copied_seq, seq);
637 more_data_avail = mptcp_subflow_data_available(ssk);
639 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
643 } while (more_data_avail);
649 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
651 struct sock *sk = (struct sock *)msk;
652 struct sk_buff *skb, *tail;
657 p = rb_first(&msk->out_of_order_queue);
658 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
661 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
665 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
667 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
670 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
674 end_seq = MPTCP_SKB_CB(skb)->end_seq;
675 tail = skb_peek_tail(&sk->sk_receive_queue);
676 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
677 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
679 /* skip overlapping data, if any */
680 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
681 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
683 MPTCP_SKB_CB(skb)->offset += delta;
684 __skb_queue_tail(&sk->sk_receive_queue, skb);
686 msk->ack_seq = end_seq;
692 /* In most cases we will be able to lock the mptcp socket. If its already
693 * owned, we need to defer to the work queue to avoid ABBA deadlock.
695 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
697 struct sock *sk = (struct sock *)msk;
698 unsigned int moved = 0;
700 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
701 __mptcp_ofo_queue(msk);
702 if (unlikely(ssk->sk_err)) {
703 if (!sock_owned_by_user(sk))
704 __mptcp_error_report(sk);
706 set_bit(MPTCP_ERROR_REPORT, &msk->flags);
709 /* If the moves have caught up with the DATA_FIN sequence number
710 * it's time to ack the DATA_FIN and change socket state, but
711 * this is not a good place to change state. Let the workqueue
714 if (mptcp_pending_data_fin(sk, NULL))
715 mptcp_schedule_work(sk);
719 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
721 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
722 struct mptcp_sock *msk = mptcp_sk(sk);
723 int sk_rbuf, ssk_rbuf;
725 /* The peer can send data while we are shutting down this
726 * subflow at msk destruction time, but we must avoid enqueuing
727 * more data to the msk receive queue
729 if (unlikely(subflow->disposable))
732 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
733 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
734 if (unlikely(ssk_rbuf > sk_rbuf))
737 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
738 if (__mptcp_rmem(sk) > sk_rbuf) {
739 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
743 /* Wake-up the reader only for in-sequence data */
745 if (move_skbs_to_msk(msk, ssk)) {
746 set_bit(MPTCP_DATA_READY, &msk->flags);
747 sk->sk_data_ready(sk);
749 mptcp_data_unlock(sk);
752 static bool mptcp_do_flush_join_list(struct mptcp_sock *msk)
754 struct mptcp_subflow_context *subflow;
757 if (likely(list_empty(&msk->join_list)))
760 spin_lock_bh(&msk->join_list_lock);
761 list_for_each_entry(subflow, &msk->join_list, node) {
762 u32 sseq = READ_ONCE(subflow->setsockopt_seq);
764 mptcp_propagate_sndbuf((struct sock *)msk, mptcp_subflow_tcp_sock(subflow));
765 if (READ_ONCE(msk->setsockopt_seq) != sseq)
768 list_splice_tail_init(&msk->join_list, &msk->conn_list);
769 spin_unlock_bh(&msk->join_list_lock);
774 void __mptcp_flush_join_list(struct mptcp_sock *msk)
776 if (likely(!mptcp_do_flush_join_list(msk)))
779 if (!test_and_set_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags))
780 mptcp_schedule_work((struct sock *)msk);
783 static void mptcp_flush_join_list(struct mptcp_sock *msk)
785 bool sync_needed = test_and_clear_bit(MPTCP_WORK_SYNC_SETSOCKOPT, &msk->flags);
789 if (!mptcp_do_flush_join_list(msk) && !sync_needed)
792 mptcp_sockopt_sync_all(msk);
795 static bool mptcp_timer_pending(struct sock *sk)
797 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
800 static void mptcp_reset_timer(struct sock *sk)
802 struct inet_connection_sock *icsk = inet_csk(sk);
805 /* prevent rescheduling on close */
806 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
809 tout = mptcp_sk(sk)->timer_ival;
810 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
813 bool mptcp_schedule_work(struct sock *sk)
815 if (inet_sk_state_load(sk) != TCP_CLOSE &&
816 schedule_work(&mptcp_sk(sk)->work)) {
817 /* each subflow already holds a reference to the sk, and the
818 * workqueue is invoked by a subflow, so sk can't go away here.
826 void mptcp_subflow_eof(struct sock *sk)
828 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
829 mptcp_schedule_work(sk);
832 static void mptcp_check_for_eof(struct mptcp_sock *msk)
834 struct mptcp_subflow_context *subflow;
835 struct sock *sk = (struct sock *)msk;
838 mptcp_for_each_subflow(msk, subflow)
839 receivers += !subflow->rx_eof;
843 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
844 /* hopefully temporary hack: propagate shutdown status
845 * to msk, when all subflows agree on it
847 sk->sk_shutdown |= RCV_SHUTDOWN;
849 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
850 set_bit(MPTCP_DATA_READY, &msk->flags);
851 sk->sk_data_ready(sk);
854 switch (sk->sk_state) {
855 case TCP_ESTABLISHED:
856 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
859 inet_sk_state_store(sk, TCP_CLOSING);
862 inet_sk_state_store(sk, TCP_CLOSE);
867 mptcp_close_wake_up(sk);
870 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
872 struct mptcp_subflow_context *subflow;
873 struct sock *sk = (struct sock *)msk;
875 sock_owned_by_me(sk);
877 mptcp_for_each_subflow(msk, subflow) {
878 if (READ_ONCE(subflow->data_avail))
879 return mptcp_subflow_tcp_sock(subflow);
885 static bool mptcp_skb_can_collapse_to(u64 write_seq,
886 const struct sk_buff *skb,
887 const struct mptcp_ext *mpext)
889 if (!tcp_skb_can_collapse_to(skb))
892 /* can collapse only if MPTCP level sequence is in order and this
893 * mapping has not been xmitted yet
895 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
899 /* we can append data to the given data frag if:
900 * - there is space available in the backing page_frag
901 * - the data frag tail matches the current page_frag free offset
902 * - the data frag end sequence number matches the current write seq
904 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
905 const struct page_frag *pfrag,
906 const struct mptcp_data_frag *df)
908 return df && pfrag->page == df->page &&
909 pfrag->size - pfrag->offset > 0 &&
910 pfrag->offset == (df->offset + df->data_len) &&
911 df->data_seq + df->data_len == msk->write_seq;
914 static int mptcp_wmem_with_overhead(int size)
916 return size + ((sizeof(struct mptcp_data_frag) * size) >> PAGE_SHIFT);
919 static void __mptcp_wmem_reserve(struct sock *sk, int size)
921 int amount = mptcp_wmem_with_overhead(size);
922 struct mptcp_sock *msk = mptcp_sk(sk);
924 WARN_ON_ONCE(msk->wmem_reserved);
925 if (WARN_ON_ONCE(amount < 0))
928 if (amount <= sk->sk_forward_alloc)
931 /* under memory pressure try to reserve at most a single page
932 * otherwise try to reserve the full estimate and fallback
933 * to a single page before entering the error path
935 if ((tcp_under_memory_pressure(sk) && amount > PAGE_SIZE) ||
936 !sk_wmem_schedule(sk, amount)) {
937 if (amount <= PAGE_SIZE)
941 if (!sk_wmem_schedule(sk, amount))
946 msk->wmem_reserved = amount;
947 sk->sk_forward_alloc -= amount;
951 /* we will wait for memory on next allocation */
952 msk->wmem_reserved = -1;
955 static void __mptcp_update_wmem(struct sock *sk)
957 struct mptcp_sock *msk = mptcp_sk(sk);
959 #ifdef CONFIG_LOCKDEP
960 WARN_ON_ONCE(!lockdep_is_held(&sk->sk_lock.slock));
963 if (!msk->wmem_reserved)
966 if (msk->wmem_reserved < 0)
967 msk->wmem_reserved = 0;
968 if (msk->wmem_reserved > 0) {
969 sk->sk_forward_alloc += msk->wmem_reserved;
970 msk->wmem_reserved = 0;
974 static bool mptcp_wmem_alloc(struct sock *sk, int size)
976 struct mptcp_sock *msk = mptcp_sk(sk);
978 /* check for pre-existing error condition */
979 if (msk->wmem_reserved < 0)
982 if (msk->wmem_reserved >= size)
986 if (!sk_wmem_schedule(sk, size)) {
987 mptcp_data_unlock(sk);
991 sk->sk_forward_alloc -= size;
992 msk->wmem_reserved += size;
993 mptcp_data_unlock(sk);
996 msk->wmem_reserved -= size;
1000 static void mptcp_wmem_uncharge(struct sock *sk, int size)
1002 struct mptcp_sock *msk = mptcp_sk(sk);
1004 if (msk->wmem_reserved < 0)
1005 msk->wmem_reserved = 0;
1006 msk->wmem_reserved += size;
1009 static void __mptcp_mem_reclaim_partial(struct sock *sk)
1011 lockdep_assert_held_once(&sk->sk_lock.slock);
1012 __mptcp_update_wmem(sk);
1013 sk_mem_reclaim_partial(sk);
1016 static void mptcp_mem_reclaim_partial(struct sock *sk)
1018 struct mptcp_sock *msk = mptcp_sk(sk);
1020 /* if we are experiencing a transint allocation error,
1021 * the forward allocation memory has been already
1024 if (msk->wmem_reserved < 0)
1027 mptcp_data_lock(sk);
1028 sk->sk_forward_alloc += msk->wmem_reserved;
1029 sk_mem_reclaim_partial(sk);
1030 msk->wmem_reserved = sk->sk_forward_alloc;
1031 sk->sk_forward_alloc = 0;
1032 mptcp_data_unlock(sk);
1035 static void dfrag_uncharge(struct sock *sk, int len)
1037 sk_mem_uncharge(sk, len);
1038 sk_wmem_queued_add(sk, -len);
1041 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
1043 int len = dfrag->data_len + dfrag->overhead;
1045 list_del(&dfrag->list);
1046 dfrag_uncharge(sk, len);
1047 put_page(dfrag->page);
1050 static void __mptcp_clean_una(struct sock *sk)
1052 struct mptcp_sock *msk = mptcp_sk(sk);
1053 struct mptcp_data_frag *dtmp, *dfrag;
1054 bool cleaned = false;
1057 /* on fallback we just need to ignore snd_una, as this is really
1060 if (__mptcp_check_fallback(msk))
1061 msk->snd_una = READ_ONCE(msk->snd_nxt);
1063 snd_una = msk->snd_una;
1064 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1065 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1068 if (unlikely(dfrag == msk->first_pending)) {
1069 /* in recovery mode can see ack after the current snd head */
1070 if (WARN_ON_ONCE(!msk->recovery))
1073 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1076 dfrag_clear(sk, dfrag);
1080 dfrag = mptcp_rtx_head(sk);
1081 if (dfrag && after64(snd_una, dfrag->data_seq)) {
1082 u64 delta = snd_una - dfrag->data_seq;
1084 /* prevent wrap around in recovery mode */
1085 if (unlikely(delta > dfrag->already_sent)) {
1086 if (WARN_ON_ONCE(!msk->recovery))
1088 if (WARN_ON_ONCE(delta > dfrag->data_len))
1090 dfrag->already_sent += delta - dfrag->already_sent;
1093 dfrag->data_seq += delta;
1094 dfrag->offset += delta;
1095 dfrag->data_len -= delta;
1096 dfrag->already_sent -= delta;
1098 dfrag_uncharge(sk, delta);
1102 /* all retransmitted data acked, recovery completed */
1103 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1104 msk->recovery = false;
1107 if (cleaned && tcp_under_memory_pressure(sk))
1108 __mptcp_mem_reclaim_partial(sk);
1110 if (snd_una == READ_ONCE(msk->snd_nxt) && !msk->recovery) {
1111 if (mptcp_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1112 mptcp_stop_timer(sk);
1114 mptcp_reset_timer(sk);
1118 static void __mptcp_clean_una_wakeup(struct sock *sk)
1120 #ifdef CONFIG_LOCKDEP
1121 WARN_ON_ONCE(!lockdep_is_held(&sk->sk_lock.slock));
1123 __mptcp_clean_una(sk);
1124 mptcp_write_space(sk);
1127 static void mptcp_clean_una_wakeup(struct sock *sk)
1129 mptcp_data_lock(sk);
1130 __mptcp_clean_una_wakeup(sk);
1131 mptcp_data_unlock(sk);
1134 static void mptcp_enter_memory_pressure(struct sock *sk)
1136 struct mptcp_subflow_context *subflow;
1137 struct mptcp_sock *msk = mptcp_sk(sk);
1140 sk_stream_moderate_sndbuf(sk);
1141 mptcp_for_each_subflow(msk, subflow) {
1142 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1145 tcp_enter_memory_pressure(ssk);
1146 sk_stream_moderate_sndbuf(ssk);
1151 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1154 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1156 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1157 pfrag, sk->sk_allocation)))
1160 mptcp_enter_memory_pressure(sk);
1164 static struct mptcp_data_frag *
1165 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1168 int offset = ALIGN(orig_offset, sizeof(long));
1169 struct mptcp_data_frag *dfrag;
1171 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1172 dfrag->data_len = 0;
1173 dfrag->data_seq = msk->write_seq;
1174 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1175 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1176 dfrag->already_sent = 0;
1177 dfrag->page = pfrag->page;
1182 struct mptcp_sendmsg_info {
1188 bool data_lock_held;
1191 static int mptcp_check_allowed_size(struct mptcp_sock *msk, u64 data_seq,
1194 u64 window_end = mptcp_wnd_end(msk);
1196 if (__mptcp_check_fallback(msk))
1199 if (!before64(data_seq + avail_size, window_end)) {
1200 u64 allowed_size = window_end - data_seq;
1202 return min_t(unsigned int, allowed_size, avail_size);
1208 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1210 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1214 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1218 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1220 struct sk_buff *skb;
1222 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1224 if (likely(__mptcp_add_ext(skb, gfp))) {
1225 skb_reserve(skb, MAX_TCP_HEADER);
1226 skb->reserved_tailroom = skb->end - skb->tail;
1231 mptcp_enter_memory_pressure(sk);
1236 static bool __mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1238 struct sk_buff *skb;
1240 if (ssk->sk_tx_skb_cache) {
1241 skb = ssk->sk_tx_skb_cache;
1242 if (unlikely(!skb_ext_find(skb, SKB_EXT_MPTCP) &&
1243 !__mptcp_add_ext(skb, gfp)))
1248 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1252 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1253 ssk->sk_tx_skb_cache = skb;
1260 static bool mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1262 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1264 if (unlikely(tcp_under_memory_pressure(sk))) {
1266 __mptcp_mem_reclaim_partial(sk);
1268 mptcp_mem_reclaim_partial(sk);
1270 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1273 /* note: this always recompute the csum on the whole skb, even
1274 * if we just appended a single frag. More status info needed
1276 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1278 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1279 __wsum csum = ~csum_unfold(mpext->csum);
1280 int offset = skb->len - added;
1282 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1285 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1286 struct mptcp_data_frag *dfrag,
1287 struct mptcp_sendmsg_info *info)
1289 u64 data_seq = dfrag->data_seq + info->sent;
1290 struct mptcp_sock *msk = mptcp_sk(sk);
1291 bool zero_window_probe = false;
1292 struct mptcp_ext *mpext = NULL;
1293 struct sk_buff *skb, *tail;
1294 bool must_collapse = false;
1299 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1300 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1302 /* compute send limit */
1303 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1304 avail_size = info->size_goal;
1305 skb = tcp_write_queue_tail(ssk);
1307 /* Limit the write to the size available in the
1308 * current skb, if any, so that we create at most a new skb.
1309 * Explicitly tells TCP internals to avoid collapsing on later
1310 * queue management operation, to avoid breaking the ext <->
1311 * SSN association set here
1313 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1314 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1315 TCP_SKB_CB(skb)->eor = 1;
1319 must_collapse = (info->size_goal > skb->len) &&
1320 (skb_shinfo(skb)->nr_frags < sysctl_max_skb_frags);
1321 if (must_collapse) {
1322 size_bias = skb->len;
1323 avail_size = info->size_goal - skb->len;
1328 if (!must_collapse &&
1329 !mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held))
1332 /* Zero window and all data acked? Probe. */
1333 avail_size = mptcp_check_allowed_size(msk, data_seq, avail_size);
1334 if (avail_size == 0) {
1335 u64 snd_una = READ_ONCE(msk->snd_una);
1337 if (skb || snd_una != msk->snd_nxt)
1339 zero_window_probe = true;
1340 data_seq = snd_una - 1;
1344 if (WARN_ON_ONCE(info->sent > info->limit ||
1345 info->limit > dfrag->data_len))
1348 ret = info->limit - info->sent;
1349 tail = tcp_build_frag(ssk, avail_size + size_bias, info->flags,
1350 dfrag->page, dfrag->offset + info->sent, &ret);
1352 tcp_remove_empty_skb(sk, tcp_write_queue_tail(ssk));
1356 /* if the tail skb is still the cached one, collapsing really happened.
1359 TCP_SKB_CB(tail)->tcp_flags &= ~TCPHDR_PSH;
1360 mpext->data_len += ret;
1361 WARN_ON_ONCE(zero_window_probe);
1365 mpext = skb_ext_find(tail, SKB_EXT_MPTCP);
1366 if (WARN_ON_ONCE(!mpext)) {
1367 /* should never reach here, stream corrupted */
1371 memset(mpext, 0, sizeof(*mpext));
1372 mpext->data_seq = data_seq;
1373 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1374 mpext->data_len = ret;
1378 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1379 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1382 if (zero_window_probe) {
1383 mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
1385 if (READ_ONCE(msk->csum_enabled))
1386 mptcp_update_data_checksum(tail, ret);
1387 tcp_push_pending_frames(ssk);
1391 if (READ_ONCE(msk->csum_enabled))
1392 mptcp_update_data_checksum(tail, ret);
1393 mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
1397 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1398 sizeof(struct tcphdr) - \
1399 MAX_TCP_OPTION_SPACE - \
1400 sizeof(struct ipv6hdr) - \
1401 sizeof(struct frag_hdr))
1403 struct subflow_send_info {
1408 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1410 if (!subflow->stale)
1414 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1417 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1419 if (unlikely(subflow->stale)) {
1420 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1422 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1425 mptcp_subflow_set_active(subflow);
1427 return __mptcp_subflow_active(subflow);
1430 /* implement the mptcp packet scheduler;
1431 * returns the subflow that will transmit the next DSS
1432 * additionally updates the rtx timeout
1434 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1436 struct subflow_send_info send_info[2];
1437 struct mptcp_subflow_context *subflow;
1438 struct sock *sk = (struct sock *)msk;
1439 int i, nr_active = 0;
1445 sock_owned_by_me(sk);
1447 if (__mptcp_check_fallback(msk)) {
1450 return sk_stream_memory_free(msk->first) ? msk->first : NULL;
1453 /* re-use last subflow, if the burst allow that */
1454 if (msk->last_snd && msk->snd_burst > 0 &&
1455 sk_stream_memory_free(msk->last_snd) &&
1456 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1457 mptcp_set_timeout(sk);
1458 return msk->last_snd;
1461 /* pick the subflow with the lower wmem/wspace ratio */
1462 for (i = 0; i < 2; ++i) {
1463 send_info[i].ssk = NULL;
1464 send_info[i].ratio = -1;
1466 mptcp_for_each_subflow(msk, subflow) {
1467 trace_mptcp_subflow_get_send(subflow);
1468 ssk = mptcp_subflow_tcp_sock(subflow);
1469 if (!mptcp_subflow_active(subflow))
1472 tout = max(tout, mptcp_timeout_from_subflow(subflow));
1473 nr_active += !subflow->backup;
1474 if (!sk_stream_memory_free(subflow->tcp_sock) || !tcp_sk(ssk)->snd_wnd)
1477 pace = READ_ONCE(ssk->sk_pacing_rate);
1481 ratio = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32,
1483 if (ratio < send_info[subflow->backup].ratio) {
1484 send_info[subflow->backup].ssk = ssk;
1485 send_info[subflow->backup].ratio = ratio;
1488 __mptcp_set_timeout(sk, tout);
1490 /* pick the best backup if no other subflow is active */
1492 send_info[0].ssk = send_info[1].ssk;
1494 if (send_info[0].ssk) {
1495 msk->last_snd = send_info[0].ssk;
1496 msk->snd_burst = min_t(int, MPTCP_SEND_BURST_SIZE,
1497 tcp_sk(msk->last_snd)->snd_wnd);
1498 return msk->last_snd;
1504 static void mptcp_push_release(struct sock *sk, struct sock *ssk,
1505 struct mptcp_sendmsg_info *info)
1507 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1511 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1513 struct sock *prev_ssk = NULL, *ssk = NULL;
1514 struct mptcp_sock *msk = mptcp_sk(sk);
1515 struct mptcp_sendmsg_info info = {
1518 struct mptcp_data_frag *dfrag;
1519 int len, copied = 0;
1521 while ((dfrag = mptcp_send_head(sk))) {
1522 info.sent = dfrag->already_sent;
1523 info.limit = dfrag->data_len;
1524 len = dfrag->data_len - dfrag->already_sent;
1529 mptcp_flush_join_list(msk);
1530 ssk = mptcp_subflow_get_send(msk);
1532 /* First check. If the ssk has changed since
1533 * the last round, release prev_ssk
1535 if (ssk != prev_ssk && prev_ssk)
1536 mptcp_push_release(sk, prev_ssk, &info);
1540 /* Need to lock the new subflow only if different
1541 * from the previous one, otherwise we are still
1542 * helding the relevant lock
1544 if (ssk != prev_ssk)
1547 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1549 mptcp_push_release(sk, ssk, &info);
1554 dfrag->already_sent += ret;
1555 msk->snd_nxt += ret;
1556 msk->snd_burst -= ret;
1557 msk->tx_pending_data -= ret;
1561 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1564 /* at this point we held the socket lock for the last subflow we used */
1566 mptcp_push_release(sk, ssk, &info);
1569 /* ensure the rtx timer is running */
1570 if (!mptcp_timer_pending(sk))
1571 mptcp_reset_timer(sk);
1573 __mptcp_check_send_data_fin(sk);
1576 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
1578 struct mptcp_sock *msk = mptcp_sk(sk);
1579 struct mptcp_sendmsg_info info = {
1580 .data_lock_held = true,
1582 struct mptcp_data_frag *dfrag;
1583 struct sock *xmit_ssk;
1584 int len, copied = 0;
1588 while ((dfrag = mptcp_send_head(sk))) {
1589 info.sent = dfrag->already_sent;
1590 info.limit = dfrag->data_len;
1591 len = dfrag->data_len - dfrag->already_sent;
1595 /* the caller already invoked the packet scheduler,
1596 * check for a different subflow usage only after
1597 * spooling the first chunk of data
1599 xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
1602 if (xmit_ssk != ssk) {
1603 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk));
1607 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1612 dfrag->already_sent += ret;
1613 msk->snd_nxt += ret;
1614 msk->snd_burst -= ret;
1615 msk->tx_pending_data -= ret;
1620 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1624 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1625 * not going to flush it via release_sock()
1627 __mptcp_update_wmem(sk);
1629 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1631 if (!mptcp_timer_pending(sk))
1632 mptcp_reset_timer(sk);
1634 if (msk->snd_data_fin_enable &&
1635 msk->snd_nxt + 1 == msk->write_seq)
1636 mptcp_schedule_work(sk);
1640 static void mptcp_set_nospace(struct sock *sk)
1642 /* enable autotune */
1643 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1645 /* will be cleared on avail space */
1646 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1649 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1651 struct mptcp_sock *msk = mptcp_sk(sk);
1652 struct page_frag *pfrag;
1657 /* we don't support FASTOPEN yet */
1658 if (msg->msg_flags & MSG_FASTOPEN)
1661 /* silently ignore everything else */
1662 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL;
1664 mptcp_lock_sock(sk, __mptcp_wmem_reserve(sk, min_t(size_t, 1 << 20, len)));
1666 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1668 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1669 ret = sk_stream_wait_connect(sk, &timeo);
1674 pfrag = sk_page_frag(sk);
1676 while (msg_data_left(msg)) {
1677 int total_ts, frag_truesize = 0;
1678 struct mptcp_data_frag *dfrag;
1679 bool dfrag_collapsed;
1680 size_t psize, offset;
1682 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
1687 /* reuse tail pfrag, if possible, or carve a new one from the
1690 dfrag = mptcp_pending_tail(sk);
1691 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1692 if (!dfrag_collapsed) {
1693 if (!sk_stream_memory_free(sk))
1694 goto wait_for_memory;
1696 if (!mptcp_page_frag_refill(sk, pfrag))
1697 goto wait_for_memory;
1699 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1700 frag_truesize = dfrag->overhead;
1703 /* we do not bound vs wspace, to allow a single packet.
1704 * memory accounting will prevent execessive memory usage
1707 offset = dfrag->offset + dfrag->data_len;
1708 psize = pfrag->size - offset;
1709 psize = min_t(size_t, psize, msg_data_left(msg));
1710 total_ts = psize + frag_truesize;
1712 if (!mptcp_wmem_alloc(sk, total_ts))
1713 goto wait_for_memory;
1715 if (copy_page_from_iter(dfrag->page, offset, psize,
1716 &msg->msg_iter) != psize) {
1717 mptcp_wmem_uncharge(sk, psize + frag_truesize);
1722 /* data successfully copied into the write queue */
1724 dfrag->data_len += psize;
1725 frag_truesize += psize;
1726 pfrag->offset += frag_truesize;
1727 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1728 msk->tx_pending_data += psize;
1730 /* charge data on mptcp pending queue to the msk socket
1731 * Note: we charge such data both to sk and ssk
1733 sk_wmem_queued_add(sk, frag_truesize);
1734 if (!dfrag_collapsed) {
1735 get_page(dfrag->page);
1736 list_add_tail(&dfrag->list, &msk->rtx_queue);
1737 if (!msk->first_pending)
1738 WRITE_ONCE(msk->first_pending, dfrag);
1740 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1741 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1747 mptcp_set_nospace(sk);
1748 __mptcp_push_pending(sk, msg->msg_flags);
1749 ret = sk_stream_wait_memory(sk, &timeo);
1755 __mptcp_push_pending(sk, msg->msg_flags);
1759 return copied ? : ret;
1762 static void mptcp_wait_data(struct sock *sk, long *timeo)
1764 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1765 struct mptcp_sock *msk = mptcp_sk(sk);
1767 add_wait_queue(sk_sleep(sk), &wait);
1768 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1770 sk_wait_event(sk, timeo,
1771 test_bit(MPTCP_DATA_READY, &msk->flags), &wait);
1773 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1774 remove_wait_queue(sk_sleep(sk), &wait);
1777 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1779 size_t len, int flags,
1780 struct scm_timestamping_internal *tss,
1783 struct sk_buff *skb, *tmp;
1786 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1787 u32 offset = MPTCP_SKB_CB(skb)->offset;
1788 u32 data_len = skb->len - offset;
1789 u32 count = min_t(size_t, len - copied, data_len);
1792 if (!(flags & MSG_TRUNC)) {
1793 err = skb_copy_datagram_msg(skb, offset, msg, count);
1794 if (unlikely(err < 0)) {
1801 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1802 tcp_update_recv_tstamps(skb, tss);
1803 *cmsg_flags |= MPTCP_CMSG_TS;
1808 if (count < data_len) {
1809 if (!(flags & MSG_PEEK))
1810 MPTCP_SKB_CB(skb)->offset += count;
1814 if (!(flags & MSG_PEEK)) {
1815 /* we will bulk release the skb memory later */
1816 skb->destructor = NULL;
1817 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1818 __skb_unlink(skb, &msk->receive_queue);
1829 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1831 * Only difference: Use highest rtt estimate of the subflows in use.
1833 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1835 struct mptcp_subflow_context *subflow;
1836 struct sock *sk = (struct sock *)msk;
1837 u32 time, advmss = 1;
1840 sock_owned_by_me(sk);
1845 msk->rcvq_space.copied += copied;
1847 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1848 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1850 rtt_us = msk->rcvq_space.rtt_us;
1851 if (rtt_us && time < (rtt_us >> 3))
1855 mptcp_for_each_subflow(msk, subflow) {
1856 const struct tcp_sock *tp;
1860 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1862 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1863 sf_advmss = READ_ONCE(tp->advmss);
1865 rtt_us = max(sf_rtt_us, rtt_us);
1866 advmss = max(sf_advmss, advmss);
1869 msk->rcvq_space.rtt_us = rtt_us;
1870 if (time < (rtt_us >> 3) || rtt_us == 0)
1873 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1876 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
1877 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1881 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1883 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1885 do_div(grow, msk->rcvq_space.space);
1886 rcvwin += (grow << 1);
1888 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
1889 while (tcp_win_from_space(sk, rcvmem) < advmss)
1892 do_div(rcvwin, advmss);
1893 rcvbuf = min_t(u64, rcvwin * rcvmem,
1894 sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
1896 if (rcvbuf > sk->sk_rcvbuf) {
1899 window_clamp = tcp_win_from_space(sk, rcvbuf);
1900 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1902 /* Make subflows follow along. If we do not do this, we
1903 * get drops at subflow level if skbs can't be moved to
1904 * the mptcp rx queue fast enough (announced rcv_win can
1905 * exceed ssk->sk_rcvbuf).
1907 mptcp_for_each_subflow(msk, subflow) {
1911 ssk = mptcp_subflow_tcp_sock(subflow);
1912 slow = lock_sock_fast(ssk);
1913 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1914 tcp_sk(ssk)->window_clamp = window_clamp;
1915 tcp_cleanup_rbuf(ssk, 1);
1916 unlock_sock_fast(ssk, slow);
1921 msk->rcvq_space.space = msk->rcvq_space.copied;
1923 msk->rcvq_space.copied = 0;
1924 msk->rcvq_space.time = mstamp;
1927 static void __mptcp_update_rmem(struct sock *sk)
1929 struct mptcp_sock *msk = mptcp_sk(sk);
1931 if (!msk->rmem_released)
1934 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
1935 sk_mem_uncharge(sk, msk->rmem_released);
1936 WRITE_ONCE(msk->rmem_released, 0);
1939 static void __mptcp_splice_receive_queue(struct sock *sk)
1941 struct mptcp_sock *msk = mptcp_sk(sk);
1943 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
1946 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
1948 struct sock *sk = (struct sock *)msk;
1949 unsigned int moved = 0;
1952 mptcp_flush_join_list(msk);
1954 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
1957 /* we can have data pending in the subflows only if the msk
1958 * receive buffer was full at subflow_data_ready() time,
1959 * that is an unlikely slow path.
1964 slowpath = lock_sock_fast(ssk);
1965 mptcp_data_lock(sk);
1966 __mptcp_update_rmem(sk);
1967 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
1968 mptcp_data_unlock(sk);
1970 if (unlikely(ssk->sk_err))
1971 __mptcp_error_report(sk);
1972 unlock_sock_fast(ssk, slowpath);
1975 /* acquire the data lock only if some input data is pending */
1977 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
1978 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
1979 mptcp_data_lock(sk);
1980 __mptcp_update_rmem(sk);
1981 ret |= __mptcp_ofo_queue(msk);
1982 __mptcp_splice_receive_queue(sk);
1983 mptcp_data_unlock(sk);
1986 mptcp_check_data_fin((struct sock *)msk);
1987 return !skb_queue_empty(&msk->receive_queue);
1990 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1991 int nonblock, int flags, int *addr_len)
1993 struct mptcp_sock *msk = mptcp_sk(sk);
1994 struct scm_timestamping_internal tss;
1995 int copied = 0, cmsg_flags = 0;
1999 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2000 if (unlikely(flags & MSG_ERRQUEUE))
2001 return inet_recv_error(sk, msg, len, addr_len);
2003 mptcp_lock_sock(sk, __mptcp_splice_receive_queue(sk));
2004 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2009 timeo = sock_rcvtimeo(sk, nonblock);
2011 len = min_t(size_t, len, INT_MAX);
2012 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2014 while (copied < len) {
2017 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2018 if (unlikely(bytes_read < 0)) {
2020 copied = bytes_read;
2024 copied += bytes_read;
2026 /* be sure to advertise window change */
2027 mptcp_cleanup_rbuf(msk);
2029 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2032 /* only the master socket status is relevant here. The exit
2033 * conditions mirror closely tcp_recvmsg()
2035 if (copied >= target)
2040 sk->sk_state == TCP_CLOSE ||
2041 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2043 signal_pending(current))
2047 copied = sock_error(sk);
2051 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2052 mptcp_check_for_eof(msk);
2054 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2055 /* race breaker: the shutdown could be after the
2056 * previous receive queue check
2058 if (__mptcp_move_skbs(msk))
2063 if (sk->sk_state == TCP_CLOSE) {
2073 if (signal_pending(current)) {
2074 copied = sock_intr_errno(timeo);
2079 pr_debug("block timeout %ld", timeo);
2080 mptcp_wait_data(sk, &timeo);
2083 if (skb_queue_empty_lockless(&sk->sk_receive_queue) &&
2084 skb_queue_empty(&msk->receive_queue)) {
2085 /* entire backlog drained, clear DATA_READY. */
2086 clear_bit(MPTCP_DATA_READY, &msk->flags);
2088 /* .. race-breaker: ssk might have gotten new data
2089 * after last __mptcp_move_skbs() returned false.
2091 if (unlikely(__mptcp_move_skbs(msk)))
2092 set_bit(MPTCP_DATA_READY, &msk->flags);
2096 if (cmsg_flags && copied >= 0) {
2097 if (cmsg_flags & MPTCP_CMSG_TS)
2098 tcp_recv_timestamp(msg, sk, &tss);
2101 pr_debug("msk=%p data_ready=%d rx queue empty=%d copied=%d",
2102 msk, test_bit(MPTCP_DATA_READY, &msk->flags),
2103 skb_queue_empty_lockless(&sk->sk_receive_queue), copied);
2104 if (!(flags & MSG_PEEK))
2105 mptcp_rcv_space_adjust(msk, copied);
2111 static void mptcp_retransmit_timer(struct timer_list *t)
2113 struct inet_connection_sock *icsk = from_timer(icsk, t,
2114 icsk_retransmit_timer);
2115 struct sock *sk = &icsk->icsk_inet.sk;
2116 struct mptcp_sock *msk = mptcp_sk(sk);
2119 if (!sock_owned_by_user(sk)) {
2120 /* we need a process context to retransmit */
2121 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2122 mptcp_schedule_work(sk);
2124 /* delegate our work to tcp_release_cb() */
2125 set_bit(MPTCP_RETRANSMIT, &msk->flags);
2131 static void mptcp_timeout_timer(struct timer_list *t)
2133 struct sock *sk = from_timer(sk, t, sk_timer);
2135 mptcp_schedule_work(sk);
2139 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2142 * A backup subflow is returned only if that is the only kind available.
2144 static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2146 struct sock *backup = NULL, *pick = NULL;
2147 struct mptcp_subflow_context *subflow;
2148 int min_stale_count = INT_MAX;
2150 sock_owned_by_me((const struct sock *)msk);
2152 if (__mptcp_check_fallback(msk))
2155 mptcp_for_each_subflow(msk, subflow) {
2156 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2158 if (!__mptcp_subflow_active(subflow))
2161 /* still data outstanding at TCP level? skip this */
2162 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2163 mptcp_pm_subflow_chk_stale(msk, ssk);
2164 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2168 if (subflow->backup) {
2181 /* use backup only if there are no progresses anywhere */
2182 return min_stale_count > 1 ? backup : NULL;
2185 static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2188 iput(SOCK_INODE(msk->subflow));
2189 msk->subflow = NULL;
2193 bool __mptcp_retransmit_pending_data(struct sock *sk)
2195 struct mptcp_data_frag *cur, *rtx_head;
2196 struct mptcp_sock *msk = mptcp_sk(sk);
2198 if (__mptcp_check_fallback(mptcp_sk(sk)))
2201 if (tcp_rtx_and_write_queues_empty(sk))
2204 /* the closing socket has some data untransmitted and/or unacked:
2205 * some data in the mptcp rtx queue has not really xmitted yet.
2206 * keep it simple and re-inject the whole mptcp level rtx queue
2208 mptcp_data_lock(sk);
2209 __mptcp_clean_una_wakeup(sk);
2210 rtx_head = mptcp_rtx_head(sk);
2212 mptcp_data_unlock(sk);
2216 /* will accept ack for reijected data before re-sending them */
2217 if (!msk->recovery || after64(msk->snd_nxt, msk->recovery_snd_nxt))
2218 msk->recovery_snd_nxt = msk->snd_nxt;
2219 msk->recovery = true;
2220 mptcp_data_unlock(sk);
2222 msk->first_pending = rtx_head;
2223 msk->tx_pending_data += msk->snd_nxt - rtx_head->data_seq;
2224 msk->snd_nxt = rtx_head->data_seq;
2227 /* be sure to clear the "sent status" on all re-injected fragments */
2228 list_for_each_entry(cur, &msk->rtx_queue, list) {
2229 if (!cur->already_sent)
2231 cur->already_sent = 0;
2237 /* subflow sockets can be either outgoing (connect) or incoming
2240 * Outgoing subflows use in-kernel sockets.
2241 * Incoming subflows do not have their own 'struct socket' allocated,
2242 * so we need to use tcp_close() after detaching them from the mptcp
2245 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2246 struct mptcp_subflow_context *subflow)
2248 struct mptcp_sock *msk = mptcp_sk(sk);
2251 list_del(&subflow->node);
2253 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2255 /* if we are invoked by the msk cleanup code, the subflow is
2261 need_push = __mptcp_retransmit_pending_data(sk);
2262 subflow->disposable = 1;
2264 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2265 * the ssk has been already destroyed, we just need to release the
2266 * reference owned by msk;
2268 if (!inet_csk(ssk)->icsk_ulp_ops) {
2269 kfree_rcu(subflow, rcu);
2271 /* otherwise tcp will dispose of the ssk and subflow ctx */
2272 __tcp_close(ssk, 0);
2274 /* close acquired an extra ref */
2281 if (ssk == msk->last_snd)
2282 msk->last_snd = NULL;
2284 if (ssk == msk->first)
2287 if (msk->subflow && ssk == msk->subflow->sk)
2288 mptcp_dispose_initial_subflow(msk);
2291 __mptcp_push_pending(sk, 0);
2294 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2295 struct mptcp_subflow_context *subflow)
2297 if (sk->sk_state == TCP_ESTABLISHED)
2298 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2299 __mptcp_close_ssk(sk, ssk, subflow);
2302 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2307 static void __mptcp_close_subflow(struct mptcp_sock *msk)
2309 struct mptcp_subflow_context *subflow, *tmp;
2313 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2314 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2316 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2319 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2320 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2323 mptcp_close_ssk((struct sock *)msk, ssk, subflow);
2327 static bool mptcp_check_close_timeout(const struct sock *sk)
2329 s32 delta = tcp_jiffies32 - inet_csk(sk)->icsk_mtup.probe_timestamp;
2330 struct mptcp_subflow_context *subflow;
2332 if (delta >= TCP_TIMEWAIT_LEN)
2335 /* if all subflows are in closed status don't bother with additional
2338 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2339 if (inet_sk_state_load(mptcp_subflow_tcp_sock(subflow)) !=
2346 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2348 struct mptcp_subflow_context *subflow, *tmp;
2349 struct sock *sk = &msk->sk.icsk_inet.sk;
2351 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2354 mptcp_token_destroy(msk);
2356 list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
2357 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2360 slow = lock_sock_fast(tcp_sk);
2361 if (tcp_sk->sk_state != TCP_CLOSE) {
2362 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2363 tcp_set_state(tcp_sk, TCP_CLOSE);
2365 unlock_sock_fast(tcp_sk, slow);
2368 inet_sk_state_store(sk, TCP_CLOSE);
2369 sk->sk_shutdown = SHUTDOWN_MASK;
2370 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2371 set_bit(MPTCP_DATA_READY, &msk->flags);
2372 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2374 mptcp_close_wake_up(sk);
2377 static void __mptcp_retrans(struct sock *sk)
2379 struct mptcp_sock *msk = mptcp_sk(sk);
2380 struct mptcp_sendmsg_info info = {};
2381 struct mptcp_data_frag *dfrag;
2386 mptcp_clean_una_wakeup(sk);
2387 dfrag = mptcp_rtx_head(sk);
2389 if (mptcp_data_fin_enabled(msk)) {
2390 struct inet_connection_sock *icsk = inet_csk(sk);
2392 icsk->icsk_retransmits++;
2393 mptcp_set_datafin_timeout(sk);
2394 mptcp_send_ack(msk);
2402 ssk = mptcp_subflow_get_retrans(msk);
2408 /* limit retransmission to the bytes already sent on some subflows */
2410 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
2411 while (info.sent < info.limit) {
2412 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2416 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2421 dfrag->already_sent = max(dfrag->already_sent, info.sent);
2422 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2429 if (!mptcp_timer_pending(sk))
2430 mptcp_reset_timer(sk);
2433 static void mptcp_worker(struct work_struct *work)
2435 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2436 struct sock *sk = &msk->sk.icsk_inet.sk;
2440 state = sk->sk_state;
2441 if (unlikely(state == TCP_CLOSE))
2444 mptcp_check_data_fin_ack(sk);
2445 mptcp_flush_join_list(msk);
2447 mptcp_check_fastclose(msk);
2450 mptcp_pm_nl_work(msk);
2452 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2453 mptcp_check_for_eof(msk);
2455 __mptcp_check_send_data_fin(sk);
2456 mptcp_check_data_fin(sk);
2458 /* There is no point in keeping around an orphaned sk timedout or
2459 * closed, but we need the msk around to reply to incoming DATA_FIN,
2460 * even if it is orphaned and in FIN_WAIT2 state
2462 if (sock_flag(sk, SOCK_DEAD) &&
2463 (mptcp_check_close_timeout(sk) || sk->sk_state == TCP_CLOSE)) {
2464 inet_sk_state_store(sk, TCP_CLOSE);
2465 __mptcp_destroy_sock(sk);
2469 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2470 __mptcp_close_subflow(msk);
2472 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2473 __mptcp_retrans(sk);
2480 static int __mptcp_init_sock(struct sock *sk)
2482 struct mptcp_sock *msk = mptcp_sk(sk);
2484 spin_lock_init(&msk->join_list_lock);
2486 INIT_LIST_HEAD(&msk->conn_list);
2487 INIT_LIST_HEAD(&msk->join_list);
2488 INIT_LIST_HEAD(&msk->rtx_queue);
2489 INIT_WORK(&msk->work, mptcp_worker);
2490 __skb_queue_head_init(&msk->receive_queue);
2491 msk->out_of_order_queue = RB_ROOT;
2492 msk->first_pending = NULL;
2493 msk->wmem_reserved = 0;
2494 WRITE_ONCE(msk->rmem_released, 0);
2495 msk->tx_pending_data = 0;
2496 msk->timer_ival = TCP_RTO_MIN;
2499 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2500 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2501 msk->recovery = false;
2503 mptcp_pm_data_init(msk);
2505 /* re-use the csk retrans timer for MPTCP-level retrans */
2506 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2507 timer_setup(&sk->sk_timer, mptcp_timeout_timer, 0);
2512 static int mptcp_init_sock(struct sock *sk)
2514 struct inet_connection_sock *icsk = inet_csk(sk);
2515 struct net *net = sock_net(sk);
2518 ret = __mptcp_init_sock(sk);
2522 if (!mptcp_is_enabled(net))
2523 return -ENOPROTOOPT;
2525 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2528 ret = __mptcp_socket_create(mptcp_sk(sk));
2532 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2533 * propagate the correct value
2535 tcp_assign_congestion_control(sk);
2536 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2538 /* no need to keep a reference to the ops, the name will suffice */
2539 tcp_cleanup_congestion_control(sk);
2540 icsk->icsk_ca_ops = NULL;
2542 sk_sockets_allocated_inc(sk);
2543 sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
2544 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
2549 static void __mptcp_clear_xmit(struct sock *sk)
2551 struct mptcp_sock *msk = mptcp_sk(sk);
2552 struct mptcp_data_frag *dtmp, *dfrag;
2554 WRITE_ONCE(msk->first_pending, NULL);
2555 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2556 dfrag_clear(sk, dfrag);
2559 static void mptcp_cancel_work(struct sock *sk)
2561 struct mptcp_sock *msk = mptcp_sk(sk);
2563 if (cancel_work_sync(&msk->work))
2567 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2571 switch (ssk->sk_state) {
2573 if (!(how & RCV_SHUTDOWN))
2577 tcp_disconnect(ssk, O_NONBLOCK);
2580 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2581 pr_debug("Fallback");
2582 ssk->sk_shutdown |= how;
2583 tcp_shutdown(ssk, how);
2585 pr_debug("Sending DATA_FIN on subflow %p", ssk);
2587 if (!mptcp_timer_pending(sk))
2588 mptcp_reset_timer(sk);
2596 static const unsigned char new_state[16] = {
2597 /* current state: new state: action: */
2598 [0 /* (Invalid) */] = TCP_CLOSE,
2599 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2600 [TCP_SYN_SENT] = TCP_CLOSE,
2601 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2602 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2603 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2604 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2605 [TCP_CLOSE] = TCP_CLOSE,
2606 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2607 [TCP_LAST_ACK] = TCP_LAST_ACK,
2608 [TCP_LISTEN] = TCP_CLOSE,
2609 [TCP_CLOSING] = TCP_CLOSING,
2610 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2613 static int mptcp_close_state(struct sock *sk)
2615 int next = (int)new_state[sk->sk_state];
2616 int ns = next & TCP_STATE_MASK;
2618 inet_sk_state_store(sk, ns);
2620 return next & TCP_ACTION_FIN;
2623 static void __mptcp_check_send_data_fin(struct sock *sk)
2625 struct mptcp_subflow_context *subflow;
2626 struct mptcp_sock *msk = mptcp_sk(sk);
2628 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2629 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2630 msk->snd_nxt, msk->write_seq);
2632 /* we still need to enqueue subflows or not really shutting down,
2635 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2636 mptcp_send_head(sk))
2639 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2641 /* fallback socket will not get data_fin/ack, can move to the next
2644 if (__mptcp_check_fallback(msk)) {
2645 if ((1 << sk->sk_state) & (TCPF_CLOSING | TCPF_LAST_ACK)) {
2646 inet_sk_state_store(sk, TCP_CLOSE);
2647 mptcp_close_wake_up(sk);
2648 } else if (sk->sk_state == TCP_FIN_WAIT1) {
2649 inet_sk_state_store(sk, TCP_FIN_WAIT2);
2653 mptcp_flush_join_list(msk);
2654 mptcp_for_each_subflow(msk, subflow) {
2655 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2657 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2661 static void __mptcp_wr_shutdown(struct sock *sk)
2663 struct mptcp_sock *msk = mptcp_sk(sk);
2665 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2666 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2667 !!mptcp_send_head(sk));
2669 /* will be ignored by fallback sockets */
2670 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2671 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2673 __mptcp_check_send_data_fin(sk);
2676 static void __mptcp_destroy_sock(struct sock *sk)
2678 struct mptcp_subflow_context *subflow, *tmp;
2679 struct mptcp_sock *msk = mptcp_sk(sk);
2680 LIST_HEAD(conn_list);
2682 pr_debug("msk=%p", msk);
2686 /* be sure to always acquire the join list lock, to sync vs
2687 * mptcp_finish_join().
2689 spin_lock_bh(&msk->join_list_lock);
2690 list_splice_tail_init(&msk->join_list, &msk->conn_list);
2691 spin_unlock_bh(&msk->join_list_lock);
2692 list_splice_init(&msk->conn_list, &conn_list);
2694 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
2695 sk_stop_timer(sk, &sk->sk_timer);
2698 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
2699 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2700 __mptcp_close_ssk(sk, ssk, subflow);
2703 sk->sk_prot->destroy(sk);
2705 WARN_ON_ONCE(msk->wmem_reserved);
2706 WARN_ON_ONCE(msk->rmem_released);
2707 sk_stream_kill_queues(sk);
2708 xfrm_sk_free_policy(sk);
2710 sk_refcnt_debug_release(sk);
2711 mptcp_dispose_initial_subflow(msk);
2715 static void mptcp_close(struct sock *sk, long timeout)
2717 struct mptcp_subflow_context *subflow;
2718 bool do_cancel_work = false;
2721 sk->sk_shutdown = SHUTDOWN_MASK;
2723 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2724 inet_sk_state_store(sk, TCP_CLOSE);
2728 if (mptcp_close_state(sk))
2729 __mptcp_wr_shutdown(sk);
2731 sk_stream_wait_close(sk, timeout);
2734 /* orphan all the subflows */
2735 inet_csk(sk)->icsk_mtup.probe_timestamp = tcp_jiffies32;
2736 mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
2737 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2738 bool slow = lock_sock_fast(ssk);
2741 unlock_sock_fast(ssk, slow);
2746 pr_debug("msk=%p state=%d", sk, sk->sk_state);
2747 if (sk->sk_state == TCP_CLOSE) {
2748 __mptcp_destroy_sock(sk);
2749 do_cancel_work = true;
2751 sk_reset_timer(sk, &sk->sk_timer, jiffies + TCP_TIMEWAIT_LEN);
2755 mptcp_cancel_work(sk);
2757 if (mptcp_sk(sk)->token)
2758 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
2763 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
2765 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2766 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
2767 struct ipv6_pinfo *msk6 = inet6_sk(msk);
2769 msk->sk_v6_daddr = ssk->sk_v6_daddr;
2770 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
2773 msk6->saddr = ssk6->saddr;
2774 msk6->flow_label = ssk6->flow_label;
2778 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
2779 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
2780 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
2781 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
2782 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
2783 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
2786 static int mptcp_disconnect(struct sock *sk, int flags)
2788 struct mptcp_subflow_context *subflow;
2789 struct mptcp_sock *msk = mptcp_sk(sk);
2791 mptcp_do_flush_join_list(msk);
2793 mptcp_for_each_subflow(msk, subflow) {
2794 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2797 tcp_disconnect(ssk, flags);
2803 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2804 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
2806 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
2808 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
2812 struct sock *mptcp_sk_clone(const struct sock *sk,
2813 const struct mptcp_options_received *mp_opt,
2814 struct request_sock *req)
2816 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
2817 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
2818 struct mptcp_sock *msk;
2824 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2825 if (nsk->sk_family == AF_INET6)
2826 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
2829 __mptcp_init_sock(nsk);
2831 msk = mptcp_sk(nsk);
2832 msk->local_key = subflow_req->local_key;
2833 msk->token = subflow_req->token;
2834 msk->subflow = NULL;
2835 WRITE_ONCE(msk->fully_established, false);
2836 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
2837 WRITE_ONCE(msk->csum_enabled, true);
2839 msk->write_seq = subflow_req->idsn + 1;
2840 msk->snd_nxt = msk->write_seq;
2841 msk->snd_una = msk->write_seq;
2842 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
2843 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
2845 if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) {
2846 msk->can_ack = true;
2847 msk->remote_key = mp_opt->sndr_key;
2848 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
2850 WRITE_ONCE(msk->ack_seq, ack_seq);
2851 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
2854 sock_reset_flag(nsk, SOCK_RCU_FREE);
2855 /* will be fully established after successful MPC subflow creation */
2856 inet_sk_state_store(nsk, TCP_SYN_RECV);
2858 security_inet_csk_clone(nsk, req);
2859 bh_unlock_sock(nsk);
2861 /* keep a single reference */
2866 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
2868 const struct tcp_sock *tp = tcp_sk(ssk);
2870 msk->rcvq_space.copied = 0;
2871 msk->rcvq_space.rtt_us = 0;
2873 msk->rcvq_space.time = tp->tcp_mstamp;
2875 /* initial rcv_space offering made to peer */
2876 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
2877 TCP_INIT_CWND * tp->advmss);
2878 if (msk->rcvq_space.space == 0)
2879 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
2881 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
2884 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
2887 struct mptcp_sock *msk = mptcp_sk(sk);
2888 struct socket *listener;
2891 listener = __mptcp_nmpc_socket(msk);
2892 if (WARN_ON_ONCE(!listener)) {
2897 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
2898 newsk = inet_csk_accept(listener->sk, flags, err, kern);
2902 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
2903 if (sk_is_mptcp(newsk)) {
2904 struct mptcp_subflow_context *subflow;
2905 struct sock *new_mptcp_sock;
2907 subflow = mptcp_subflow_ctx(newsk);
2908 new_mptcp_sock = subflow->conn;
2910 /* is_mptcp should be false if subflow->conn is missing, see
2911 * subflow_syn_recv_sock()
2913 if (WARN_ON_ONCE(!new_mptcp_sock)) {
2914 tcp_sk(newsk)->is_mptcp = 0;
2918 /* acquire the 2nd reference for the owning socket */
2919 sock_hold(new_mptcp_sock);
2920 newsk = new_mptcp_sock;
2921 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
2923 MPTCP_INC_STATS(sock_net(sk),
2924 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
2930 void mptcp_destroy_common(struct mptcp_sock *msk)
2932 struct sock *sk = (struct sock *)msk;
2934 __mptcp_clear_xmit(sk);
2936 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
2937 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
2939 skb_rbtree_purge(&msk->out_of_order_queue);
2940 mptcp_token_destroy(msk);
2941 mptcp_pm_free_anno_list(msk);
2944 static void mptcp_destroy(struct sock *sk)
2946 struct mptcp_sock *msk = mptcp_sk(sk);
2948 mptcp_destroy_common(msk);
2949 sk_sockets_allocated_dec(sk);
2952 void __mptcp_data_acked(struct sock *sk)
2954 if (!sock_owned_by_user(sk))
2955 __mptcp_clean_una(sk);
2957 set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags);
2959 if (mptcp_pending_data_fin_ack(sk))
2960 mptcp_schedule_work(sk);
2963 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
2965 if (!mptcp_send_head(sk))
2968 if (!sock_owned_by_user(sk)) {
2969 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
2971 if (xmit_ssk == ssk)
2972 __mptcp_subflow_push_pending(sk, ssk);
2974 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk));
2976 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
2980 /* processes deferred events and flush wmem */
2981 static void mptcp_release_cb(struct sock *sk)
2984 unsigned long flags = 0;
2986 if (test_and_clear_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags))
2987 flags |= BIT(MPTCP_PUSH_PENDING);
2988 if (test_and_clear_bit(MPTCP_RETRANSMIT, &mptcp_sk(sk)->flags))
2989 flags |= BIT(MPTCP_RETRANSMIT);
2993 /* the following actions acquire the subflow socket lock
2995 * 1) can't be invoked in atomic scope
2996 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
2997 * datapath acquires the msk socket spinlock while helding
2998 * the subflow socket lock
3001 spin_unlock_bh(&sk->sk_lock.slock);
3002 if (flags & BIT(MPTCP_PUSH_PENDING))
3003 __mptcp_push_pending(sk, 0);
3004 if (flags & BIT(MPTCP_RETRANSMIT))
3005 __mptcp_retrans(sk);
3008 spin_lock_bh(&sk->sk_lock.slock);
3011 /* be sure to set the current sk state before tacking actions
3012 * depending on sk_state
3014 if (test_and_clear_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->flags))
3015 __mptcp_set_connected(sk);
3016 if (test_and_clear_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->flags))
3017 __mptcp_clean_una_wakeup(sk);
3018 if (test_and_clear_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags))
3019 __mptcp_error_report(sk);
3021 /* push_pending may touch wmem_reserved, ensure we do the cleanup
3024 __mptcp_update_wmem(sk);
3025 __mptcp_update_rmem(sk);
3028 void mptcp_subflow_process_delegated(struct sock *ssk)
3030 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3031 struct sock *sk = subflow->conn;
3033 mptcp_data_lock(sk);
3034 if (!sock_owned_by_user(sk))
3035 __mptcp_subflow_push_pending(sk, ssk);
3037 set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->flags);
3038 mptcp_data_unlock(sk);
3039 mptcp_subflow_delegated_done(subflow);
3042 static int mptcp_hash(struct sock *sk)
3044 /* should never be called,
3045 * we hash the TCP subflows not the master socket
3051 static void mptcp_unhash(struct sock *sk)
3053 /* called from sk_common_release(), but nothing to do here */
3056 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3058 struct mptcp_sock *msk = mptcp_sk(sk);
3059 struct socket *ssock;
3061 ssock = __mptcp_nmpc_socket(msk);
3062 pr_debug("msk=%p, subflow=%p", msk, ssock);
3063 if (WARN_ON_ONCE(!ssock))
3066 return inet_csk_get_port(ssock->sk, snum);
3069 void mptcp_finish_connect(struct sock *ssk)
3071 struct mptcp_subflow_context *subflow;
3072 struct mptcp_sock *msk;
3076 subflow = mptcp_subflow_ctx(ssk);
3080 pr_debug("msk=%p, token=%u", sk, subflow->token);
3082 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
3084 subflow->map_seq = ack_seq;
3085 subflow->map_subflow_seq = 1;
3087 /* the socket is not connected yet, no msk/subflow ops can access/race
3088 * accessing the field below
3090 WRITE_ONCE(msk->remote_key, subflow->remote_key);
3091 WRITE_ONCE(msk->local_key, subflow->local_key);
3092 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3093 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3094 WRITE_ONCE(msk->ack_seq, ack_seq);
3095 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
3096 WRITE_ONCE(msk->can_ack, 1);
3097 WRITE_ONCE(msk->snd_una, msk->write_seq);
3099 mptcp_pm_new_connection(msk, ssk, 0);
3101 mptcp_rcv_space_init(msk, ssk);
3104 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3106 write_lock_bh(&sk->sk_callback_lock);
3107 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3108 sk_set_socket(sk, parent);
3109 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3110 write_unlock_bh(&sk->sk_callback_lock);
3113 bool mptcp_finish_join(struct sock *ssk)
3115 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3116 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3117 struct sock *parent = (void *)msk;
3118 struct socket *parent_sock;
3121 pr_debug("msk=%p, subflow=%p", msk, subflow);
3123 /* mptcp socket already closing? */
3124 if (!mptcp_is_fully_established(parent)) {
3125 subflow->reset_reason = MPTCP_RST_EMPTCP;
3129 if (!msk->pm.server_side)
3132 if (!mptcp_pm_allow_new_subflow(msk)) {
3133 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3137 /* active connections are already on conn_list, and we can't acquire
3139 * use the join list lock as synchronization point and double-check
3140 * msk status to avoid racing with __mptcp_destroy_sock()
3142 spin_lock_bh(&msk->join_list_lock);
3143 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
3144 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node))) {
3145 list_add_tail(&subflow->node, &msk->join_list);
3148 spin_unlock_bh(&msk->join_list_lock);
3150 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3154 /* attach to msk socket only after we are sure he will deal with us
3157 parent_sock = READ_ONCE(parent->sk_socket);
3158 if (parent_sock && !ssk->sk_socket)
3159 mptcp_sock_graft(ssk, parent_sock);
3160 subflow->map_seq = READ_ONCE(msk->ack_seq);
3162 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
3166 static void mptcp_shutdown(struct sock *sk, int how)
3168 pr_debug("sk=%p, how=%d", sk, how);
3170 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3171 __mptcp_wr_shutdown(sk);
3174 static struct proto mptcp_prot = {
3176 .owner = THIS_MODULE,
3177 .init = mptcp_init_sock,
3178 .disconnect = mptcp_disconnect,
3179 .close = mptcp_close,
3180 .accept = mptcp_accept,
3181 .setsockopt = mptcp_setsockopt,
3182 .getsockopt = mptcp_getsockopt,
3183 .shutdown = mptcp_shutdown,
3184 .destroy = mptcp_destroy,
3185 .sendmsg = mptcp_sendmsg,
3186 .recvmsg = mptcp_recvmsg,
3187 .release_cb = mptcp_release_cb,
3189 .unhash = mptcp_unhash,
3190 .get_port = mptcp_get_port,
3191 .sockets_allocated = &mptcp_sockets_allocated,
3192 .memory_allocated = &tcp_memory_allocated,
3193 .memory_pressure = &tcp_memory_pressure,
3194 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3195 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3196 .sysctl_mem = sysctl_tcp_mem,
3197 .obj_size = sizeof(struct mptcp_sock),
3198 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3199 .no_autobind = true,
3202 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3204 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3205 struct socket *ssock;
3208 lock_sock(sock->sk);
3209 ssock = __mptcp_nmpc_socket(msk);
3215 err = ssock->ops->bind(ssock, uaddr, addr_len);
3217 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3220 release_sock(sock->sk);
3224 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3225 struct mptcp_subflow_context *subflow)
3227 subflow->request_mptcp = 0;
3228 __mptcp_do_fallback(msk);
3231 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
3232 int addr_len, int flags)
3234 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3235 struct mptcp_subflow_context *subflow;
3236 struct socket *ssock;
3239 lock_sock(sock->sk);
3240 if (sock->state != SS_UNCONNECTED && msk->subflow) {
3241 /* pending connection or invalid state, let existing subflow
3244 ssock = msk->subflow;
3248 ssock = __mptcp_nmpc_socket(msk);
3254 mptcp_token_destroy(msk);
3255 inet_sk_state_store(sock->sk, TCP_SYN_SENT);
3256 subflow = mptcp_subflow_ctx(ssock->sk);
3257 #ifdef CONFIG_TCP_MD5SIG
3258 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3261 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
3262 mptcp_subflow_early_fallback(msk, subflow);
3264 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3265 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
3266 mptcp_subflow_early_fallback(msk, subflow);
3268 if (likely(!__mptcp_check_fallback(msk)))
3269 MPTCP_INC_STATS(sock_net(sock->sk), MPTCP_MIB_MPCAPABLEACTIVE);
3272 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
3273 sock->state = ssock->state;
3275 /* on successful connect, the msk state will be moved to established by
3276 * subflow_finish_connect()
3278 if (!err || err == -EINPROGRESS)
3279 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3281 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3284 release_sock(sock->sk);
3288 static int mptcp_listen(struct socket *sock, int backlog)
3290 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3291 struct socket *ssock;
3294 pr_debug("msk=%p", msk);
3296 lock_sock(sock->sk);
3297 ssock = __mptcp_nmpc_socket(msk);
3303 mptcp_token_destroy(msk);
3304 inet_sk_state_store(sock->sk, TCP_LISTEN);
3305 sock_set_flag(sock->sk, SOCK_RCU_FREE);
3307 err = ssock->ops->listen(ssock, backlog);
3308 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3310 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3313 release_sock(sock->sk);
3317 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3318 int flags, bool kern)
3320 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3321 struct socket *ssock;
3324 pr_debug("msk=%p", msk);
3326 lock_sock(sock->sk);
3327 if (sock->sk->sk_state != TCP_LISTEN)
3330 ssock = __mptcp_nmpc_socket(msk);
3334 clear_bit(MPTCP_DATA_READY, &msk->flags);
3335 sock_hold(ssock->sk);
3336 release_sock(sock->sk);
3338 err = ssock->ops->accept(sock, newsock, flags, kern);
3339 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
3340 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3341 struct mptcp_subflow_context *subflow;
3342 struct sock *newsk = newsock->sk;
3346 /* PM/worker can now acquire the first subflow socket
3347 * lock without racing with listener queue cleanup,
3348 * we can notify it, if needed.
3350 * Even if remote has reset the initial subflow by now
3351 * the refcnt is still at least one.
3353 subflow = mptcp_subflow_ctx(msk->first);
3354 list_add(&subflow->node, &msk->conn_list);
3355 sock_hold(msk->first);
3356 if (mptcp_is_fully_established(newsk))
3357 mptcp_pm_fully_established(msk, msk->first, GFP_KERNEL);
3359 mptcp_copy_inaddrs(newsk, msk->first);
3360 mptcp_rcv_space_init(msk, msk->first);
3361 mptcp_propagate_sndbuf(newsk, msk->first);
3363 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3364 * This is needed so NOSPACE flag can be set from tcp stack.
3366 mptcp_flush_join_list(msk);
3367 mptcp_for_each_subflow(msk, subflow) {
3368 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3370 if (!ssk->sk_socket)
3371 mptcp_sock_graft(ssk, newsock);
3373 release_sock(newsk);
3376 if (inet_csk_listen_poll(ssock->sk))
3377 set_bit(MPTCP_DATA_READY, &msk->flags);
3378 sock_put(ssock->sk);
3382 release_sock(sock->sk);
3386 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
3388 return test_bit(MPTCP_DATA_READY, &msk->flags) ? EPOLLIN | EPOLLRDNORM :
3392 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3394 struct sock *sk = (struct sock *)msk;
3396 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
3397 return EPOLLOUT | EPOLLWRNORM;
3399 if (sk_stream_is_writeable(sk))
3400 return EPOLLOUT | EPOLLWRNORM;
3402 mptcp_set_nospace(sk);
3403 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3404 if (sk_stream_is_writeable(sk))
3405 return EPOLLOUT | EPOLLWRNORM;
3410 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3411 struct poll_table_struct *wait)
3413 struct sock *sk = sock->sk;
3414 struct mptcp_sock *msk;
3419 sock_poll_wait(file, sock, wait);
3421 state = inet_sk_state_load(sk);
3422 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
3423 if (state == TCP_LISTEN)
3424 return mptcp_check_readable(msk);
3426 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3427 mask |= mptcp_check_readable(msk);
3428 mask |= mptcp_check_writeable(msk);
3430 if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3432 if (sk->sk_shutdown & RCV_SHUTDOWN)
3433 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3435 /* This barrier is coupled with smp_wmb() in tcp_reset() */
3443 static const struct proto_ops mptcp_stream_ops = {
3445 .owner = THIS_MODULE,
3446 .release = inet_release,
3448 .connect = mptcp_stream_connect,
3449 .socketpair = sock_no_socketpair,
3450 .accept = mptcp_stream_accept,
3451 .getname = inet_getname,
3453 .ioctl = inet_ioctl,
3454 .gettstamp = sock_gettstamp,
3455 .listen = mptcp_listen,
3456 .shutdown = inet_shutdown,
3457 .setsockopt = sock_common_setsockopt,
3458 .getsockopt = sock_common_getsockopt,
3459 .sendmsg = inet_sendmsg,
3460 .recvmsg = inet_recvmsg,
3461 .mmap = sock_no_mmap,
3462 .sendpage = inet_sendpage,
3465 static struct inet_protosw mptcp_protosw = {
3466 .type = SOCK_STREAM,
3467 .protocol = IPPROTO_MPTCP,
3468 .prot = &mptcp_prot,
3469 .ops = &mptcp_stream_ops,
3470 .flags = INET_PROTOSW_ICSK,
3473 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3475 struct mptcp_delegated_action *delegated;
3476 struct mptcp_subflow_context *subflow;
3479 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3480 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3481 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3483 bh_lock_sock_nested(ssk);
3484 if (!sock_owned_by_user(ssk) &&
3485 mptcp_subflow_has_delegated_action(subflow))
3486 mptcp_subflow_process_delegated(ssk);
3487 /* ... elsewhere tcp_release_cb_override already processed
3488 * the action or will do at next release_sock().
3489 * In both case must dequeue the subflow here - on the same
3490 * CPU that scheduled it.
3492 bh_unlock_sock(ssk);
3495 if (++work_done == budget)
3499 /* always provide a 0 'work_done' argument, so that napi_complete_done
3500 * will not try accessing the NULL napi->dev ptr
3502 napi_complete_done(napi, 0);
3506 void __init mptcp_proto_init(void)
3508 struct mptcp_delegated_action *delegated;
3511 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
3513 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
3514 panic("Failed to allocate MPTCP pcpu counter\n");
3516 init_dummy_netdev(&mptcp_napi_dev);
3517 for_each_possible_cpu(cpu) {
3518 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
3519 INIT_LIST_HEAD(&delegated->head);
3520 netif_tx_napi_add(&mptcp_napi_dev, &delegated->napi, mptcp_napi_poll,
3522 napi_enable(&delegated->napi);
3525 mptcp_subflow_init();
3529 if (proto_register(&mptcp_prot, 1) != 0)
3530 panic("Failed to register MPTCP proto.\n");
3532 inet_register_protosw(&mptcp_protosw);
3534 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
3537 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3538 static const struct proto_ops mptcp_v6_stream_ops = {
3540 .owner = THIS_MODULE,
3541 .release = inet6_release,
3543 .connect = mptcp_stream_connect,
3544 .socketpair = sock_no_socketpair,
3545 .accept = mptcp_stream_accept,
3546 .getname = inet6_getname,
3548 .ioctl = inet6_ioctl,
3549 .gettstamp = sock_gettstamp,
3550 .listen = mptcp_listen,
3551 .shutdown = inet_shutdown,
3552 .setsockopt = sock_common_setsockopt,
3553 .getsockopt = sock_common_getsockopt,
3554 .sendmsg = inet6_sendmsg,
3555 .recvmsg = inet6_recvmsg,
3556 .mmap = sock_no_mmap,
3557 .sendpage = inet_sendpage,
3558 #ifdef CONFIG_COMPAT
3559 .compat_ioctl = inet6_compat_ioctl,
3563 static struct proto mptcp_v6_prot;
3565 static void mptcp_v6_destroy(struct sock *sk)
3568 inet6_destroy_sock(sk);
3571 static struct inet_protosw mptcp_v6_protosw = {
3572 .type = SOCK_STREAM,
3573 .protocol = IPPROTO_MPTCP,
3574 .prot = &mptcp_v6_prot,
3575 .ops = &mptcp_v6_stream_ops,
3576 .flags = INET_PROTOSW_ICSK,
3579 int __init mptcp_proto_v6_init(void)
3583 mptcp_v6_prot = mptcp_prot;
3584 strcpy(mptcp_v6_prot.name, "MPTCPv6");
3585 mptcp_v6_prot.slab = NULL;
3586 mptcp_v6_prot.destroy = mptcp_v6_destroy;
3587 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
3589 err = proto_register(&mptcp_v6_prot, 1);
3593 err = inet6_register_protosw(&mptcp_v6_protosw);
3595 proto_unregister(&mptcp_v6_prot);