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