Debian Codegraph @

net/mptcp/protocol.c

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