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