blob: 3980fbb6f31ea154e12ef41f896916892eb015b2 [file] [log] [blame]
Mat Martineauf870fa02020-01-21 16:56:15 -08001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
7#define pr_fmt(fmt) "MPTCP: " fmt
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/netdevice.h>
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -080012#include <linux/sched/signal.h>
13#include <linux/atomic.h>
Mat Martineauf870fa02020-01-21 16:56:15 -080014#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>
Peter Krystadcf7da0d2020-01-21 16:56:19 -080019#if IS_ENABLED(CONFIG_MPTCP_IPV6)
20#include <net/transp_v6.h>
21#endif
Mat Martineauf870fa02020-01-21 16:56:15 -080022#include <net/mptcp.h>
23#include "protocol.h"
Florian Westphalfc518952020-03-27 14:48:50 -070024#include "mib.h"
Mat Martineauf870fa02020-01-21 16:56:15 -080025
Peter Krystad2303f992020-01-21 16:56:17 -080026#define MPTCP_SAME_STATE TCP_MAX_STATES
27
Florian Westphalb0519de2020-02-06 00:39:37 +010028#if IS_ENABLED(CONFIG_MPTCP_IPV6)
29struct mptcp6_sock {
30 struct mptcp_sock msk;
31 struct ipv6_pinfo np;
32};
33#endif
34
Florian Westphal6771bfd2020-02-26 10:14:48 +010035struct mptcp_skb_cb {
36 u32 offset;
37};
38
39#define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
40
Paolo Abenid0272362020-03-27 14:48:45 -070041static struct percpu_counter mptcp_sockets_allocated;
42
Peter Krystad2303f992020-01-21 16:56:17 -080043/* 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.
46 */
47static struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
48{
Christoph Paaschd22f4982020-01-21 16:56:32 -080049 if (!msk->subflow || READ_ONCE(msk->can_ack))
Peter Krystad2303f992020-01-21 16:56:17 -080050 return NULL;
51
52 return msk->subflow;
53}
54
Paolo Abeni8ab183d2020-01-21 16:56:33 -080055static bool __mptcp_needs_tcp_fallback(const struct mptcp_sock *msk)
56{
57 return msk->first && !sk_is_mptcp(msk->first);
58}
59
Florian Westphal0b4f33d2020-04-02 13:44:51 +020060static struct socket *mptcp_is_tcpsk(struct sock *sk)
61{
62 struct socket *sock = sk->sk_socket;
63
64 if (sock->sk != sk)
65 return NULL;
66
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.
71 *
72 * Hand the socket over to tcp so all further socket ops
73 * bypass mptcp.
74 */
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
82 }
83
84 return NULL;
85}
86
Paolo Abeni8ab183d2020-01-21 16:56:33 -080087static struct socket *__mptcp_tcp_fallback(struct mptcp_sock *msk)
Peter Krystadcec37a62020-01-21 16:56:18 -080088{
Florian Westphal0b4f33d2020-04-02 13:44:51 +020089 struct socket *sock;
90
Peter Krystadcec37a62020-01-21 16:56:18 -080091 sock_owned_by_me((const struct sock *)msk);
92
Florian Westphal0b4f33d2020-04-02 13:44:51 +020093 sock = mptcp_is_tcpsk((struct sock *)msk);
94 if (unlikely(sock))
95 return sock;
96
Paolo Abeni8ab183d2020-01-21 16:56:33 -080097 if (likely(!__mptcp_needs_tcp_fallback(msk)))
Peter Krystadcec37a62020-01-21 16:56:18 -080098 return NULL;
99
Florian Westphale1546592020-04-11 21:05:01 +0200100 return msk->subflow;
Peter Krystadcec37a62020-01-21 16:56:18 -0800101}
102
Peter Krystad2303f992020-01-21 16:56:17 -0800103static bool __mptcp_can_create_subflow(const struct mptcp_sock *msk)
104{
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800105 return !msk->first;
Peter Krystad2303f992020-01-21 16:56:17 -0800106}
107
108static struct socket *__mptcp_socket_create(struct mptcp_sock *msk, int state)
109{
110 struct mptcp_subflow_context *subflow;
111 struct sock *sk = (struct sock *)msk;
112 struct socket *ssock;
113 int err;
114
Florian Westphal0b4f33d2020-04-02 13:44:51 +0200115 ssock = __mptcp_tcp_fallback(msk);
116 if (unlikely(ssock))
117 return ssock;
118
Peter Krystad2303f992020-01-21 16:56:17 -0800119 ssock = __mptcp_nmpc_socket(msk);
120 if (ssock)
121 goto set_state;
122
123 if (!__mptcp_can_create_subflow(msk))
124 return ERR_PTR(-EINVAL);
125
126 err = mptcp_subflow_create_socket(sk, &ssock);
127 if (err)
128 return ERR_PTR(err);
129
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800130 msk->first = ssock->sk;
Peter Krystad2303f992020-01-21 16:56:17 -0800131 msk->subflow = ssock;
132 subflow = mptcp_subflow_ctx(ssock->sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800133 list_add(&subflow->node, &msk->conn_list);
Peter Krystad2303f992020-01-21 16:56:17 -0800134 subflow->request_mptcp = 1;
135
136set_state:
137 if (state != MPTCP_SAME_STATE)
138 inet_sk_state_store(sk, state);
139 return ssock;
140}
141
Florian Westphal6771bfd2020-02-26 10:14:48 +0100142static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
143 struct sk_buff *skb,
144 unsigned int offset, size_t copy_len)
145{
146 struct sock *sk = (struct sock *)msk;
Florian Westphal4e637c72020-05-25 23:41:13 +0200147 struct sk_buff *tail;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100148
149 __skb_unlink(skb, &ssk->sk_receive_queue);
Florian Westphal4e637c72020-05-25 23:41:13 +0200150
151 skb_ext_reset(skb);
152 skb_orphan(skb);
153 msk->ack_seq += copy_len;
154
155 tail = skb_peek_tail(&sk->sk_receive_queue);
156 if (offset == 0 && tail) {
157 bool fragstolen;
158 int delta;
159
160 if (skb_try_coalesce(tail, skb, &fragstolen, &delta)) {
161 kfree_skb_partial(skb, fragstolen);
162 atomic_add(delta, &sk->sk_rmem_alloc);
163 sk_mem_charge(sk, delta);
164 return;
165 }
166 }
167
Florian Westphal600911f2020-02-26 10:14:49 +0100168 skb_set_owner_r(skb, sk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100169 __skb_queue_tail(&sk->sk_receive_queue, skb);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100170 MPTCP_SKB_CB(skb)->offset = offset;
171}
172
Florian Westphalde06f572020-04-02 13:44:53 +0200173/* both sockets must be locked */
174static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk,
175 struct sock *ssk)
176{
177 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
178 u64 dsn = mptcp_subflow_get_mapped_dsn(subflow);
179
180 /* revalidate data sequence number.
181 *
182 * mptcp_subflow_data_available() is usually called
183 * without msk lock. Its unlikely (but possible)
184 * that msk->ack_seq has been advanced since the last
185 * call found in-sequence data.
186 */
187 if (likely(dsn == msk->ack_seq))
188 return true;
189
190 subflow->data_avail = 0;
191 return mptcp_subflow_data_available(ssk);
192}
193
Florian Westphal6771bfd2020-02-26 10:14:48 +0100194static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
195 struct sock *ssk,
196 unsigned int *bytes)
197{
198 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal600911f2020-02-26 10:14:49 +0100199 struct sock *sk = (struct sock *)msk;
Florian Westphal6771bfd2020-02-26 10:14:48 +0100200 unsigned int moved = 0;
201 bool more_data_avail;
202 struct tcp_sock *tp;
203 bool done = false;
Florian Westphal600911f2020-02-26 10:14:49 +0100204
Florian Westphalde06f572020-04-02 13:44:53 +0200205 if (!mptcp_subflow_dsn_valid(msk, ssk)) {
206 *bytes = 0;
207 return false;
208 }
209
Florian Westphalec339162020-03-06 21:29:46 +0100210 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
211 int rcvbuf = max(ssk->sk_rcvbuf, sk->sk_rcvbuf);
212
213 if (rcvbuf > sk->sk_rcvbuf)
214 sk->sk_rcvbuf = rcvbuf;
215 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100216
217 tp = tcp_sk(ssk);
218 do {
219 u32 map_remaining, offset;
220 u32 seq = tp->copied_seq;
221 struct sk_buff *skb;
222 bool fin;
223
224 /* try to move as much data as available */
225 map_remaining = subflow->map_data_len -
226 mptcp_subflow_get_map_offset(subflow);
227
228 skb = skb_peek(&ssk->sk_receive_queue);
229 if (!skb)
230 break;
231
232 offset = seq - TCP_SKB_CB(skb)->seq;
233 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
234 if (fin) {
235 done = true;
236 seq++;
237 }
238
239 if (offset < skb->len) {
240 size_t len = skb->len - offset;
241
242 if (tp->urg_data)
243 done = true;
244
245 __mptcp_move_skb(msk, ssk, skb, offset, len);
246 seq += len;
247 moved += len;
248
249 if (WARN_ON_ONCE(map_remaining < len))
250 break;
251 } else {
252 WARN_ON_ONCE(!fin);
253 sk_eat_skb(ssk, skb);
254 done = true;
255 }
256
257 WRITE_ONCE(tp->copied_seq, seq);
258 more_data_avail = mptcp_subflow_data_available(ssk);
Florian Westphal600911f2020-02-26 10:14:49 +0100259
260 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf)) {
261 done = true;
262 break;
263 }
Florian Westphal6771bfd2020-02-26 10:14:48 +0100264 } while (more_data_avail);
265
266 *bytes = moved;
267
268 return done;
269}
270
Florian Westphal2e522132020-02-26 10:14:51 +0100271/* In most cases we will be able to lock the mptcp socket. If its already
272 * owned, we need to defer to the work queue to avoid ABBA deadlock.
273 */
274static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
275{
276 struct sock *sk = (struct sock *)msk;
277 unsigned int moved = 0;
278
279 if (READ_ONCE(sk->sk_lock.owned))
280 return false;
281
282 if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock)))
283 return false;
284
285 /* must re-check after taking the lock */
286 if (!READ_ONCE(sk->sk_lock.owned))
287 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
288
289 spin_unlock_bh(&sk->sk_lock.slock);
290
291 return moved > 0;
292}
293
294void mptcp_data_ready(struct sock *sk, struct sock *ssk)
Florian Westphal101f6f82020-02-26 10:14:46 +0100295{
296 struct mptcp_sock *msk = mptcp_sk(sk);
297
298 set_bit(MPTCP_DATA_READY, &msk->flags);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100299
Florian Westphal2e522132020-02-26 10:14:51 +0100300 if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) &&
301 move_skbs_to_msk(msk, ssk))
302 goto wake;
303
Florian Westphal600911f2020-02-26 10:14:49 +0100304 /* don't schedule if mptcp sk is (still) over limit */
305 if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf))
306 goto wake;
307
Paolo Abeni14c441b2020-02-26 10:14:52 +0100308 /* mptcp socket is owned, release_cb should retry */
309 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
310 &sk->sk_tsq_flags)) {
311 sock_hold(sk);
Florian Westphal6771bfd2020-02-26 10:14:48 +0100312
Paolo Abeni14c441b2020-02-26 10:14:52 +0100313 /* need to try again, its possible release_cb() has already
314 * been called after the test_and_set_bit() above.
315 */
316 move_skbs_to_msk(msk, ssk);
317 }
Florian Westphal600911f2020-02-26 10:14:49 +0100318wake:
Florian Westphal101f6f82020-02-26 10:14:46 +0100319 sk->sk_data_ready(sk);
320}
321
Peter Krystadec3edaa2020-03-27 14:48:40 -0700322static void __mptcp_flush_join_list(struct mptcp_sock *msk)
323{
324 if (likely(list_empty(&msk->join_list)))
325 return;
326
327 spin_lock_bh(&msk->join_list_lock);
328 list_splice_tail_init(&msk->join_list, &msk->conn_list);
329 spin_unlock_bh(&msk->join_list_lock);
330}
331
Paolo Abenib51f9b82020-03-27 14:48:44 -0700332static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
333{
334 long tout = ssk && inet_csk(ssk)->icsk_pending ?
335 inet_csk(ssk)->icsk_timeout - jiffies : 0;
336
337 if (tout <= 0)
338 tout = mptcp_sk(sk)->timer_ival;
339 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
340}
341
342static bool mptcp_timer_pending(struct sock *sk)
343{
344 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
345}
346
347static void mptcp_reset_timer(struct sock *sk)
348{
349 struct inet_connection_sock *icsk = inet_csk(sk);
350 unsigned long tout;
351
352 /* should never be called with mptcp level timer cleared */
353 tout = READ_ONCE(mptcp_sk(sk)->timer_ival);
354 if (WARN_ON_ONCE(!tout))
355 tout = TCP_RTO_MIN;
356 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
357}
358
359void mptcp_data_acked(struct sock *sk)
360{
361 mptcp_reset_timer(sk);
Paolo Abeni3b1d6212020-03-27 14:48:48 -0700362
363 if (!sk_stream_is_writeable(sk) &&
364 schedule_work(&mptcp_sk(sk)->work))
365 sock_hold(sk);
Paolo Abenib51f9b82020-03-27 14:48:44 -0700366}
367
Florian Westphal59832e22020-04-02 13:44:52 +0200368void mptcp_subflow_eof(struct sock *sk)
369{
370 struct mptcp_sock *msk = mptcp_sk(sk);
371
372 if (!test_and_set_bit(MPTCP_WORK_EOF, &msk->flags) &&
373 schedule_work(&msk->work))
374 sock_hold(sk);
375}
376
Paolo Abeni59698562020-06-10 10:47:41 +0200377static void mptcp_check_for_eof(struct mptcp_sock *msk)
378{
379 struct mptcp_subflow_context *subflow;
380 struct sock *sk = (struct sock *)msk;
381 int receivers = 0;
382
383 mptcp_for_each_subflow(msk, subflow)
384 receivers += !subflow->rx_eof;
385
386 if (!receivers && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
387 /* hopefully temporary hack: propagate shutdown status
388 * to msk, when all subflows agree on it
389 */
390 sk->sk_shutdown |= RCV_SHUTDOWN;
391
392 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
393 set_bit(MPTCP_DATA_READY, &msk->flags);
394 sk->sk_data_ready(sk);
395 }
396}
397
Paolo Abenib51f9b82020-03-27 14:48:44 -0700398static void mptcp_stop_timer(struct sock *sk)
399{
400 struct inet_connection_sock *icsk = inet_csk(sk);
401
402 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
403 mptcp_sk(sk)->timer_ival = 0;
404}
405
Mat Martineau6d0060f2020-01-21 16:56:23 -0800406static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
407{
Florian Westphal4930f482020-05-16 10:46:23 +0200408 const struct sock *sk = (const struct sock *)msk;
409
Mat Martineau6d0060f2020-01-21 16:56:23 -0800410 if (!msk->cached_ext)
Florian Westphal4930f482020-05-16 10:46:23 +0200411 msk->cached_ext = __skb_ext_alloc(sk->sk_allocation);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800412
413 return !!msk->cached_ext;
414}
415
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800416static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
417{
418 struct mptcp_subflow_context *subflow;
419 struct sock *sk = (struct sock *)msk;
420
421 sock_owned_by_me(sk);
422
423 mptcp_for_each_subflow(msk, subflow) {
424 if (subflow->data_avail)
425 return mptcp_subflow_tcp_sock(subflow);
426 }
427
428 return NULL;
429}
430
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700431static bool mptcp_skb_can_collapse_to(u64 write_seq,
432 const struct sk_buff *skb,
433 const struct mptcp_ext *mpext)
Mat Martineau6d0060f2020-01-21 16:56:23 -0800434{
Paolo Abeni57040752020-01-21 16:56:27 -0800435 if (!tcp_skb_can_collapse_to(skb))
436 return false;
437
438 /* can collapse only if MPTCP level sequence is in order */
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700439 return mpext && mpext->data_seq + mpext->data_len == write_seq;
Paolo Abeni57040752020-01-21 16:56:27 -0800440}
441
Paolo Abeni18b683b2020-03-27 14:48:43 -0700442static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
443 const struct page_frag *pfrag,
444 const struct mptcp_data_frag *df)
445{
446 return df && pfrag->page == df->page &&
447 df->data_seq + df->data_len == msk->write_seq;
448}
449
Paolo Abenid0272362020-03-27 14:48:45 -0700450static void dfrag_uncharge(struct sock *sk, int len)
Paolo Abeni18b683b2020-03-27 14:48:43 -0700451{
Paolo Abenid0272362020-03-27 14:48:45 -0700452 sk_mem_uncharge(sk, len);
Florian Westphal7948f6c2020-03-27 14:48:46 -0700453 sk_wmem_queued_add(sk, -len);
Paolo Abenid0272362020-03-27 14:48:45 -0700454}
455
456static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
457{
458 int len = dfrag->data_len + dfrag->overhead;
459
Paolo Abeni18b683b2020-03-27 14:48:43 -0700460 list_del(&dfrag->list);
Paolo Abenid0272362020-03-27 14:48:45 -0700461 dfrag_uncharge(sk, len);
Paolo Abeni18b683b2020-03-27 14:48:43 -0700462 put_page(dfrag->page);
463}
464
465static void mptcp_clean_una(struct sock *sk)
466{
467 struct mptcp_sock *msk = mptcp_sk(sk);
468 struct mptcp_data_frag *dtmp, *dfrag;
469 u64 snd_una = atomic64_read(&msk->snd_una);
Paolo Abenid0272362020-03-27 14:48:45 -0700470 bool cleaned = false;
Paolo Abeni18b683b2020-03-27 14:48:43 -0700471
472 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
473 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
474 break;
475
Paolo Abenid0272362020-03-27 14:48:45 -0700476 dfrag_clear(sk, dfrag);
477 cleaned = true;
478 }
479
Florian Westphal7948f6c2020-03-27 14:48:46 -0700480 dfrag = mptcp_rtx_head(sk);
481 if (dfrag && after64(snd_una, dfrag->data_seq)) {
482 u64 delta = dfrag->data_seq + dfrag->data_len - snd_una;
483
484 dfrag->data_seq += delta;
485 dfrag->data_len -= delta;
486
487 dfrag_uncharge(sk, delta);
488 cleaned = true;
489 }
490
Paolo Abenid0272362020-03-27 14:48:45 -0700491 if (cleaned) {
492 sk_mem_reclaim_partial(sk);
Florian Westphal7948f6c2020-03-27 14:48:46 -0700493
494 /* Only wake up writers if a subflow is ready */
495 if (test_bit(MPTCP_SEND_SPACE, &msk->flags))
496 sk_stream_write_space(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -0700497 }
498}
499
500/* ensure we get enough memory for the frag hdr, beyond some minimal amount of
501 * data
502 */
503static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
504{
505 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
506 pfrag, sk->sk_allocation)))
507 return true;
508
509 sk->sk_prot->enter_memory_pressure(sk);
510 sk_stream_moderate_sndbuf(sk);
511 return false;
512}
513
514static struct mptcp_data_frag *
515mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
516 int orig_offset)
517{
518 int offset = ALIGN(orig_offset, sizeof(long));
519 struct mptcp_data_frag *dfrag;
520
521 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
522 dfrag->data_len = 0;
523 dfrag->data_seq = msk->write_seq;
524 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
525 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
526 dfrag->page = pfrag->page;
527
528 return dfrag;
529}
530
Paolo Abeni57040752020-01-21 16:56:27 -0800531static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700532 struct msghdr *msg, struct mptcp_data_frag *dfrag,
533 long *timeo, int *pmss_now,
Paolo Abeni57040752020-01-21 16:56:27 -0800534 int *ps_goal)
535{
Paolo Abeni18b683b2020-03-27 14:48:43 -0700536 int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0;
537 bool dfrag_collapsed, can_collapse = false;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800538 struct mptcp_sock *msk = mptcp_sk(sk);
539 struct mptcp_ext *mpext = NULL;
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700540 bool retransmission = !!dfrag;
Paolo Abeni57040752020-01-21 16:56:27 -0800541 struct sk_buff *skb, *tail;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800542 struct page_frag *pfrag;
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700543 struct page *page;
544 u64 *write_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800545 size_t psize;
546
547 /* use the mptcp page cache so that we can easily move the data
548 * from one substream to another, but do per subflow memory accounting
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700549 * Note: pfrag is used only !retransmission, but the compiler if
550 * fooled into a warning if we don't init here
Mat Martineau6d0060f2020-01-21 16:56:23 -0800551 */
552 pfrag = sk_page_frag(sk);
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700553 if (!retransmission) {
554 write_seq = &msk->write_seq;
555 page = pfrag->page;
556 } else {
557 write_seq = &dfrag->data_seq;
558 page = dfrag->page;
559 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800560
561 /* compute copy limit */
562 mss_now = tcp_send_mss(ssk, &size_goal, msg->msg_flags);
Paolo Abeni57040752020-01-21 16:56:27 -0800563 *pmss_now = mss_now;
564 *ps_goal = size_goal;
565 avail_size = size_goal;
566 skb = tcp_write_queue_tail(ssk);
567 if (skb) {
568 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800569
Paolo Abeni57040752020-01-21 16:56:27 -0800570 /* Limit the write to the size available in the
571 * current skb, if any, so that we create at most a new skb.
572 * Explicitly tells TCP internals to avoid collapsing on later
573 * queue management operation, to avoid breaking the ext <->
574 * SSN association set here
575 */
576 can_collapse = (size_goal - skb->len > 0) &&
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700577 mptcp_skb_can_collapse_to(*write_seq, skb, mpext);
Paolo Abeni57040752020-01-21 16:56:27 -0800578 if (!can_collapse)
579 TCP_SKB_CB(skb)->eor = 1;
580 else
581 avail_size = size_goal - skb->len;
582 }
Paolo Abeni18b683b2020-03-27 14:48:43 -0700583
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700584 if (!retransmission) {
585 /* reuse tail pfrag, if possible, or carve a new one from the
586 * page allocator
587 */
588 dfrag = mptcp_rtx_tail(sk);
589 offset = pfrag->offset;
590 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
591 if (!dfrag_collapsed) {
592 dfrag = mptcp_carve_data_frag(msk, pfrag, offset);
593 offset = dfrag->offset;
594 frag_truesize = dfrag->overhead;
595 }
596 psize = min_t(size_t, pfrag->size - offset, avail_size);
597
598 /* Copy to page */
599 pr_debug("left=%zu", msg_data_left(msg));
600 psize = copy_page_from_iter(pfrag->page, offset,
601 min_t(size_t, msg_data_left(msg),
602 psize),
603 &msg->msg_iter);
604 pr_debug("left=%zu", msg_data_left(msg));
605 if (!psize)
606 return -EINVAL;
607
608 if (!sk_wmem_schedule(sk, psize + dfrag->overhead))
609 return -ENOMEM;
610 } else {
Paolo Abeni18b683b2020-03-27 14:48:43 -0700611 offset = dfrag->offset;
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700612 psize = min_t(size_t, dfrag->data_len, avail_size);
Paolo Abeni18b683b2020-03-27 14:48:43 -0700613 }
Paolo Abenid0272362020-03-27 14:48:45 -0700614
Paolo Abeni57040752020-01-21 16:56:27 -0800615 /* tell the TCP stack to delay the push so that we can safely
616 * access the skb after the sendpages call
Mat Martineau6d0060f2020-01-21 16:56:23 -0800617 */
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700618 ret = do_tcp_sendpages(ssk, page, offset, psize,
Florian Westphal72511aab2020-05-16 10:46:19 +0200619 msg->msg_flags | MSG_SENDPAGE_NOTLAST | MSG_DONTWAIT);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800620 if (ret <= 0)
621 return ret;
Paolo Abeni18b683b2020-03-27 14:48:43 -0700622
623 frag_truesize += ret;
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700624 if (!retransmission) {
625 if (unlikely(ret < psize))
626 iov_iter_revert(&msg->msg_iter, psize - ret);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800627
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700628 /* send successful, keep track of sent data for mptcp-level
629 * retransmission
630 */
631 dfrag->data_len += ret;
632 if (!dfrag_collapsed) {
633 get_page(dfrag->page);
634 list_add_tail(&dfrag->list, &msk->rtx_queue);
635 sk_wmem_queued_add(sk, frag_truesize);
636 } else {
637 sk_wmem_queued_add(sk, ret);
638 }
639
640 /* charge data on mptcp rtx queue to the master socket
641 * Note: we charge such data both to sk and ssk
642 */
643 sk->sk_forward_alloc -= frag_truesize;
Paolo Abeni18b683b2020-03-27 14:48:43 -0700644 }
645
Paolo Abeni57040752020-01-21 16:56:27 -0800646 /* if the tail skb extension is still the cached one, collapsing
647 * really happened. Note: we can't check for 'same skb' as the sk_buff
648 * hdr on tail can be transmitted, freed and re-allocated by the
649 * do_tcp_sendpages() call
650 */
651 tail = tcp_write_queue_tail(ssk);
652 if (mpext && tail && mpext == skb_ext_find(tail, SKB_EXT_MPTCP)) {
653 WARN_ON_ONCE(!can_collapse);
654 mpext->data_len += ret;
655 goto out;
656 }
657
Mat Martineau6d0060f2020-01-21 16:56:23 -0800658 skb = tcp_write_queue_tail(ssk);
659 mpext = __skb_ext_set(skb, SKB_EXT_MPTCP, msk->cached_ext);
660 msk->cached_ext = NULL;
661
662 memset(mpext, 0, sizeof(*mpext));
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700663 mpext->data_seq = *write_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800664 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
665 mpext->data_len = ret;
666 mpext->use_map = 1;
667 mpext->dsn64 = 1;
668
669 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
670 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
671 mpext->dsn64);
672
Paolo Abeni57040752020-01-21 16:56:27 -0800673out:
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700674 if (!retransmission)
675 pfrag->offset += frag_truesize;
676 *write_seq += ret;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800677 mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
678
Mat Martineau6d0060f2020-01-21 16:56:23 -0800679 return ret;
680}
681
Florian Westphala0e17062020-05-16 10:46:17 +0200682static void mptcp_nospace(struct mptcp_sock *msk, struct socket *sock)
683{
684 clear_bit(MPTCP_SEND_SPACE, &msk->flags);
685 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
686
687 /* enables sk->write_space() callbacks */
688 set_bit(SOCK_NOSPACE, &sock->flags);
689}
690
Peter Krystadf2962342020-03-27 14:48:39 -0700691static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
692{
693 struct mptcp_subflow_context *subflow;
694 struct sock *backup = NULL;
695
696 sock_owned_by_me((const struct sock *)msk);
697
Florian Westphal149f7c72020-05-16 10:46:20 +0200698 if (!mptcp_ext_cache_refill(msk))
699 return NULL;
700
Peter Krystadf2962342020-03-27 14:48:39 -0700701 mptcp_for_each_subflow(msk, subflow) {
702 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
703
704 if (!sk_stream_memory_free(ssk)) {
705 struct socket *sock = ssk->sk_socket;
706
Florian Westphala0e17062020-05-16 10:46:17 +0200707 if (sock)
708 mptcp_nospace(msk, sock);
Peter Krystadf2962342020-03-27 14:48:39 -0700709
710 return NULL;
711 }
712
713 if (subflow->backup) {
714 if (!backup)
715 backup = ssk;
716
717 continue;
718 }
719
720 return ssk;
721 }
722
723 return backup;
724}
725
Florian Westphal1891c4a2020-01-21 16:56:25 -0800726static void ssk_check_wmem(struct mptcp_sock *msk, struct sock *ssk)
727{
728 struct socket *sock;
729
730 if (likely(sk_stream_is_writeable(ssk)))
731 return;
732
733 sock = READ_ONCE(ssk->sk_socket);
Florian Westphala0e17062020-05-16 10:46:17 +0200734 if (sock)
735 mptcp_nospace(msk, sock);
Florian Westphal1891c4a2020-01-21 16:56:25 -0800736}
737
Mat Martineauf870fa02020-01-21 16:56:15 -0800738static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
739{
Paolo Abeni57040752020-01-21 16:56:27 -0800740 int mss_now = 0, size_goal = 0, ret = 0;
Mat Martineauf870fa02020-01-21 16:56:15 -0800741 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphal17091702020-05-16 10:46:21 +0200742 struct page_frag *pfrag;
Peter Krystadcec37a62020-01-21 16:56:18 -0800743 struct socket *ssock;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800744 size_t copied = 0;
Peter Krystadcec37a62020-01-21 16:56:18 -0800745 struct sock *ssk;
Florian Westphal72511aab2020-05-16 10:46:19 +0200746 bool tx_ok;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800747 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -0800748
749 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
750 return -EOPNOTSUPP;
751
Peter Krystadcec37a62020-01-21 16:56:18 -0800752 lock_sock(sk);
Mat Martineau1954b862020-02-28 15:47:39 -0800753
754 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
755
756 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
757 ret = sk_stream_wait_connect(sk, &timeo);
758 if (ret)
759 goto out;
760 }
761
Florian Westphale1546592020-04-11 21:05:01 +0200762fallback:
Peter Krystadcec37a62020-01-21 16:56:18 -0800763 ssock = __mptcp_tcp_fallback(msk);
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800764 if (unlikely(ssock)) {
Florian Westphale1546592020-04-11 21:05:01 +0200765 release_sock(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800766 pr_debug("fallback passthrough");
767 ret = sock_sendmsg(ssock, msg);
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800768 return ret >= 0 ? ret + copied : (copied ? copied : ret);
Peter Krystadcec37a62020-01-21 16:56:18 -0800769 }
770
Florian Westphal17091702020-05-16 10:46:21 +0200771 pfrag = sk_page_frag(sk);
Florian Westphal72511aab2020-05-16 10:46:19 +0200772restart:
Paolo Abeni18b683b2020-03-27 14:48:43 -0700773 mptcp_clean_una(sk);
774
Florian Westphalfb529e62020-05-16 10:46:18 +0200775wait_for_sndbuf:
Peter Krystadec3edaa2020-03-27 14:48:40 -0700776 __mptcp_flush_join_list(msk);
Peter Krystadf2962342020-03-27 14:48:39 -0700777 ssk = mptcp_subflow_get_send(msk);
Florian Westphal17091702020-05-16 10:46:21 +0200778 while (!sk_stream_memory_free(sk) ||
779 !ssk ||
780 !mptcp_page_frag_refill(ssk, pfrag)) {
Florian Westphalfb529e62020-05-16 10:46:18 +0200781 if (ssk) {
782 /* make sure retransmit timer is
783 * running before we wait for memory.
784 *
785 * The retransmit timer might be needed
786 * to make the peer send an up-to-date
787 * MPTCP Ack.
788 */
789 mptcp_set_timeout(sk, ssk);
790 if (!mptcp_timer_pending(sk))
791 mptcp_reset_timer(sk);
792 }
793
Peter Krystadf2962342020-03-27 14:48:39 -0700794 ret = sk_stream_wait_memory(sk, &timeo);
795 if (ret)
796 goto out;
797
Paolo Abeni18b683b2020-03-27 14:48:43 -0700798 mptcp_clean_una(sk);
799
Peter Krystadf2962342020-03-27 14:48:39 -0700800 ssk = mptcp_subflow_get_send(msk);
801 if (list_empty(&msk->conn_list)) {
802 ret = -ENOTCONN;
803 goto out;
804 }
Peter Krystadcec37a62020-01-21 16:56:18 -0800805 }
806
Mat Martineau6d0060f2020-01-21 16:56:23 -0800807 pr_debug("conn_list->subflow=%p", ssk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800808
Mat Martineau6d0060f2020-01-21 16:56:23 -0800809 lock_sock(ssk);
Florian Westphal72511aab2020-05-16 10:46:19 +0200810 tx_ok = msg_data_left(msg);
811 while (tx_ok) {
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -0700812 ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now,
Paolo Abeni57040752020-01-21 16:56:27 -0800813 &size_goal);
Florian Westphal72511aab2020-05-16 10:46:19 +0200814 if (ret < 0) {
815 if (ret == -EAGAIN && timeo > 0) {
816 mptcp_set_timeout(sk, ssk);
817 release_sock(ssk);
818 goto restart;
819 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800820 break;
Florian Westphal72511aab2020-05-16 10:46:19 +0200821 }
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800822 if (ret == 0 && unlikely(__mptcp_needs_tcp_fallback(msk))) {
Florian Westphale1546592020-04-11 21:05:01 +0200823 /* Can happen for passive sockets:
824 * 3WHS negotiated MPTCP, but first packet after is
825 * plain TCP (e.g. due to middlebox filtering unknown
826 * options).
827 *
828 * Fall back to TCP.
829 */
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800830 release_sock(ssk);
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800831 goto fallback;
832 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800833
834 copied += ret;
Florian Westphalfb529e62020-05-16 10:46:18 +0200835
Florian Westphal72511aab2020-05-16 10:46:19 +0200836 tx_ok = msg_data_left(msg);
837 if (!tx_ok)
838 break;
839
Florian Westphal149f7c72020-05-16 10:46:20 +0200840 if (!sk_stream_memory_free(ssk) ||
Florian Westphal17091702020-05-16 10:46:21 +0200841 !mptcp_page_frag_refill(ssk, pfrag) ||
Florian Westphal149f7c72020-05-16 10:46:20 +0200842 !mptcp_ext_cache_refill(msk)) {
Florian Westphal72511aab2020-05-16 10:46:19 +0200843 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
844 tcp_push(ssk, msg->msg_flags, mss_now,
845 tcp_sk(ssk)->nonagle, size_goal);
846 mptcp_set_timeout(sk, ssk);
847 release_sock(ssk);
848 goto restart;
849 }
850
Florian Westphalfb529e62020-05-16 10:46:18 +0200851 /* memory is charged to mptcp level socket as well, i.e.
852 * if msg is very large, mptcp socket may run out of buffer
853 * space. mptcp_clean_una() will release data that has
854 * been acked at mptcp level in the mean time, so there is
855 * a good chance we can continue sending data right away.
Florian Westphal72511aab2020-05-16 10:46:19 +0200856 *
857 * Normally, when the tcp subflow can accept more data, then
858 * so can the MPTCP socket. However, we need to cope with
859 * peers that might lag behind in their MPTCP-level
860 * acknowledgements, i.e. data might have been acked at
861 * tcp level only. So, we must also check the MPTCP socket
862 * limits before we send more data.
Florian Westphalfb529e62020-05-16 10:46:18 +0200863 */
864 if (unlikely(!sk_stream_memory_free(sk))) {
865 tcp_push(ssk, msg->msg_flags, mss_now,
866 tcp_sk(ssk)->nonagle, size_goal);
867 mptcp_clean_una(sk);
868 if (!sk_stream_memory_free(sk)) {
869 /* can't send more for now, need to wait for
870 * MPTCP-level ACKs from peer.
871 *
872 * Wakeup will happen via mptcp_clean_una().
873 */
874 mptcp_set_timeout(sk, ssk);
875 release_sock(ssk);
876 goto wait_for_sndbuf;
877 }
878 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800879 }
880
Paolo Abenib51f9b82020-03-27 14:48:44 -0700881 mptcp_set_timeout(sk, ssk);
Paolo Abeni57040752020-01-21 16:56:27 -0800882 if (copied) {
Mat Martineau6d0060f2020-01-21 16:56:23 -0800883 ret = copied;
Paolo Abeni57040752020-01-21 16:56:27 -0800884 tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle,
885 size_goal);
Paolo Abenib51f9b82020-03-27 14:48:44 -0700886
887 /* start the timer, if it's not pending */
888 if (!mptcp_timer_pending(sk))
889 mptcp_reset_timer(sk);
Paolo Abeni57040752020-01-21 16:56:27 -0800890 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800891
Florian Westphal1891c4a2020-01-21 16:56:25 -0800892 ssk_check_wmem(msk, ssk);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800893 release_sock(ssk);
Mat Martineau1954b862020-02-28 15:47:39 -0800894out:
Peter Krystadcec37a62020-01-21 16:56:18 -0800895 release_sock(sk);
896 return ret;
Mat Martineauf870fa02020-01-21 16:56:15 -0800897}
898
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800899static void mptcp_wait_data(struct sock *sk, long *timeo)
900{
901 DEFINE_WAIT_FUNC(wait, woken_wake_function);
902 struct mptcp_sock *msk = mptcp_sk(sk);
903
904 add_wait_queue(sk_sleep(sk), &wait);
905 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
906
907 sk_wait_event(sk, timeo,
908 test_and_clear_bit(MPTCP_DATA_READY, &msk->flags), &wait);
909
910 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
911 remove_wait_queue(sk_sleep(sk), &wait);
912}
913
Florian Westphal6771bfd2020-02-26 10:14:48 +0100914static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
915 struct msghdr *msg,
916 size_t len)
917{
918 struct sock *sk = (struct sock *)msk;
919 struct sk_buff *skb;
920 int copied = 0;
921
922 while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
923 u32 offset = MPTCP_SKB_CB(skb)->offset;
924 u32 data_len = skb->len - offset;
925 u32 count = min_t(size_t, len - copied, data_len);
926 int err;
927
928 err = skb_copy_datagram_msg(skb, offset, msg, count);
929 if (unlikely(err < 0)) {
930 if (!copied)
931 return err;
932 break;
933 }
934
935 copied += count;
936
937 if (count < data_len) {
938 MPTCP_SKB_CB(skb)->offset += count;
939 break;
940 }
941
942 __skb_unlink(skb, &sk->sk_receive_queue);
943 __kfree_skb(skb);
944
945 if (copied >= len)
946 break;
947 }
948
949 return copied;
950}
951
952static bool __mptcp_move_skbs(struct mptcp_sock *msk)
953{
954 unsigned int moved = 0;
955 bool done;
956
957 do {
958 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
959
960 if (!ssk)
961 break;
962
963 lock_sock(ssk);
964 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
965 release_sock(ssk);
966 } while (!done);
967
968 return moved > 0;
969}
970
Mat Martineauf870fa02020-01-21 16:56:15 -0800971static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
972 int nonblock, int flags, int *addr_len)
973{
974 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800975 struct socket *ssock;
Peter Krystadcec37a62020-01-21 16:56:18 -0800976 int copied = 0;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800977 int target;
978 long timeo;
Mat Martineauf870fa02020-01-21 16:56:15 -0800979
980 if (msg->msg_flags & ~(MSG_WAITALL | MSG_DONTWAIT))
981 return -EOPNOTSUPP;
982
Peter Krystadcec37a62020-01-21 16:56:18 -0800983 lock_sock(sk);
984 ssock = __mptcp_tcp_fallback(msk);
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800985 if (unlikely(ssock)) {
986fallback:
Florian Westphale1546592020-04-11 21:05:01 +0200987 release_sock(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800988 pr_debug("fallback-read subflow=%p",
989 mptcp_subflow_ctx(ssock->sk));
990 copied = sock_recvmsg(ssock, msg, flags);
Peter Krystadcec37a62020-01-21 16:56:18 -0800991 return copied;
992 }
993
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800994 timeo = sock_rcvtimeo(sk, nonblock);
995
996 len = min_t(size_t, len, INT_MAX);
997 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700998 __mptcp_flush_join_list(msk);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -0800999
Florian Westphal6771bfd2020-02-26 10:14:48 +01001000 while (len > (size_t)copied) {
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001001 int bytes_read;
1002
Florian Westphal6771bfd2020-02-26 10:14:48 +01001003 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied);
1004 if (unlikely(bytes_read < 0)) {
1005 if (!copied)
1006 copied = bytes_read;
1007 goto out_err;
1008 }
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001009
Florian Westphal6771bfd2020-02-26 10:14:48 +01001010 copied += bytes_read;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001011
Florian Westphal6771bfd2020-02-26 10:14:48 +01001012 if (skb_queue_empty(&sk->sk_receive_queue) &&
1013 __mptcp_move_skbs(msk))
1014 continue;
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001015
1016 /* only the master socket status is relevant here. The exit
1017 * conditions mirror closely tcp_recvmsg()
1018 */
1019 if (copied >= target)
1020 break;
1021
1022 if (copied) {
1023 if (sk->sk_err ||
1024 sk->sk_state == TCP_CLOSE ||
1025 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1026 !timeo ||
1027 signal_pending(current))
1028 break;
1029 } else {
1030 if (sk->sk_err) {
1031 copied = sock_error(sk);
1032 break;
1033 }
1034
Paolo Abeni59698562020-06-10 10:47:41 +02001035 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
1036 mptcp_check_for_eof(msk);
1037
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001038 if (sk->sk_shutdown & RCV_SHUTDOWN)
1039 break;
1040
1041 if (sk->sk_state == TCP_CLOSE) {
1042 copied = -ENOTCONN;
1043 break;
1044 }
1045
1046 if (!timeo) {
1047 copied = -EAGAIN;
1048 break;
1049 }
1050
1051 if (signal_pending(current)) {
1052 copied = sock_intr_errno(timeo);
1053 break;
1054 }
1055 }
1056
1057 pr_debug("block timeout %ld", timeo);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001058 mptcp_wait_data(sk, &timeo);
Paolo Abeni0a82e232020-05-25 16:38:47 +02001059 ssock = __mptcp_tcp_fallback(msk);
1060 if (unlikely(ssock))
Paolo Abeni8ab183d2020-01-21 16:56:33 -08001061 goto fallback;
Peter Krystadcec37a62020-01-21 16:56:18 -08001062 }
1063
Florian Westphal6771bfd2020-02-26 10:14:48 +01001064 if (skb_queue_empty(&sk->sk_receive_queue)) {
1065 /* entire backlog drained, clear DATA_READY. */
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001066 clear_bit(MPTCP_DATA_READY, &msk->flags);
1067
Florian Westphal6771bfd2020-02-26 10:14:48 +01001068 /* .. race-breaker: ssk might have gotten new data
1069 * after last __mptcp_move_skbs() returned false.
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001070 */
Florian Westphal6771bfd2020-02-26 10:14:48 +01001071 if (unlikely(__mptcp_move_skbs(msk)))
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001072 set_bit(MPTCP_DATA_READY, &msk->flags);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001073 } else if (unlikely(!test_bit(MPTCP_DATA_READY, &msk->flags))) {
1074 /* data to read but mptcp_wait_data() cleared DATA_READY */
1075 set_bit(MPTCP_DATA_READY, &msk->flags);
Paolo Abeni7a6a6cb2020-01-21 16:56:26 -08001076 }
Florian Westphal6771bfd2020-02-26 10:14:48 +01001077out_err:
Peter Krystadcec37a62020-01-21 16:56:18 -08001078 release_sock(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08001079 return copied;
1080}
1081
Paolo Abenib51f9b82020-03-27 14:48:44 -07001082static void mptcp_retransmit_handler(struct sock *sk)
1083{
1084 struct mptcp_sock *msk = mptcp_sk(sk);
1085
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001086 if (atomic64_read(&msk->snd_una) == msk->write_seq) {
Paolo Abenib51f9b82020-03-27 14:48:44 -07001087 mptcp_stop_timer(sk);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001088 } else {
1089 set_bit(MPTCP_WORK_RTX, &msk->flags);
1090 if (schedule_work(&msk->work))
1091 sock_hold(sk);
1092 }
Paolo Abenib51f9b82020-03-27 14:48:44 -07001093}
1094
1095static void mptcp_retransmit_timer(struct timer_list *t)
1096{
1097 struct inet_connection_sock *icsk = from_timer(icsk, t,
1098 icsk_retransmit_timer);
1099 struct sock *sk = &icsk->icsk_inet.sk;
1100
1101 bh_lock_sock(sk);
1102 if (!sock_owned_by_user(sk)) {
1103 mptcp_retransmit_handler(sk);
1104 } else {
1105 /* delegate our work to tcp_release_cb() */
1106 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED,
1107 &sk->sk_tsq_flags))
1108 sock_hold(sk);
1109 }
1110 bh_unlock_sock(sk);
1111 sock_put(sk);
1112}
1113
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001114/* Find an idle subflow. Return NULL if there is unacked data at tcp
1115 * level.
1116 *
1117 * A backup subflow is returned only if that is the only kind available.
1118 */
1119static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
1120{
1121 struct mptcp_subflow_context *subflow;
1122 struct sock *backup = NULL;
1123
1124 sock_owned_by_me((const struct sock *)msk);
1125
1126 mptcp_for_each_subflow(msk, subflow) {
1127 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1128
1129 /* still data outstanding at TCP level? Don't retransmit. */
1130 if (!tcp_write_queue_empty(ssk))
1131 return NULL;
1132
1133 if (subflow->backup) {
1134 if (!backup)
1135 backup = ssk;
1136 continue;
1137 }
1138
1139 return ssk;
1140 }
1141
1142 return backup;
1143}
1144
Peter Krystadcec37a62020-01-21 16:56:18 -08001145/* subflow sockets can be either outgoing (connect) or incoming
1146 * (accept).
1147 *
1148 * Outgoing subflows use in-kernel sockets.
1149 * Incoming subflows do not have their own 'struct socket' allocated,
1150 * so we need to use tcp_close() after detaching them from the mptcp
1151 * parent socket.
1152 */
1153static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
1154 struct mptcp_subflow_context *subflow,
1155 long timeout)
1156{
1157 struct socket *sock = READ_ONCE(ssk->sk_socket);
1158
1159 list_del(&subflow->node);
1160
1161 if (sock && sock != sk->sk_socket) {
1162 /* outgoing subflow */
1163 sock_release(sock);
1164 } else {
1165 /* incoming subflow */
1166 tcp_close(ssk, timeout);
1167 }
Mat Martineauf870fa02020-01-21 16:56:15 -08001168}
1169
Paolo Abenidc24f8b2020-02-26 12:19:03 +01001170static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
1171{
1172 return 0;
1173}
1174
Paolo Abeni80992012020-02-26 10:14:47 +01001175static void mptcp_worker(struct work_struct *work)
1176{
1177 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001178 struct sock *ssk, *sk = &msk->sk.icsk_inet.sk;
Florian Westphal149f7c72020-05-16 10:46:20 +02001179 int orig_len, orig_offset, mss_now = 0, size_goal = 0;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001180 struct mptcp_data_frag *dfrag;
1181 u64 orig_write_seq;
1182 size_t copied = 0;
1183 struct msghdr msg;
1184 long timeo = 0;
Paolo Abeni80992012020-02-26 10:14:47 +01001185
1186 lock_sock(sk);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001187 mptcp_clean_una(sk);
Peter Krystadec3edaa2020-03-27 14:48:40 -07001188 __mptcp_flush_join_list(msk);
Florian Westphal6771bfd2020-02-26 10:14:48 +01001189 __mptcp_move_skbs(msk);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001190
Florian Westphal59832e22020-04-02 13:44:52 +02001191 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
1192 mptcp_check_for_eof(msk);
1193
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001194 if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
1195 goto unlock;
1196
1197 dfrag = mptcp_rtx_head(sk);
1198 if (!dfrag)
1199 goto unlock;
1200
Florian Westphal149f7c72020-05-16 10:46:20 +02001201 if (!mptcp_ext_cache_refill(msk))
1202 goto reset_unlock;
1203
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001204 ssk = mptcp_subflow_get_retrans(msk);
1205 if (!ssk)
1206 goto reset_unlock;
1207
1208 lock_sock(ssk);
1209
1210 msg.msg_flags = MSG_DONTWAIT;
1211 orig_len = dfrag->data_len;
1212 orig_offset = dfrag->offset;
1213 orig_write_seq = dfrag->data_seq;
1214 while (dfrag->data_len > 0) {
Florian Westphal149f7c72020-05-16 10:46:20 +02001215 int ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo,
1216 &mss_now, &size_goal);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001217 if (ret < 0)
1218 break;
1219
Florian Westphalfc518952020-03-27 14:48:50 -07001220 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001221 copied += ret;
1222 dfrag->data_len -= ret;
1223 dfrag->offset += ret;
Florian Westphal149f7c72020-05-16 10:46:20 +02001224
1225 if (!mptcp_ext_cache_refill(msk))
1226 break;
Paolo Abeni3b1d6212020-03-27 14:48:48 -07001227 }
1228 if (copied)
1229 tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle,
1230 size_goal);
1231
1232 dfrag->data_seq = orig_write_seq;
1233 dfrag->offset = orig_offset;
1234 dfrag->data_len = orig_len;
1235
1236 mptcp_set_timeout(sk, ssk);
1237 release_sock(ssk);
1238
1239reset_unlock:
1240 if (!mptcp_timer_pending(sk))
1241 mptcp_reset_timer(sk);
1242
1243unlock:
Paolo Abeni80992012020-02-26 10:14:47 +01001244 release_sock(sk);
1245 sock_put(sk);
1246}
1247
Matthieu Baerts784325e2020-01-21 16:56:28 -08001248static int __mptcp_init_sock(struct sock *sk)
Mat Martineauf870fa02020-01-21 16:56:15 -08001249{
Peter Krystadcec37a62020-01-21 16:56:18 -08001250 struct mptcp_sock *msk = mptcp_sk(sk);
1251
Peter Krystadec3edaa2020-03-27 14:48:40 -07001252 spin_lock_init(&msk->join_list_lock);
1253
Peter Krystadcec37a62020-01-21 16:56:18 -08001254 INIT_LIST_HEAD(&msk->conn_list);
Peter Krystadec3edaa2020-03-27 14:48:40 -07001255 INIT_LIST_HEAD(&msk->join_list);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001256 INIT_LIST_HEAD(&msk->rtx_queue);
Florian Westphal1891c4a2020-01-21 16:56:25 -08001257 __set_bit(MPTCP_SEND_SPACE, &msk->flags);
Paolo Abeni80992012020-02-26 10:14:47 +01001258 INIT_WORK(&msk->work, mptcp_worker);
Peter Krystadcec37a62020-01-21 16:56:18 -08001259
Paolo Abeni8ab183d2020-01-21 16:56:33 -08001260 msk->first = NULL;
Paolo Abenidc24f8b2020-02-26 12:19:03 +01001261 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08001262
Peter Krystad1b1c7a02020-03-27 14:48:38 -07001263 mptcp_pm_data_init(msk);
1264
Paolo Abenib51f9b82020-03-27 14:48:44 -07001265 /* re-use the csk retrans timer for MPTCP-level retrans */
1266 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
1267
Mat Martineauf870fa02020-01-21 16:56:15 -08001268 return 0;
1269}
1270
Matthieu Baerts784325e2020-01-21 16:56:28 -08001271static int mptcp_init_sock(struct sock *sk)
1272{
Florian Westphalfc518952020-03-27 14:48:50 -07001273 struct net *net = sock_net(sk);
1274 int ret;
Paolo Abeni18b683b2020-03-27 14:48:43 -07001275
Florian Westphalfc518952020-03-27 14:48:50 -07001276 if (!mptcp_is_enabled(net))
1277 return -ENOPROTOOPT;
1278
1279 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
1280 return -ENOMEM;
1281
1282 ret = __mptcp_init_sock(sk);
Paolo Abeni18b683b2020-03-27 14:48:43 -07001283 if (ret)
1284 return ret;
1285
Paolo Abenid0272362020-03-27 14:48:45 -07001286 sk_sockets_allocated_inc(sk);
Paolo Abeni3f8e0aa2020-03-27 14:48:47 -07001287 sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[2];
Paolo Abenid0272362020-03-27 14:48:45 -07001288
Paolo Abeni18b683b2020-03-27 14:48:43 -07001289 return 0;
1290}
1291
1292static void __mptcp_clear_xmit(struct sock *sk)
1293{
1294 struct mptcp_sock *msk = mptcp_sk(sk);
1295 struct mptcp_data_frag *dtmp, *dfrag;
1296
Paolo Abenib51f9b82020-03-27 14:48:44 -07001297 sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
1298
Paolo Abeni18b683b2020-03-27 14:48:43 -07001299 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
Paolo Abenid0272362020-03-27 14:48:45 -07001300 dfrag_clear(sk, dfrag);
Matthieu Baerts784325e2020-01-21 16:56:28 -08001301}
1302
Paolo Abeni80992012020-02-26 10:14:47 +01001303static void mptcp_cancel_work(struct sock *sk)
1304{
1305 struct mptcp_sock *msk = mptcp_sk(sk);
1306
1307 if (cancel_work_sync(&msk->work))
1308 sock_put(sk);
1309}
1310
Mat Martineau76c42a22020-02-28 15:47:40 -08001311static void mptcp_subflow_shutdown(struct sock *ssk, int how,
1312 bool data_fin_tx_enable, u64 data_fin_tx_seq)
Peter Krystad21498492020-01-21 16:56:21 -08001313{
1314 lock_sock(ssk);
1315
1316 switch (ssk->sk_state) {
1317 case TCP_LISTEN:
1318 if (!(how & RCV_SHUTDOWN))
1319 break;
1320 /* fall through */
1321 case TCP_SYN_SENT:
1322 tcp_disconnect(ssk, O_NONBLOCK);
1323 break;
1324 default:
Mat Martineau76c42a22020-02-28 15:47:40 -08001325 if (data_fin_tx_enable) {
1326 struct mptcp_subflow_context *subflow;
1327
1328 subflow = mptcp_subflow_ctx(ssk);
1329 subflow->data_fin_tx_seq = data_fin_tx_seq;
1330 subflow->data_fin_tx_enable = 1;
1331 }
1332
Peter Krystad21498492020-01-21 16:56:21 -08001333 ssk->sk_shutdown |= how;
1334 tcp_shutdown(ssk, how);
1335 break;
1336 }
1337
1338 /* Wake up anyone sleeping in poll. */
1339 ssk->sk_state_change(ssk);
1340 release_sock(ssk);
1341}
1342
Paolo Abeni8ab183d2020-01-21 16:56:33 -08001343/* Called with msk lock held, releases such lock before returning */
Florian Westphal2c22c062020-02-04 18:12:30 +01001344static void mptcp_close(struct sock *sk, long timeout)
Mat Martineauf870fa02020-01-21 16:56:15 -08001345{
Peter Krystadcec37a62020-01-21 16:56:18 -08001346 struct mptcp_subflow_context *subflow, *tmp;
Mat Martineauf870fa02020-01-21 16:56:15 -08001347 struct mptcp_sock *msk = mptcp_sk(sk);
Florian Westphalb2c5b612020-01-29 15:54:45 +01001348 LIST_HEAD(conn_list);
Mat Martineau76c42a22020-02-28 15:47:40 -08001349 u64 data_fin_tx_seq;
Mat Martineauf870fa02020-01-21 16:56:15 -08001350
Florian Westphal2c22c062020-02-04 18:12:30 +01001351 lock_sock(sk);
1352
Mat Martineauf870fa02020-01-21 16:56:15 -08001353 inet_sk_state_store(sk, TCP_CLOSE);
1354
Paolo Abeni10f6d462020-05-29 17:43:30 +02001355 /* be sure to always acquire the join list lock, to sync vs
1356 * mptcp_finish_join().
1357 */
1358 spin_lock_bh(&msk->join_list_lock);
1359 list_splice_tail_init(&msk->join_list, &msk->conn_list);
1360 spin_unlock_bh(&msk->join_list_lock);
Florian Westphalb2c5b612020-01-29 15:54:45 +01001361 list_splice_init(&msk->conn_list, &conn_list);
1362
Mat Martineau76c42a22020-02-28 15:47:40 -08001363 data_fin_tx_seq = msk->write_seq;
1364
Paolo Abeni18b683b2020-03-27 14:48:43 -07001365 __mptcp_clear_xmit(sk);
1366
Florian Westphalb2c5b612020-01-29 15:54:45 +01001367 release_sock(sk);
1368
1369 list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
Peter Krystadcec37a62020-01-21 16:56:18 -08001370 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1371
Mat Martineau76c42a22020-02-28 15:47:40 -08001372 subflow->data_fin_tx_seq = data_fin_tx_seq;
1373 subflow->data_fin_tx_enable = 1;
Peter Krystadcec37a62020-01-21 16:56:18 -08001374 __mptcp_close_ssk(sk, ssk, subflow, timeout);
Mat Martineauf870fa02020-01-21 16:56:15 -08001375 }
1376
Paolo Abeni80992012020-02-26 10:14:47 +01001377 mptcp_cancel_work(sk);
Peter Krystad926bdea2020-03-27 14:48:41 -07001378 mptcp_pm_close(msk);
Paolo Abeni80992012020-02-26 10:14:47 +01001379
Florian Westphal6771bfd2020-02-26 10:14:48 +01001380 __skb_queue_purge(&sk->sk_receive_queue);
1381
Peter Krystadcec37a62020-01-21 16:56:18 -08001382 sk_common_release(sk);
Mat Martineauf870fa02020-01-21 16:56:15 -08001383}
1384
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001385static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
1386{
1387#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1388 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
1389 struct ipv6_pinfo *msk6 = inet6_sk(msk);
1390
1391 msk->sk_v6_daddr = ssk->sk_v6_daddr;
1392 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
1393
1394 if (msk6 && ssk6) {
1395 msk6->saddr = ssk6->saddr;
1396 msk6->flow_label = ssk6->flow_label;
1397 }
1398#endif
1399
1400 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
1401 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
1402 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
1403 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
1404 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
1405 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
1406}
1407
Paolo Abeni18b683b2020-03-27 14:48:43 -07001408static int mptcp_disconnect(struct sock *sk, int flags)
1409{
Florian Westphal42c556f2020-04-29 20:43:20 +02001410 /* Should never be called.
1411 * inet_stream_connect() calls ->disconnect, but that
1412 * refers to the subflow socket, not the mptcp one.
1413 */
1414 WARN_ON_ONCE(1);
1415 return 0;
Paolo Abeni18b683b2020-03-27 14:48:43 -07001416}
1417
Florian Westphalb0519de2020-02-06 00:39:37 +01001418#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1419static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
1420{
1421 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
1422
1423 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
1424}
1425#endif
1426
Paolo Abenifca5c822020-04-20 16:25:06 +02001427struct sock *mptcp_sk_clone(const struct sock *sk,
Paolo Abenicfde1412020-04-30 15:01:52 +02001428 const struct mptcp_options_received *mp_opt,
Paolo Abenifca5c822020-04-20 16:25:06 +02001429 struct request_sock *req)
Florian Westphalb0519de2020-02-06 00:39:37 +01001430{
Paolo Abeni58b09912020-03-13 16:52:41 +01001431 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
Florian Westphalb0519de2020-02-06 00:39:37 +01001432 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
Paolo Abeni58b09912020-03-13 16:52:41 +01001433 struct mptcp_sock *msk;
1434 u64 ack_seq;
Florian Westphalb0519de2020-02-06 00:39:37 +01001435
1436 if (!nsk)
1437 return NULL;
1438
1439#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1440 if (nsk->sk_family == AF_INET6)
1441 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
1442#endif
1443
Paolo Abeni58b09912020-03-13 16:52:41 +01001444 __mptcp_init_sock(nsk);
1445
1446 msk = mptcp_sk(nsk);
1447 msk->local_key = subflow_req->local_key;
1448 msk->token = subflow_req->token;
1449 msk->subflow = NULL;
1450
1451 if (unlikely(mptcp_token_new_accept(subflow_req->token, nsk))) {
Florian Westphal9f5ca6a2020-04-17 09:28:23 +02001452 nsk->sk_state = TCP_CLOSE;
Paolo Abeni58b09912020-03-13 16:52:41 +01001453 bh_unlock_sock(nsk);
1454
1455 /* we can't call into mptcp_close() here - possible BH context
Florian Westphal9f5ca6a2020-04-17 09:28:23 +02001456 * free the sock directly.
1457 * sk_clone_lock() sets nsk refcnt to two, hence call sk_free()
1458 * too.
Paolo Abeni58b09912020-03-13 16:52:41 +01001459 */
Florian Westphal9f5ca6a2020-04-17 09:28:23 +02001460 sk_common_release(nsk);
Paolo Abeni58b09912020-03-13 16:52:41 +01001461 sk_free(nsk);
1462 return NULL;
1463 }
1464
1465 msk->write_seq = subflow_req->idsn + 1;
Paolo Abenicc9d2562020-03-27 14:48:42 -07001466 atomic64_set(&msk->snd_una, msk->write_seq);
Paolo Abenicfde1412020-04-30 15:01:52 +02001467 if (mp_opt->mp_capable) {
Paolo Abeni58b09912020-03-13 16:52:41 +01001468 msk->can_ack = true;
Paolo Abenicfde1412020-04-30 15:01:52 +02001469 msk->remote_key = mp_opt->sndr_key;
Paolo Abeni58b09912020-03-13 16:52:41 +01001470 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
1471 ack_seq++;
1472 msk->ack_seq = ack_seq;
1473 }
Paolo Abeni7f20d5f2020-03-17 15:53:34 +01001474
Florian Westphal5e200872020-04-20 16:25:04 +02001475 sock_reset_flag(nsk, SOCK_RCU_FREE);
Paolo Abeni7f20d5f2020-03-17 15:53:34 +01001476 /* will be fully established after successful MPC subflow creation */
1477 inet_sk_state_store(nsk, TCP_SYN_RECV);
Paolo Abeni58b09912020-03-13 16:52:41 +01001478 bh_unlock_sock(nsk);
1479
1480 /* keep a single reference */
1481 __sock_put(nsk);
Florian Westphalb0519de2020-02-06 00:39:37 +01001482 return nsk;
1483}
1484
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001485static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
1486 bool kern)
1487{
1488 struct mptcp_sock *msk = mptcp_sk(sk);
1489 struct socket *listener;
1490 struct sock *newsk;
1491
1492 listener = __mptcp_nmpc_socket(msk);
1493 if (WARN_ON_ONCE(!listener)) {
1494 *err = -EINVAL;
1495 return NULL;
1496 }
1497
1498 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
1499 newsk = inet_csk_accept(listener->sk, flags, err, kern);
1500 if (!newsk)
1501 return NULL;
1502
1503 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
1504
1505 if (sk_is_mptcp(newsk)) {
1506 struct mptcp_subflow_context *subflow;
1507 struct sock *new_mptcp_sock;
1508 struct sock *ssk = newsk;
1509
1510 subflow = mptcp_subflow_ctx(newsk);
Paolo Abeni58b09912020-03-13 16:52:41 +01001511 new_mptcp_sock = subflow->conn;
1512
1513 /* is_mptcp should be false if subflow->conn is missing, see
1514 * subflow_syn_recv_sock()
1515 */
1516 if (WARN_ON_ONCE(!new_mptcp_sock)) {
1517 tcp_sk(newsk)->is_mptcp = 0;
1518 return newsk;
1519 }
1520
1521 /* acquire the 2nd reference for the owning socket */
1522 sock_hold(new_mptcp_sock);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001523
1524 local_bh_disable();
Paolo Abeni58b09912020-03-13 16:52:41 +01001525 bh_lock_sock(new_mptcp_sock);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001526 msk = mptcp_sk(new_mptcp_sock);
Paolo Abeni8ab183d2020-01-21 16:56:33 -08001527 msk->first = newsk;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001528
1529 newsk = new_mptcp_sock;
1530 mptcp_copy_inaddrs(newsk, ssk);
1531 list_add(&subflow->node, &msk->conn_list);
Florian Westphaldf1036d2020-04-17 09:28:22 +02001532 inet_sk_state_store(newsk, TCP_ESTABLISHED);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001533
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001534 bh_unlock_sock(new_mptcp_sock);
Florian Westphalfc518952020-03-27 14:48:50 -07001535
1536 __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001537 local_bh_enable();
Florian Westphalfc518952020-03-27 14:48:50 -07001538 } else {
1539 MPTCP_INC_STATS(sock_net(sk),
1540 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001541 }
1542
1543 return newsk;
1544}
1545
Peter Krystad79c09492020-01-21 16:56:20 -08001546static void mptcp_destroy(struct sock *sk)
1547{
Florian Westphalc9fd9c52020-01-29 15:54:43 +01001548 struct mptcp_sock *msk = mptcp_sk(sk);
1549
Paolo Abenic5c79762020-05-29 17:43:31 +02001550 mptcp_token_destroy(msk->token);
Florian Westphalc9fd9c52020-01-29 15:54:43 +01001551 if (msk->cached_ext)
1552 __skb_ext_put(msk->cached_ext);
Paolo Abenid0272362020-03-27 14:48:45 -07001553
1554 sk_sockets_allocated_dec(sk);
Peter Krystad79c09492020-01-21 16:56:20 -08001555}
1556
Peter Krystad717e79c2020-01-21 16:56:22 -08001557static int mptcp_setsockopt(struct sock *sk, int level, int optname,
Florian Westphal50e741b2020-01-29 15:54:44 +01001558 char __user *optval, unsigned int optlen)
Peter Krystad717e79c2020-01-21 16:56:22 -08001559{
1560 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystad717e79c2020-01-21 16:56:22 -08001561 struct socket *ssock;
Peter Krystad717e79c2020-01-21 16:56:22 -08001562
1563 pr_debug("msk=%p", msk);
1564
1565 /* @@ the meaning of setsockopt() when the socket is connected and
Mat Martineaub6e4a1a2020-02-14 14:14:29 -08001566 * there are multiple subflows is not yet defined. It is up to the
1567 * MPTCP-level socket to configure the subflows until the subflow
1568 * is in TCP fallback, when TCP socket options are passed through
1569 * to the one remaining subflow.
Peter Krystad717e79c2020-01-21 16:56:22 -08001570 */
1571 lock_sock(sk);
Mat Martineaub6e4a1a2020-02-14 14:14:29 -08001572 ssock = __mptcp_tcp_fallback(msk);
Florian Westphale1546592020-04-11 21:05:01 +02001573 release_sock(sk);
Mat Martineaub6e4a1a2020-02-14 14:14:29 -08001574 if (ssock)
1575 return tcp_setsockopt(ssock->sk, level, optname, optval,
1576 optlen);
Florian Westphal50e741b2020-01-29 15:54:44 +01001577
Mat Martineaub6e4a1a2020-02-14 14:14:29 -08001578 return -EOPNOTSUPP;
Peter Krystad717e79c2020-01-21 16:56:22 -08001579}
1580
1581static int mptcp_getsockopt(struct sock *sk, int level, int optname,
Florian Westphal50e741b2020-01-29 15:54:44 +01001582 char __user *optval, int __user *option)
Peter Krystad717e79c2020-01-21 16:56:22 -08001583{
1584 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystad717e79c2020-01-21 16:56:22 -08001585 struct socket *ssock;
Peter Krystad717e79c2020-01-21 16:56:22 -08001586
1587 pr_debug("msk=%p", msk);
1588
Mat Martineaub6e4a1a2020-02-14 14:14:29 -08001589 /* @@ the meaning of setsockopt() when the socket is connected and
1590 * there are multiple subflows is not yet defined. It is up to the
1591 * MPTCP-level socket to configure the subflows until the subflow
1592 * is in TCP fallback, when socket options are passed through
1593 * to the one remaining subflow.
Peter Krystad717e79c2020-01-21 16:56:22 -08001594 */
1595 lock_sock(sk);
Mat Martineaub6e4a1a2020-02-14 14:14:29 -08001596 ssock = __mptcp_tcp_fallback(msk);
Florian Westphale1546592020-04-11 21:05:01 +02001597 release_sock(sk);
Mat Martineaub6e4a1a2020-02-14 14:14:29 -08001598 if (ssock)
1599 return tcp_getsockopt(ssock->sk, level, optname, optval,
1600 option);
Florian Westphal50e741b2020-01-29 15:54:44 +01001601
Mat Martineaub6e4a1a2020-02-14 14:14:29 -08001602 return -EOPNOTSUPP;
Peter Krystad717e79c2020-01-21 16:56:22 -08001603}
1604
Paolo Abenib51f9b82020-03-27 14:48:44 -07001605#define MPTCP_DEFERRED_ALL (TCPF_DELACK_TIMER_DEFERRED | \
1606 TCPF_WRITE_TIMER_DEFERRED)
Paolo Abeni14c441b2020-02-26 10:14:52 +01001607
1608/* this is very alike tcp_release_cb() but we must handle differently a
1609 * different set of events
1610 */
1611static void mptcp_release_cb(struct sock *sk)
1612{
1613 unsigned long flags, nflags;
1614
1615 do {
1616 flags = sk->sk_tsq_flags;
1617 if (!(flags & MPTCP_DEFERRED_ALL))
1618 return;
1619 nflags = flags & ~MPTCP_DEFERRED_ALL;
1620 } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
1621
Paolo Abenib51f9b82020-03-27 14:48:44 -07001622 sock_release_ownership(sk);
1623
Paolo Abeni14c441b2020-02-26 10:14:52 +01001624 if (flags & TCPF_DELACK_TIMER_DEFERRED) {
1625 struct mptcp_sock *msk = mptcp_sk(sk);
1626 struct sock *ssk;
1627
1628 ssk = mptcp_subflow_recv_lookup(msk);
1629 if (!ssk || !schedule_work(&msk->work))
1630 __sock_put(sk);
1631 }
Paolo Abenib51f9b82020-03-27 14:48:44 -07001632
1633 if (flags & TCPF_WRITE_TIMER_DEFERRED) {
1634 mptcp_retransmit_handler(sk);
1635 __sock_put(sk);
1636 }
Paolo Abeni14c441b2020-02-26 10:14:52 +01001637}
1638
Peter Krystadcec37a62020-01-21 16:56:18 -08001639static int mptcp_get_port(struct sock *sk, unsigned short snum)
Mat Martineauf870fa02020-01-21 16:56:15 -08001640{
1641 struct mptcp_sock *msk = mptcp_sk(sk);
Peter Krystadcec37a62020-01-21 16:56:18 -08001642 struct socket *ssock;
Mat Martineauf870fa02020-01-21 16:56:15 -08001643
Peter Krystadcec37a62020-01-21 16:56:18 -08001644 ssock = __mptcp_nmpc_socket(msk);
1645 pr_debug("msk=%p, subflow=%p", msk, ssock);
1646 if (WARN_ON_ONCE(!ssock))
1647 return -EINVAL;
Mat Martineauf870fa02020-01-21 16:56:15 -08001648
Peter Krystadcec37a62020-01-21 16:56:18 -08001649 return inet_csk_get_port(ssock->sk, snum);
1650}
Mat Martineauf870fa02020-01-21 16:56:15 -08001651
Peter Krystadcec37a62020-01-21 16:56:18 -08001652void mptcp_finish_connect(struct sock *ssk)
1653{
1654 struct mptcp_subflow_context *subflow;
1655 struct mptcp_sock *msk;
1656 struct sock *sk;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001657 u64 ack_seq;
Mat Martineauf870fa02020-01-21 16:56:15 -08001658
Peter Krystadcec37a62020-01-21 16:56:18 -08001659 subflow = mptcp_subflow_ctx(ssk);
Peter Krystadcec37a62020-01-21 16:56:18 -08001660 sk = subflow->conn;
1661 msk = mptcp_sk(sk);
1662
Florian Westphalfc518952020-03-27 14:48:50 -07001663 if (!subflow->mp_capable) {
1664 MPTCP_INC_STATS(sock_net(sk),
1665 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
1666 return;
1667 }
1668
Mat Martineau648ef4b2020-01-21 16:56:24 -08001669 pr_debug("msk=%p, token=%u", sk, subflow->token);
1670
Mat Martineau6d0060f2020-01-21 16:56:23 -08001671 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
1672 ack_seq++;
Mat Martineau648ef4b2020-01-21 16:56:24 -08001673 subflow->map_seq = ack_seq;
1674 subflow->map_subflow_seq = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001675 subflow->rel_write_seq = 1;
1676
Peter Krystadcec37a62020-01-21 16:56:18 -08001677 /* the socket is not connected yet, no msk/subflow ops can access/race
1678 * accessing the field below
1679 */
1680 WRITE_ONCE(msk->remote_key, subflow->remote_key);
1681 WRITE_ONCE(msk->local_key, subflow->local_key);
Peter Krystad79c09492020-01-21 16:56:20 -08001682 WRITE_ONCE(msk->token, subflow->token);
Mat Martineau6d0060f2020-01-21 16:56:23 -08001683 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
1684 WRITE_ONCE(msk->ack_seq, ack_seq);
Christoph Paaschd22f4982020-01-21 16:56:32 -08001685 WRITE_ONCE(msk->can_ack, 1);
Paolo Abenicc9d2562020-03-27 14:48:42 -07001686 atomic64_set(&msk->snd_una, msk->write_seq);
Peter Krystad1b1c7a02020-03-27 14:48:38 -07001687
1688 mptcp_pm_new_connection(msk, 0);
Mat Martineauf870fa02020-01-21 16:56:15 -08001689}
1690
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001691static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
1692{
1693 write_lock_bh(&sk->sk_callback_lock);
1694 rcu_assign_pointer(sk->sk_wq, &parent->wq);
1695 sk_set_socket(sk, parent);
1696 sk->sk_uid = SOCK_INODE(parent)->i_uid;
1697 write_unlock_bh(&sk->sk_callback_lock);
1698}
1699
Peter Krystadf2962342020-03-27 14:48:39 -07001700bool mptcp_finish_join(struct sock *sk)
1701{
1702 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1703 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1704 struct sock *parent = (void *)msk;
1705 struct socket *parent_sock;
Peter Krystadec3edaa2020-03-27 14:48:40 -07001706 bool ret;
Peter Krystadf2962342020-03-27 14:48:39 -07001707
1708 pr_debug("msk=%p, subflow=%p", msk, subflow);
1709
1710 /* mptcp socket already closing? */
1711 if (inet_sk_state_load(parent) != TCP_ESTABLISHED)
1712 return false;
1713
1714 if (!msk->pm.server_side)
1715 return true;
1716
Paolo Abeni10f6d462020-05-29 17:43:30 +02001717 if (!mptcp_pm_allow_new_subflow(msk))
1718 return false;
1719
1720 /* active connections are already on conn_list, and we can't acquire
1721 * msk lock here.
1722 * use the join list lock as synchronization point and double-check
1723 * msk status to avoid racing with mptcp_close()
1724 */
1725 spin_lock_bh(&msk->join_list_lock);
1726 ret = inet_sk_state_load(parent) == TCP_ESTABLISHED;
1727 if (ret && !WARN_ON_ONCE(!list_empty(&subflow->node)))
1728 list_add_tail(&subflow->node, &msk->join_list);
1729 spin_unlock_bh(&msk->join_list_lock);
1730 if (!ret)
1731 return false;
1732
1733 /* attach to msk socket only after we are sure he will deal with us
1734 * at close time
1735 */
Peter Krystadf2962342020-03-27 14:48:39 -07001736 parent_sock = READ_ONCE(parent->sk_socket);
1737 if (parent_sock && !sk->sk_socket)
1738 mptcp_sock_graft(sk, parent_sock);
Paolo Abeni10f6d462020-05-29 17:43:30 +02001739 subflow->map_seq = msk->ack_seq;
1740 return true;
Peter Krystadf2962342020-03-27 14:48:39 -07001741}
1742
Florian Westphal1891c4a2020-01-21 16:56:25 -08001743static bool mptcp_memory_free(const struct sock *sk, int wake)
1744{
1745 struct mptcp_sock *msk = mptcp_sk(sk);
1746
1747 return wake ? test_bit(MPTCP_SEND_SPACE, &msk->flags) : true;
1748}
1749
Mat Martineauf870fa02020-01-21 16:56:15 -08001750static struct proto mptcp_prot = {
1751 .name = "MPTCP",
1752 .owner = THIS_MODULE,
1753 .init = mptcp_init_sock,
Paolo Abeni18b683b2020-03-27 14:48:43 -07001754 .disconnect = mptcp_disconnect,
Mat Martineauf870fa02020-01-21 16:56:15 -08001755 .close = mptcp_close,
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001756 .accept = mptcp_accept,
Peter Krystad717e79c2020-01-21 16:56:22 -08001757 .setsockopt = mptcp_setsockopt,
1758 .getsockopt = mptcp_getsockopt,
Mat Martineauf870fa02020-01-21 16:56:15 -08001759 .shutdown = tcp_shutdown,
Peter Krystad79c09492020-01-21 16:56:20 -08001760 .destroy = mptcp_destroy,
Mat Martineauf870fa02020-01-21 16:56:15 -08001761 .sendmsg = mptcp_sendmsg,
1762 .recvmsg = mptcp_recvmsg,
Paolo Abeni14c441b2020-02-26 10:14:52 +01001763 .release_cb = mptcp_release_cb,
Mat Martineauf870fa02020-01-21 16:56:15 -08001764 .hash = inet_hash,
1765 .unhash = inet_unhash,
Peter Krystadcec37a62020-01-21 16:56:18 -08001766 .get_port = mptcp_get_port,
Paolo Abenid0272362020-03-27 14:48:45 -07001767 .sockets_allocated = &mptcp_sockets_allocated,
1768 .memory_allocated = &tcp_memory_allocated,
1769 .memory_pressure = &tcp_memory_pressure,
Florian Westphal1891c4a2020-01-21 16:56:25 -08001770 .stream_memory_free = mptcp_memory_free,
Paolo Abenid0272362020-03-27 14:48:45 -07001771 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
1772 .sysctl_mem = sysctl_tcp_mem,
Mat Martineauf870fa02020-01-21 16:56:15 -08001773 .obj_size = sizeof(struct mptcp_sock),
1774 .no_autobind = true,
1775};
1776
Peter Krystad2303f992020-01-21 16:56:17 -08001777static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1778{
1779 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1780 struct socket *ssock;
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001781 int err;
Peter Krystad2303f992020-01-21 16:56:17 -08001782
1783 lock_sock(sock->sk);
1784 ssock = __mptcp_socket_create(msk, MPTCP_SAME_STATE);
1785 if (IS_ERR(ssock)) {
1786 err = PTR_ERR(ssock);
1787 goto unlock;
1788 }
1789
1790 err = ssock->ops->bind(ssock, uaddr, addr_len);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001791 if (!err)
1792 mptcp_copy_inaddrs(sock->sk, ssock->sk);
Peter Krystad2303f992020-01-21 16:56:17 -08001793
1794unlock:
1795 release_sock(sock->sk);
1796 return err;
1797}
1798
1799static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1800 int addr_len, int flags)
1801{
1802 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1803 struct socket *ssock;
1804 int err;
1805
1806 lock_sock(sock->sk);
Paolo Abeni41be81a2020-05-29 17:43:29 +02001807 if (sock->state != SS_UNCONNECTED && msk->subflow) {
1808 /* pending connection or invalid state, let existing subflow
1809 * cope with that
1810 */
1811 ssock = msk->subflow;
1812 goto do_connect;
1813 }
1814
Peter Krystad2303f992020-01-21 16:56:17 -08001815 ssock = __mptcp_socket_create(msk, TCP_SYN_SENT);
1816 if (IS_ERR(ssock)) {
1817 err = PTR_ERR(ssock);
1818 goto unlock;
1819 }
1820
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001821#ifdef CONFIG_TCP_MD5SIG
1822 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
1823 * TCP option space.
1824 */
1825 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
1826 mptcp_subflow_ctx(ssock->sk)->request_mptcp = 0;
1827#endif
1828
Paolo Abeni41be81a2020-05-29 17:43:29 +02001829do_connect:
Peter Krystad2303f992020-01-21 16:56:17 -08001830 err = ssock->ops->connect(ssock, uaddr, addr_len, flags);
Paolo Abeni41be81a2020-05-29 17:43:29 +02001831 sock->state = ssock->state;
1832
1833 /* on successful connect, the msk state will be moved to established by
1834 * subflow_finish_connect()
1835 */
1836 if (!err || err == EINPROGRESS)
1837 mptcp_copy_inaddrs(sock->sk, ssock->sk);
1838 else
1839 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
Peter Krystad2303f992020-01-21 16:56:17 -08001840
1841unlock:
1842 release_sock(sock->sk);
1843 return err;
1844}
1845
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001846static int mptcp_v4_getname(struct socket *sock, struct sockaddr *uaddr,
1847 int peer)
1848{
1849 if (sock->sk->sk_prot == &tcp_prot) {
1850 /* we are being invoked from __sys_accept4, after
1851 * mptcp_accept() has just accepted a non-mp-capable
1852 * flow: sk is a tcp_sk, not an mptcp one.
1853 *
1854 * Hand the socket over to tcp so all further socket ops
1855 * bypass mptcp.
1856 */
1857 sock->ops = &inet_stream_ops;
1858 }
1859
1860 return inet_getname(sock, uaddr, peer);
1861}
1862
1863#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1864static int mptcp_v6_getname(struct socket *sock, struct sockaddr *uaddr,
1865 int peer)
1866{
1867 if (sock->sk->sk_prot == &tcpv6_prot) {
1868 /* we are being invoked from __sys_accept4 after
1869 * mptcp_accept() has accepted a non-mp-capable
1870 * subflow: sk is a tcp_sk, not mptcp.
1871 *
1872 * Hand the socket over to tcp so all further
1873 * socket ops bypass mptcp.
1874 */
1875 sock->ops = &inet6_stream_ops;
1876 }
1877
1878 return inet6_getname(sock, uaddr, peer);
1879}
1880#endif
1881
1882static int mptcp_listen(struct socket *sock, int backlog)
1883{
1884 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1885 struct socket *ssock;
1886 int err;
1887
1888 pr_debug("msk=%p", msk);
1889
1890 lock_sock(sock->sk);
1891 ssock = __mptcp_socket_create(msk, TCP_LISTEN);
1892 if (IS_ERR(ssock)) {
1893 err = PTR_ERR(ssock);
1894 goto unlock;
1895 }
1896
Florian Westphal5e200872020-04-20 16:25:04 +02001897 sock_set_flag(sock->sk, SOCK_RCU_FREE);
1898
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001899 err = ssock->ops->listen(ssock, backlog);
1900 inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
1901 if (!err)
1902 mptcp_copy_inaddrs(sock->sk, ssock->sk);
1903
1904unlock:
1905 release_sock(sock->sk);
1906 return err;
1907}
1908
1909static bool is_tcp_proto(const struct proto *p)
1910{
1911#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1912 return p == &tcp_prot || p == &tcpv6_prot;
1913#else
1914 return p == &tcp_prot;
1915#endif
1916}
1917
1918static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
1919 int flags, bool kern)
1920{
1921 struct mptcp_sock *msk = mptcp_sk(sock->sk);
1922 struct socket *ssock;
1923 int err;
1924
1925 pr_debug("msk=%p", msk);
1926
1927 lock_sock(sock->sk);
1928 if (sock->sk->sk_state != TCP_LISTEN)
1929 goto unlock_fail;
1930
1931 ssock = __mptcp_nmpc_socket(msk);
1932 if (!ssock)
1933 goto unlock_fail;
1934
1935 sock_hold(ssock->sk);
1936 release_sock(sock->sk);
1937
1938 err = ssock->ops->accept(sock, newsock, flags, kern);
1939 if (err == 0 && !is_tcp_proto(newsock->sk->sk_prot)) {
1940 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
1941 struct mptcp_subflow_context *subflow;
1942
1943 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
1944 * This is needed so NOSPACE flag can be set from tcp stack.
1945 */
Peter Krystadec3edaa2020-03-27 14:48:40 -07001946 __mptcp_flush_join_list(msk);
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001947 list_for_each_entry(subflow, &msk->conn_list, node) {
1948 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1949
1950 if (!ssk->sk_socket)
1951 mptcp_sock_graft(ssk, newsock);
1952 }
Peter Krystadcf7da0d2020-01-21 16:56:19 -08001953 }
1954
1955 sock_put(ssock->sk);
1956 return err;
1957
1958unlock_fail:
1959 release_sock(sock->sk);
1960 return -EINVAL;
1961}
1962
Peter Krystad2303f992020-01-21 16:56:17 -08001963static __poll_t mptcp_poll(struct file *file, struct socket *sock,
1964 struct poll_table_struct *wait)
1965{
Florian Westphal1891c4a2020-01-21 16:56:25 -08001966 struct sock *sk = sock->sk;
Paolo Abeni8ab183d2020-01-21 16:56:33 -08001967 struct mptcp_sock *msk;
Florian Westphal1891c4a2020-01-21 16:56:25 -08001968 struct socket *ssock;
Peter Krystad2303f992020-01-21 16:56:17 -08001969 __poll_t mask = 0;
1970
Florian Westphal1891c4a2020-01-21 16:56:25 -08001971 msk = mptcp_sk(sk);
1972 lock_sock(sk);
Florian Westphal0b4f33d2020-04-02 13:44:51 +02001973 ssock = __mptcp_tcp_fallback(msk);
1974 if (!ssock)
1975 ssock = __mptcp_nmpc_socket(msk);
Florian Westphal1891c4a2020-01-21 16:56:25 -08001976 if (ssock) {
1977 mask = ssock->ops->poll(file, ssock, wait);
1978 release_sock(sk);
1979 return mask;
1980 }
1981
1982 release_sock(sk);
1983 sock_poll_wait(file, sock, wait);
1984 lock_sock(sk);
1985
1986 if (test_bit(MPTCP_DATA_READY, &msk->flags))
1987 mask = EPOLLIN | EPOLLRDNORM;
1988 if (sk_stream_is_writeable(sk) &&
1989 test_bit(MPTCP_SEND_SPACE, &msk->flags))
1990 mask |= EPOLLOUT | EPOLLWRNORM;
1991 if (sk->sk_shutdown & RCV_SHUTDOWN)
1992 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1993
1994 release_sock(sk);
1995
Peter Krystad2303f992020-01-21 16:56:17 -08001996 return mask;
1997}
1998
Peter Krystad21498492020-01-21 16:56:21 -08001999static int mptcp_shutdown(struct socket *sock, int how)
2000{
2001 struct mptcp_sock *msk = mptcp_sk(sock->sk);
2002 struct mptcp_subflow_context *subflow;
Florian Westphal0b4f33d2020-04-02 13:44:51 +02002003 struct socket *ssock;
Peter Krystad21498492020-01-21 16:56:21 -08002004 int ret = 0;
2005
2006 pr_debug("sk=%p, how=%d", msk, how);
2007
2008 lock_sock(sock->sk);
Florian Westphal0b4f33d2020-04-02 13:44:51 +02002009 ssock = __mptcp_tcp_fallback(msk);
2010 if (ssock) {
2011 release_sock(sock->sk);
2012 return inet_shutdown(ssock, how);
2013 }
Peter Krystad21498492020-01-21 16:56:21 -08002014
2015 if (how == SHUT_WR || how == SHUT_RDWR)
2016 inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
2017
2018 how++;
2019
2020 if ((how & ~SHUTDOWN_MASK) || !how) {
2021 ret = -EINVAL;
2022 goto out_unlock;
2023 }
2024
2025 if (sock->state == SS_CONNECTING) {
2026 if ((1 << sock->sk->sk_state) &
2027 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
2028 sock->state = SS_DISCONNECTING;
2029 else
2030 sock->state = SS_CONNECTED;
2031 }
2032
Peter Krystadec3edaa2020-03-27 14:48:40 -07002033 __mptcp_flush_join_list(msk);
Peter Krystad21498492020-01-21 16:56:21 -08002034 mptcp_for_each_subflow(msk, subflow) {
2035 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2036
Mat Martineau76c42a22020-02-28 15:47:40 -08002037 mptcp_subflow_shutdown(tcp_sk, how, 1, msk->write_seq);
Peter Krystad21498492020-01-21 16:56:21 -08002038 }
2039
2040out_unlock:
2041 release_sock(sock->sk);
2042
2043 return ret;
2044}
2045
Florian Westphale42f1ac2020-01-24 16:04:02 -08002046static const struct proto_ops mptcp_stream_ops = {
2047 .family = PF_INET,
2048 .owner = THIS_MODULE,
2049 .release = inet_release,
2050 .bind = mptcp_bind,
2051 .connect = mptcp_stream_connect,
2052 .socketpair = sock_no_socketpair,
2053 .accept = mptcp_stream_accept,
2054 .getname = mptcp_v4_getname,
2055 .poll = mptcp_poll,
2056 .ioctl = inet_ioctl,
2057 .gettstamp = sock_gettstamp,
2058 .listen = mptcp_listen,
2059 .shutdown = mptcp_shutdown,
2060 .setsockopt = sock_common_setsockopt,
2061 .getsockopt = sock_common_getsockopt,
2062 .sendmsg = inet_sendmsg,
2063 .recvmsg = inet_recvmsg,
2064 .mmap = sock_no_mmap,
2065 .sendpage = inet_sendpage,
2066#ifdef CONFIG_COMPAT
2067 .compat_setsockopt = compat_sock_common_setsockopt,
2068 .compat_getsockopt = compat_sock_common_getsockopt,
2069#endif
2070};
Peter Krystad2303f992020-01-21 16:56:17 -08002071
Mat Martineauf870fa02020-01-21 16:56:15 -08002072static struct inet_protosw mptcp_protosw = {
2073 .type = SOCK_STREAM,
2074 .protocol = IPPROTO_MPTCP,
2075 .prot = &mptcp_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08002076 .ops = &mptcp_stream_ops,
2077 .flags = INET_PROTOSW_ICSK,
Mat Martineauf870fa02020-01-21 16:56:15 -08002078};
2079
Matthieu Baerts784325e2020-01-21 16:56:28 -08002080void mptcp_proto_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08002081{
Peter Krystad2303f992020-01-21 16:56:17 -08002082 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
Peter Krystad2303f992020-01-21 16:56:17 -08002083
Paolo Abenid0272362020-03-27 14:48:45 -07002084 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
2085 panic("Failed to allocate MPTCP pcpu counter\n");
2086
Peter Krystad2303f992020-01-21 16:56:17 -08002087 mptcp_subflow_init();
Peter Krystad1b1c7a02020-03-27 14:48:38 -07002088 mptcp_pm_init();
Peter Krystad2303f992020-01-21 16:56:17 -08002089
Mat Martineauf870fa02020-01-21 16:56:15 -08002090 if (proto_register(&mptcp_prot, 1) != 0)
2091 panic("Failed to register MPTCP proto.\n");
2092
2093 inet_register_protosw(&mptcp_protosw);
Florian Westphal6771bfd2020-02-26 10:14:48 +01002094
2095 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
Mat Martineauf870fa02020-01-21 16:56:15 -08002096}
2097
2098#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Florian Westphale42f1ac2020-01-24 16:04:02 -08002099static const struct proto_ops mptcp_v6_stream_ops = {
2100 .family = PF_INET6,
2101 .owner = THIS_MODULE,
2102 .release = inet6_release,
2103 .bind = mptcp_bind,
2104 .connect = mptcp_stream_connect,
2105 .socketpair = sock_no_socketpair,
2106 .accept = mptcp_stream_accept,
2107 .getname = mptcp_v6_getname,
2108 .poll = mptcp_poll,
2109 .ioctl = inet6_ioctl,
2110 .gettstamp = sock_gettstamp,
2111 .listen = mptcp_listen,
2112 .shutdown = mptcp_shutdown,
2113 .setsockopt = sock_common_setsockopt,
2114 .getsockopt = sock_common_getsockopt,
2115 .sendmsg = inet6_sendmsg,
2116 .recvmsg = inet6_recvmsg,
2117 .mmap = sock_no_mmap,
2118 .sendpage = inet_sendpage,
2119#ifdef CONFIG_COMPAT
Christoph Hellwig39869122020-05-18 08:28:06 +02002120 .compat_ioctl = inet6_compat_ioctl,
Florian Westphale42f1ac2020-01-24 16:04:02 -08002121 .compat_setsockopt = compat_sock_common_setsockopt,
2122 .compat_getsockopt = compat_sock_common_getsockopt,
2123#endif
2124};
2125
Mat Martineauf870fa02020-01-21 16:56:15 -08002126static struct proto mptcp_v6_prot;
2127
Peter Krystad79c09492020-01-21 16:56:20 -08002128static void mptcp_v6_destroy(struct sock *sk)
2129{
2130 mptcp_destroy(sk);
2131 inet6_destroy_sock(sk);
2132}
2133
Mat Martineauf870fa02020-01-21 16:56:15 -08002134static struct inet_protosw mptcp_v6_protosw = {
2135 .type = SOCK_STREAM,
2136 .protocol = IPPROTO_MPTCP,
2137 .prot = &mptcp_v6_prot,
Peter Krystad2303f992020-01-21 16:56:17 -08002138 .ops = &mptcp_v6_stream_ops,
Mat Martineauf870fa02020-01-21 16:56:15 -08002139 .flags = INET_PROTOSW_ICSK,
2140};
2141
Matthieu Baerts784325e2020-01-21 16:56:28 -08002142int mptcp_proto_v6_init(void)
Mat Martineauf870fa02020-01-21 16:56:15 -08002143{
2144 int err;
2145
2146 mptcp_v6_prot = mptcp_prot;
2147 strcpy(mptcp_v6_prot.name, "MPTCPv6");
2148 mptcp_v6_prot.slab = NULL;
Peter Krystad79c09492020-01-21 16:56:20 -08002149 mptcp_v6_prot.destroy = mptcp_v6_destroy;
Florian Westphalb0519de2020-02-06 00:39:37 +01002150 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
Mat Martineauf870fa02020-01-21 16:56:15 -08002151
2152 err = proto_register(&mptcp_v6_prot, 1);
2153 if (err)
2154 return err;
2155
Mat Martineauf870fa02020-01-21 16:56:15 -08002156 err = inet6_register_protosw(&mptcp_v6_protosw);
2157 if (err)
2158 proto_unregister(&mptcp_v6_prot);
2159
2160 return err;
2161}
2162#endif