1 // 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>
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 #include <net/tcp_states.h>
20 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
21 #include <net/transp_v6.h>
22 #endif
23 #include <net/mptcp.h>
24 #include <net/xfrm.h>
25 #include <asm/ioctls.h>
26 #include "protocol.h"
27 #include "mib.h"
28
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/mptcp.h>
31
32 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
33 struct mptcp6_sock {
34 struct mptcp_sock msk;
35 struct ipv6_pinfo np;
36 };
37 #endif
38
39 struct mptcp_skb_cb {
40 u64 map_seq;
41 u64 end_seq;
42 u32 offset;
43 u8 has_rxtstamp:1;
44 };
45
46 #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
47
48 enum {
49 MPTCP_CMSG_TS = BIT(0),
50 MPTCP_CMSG_INQ = BIT(1),
51 };
52
53 static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
54
55 static void __mptcp_destroy_sock(struct sock *sk);
56 static void mptcp_check_send_data_fin(struct sock *sk);
57
58 DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
59 static struct net_device mptcp_napi_dev;
60
61 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
62 * completed yet or has failed, return the subflow socket.
63 * Otherwise return NULL.
64 */
__mptcp_nmpc_socket(const struct mptcp_sock * msk)65 struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk)
66 {
67 if (!msk->subflow || READ_ONCE(msk->can_ack))
68 return NULL;
69
70 return msk->subflow;
71 }
72
73 /* Returns end sequence number of the receiver's advertised window */
mptcp_wnd_end(const struct mptcp_sock * msk)74 static u64 mptcp_wnd_end(const struct mptcp_sock *msk)
75 {
76 return READ_ONCE(msk->wnd_end);
77 }
78
mptcp_is_tcpsk(struct sock * sk)79 static bool mptcp_is_tcpsk(struct sock *sk)
80 {
81 struct socket *sock = sk->sk_socket;
82
83 if (unlikely(sk->sk_prot == &tcp_prot)) {
84 /* we are being invoked after mptcp_accept() has
85 * accepted a non-mp-capable flow: sk is a tcp_sk,
86 * not an mptcp one.
87 *
88 * Hand the socket over to tcp so all further socket ops
89 * bypass mptcp.
90 */
91 sock->ops = &inet_stream_ops;
92 return true;
93 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
94 } else if (unlikely(sk->sk_prot == &tcpv6_prot)) {
95 sock->ops = &inet6_stream_ops;
96 return true;
97 #endif
98 }
99
100 return false;
101 }
102
__mptcp_socket_create(struct mptcp_sock * msk)103 static int __mptcp_socket_create(struct mptcp_sock *msk)
104 {
105 struct mptcp_subflow_context *subflow;
106 struct sock *sk = (struct sock *)msk;
107 struct socket *ssock;
108 int err;
109
110 err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
111 if (err)
112 return err;
113
114 WRITE_ONCE(msk->first, ssock->sk);
115 WRITE_ONCE(msk->subflow, ssock);
116 subflow = mptcp_subflow_ctx(ssock->sk);
117 list_add(&subflow->node, &msk->conn_list);
118 sock_hold(ssock->sk);
119 subflow->request_mptcp = 1;
120
121 /* This is the first subflow, always with id 0 */
122 subflow->local_id_valid = 1;
123 mptcp_sock_graft(msk->first, sk->sk_socket);
124
125 return 0;
126 }
127
mptcp_drop(struct sock * sk,struct sk_buff * skb)128 static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
129 {
130 sk_drops_add(sk, skb);
131 __kfree_skb(skb);
132 }
133
mptcp_rmem_fwd_alloc_add(struct sock * sk,int size)134 static void mptcp_rmem_fwd_alloc_add(struct sock *sk, int size)
135 {
136 WRITE_ONCE(mptcp_sk(sk)->rmem_fwd_alloc,
137 mptcp_sk(sk)->rmem_fwd_alloc + size);
138 }
139
mptcp_rmem_charge(struct sock * sk,int size)140 static void mptcp_rmem_charge(struct sock *sk, int size)
141 {
142 mptcp_rmem_fwd_alloc_add(sk, -size);
143 }
144
mptcp_try_coalesce(struct sock * sk,struct sk_buff * to,struct sk_buff * from)145 static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
146 struct sk_buff *from)
147 {
148 bool fragstolen;
149 int delta;
150
151 if (MPTCP_SKB_CB(from)->offset ||
152 !skb_try_coalesce(to, from, &fragstolen, &delta))
153 return false;
154
155 pr_debug("colesced seq %llx into %llx new len %d new end seq %llx",
156 MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
157 to->len, MPTCP_SKB_CB(from)->end_seq);
158 MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
159
160 /* note the fwd memory can reach a negative value after accounting
161 * for the delta, but the later skb free will restore a non
162 * negative one
163 */
164 atomic_add(delta, &sk->sk_rmem_alloc);
165 mptcp_rmem_charge(sk, delta);
166 kfree_skb_partial(from, fragstolen);
167
168 return true;
169 }
170
mptcp_ooo_try_coalesce(struct mptcp_sock * msk,struct sk_buff * to,struct sk_buff * from)171 static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
172 struct sk_buff *from)
173 {
174 if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
175 return false;
176
177 return mptcp_try_coalesce((struct sock *)msk, to, from);
178 }
179
__mptcp_rmem_reclaim(struct sock * sk,int amount)180 static void __mptcp_rmem_reclaim(struct sock *sk, int amount)
181 {
182 amount >>= PAGE_SHIFT;
183 mptcp_rmem_charge(sk, amount << PAGE_SHIFT);
184 __sk_mem_reduce_allocated(sk, amount);
185 }
186
mptcp_rmem_uncharge(struct sock * sk,int size)187 static void mptcp_rmem_uncharge(struct sock *sk, int size)
188 {
189 struct mptcp_sock *msk = mptcp_sk(sk);
190 int reclaimable;
191
192 mptcp_rmem_fwd_alloc_add(sk, size);
193 reclaimable = msk->rmem_fwd_alloc - sk_unused_reserved_mem(sk);
194
195 /* see sk_mem_uncharge() for the rationale behind the following schema */
196 if (unlikely(reclaimable >= PAGE_SIZE))
197 __mptcp_rmem_reclaim(sk, reclaimable);
198 }
199
mptcp_rfree(struct sk_buff * skb)200 static void mptcp_rfree(struct sk_buff *skb)
201 {
202 unsigned int len = skb->truesize;
203 struct sock *sk = skb->sk;
204
205 atomic_sub(len, &sk->sk_rmem_alloc);
206 mptcp_rmem_uncharge(sk, len);
207 }
208
mptcp_set_owner_r(struct sk_buff * skb,struct sock * sk)209 static void mptcp_set_owner_r(struct sk_buff *skb, struct sock *sk)
210 {
211 skb_orphan(skb);
212 skb->sk = sk;
213 skb->destructor = mptcp_rfree;
214 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
215 mptcp_rmem_charge(sk, skb->truesize);
216 }
217
218 /* "inspired" by tcp_data_queue_ofo(), main differences:
219 * - use mptcp seqs
220 * - don't cope with sacks
221 */
mptcp_data_queue_ofo(struct mptcp_sock * msk,struct sk_buff * skb)222 static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
223 {
224 struct sock *sk = (struct sock *)msk;
225 struct rb_node **p, *parent;
226 u64 seq, end_seq, max_seq;
227 struct sk_buff *skb1;
228
229 seq = MPTCP_SKB_CB(skb)->map_seq;
230 end_seq = MPTCP_SKB_CB(skb)->end_seq;
231 max_seq = atomic64_read(&msk->rcv_wnd_sent);
232
233 pr_debug("msk=%p seq=%llx limit=%llx empty=%d", msk, seq, max_seq,
234 RB_EMPTY_ROOT(&msk->out_of_order_queue));
235 if (after64(end_seq, max_seq)) {
236 /* out of window */
237 mptcp_drop(sk, skb);
238 pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
239 (unsigned long long)end_seq - (unsigned long)max_seq,
240 (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
241 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
242 return;
243 }
244
245 p = &msk->out_of_order_queue.rb_node;
246 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
247 if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
248 rb_link_node(&skb->rbnode, NULL, p);
249 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
250 msk->ooo_last_skb = skb;
251 goto end;
252 }
253
254 /* with 2 subflows, adding at end of ooo queue is quite likely
255 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
256 */
257 if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
258 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
259 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
260 return;
261 }
262
263 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
264 if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
265 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
266 parent = &msk->ooo_last_skb->rbnode;
267 p = &parent->rb_right;
268 goto insert;
269 }
270
271 /* Find place to insert this segment. Handle overlaps on the way. */
272 parent = NULL;
273 while (*p) {
274 parent = *p;
275 skb1 = rb_to_skb(parent);
276 if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
277 p = &parent->rb_left;
278 continue;
279 }
280 if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
281 if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
282 /* All the bits are present. Drop. */
283 mptcp_drop(sk, skb);
284 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
285 return;
286 }
287 if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
288 /* partial overlap:
289 * | skb |
290 * | skb1 |
291 * continue traversing
292 */
293 } else {
294 /* skb's seq == skb1's seq and skb covers skb1.
295 * Replace skb1 with skb.
296 */
297 rb_replace_node(&skb1->rbnode, &skb->rbnode,
298 &msk->out_of_order_queue);
299 mptcp_drop(sk, skb1);
300 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
301 goto merge_right;
302 }
303 } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
304 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
305 return;
306 }
307 p = &parent->rb_right;
308 }
309
310 insert:
311 /* Insert segment into RB tree. */
312 rb_link_node(&skb->rbnode, parent, p);
313 rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
314
315 merge_right:
316 /* Remove other segments covered by skb. */
317 while ((skb1 = skb_rb_next(skb)) != NULL) {
318 if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
319 break;
320 rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
321 mptcp_drop(sk, skb1);
322 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
323 }
324 /* If there is no skb after us, we are the last_skb ! */
325 if (!skb1)
326 msk->ooo_last_skb = skb;
327
328 end:
329 skb_condense(skb);
330 mptcp_set_owner_r(skb, sk);
331 }
332
mptcp_rmem_schedule(struct sock * sk,struct sock * ssk,int size)333 static bool mptcp_rmem_schedule(struct sock *sk, struct sock *ssk, int size)
334 {
335 struct mptcp_sock *msk = mptcp_sk(sk);
336 int amt, amount;
337
338 if (size <= msk->rmem_fwd_alloc)
339 return true;
340
341 size -= msk->rmem_fwd_alloc;
342 amt = sk_mem_pages(size);
343 amount = amt << PAGE_SHIFT;
344 if (!__sk_mem_raise_allocated(sk, size, amt, SK_MEM_RECV))
345 return false;
346
347 mptcp_rmem_fwd_alloc_add(sk, amount);
348 return true;
349 }
350
__mptcp_move_skb(struct mptcp_sock * msk,struct sock * ssk,struct sk_buff * skb,unsigned int offset,size_t copy_len)351 static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
352 struct sk_buff *skb, unsigned int offset,
353 size_t copy_len)
354 {
355 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
356 struct sock *sk = (struct sock *)msk;
357 struct sk_buff *tail;
358 bool has_rxtstamp;
359
360 __skb_unlink(skb, &ssk->sk_receive_queue);
361
362 skb_ext_reset(skb);
363 skb_orphan(skb);
364
365 /* try to fetch required memory from subflow */
366 if (!mptcp_rmem_schedule(sk, ssk, skb->truesize))
367 goto drop;
368
369 has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
370
371 /* the skb map_seq accounts for the skb offset:
372 * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
373 * value
374 */
375 MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
376 MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
377 MPTCP_SKB_CB(skb)->offset = offset;
378 MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
379
380 if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
381 /* in sequence */
382 WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
383 tail = skb_peek_tail(&sk->sk_receive_queue);
384 if (tail && mptcp_try_coalesce(sk, tail, skb))
385 return true;
386
387 mptcp_set_owner_r(skb, sk);
388 __skb_queue_tail(&sk->sk_receive_queue, skb);
389 return true;
390 } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
391 mptcp_data_queue_ofo(msk, skb);
392 return false;
393 }
394
395 /* old data, keep it simple and drop the whole pkt, sender
396 * will retransmit as needed, if needed.
397 */
398 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
399 drop:
400 mptcp_drop(sk, skb);
401 return false;
402 }
403
mptcp_stop_rtx_timer(struct sock * sk)404 static void mptcp_stop_rtx_timer(struct sock *sk)
405 {
406 struct inet_connection_sock *icsk = inet_csk(sk);
407
408 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
409 mptcp_sk(sk)->timer_ival = 0;
410 }
411
mptcp_close_wake_up(struct sock * sk)412 static void mptcp_close_wake_up(struct sock *sk)
413 {
414 if (sock_flag(sk, SOCK_DEAD))
415 return;
416
417 sk->sk_state_change(sk);
418 if (sk->sk_shutdown == SHUTDOWN_MASK ||
419 sk->sk_state == TCP_CLOSE)
420 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
421 else
422 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
423 }
424
mptcp_pending_data_fin_ack(struct sock * sk)425 static bool mptcp_pending_data_fin_ack(struct sock *sk)
426 {
427 struct mptcp_sock *msk = mptcp_sk(sk);
428
429 return ((1 << sk->sk_state) &
430 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
431 msk->write_seq == READ_ONCE(msk->snd_una);
432 }
433
mptcp_check_data_fin_ack(struct sock * sk)434 static void mptcp_check_data_fin_ack(struct sock *sk)
435 {
436 struct mptcp_sock *msk = mptcp_sk(sk);
437
438 /* Look for an acknowledged DATA_FIN */
439 if (mptcp_pending_data_fin_ack(sk)) {
440 WRITE_ONCE(msk->snd_data_fin_enable, 0);
441
442 switch (sk->sk_state) {
443 case TCP_FIN_WAIT1:
444 inet_sk_state_store(sk, TCP_FIN_WAIT2);
445 break;
446 case TCP_CLOSING:
447 case TCP_LAST_ACK:
448 inet_sk_state_store(sk, TCP_CLOSE);
449 break;
450 }
451
452 mptcp_close_wake_up(sk);
453 }
454 }
455
mptcp_pending_data_fin(struct sock * sk,u64 * seq)456 static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
457 {
458 struct mptcp_sock *msk = mptcp_sk(sk);
459
460 if (READ_ONCE(msk->rcv_data_fin) &&
461 ((1 << sk->sk_state) &
462 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
463 u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
464
465 if (msk->ack_seq == rcv_data_fin_seq) {
466 if (seq)
467 *seq = rcv_data_fin_seq;
468
469 return true;
470 }
471 }
472
473 return false;
474 }
475
mptcp_set_datafin_timeout(const struct sock * sk)476 static void mptcp_set_datafin_timeout(const struct sock *sk)
477 {
478 struct inet_connection_sock *icsk = inet_csk(sk);
479 u32 retransmits;
480
481 retransmits = min_t(u32, icsk->icsk_retransmits,
482 ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
483
484 mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
485 }
486
__mptcp_set_timeout(struct sock * sk,long tout)487 static void __mptcp_set_timeout(struct sock *sk, long tout)
488 {
489 mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
490 }
491
mptcp_timeout_from_subflow(const struct mptcp_subflow_context * subflow)492 static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
493 {
494 const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
495
496 return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
497 inet_csk(ssk)->icsk_timeout - jiffies : 0;
498 }
499
mptcp_set_timeout(struct sock * sk)500 static void mptcp_set_timeout(struct sock *sk)
501 {
502 struct mptcp_subflow_context *subflow;
503 long tout = 0;
504
505 mptcp_for_each_subflow(mptcp_sk(sk), subflow)
506 tout = max(tout, mptcp_timeout_from_subflow(subflow));
507 __mptcp_set_timeout(sk, tout);
508 }
509
tcp_can_send_ack(const struct sock * ssk)510 static inline bool tcp_can_send_ack(const struct sock *ssk)
511 {
512 return !((1 << inet_sk_state_load(ssk)) &
513 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
514 }
515
__mptcp_subflow_send_ack(struct sock * ssk)516 void __mptcp_subflow_send_ack(struct sock *ssk)
517 {
518 if (tcp_can_send_ack(ssk))
519 tcp_send_ack(ssk);
520 }
521
mptcp_subflow_send_ack(struct sock * ssk)522 static void mptcp_subflow_send_ack(struct sock *ssk)
523 {
524 bool slow;
525
526 slow = lock_sock_fast(ssk);
527 __mptcp_subflow_send_ack(ssk);
528 unlock_sock_fast(ssk, slow);
529 }
530
mptcp_send_ack(struct mptcp_sock * msk)531 static void mptcp_send_ack(struct mptcp_sock *msk)
532 {
533 struct mptcp_subflow_context *subflow;
534
535 mptcp_for_each_subflow(msk, subflow)
536 mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
537 }
538
mptcp_subflow_cleanup_rbuf(struct sock * ssk)539 static void mptcp_subflow_cleanup_rbuf(struct sock *ssk)
540 {
541 bool slow;
542
543 slow = lock_sock_fast(ssk);
544 if (tcp_can_send_ack(ssk))
545 tcp_cleanup_rbuf(ssk, 1);
546 unlock_sock_fast(ssk, slow);
547 }
548
mptcp_subflow_could_cleanup(const struct sock * ssk,bool rx_empty)549 static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
550 {
551 const struct inet_connection_sock *icsk = inet_csk(ssk);
552 u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
553 const struct tcp_sock *tp = tcp_sk(ssk);
554
555 return (ack_pending & ICSK_ACK_SCHED) &&
556 ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
557 READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
558 (rx_empty && ack_pending &
559 (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
560 }
561
mptcp_cleanup_rbuf(struct mptcp_sock * msk)562 static void mptcp_cleanup_rbuf(struct mptcp_sock *msk)
563 {
564 int old_space = READ_ONCE(msk->old_wspace);
565 struct mptcp_subflow_context *subflow;
566 struct sock *sk = (struct sock *)msk;
567 int space = __mptcp_space(sk);
568 bool cleanup, rx_empty;
569
570 cleanup = (space > 0) && (space >= (old_space << 1));
571 rx_empty = !__mptcp_rmem(sk);
572
573 mptcp_for_each_subflow(msk, subflow) {
574 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
575
576 if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
577 mptcp_subflow_cleanup_rbuf(ssk);
578 }
579 }
580
mptcp_check_data_fin(struct sock * sk)581 static bool mptcp_check_data_fin(struct sock *sk)
582 {
583 struct mptcp_sock *msk = mptcp_sk(sk);
584 u64 rcv_data_fin_seq;
585 bool ret = false;
586
587 /* Need to ack a DATA_FIN received from a peer while this side
588 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
589 * msk->rcv_data_fin was set when parsing the incoming options
590 * at the subflow level and the msk lock was not held, so this
591 * is the first opportunity to act on the DATA_FIN and change
592 * the msk state.
593 *
594 * If we are caught up to the sequence number of the incoming
595 * DATA_FIN, send the DATA_ACK now and do state transition. If
596 * not caught up, do nothing and let the recv code send DATA_ACK
597 * when catching up.
598 */
599
600 if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
601 WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
602 WRITE_ONCE(msk->rcv_data_fin, 0);
603
604 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
605 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
606
607 switch (sk->sk_state) {
608 case TCP_ESTABLISHED:
609 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
610 break;
611 case TCP_FIN_WAIT1:
612 inet_sk_state_store(sk, TCP_CLOSING);
613 break;
614 case TCP_FIN_WAIT2:
615 inet_sk_state_store(sk, TCP_CLOSE);
616 break;
617 default:
618 /* Other states not expected */
619 WARN_ON_ONCE(1);
620 break;
621 }
622
623 ret = true;
624 if (!__mptcp_check_fallback(msk))
625 mptcp_send_ack(msk);
626 mptcp_close_wake_up(sk);
627 }
628 return ret;
629 }
630
__mptcp_move_skbs_from_subflow(struct mptcp_sock * msk,struct sock * ssk,unsigned int * bytes)631 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
632 struct sock *ssk,
633 unsigned int *bytes)
634 {
635 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
636 struct sock *sk = (struct sock *)msk;
637 unsigned int moved = 0;
638 bool more_data_avail;
639 struct tcp_sock *tp;
640 bool done = false;
641 int sk_rbuf;
642
643 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
644
645 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
646 int ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
647
648 if (unlikely(ssk_rbuf > sk_rbuf)) {
649 WRITE_ONCE(sk->sk_rcvbuf, ssk_rbuf);
650 sk_rbuf = ssk_rbuf;
651 }
652 }
653
654 pr_debug("msk=%p ssk=%p", msk, ssk);
655 tp = tcp_sk(ssk);
656 do {
657 u32 map_remaining, offset;
658 u32 seq = tp->copied_seq;
659 struct sk_buff *skb;
660 bool fin;
661
662 /* try to move as much data as available */
663 map_remaining = subflow->map_data_len -
664 mptcp_subflow_get_map_offset(subflow);
665
666 skb = skb_peek(&ssk->sk_receive_queue);
667 if (!skb) {
668 /* With racing move_skbs_to_msk() and __mptcp_move_skbs(),
669 * a different CPU can have already processed the pending
670 * data, stop here or we can enter an infinite loop
671 */
672 if (!moved)
673 done = true;
674 break;
675 }
676
677 if (__mptcp_check_fallback(msk)) {
678 /* Under fallback skbs have no MPTCP extension and TCP could
679 * collapse them between the dummy map creation and the
680 * current dequeue. Be sure to adjust the map size.
681 */
682 map_remaining = skb->len;
683 subflow->map_data_len = skb->len;
684 }
685
686 offset = seq - TCP_SKB_CB(skb)->seq;
687 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
688 if (fin) {
689 done = true;
690 seq++;
691 }
692
693 if (offset < skb->len) {
694 size_t len = skb->len - offset;
695
696 if (tp->urg_data)
697 done = true;
698
699 if (__mptcp_move_skb(msk, ssk, skb, offset, len))
700 moved += len;
701 seq += len;
702
703 if (WARN_ON_ONCE(map_remaining < len))
704 break;
705 } else {
706 WARN_ON_ONCE(!fin);
707 sk_eat_skb(ssk, skb);
708 done = true;
709 }
710
711 WRITE_ONCE(tp->copied_seq, seq);
712 more_data_avail = mptcp_subflow_data_available(ssk);
713
714 if (atomic_read(&sk->sk_rmem_alloc) > sk_rbuf) {
715 done = true;
716 break;
717 }
718 } while (more_data_avail);
719
720 *bytes += moved;
721 return done;
722 }
723
__mptcp_ofo_queue(struct mptcp_sock * msk)724 static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
725 {
726 struct sock *sk = (struct sock *)msk;
727 struct sk_buff *skb, *tail;
728 bool moved = false;
729 struct rb_node *p;
730 u64 end_seq;
731
732 p = rb_first(&msk->out_of_order_queue);
733 pr_debug("msk=%p empty=%d", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
734 while (p) {
735 skb = rb_to_skb(p);
736 if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
737 break;
738
739 p = rb_next(p);
740 rb_erase(&skb->rbnode, &msk->out_of_order_queue);
741
742 if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
743 msk->ack_seq))) {
744 mptcp_drop(sk, skb);
745 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
746 continue;
747 }
748
749 end_seq = MPTCP_SKB_CB(skb)->end_seq;
750 tail = skb_peek_tail(&sk->sk_receive_queue);
751 if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
752 int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
753
754 /* skip overlapping data, if any */
755 pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d",
756 MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
757 delta);
758 MPTCP_SKB_CB(skb)->offset += delta;
759 MPTCP_SKB_CB(skb)->map_seq += delta;
760 __skb_queue_tail(&sk->sk_receive_queue, skb);
761 }
762 msk->ack_seq = end_seq;
763 moved = true;
764 }
765 return moved;
766 }
767
__mptcp_subflow_error_report(struct sock * sk,struct sock * ssk)768 static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
769 {
770 int err = sock_error(ssk);
771 int ssk_state;
772
773 if (!err)
774 return false;
775
776 /* only propagate errors on fallen-back sockets or
777 * on MPC connect
778 */
779 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk)))
780 return false;
781
782 /* We need to propagate only transition to CLOSE state.
783 * Orphaned socket will see such state change via
784 * subflow_sched_work_if_closed() and that path will properly
785 * destroy the msk as needed.
786 */
787 ssk_state = inet_sk_state_load(ssk);
788 if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
789 inet_sk_state_store(sk, ssk_state);
790 WRITE_ONCE(sk->sk_err, -err);
791
792 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
793 smp_wmb();
794 sk_error_report(sk);
795 return true;
796 }
797
__mptcp_error_report(struct sock * sk)798 void __mptcp_error_report(struct sock *sk)
799 {
800 struct mptcp_subflow_context *subflow;
801 struct mptcp_sock *msk = mptcp_sk(sk);
802
803 mptcp_for_each_subflow(msk, subflow)
804 if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
805 break;
806 }
807
808 /* In most cases we will be able to lock the mptcp socket. If its already
809 * owned, we need to defer to the work queue to avoid ABBA deadlock.
810 */
move_skbs_to_msk(struct mptcp_sock * msk,struct sock * ssk)811 static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
812 {
813 struct sock *sk = (struct sock *)msk;
814 unsigned int moved = 0;
815
816 __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
817 __mptcp_ofo_queue(msk);
818 if (unlikely(ssk->sk_err)) {
819 if (!sock_owned_by_user(sk))
820 __mptcp_error_report(sk);
821 else
822 __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags);
823 }
824
825 /* If the moves have caught up with the DATA_FIN sequence number
826 * it's time to ack the DATA_FIN and change socket state, but
827 * this is not a good place to change state. Let the workqueue
828 * do it.
829 */
830 if (mptcp_pending_data_fin(sk, NULL))
831 mptcp_schedule_work(sk);
832 return moved > 0;
833 }
834
mptcp_data_ready(struct sock * sk,struct sock * ssk)835 void mptcp_data_ready(struct sock *sk, struct sock *ssk)
836 {
837 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
838 struct mptcp_sock *msk = mptcp_sk(sk);
839 int sk_rbuf, ssk_rbuf;
840
841 /* The peer can send data while we are shutting down this
842 * subflow at msk destruction time, but we must avoid enqueuing
843 * more data to the msk receive queue
844 */
845 if (unlikely(subflow->disposable))
846 return;
847
848 ssk_rbuf = READ_ONCE(ssk->sk_rcvbuf);
849 sk_rbuf = READ_ONCE(sk->sk_rcvbuf);
850 if (unlikely(ssk_rbuf > sk_rbuf))
851 sk_rbuf = ssk_rbuf;
852
853 /* over limit? can't append more skbs to msk, Also, no need to wake-up*/
854 if (__mptcp_rmem(sk) > sk_rbuf) {
855 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
856 return;
857 }
858
859 /* Wake-up the reader only for in-sequence data */
860 mptcp_data_lock(sk);
861 if (move_skbs_to_msk(msk, ssk))
862 sk->sk_data_ready(sk);
863
864 mptcp_data_unlock(sk);
865 }
866
mptcp_subflow_joined(struct mptcp_sock * msk,struct sock * ssk)867 static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
868 {
869 mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
870 WRITE_ONCE(msk->allow_infinite_fallback, false);
871 mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
872 }
873
__mptcp_finish_join(struct mptcp_sock * msk,struct sock * ssk)874 static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
875 {
876 struct sock *sk = (struct sock *)msk;
877
878 if (sk->sk_state != TCP_ESTABLISHED)
879 return false;
880
881 /* attach to msk socket only after we are sure we will deal with it
882 * at close time
883 */
884 if (sk->sk_socket && !ssk->sk_socket)
885 mptcp_sock_graft(ssk, sk->sk_socket);
886
887 mptcp_sockopt_sync_locked(msk, ssk);
888 mptcp_subflow_joined(msk, ssk);
889 mptcp_stop_tout_timer(sk);
890 return true;
891 }
892
__mptcp_flush_join_list(struct sock * sk,struct list_head * join_list)893 static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
894 {
895 struct mptcp_subflow_context *tmp, *subflow;
896 struct mptcp_sock *msk = mptcp_sk(sk);
897
898 list_for_each_entry_safe(subflow, tmp, join_list, node) {
899 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
900 bool slow = lock_sock_fast(ssk);
901
902 list_move_tail(&subflow->node, &msk->conn_list);
903 if (!__mptcp_finish_join(msk, ssk))
904 mptcp_subflow_reset(ssk);
905 unlock_sock_fast(ssk, slow);
906 }
907 }
908
mptcp_rtx_timer_pending(struct sock * sk)909 static bool mptcp_rtx_timer_pending(struct sock *sk)
910 {
911 return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
912 }
913
mptcp_reset_rtx_timer(struct sock * sk)914 static void mptcp_reset_rtx_timer(struct sock *sk)
915 {
916 struct inet_connection_sock *icsk = inet_csk(sk);
917 unsigned long tout;
918
919 /* prevent rescheduling on close */
920 if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
921 return;
922
923 tout = mptcp_sk(sk)->timer_ival;
924 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
925 }
926
mptcp_schedule_work(struct sock * sk)927 bool mptcp_schedule_work(struct sock *sk)
928 {
929 if (inet_sk_state_load(sk) != TCP_CLOSE &&
930 schedule_work(&mptcp_sk(sk)->work)) {
931 /* each subflow already holds a reference to the sk, and the
932 * workqueue is invoked by a subflow, so sk can't go away here.
933 */
934 sock_hold(sk);
935 return true;
936 }
937 return false;
938 }
939
mptcp_subflow_eof(struct sock * sk)940 void mptcp_subflow_eof(struct sock *sk)
941 {
942 if (!test_and_set_bit(MPTCP_WORK_EOF, &mptcp_sk(sk)->flags))
943 mptcp_schedule_work(sk);
944 }
945
mptcp_check_for_eof(struct mptcp_sock * msk)946 static void mptcp_check_for_eof(struct mptcp_sock *msk)
947 {
948 struct mptcp_subflow_context *subflow;
949 struct sock *sk = (struct sock *)msk;
950 int receivers = 0;
951
952 mptcp_for_each_subflow(msk, subflow)
953 receivers += !subflow->rx_eof;
954 if (receivers)
955 return;
956
957 if (!(sk->sk_shutdown & RCV_SHUTDOWN)) {
958 /* hopefully temporary hack: propagate shutdown status
959 * to msk, when all subflows agree on it
960 */
961 WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
962
963 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
964 sk->sk_data_ready(sk);
965 }
966
967 switch (sk->sk_state) {
968 case TCP_ESTABLISHED:
969 inet_sk_state_store(sk, TCP_CLOSE_WAIT);
970 break;
971 case TCP_FIN_WAIT1:
972 inet_sk_state_store(sk, TCP_CLOSING);
973 break;
974 case TCP_FIN_WAIT2:
975 inet_sk_state_store(sk, TCP_CLOSE);
976 break;
977 default:
978 return;
979 }
980 mptcp_close_wake_up(sk);
981 }
982
mptcp_subflow_recv_lookup(const struct mptcp_sock * msk)983 static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
984 {
985 struct mptcp_subflow_context *subflow;
986 struct sock *sk = (struct sock *)msk;
987
988 sock_owned_by_me(sk);
989
990 mptcp_for_each_subflow(msk, subflow) {
991 if (READ_ONCE(subflow->data_avail))
992 return mptcp_subflow_tcp_sock(subflow);
993 }
994
995 return NULL;
996 }
997
mptcp_skb_can_collapse_to(u64 write_seq,const struct sk_buff * skb,const struct mptcp_ext * mpext)998 static bool mptcp_skb_can_collapse_to(u64 write_seq,
999 const struct sk_buff *skb,
1000 const struct mptcp_ext *mpext)
1001 {
1002 if (!tcp_skb_can_collapse_to(skb))
1003 return false;
1004
1005 /* can collapse only if MPTCP level sequence is in order and this
1006 * mapping has not been xmitted yet
1007 */
1008 return mpext && mpext->data_seq + mpext->data_len == write_seq &&
1009 !mpext->frozen;
1010 }
1011
1012 /* we can append data to the given data frag if:
1013 * - there is space available in the backing page_frag
1014 * - the data frag tail matches the current page_frag free offset
1015 * - the data frag end sequence number matches the current write seq
1016 */
mptcp_frag_can_collapse_to(const struct mptcp_sock * msk,const struct page_frag * pfrag,const struct mptcp_data_frag * df)1017 static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
1018 const struct page_frag *pfrag,
1019 const struct mptcp_data_frag *df)
1020 {
1021 return df && pfrag->page == df->page &&
1022 pfrag->size - pfrag->offset > 0 &&
1023 pfrag->offset == (df->offset + df->data_len) &&
1024 df->data_seq + df->data_len == msk->write_seq;
1025 }
1026
dfrag_uncharge(struct sock * sk,int len)1027 static void dfrag_uncharge(struct sock *sk, int len)
1028 {
1029 sk_mem_uncharge(sk, len);
1030 sk_wmem_queued_add(sk, -len);
1031 }
1032
dfrag_clear(struct sock * sk,struct mptcp_data_frag * dfrag)1033 static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
1034 {
1035 int len = dfrag->data_len + dfrag->overhead;
1036
1037 list_del(&dfrag->list);
1038 dfrag_uncharge(sk, len);
1039 put_page(dfrag->page);
1040 }
1041
__mptcp_clean_una(struct sock * sk)1042 static void __mptcp_clean_una(struct sock *sk)
1043 {
1044 struct mptcp_sock *msk = mptcp_sk(sk);
1045 struct mptcp_data_frag *dtmp, *dfrag;
1046 u64 snd_una;
1047
1048 /* on fallback we just need to ignore snd_una, as this is really
1049 * plain TCP
1050 */
1051 if (__mptcp_check_fallback(msk))
1052 msk->snd_una = READ_ONCE(msk->snd_nxt);
1053
1054 snd_una = msk->snd_una;
1055 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1056 if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1057 break;
1058
1059 if (unlikely(dfrag == msk->first_pending)) {
1060 /* in recovery mode can see ack after the current snd head */
1061 if (WARN_ON_ONCE(!msk->recovery))
1062 break;
1063
1064 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1065 }
1066
1067 dfrag_clear(sk, dfrag);
1068 }
1069
1070 dfrag = mptcp_rtx_head(sk);
1071 if (dfrag && after64(snd_una, dfrag->data_seq)) {
1072 u64 delta = snd_una - dfrag->data_seq;
1073
1074 /* prevent wrap around in recovery mode */
1075 if (unlikely(delta > dfrag->already_sent)) {
1076 if (WARN_ON_ONCE(!msk->recovery))
1077 goto out;
1078 if (WARN_ON_ONCE(delta > dfrag->data_len))
1079 goto out;
1080 dfrag->already_sent += delta - dfrag->already_sent;
1081 }
1082
1083 dfrag->data_seq += delta;
1084 dfrag->offset += delta;
1085 dfrag->data_len -= delta;
1086 dfrag->already_sent -= delta;
1087
1088 dfrag_uncharge(sk, delta);
1089 }
1090
1091 /* all retransmitted data acked, recovery completed */
1092 if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
1093 msk->recovery = false;
1094
1095 out:
1096 if (snd_una == READ_ONCE(msk->snd_nxt) &&
1097 snd_una == READ_ONCE(msk->write_seq)) {
1098 if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
1099 mptcp_stop_rtx_timer(sk);
1100 } else {
1101 mptcp_reset_rtx_timer(sk);
1102 }
1103 }
1104
__mptcp_clean_una_wakeup(struct sock * sk)1105 static void __mptcp_clean_una_wakeup(struct sock *sk)
1106 {
1107 lockdep_assert_held_once(&sk->sk_lock.slock);
1108
1109 __mptcp_clean_una(sk);
1110 mptcp_write_space(sk);
1111 }
1112
mptcp_clean_una_wakeup(struct sock * sk)1113 static void mptcp_clean_una_wakeup(struct sock *sk)
1114 {
1115 mptcp_data_lock(sk);
1116 __mptcp_clean_una_wakeup(sk);
1117 mptcp_data_unlock(sk);
1118 }
1119
mptcp_enter_memory_pressure(struct sock * sk)1120 static void mptcp_enter_memory_pressure(struct sock *sk)
1121 {
1122 struct mptcp_subflow_context *subflow;
1123 struct mptcp_sock *msk = mptcp_sk(sk);
1124 bool first = true;
1125
1126 sk_stream_moderate_sndbuf(sk);
1127 mptcp_for_each_subflow(msk, subflow) {
1128 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1129
1130 if (first)
1131 tcp_enter_memory_pressure(ssk);
1132 sk_stream_moderate_sndbuf(ssk);
1133 first = false;
1134 }
1135 }
1136
1137 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1138 * data
1139 */
mptcp_page_frag_refill(struct sock * sk,struct page_frag * pfrag)1140 static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1141 {
1142 if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1143 pfrag, sk->sk_allocation)))
1144 return true;
1145
1146 mptcp_enter_memory_pressure(sk);
1147 return false;
1148 }
1149
1150 static struct mptcp_data_frag *
mptcp_carve_data_frag(const struct mptcp_sock * msk,struct page_frag * pfrag,int orig_offset)1151 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1152 int orig_offset)
1153 {
1154 int offset = ALIGN(orig_offset, sizeof(long));
1155 struct mptcp_data_frag *dfrag;
1156
1157 dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1158 dfrag->data_len = 0;
1159 dfrag->data_seq = msk->write_seq;
1160 dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1161 dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1162 dfrag->already_sent = 0;
1163 dfrag->page = pfrag->page;
1164
1165 return dfrag;
1166 }
1167
1168 struct mptcp_sendmsg_info {
1169 int mss_now;
1170 int size_goal;
1171 u16 limit;
1172 u16 sent;
1173 unsigned int flags;
1174 bool data_lock_held;
1175 };
1176
mptcp_check_allowed_size(const struct mptcp_sock * msk,struct sock * ssk,u64 data_seq,int avail_size)1177 static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1178 u64 data_seq, int avail_size)
1179 {
1180 u64 window_end = mptcp_wnd_end(msk);
1181 u64 mptcp_snd_wnd;
1182
1183 if (__mptcp_check_fallback(msk))
1184 return avail_size;
1185
1186 mptcp_snd_wnd = window_end - data_seq;
1187 avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1188
1189 if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1190 tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1191 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1192 }
1193
1194 return avail_size;
1195 }
1196
__mptcp_add_ext(struct sk_buff * skb,gfp_t gfp)1197 static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1198 {
1199 struct skb_ext *mpext = __skb_ext_alloc(gfp);
1200
1201 if (!mpext)
1202 return false;
1203 __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1204 return true;
1205 }
1206
__mptcp_do_alloc_tx_skb(struct sock * sk,gfp_t gfp)1207 static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1208 {
1209 struct sk_buff *skb;
1210
1211 skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1212 if (likely(skb)) {
1213 if (likely(__mptcp_add_ext(skb, gfp))) {
1214 skb_reserve(skb, MAX_TCP_HEADER);
1215 skb->ip_summed = CHECKSUM_PARTIAL;
1216 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1217 return skb;
1218 }
1219 __kfree_skb(skb);
1220 } else {
1221 mptcp_enter_memory_pressure(sk);
1222 }
1223 return NULL;
1224 }
1225
__mptcp_alloc_tx_skb(struct sock * sk,struct sock * ssk,gfp_t gfp)1226 static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1227 {
1228 struct sk_buff *skb;
1229
1230 skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1231 if (!skb)
1232 return NULL;
1233
1234 if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1235 tcp_skb_entail(ssk, skb);
1236 return skb;
1237 }
1238 tcp_skb_tsorted_anchor_cleanup(skb);
1239 kfree_skb(skb);
1240 return NULL;
1241 }
1242
mptcp_alloc_tx_skb(struct sock * sk,struct sock * ssk,bool data_lock_held)1243 static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1244 {
1245 gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1246
1247 return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1248 }
1249
1250 /* note: this always recompute the csum on the whole skb, even
1251 * if we just appended a single frag. More status info needed
1252 */
mptcp_update_data_checksum(struct sk_buff * skb,int added)1253 static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1254 {
1255 struct mptcp_ext *mpext = mptcp_get_ext(skb);
1256 __wsum csum = ~csum_unfold(mpext->csum);
1257 int offset = skb->len - added;
1258
1259 mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1260 }
1261
mptcp_update_infinite_map(struct mptcp_sock * msk,struct sock * ssk,struct mptcp_ext * mpext)1262 static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1263 struct sock *ssk,
1264 struct mptcp_ext *mpext)
1265 {
1266 if (!mpext)
1267 return;
1268
1269 mpext->infinite_map = 1;
1270 mpext->data_len = 0;
1271
1272 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1273 mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1274 pr_fallback(msk);
1275 mptcp_do_fallback(ssk);
1276 }
1277
1278 #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1))
1279
mptcp_sendmsg_frag(struct sock * sk,struct sock * ssk,struct mptcp_data_frag * dfrag,struct mptcp_sendmsg_info * info)1280 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1281 struct mptcp_data_frag *dfrag,
1282 struct mptcp_sendmsg_info *info)
1283 {
1284 u64 data_seq = dfrag->data_seq + info->sent;
1285 int offset = dfrag->offset + info->sent;
1286 struct mptcp_sock *msk = mptcp_sk(sk);
1287 bool zero_window_probe = false;
1288 struct mptcp_ext *mpext = NULL;
1289 bool can_coalesce = false;
1290 bool reuse_skb = true;
1291 struct sk_buff *skb;
1292 size_t copy;
1293 int i;
1294
1295 pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u",
1296 msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1297
1298 if (WARN_ON_ONCE(info->sent > info->limit ||
1299 info->limit > dfrag->data_len))
1300 return 0;
1301
1302 if (unlikely(!__tcp_can_send(ssk)))
1303 return -EAGAIN;
1304
1305 /* compute send limit */
1306 if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE))
1307 ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE;
1308 info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1309 copy = info->size_goal;
1310
1311 skb = tcp_write_queue_tail(ssk);
1312 if (skb && copy > skb->len) {
1313 /* Limit the write to the size available in the
1314 * current skb, if any, so that we create at most a new skb.
1315 * Explicitly tells TCP internals to avoid collapsing on later
1316 * queue management operation, to avoid breaking the ext <->
1317 * SSN association set here
1318 */
1319 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1320 if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1321 TCP_SKB_CB(skb)->eor = 1;
1322 goto alloc_skb;
1323 }
1324
1325 i = skb_shinfo(skb)->nr_frags;
1326 can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1327 if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
1328 tcp_mark_push(tcp_sk(ssk), skb);
1329 goto alloc_skb;
1330 }
1331
1332 copy -= skb->len;
1333 } else {
1334 alloc_skb:
1335 skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1336 if (!skb)
1337 return -ENOMEM;
1338
1339 i = skb_shinfo(skb)->nr_frags;
1340 reuse_skb = false;
1341 mpext = skb_ext_find(skb, SKB_EXT_MPTCP);
1342 }
1343
1344 /* Zero window and all data acked? Probe. */
1345 copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1346 if (copy == 0) {
1347 u64 snd_una = READ_ONCE(msk->snd_una);
1348
1349 if (snd_una != msk->snd_nxt || tcp_write_queue_tail(ssk)) {
1350 tcp_remove_empty_skb(ssk);
1351 return 0;
1352 }
1353
1354 zero_window_probe = true;
1355 data_seq = snd_una - 1;
1356 copy = 1;
1357 }
1358
1359 copy = min_t(size_t, copy, info->limit - info->sent);
1360 if (!sk_wmem_schedule(ssk, copy)) {
1361 tcp_remove_empty_skb(ssk);
1362 return -ENOMEM;
1363 }
1364
1365 if (can_coalesce) {
1366 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1367 } else {
1368 get_page(dfrag->page);
1369 skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1370 }
1371
1372 skb->len += copy;
1373 skb->data_len += copy;
1374 skb->truesize += copy;
1375 sk_wmem_queued_add(ssk, copy);
1376 sk_mem_charge(ssk, copy);
1377 WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1378 TCP_SKB_CB(skb)->end_seq += copy;
1379 tcp_skb_pcount_set(skb, 0);
1380
1381 /* on skb reuse we just need to update the DSS len */
1382 if (reuse_skb) {
1383 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1384 mpext->data_len += copy;
1385 goto out;
1386 }
1387
1388 memset(mpext, 0, sizeof(*mpext));
1389 mpext->data_seq = data_seq;
1390 mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1391 mpext->data_len = copy;
1392 mpext->use_map = 1;
1393 mpext->dsn64 = 1;
1394
1395 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d",
1396 mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1397 mpext->dsn64);
1398
1399 if (zero_window_probe) {
1400 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1401 mpext->frozen = 1;
1402 if (READ_ONCE(msk->csum_enabled))
1403 mptcp_update_data_checksum(skb, copy);
1404 tcp_push_pending_frames(ssk);
1405 return 0;
1406 }
1407 out:
1408 if (READ_ONCE(msk->csum_enabled))
1409 mptcp_update_data_checksum(skb, copy);
1410 if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1411 mptcp_update_infinite_map(msk, ssk, mpext);
1412 trace_mptcp_sendmsg_frag(mpext);
1413 mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1414 return copy;
1415 }
1416
1417 #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1418 sizeof(struct tcphdr) - \
1419 MAX_TCP_OPTION_SPACE - \
1420 sizeof(struct ipv6hdr) - \
1421 sizeof(struct frag_hdr))
1422
1423 struct subflow_send_info {
1424 struct sock *ssk;
1425 u64 linger_time;
1426 };
1427
mptcp_subflow_set_active(struct mptcp_subflow_context * subflow)1428 void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1429 {
1430 if (!subflow->stale)
1431 return;
1432
1433 subflow->stale = 0;
1434 MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1435 }
1436
mptcp_subflow_active(struct mptcp_subflow_context * subflow)1437 bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1438 {
1439 if (unlikely(subflow->stale)) {
1440 u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1441
1442 if (subflow->stale_rcv_tstamp == rcv_tstamp)
1443 return false;
1444
1445 mptcp_subflow_set_active(subflow);
1446 }
1447 return __mptcp_subflow_active(subflow);
1448 }
1449
1450 #define SSK_MODE_ACTIVE 0
1451 #define SSK_MODE_BACKUP 1
1452 #define SSK_MODE_MAX 2
1453
1454 /* implement the mptcp packet scheduler;
1455 * returns the subflow that will transmit the next DSS
1456 * additionally updates the rtx timeout
1457 */
mptcp_subflow_get_send(struct mptcp_sock * msk)1458 static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1459 {
1460 struct subflow_send_info send_info[SSK_MODE_MAX];
1461 struct mptcp_subflow_context *subflow;
1462 struct sock *sk = (struct sock *)msk;
1463 u32 pace, burst, wmem;
1464 int i, nr_active = 0;
1465 struct sock *ssk;
1466 u64 linger_time;
1467 long tout = 0;
1468
1469 sock_owned_by_me(sk);
1470
1471 if (__mptcp_check_fallback(msk)) {
1472 if (!msk->first)
1473 return NULL;
1474 return __tcp_can_send(msk->first) &&
1475 sk_stream_memory_free(msk->first) ? msk->first : NULL;
1476 }
1477
1478 /* re-use last subflow, if the burst allow that */
1479 if (msk->last_snd && msk->snd_burst > 0 &&
1480 sk_stream_memory_free(msk->last_snd) &&
1481 mptcp_subflow_active(mptcp_subflow_ctx(msk->last_snd))) {
1482 mptcp_set_timeout(sk);
1483 return msk->last_snd;
1484 }
1485
1486 /* pick the subflow with the lower wmem/wspace ratio */
1487 for (i = 0; i < SSK_MODE_MAX; ++i) {
1488 send_info[i].ssk = NULL;
1489 send_info[i].linger_time = -1;
1490 }
1491
1492 mptcp_for_each_subflow(msk, subflow) {
1493 trace_mptcp_subflow_get_send(subflow);
1494 ssk = mptcp_subflow_tcp_sock(subflow);
1495 if (!mptcp_subflow_active(subflow))
1496 continue;
1497
1498 tout = max(tout, mptcp_timeout_from_subflow(subflow));
1499 nr_active += !subflow->backup;
1500 pace = subflow->avg_pacing_rate;
1501 if (unlikely(!pace)) {
1502 /* init pacing rate from socket */
1503 subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1504 pace = subflow->avg_pacing_rate;
1505 if (!pace)
1506 continue;
1507 }
1508
1509 linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1510 if (linger_time < send_info[subflow->backup].linger_time) {
1511 send_info[subflow->backup].ssk = ssk;
1512 send_info[subflow->backup].linger_time = linger_time;
1513 }
1514 }
1515 __mptcp_set_timeout(sk, tout);
1516
1517 /* pick the best backup if no other subflow is active */
1518 if (!nr_active)
1519 send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1520
1521 /* According to the blest algorithm, to avoid HoL blocking for the
1522 * faster flow, we need to:
1523 * - estimate the faster flow linger time
1524 * - use the above to estimate the amount of byte transferred
1525 * by the faster flow
1526 * - check that the amount of queued data is greter than the above,
1527 * otherwise do not use the picked, slower, subflow
1528 * We select the subflow with the shorter estimated time to flush
1529 * the queued mem, which basically ensure the above. We just need
1530 * to check that subflow has a non empty cwin.
1531 */
1532 ssk = send_info[SSK_MODE_ACTIVE].ssk;
1533 if (!ssk || !sk_stream_memory_free(ssk))
1534 return NULL;
1535
1536 burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1537 wmem = READ_ONCE(ssk->sk_wmem_queued);
1538 if (!burst) {
1539 msk->last_snd = NULL;
1540 return ssk;
1541 }
1542
1543 subflow = mptcp_subflow_ctx(ssk);
1544 subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1545 READ_ONCE(ssk->sk_pacing_rate) * burst,
1546 burst + wmem);
1547 msk->last_snd = ssk;
1548 msk->snd_burst = burst;
1549 return ssk;
1550 }
1551
mptcp_push_release(struct sock * ssk,struct mptcp_sendmsg_info * info)1552 static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1553 {
1554 tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1555 release_sock(ssk);
1556 }
1557
mptcp_update_post_push(struct mptcp_sock * msk,struct mptcp_data_frag * dfrag,u32 sent)1558 static void mptcp_update_post_push(struct mptcp_sock *msk,
1559 struct mptcp_data_frag *dfrag,
1560 u32 sent)
1561 {
1562 u64 snd_nxt_new = dfrag->data_seq;
1563
1564 dfrag->already_sent += sent;
1565
1566 msk->snd_burst -= sent;
1567
1568 snd_nxt_new += dfrag->already_sent;
1569
1570 /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1571 * is recovering after a failover. In that event, this re-sends
1572 * old segments.
1573 *
1574 * Thus compute snd_nxt_new candidate based on
1575 * the dfrag->data_seq that was sent and the data
1576 * that has been handed to the subflow for transmission
1577 * and skip update in case it was old dfrag.
1578 */
1579 if (likely(after64(snd_nxt_new, msk->snd_nxt)))
1580 msk->snd_nxt = snd_nxt_new;
1581 }
1582
mptcp_check_and_set_pending(struct sock * sk)1583 void mptcp_check_and_set_pending(struct sock *sk)
1584 {
1585 if (mptcp_send_head(sk)) {
1586 mptcp_data_lock(sk);
1587 mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING);
1588 mptcp_data_unlock(sk);
1589 }
1590 }
1591
__mptcp_push_pending(struct sock * sk,unsigned int flags)1592 void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1593 {
1594 struct sock *prev_ssk = NULL, *ssk = NULL;
1595 struct mptcp_sock *msk = mptcp_sk(sk);
1596 struct mptcp_sendmsg_info info = {
1597 .flags = flags,
1598 };
1599 bool do_check_data_fin = false;
1600 struct mptcp_data_frag *dfrag;
1601 int len;
1602
1603 while ((dfrag = mptcp_send_head(sk))) {
1604 info.sent = dfrag->already_sent;
1605 info.limit = dfrag->data_len;
1606 len = dfrag->data_len - dfrag->already_sent;
1607 while (len > 0) {
1608 int ret = 0;
1609
1610 prev_ssk = ssk;
1611 ssk = mptcp_subflow_get_send(msk);
1612
1613 /* First check. If the ssk has changed since
1614 * the last round, release prev_ssk
1615 */
1616 if (ssk != prev_ssk && prev_ssk)
1617 mptcp_push_release(prev_ssk, &info);
1618 if (!ssk)
1619 goto out;
1620
1621 /* Need to lock the new subflow only if different
1622 * from the previous one, otherwise we are still
1623 * helding the relevant lock
1624 */
1625 if (ssk != prev_ssk)
1626 lock_sock(ssk);
1627
1628 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1629 if (ret <= 0) {
1630 if (ret == -EAGAIN)
1631 continue;
1632 mptcp_push_release(ssk, &info);
1633 goto out;
1634 }
1635
1636 do_check_data_fin = true;
1637 info.sent += ret;
1638 len -= ret;
1639
1640 mptcp_update_post_push(msk, dfrag, ret);
1641 }
1642 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1643 }
1644
1645 /* at this point we held the socket lock for the last subflow we used */
1646 if (ssk)
1647 mptcp_push_release(ssk, &info);
1648
1649 out:
1650 /* ensure the rtx timer is running */
1651 if (!mptcp_rtx_timer_pending(sk))
1652 mptcp_reset_rtx_timer(sk);
1653 if (do_check_data_fin)
1654 mptcp_check_send_data_fin(sk);
1655 }
1656
__mptcp_subflow_push_pending(struct sock * sk,struct sock * ssk)1657 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
1658 {
1659 struct mptcp_sock *msk = mptcp_sk(sk);
1660 struct mptcp_sendmsg_info info = {
1661 .data_lock_held = true,
1662 };
1663 struct mptcp_data_frag *dfrag;
1664 struct sock *xmit_ssk;
1665 int len, copied = 0;
1666 bool first = true;
1667
1668 info.flags = 0;
1669 while ((dfrag = mptcp_send_head(sk))) {
1670 info.sent = dfrag->already_sent;
1671 info.limit = dfrag->data_len;
1672 len = dfrag->data_len - dfrag->already_sent;
1673 while (len > 0) {
1674 int ret = 0;
1675
1676 /* the caller already invoked the packet scheduler,
1677 * check for a different subflow usage only after
1678 * spooling the first chunk of data
1679 */
1680 xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
1681 if (!xmit_ssk)
1682 goto out;
1683 if (xmit_ssk != ssk) {
1684 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk),
1685 MPTCP_DELEGATE_SEND);
1686 goto out;
1687 }
1688
1689 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
1690 if (ret <= 0)
1691 goto out;
1692
1693 info.sent += ret;
1694 copied += ret;
1695 len -= ret;
1696 first = false;
1697
1698 mptcp_update_post_push(msk, dfrag, ret);
1699 }
1700 WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1701 }
1702
1703 out:
1704 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1705 * not going to flush it via release_sock()
1706 */
1707 if (copied) {
1708 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1709 info.size_goal);
1710 if (!mptcp_rtx_timer_pending(sk))
1711 mptcp_reset_rtx_timer(sk);
1712
1713 if (msk->snd_data_fin_enable &&
1714 msk->snd_nxt + 1 == msk->write_seq)
1715 mptcp_schedule_work(sk);
1716 }
1717 }
1718
mptcp_set_nospace(struct sock * sk)1719 static void mptcp_set_nospace(struct sock *sk)
1720 {
1721 /* enable autotune */
1722 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1723
1724 /* will be cleared on avail space */
1725 set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
1726 }
1727
1728 static int mptcp_disconnect(struct sock *sk, int flags);
1729
mptcp_sendmsg_fastopen(struct sock * sk,struct sock * ssk,struct msghdr * msg,size_t len,int * copied_syn)1730 static int mptcp_sendmsg_fastopen(struct sock *sk, struct sock *ssk, struct msghdr *msg,
1731 size_t len, int *copied_syn)
1732 {
1733 unsigned int saved_flags = msg->msg_flags;
1734 struct mptcp_sock *msk = mptcp_sk(sk);
1735 int ret;
1736
1737 lock_sock(ssk);
1738 msg->msg_flags |= MSG_DONTWAIT;
1739 msk->fastopening = 1;
1740 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1741 msk->fastopening = 0;
1742 msg->msg_flags = saved_flags;
1743 release_sock(ssk);
1744
1745 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1746 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1747 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1748 msg->msg_namelen, msg->msg_flags, 1);
1749
1750 /* Keep the same behaviour of plain TCP: zero the copied bytes in
1751 * case of any error, except timeout or signal
1752 */
1753 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1754 *copied_syn = 0;
1755 } else if (ret && ret != -EINPROGRESS) {
1756 /* The disconnect() op called by tcp_sendmsg_fastopen()/
1757 * __inet_stream_connect() can fail, due to looking check,
1758 * see mptcp_disconnect().
1759 * Attempt it again outside the problematic scope.
1760 */
1761 if (!mptcp_disconnect(sk, 0))
1762 sk->sk_socket->state = SS_UNCONNECTED;
1763 }
1764
1765 return ret;
1766 }
1767
mptcp_sendmsg(struct sock * sk,struct msghdr * msg,size_t len)1768 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1769 {
1770 struct mptcp_sock *msk = mptcp_sk(sk);
1771 struct page_frag *pfrag;
1772 struct socket *ssock;
1773 size_t copied = 0;
1774 int ret = 0;
1775 long timeo;
1776
1777 /* we don't support FASTOPEN yet */
1778 if (msg->msg_flags & MSG_FASTOPEN)
1779 return -EOPNOTSUPP;
1780
1781 /* silently ignore everything else */
1782 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL;
1783
1784 lock_sock(sk);
1785
1786 ssock = __mptcp_nmpc_socket(msk);
1787 if (unlikely(ssock && inet_sk(ssock->sk)->defer_connect)) {
1788 int copied_syn = 0;
1789
1790 ret = mptcp_sendmsg_fastopen(sk, ssock->sk, msg, len, &copied_syn);
1791 copied += copied_syn;
1792 if (ret == -EINPROGRESS && copied_syn > 0)
1793 goto out;
1794 else if (ret)
1795 goto do_error;
1796 }
1797
1798 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1799
1800 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1801 ret = sk_stream_wait_connect(sk, &timeo);
1802 if (ret)
1803 goto do_error;
1804 }
1805
1806 ret = -EPIPE;
1807 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1808 goto do_error;
1809
1810 pfrag = sk_page_frag(sk);
1811
1812 while (msg_data_left(msg)) {
1813 int total_ts, frag_truesize = 0;
1814 struct mptcp_data_frag *dfrag;
1815 bool dfrag_collapsed;
1816 size_t psize, offset;
1817
1818 /* reuse tail pfrag, if possible, or carve a new one from the
1819 * page allocator
1820 */
1821 dfrag = mptcp_pending_tail(sk);
1822 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1823 if (!dfrag_collapsed) {
1824 if (!sk_stream_memory_free(sk))
1825 goto wait_for_memory;
1826
1827 if (!mptcp_page_frag_refill(sk, pfrag))
1828 goto wait_for_memory;
1829
1830 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1831 frag_truesize = dfrag->overhead;
1832 }
1833
1834 /* we do not bound vs wspace, to allow a single packet.
1835 * memory accounting will prevent execessive memory usage
1836 * anyway
1837 */
1838 offset = dfrag->offset + dfrag->data_len;
1839 psize = pfrag->size - offset;
1840 psize = min_t(size_t, psize, msg_data_left(msg));
1841 total_ts = psize + frag_truesize;
1842
1843 if (!sk_wmem_schedule(sk, total_ts))
1844 goto wait_for_memory;
1845
1846 if (copy_page_from_iter(dfrag->page, offset, psize,
1847 &msg->msg_iter) != psize) {
1848 ret = -EFAULT;
1849 goto do_error;
1850 }
1851
1852 /* data successfully copied into the write queue */
1853 sk_forward_alloc_add(sk, -total_ts);
1854 copied += psize;
1855 dfrag->data_len += psize;
1856 frag_truesize += psize;
1857 pfrag->offset += frag_truesize;
1858 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1859
1860 /* charge data on mptcp pending queue to the msk socket
1861 * Note: we charge such data both to sk and ssk
1862 */
1863 sk_wmem_queued_add(sk, frag_truesize);
1864 if (!dfrag_collapsed) {
1865 get_page(dfrag->page);
1866 list_add_tail(&dfrag->list, &msk->rtx_queue);
1867 if (!msk->first_pending)
1868 WRITE_ONCE(msk->first_pending, dfrag);
1869 }
1870 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d", msk,
1871 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1872 !dfrag_collapsed);
1873
1874 continue;
1875
1876 wait_for_memory:
1877 mptcp_set_nospace(sk);
1878 __mptcp_push_pending(sk, msg->msg_flags);
1879 ret = sk_stream_wait_memory(sk, &timeo);
1880 if (ret)
1881 goto do_error;
1882 }
1883
1884 if (copied)
1885 __mptcp_push_pending(sk, msg->msg_flags);
1886
1887 out:
1888 release_sock(sk);
1889 return copied;
1890
1891 do_error:
1892 if (copied)
1893 goto out;
1894
1895 copied = sk_stream_error(sk, msg->msg_flags, ret);
1896 goto out;
1897 }
1898
__mptcp_recvmsg_mskq(struct mptcp_sock * msk,struct msghdr * msg,size_t len,int flags,struct scm_timestamping_internal * tss,int * cmsg_flags)1899 static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
1900 struct msghdr *msg,
1901 size_t len, int flags,
1902 struct scm_timestamping_internal *tss,
1903 int *cmsg_flags)
1904 {
1905 struct sk_buff *skb, *tmp;
1906 int copied = 0;
1907
1908 skb_queue_walk_safe(&msk->receive_queue, skb, tmp) {
1909 u32 offset = MPTCP_SKB_CB(skb)->offset;
1910 u32 data_len = skb->len - offset;
1911 u32 count = min_t(size_t, len - copied, data_len);
1912 int err;
1913
1914 if (!(flags & MSG_TRUNC)) {
1915 err = skb_copy_datagram_msg(skb, offset, msg, count);
1916 if (unlikely(err < 0)) {
1917 if (!copied)
1918 return err;
1919 break;
1920 }
1921 }
1922
1923 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1924 tcp_update_recv_tstamps(skb, tss);
1925 *cmsg_flags |= MPTCP_CMSG_TS;
1926 }
1927
1928 copied += count;
1929
1930 if (count < data_len) {
1931 if (!(flags & MSG_PEEK)) {
1932 MPTCP_SKB_CB(skb)->offset += count;
1933 MPTCP_SKB_CB(skb)->map_seq += count;
1934 }
1935 break;
1936 }
1937
1938 if (!(flags & MSG_PEEK)) {
1939 /* we will bulk release the skb memory later */
1940 skb->destructor = NULL;
1941 WRITE_ONCE(msk->rmem_released, msk->rmem_released + skb->truesize);
1942 __skb_unlink(skb, &msk->receive_queue);
1943 __kfree_skb(skb);
1944 }
1945
1946 if (copied >= len)
1947 break;
1948 }
1949
1950 return copied;
1951 }
1952
1953 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1954 *
1955 * Only difference: Use highest rtt estimate of the subflows in use.
1956 */
mptcp_rcv_space_adjust(struct mptcp_sock * msk,int copied)1957 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1958 {
1959 struct mptcp_subflow_context *subflow;
1960 struct sock *sk = (struct sock *)msk;
1961 u32 time, advmss = 1;
1962 u64 rtt_us, mstamp;
1963
1964 sock_owned_by_me(sk);
1965
1966 if (copied <= 0)
1967 return;
1968
1969 msk->rcvq_space.copied += copied;
1970
1971 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1972 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1973
1974 rtt_us = msk->rcvq_space.rtt_us;
1975 if (rtt_us && time < (rtt_us >> 3))
1976 return;
1977
1978 rtt_us = 0;
1979 mptcp_for_each_subflow(msk, subflow) {
1980 const struct tcp_sock *tp;
1981 u64 sf_rtt_us;
1982 u32 sf_advmss;
1983
1984 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1985
1986 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1987 sf_advmss = READ_ONCE(tp->advmss);
1988
1989 rtt_us = max(sf_rtt_us, rtt_us);
1990 advmss = max(sf_advmss, advmss);
1991 }
1992
1993 msk->rcvq_space.rtt_us = rtt_us;
1994 if (time < (rtt_us >> 3) || rtt_us == 0)
1995 return;
1996
1997 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1998 goto new_measure;
1999
2000 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
2001 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
2002 int rcvmem, rcvbuf;
2003 u64 rcvwin, grow;
2004
2005 rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
2006
2007 grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
2008
2009 do_div(grow, msk->rcvq_space.space);
2010 rcvwin += (grow << 1);
2011
2012 rcvmem = SKB_TRUESIZE(advmss + MAX_TCP_HEADER);
2013 while (tcp_win_from_space(sk, rcvmem) < advmss)
2014 rcvmem += 128;
2015
2016 do_div(rcvwin, advmss);
2017 rcvbuf = min_t(u64, rcvwin * rcvmem,
2018 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
2019
2020 if (rcvbuf > sk->sk_rcvbuf) {
2021 u32 window_clamp;
2022
2023 window_clamp = tcp_win_from_space(sk, rcvbuf);
2024 WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
2025
2026 /* Make subflows follow along. If we do not do this, we
2027 * get drops at subflow level if skbs can't be moved to
2028 * the mptcp rx queue fast enough (announced rcv_win can
2029 * exceed ssk->sk_rcvbuf).
2030 */
2031 mptcp_for_each_subflow(msk, subflow) {
2032 struct sock *ssk;
2033 bool slow;
2034
2035 ssk = mptcp_subflow_tcp_sock(subflow);
2036 slow = lock_sock_fast(ssk);
2037 WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
2038 tcp_sk(ssk)->window_clamp = window_clamp;
2039 tcp_cleanup_rbuf(ssk, 1);
2040 unlock_sock_fast(ssk, slow);
2041 }
2042 }
2043 }
2044
2045 msk->rcvq_space.space = msk->rcvq_space.copied;
2046 new_measure:
2047 msk->rcvq_space.copied = 0;
2048 msk->rcvq_space.time = mstamp;
2049 }
2050
__mptcp_update_rmem(struct sock * sk)2051 static void __mptcp_update_rmem(struct sock *sk)
2052 {
2053 struct mptcp_sock *msk = mptcp_sk(sk);
2054
2055 if (!msk->rmem_released)
2056 return;
2057
2058 atomic_sub(msk->rmem_released, &sk->sk_rmem_alloc);
2059 mptcp_rmem_uncharge(sk, msk->rmem_released);
2060 WRITE_ONCE(msk->rmem_released, 0);
2061 }
2062
__mptcp_splice_receive_queue(struct sock * sk)2063 static void __mptcp_splice_receive_queue(struct sock *sk)
2064 {
2065 struct mptcp_sock *msk = mptcp_sk(sk);
2066
2067 skb_queue_splice_tail_init(&sk->sk_receive_queue, &msk->receive_queue);
2068 }
2069
__mptcp_move_skbs(struct mptcp_sock * msk)2070 static bool __mptcp_move_skbs(struct mptcp_sock *msk)
2071 {
2072 struct sock *sk = (struct sock *)msk;
2073 unsigned int moved = 0;
2074 bool ret, done;
2075
2076 do {
2077 struct sock *ssk = mptcp_subflow_recv_lookup(msk);
2078 bool slowpath;
2079
2080 /* we can have data pending in the subflows only if the msk
2081 * receive buffer was full at subflow_data_ready() time,
2082 * that is an unlikely slow path.
2083 */
2084 if (likely(!ssk))
2085 break;
2086
2087 slowpath = lock_sock_fast(ssk);
2088 mptcp_data_lock(sk);
2089 __mptcp_update_rmem(sk);
2090 done = __mptcp_move_skbs_from_subflow(msk, ssk, &moved);
2091 mptcp_data_unlock(sk);
2092
2093 if (unlikely(ssk->sk_err))
2094 __mptcp_error_report(sk);
2095 unlock_sock_fast(ssk, slowpath);
2096 } while (!done);
2097
2098 /* acquire the data lock only if some input data is pending */
2099 ret = moved > 0;
2100 if (!RB_EMPTY_ROOT(&msk->out_of_order_queue) ||
2101 !skb_queue_empty_lockless(&sk->sk_receive_queue)) {
2102 mptcp_data_lock(sk);
2103 __mptcp_update_rmem(sk);
2104 ret |= __mptcp_ofo_queue(msk);
2105 __mptcp_splice_receive_queue(sk);
2106 mptcp_data_unlock(sk);
2107 }
2108 if (ret)
2109 mptcp_check_data_fin((struct sock *)msk);
2110 return !skb_queue_empty(&msk->receive_queue);
2111 }
2112
mptcp_inq_hint(const struct sock * sk)2113 static unsigned int mptcp_inq_hint(const struct sock *sk)
2114 {
2115 const struct mptcp_sock *msk = mptcp_sk(sk);
2116 const struct sk_buff *skb;
2117
2118 skb = skb_peek(&msk->receive_queue);
2119 if (skb) {
2120 u64 hint_val = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
2121
2122 if (hint_val >= INT_MAX)
2123 return INT_MAX;
2124
2125 return (unsigned int)hint_val;
2126 }
2127
2128 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2129 return 1;
2130
2131 return 0;
2132 }
2133
mptcp_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int flags,int * addr_len)2134 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2135 int flags, int *addr_len)
2136 {
2137 struct mptcp_sock *msk = mptcp_sk(sk);
2138 struct scm_timestamping_internal tss;
2139 int copied = 0, cmsg_flags = 0;
2140 int target;
2141 long timeo;
2142
2143 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2144 if (unlikely(flags & MSG_ERRQUEUE))
2145 return inet_recv_error(sk, msg, len, addr_len);
2146
2147 lock_sock(sk);
2148 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2149 copied = -ENOTCONN;
2150 goto out_err;
2151 }
2152
2153 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2154
2155 len = min_t(size_t, len, INT_MAX);
2156 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2157
2158 if (unlikely(msk->recvmsg_inq))
2159 cmsg_flags = MPTCP_CMSG_INQ;
2160
2161 while (copied < len) {
2162 int bytes_read;
2163
2164 bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied, flags, &tss, &cmsg_flags);
2165 if (unlikely(bytes_read < 0)) {
2166 if (!copied)
2167 copied = bytes_read;
2168 goto out_err;
2169 }
2170
2171 copied += bytes_read;
2172
2173 /* be sure to advertise window change */
2174 mptcp_cleanup_rbuf(msk);
2175
2176 if (skb_queue_empty(&msk->receive_queue) && __mptcp_move_skbs(msk))
2177 continue;
2178
2179 /* only the master socket status is relevant here. The exit
2180 * conditions mirror closely tcp_recvmsg()
2181 */
2182 if (copied >= target)
2183 break;
2184
2185 if (copied) {
2186 if (sk->sk_err ||
2187 sk->sk_state == TCP_CLOSE ||
2188 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2189 !timeo ||
2190 signal_pending(current))
2191 break;
2192 } else {
2193 if (sk->sk_err) {
2194 copied = sock_error(sk);
2195 break;
2196 }
2197
2198 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2199 mptcp_check_for_eof(msk);
2200
2201 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2202 /* race breaker: the shutdown could be after the
2203 * previous receive queue check
2204 */
2205 if (__mptcp_move_skbs(msk))
2206 continue;
2207 break;
2208 }
2209
2210 if (sk->sk_state == TCP_CLOSE) {
2211 copied = -ENOTCONN;
2212 break;
2213 }
2214
2215 if (!timeo) {
2216 copied = -EAGAIN;
2217 break;
2218 }
2219
2220 if (signal_pending(current)) {
2221 copied = sock_intr_errno(timeo);
2222 break;
2223 }
2224 }
2225
2226 pr_debug("block timeout %ld", timeo);
2227 sk_wait_data(sk, &timeo, NULL);
2228 }
2229
2230 out_err:
2231 if (cmsg_flags && copied >= 0) {
2232 if (cmsg_flags & MPTCP_CMSG_TS)
2233 tcp_recv_timestamp(msg, sk, &tss);
2234
2235 if (cmsg_flags & MPTCP_CMSG_INQ) {
2236 unsigned int inq = mptcp_inq_hint(sk);
2237
2238 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2239 }
2240 }
2241
2242 pr_debug("msk=%p rx queue empty=%d:%d copied=%d",
2243 msk, skb_queue_empty_lockless(&sk->sk_receive_queue),
2244 skb_queue_empty(&msk->receive_queue), copied);
2245 if (!(flags & MSG_PEEK))
2246 mptcp_rcv_space_adjust(msk, copied);
2247
2248 release_sock(sk);
2249 return copied;
2250 }
2251
mptcp_retransmit_timer(struct timer_list * t)2252 static void mptcp_retransmit_timer(struct timer_list *t)
2253 {
2254 struct inet_connection_sock *icsk = from_timer(icsk, t,
2255 icsk_retransmit_timer);
2256 struct sock *sk = &icsk->icsk_inet.sk;
2257 struct mptcp_sock *msk = mptcp_sk(sk);
2258
2259 bh_lock_sock(sk);
2260 if (!sock_owned_by_user(sk)) {
2261 /* we need a process context to retransmit */
2262 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2263 mptcp_schedule_work(sk);
2264 } else {
2265 /* delegate our work to tcp_release_cb() */
2266 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2267 }
2268 bh_unlock_sock(sk);
2269 sock_put(sk);
2270 }
2271
mptcp_tout_timer(struct timer_list * t)2272 static void mptcp_tout_timer(struct timer_list *t)
2273 {
2274 struct sock *sk = from_timer(sk, t, sk_timer);
2275
2276 mptcp_schedule_work(sk);
2277 sock_put(sk);
2278 }
2279
2280 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2281 * level.
2282 *
2283 * A backup subflow is returned only if that is the only kind available.
2284 */
mptcp_subflow_get_retrans(struct mptcp_sock * msk)2285 static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2286 {
2287 struct sock *backup = NULL, *pick = NULL;
2288 struct mptcp_subflow_context *subflow;
2289 int min_stale_count = INT_MAX;
2290
2291 sock_owned_by_me((const struct sock *)msk);
2292
2293 if (__mptcp_check_fallback(msk))
2294 return NULL;
2295
2296 mptcp_for_each_subflow(msk, subflow) {
2297 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2298
2299 if (!__mptcp_subflow_active(subflow))
2300 continue;
2301
2302 /* still data outstanding at TCP level? skip this */
2303 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2304 mptcp_pm_subflow_chk_stale(msk, ssk);
2305 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2306 continue;
2307 }
2308
2309 if (subflow->backup) {
2310 if (!backup)
2311 backup = ssk;
2312 continue;
2313 }
2314
2315 if (!pick)
2316 pick = ssk;
2317 }
2318
2319 if (pick)
2320 return pick;
2321
2322 /* use backup only if there are no progresses anywhere */
2323 return min_stale_count > 1 ? backup : NULL;
2324 }
2325
mptcp_dispose_initial_subflow(struct mptcp_sock * msk)2326 static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
2327 {
2328 if (msk->subflow) {
2329 iput(SOCK_INODE(msk->subflow));
2330 WRITE_ONCE(msk->subflow, NULL);
2331 }
2332 }
2333
__mptcp_retransmit_pending_data(struct sock * sk)2334 bool __mptcp_retransmit_pending_data(struct sock *sk)
2335 {
2336 struct mptcp_data_frag *cur, *rtx_head;
2337 struct mptcp_sock *msk = mptcp_sk(sk);
2338
2339 if (__mptcp_check_fallback(mptcp_sk(sk)))
2340 return false;
2341
2342 /* the closing socket has some data untransmitted and/or unacked:
2343 * some data in the mptcp rtx queue has not really xmitted yet.
2344 * keep it simple and re-inject the whole mptcp level rtx queue
2345 */
2346 mptcp_data_lock(sk);
2347 __mptcp_clean_una_wakeup(sk);
2348 rtx_head = mptcp_rtx_head(sk);
2349 if (!rtx_head) {
2350 mptcp_data_unlock(sk);
2351 return false;
2352 }
2353
2354 msk->recovery_snd_nxt = msk->snd_nxt;
2355 msk->recovery = true;
2356 mptcp_data_unlock(sk);
2357
2358 msk->first_pending = rtx_head;
2359 msk->snd_burst = 0;
2360
2361 /* be sure to clear the "sent status" on all re-injected fragments */
2362 list_for_each_entry(cur, &msk->rtx_queue, list) {
2363 if (!cur->already_sent)
2364 break;
2365 cur->already_sent = 0;
2366 }
2367
2368 return true;
2369 }
2370
2371 /* flags for __mptcp_close_ssk() */
2372 #define MPTCP_CF_PUSH BIT(1)
2373 #define MPTCP_CF_FASTCLOSE BIT(2)
2374
2375 /* be sure to send a reset only if the caller asked for it, also
2376 * clean completely the subflow status when the subflow reaches
2377 * TCP_CLOSE state
2378 */
__mptcp_subflow_disconnect(struct sock * ssk,struct mptcp_subflow_context * subflow,unsigned int flags)2379 static void __mptcp_subflow_disconnect(struct sock *ssk,
2380 struct mptcp_subflow_context *subflow,
2381 unsigned int flags)
2382 {
2383 if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
2384 (flags & MPTCP_CF_FASTCLOSE)) {
2385 /* The MPTCP code never wait on the subflow sockets, TCP-level
2386 * disconnect should never fail
2387 */
2388 WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2389 mptcp_subflow_ctx_reset(subflow);
2390 } else {
2391 tcp_shutdown(ssk, SEND_SHUTDOWN);
2392 }
2393 }
2394
2395 /* subflow sockets can be either outgoing (connect) or incoming
2396 * (accept).
2397 *
2398 * Outgoing subflows use in-kernel sockets.
2399 * Incoming subflows do not have their own 'struct socket' allocated,
2400 * so we need to use tcp_close() after detaching them from the mptcp
2401 * parent socket.
2402 */
__mptcp_close_ssk(struct sock * sk,struct sock * ssk,struct mptcp_subflow_context * subflow,unsigned int flags)2403 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2404 struct mptcp_subflow_context *subflow,
2405 unsigned int flags)
2406 {
2407 struct mptcp_sock *msk = mptcp_sk(sk);
2408 bool dispose_it, need_push = false;
2409
2410 /* If the first subflow moved to a close state before accept, e.g. due
2411 * to an incoming reset or listener shutdown, the subflow socket is
2412 * already deleted by inet_child_forget() and the mptcp socket can't
2413 * survive too.
2414 */
2415 if (msk->in_accept_queue && msk->first == ssk &&
2416 (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) {
2417 /* ensure later check in mptcp_worker() will dispose the msk */
2418 mptcp_set_close_tout(sk, tcp_jiffies32 - (TCP_TIMEWAIT_LEN + 1));
2419 sock_set_flag(sk, SOCK_DEAD);
2420 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2421 mptcp_subflow_drop_ctx(ssk);
2422 goto out_release;
2423 }
2424
2425 dispose_it = msk->free_first || ssk != msk->first;
2426 if (dispose_it)
2427 list_del(&subflow->node);
2428
2429 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2430
2431 if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) {
2432 /* be sure to force the tcp_close path
2433 * to generate the egress reset
2434 */
2435 ssk->sk_lingertime = 0;
2436 sock_set_flag(ssk, SOCK_LINGER);
2437 subflow->send_fastclose = 1;
2438 }
2439
2440 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2441 if (!dispose_it) {
2442 __mptcp_subflow_disconnect(ssk, subflow, flags);
2443 release_sock(ssk);
2444
2445 goto out;
2446 }
2447
2448 subflow->disposable = 1;
2449
2450 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2451 * the ssk has been already destroyed, we just need to release the
2452 * reference owned by msk;
2453 */
2454 if (!inet_csk(ssk)->icsk_ulp_ops) {
2455 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2456 kfree_rcu(subflow, rcu);
2457 } else {
2458 /* otherwise tcp will dispose of the ssk and subflow ctx */
2459 __tcp_close(ssk, 0);
2460
2461 /* close acquired an extra ref */
2462 __sock_put(ssk);
2463 }
2464
2465 out_release:
2466 __mptcp_subflow_error_report(sk, ssk);
2467 release_sock(ssk);
2468
2469 sock_put(ssk);
2470
2471 if (ssk == msk->first)
2472 WRITE_ONCE(msk->first, NULL);
2473
2474 out:
2475 if (ssk == msk->last_snd)
2476 msk->last_snd = NULL;
2477
2478 if (need_push)
2479 __mptcp_push_pending(sk, 0);
2480
2481 /* Catch every 'all subflows closed' scenario, including peers silently
2482 * closing them, e.g. due to timeout.
2483 * For established sockets, allow an additional timeout before closing,
2484 * as the protocol can still create more subflows.
2485 */
2486 if (list_is_singular(&msk->conn_list) && msk->first &&
2487 inet_sk_state_load(msk->first) == TCP_CLOSE) {
2488 if (sk->sk_state != TCP_ESTABLISHED ||
2489 msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
2490 inet_sk_state_store(sk, TCP_CLOSE);
2491 mptcp_close_wake_up(sk);
2492 } else {
2493 mptcp_start_tout_timer(sk);
2494 }
2495 }
2496 }
2497
mptcp_close_ssk(struct sock * sk,struct sock * ssk,struct mptcp_subflow_context * subflow)2498 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2499 struct mptcp_subflow_context *subflow)
2500 {
2501 if (sk->sk_state == TCP_ESTABLISHED)
2502 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2503
2504 /* subflow aborted before reaching the fully_established status
2505 * attempt the creation of the next subflow
2506 */
2507 mptcp_pm_subflow_check_next(mptcp_sk(sk), ssk, subflow);
2508
2509 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2510 }
2511
mptcp_sync_mss(struct sock * sk,u32 pmtu)2512 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2513 {
2514 return 0;
2515 }
2516
__mptcp_close_subflow(struct sock * sk)2517 static void __mptcp_close_subflow(struct sock *sk)
2518 {
2519 struct mptcp_subflow_context *subflow, *tmp;
2520 struct mptcp_sock *msk = mptcp_sk(sk);
2521
2522 might_sleep();
2523
2524 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2525 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2526
2527 if (inet_sk_state_load(ssk) != TCP_CLOSE)
2528 continue;
2529
2530 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2531 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2532 continue;
2533
2534 mptcp_close_ssk(sk, ssk, subflow);
2535 }
2536
2537 }
2538
mptcp_close_tout_expired(const struct sock * sk)2539 static bool mptcp_close_tout_expired(const struct sock *sk)
2540 {
2541 if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
2542 sk->sk_state == TCP_CLOSE)
2543 return false;
2544
2545 return time_after32(tcp_jiffies32,
2546 inet_csk(sk)->icsk_mtup.probe_timestamp + TCP_TIMEWAIT_LEN);
2547 }
2548
mptcp_check_fastclose(struct mptcp_sock * msk)2549 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2550 {
2551 struct mptcp_subflow_context *subflow, *tmp;
2552 struct sock *sk = &msk->sk.icsk_inet.sk;
2553
2554 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2555 return;
2556
2557 mptcp_token_destroy(msk);
2558
2559 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2560 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2561 bool slow;
2562
2563 slow = lock_sock_fast(tcp_sk);
2564 if (tcp_sk->sk_state != TCP_CLOSE) {
2565 tcp_send_active_reset(tcp_sk, GFP_ATOMIC);
2566 tcp_set_state(tcp_sk, TCP_CLOSE);
2567 }
2568 unlock_sock_fast(tcp_sk, slow);
2569 }
2570
2571 /* Mirror the tcp_reset() error propagation */
2572 switch (sk->sk_state) {
2573 case TCP_SYN_SENT:
2574 WRITE_ONCE(sk->sk_err, ECONNREFUSED);
2575 break;
2576 case TCP_CLOSE_WAIT:
2577 WRITE_ONCE(sk->sk_err, EPIPE);
2578 break;
2579 case TCP_CLOSE:
2580 return;
2581 default:
2582 WRITE_ONCE(sk->sk_err, ECONNRESET);
2583 }
2584
2585 inet_sk_state_store(sk, TCP_CLOSE);
2586 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2587 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2588 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2589
2590 /* the calling mptcp_worker will properly destroy the socket */
2591 if (sock_flag(sk, SOCK_DEAD))
2592 return;
2593
2594 sk->sk_state_change(sk);
2595 sk_error_report(sk);
2596 }
2597
__mptcp_retrans(struct sock * sk)2598 static void __mptcp_retrans(struct sock *sk)
2599 {
2600 struct mptcp_sock *msk = mptcp_sk(sk);
2601 struct mptcp_sendmsg_info info = {};
2602 struct mptcp_data_frag *dfrag;
2603 size_t copied = 0;
2604 struct sock *ssk;
2605 int ret;
2606
2607 mptcp_clean_una_wakeup(sk);
2608
2609 /* first check ssk: need to kick "stale" logic */
2610 ssk = mptcp_subflow_get_retrans(msk);
2611 dfrag = mptcp_rtx_head(sk);
2612 if (!dfrag) {
2613 if (mptcp_data_fin_enabled(msk)) {
2614 struct inet_connection_sock *icsk = inet_csk(sk);
2615
2616 icsk->icsk_retransmits++;
2617 mptcp_set_datafin_timeout(sk);
2618 mptcp_send_ack(msk);
2619
2620 goto reset_timer;
2621 }
2622
2623 if (!mptcp_send_head(sk))
2624 return;
2625
2626 goto reset_timer;
2627 }
2628
2629 if (!ssk)
2630 goto reset_timer;
2631
2632 lock_sock(ssk);
2633
2634 /* limit retransmission to the bytes already sent on some subflows */
2635 info.sent = 0;
2636 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len : dfrag->already_sent;
2637 while (info.sent < info.limit) {
2638 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2639 if (ret <= 0)
2640 break;
2641
2642 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2643 copied += ret;
2644 info.sent += ret;
2645 }
2646 if (copied) {
2647 dfrag->already_sent = max(dfrag->already_sent, info.sent);
2648 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2649 info.size_goal);
2650 WRITE_ONCE(msk->allow_infinite_fallback, false);
2651 }
2652
2653 release_sock(ssk);
2654
2655 reset_timer:
2656 mptcp_check_and_set_pending(sk);
2657
2658 if (!mptcp_rtx_timer_pending(sk))
2659 mptcp_reset_rtx_timer(sk);
2660 }
2661
2662 /* schedule the timeout timer for the relevant event: either close timeout
2663 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2664 */
mptcp_reset_tout_timer(struct mptcp_sock * msk,unsigned long fail_tout)2665 void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
2666 {
2667 struct sock *sk = (struct sock *)msk;
2668 unsigned long timeout, close_timeout;
2669
2670 if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
2671 return;
2672
2673 close_timeout = inet_csk(sk)->icsk_mtup.probe_timestamp - tcp_jiffies32 + jiffies +
2674 TCP_TIMEWAIT_LEN;
2675
2676 /* the close timeout takes precedence on the fail one, and here at least one of
2677 * them is active
2678 */
2679 timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
2680
2681 sk_reset_timer(sk, &sk->sk_timer, timeout);
2682 }
2683
mptcp_mp_fail_no_response(struct mptcp_sock * msk)2684 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2685 {
2686 struct sock *ssk = msk->first;
2687 bool slow;
2688
2689 if (!ssk)
2690 return;
2691
2692 pr_debug("MP_FAIL doesn't respond, reset the subflow");
2693
2694 slow = lock_sock_fast(ssk);
2695 mptcp_subflow_reset(ssk);
2696 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2697 unlock_sock_fast(ssk, slow);
2698 }
2699
mptcp_do_fastclose(struct sock * sk)2700 static void mptcp_do_fastclose(struct sock *sk)
2701 {
2702 struct mptcp_subflow_context *subflow, *tmp;
2703 struct mptcp_sock *msk = mptcp_sk(sk);
2704
2705 mptcp_for_each_subflow_safe(msk, subflow, tmp)
2706 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2707 subflow, MPTCP_CF_FASTCLOSE);
2708 }
2709
mptcp_worker(struct work_struct * work)2710 static void mptcp_worker(struct work_struct *work)
2711 {
2712 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2713 struct sock *sk = &msk->sk.icsk_inet.sk;
2714 unsigned long fail_tout;
2715 int state;
2716
2717 lock_sock(sk);
2718 state = sk->sk_state;
2719 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2720 goto unlock;
2721
2722 mptcp_check_fastclose(msk);
2723
2724 mptcp_pm_nl_work(msk);
2725
2726 if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
2727 mptcp_check_for_eof(msk);
2728
2729 mptcp_check_send_data_fin(sk);
2730 mptcp_check_data_fin_ack(sk);
2731 mptcp_check_data_fin(sk);
2732
2733 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2734 __mptcp_close_subflow(sk);
2735
2736 if (mptcp_close_tout_expired(sk)) {
2737 inet_sk_state_store(sk, TCP_CLOSE);
2738 mptcp_do_fastclose(sk);
2739 mptcp_close_wake_up(sk);
2740 }
2741
2742 if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
2743 __mptcp_destroy_sock(sk);
2744 goto unlock;
2745 }
2746
2747 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2748 __mptcp_retrans(sk);
2749
2750 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2751 if (fail_tout && time_after(jiffies, fail_tout))
2752 mptcp_mp_fail_no_response(msk);
2753
2754 unlock:
2755 release_sock(sk);
2756 sock_put(sk);
2757 }
2758
__mptcp_init_sock(struct sock * sk)2759 static int __mptcp_init_sock(struct sock *sk)
2760 {
2761 struct mptcp_sock *msk = mptcp_sk(sk);
2762
2763 INIT_LIST_HEAD(&msk->conn_list);
2764 INIT_LIST_HEAD(&msk->join_list);
2765 INIT_LIST_HEAD(&msk->rtx_queue);
2766 INIT_WORK(&msk->work, mptcp_worker);
2767 __skb_queue_head_init(&msk->receive_queue);
2768 msk->out_of_order_queue = RB_ROOT;
2769 msk->first_pending = NULL;
2770 msk->rmem_fwd_alloc = 0;
2771 WRITE_ONCE(msk->rmem_released, 0);
2772 msk->timer_ival = TCP_RTO_MIN;
2773
2774 WRITE_ONCE(msk->first, NULL);
2775 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2776 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2777 WRITE_ONCE(msk->allow_infinite_fallback, true);
2778 msk->recovery = false;
2779
2780 mptcp_pm_data_init(msk);
2781
2782 /* re-use the csk retrans timer for MPTCP-level retrans */
2783 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2784 timer_setup(&sk->sk_timer, mptcp_tout_timer, 0);
2785
2786 return 0;
2787 }
2788
mptcp_ca_reset(struct sock * sk)2789 static void mptcp_ca_reset(struct sock *sk)
2790 {
2791 struct inet_connection_sock *icsk = inet_csk(sk);
2792
2793 tcp_assign_congestion_control(sk);
2794 strcpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name);
2795
2796 /* no need to keep a reference to the ops, the name will suffice */
2797 tcp_cleanup_congestion_control(sk);
2798 icsk->icsk_ca_ops = NULL;
2799 }
2800
mptcp_init_sock(struct sock * sk)2801 static int mptcp_init_sock(struct sock *sk)
2802 {
2803 struct net *net = sock_net(sk);
2804 int ret;
2805
2806 ret = __mptcp_init_sock(sk);
2807 if (ret)
2808 return ret;
2809
2810 if (!mptcp_is_enabled(net))
2811 return -ENOPROTOOPT;
2812
2813 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2814 return -ENOMEM;
2815
2816 ret = __mptcp_socket_create(mptcp_sk(sk));
2817 if (ret)
2818 return ret;
2819
2820 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2821 * propagate the correct value
2822 */
2823 mptcp_ca_reset(sk);
2824
2825 sk_sockets_allocated_inc(sk);
2826 sk->sk_rcvbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[1]);
2827 sk->sk_sndbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[1]);
2828
2829 return 0;
2830 }
2831
__mptcp_clear_xmit(struct sock * sk)2832 static void __mptcp_clear_xmit(struct sock *sk)
2833 {
2834 struct mptcp_sock *msk = mptcp_sk(sk);
2835 struct mptcp_data_frag *dtmp, *dfrag;
2836
2837 WRITE_ONCE(msk->first_pending, NULL);
2838 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2839 dfrag_clear(sk, dfrag);
2840 }
2841
mptcp_cancel_work(struct sock * sk)2842 void mptcp_cancel_work(struct sock *sk)
2843 {
2844 struct mptcp_sock *msk = mptcp_sk(sk);
2845
2846 if (cancel_work_sync(&msk->work))
2847 __sock_put(sk);
2848 }
2849
mptcp_subflow_shutdown(struct sock * sk,struct sock * ssk,int how)2850 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2851 {
2852 lock_sock(ssk);
2853
2854 switch (ssk->sk_state) {
2855 case TCP_LISTEN:
2856 if (!(how & RCV_SHUTDOWN))
2857 break;
2858 fallthrough;
2859 case TCP_SYN_SENT:
2860 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2861 break;
2862 default:
2863 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2864 pr_debug("Fallback");
2865 ssk->sk_shutdown |= how;
2866 tcp_shutdown(ssk, how);
2867
2868 /* simulate the data_fin ack reception to let the state
2869 * machine move forward
2870 */
2871 WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2872 mptcp_schedule_work(sk);
2873 } else {
2874 pr_debug("Sending DATA_FIN on subflow %p", ssk);
2875 tcp_send_ack(ssk);
2876 if (!mptcp_rtx_timer_pending(sk))
2877 mptcp_reset_rtx_timer(sk);
2878 }
2879 break;
2880 }
2881
2882 release_sock(ssk);
2883 }
2884
2885 static const unsigned char new_state[16] = {
2886 /* current state: new state: action: */
2887 [0 /* (Invalid) */] = TCP_CLOSE,
2888 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2889 [TCP_SYN_SENT] = TCP_CLOSE,
2890 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2891 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2892 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2893 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2894 [TCP_CLOSE] = TCP_CLOSE,
2895 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2896 [TCP_LAST_ACK] = TCP_LAST_ACK,
2897 [TCP_LISTEN] = TCP_CLOSE,
2898 [TCP_CLOSING] = TCP_CLOSING,
2899 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2900 };
2901
mptcp_close_state(struct sock * sk)2902 static int mptcp_close_state(struct sock *sk)
2903 {
2904 int next = (int)new_state[sk->sk_state];
2905 int ns = next & TCP_STATE_MASK;
2906
2907 inet_sk_state_store(sk, ns);
2908
2909 return next & TCP_ACTION_FIN;
2910 }
2911
mptcp_check_send_data_fin(struct sock * sk)2912 static void mptcp_check_send_data_fin(struct sock *sk)
2913 {
2914 struct mptcp_subflow_context *subflow;
2915 struct mptcp_sock *msk = mptcp_sk(sk);
2916
2917 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu",
2918 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2919 msk->snd_nxt, msk->write_seq);
2920
2921 /* we still need to enqueue subflows or not really shutting down,
2922 * skip this
2923 */
2924 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
2925 mptcp_send_head(sk))
2926 return;
2927
2928 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2929
2930 mptcp_for_each_subflow(msk, subflow) {
2931 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2932
2933 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2934 }
2935 }
2936
__mptcp_wr_shutdown(struct sock * sk)2937 static void __mptcp_wr_shutdown(struct sock *sk)
2938 {
2939 struct mptcp_sock *msk = mptcp_sk(sk);
2940
2941 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d",
2942 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2943 !!mptcp_send_head(sk));
2944
2945 /* will be ignored by fallback sockets */
2946 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2947 WRITE_ONCE(msk->snd_data_fin_enable, 1);
2948
2949 mptcp_check_send_data_fin(sk);
2950 }
2951
__mptcp_destroy_sock(struct sock * sk)2952 static void __mptcp_destroy_sock(struct sock *sk)
2953 {
2954 struct mptcp_sock *msk = mptcp_sk(sk);
2955
2956 pr_debug("msk=%p", msk);
2957
2958 might_sleep();
2959
2960 mptcp_stop_rtx_timer(sk);
2961 sk_stop_timer(sk, &sk->sk_timer);
2962 msk->pm.status = 0;
2963
2964 sk->sk_prot->destroy(sk);
2965
2966 WARN_ON_ONCE(msk->rmem_fwd_alloc);
2967 WARN_ON_ONCE(msk->rmem_released);
2968 sk_stream_kill_queues(sk);
2969 xfrm_sk_free_policy(sk);
2970
2971 sk_refcnt_debug_release(sk);
2972 sock_put(sk);
2973 }
2974
__mptcp_unaccepted_force_close(struct sock * sk)2975 void __mptcp_unaccepted_force_close(struct sock *sk)
2976 {
2977 sock_set_flag(sk, SOCK_DEAD);
2978 inet_sk_state_store(sk, TCP_CLOSE);
2979 mptcp_do_fastclose(sk);
2980 __mptcp_destroy_sock(sk);
2981 }
2982
mptcp_check_readable(struct mptcp_sock * msk)2983 static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
2984 {
2985 /* Concurrent splices from sk_receive_queue into receive_queue will
2986 * always show at least one non-empty queue when checked in this order.
2987 */
2988 if (skb_queue_empty_lockless(&((struct sock *)msk)->sk_receive_queue) &&
2989 skb_queue_empty_lockless(&msk->receive_queue))
2990 return 0;
2991
2992 return EPOLLIN | EPOLLRDNORM;
2993 }
2994
mptcp_check_listen_stop(struct sock * sk)2995 static void mptcp_check_listen_stop(struct sock *sk)
2996 {
2997 struct sock *ssk;
2998
2999 if (inet_sk_state_load(sk) != TCP_LISTEN)
3000 return;
3001
3002 ssk = mptcp_sk(sk)->first;
3003 if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
3004 return;
3005
3006 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
3007 tcp_set_state(ssk, TCP_CLOSE);
3008 mptcp_subflow_queue_clean(sk, ssk);
3009 inet_csk_listen_stop(ssk);
3010 release_sock(ssk);
3011 }
3012
__mptcp_close(struct sock * sk,long timeout)3013 bool __mptcp_close(struct sock *sk, long timeout)
3014 {
3015 struct mptcp_subflow_context *subflow;
3016 struct mptcp_sock *msk = mptcp_sk(sk);
3017 bool do_cancel_work = false;
3018 int subflows_alive = 0;
3019
3020 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
3021
3022 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
3023 mptcp_check_listen_stop(sk);
3024 inet_sk_state_store(sk, TCP_CLOSE);
3025 goto cleanup;
3026 }
3027
3028 if (mptcp_check_readable(msk)) {
3029 /* the msk has read data, do the MPTCP equivalent of TCP reset */
3030 inet_sk_state_store(sk, TCP_CLOSE);
3031 mptcp_do_fastclose(sk);
3032 } else if (mptcp_close_state(sk)) {
3033 __mptcp_wr_shutdown(sk);
3034 }
3035
3036 sk_stream_wait_close(sk, timeout);
3037
3038 cleanup:
3039 /* orphan all the subflows */
3040 mptcp_for_each_subflow(msk, subflow) {
3041 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3042 bool slow = lock_sock_fast_nested(ssk);
3043
3044 subflows_alive += ssk->sk_state != TCP_CLOSE;
3045
3046 /* since the close timeout takes precedence on the fail one,
3047 * cancel the latter
3048 */
3049 if (ssk == msk->first)
3050 subflow->fail_tout = 0;
3051
3052 /* detach from the parent socket, but allow data_ready to
3053 * push incoming data into the mptcp stack, to properly ack it
3054 */
3055 ssk->sk_socket = NULL;
3056 ssk->sk_wq = NULL;
3057 unlock_sock_fast(ssk, slow);
3058 }
3059 sock_orphan(sk);
3060
3061 /* all the subflows are closed, only timeout can change the msk
3062 * state, let's not keep resources busy for no reasons
3063 */
3064 if (subflows_alive == 0)
3065 inet_sk_state_store(sk, TCP_CLOSE);
3066
3067 sock_hold(sk);
3068 pr_debug("msk=%p state=%d", sk, sk->sk_state);
3069 if (mptcp_sk(sk)->token)
3070 mptcp_event(MPTCP_EVENT_CLOSED, msk, NULL, GFP_KERNEL);
3071
3072 if (sk->sk_state == TCP_CLOSE) {
3073 __mptcp_destroy_sock(sk);
3074 do_cancel_work = true;
3075 } else {
3076 mptcp_start_tout_timer(sk);
3077 }
3078
3079 return do_cancel_work;
3080 }
3081
mptcp_close(struct sock * sk,long timeout)3082 static void mptcp_close(struct sock *sk, long timeout)
3083 {
3084 bool do_cancel_work;
3085
3086 lock_sock(sk);
3087
3088 do_cancel_work = __mptcp_close(sk, timeout);
3089 release_sock(sk);
3090 if (do_cancel_work)
3091 mptcp_cancel_work(sk);
3092
3093 sock_put(sk);
3094 }
3095
mptcp_copy_inaddrs(struct sock * msk,const struct sock * ssk)3096 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3097 {
3098 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3099 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3100 struct ipv6_pinfo *msk6 = inet6_sk(msk);
3101
3102 msk->sk_v6_daddr = ssk->sk_v6_daddr;
3103 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3104
3105 if (msk6 && ssk6) {
3106 msk6->saddr = ssk6->saddr;
3107 msk6->flow_label = ssk6->flow_label;
3108 }
3109 #endif
3110
3111 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3112 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3113 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3114 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3115 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3116 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3117 }
3118
mptcp_disconnect(struct sock * sk,int flags)3119 static int mptcp_disconnect(struct sock *sk, int flags)
3120 {
3121 struct mptcp_sock *msk = mptcp_sk(sk);
3122
3123 /* Deny disconnect if other threads are blocked in sk_wait_event()
3124 * or inet_wait_for_connect().
3125 */
3126 if (sk->sk_wait_pending)
3127 return -EBUSY;
3128
3129 /* We are on the fastopen error path. We can't call straight into the
3130 * subflows cleanup code due to lock nesting (we are already under
3131 * msk->firstsocket lock).
3132 */
3133 if (msk->fastopening)
3134 return -EBUSY;
3135
3136 mptcp_check_listen_stop(sk);
3137 inet_sk_state_store(sk, TCP_CLOSE);
3138
3139 mptcp_stop_rtx_timer(sk);
3140 mptcp_stop_tout_timer(sk);
3141
3142 if (mptcp_sk(sk)->token)
3143 mptcp_event(MPTCP_EVENT_CLOSED, mptcp_sk(sk), NULL, GFP_KERNEL);
3144
3145 /* msk->subflow is still intact, the following will not free the first
3146 * subflow
3147 */
3148 mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3149 msk->last_snd = NULL;
3150 WRITE_ONCE(msk->flags, 0);
3151 msk->cb_flags = 0;
3152 msk->recovery = false;
3153 msk->can_ack = false;
3154 msk->fully_established = false;
3155 msk->rcv_data_fin = false;
3156 msk->snd_data_fin_enable = false;
3157 msk->rcv_fastclose = false;
3158 msk->use_64bit_ack = false;
3159 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3160 mptcp_pm_data_reset(msk);
3161 mptcp_ca_reset(sk);
3162
3163 WRITE_ONCE(sk->sk_shutdown, 0);
3164 sk_error_report(sk);
3165 return 0;
3166 }
3167
3168 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
mptcp_inet6_sk(const struct sock * sk)3169 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3170 {
3171 unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3172
3173 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3174 }
3175 #endif
3176
mptcp_sk_clone_init(const struct sock * sk,const struct mptcp_options_received * mp_opt,struct sock * ssk,struct request_sock * req)3177 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3178 const struct mptcp_options_received *mp_opt,
3179 struct sock *ssk,
3180 struct request_sock *req)
3181 {
3182 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3183 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3184 struct mptcp_sock *msk;
3185 u64 ack_seq;
3186
3187 if (!nsk)
3188 return NULL;
3189
3190 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3191 if (nsk->sk_family == AF_INET6)
3192 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3193 #endif
3194
3195 nsk->sk_wait_pending = 0;
3196 __mptcp_init_sock(nsk);
3197
3198 msk = mptcp_sk(nsk);
3199 msk->local_key = subflow_req->local_key;
3200 msk->token = subflow_req->token;
3201 WRITE_ONCE(msk->subflow, NULL);
3202 msk->in_accept_queue = 1;
3203 WRITE_ONCE(msk->fully_established, false);
3204 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3205 WRITE_ONCE(msk->csum_enabled, true);
3206
3207 msk->write_seq = subflow_req->idsn + 1;
3208 msk->snd_nxt = msk->write_seq;
3209 msk->snd_una = msk->write_seq;
3210 msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
3211 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3212
3213 if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) {
3214 msk->can_ack = true;
3215 msk->remote_key = mp_opt->sndr_key;
3216 mptcp_crypto_key_sha(msk->remote_key, NULL, &ack_seq);
3217 ack_seq++;
3218 WRITE_ONCE(msk->ack_seq, ack_seq);
3219 atomic64_set(&msk->rcv_wnd_sent, ack_seq);
3220 }
3221
3222 sock_reset_flag(nsk, SOCK_RCU_FREE);
3223 security_inet_csk_clone(nsk, req);
3224
3225 /* this can't race with mptcp_close(), as the msk is
3226 * not yet exposted to user-space
3227 */
3228 inet_sk_state_store(nsk, TCP_ESTABLISHED);
3229
3230 /* The msk maintain a ref to each subflow in the connections list */
3231 WRITE_ONCE(msk->first, ssk);
3232 list_add(&mptcp_subflow_ctx(ssk)->node, &msk->conn_list);
3233 sock_hold(ssk);
3234
3235 /* new mpc subflow takes ownership of the newly
3236 * created mptcp socket
3237 */
3238 mptcp_token_accept(subflow_req, msk);
3239
3240 /* set msk addresses early to ensure mptcp_pm_get_local_id()
3241 * uses the correct data
3242 */
3243 mptcp_copy_inaddrs(nsk, ssk);
3244 mptcp_propagate_sndbuf(nsk, ssk);
3245
3246 mptcp_rcv_space_init(msk, ssk);
3247 bh_unlock_sock(nsk);
3248
3249 /* note: the newly allocated socket refcount is 2 now */
3250 return nsk;
3251 }
3252
mptcp_rcv_space_init(struct mptcp_sock * msk,const struct sock * ssk)3253 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3254 {
3255 const struct tcp_sock *tp = tcp_sk(ssk);
3256
3257 msk->rcvq_space.copied = 0;
3258 msk->rcvq_space.rtt_us = 0;
3259
3260 msk->rcvq_space.time = tp->tcp_mstamp;
3261
3262 /* initial rcv_space offering made to peer */
3263 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3264 TCP_INIT_CWND * tp->advmss);
3265 if (msk->rcvq_space.space == 0)
3266 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3267
3268 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3269 }
3270
mptcp_accept(struct sock * sk,int flags,int * err,bool kern)3271 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
3272 bool kern)
3273 {
3274 struct mptcp_sock *msk = mptcp_sk(sk);
3275 struct socket *listener;
3276 struct sock *newsk;
3277
3278 listener = READ_ONCE(msk->subflow);
3279 if (WARN_ON_ONCE(!listener)) {
3280 *err = -EINVAL;
3281 return NULL;
3282 }
3283
3284 pr_debug("msk=%p, listener=%p", msk, mptcp_subflow_ctx(listener->sk));
3285 newsk = inet_csk_accept(listener->sk, flags, err, kern);
3286 if (!newsk)
3287 return NULL;
3288
3289 pr_debug("msk=%p, subflow is mptcp=%d", msk, sk_is_mptcp(newsk));
3290 if (sk_is_mptcp(newsk)) {
3291 struct mptcp_subflow_context *subflow;
3292 struct sock *new_mptcp_sock;
3293
3294 subflow = mptcp_subflow_ctx(newsk);
3295 new_mptcp_sock = subflow->conn;
3296
3297 /* is_mptcp should be false if subflow->conn is missing, see
3298 * subflow_syn_recv_sock()
3299 */
3300 if (WARN_ON_ONCE(!new_mptcp_sock)) {
3301 tcp_sk(newsk)->is_mptcp = 0;
3302 goto out;
3303 }
3304
3305 newsk = new_mptcp_sock;
3306 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3307 } else {
3308 MPTCP_INC_STATS(sock_net(sk),
3309 MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
3310 }
3311
3312 out:
3313 newsk->sk_kern_sock = kern;
3314 return newsk;
3315 }
3316
mptcp_destroy_common(struct mptcp_sock * msk,unsigned int flags)3317 void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
3318 {
3319 struct mptcp_subflow_context *subflow, *tmp;
3320 struct sock *sk = (struct sock *)msk;
3321
3322 __mptcp_clear_xmit(sk);
3323
3324 /* join list will be eventually flushed (with rst) at sock lock release time */
3325 mptcp_for_each_subflow_safe(msk, subflow, tmp)
3326 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3327
3328 /* move to sk_receive_queue, sk_stream_kill_queues will purge it */
3329 mptcp_data_lock(sk);
3330 skb_queue_splice_tail_init(&msk->receive_queue, &sk->sk_receive_queue);
3331 __skb_queue_purge(&sk->sk_receive_queue);
3332 skb_rbtree_purge(&msk->out_of_order_queue);
3333 mptcp_data_unlock(sk);
3334
3335 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3336 * inet_sock_destruct() will dispose it
3337 */
3338 sk_forward_alloc_add(sk, msk->rmem_fwd_alloc);
3339 WRITE_ONCE(msk->rmem_fwd_alloc, 0);
3340 mptcp_token_destroy(msk);
3341 mptcp_pm_free_anno_list(msk);
3342 mptcp_free_local_addr_list(msk);
3343 }
3344
mptcp_destroy(struct sock * sk)3345 static void mptcp_destroy(struct sock *sk)
3346 {
3347 struct mptcp_sock *msk = mptcp_sk(sk);
3348
3349 mptcp_dispose_initial_subflow(msk);
3350
3351 /* allow the following to close even the initial subflow */
3352 msk->free_first = 1;
3353 mptcp_destroy_common(msk, 0);
3354 sk_sockets_allocated_dec(sk);
3355 }
3356
__mptcp_data_acked(struct sock * sk)3357 void __mptcp_data_acked(struct sock *sk)
3358 {
3359 if (!sock_owned_by_user(sk))
3360 __mptcp_clean_una(sk);
3361 else
3362 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3363
3364 if (mptcp_pending_data_fin_ack(sk))
3365 mptcp_schedule_work(sk);
3366 }
3367
__mptcp_check_push(struct sock * sk,struct sock * ssk)3368 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3369 {
3370 if (!mptcp_send_head(sk))
3371 return;
3372
3373 if (!sock_owned_by_user(sk)) {
3374 struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
3375
3376 if (xmit_ssk == ssk)
3377 __mptcp_subflow_push_pending(sk, ssk);
3378 else if (xmit_ssk)
3379 mptcp_subflow_delegate(mptcp_subflow_ctx(xmit_ssk), MPTCP_DELEGATE_SEND);
3380 } else {
3381 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3382 }
3383 }
3384
3385 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3386 BIT(MPTCP_RETRANSMIT) | \
3387 BIT(MPTCP_FLUSH_JOIN_LIST))
3388
3389 /* processes deferred events and flush wmem */
mptcp_release_cb(struct sock * sk)3390 static void mptcp_release_cb(struct sock *sk)
3391 __must_hold(&sk->sk_lock.slock)
3392 {
3393 struct mptcp_sock *msk = mptcp_sk(sk);
3394
3395 for (;;) {
3396 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
3397 struct list_head join_list;
3398
3399 if (!flags)
3400 break;
3401
3402 INIT_LIST_HEAD(&join_list);
3403 list_splice_init(&msk->join_list, &join_list);
3404
3405 /* the following actions acquire the subflow socket lock
3406 *
3407 * 1) can't be invoked in atomic scope
3408 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3409 * datapath acquires the msk socket spinlock while helding
3410 * the subflow socket lock
3411 */
3412 msk->cb_flags &= ~flags;
3413 spin_unlock_bh(&sk->sk_lock.slock);
3414
3415 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3416 __mptcp_flush_join_list(sk, &join_list);
3417 if (flags & BIT(MPTCP_PUSH_PENDING))
3418 __mptcp_push_pending(sk, 0);
3419 if (flags & BIT(MPTCP_RETRANSMIT))
3420 __mptcp_retrans(sk);
3421
3422 cond_resched();
3423 spin_lock_bh(&sk->sk_lock.slock);
3424 }
3425
3426 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3427 __mptcp_clean_una_wakeup(sk);
3428 if (unlikely(msk->cb_flags)) {
3429 /* be sure to set the current sk state before tacking actions
3430 * depending on sk_state, that is processing MPTCP_ERROR_REPORT
3431 */
3432 if (__test_and_clear_bit(MPTCP_CONNECTED, &msk->cb_flags))
3433 __mptcp_set_connected(sk);
3434 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3435 __mptcp_error_report(sk);
3436 if (__test_and_clear_bit(MPTCP_RESET_SCHEDULER, &msk->cb_flags))
3437 msk->last_snd = NULL;
3438 }
3439
3440 __mptcp_update_rmem(sk);
3441 }
3442
3443 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3444 * TCP can't schedule delack timer before the subflow is fully established.
3445 * MPTCP uses the delack timer to do 3rd ack retransmissions
3446 */
schedule_3rdack_retransmission(struct sock * ssk)3447 static void schedule_3rdack_retransmission(struct sock *ssk)
3448 {
3449 struct inet_connection_sock *icsk = inet_csk(ssk);
3450 struct tcp_sock *tp = tcp_sk(ssk);
3451 unsigned long timeout;
3452
3453 if (mptcp_subflow_ctx(ssk)->fully_established)
3454 return;
3455
3456 /* reschedule with a timeout above RTT, as we must look only for drop */
3457 if (tp->srtt_us)
3458 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3459 else
3460 timeout = TCP_TIMEOUT_INIT;
3461 timeout += jiffies;
3462
3463 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3464 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3465 icsk->icsk_ack.timeout = timeout;
3466 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3467 }
3468
mptcp_subflow_process_delegated(struct sock * ssk,long status)3469 void mptcp_subflow_process_delegated(struct sock *ssk, long status)
3470 {
3471 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3472 struct sock *sk = subflow->conn;
3473
3474 if (status & BIT(MPTCP_DELEGATE_SEND)) {
3475 mptcp_data_lock(sk);
3476 if (!sock_owned_by_user(sk))
3477 __mptcp_subflow_push_pending(sk, ssk);
3478 else
3479 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3480 mptcp_data_unlock(sk);
3481 }
3482 if (status & BIT(MPTCP_DELEGATE_ACK))
3483 schedule_3rdack_retransmission(ssk);
3484 }
3485
mptcp_hash(struct sock * sk)3486 static int mptcp_hash(struct sock *sk)
3487 {
3488 /* should never be called,
3489 * we hash the TCP subflows not the master socket
3490 */
3491 WARN_ON_ONCE(1);
3492 return 0;
3493 }
3494
mptcp_unhash(struct sock * sk)3495 static void mptcp_unhash(struct sock *sk)
3496 {
3497 /* called from sk_common_release(), but nothing to do here */
3498 }
3499
mptcp_get_port(struct sock * sk,unsigned short snum)3500 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3501 {
3502 struct mptcp_sock *msk = mptcp_sk(sk);
3503 struct socket *ssock;
3504
3505 ssock = msk->subflow;
3506 pr_debug("msk=%p, subflow=%p", msk, ssock);
3507 if (WARN_ON_ONCE(!ssock))
3508 return -EINVAL;
3509
3510 return inet_csk_get_port(ssock->sk, snum);
3511 }
3512
mptcp_finish_connect(struct sock * ssk)3513 void mptcp_finish_connect(struct sock *ssk)
3514 {
3515 struct mptcp_subflow_context *subflow;
3516 struct mptcp_sock *msk;
3517 struct sock *sk;
3518 u64 ack_seq;
3519
3520 subflow = mptcp_subflow_ctx(ssk);
3521 sk = subflow->conn;
3522 msk = mptcp_sk(sk);
3523
3524 pr_debug("msk=%p, token=%u", sk, subflow->token);
3525
3526 mptcp_crypto_key_sha(subflow->remote_key, NULL, &ack_seq);
3527 ack_seq++;
3528 subflow->map_seq = ack_seq;
3529 subflow->map_subflow_seq = 1;
3530
3531 /* the socket is not connected yet, no msk/subflow ops can access/race
3532 * accessing the field below
3533 */
3534 WRITE_ONCE(msk->remote_key, subflow->remote_key);
3535 WRITE_ONCE(msk->local_key, subflow->local_key);
3536 WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
3537 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3538 WRITE_ONCE(msk->ack_seq, ack_seq);
3539 WRITE_ONCE(msk->can_ack, 1);
3540 WRITE_ONCE(msk->snd_una, msk->write_seq);
3541 atomic64_set(&msk->rcv_wnd_sent, ack_seq);
3542
3543 mptcp_pm_new_connection(msk, ssk, 0);
3544
3545 mptcp_rcv_space_init(msk, ssk);
3546 }
3547
mptcp_sock_graft(struct sock * sk,struct socket * parent)3548 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3549 {
3550 write_lock_bh(&sk->sk_callback_lock);
3551 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3552 sk_set_socket(sk, parent);
3553 sk->sk_uid = SOCK_INODE(parent)->i_uid;
3554 write_unlock_bh(&sk->sk_callback_lock);
3555 }
3556
mptcp_finish_join(struct sock * ssk)3557 bool mptcp_finish_join(struct sock *ssk)
3558 {
3559 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3560 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3561 struct sock *parent = (void *)msk;
3562 bool ret = true;
3563
3564 pr_debug("msk=%p, subflow=%p", msk, subflow);
3565
3566 /* mptcp socket already closing? */
3567 if (!mptcp_is_fully_established(parent)) {
3568 subflow->reset_reason = MPTCP_RST_EMPTCP;
3569 return false;
3570 }
3571
3572 /* active subflow, already present inside the conn_list */
3573 if (!list_empty(&subflow->node)) {
3574 mptcp_subflow_joined(msk, ssk);
3575 return true;
3576 }
3577
3578 if (!mptcp_pm_allow_new_subflow(msk))
3579 goto err_prohibited;
3580
3581 /* If we can't acquire msk socket lock here, let the release callback
3582 * handle it
3583 */
3584 mptcp_data_lock(parent);
3585 if (!sock_owned_by_user(parent)) {
3586 ret = __mptcp_finish_join(msk, ssk);
3587 if (ret) {
3588 sock_hold(ssk);
3589 list_add_tail(&subflow->node, &msk->conn_list);
3590 }
3591 } else {
3592 sock_hold(ssk);
3593 list_add_tail(&subflow->node, &msk->join_list);
3594 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3595 }
3596 mptcp_data_unlock(parent);
3597
3598 if (!ret) {
3599 err_prohibited:
3600 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3601 return false;
3602 }
3603
3604 return true;
3605 }
3606
mptcp_shutdown(struct sock * sk,int how)3607 static void mptcp_shutdown(struct sock *sk, int how)
3608 {
3609 pr_debug("sk=%p, how=%d", sk, how);
3610
3611 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3612 __mptcp_wr_shutdown(sk);
3613 }
3614
mptcp_forward_alloc_get(const struct sock * sk)3615 static int mptcp_forward_alloc_get(const struct sock *sk)
3616 {
3617 return READ_ONCE(sk->sk_forward_alloc) +
3618 READ_ONCE(mptcp_sk(sk)->rmem_fwd_alloc);
3619 }
3620
mptcp_ioctl_outq(const struct mptcp_sock * msk,u64 v)3621 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3622 {
3623 const struct sock *sk = (void *)msk;
3624 u64 delta;
3625
3626 if (sk->sk_state == TCP_LISTEN)
3627 return -EINVAL;
3628
3629 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3630 return 0;
3631
3632 delta = msk->write_seq - v;
3633 if (__mptcp_check_fallback(msk) && msk->first) {
3634 struct tcp_sock *tp = tcp_sk(msk->first);
3635
3636 /* the first subflow is disconnected after close - see
3637 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3638 * so ignore that status, too.
3639 */
3640 if (!((1 << msk->first->sk_state) &
3641 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3642 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3643 }
3644 if (delta > INT_MAX)
3645 delta = INT_MAX;
3646
3647 return (int)delta;
3648 }
3649
mptcp_ioctl(struct sock * sk,int cmd,unsigned long arg)3650 static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3651 {
3652 struct mptcp_sock *msk = mptcp_sk(sk);
3653 bool slow;
3654 int answ;
3655
3656 switch (cmd) {
3657 case SIOCINQ:
3658 if (sk->sk_state == TCP_LISTEN)
3659 return -EINVAL;
3660
3661 lock_sock(sk);
3662 __mptcp_move_skbs(msk);
3663 answ = mptcp_inq_hint(sk);
3664 release_sock(sk);
3665 break;
3666 case SIOCOUTQ:
3667 slow = lock_sock_fast(sk);
3668 answ = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3669 unlock_sock_fast(sk, slow);
3670 break;
3671 case SIOCOUTQNSD:
3672 slow = lock_sock_fast(sk);
3673 answ = mptcp_ioctl_outq(msk, msk->snd_nxt);
3674 unlock_sock_fast(sk, slow);
3675 break;
3676 default:
3677 return -ENOIOCTLCMD;
3678 }
3679
3680 return put_user(answ, (int __user *)arg);
3681 }
3682
mptcp_subflow_early_fallback(struct mptcp_sock * msk,struct mptcp_subflow_context * subflow)3683 static void mptcp_subflow_early_fallback(struct mptcp_sock *msk,
3684 struct mptcp_subflow_context *subflow)
3685 {
3686 subflow->request_mptcp = 0;
3687 __mptcp_do_fallback(msk);
3688 }
3689
mptcp_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)3690 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3691 {
3692 struct mptcp_subflow_context *subflow;
3693 struct mptcp_sock *msk = mptcp_sk(sk);
3694 struct socket *ssock;
3695 int err = -EINVAL;
3696
3697 ssock = __mptcp_nmpc_socket(msk);
3698 if (!ssock)
3699 return -EINVAL;
3700
3701 mptcp_token_destroy(msk);
3702 inet_sk_state_store(sk, TCP_SYN_SENT);
3703 subflow = mptcp_subflow_ctx(ssock->sk);
3704 #ifdef CONFIG_TCP_MD5SIG
3705 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3706 * TCP option space.
3707 */
3708 if (rcu_access_pointer(tcp_sk(ssock->sk)->md5sig_info))
3709 mptcp_subflow_early_fallback(msk, subflow);
3710 #endif
3711 if (subflow->request_mptcp && mptcp_token_new_connect(ssock->sk)) {
3712 MPTCP_INC_STATS(sock_net(ssock->sk), MPTCP_MIB_TOKENFALLBACKINIT);
3713 mptcp_subflow_early_fallback(msk, subflow);
3714 }
3715 if (likely(!__mptcp_check_fallback(msk)))
3716 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3717
3718 /* if reaching here via the fastopen/sendmsg path, the caller already
3719 * acquired the subflow socket lock, too.
3720 */
3721 if (msk->fastopening)
3722 err = __inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK, 1);
3723 else
3724 err = inet_stream_connect(ssock, uaddr, addr_len, O_NONBLOCK);
3725 inet_sk(sk)->defer_connect = inet_sk(ssock->sk)->defer_connect;
3726
3727 /* on successful connect, the msk state will be moved to established by
3728 * subflow_finish_connect()
3729 */
3730 if (unlikely(err && err != -EINPROGRESS)) {
3731 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk));
3732 return err;
3733 }
3734
3735 mptcp_copy_inaddrs(sk, ssock->sk);
3736
3737 /* silence EINPROGRESS and let the caller inet_stream_connect
3738 * handle the connection in progress
3739 */
3740 return 0;
3741 }
3742
3743 static struct proto mptcp_prot = {
3744 .name = "MPTCP",
3745 .owner = THIS_MODULE,
3746 .init = mptcp_init_sock,
3747 .connect = mptcp_connect,
3748 .disconnect = mptcp_disconnect,
3749 .close = mptcp_close,
3750 .accept = mptcp_accept,
3751 .setsockopt = mptcp_setsockopt,
3752 .getsockopt = mptcp_getsockopt,
3753 .shutdown = mptcp_shutdown,
3754 .destroy = mptcp_destroy,
3755 .sendmsg = mptcp_sendmsg,
3756 .ioctl = mptcp_ioctl,
3757 .recvmsg = mptcp_recvmsg,
3758 .release_cb = mptcp_release_cb,
3759 .hash = mptcp_hash,
3760 .unhash = mptcp_unhash,
3761 .get_port = mptcp_get_port,
3762 .forward_alloc_get = mptcp_forward_alloc_get,
3763 .sockets_allocated = &mptcp_sockets_allocated,
3764
3765 .memory_allocated = &tcp_memory_allocated,
3766 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3767
3768 .memory_pressure = &tcp_memory_pressure,
3769 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3770 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3771 .sysctl_mem = sysctl_tcp_mem,
3772 .obj_size = sizeof(struct mptcp_sock),
3773 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3774 .no_autobind = true,
3775 };
3776
mptcp_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)3777 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3778 {
3779 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3780 struct socket *ssock;
3781 int err;
3782
3783 lock_sock(sock->sk);
3784 ssock = __mptcp_nmpc_socket(msk);
3785 if (!ssock) {
3786 err = -EINVAL;
3787 goto unlock;
3788 }
3789
3790 err = ssock->ops->bind(ssock, uaddr, addr_len);
3791 if (!err)
3792 mptcp_copy_inaddrs(sock->sk, ssock->sk);
3793
3794 unlock:
3795 release_sock(sock->sk);
3796 return err;
3797 }
3798
mptcp_listen(struct socket * sock,int backlog)3799 static int mptcp_listen(struct socket *sock, int backlog)
3800 {
3801 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3802 struct sock *sk = sock->sk;
3803 struct socket *ssock;
3804 int err;
3805
3806 pr_debug("msk=%p", msk);
3807
3808 lock_sock(sk);
3809
3810 err = -EINVAL;
3811 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
3812 goto unlock;
3813
3814 ssock = __mptcp_nmpc_socket(msk);
3815 if (!ssock) {
3816 err = -EINVAL;
3817 goto unlock;
3818 }
3819
3820 mptcp_token_destroy(msk);
3821 inet_sk_state_store(sk, TCP_LISTEN);
3822 sock_set_flag(sk, SOCK_RCU_FREE);
3823
3824 err = ssock->ops->listen(ssock, backlog);
3825 inet_sk_state_store(sk, inet_sk_state_load(ssock->sk));
3826 if (!err)
3827 mptcp_copy_inaddrs(sk, ssock->sk);
3828
3829 unlock:
3830 release_sock(sk);
3831 return err;
3832 }
3833
mptcp_stream_accept(struct socket * sock,struct socket * newsock,int flags,bool kern)3834 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3835 int flags, bool kern)
3836 {
3837 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3838 struct socket *ssock;
3839 int err;
3840
3841 pr_debug("msk=%p", msk);
3842
3843 /* Buggy applications can call accept on socket states other then LISTEN
3844 * but no need to allocate the first subflow just to error out.
3845 */
3846 ssock = READ_ONCE(msk->subflow);
3847 if (!ssock)
3848 return -EINVAL;
3849
3850 err = ssock->ops->accept(sock, newsock, flags, kern);
3851 if (err == 0 && !mptcp_is_tcpsk(newsock->sk)) {
3852 struct mptcp_sock *msk = mptcp_sk(newsock->sk);
3853 struct mptcp_subflow_context *subflow;
3854 struct sock *newsk = newsock->sk;
3855
3856 msk->in_accept_queue = 0;
3857
3858 lock_sock(newsk);
3859
3860 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3861 * This is needed so NOSPACE flag can be set from tcp stack.
3862 */
3863 mptcp_for_each_subflow(msk, subflow) {
3864 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3865
3866 if (!ssk->sk_socket)
3867 mptcp_sock_graft(ssk, newsock);
3868 }
3869
3870 /* Do late cleanup for the first subflow as necessary. Also
3871 * deal with bad peers not doing a complete shutdown.
3872 */
3873 if (msk->first &&
3874 unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
3875 __mptcp_close_ssk(newsk, msk->first,
3876 mptcp_subflow_ctx(msk->first), 0);
3877 if (unlikely(list_empty(&msk->conn_list)))
3878 inet_sk_state_store(newsk, TCP_CLOSE);
3879 }
3880
3881 release_sock(newsk);
3882 }
3883
3884 return err;
3885 }
3886
mptcp_check_writeable(struct mptcp_sock * msk)3887 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3888 {
3889 struct sock *sk = (struct sock *)msk;
3890
3891 if (sk_stream_is_writeable(sk))
3892 return EPOLLOUT | EPOLLWRNORM;
3893
3894 mptcp_set_nospace(sk);
3895 smp_mb__after_atomic(); /* msk->flags is changed by write_space cb */
3896 if (sk_stream_is_writeable(sk))
3897 return EPOLLOUT | EPOLLWRNORM;
3898
3899 return 0;
3900 }
3901
mptcp_poll(struct file * file,struct socket * sock,struct poll_table_struct * wait)3902 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3903 struct poll_table_struct *wait)
3904 {
3905 struct sock *sk = sock->sk;
3906 struct mptcp_sock *msk;
3907 __poll_t mask = 0;
3908 u8 shutdown;
3909 int state;
3910
3911 msk = mptcp_sk(sk);
3912 sock_poll_wait(file, sock, wait);
3913
3914 state = inet_sk_state_load(sk);
3915 pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
3916 if (state == TCP_LISTEN) {
3917 struct socket *ssock = READ_ONCE(msk->subflow);
3918
3919 if (WARN_ON_ONCE(!ssock || !ssock->sk))
3920 return 0;
3921
3922 return inet_csk_listen_poll(ssock->sk);
3923 }
3924
3925 shutdown = READ_ONCE(sk->sk_shutdown);
3926 if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3927 mask |= EPOLLHUP;
3928 if (shutdown & RCV_SHUTDOWN)
3929 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3930
3931 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3932 mask |= mptcp_check_readable(msk);
3933 if (shutdown & SEND_SHUTDOWN)
3934 mask |= EPOLLOUT | EPOLLWRNORM;
3935 else
3936 mask |= mptcp_check_writeable(msk);
3937 } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) {
3938 /* cf tcp_poll() note about TFO */
3939 mask |= EPOLLOUT | EPOLLWRNORM;
3940 }
3941
3942 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
3943 smp_rmb();
3944 if (READ_ONCE(sk->sk_err))
3945 mask |= EPOLLERR;
3946
3947 return mask;
3948 }
3949
3950 static const struct proto_ops mptcp_stream_ops = {
3951 .family = PF_INET,
3952 .owner = THIS_MODULE,
3953 .release = inet_release,
3954 .bind = mptcp_bind,
3955 .connect = inet_stream_connect,
3956 .socketpair = sock_no_socketpair,
3957 .accept = mptcp_stream_accept,
3958 .getname = inet_getname,
3959 .poll = mptcp_poll,
3960 .ioctl = inet_ioctl,
3961 .gettstamp = sock_gettstamp,
3962 .listen = mptcp_listen,
3963 .shutdown = inet_shutdown,
3964 .setsockopt = sock_common_setsockopt,
3965 .getsockopt = sock_common_getsockopt,
3966 .sendmsg = inet_sendmsg,
3967 .recvmsg = inet_recvmsg,
3968 .mmap = sock_no_mmap,
3969 .sendpage = inet_sendpage,
3970 };
3971
3972 static struct inet_protosw mptcp_protosw = {
3973 .type = SOCK_STREAM,
3974 .protocol = IPPROTO_MPTCP,
3975 .prot = &mptcp_prot,
3976 .ops = &mptcp_stream_ops,
3977 .flags = INET_PROTOSW_ICSK,
3978 };
3979
mptcp_napi_poll(struct napi_struct * napi,int budget)3980 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3981 {
3982 struct mptcp_delegated_action *delegated;
3983 struct mptcp_subflow_context *subflow;
3984 int work_done = 0;
3985
3986 delegated = container_of(napi, struct mptcp_delegated_action, napi);
3987 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3988 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3989
3990 bh_lock_sock_nested(ssk);
3991 if (!sock_owned_by_user(ssk)) {
3992 mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
3993 } else {
3994 /* tcp_release_cb_override already processed
3995 * the action or will do at next release_sock().
3996 * In both case must dequeue the subflow here - on the same
3997 * CPU that scheduled it.
3998 */
3999 smp_wmb();
4000 clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
4001 }
4002 bh_unlock_sock(ssk);
4003 sock_put(ssk);
4004
4005 if (++work_done == budget)
4006 return budget;
4007 }
4008
4009 /* always provide a 0 'work_done' argument, so that napi_complete_done
4010 * will not try accessing the NULL napi->dev ptr
4011 */
4012 napi_complete_done(napi, 0);
4013 return work_done;
4014 }
4015
mptcp_proto_init(void)4016 void __init mptcp_proto_init(void)
4017 {
4018 struct mptcp_delegated_action *delegated;
4019 int cpu;
4020
4021 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
4022
4023 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4024 panic("Failed to allocate MPTCP pcpu counter\n");
4025
4026 init_dummy_netdev(&mptcp_napi_dev);
4027 for_each_possible_cpu(cpu) {
4028 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4029 INIT_LIST_HEAD(&delegated->head);
4030 netif_napi_add_tx(&mptcp_napi_dev, &delegated->napi,
4031 mptcp_napi_poll);
4032 napi_enable(&delegated->napi);
4033 }
4034
4035 mptcp_subflow_init();
4036 mptcp_pm_init();
4037 mptcp_token_init();
4038
4039 if (proto_register(&mptcp_prot, 1) != 0)
4040 panic("Failed to register MPTCP proto.\n");
4041
4042 inet_register_protosw(&mptcp_protosw);
4043
4044 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4045 }
4046
4047 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4048 static const struct proto_ops mptcp_v6_stream_ops = {
4049 .family = PF_INET6,
4050 .owner = THIS_MODULE,
4051 .release = inet6_release,
4052 .bind = mptcp_bind,
4053 .connect = inet_stream_connect,
4054 .socketpair = sock_no_socketpair,
4055 .accept = mptcp_stream_accept,
4056 .getname = inet6_getname,
4057 .poll = mptcp_poll,
4058 .ioctl = inet6_ioctl,
4059 .gettstamp = sock_gettstamp,
4060 .listen = mptcp_listen,
4061 .shutdown = inet_shutdown,
4062 .setsockopt = sock_common_setsockopt,
4063 .getsockopt = sock_common_getsockopt,
4064 .sendmsg = inet6_sendmsg,
4065 .recvmsg = inet6_recvmsg,
4066 .mmap = sock_no_mmap,
4067 .sendpage = inet_sendpage,
4068 #ifdef CONFIG_COMPAT
4069 .compat_ioctl = inet6_compat_ioctl,
4070 #endif
4071 };
4072
4073 static struct proto mptcp_v6_prot;
4074
4075 static struct inet_protosw mptcp_v6_protosw = {
4076 .type = SOCK_STREAM,
4077 .protocol = IPPROTO_MPTCP,
4078 .prot = &mptcp_v6_prot,
4079 .ops = &mptcp_v6_stream_ops,
4080 .flags = INET_PROTOSW_ICSK,
4081 };
4082
mptcp_proto_v6_init(void)4083 int __init mptcp_proto_v6_init(void)
4084 {
4085 int err;
4086
4087 mptcp_v6_prot = mptcp_prot;
4088 strcpy(mptcp_v6_prot.name, "MPTCPv6");
4089 mptcp_v6_prot.slab = NULL;
4090 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4091
4092 err = proto_register(&mptcp_v6_prot, 1);
4093 if (err)
4094 return err;
4095
4096 err = inet6_register_protosw(&mptcp_v6_protosw);
4097 if (err)
4098 proto_unregister(&mptcp_v6_prot);
4099
4100 return err;
4101 }
4102 #endif
4103