Linux 5.7.7
[linux/fpc-iii.git] / net / mptcp / protocol.c
blob4bf4f629975d9d6b760bf26306de5ea713e98109
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
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>
14 #include <net/sock.h>
15 #include <net/inet_common.h>
16 #include <net/inet_hashtables.h>
17 #include <net/protocol.h>
18 #include <net/tcp.h>
19 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
20 #include <net/transp_v6.h>
21 #endif
22 #include <net/mptcp.h>
23 #include "protocol.h"
24 #include "mib.h"
26 #define MPTCP_SAME_STATE TCP_MAX_STATES
28 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
29 struct mptcp6_sock {
30 struct mptcp_sock msk;
31 struct ipv6_pinfo np;
33 #endif
35 struct mptcp_skb_cb {
36 u32 offset;
39 #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
41 static struct percpu_counter mptcp_sockets_allocated;
43 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
44 * completed yet or has failed, return the subflow socket.
45 * Otherwise return NULL.
47 static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
49 if (!msk->subflow || READ_ONCE(msk->can_ack))
50 return NULL;
52 return msk->subflow;
55 static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk)
57 return msk->first && !sk_is_mptcp(msk->first);
60 static struct socket *mptcp_is_tcpsk(struct sock *sk)
62 struct socket *sock = sk->sk_socket;
64 if (sock->sk != sk)
65 return NULL;
67 if (unlikely(sk->sk_prot == &tcp_prot)) {
68 /* we are being invoked after mptcp_accept() has
69 * accepted a non-mp-capable flow: sk is a tcp_sk,
70 * not an mptcp one.
72 * Hand the socket over to tcp so all further socket ops
73 * bypass mptcp.
75 sock->ops = &inet_stream_ops;
76 return sock;
77 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
78 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
79 sock->ops = &inet6_stream_ops;
80 return sock;
81 #endif
84 return NULL;
87 static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk)
89 struct socket *sock;
91 sock_owned_by_me((const struct sock *)msk);
93 sock = mptcp_is_tcpsk((struct sock *)msk);
94 if (unlikely(sock))
95 return sock;
97 if (likely(!__mptcp_needs_tcp_fallback(msk)))
98 return NULL;
100 return msk->subflow;
103 static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
105 return !msk->first;
108 static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state)
110 struct mptcp_subflow_context *subflow;
111 struct sock *sk = (struct sock *)msk;
112 struct socket *ssock;
113 int err;
115 ssock = __mptcp_tcp_fallback(msk);
116 if (unlikely(ssock))
117 return ssock;
119 ssock = __mptcp_nmpc_socket(msk);
120 if (ssock)
121 goto set_state;
123 if (!__mptcp_can_create_subflow(msk))
124 return ERR_PTR(-EINVAL);
126 err = mptcp_subflow_create_socket(sk, &ssock);
127 if (err)
128 return ERR_PTR(err);
130 msk->first = ssock->sk;
131 msk->subflow = ssock;
132 subflow = mptcp_subflow_ctx(ssock->sk);
133 list_add(&subflow->node, &msk->conn_list);
134 subflow->request_mptcp = 1;
136 set_state:
137 if (state != MPTCP_SAME_STATE)
138 inet_sk_state_store(sk, state);
139 return ssock;
142 static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
143 struct sk_buff *skb,
144 unsigned int offset, size_t copy_len)
146 struct sock *sk = (struct sock *)msk;
148 __skb_unlink(skb, &ssk->sk_receive_queue);
149 skb_set_owner_r(skb, sk);
150 __skb_queue_tail(&sk->sk_receive_queue, skb);
152 msk->ack_seq += copy_len;
153 MPTCP_SKB_CB(skb)->offset = offset;
156 /* both sockets must be locked */
157 static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk,
158 struct sock *ssk)
160 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
161 u64 dsn = mptcp_subflow_get_mapped_dsn(subflow);
163 /* revalidate data sequence number.
165 * mptcp_subflow_data_available() is usually called
166 * without msk lock. Its unlikely (but possible)
167 * that msk->ack_seq has been advanced since the last
168 * call found in-sequence data.
170 if (likely(dsn == msk->ack_seq))
171 return true;
173 subflow->data_avail = 0;
174 return mptcp_subflow_data_available(ssk);
177 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
178 struct sock *ssk,
179 unsigned int *bytes)
181 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
182 struct sock *sk = (struct sock *)msk;
183 unsigned int moved = 0;
184 bool more_data_avail;
185 struct tcp_sock *tp;
186 bool done = false;
188 if (!mptcp_subflow_dsn_valid(msk, ssk)) {
189 *bytes = 0;
190 return false;
193 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
194 int rcvbuf = max(ssk->sk_rcvbuf, sk->sk_rcvbuf);
196 if (rcvbuf > sk->sk_rcvbuf)
197 sk->sk_rcvbuf = rcvbuf;
200 tp = tcp_sk(ssk);
201 do {
202 u32 map_remaining, offset;
203 u32 seq = tp->copied_seq;
204 struct sk_buff *skb;
205 bool fin;
207 /* try to move as much data as available */
208 map_remaining = subflow->map_data_len -
209 mptcp_subflow_get_map_offset(subflow);
211 skb = skb_peek(&ssk->sk_receive_queue);
212 if (!skb)
213 break;
215 offset = seq - TCP_SKB_CB(skb)->seq;
216 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
217 if (fin) {
218 done = true;
219 seq++;
222 if (offset < skb->len) {
223 size_t len = skb->len - offset;
225 if (tp->urg_data)
226 done = true;
228 __mptcp_move_skb(msk, ssk, skb, offset, len);
229 seq += len;
230 moved += len;
232 if (WARN_ON_ONCE(map_remaining < len))
233 break;
234 } else {
235 WARN_ON_ONCE(!fin);
236 sk_eat_skb(ssk, skb);
237 done = true;
240 WRITE_ONCE(tp->copied_seq, seq);
241 more_data_avail = mptcp_subflow_data_available(ssk);
243 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) {
244 done = true;
245 break;
247 } while (more_data_avail);
249 *bytes = moved;
251 return done;
254 /* In most cases we will be able to lock the mptcp socket. If its already
255 * owned, we need to defer to the work queue to avoid ABBA deadlock.
257 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
259 struct sock *sk = (struct sock *)msk;
260 unsigned int moved = 0;
262 if (READ_ONCE(sk->sk_lock.owned))
263 return false;
265 if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock)))
266 return false;
268 /* must re-check after taking the lock */
269 if (!READ_ONCE(sk->sk_lock.owned))
270 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
272 spin_unlock_bh(&sk->sk_lock.slock);
274 return moved > 0;
277 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
279 struct mptcp_sock *msk = mptcp_sk(sk);
281 set_bit(MPTCP_DATA_READY, &msk->flags);
283 if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) &&
284 move_skbs_to_msk(msk, ssk))
285 goto wake;
287 /* don't schedule if mptcp sk is (still) over limit */
288 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf))
289 goto wake;
291 /* mptcp socket is owned, release_cb should retry */
292 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
293 &sk->sk_tsq_flags)) {
294 sock_hold(sk);
296 /* need to try again, its possible release_cb() has already
297 * been called after the test_and_set_bit() above.
299 move_skbs_to_msk(msk, ssk);
301 wake:
302 sk->sk_data_ready(sk);
305 static void __mptcp_flush_join_list(struct mptcp_sock *msk)
307 if (likely(list_empty(&msk->join_list)))
308 return;
310 spin_lock_bh(&msk->join_list_lock);
311 list_splice_tail_init(&msk->join_list, &msk->conn_list);
312 spin_unlock_bh(&msk->join_list_lock);
315 static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
317 long tout = ssk && inet_csk(ssk)->icsk_pending ?
318 inet_csk(ssk)->icsk_timeout - jiffies : 0;
320 if (tout <= 0)
321 tout = mptcp_sk(sk)->timer_ival;
322 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
325 static bool mptcp_timer_pending(struct sock *sk)
327 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
330 static void mptcp_reset_timer(struct sock *sk)
332 struct inet_connection_sock *icsk = inet_csk(sk);
333 unsigned long tout;
335 /* should never be called with mptcp level timer cleared */
336 tout = READ_ONCE(mptcp_sk(sk)->timer_ival);
337 if (WARN_ON_ONCE(!tout))
338 tout = TCP_RTO_MIN;
339 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
342 void mptcp_data_acked(struct sock *sk)
344 mptcp_reset_timer(sk);
346 if (!sk_stream_is_writeable(sk) &&
347 schedule_work(&mptcp_sk(sk)->work))
348 sock_hold(sk);
351 void mptcp_subflow_eof(struct sock *sk)
353 struct mptcp_sock *msk = mptcp_sk(sk);
355 if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) &&
356 schedule_work(&msk->work))
357 sock_hold(sk);
360 static void mptcp_check_for_eof(struct mptcp_sock *msk)
362 struct mptcp_subflow_context *subflow;
363 struct sock *sk = (struct sock *)msk;
364 int receivers = 0;
366 mptcp_for_each_subflow(msk, subflow)
367 receivers += !subflow->rx_eof;
369 if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
370 /* hopefully temporary hack: propagate shutdown status
371 * to msk, when all subflows agree on it
373 sk->sk_shutdown |= RCV_SHUTDOWN;
375 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
376 set_bit(MPTCP_DATA_READY, &msk->flags);
377 sk->sk_data_ready(sk);
381 static void mptcp_stop_timer(struct sock *sk)
383 struct inet_connection_sock *icsk = inet_csk(sk);
385 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
386 mptcp_sk(sk)->timer_ival = 0;
389 static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
391 if (!msk->cached_ext)
392 msk->cached_ext = __skb_ext_alloc();
394 return !!msk->cached_ext;
397 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
399 struct mptcp_subflow_context *subflow;
400 struct sock *sk = (struct sock *)msk;
402 sock_owned_by_me(sk);
404 mptcp_for_each_subflow(msk, subflow) {
405 if (subflow->data_avail)
406 return mptcp_subflow_tcp_sock(subflow);
409 return NULL;
412 static bool mptcp_skb_can_collapse_to(u64 write_seq,
413 const struct sk_buff *skb,
414 const struct mptcp_ext *mpext)
416 if (!tcp_skb_can_collapse_to(skb))
417 return false;
419 /* can collapse only if MPTCP level sequence is in order */
420 return mpext && mpext->data_seq + mpext->data_len == write_seq;
423 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
424 const struct page_frag *pfrag,
425 const struct mptcp_data_frag *df)
427 return df && pfrag->page == df->page &&
428 df->data_seq + df->data_len == msk->write_seq;
431 static void dfrag_uncharge(struct sock *sk, int len)
433 sk_mem_uncharge(sk, len);
434 sk_wmem_queued_add(sk, -len);
437 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
439 int len = dfrag->data_len + dfrag->overhead;
441 list_del(&dfrag->list);
442 dfrag_uncharge(sk, len);
443 put_page(dfrag->page);
446 static void mptcp_clean_una(struct sock *sk)
448 struct mptcp_sock *msk = mptcp_sk(sk);
449 struct mptcp_data_frag *dtmp, *dfrag;
450 u64 snd_una = atomic64_read(&msk->snd_una);
451 bool cleaned = false;
453 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
454 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
455 break;
457 dfrag_clear(sk, dfrag);
458 cleaned = true;
461 dfrag = mptcp_rtx_head(sk);
462 if (dfrag && after64(snd_una, dfrag->data_seq)) {
463 u64 delta = dfrag->data_seq + dfrag->data_len - snd_una;
465 dfrag->data_seq += delta;
466 dfrag->data_len -= delta;
468 dfrag_uncharge(sk, delta);
469 cleaned = true;
472 if (cleaned) {
473 sk_mem_reclaim_partial(sk);
475 /* Only wake up writers if a subflow is ready */
476 if (test_bit(MPTCP_SEND_SPACE, &msk->flags))
477 sk_stream_write_space(sk);
481 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
482 * data
484 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
486 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
487 pfrag, sk->sk_allocation)))
488 return true;
490 sk->sk_prot->enter_memory_pressure(sk);
491 sk_stream_moderate_sndbuf(sk);
492 return false;
495 static struct mptcp_data_frag *
496 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
497 int orig_offset)
499 int offset = ALIGN(orig_offset, sizeof(long));
500 struct mptcp_data_frag *dfrag;
502 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
503 dfrag->data_len = 0;
504 dfrag->data_seq = msk->write_seq;
505 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
506 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
507 dfrag->page = pfrag->page;
509 return dfrag;
512 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
513 struct msghdr *msg, struct mptcp_data_frag *dfrag,
514 long *timeo, int *pmss_now,
515 int *ps_goal)
517 int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0;
518 bool dfrag_collapsed, can_collapse = false;
519 struct mptcp_sock *msk = mptcp_sk(sk);
520 struct mptcp_ext *mpext = NULL;
521 bool retransmission = !!dfrag;
522 struct sk_buff *skb, *tail;
523 struct page_frag *pfrag;
524 struct page *page;
525 u64 *write_seq;
526 size_t psize;
528 /* use the mptcp page cache so that we can easily move the data
529 * from one substream to another, but do per subflow memory accounting
530 * Note: pfrag is used only !retransmission, but the compiler if
531 * fooled into a warning if we don't init here
533 pfrag = sk_page_frag(sk);
534 while ((!retransmission && !mptcp_page_frag_refill(ssk, pfrag)) ||
535 !mptcp_ext_cache_refill(msk)) {
536 ret = sk_stream_wait_memory(ssk, timeo);
537 if (ret)
538 return ret;
540 /* if sk_stream_wait_memory() sleeps snd_una can change
541 * significantly, refresh the rtx queue
543 mptcp_clean_una(sk);
545 if (unlikely(__mptcp_needs_tcp_fallback(msk)))
546 return 0;
548 if (!retransmission) {
549 write_seq = &msk->write_seq;
550 page = pfrag->page;
551 } else {
552 write_seq = &dfrag->data_seq;
553 page = dfrag->page;
556 /* compute copy limit */
557 mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags);
558 *pmss_now = mss_now;
559 *ps_goal = size_goal;
560 avail_size = size_goal;
561 skb = tcp_write_queue_tail(ssk);
562 if (skb) {
563 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
565 /* Limit the write to the size available in the
566 * current skb, if any, so that we create at most a new skb.
567 * Explicitly tells TCP internals to avoid collapsing on later
568 * queue management operation, to avoid breaking the ext <->
569 * SSN association set here
571 can_collapse = (size_goal - skb->len > 0) &&
572 mptcp_skb_can_collapse_to(*write_seq, skb, mpext);
573 if (!can_collapse)
574 TCP_SKB_CB(skb)->eor = 1;
575 else
576 avail_size = size_goal - skb->len;
579 if (!retransmission) {
580 /* reuse tail pfrag, if possible, or carve a new one from the
581 * page allocator
583 dfrag = mptcp_rtx_tail(sk);
584 offset = pfrag->offset;
585 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
586 if (!dfrag_collapsed) {
587 dfrag = mptcp_carve_data_frag(msk, pfrag, offset);
588 offset = dfrag->offset;
589 frag_truesize = dfrag->overhead;
591 psize = min_t(size_t, pfrag->size - offset, avail_size);
593 /* Copy to page */
594 pr_debug("left=%zu", msg_data_left(msg));
595 psize = copy_page_from_iter(pfrag->page, offset,
596 min_t(size_t, msg_data_left(msg),
597 psize),
598 &msg->msg_iter);
599 pr_debug("left=%zu", msg_data_left(msg));
600 if (!psize)
601 return -EINVAL;
603 if (!sk_wmem_schedule(sk, psize + dfrag->overhead))
604 return -ENOMEM;
605 } else {
606 offset = dfrag->offset;
607 psize = min_t(size_t, dfrag->data_len, avail_size);
610 /* tell the TCP stack to delay the push so that we can safely
611 * access the skb after the sendpages call
613 ret = do_tcp_sendpages(ssk, page, offset, psize,
614 msg->msg_flags | MSG_SENDPAGE_NOTLAST);
615 if (ret <= 0)
616 return ret;
618 frag_truesize += ret;
619 if (!retransmission) {
620 if (unlikely(ret < psize))
621 iov_iter_revert(&msg->msg_iter, psize - ret);
623 /* send successful, keep track of sent data for mptcp-level
624 * retransmission
626 dfrag->data_len += ret;
627 if (!dfrag_collapsed) {
628 get_page(dfrag->page);
629 list_add_tail(&dfrag->list, &msk->rtx_queue);
630 sk_wmem_queued_add(sk, frag_truesize);
631 } else {
632 sk_wmem_queued_add(sk, ret);
635 /* charge data on mptcp rtx queue to the master socket
636 * Note: we charge such data both to sk and ssk
638 sk->sk_forward_alloc -= frag_truesize;
641 /* if the tail skb extension is still the cached one, collapsing
642 * really happened. Note: we can't check for 'same skb' as the sk_buff
643 * hdr on tail can be transmitted, freed and re-allocated by the
644 * do_tcp_sendpages() call
646 tail = tcp_write_queue_tail(ssk);
647 if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) {
648 WARN_ON_ONCE(!can_collapse);
649 mpext->data_len += ret;
650 goto out;
653 skb = tcp_write_queue_tail(ssk);
654 mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext);
655 msk->cached_ext = NULL;
657 memset(mpext, 0, sizeof(*mpext));
658 mpext->data_seq = *write_seq;
659 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
660 mpext->data_len = ret;
661 mpext->use_map = 1;
662 mpext->dsn64 = 1;
664 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
665 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
666 mpext->dsn64);
668 out:
669 if (!retransmission)
670 pfrag->offset += frag_truesize;
671 *write_seq += ret;
672 mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
674 return ret;
677 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
679 struct mptcp_subflow_context *subflow;
680 struct sock *backup = NULL;
682 sock_owned_by_me((const struct sock *)msk);
684 mptcp_for_each_subflow(msk, subflow) {
685 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
687 if (!sk_stream_memory_free(ssk)) {
688 struct socket *sock = ssk->sk_socket;
690 if (sock) {
691 clear_bit(MPTCP_SEND_SPACE, &msk->flags);
692 smp_mb__after_atomic();
694 /* enables sk->write_space() callbacks */
695 set_bit(SOCK_NOSPACE, &sock->flags);
698 return NULL;
701 if (subflow->backup) {
702 if (!backup)
703 backup = ssk;
705 continue;
708 return ssk;
711 return backup;
714 static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk)
716 struct socket *sock;
718 if (likely(sk_stream_is_writeable(ssk)))
719 return;
721 sock = READ_ONCE(ssk->sk_socket);
723 if (sock) {
724 clear_bit(MPTCP_SEND_SPACE, &msk->flags);
725 smp_mb__after_atomic();
726 /* set NOSPACE only after clearing SEND_SPACE flag */
727 set_bit(SOCK_NOSPACE, &sock->flags);
731 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
733 int mss_now = 0, size_goal = 0, ret = 0;
734 struct mptcp_sock *msk = mptcp_sk(sk);
735 struct socket *ssock;
736 size_t copied = 0;
737 struct sock *ssk;
738 long timeo;
740 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
741 return -EOPNOTSUPP;
743 lock_sock(sk);
745 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
747 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
748 ret = sk_stream_wait_connect(sk, &timeo);
749 if (ret)
750 goto out;
753 fallback:
754 ssock = __mptcp_tcp_fallback(msk);
755 if (unlikely(ssock)) {
756 release_sock(sk);
757 pr_debug("fallback passthrough");
758 ret = sock_sendmsg(ssock, msg);
759 return ret >= 0 ? ret + copied : (copied ? copied : ret);
762 mptcp_clean_una(sk);
764 __mptcp_flush_join_list(msk);
765 ssk = mptcp_subflow_get_send(msk);
766 while (!sk_stream_memory_free(sk) || !ssk) {
767 ret = sk_stream_wait_memory(sk, &timeo);
768 if (ret)
769 goto out;
771 mptcp_clean_una(sk);
773 ssk = mptcp_subflow_get_send(msk);
774 if (list_empty(&msk->conn_list)) {
775 ret = -ENOTCONN;
776 goto out;
780 pr_debug("conn_list->subflow=%p", ssk);
782 lock_sock(ssk);
783 while (msg_data_left(msg)) {
784 ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now,
785 &size_goal);
786 if (ret < 0)
787 break;
788 if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) {
789 /* Can happen for passive sockets:
790 * 3WHS negotiated MPTCP, but first packet after is
791 * plain TCP (e.g. due to middlebox filtering unknown
792 * options).
794 * Fall back to TCP.
796 release_sock(ssk);
797 goto fallback;
800 copied += ret;
803 mptcp_set_timeout(sk, ssk);
804 if (copied) {
805 ret = copied;
806 tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle,
807 size_goal);
809 /* start the timer, if it's not pending */
810 if (!mptcp_timer_pending(sk))
811 mptcp_reset_timer(sk);
814 ssk_check_wmem(msk, ssk);
815 release_sock(ssk);
816 out:
817 release_sock(sk);
818 return ret;
821 static void mptcp_wait_data(struct sock *sk, long *timeo)
823 DEFINE_WAIT_FUNC(wait, woken_wake_function);
824 struct mptcp_sock *msk = mptcp_sk(sk);
826 add_wait_queue(sk_sleep(sk), &wait);
827 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
829 sk_wait_event(sk, timeo,
830 test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait);
832 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
833 remove_wait_queue(sk_sleep(sk), &wait);
836 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
837 struct msghdr *msg,
838 size_t len)
840 struct sock *sk = (struct sock *)msk;
841 struct sk_buff *skb;
842 int copied = 0;
844 while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
845 u32 offset = MPTCP_SKB_CB(skb)->offset;
846 u32 data_len = skb->len - offset;
847 u32 count = min_t(size_t, len - copied, data_len);
848 int err;
850 err = skb_copy_datagram_msg(skb, offset, msg, count);
851 if (unlikely(err < 0)) {
852 if (!copied)
853 return err;
854 break;
857 copied += count;
859 if (count < data_len) {
860 MPTCP_SKB_CB(skb)->offset += count;
861 break;
864 __skb_unlink(skb, &sk->sk_receive_queue);
865 __kfree_skb(skb);
867 if (copied >= len)
868 break;
871 return copied;
874 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
876 unsigned int moved = 0;
877 bool done;
879 do {
880 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
882 if (!ssk)
883 break;
885 lock_sock(ssk);
886 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
887 release_sock(ssk);
888 } while (!done);
890 return moved > 0;
893 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
894 int nonblock, int flags, int *addr_len)
896 struct mptcp_sock *msk = mptcp_sk(sk);
897 struct socket *ssock;
898 int copied = 0;
899 int target;
900 long timeo;
902 if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT))
903 return -EOPNOTSUPP;
905 lock_sock(sk);
906 ssock = __mptcp_tcp_fallback(msk);
907 if (unlikely(ssock)) {
908 fallback:
909 release_sock(sk);
910 pr_debug("fallback-read subflow=%p",
911 mptcp_subflow_ctx(ssock->sk));
912 copied = sock_recvmsg(ssock, msg, flags);
913 return copied;
916 timeo = sock_rcvtimeo(sk, nonblock);
918 len = min_t(size_t, len, INT_MAX);
919 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
920 __mptcp_flush_join_list(msk);
922 while (len > (size_t)copied) {
923 int bytes_read;
925 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied);
926 if (unlikely(bytes_read < 0)) {
927 if (!copied)
928 copied = bytes_read;
929 goto out_err;
932 copied += bytes_read;
934 if (skb_queue_empty(&sk->sk_receive_queue) &&
935 __mptcp_move_skbs(msk))
936 continue;
938 /* only the master socket status is relevant here. The exit
939 * conditions mirror closely tcp_recvmsg()
941 if (copied >= target)
942 break;
944 if (copied) {
945 if (sk->sk_err ||
946 sk->sk_state == TCP_CLOSE ||
947 (sk->sk_shutdown & RCV_SHUTDOWN) ||
948 !timeo ||
949 signal_pending(current))
950 break;
951 } else {
952 if (sk->sk_err) {
953 copied = sock_error(sk);
954 break;
957 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
958 mptcp_check_for_eof(msk);
960 if (sk->sk_shutdown & RCV_SHUTDOWN)
961 break;
963 if (sk->sk_state == TCP_CLOSE) {
964 copied = -ENOTCONN;
965 break;
968 if (!timeo) {
969 copied = -EAGAIN;
970 break;
973 if (signal_pending(current)) {
974 copied = sock_intr_errno(timeo);
975 break;
979 pr_debug("block timeout %ld", timeo);
980 mptcp_wait_data(sk, &timeo);
981 ssock = __mptcp_tcp_fallback(msk);
982 if (unlikely(ssock))
983 goto fallback;
986 if (skb_queue_empty(&sk->sk_receive_queue)) {
987 /* entire backlog drained, clear DATA_READY. */
988 clear_bit(MPTCP_DATA_READY, &msk->flags);
990 /* .. race-breaker: ssk might have gotten new data
991 * after last __mptcp_move_skbs() returned false.
993 if (unlikely(__mptcp_move_skbs(msk)))
994 set_bit(MPTCP_DATA_READY, &msk->flags);
995 } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) {
996 /* data to read but mptcp_wait_data() cleared DATA_READY */
997 set_bit(MPTCP_DATA_READY, &msk->flags);
999 out_err:
1000 release_sock(sk);
1001 return copied;
1004 static void mptcp_retransmit_handler(struct sock *sk)
1006 struct mptcp_sock *msk = mptcp_sk(sk);
1008 if (atomic64_read(&msk->snd_una) == msk->write_seq) {
1009 mptcp_stop_timer(sk);
1010 } else {
1011 set_bit(MPTCP_WORK_RTX, &msk->flags);
1012 if (schedule_work(&msk->work))
1013 sock_hold(sk);
1017 static void mptcp_retransmit_timer(struct timer_list *t)
1019 struct inet_connection_sock *icsk = from_timer(icsk, t,
1020 icsk_retransmit_timer);
1021 struct sock *sk = &icsk->icsk_inet.sk;
1023 bh_lock_sock(sk);
1024 if (!sock_owned_by_user(sk)) {
1025 mptcp_retransmit_handler(sk);
1026 } else {
1027 /* delegate our work to tcp_release_cb() */
1028 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED,
1029 &sk->sk_tsq_flags))
1030 sock_hold(sk);
1032 bh_unlock_sock(sk);
1033 sock_put(sk);
1036 /* Find an idle subflow. Return NULL if there is unacked data at tcp
1037 * level.
1039 * A backup subflow is returned only if that is the only kind available.
1041 static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
1043 struct mptcp_subflow_context *subflow;
1044 struct sock *backup = NULL;
1046 sock_owned_by_me((const struct sock *)msk);
1048 mptcp_for_each_subflow(msk, subflow) {
1049 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1051 /* still data outstanding at TCP level? Don't retransmit. */
1052 if (!tcp_write_queue_empty(ssk))
1053 return NULL;
1055 if (subflow->backup) {
1056 if (!backup)
1057 backup = ssk;
1058 continue;
1061 return ssk;
1064 return backup;
1067 /* subflow sockets can be either outgoing (connect) or incoming
1068 * (accept).
1070 * Outgoing subflows use in-kernel sockets.
1071 * Incoming subflows do not have their own 'struct socket' allocated,
1072 * so we need to use tcp_close() after detaching them from the mptcp
1073 * parent socket.
1075 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
1076 struct mptcp_subflow_context *subflow,
1077 long timeout)
1079 struct socket *sock = READ_ONCE(ssk->sk_socket);
1081 list_del(&subflow->node);
1083 if (sock && sock != sk->sk_socket) {
1084 /* outgoing subflow */
1085 sock_release(sock);
1086 } else {
1087 /* incoming subflow */
1088 tcp_close(ssk, timeout);
1092 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
1094 return 0;
1097 static void mptcp_worker(struct work_struct *work)
1099 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
1100 struct sock *ssk, *sk = &msk->sk.icsk_inet.sk;
1101 int orig_len, orig_offset, ret, mss_now = 0, size_goal = 0;
1102 struct mptcp_data_frag *dfrag;
1103 u64 orig_write_seq;
1104 size_t copied = 0;
1105 struct msghdr msg;
1106 long timeo = 0;
1108 lock_sock(sk);
1109 mptcp_clean_una(sk);
1110 __mptcp_flush_join_list(msk);
1111 __mptcp_move_skbs(msk);
1113 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
1114 mptcp_check_for_eof(msk);
1116 if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
1117 goto unlock;
1119 dfrag = mptcp_rtx_head(sk);
1120 if (!dfrag)
1121 goto unlock;
1123 ssk = mptcp_subflow_get_retrans(msk);
1124 if (!ssk)
1125 goto reset_unlock;
1127 lock_sock(ssk);
1129 msg.msg_flags = MSG_DONTWAIT;
1130 orig_len = dfrag->data_len;
1131 orig_offset = dfrag->offset;
1132 orig_write_seq = dfrag->data_seq;
1133 while (dfrag->data_len > 0) {
1134 ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo, &mss_now,
1135 &size_goal);
1136 if (ret < 0)
1137 break;
1139 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
1140 copied += ret;
1141 dfrag->data_len -= ret;
1142 dfrag->offset += ret;
1144 if (copied)
1145 tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle,
1146 size_goal);
1148 dfrag->data_seq = orig_write_seq;
1149 dfrag->offset = orig_offset;
1150 dfrag->data_len = orig_len;
1152 mptcp_set_timeout(sk, ssk);
1153 release_sock(ssk);
1155 reset_unlock:
1156 if (!mptcp_timer_pending(sk))
1157 mptcp_reset_timer(sk);
1159 unlock:
1160 release_sock(sk);
1161 sock_put(sk);
1164 static int __mptcp_init_sock(struct sock *sk)
1166 struct mptcp_sock *msk = mptcp_sk(sk);
1168 spin_lock_init(&msk->join_list_lock);
1170 INIT_LIST_HEAD(&msk->conn_list);
1171 INIT_LIST_HEAD(&msk->join_list);
1172 INIT_LIST_HEAD(&msk->rtx_queue);
1173 __set_bit(MPTCP_SEND_SPACE, &msk->flags);
1174 INIT_WORK(&msk->work, mptcp_worker);
1176 msk->first = NULL;
1177 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
1179 mptcp_pm_data_init(msk);
1181 /* re-use the csk retrans timer for MPTCP-level retrans */
1182 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
1184 return 0;
1187 static int mptcp_init_sock(struct sock *sk)
1189 struct net *net = sock_net(sk);
1190 int ret;
1192 if (!mptcp_is_enabled(net))
1193 return -ENOPROTOOPT;
1195 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
1196 return -ENOMEM;
1198 ret = __mptcp_init_sock(sk);
1199 if (ret)
1200 return ret;
1202 sk_sockets_allocated_inc(sk);
1203 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[2];
1205 return 0;
1208 static void __mptcp_clear_xmit(struct sock *sk)
1210 struct mptcp_sock *msk = mptcp_sk(sk);
1211 struct mptcp_data_frag *dtmp, *dfrag;
1213 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
1215 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
1216 dfrag_clear(sk, dfrag);
1219 static void mptcp_cancel_work(struct sock *sk)
1221 struct mptcp_sock *msk = mptcp_sk(sk);
1223 if (cancel_work_sync(&msk->work))
1224 sock_put(sk);
1227 static void mptcp_subflow_shutdown(struct sock *ssk, int how,
1228 bool data_fin_tx_enable, u64 data_fin_tx_seq)
1230 lock_sock(ssk);
1232 switch (ssk->sk_state) {
1233 case TCP_LISTEN:
1234 if (!(how & RCV_SHUTDOWN))
1235 break;
1236 /* fall through */
1237 case TCP_SYN_SENT:
1238 tcp_disconnect(ssk, O_NONBLOCK);
1239 break;
1240 default:
1241 if (data_fin_tx_enable) {
1242 struct mptcp_subflow_context *subflow;
1244 subflow = mptcp_subflow_ctx(ssk);
1245 subflow->data_fin_tx_seq = data_fin_tx_seq;
1246 subflow->data_fin_tx_enable = 1;
1249 ssk->sk_shutdown |= how;
1250 tcp_shutdown(ssk, how);
1251 break;
1254 /* Wake up anyone sleeping in poll. */
1255 ssk->sk_state_change(ssk);
1256 release_sock(ssk);
1259 /* Called with msk lock held, releases such lock before returning */
1260 static void mptcp_close(struct sock *sk, long timeout)
1262 struct mptcp_subflow_context *subflow, *tmp;
1263 struct mptcp_sock *msk = mptcp_sk(sk);
1264 LIST_HEAD(conn_list);
1265 u64 data_fin_tx_seq;
1267 lock_sock(sk);
1269 inet_sk_state_store(sk, TCP_CLOSE);
1271 /* be sure to always acquire the join list lock, to sync vs
1272 * mptcp_finish_join().
1274 spin_lock_bh(&msk->join_list_lock);
1275 list_splice_tail_init(&msk->join_list, &msk->conn_list);
1276 spin_unlock_bh(&msk->join_list_lock);
1277 list_splice_init(&msk->conn_list, &conn_list);
1279 data_fin_tx_seq = msk->write_seq;
1281 __mptcp_clear_xmit(sk);
1283 release_sock(sk);
1285 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
1286 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1288 subflow->data_fin_tx_seq = data_fin_tx_seq;
1289 subflow->data_fin_tx_enable = 1;
1290 __mptcp_close_ssk(sk, ssk, subflow, timeout);
1293 mptcp_cancel_work(sk);
1294 mptcp_pm_close(msk);
1296 __skb_queue_purge(&sk->sk_receive_queue);
1298 sk_common_release(sk);
1301 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
1303 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1304 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
1305 struct ipv6_pinfo *msk6 = inet6_sk(msk);
1307 msk->sk_v6_daddr = ssk->sk_v6_daddr;
1308 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
1310 if (msk6 && ssk6) {
1311 msk6->saddr = ssk6->saddr;
1312 msk6->flow_label = ssk6->flow_label;
1314 #endif
1316 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
1317 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
1318 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
1319 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
1320 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
1321 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
1324 static int mptcp_disconnect(struct sock *sk, int flags)
1326 /* Should never be called.
1327 * inet_stream_connect() calls ->disconnect, but that
1328 * refers to the subflow socket, not the mptcp one.
1330 WARN_ON_ONCE(1);
1331 return 0;
1334 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1335 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
1337 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
1339 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
1341 #endif
1343 struct sock *mptcp_sk_clone(const struct sock *sk,
1344 const struct mptcp_options_received *mp_opt,
1345 struct request_sock *req)
1347 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1348 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
1349 struct mptcp_sock *msk;
1350 u64 ack_seq;
1352 if (!nsk)
1353 return NULL;
1355 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1356 if (nsk->sk_family == AF_INET6)
1357 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
1358 #endif
1360 __mptcp_init_sock(nsk);
1362 msk = mptcp_sk(nsk);
1363 msk->local_key = subflow_req->local_key;
1364 msk->token = subflow_req->token;
1365 msk->subflow = NULL;
1367 if (unlikely(mptcp_token_new_accept(subflow_req->token, nsk))) {
1368 nsk->sk_state = TCP_CLOSE;
1369 bh_unlock_sock(nsk);
1371 /* we can't call into mptcp_close() here - possible BH context
1372 * free the sock directly.
1373 * sk_clone_lock() sets nsk refcnt to two, hence call sk_free()
1374 * too.
1376 sk_common_release(nsk);
1377 sk_free(nsk);
1378 return NULL;
1381 msk->write_seq = subflow_req->idsn + 1;
1382 atomic64_set(&msk->snd_una, msk->write_seq);
1383 if (mp_opt->mp_capable) {
1384 msk->can_ack = true;
1385 msk->remote_key = mp_opt->sndr_key;
1386 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
1387 ack_seq++;
1388 msk->ack_seq = ack_seq;
1391 sock_reset_flag(nsk, SOCK_RCU_FREE);
1392 /* will be fully established after successful MPC subflow creation */
1393 inet_sk_state_store(nsk, TCP_SYN_RECV);
1394 bh_unlock_sock(nsk);
1396 /* keep a single reference */
1397 __sock_put(nsk);
1398 return nsk;
1401 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
1402 bool kern)
1404 struct mptcp_sock *msk = mptcp_sk(sk);
1405 struct socket *listener;
1406 struct sock *newsk;
1408 listener = __mptcp_nmpc_socket(msk);
1409 if (WARN_ON_ONCE(!listener)) {
1410 *err = -EINVAL;
1411 return NULL;
1414 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
1415 newsk = inet_csk_accept(listener->sk, flags, err, kern);
1416 if (!newsk)
1417 return NULL;
1419 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
1421 if (sk_is_mptcp(newsk)) {
1422 struct mptcp_subflow_context *subflow;
1423 struct sock *new_mptcp_sock;
1424 struct sock *ssk = newsk;
1426 subflow = mptcp_subflow_ctx(newsk);
1427 new_mptcp_sock = subflow->conn;
1429 /* is_mptcp should be false if subflow->conn is missing, see
1430 * subflow_syn_recv_sock()
1432 if (WARN_ON_ONCE(!new_mptcp_sock)) {
1433 tcp_sk(newsk)->is_mptcp = 0;
1434 return newsk;
1437 /* acquire the 2nd reference for the owning socket */
1438 sock_hold(new_mptcp_sock);
1440 local_bh_disable();
1441 bh_lock_sock(new_mptcp_sock);
1442 msk = mptcp_sk(new_mptcp_sock);
1443 msk->first = newsk;
1445 newsk = new_mptcp_sock;
1446 mptcp_copy_inaddrs(newsk, ssk);
1447 list_add(&subflow->node, &msk->conn_list);
1448 inet_sk_state_store(newsk, TCP_ESTABLISHED);
1450 bh_unlock_sock(new_mptcp_sock);
1452 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
1453 local_bh_enable();
1454 } else {
1455 MPTCP_INC_STATS(sock_net(sk),
1456 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
1459 return newsk;
1462 static void mptcp_destroy(struct sock *sk)
1464 struct mptcp_sock *msk = mptcp_sk(sk);
1466 mptcp_token_destroy(msk->token);
1467 if (msk->cached_ext)
1468 __skb_ext_put(msk->cached_ext);
1470 sk_sockets_allocated_dec(sk);
1473 static int mptcp_setsockopt(struct sock *sk, int level, int optname,
1474 char __user *optval, unsigned int optlen)
1476 struct mptcp_sock *msk = mptcp_sk(sk);
1477 struct socket *ssock;
1479 pr_debug("msk=%p", msk);
1481 /* @@ the meaning of setsockopt() when the socket is connected and
1482 * there are multiple subflows is not yet defined. It is up to the
1483 * MPTCP-level socket to configure the subflows until the subflow
1484 * is in TCP fallback, when TCP socket options are passed through
1485 * to the one remaining subflow.
1487 lock_sock(sk);
1488 ssock = __mptcp_tcp_fallback(msk);
1489 release_sock(sk);
1490 if (ssock)
1491 return tcp_setsockopt(ssock->sk, level, optname, optval,
1492 optlen);
1494 return -EOPNOTSUPP;
1497 static int mptcp_getsockopt(struct sock *sk, int level, int optname,
1498 char __user *optval, int __user *option)
1500 struct mptcp_sock *msk = mptcp_sk(sk);
1501 struct socket *ssock;
1503 pr_debug("msk=%p", msk);
1505 /* @@ the meaning of setsockopt() when the socket is connected and
1506 * there are multiple subflows is not yet defined. It is up to the
1507 * MPTCP-level socket to configure the subflows until the subflow
1508 * is in TCP fallback, when socket options are passed through
1509 * to the one remaining subflow.
1511 lock_sock(sk);
1512 ssock = __mptcp_tcp_fallback(msk);
1513 release_sock(sk);
1514 if (ssock)
1515 return tcp_getsockopt(ssock->sk, level, optname, optval,
1516 option);
1518 return -EOPNOTSUPP;
1521 #define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \
1522 TCPF_WRITE_TIMER_DEFERRED)
1524 /* this is very alike tcp_release_cb() but we must handle differently a
1525 * different set of events
1527 static void mptcp_release_cb(struct sock *sk)
1529 unsigned long flags, nflags;
1531 do {
1532 flags = sk->sk_tsq_flags;
1533 if (!(flags & MPTCP_DEFERRED_ALL))
1534 return;
1535 nflags = flags & ~MPTCP_DEFERRED_ALL;
1536 } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
1538 sock_release_ownership(sk);
1540 if (flags & TCPF_DELACK_TIMER_DEFERRED) {
1541 struct mptcp_sock *msk = mptcp_sk(sk);
1542 struct sock *ssk;
1544 ssk = mptcp_subflow_recv_lookup(msk);
1545 if (!ssk || !schedule_work(&msk->work))
1546 __sock_put(sk);
1549 if (flags & TCPF_WRITE_TIMER_DEFERRED) {
1550 mptcp_retransmit_handler(sk);
1551 __sock_put(sk);
1555 static int mptcp_get_port(struct sock *sk, unsigned short snum)
1557 struct mptcp_sock *msk = mptcp_sk(sk);
1558 struct socket *ssock;
1560 ssock = __mptcp_nmpc_socket(msk);
1561 pr_debug("msk=%p, subflow=%p", msk, ssock);
1562 if (WARN_ON_ONCE(!ssock))
1563 return -EINVAL;
1565 return inet_csk_get_port(ssock->sk, snum);
1568 void mptcp_finish_connect(struct sock *ssk)
1570 struct mptcp_subflow_context *subflow;
1571 struct mptcp_sock *msk;
1572 struct sock *sk;
1573 u64 ack_seq;
1575 subflow = mptcp_subflow_ctx(ssk);
1576 sk = subflow->conn;
1577 msk = mptcp_sk(sk);
1579 if (!subflow->mp_capable) {
1580 MPTCP_INC_STATS(sock_net(sk),
1581 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
1582 return;
1585 pr_debug("msk=%p, token=%u", sk, subflow->token);
1587 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
1588 ack_seq++;
1589 subflow->map_seq = ack_seq;
1590 subflow->map_subflow_seq = 1;
1591 subflow->rel_write_seq = 1;
1593 /* the socket is not connected yet, no msk/subflow ops can access/race
1594 * accessing the field below
1596 WRITE_ONCE(msk->remote_key, subflow->remote_key);
1597 WRITE_ONCE(msk->local_key, subflow->local_key);
1598 WRITE_ONCE(msk->token, subflow->token);
1599 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
1600 WRITE_ONCE(msk->ack_seq, ack_seq);
1601 WRITE_ONCE(msk->can_ack, 1);
1602 atomic64_set(&msk->snd_una, msk->write_seq);
1604 mptcp_pm_new_connection(msk, 0);
1607 static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
1609 write_lock_bh(&sk->sk_callback_lock);
1610 rcu_assign_pointer(sk->sk_wq, &parent->wq);
1611 sk_set_socket(sk, parent);
1612 sk->sk_uid = SOCK_INODE(parent)->i_uid;
1613 write_unlock_bh(&sk->sk_callback_lock);
1616 bool mptcp_finish_join(struct sock *sk)
1618 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1619 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1620 struct sock *parent = (void *)msk;
1621 struct socket *parent_sock;
1622 bool ret;
1624 pr_debug("msk=%p, subflow=%p", msk, subflow);
1626 /* mptcp socket already closing? */
1627 if (inet_sk_state_load(parent) != TCP_ESTABLISHED)
1628 return false;
1630 if (!msk->pm.server_side)
1631 return true;
1633 if (!mptcp_pm_allow_new_subflow(msk))
1634 return false;
1636 /* active connections are already on conn_list, and we can't acquire
1637 * msk lock here.
1638 * use the join list lock as synchronization point and double-check
1639 * msk status to avoid racing with mptcp_close()
1641 spin_lock_bh(&msk->join_list_lock);
1642 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
1643 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node)))
1644 list_add_tail(&subflow->node, &msk->join_list);
1645 spin_unlock_bh(&msk->join_list_lock);
1646 if (!ret)
1647 return false;
1649 /* attach to msk socket only after we are sure he will deal with us
1650 * at close time
1652 parent_sock = READ_ONCE(parent->sk_socket);
1653 if (parent_sock && !sk->sk_socket)
1654 mptcp_sock_graft(sk, parent_sock);
1655 subflow->map_seq = msk->ack_seq;
1656 return true;
1659 bool mptcp_sk_is_subflow(const struct sock *sk)
1661 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1663 return subflow->mp_join == 1;
1666 static bool mptcp_memory_free(const struct sock *sk, int wake)
1668 struct mptcp_sock *msk = mptcp_sk(sk);
1670 return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true;
1673 static struct proto mptcp_prot = {
1674 .name = "MPTCP",
1675 .owner = THIS_MODULE,
1676 .init = mptcp_init_sock,
1677 .disconnect = mptcp_disconnect,
1678 .close = mptcp_close,
1679 .accept = mptcp_accept,
1680 .setsockopt = mptcp_setsockopt,
1681 .getsockopt = mptcp_getsockopt,
1682 .shutdown = tcp_shutdown,
1683 .destroy = mptcp_destroy,
1684 .sendmsg = mptcp_sendmsg,
1685 .recvmsg = mptcp_recvmsg,
1686 .release_cb = mptcp_release_cb,
1687 .hash = inet_hash,
1688 .unhash = inet_unhash,
1689 .get_port = mptcp_get_port,
1690 .sockets_allocated = &mptcp_sockets_allocated,
1691 .memory_allocated = &tcp_memory_allocated,
1692 .memory_pressure = &tcp_memory_pressure,
1693 .stream_memory_free = mptcp_memory_free,
1694 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
1695 .sysctl_mem = sysctl_tcp_mem,
1696 .obj_size = sizeof(struct mptcp_sock),
1697 .no_autobind = true,
1700 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1702 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1703 struct socket *ssock;
1704 int err;
1706 lock_sock(sock->sk);
1707 ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
1708 if (IS_ERR(ssock)) {
1709 err = PTR_ERR(ssock);
1710 goto unlock;
1713 err = ssock->ops->bind(ssock, uaddr, addr_len);
1714 if (!err)
1715 mptcp_copy_inaddrs(sock->sk, ssock->sk);
1717 unlock:
1718 release_sock(sock->sk);
1719 return err;
1722 static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1723 int addr_len, int flags)
1725 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1726 struct socket *ssock;
1727 int err;
1729 lock_sock(sock->sk);
1730 if (sock->state != SS_UNCONNECTED && msk->subflow) {
1731 /* pending connection or invalid state, let existing subflow
1732 * cope with that
1734 ssock = msk->subflow;
1735 goto do_connect;
1738 ssock = __mptcp_socket_create(msk, TCP_SYN_SENT);
1739 if (IS_ERR(ssock)) {
1740 err = PTR_ERR(ssock);
1741 goto unlock;
1744 #ifdef CONFIG_TCP_MD5SIG
1745 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
1746 * TCP option space.
1748 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
1749 mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0;
1750 #endif
1752 do_connect:
1753 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
1754 sock->state = ssock->state;
1756 /* on successful connect, the msk state will be moved to established by
1757 * subflow_finish_connect()
1759 if (!err || err == EINPROGRESS)
1760 mptcp_copy_inaddrs(sock->sk, ssock->sk);
1761 else
1762 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
1764 unlock:
1765 release_sock(sock->sk);
1766 return err;
1769 static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr,
1770 int peer)
1772 if (sock->sk->sk_prot == &tcp_prot) {
1773 /* we are being invoked from __sys_accept4, after
1774 * mptcp_accept() has just accepted a non-mp-capable
1775 * flow: sk is a tcp_sk, not an mptcp one.
1777 * Hand the socket over to tcp so all further socket ops
1778 * bypass mptcp.
1780 sock->ops = &inet_stream_ops;
1783 return inet_getname(sock, uaddr, peer);
1786 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1787 static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr,
1788 int peer)
1790 if (sock->sk->sk_prot == &tcpv6_prot) {
1791 /* we are being invoked from __sys_accept4 after
1792 * mptcp_accept() has accepted a non-mp-capable
1793 * subflow: sk is a tcp_sk, not mptcp.
1795 * Hand the socket over to tcp so all further
1796 * socket ops bypass mptcp.
1798 sock->ops = &inet6_stream_ops;
1801 return inet6_getname(sock, uaddr, peer);
1803 #endif
1805 static int mptcp_listen(struct socket *sock, int backlog)
1807 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1808 struct socket *ssock;
1809 int err;
1811 pr_debug("msk=%p", msk);
1813 lock_sock(sock->sk);
1814 ssock = __mptcp_socket_create(msk, TCP_LISTEN);
1815 if (IS_ERR(ssock)) {
1816 err = PTR_ERR(ssock);
1817 goto unlock;
1820 sock_set_flag(sock->sk, SOCK_RCU_FREE);
1822 err = ssock->ops->listen(ssock, backlog);
1823 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
1824 if (!err)
1825 mptcp_copy_inaddrs(sock->sk, ssock->sk);
1827 unlock:
1828 release_sock(sock->sk);
1829 return err;
1832 static bool is_tcp_proto(const struct proto *p)
1834 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1835 return p == &tcp_prot || p == &tcpv6_prot;
1836 #else
1837 return p == &tcp_prot;
1838 #endif
1841 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
1842 int flags, bool kern)
1844 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1845 struct socket *ssock;
1846 int err;
1848 pr_debug("msk=%p", msk);
1850 lock_sock(sock->sk);
1851 if (sock->sk->sk_state != TCP_LISTEN)
1852 goto unlock_fail;
1854 ssock = __mptcp_nmpc_socket(msk);
1855 if (!ssock)
1856 goto unlock_fail;
1858 sock_hold(ssock->sk);
1859 release_sock(sock->sk);
1861 err = ssock->ops->accept(sock, newsock, flags, kern);
1862 if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) {
1863 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
1864 struct mptcp_subflow_context *subflow;
1866 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
1867 * This is needed so NOSPACE flag can be set from tcp stack.
1869 __mptcp_flush_join_list(msk);
1870 list_for_each_entry(subflow, &msk->conn_list, node) {
1871 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1873 if (!ssk->sk_socket)
1874 mptcp_sock_graft(ssk, newsock);
1878 sock_put(ssock->sk);
1879 return err;
1881 unlock_fail:
1882 release_sock(sock->sk);
1883 return -EINVAL;
1886 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
1887 struct poll_table_struct *wait)
1889 struct sock *sk = sock->sk;
1890 struct mptcp_sock *msk;
1891 struct socket *ssock;
1892 __poll_t mask = 0;
1894 msk = mptcp_sk(sk);
1895 lock_sock(sk);
1896 ssock = __mptcp_tcp_fallback(msk);
1897 if (!ssock)
1898 ssock = __mptcp_nmpc_socket(msk);
1899 if (ssock) {
1900 mask = ssock->ops->poll(file, ssock, wait);
1901 release_sock(sk);
1902 return mask;
1905 release_sock(sk);
1906 sock_poll_wait(file, sock, wait);
1907 lock_sock(sk);
1909 if (test_bit(MPTCP_DATA_READY, &msk->flags))
1910 mask = EPOLLIN | EPOLLRDNORM;
1911 if (sk_stream_is_writeable(sk) &&
1912 test_bit(MPTCP_SEND_SPACE, &msk->flags))
1913 mask |= EPOLLOUT | EPOLLWRNORM;
1914 if (sk->sk_shutdown & RCV_SHUTDOWN)
1915 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1917 release_sock(sk);
1919 return mask;
1922 static int mptcp_shutdown(struct socket *sock, int how)
1924 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1925 struct mptcp_subflow_context *subflow;
1926 struct socket *ssock;
1927 int ret = 0;
1929 pr_debug("sk=%p, how=%d", msk, how);
1931 lock_sock(sock->sk);
1932 ssock = __mptcp_tcp_fallback(msk);
1933 if (ssock) {
1934 release_sock(sock->sk);
1935 return inet_shutdown(ssock, how);
1938 if (how == SHUT_WR || how == SHUT_RDWR)
1939 inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
1941 how++;
1943 if ((how & ~SHUTDOWN_MASK) || !how) {
1944 ret = -EINVAL;
1945 goto out_unlock;
1948 if (sock->state == SS_CONNECTING) {
1949 if ((1 << sock->sk->sk_state) &
1950 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
1951 sock->state = SS_DISCONNECTING;
1952 else
1953 sock->state = SS_CONNECTED;
1956 __mptcp_flush_join_list(msk);
1957 mptcp_for_each_subflow(msk, subflow) {
1958 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
1960 mptcp_subflow_shutdown(tcp_sk, how, 1, msk->write_seq);
1963 out_unlock:
1964 release_sock(sock->sk);
1966 return ret;
1969 static const struct proto_ops mptcp_stream_ops = {
1970 .family = PF_INET,
1971 .owner = THIS_MODULE,
1972 .release = inet_release,
1973 .bind = mptcp_bind,
1974 .connect = mptcp_stream_connect,
1975 .socketpair = sock_no_socketpair,
1976 .accept = mptcp_stream_accept,
1977 .getname = mptcp_v4_getname,
1978 .poll = mptcp_poll,
1979 .ioctl = inet_ioctl,
1980 .gettstamp = sock_gettstamp,
1981 .listen = mptcp_listen,
1982 .shutdown = mptcp_shutdown,
1983 .setsockopt = sock_common_setsockopt,
1984 .getsockopt = sock_common_getsockopt,
1985 .sendmsg = inet_sendmsg,
1986 .recvmsg = inet_recvmsg,
1987 .mmap = sock_no_mmap,
1988 .sendpage = inet_sendpage,
1989 #ifdef CONFIG_COMPAT
1990 .compat_setsockopt = compat_sock_common_setsockopt,
1991 .compat_getsockopt = compat_sock_common_getsockopt,
1992 #endif
1995 static struct inet_protosw mptcp_protosw = {
1996 .type = SOCK_STREAM,
1997 .protocol = IPPROTO_MPTCP,
1998 .prot = &mptcp_prot,
1999 .ops = &mptcp_stream_ops,
2000 .flags = INET_PROTOSW_ICSK,
2003 void mptcp_proto_init(void)
2005 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
2007 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
2008 panic("Failed to allocate MPTCP pcpu counter\n");
2010 mptcp_subflow_init();
2011 mptcp_pm_init();
2013 if (proto_register(&mptcp_prot, 1) != 0)
2014 panic("Failed to register MPTCP proto.\n");
2016 inet_register_protosw(&mptcp_protosw);
2018 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
2021 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2022 static const struct proto_ops mptcp_v6_stream_ops = {
2023 .family = PF_INET6,
2024 .owner = THIS_MODULE,
2025 .release = inet6_release,
2026 .bind = mptcp_bind,
2027 .connect = mptcp_stream_connect,
2028 .socketpair = sock_no_socketpair,
2029 .accept = mptcp_stream_accept,
2030 .getname = mptcp_v6_getname,
2031 .poll = mptcp_poll,
2032 .ioctl = inet6_ioctl,
2033 .gettstamp = sock_gettstamp,
2034 .listen = mptcp_listen,
2035 .shutdown = mptcp_shutdown,
2036 .setsockopt = sock_common_setsockopt,
2037 .getsockopt = sock_common_getsockopt,
2038 .sendmsg = inet6_sendmsg,
2039 .recvmsg = inet6_recvmsg,
2040 .mmap = sock_no_mmap,
2041 .sendpage = inet_sendpage,
2042 #ifdef CONFIG_COMPAT
2043 .compat_setsockopt = compat_sock_common_setsockopt,
2044 .compat_getsockopt = compat_sock_common_getsockopt,
2045 #endif
2048 static struct proto mptcp_v6_prot;
2050 static void mptcp_v6_destroy(struct sock *sk)
2052 mptcp_destroy(sk);
2053 inet6_destroy_sock(sk);
2056 static struct inet_protosw mptcp_v6_protosw = {
2057 .type = SOCK_STREAM,
2058 .protocol = IPPROTO_MPTCP,
2059 .prot = &mptcp_v6_prot,
2060 .ops = &mptcp_v6_stream_ops,
2061 .flags = INET_PROTOSW_ICSK,
2064 int mptcp_proto_v6_init(void)
2066 int err;
2068 mptcp_v6_prot = mptcp_prot;
2069 strcpy(mptcp_v6_prot.name, "MPTCPv6");
2070 mptcp_v6_prot.slab = NULL;
2071 mptcp_v6_prot.destroy = mptcp_v6_destroy;
2072 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
2074 err = proto_register(&mptcp_v6_prot, 1);
2075 if (err)
2076 return err;
2078 err = inet6_register_protosw(&mptcp_v6_protosw);
2079 if (err)
2080 proto_unregister(&mptcp_v6_prot);
2082 return err;
2084 #endif