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 do_check_data_fin = 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 do_check_data_fin = 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 /* ensure the rtx timer is running */
1678 if (!mptcp_rtx_timer_pending(sk))
1679 mptcp_reset_rtx_timer(sk);
1680 if (do_check_data_fin)
1681 mptcp_check_send_data_fin(sk);
1682 }
1683
__mptcp_subflow_push_pending(struct sock * sk,struct sock * ssk,bool first)1684 static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
1685 {
1686 struct mptcp_sock *msk = mptcp_sk(sk);
1687 struct mptcp_sendmsg_info info = {
1688 .data_lock_held = true,
1689 };
1690 bool keep_pushing = true;
1691 struct sock *xmit_ssk;
1692 int copied = 0;
1693
1694 info.flags = 0;
1695 while (mptcp_send_head(sk) && keep_pushing) {
1696 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1697 int ret = 0;
1698
1699 /* check for a different subflow usage only after
1700 * spooling the first chunk of data
1701 */
1702 if (first) {
1703 mptcp_subflow_set_scheduled(subflow, false);
1704 ret = __subflow_push_pending(sk, ssk, &info);
1705 first = false;
1706 if (ret <= 0)
1707 break;
1708 copied += ret;
1709 continue;
1710 }
1711
1712 if (mptcp_sched_get_send(msk))
1713 goto out;
1714
1715 if (READ_ONCE(subflow->scheduled)) {
1716 mptcp_subflow_set_scheduled(subflow, false);
1717 ret = __subflow_push_pending(sk, ssk, &info);
1718 if (ret <= 0)
1719 keep_pushing = false;
1720 copied += ret;
1721 }
1722
1723 mptcp_for_each_subflow(msk, subflow) {
1724 if (READ_ONCE(subflow->scheduled)) {
1725 xmit_ssk = mptcp_subflow_tcp_sock(subflow);
1726 if (xmit_ssk != ssk) {
1727 mptcp_subflow_delegate(subflow,
1728 MPTCP_DELEGATE_SEND);
1729 keep_pushing = false;
1730 }
1731 }
1732 }
1733 }
1734
1735 out:
1736 /* __mptcp_alloc_tx_skb could have released some wmem and we are
1737 * not going to flush it via release_sock()
1738 */
1739 if (copied) {
1740 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1741 info.size_goal);
1742 if (!mptcp_rtx_timer_pending(sk))
1743 mptcp_reset_rtx_timer(sk);
1744
1745 if (msk->snd_data_fin_enable &&
1746 msk->snd_nxt + 1 == msk->write_seq)
1747 mptcp_schedule_work(sk);
1748 }
1749 }
1750
1751 static int mptcp_disconnect(struct sock *sk, int flags);
1752
mptcp_sendmsg_fastopen(struct sock * sk,struct msghdr * msg,size_t len,int * copied_syn)1753 static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1754 size_t len, int *copied_syn)
1755 {
1756 unsigned int saved_flags = msg->msg_flags;
1757 struct mptcp_sock *msk = mptcp_sk(sk);
1758 struct sock *ssk;
1759 int ret;
1760
1761 /* on flags based fastopen the mptcp is supposed to create the
1762 * first subflow right now. Otherwise we are in the defer_connect
1763 * path, and the first subflow must be already present.
1764 * Since the defer_connect flag is cleared after the first succsful
1765 * fastopen attempt, no need to check for additional subflow status.
1766 */
1767 if (msg->msg_flags & MSG_FASTOPEN) {
1768 ssk = __mptcp_nmpc_sk(msk);
1769 if (IS_ERR(ssk))
1770 return PTR_ERR(ssk);
1771 }
1772 if (!msk->first)
1773 return -EINVAL;
1774
1775 ssk = msk->first;
1776
1777 lock_sock(ssk);
1778 msg->msg_flags |= MSG_DONTWAIT;
1779 msk->fastopening = 1;
1780 ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1781 msk->fastopening = 0;
1782 msg->msg_flags = saved_flags;
1783 release_sock(ssk);
1784
1785 /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1786 if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1787 ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1788 msg->msg_namelen, msg->msg_flags, 1);
1789
1790 /* Keep the same behaviour of plain TCP: zero the copied bytes in
1791 * case of any error, except timeout or signal
1792 */
1793 if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1794 *copied_syn = 0;
1795 } else if (ret && ret != -EINPROGRESS) {
1796 /* The disconnect() op called by tcp_sendmsg_fastopen()/
1797 * __inet_stream_connect() can fail, due to looking check,
1798 * see mptcp_disconnect().
1799 * Attempt it again outside the problematic scope.
1800 */
1801 if (!mptcp_disconnect(sk, 0)) {
1802 sk->sk_disconnects++;
1803 sk->sk_socket->state = SS_UNCONNECTED;
1804 }
1805 }
1806 inet_clear_bit(DEFER_CONNECT, sk);
1807
1808 return ret;
1809 }
1810
do_copy_data_nocache(struct sock * sk,int copy,struct iov_iter * from,char * to)1811 static int do_copy_data_nocache(struct sock *sk, int copy,
1812 struct iov_iter *from, char *to)
1813 {
1814 if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
1815 if (!copy_from_iter_full_nocache(to, copy, from))
1816 return -EFAULT;
1817 } else if (!copy_from_iter_full(to, copy, from)) {
1818 return -EFAULT;
1819 }
1820 return 0;
1821 }
1822
1823 /* open-code sk_stream_memory_free() plus sent limit computation to
1824 * avoid indirect calls in fast-path.
1825 * Called under the msk socket lock, so we can avoid a bunch of ONCE
1826 * annotations.
1827 */
mptcp_send_limit(const struct sock * sk)1828 static u32 mptcp_send_limit(const struct sock *sk)
1829 {
1830 const struct mptcp_sock *msk = mptcp_sk(sk);
1831 u32 limit, not_sent;
1832
1833 if (sk->sk_wmem_queued >= READ_ONCE(sk->sk_sndbuf))
1834 return 0;
1835
1836 limit = mptcp_notsent_lowat(sk);
1837 if (limit == UINT_MAX)
1838 return UINT_MAX;
1839
1840 not_sent = msk->write_seq - msk->snd_nxt;
1841 if (not_sent >= limit)
1842 return 0;
1843
1844 return limit - not_sent;
1845 }
1846
mptcp_rps_record_subflows(const struct mptcp_sock * msk)1847 static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
1848 {
1849 struct mptcp_subflow_context *subflow;
1850
1851 if (!rfs_is_needed())
1852 return;
1853
1854 mptcp_for_each_subflow(msk, subflow) {
1855 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1856
1857 sock_rps_record_flow(ssk);
1858 }
1859 }
1860
mptcp_sendmsg(struct sock * sk,struct msghdr * msg,size_t len)1861 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1862 {
1863 struct mptcp_sock *msk = mptcp_sk(sk);
1864 struct page_frag *pfrag;
1865 size_t copied = 0;
1866 int ret = 0;
1867 long timeo;
1868
1869 /* silently ignore everything else */
1870 msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1871
1872 lock_sock(sk);
1873
1874 mptcp_rps_record_subflows(msk);
1875
1876 if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
1877 msg->msg_flags & MSG_FASTOPEN)) {
1878 int copied_syn = 0;
1879
1880 ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1881 copied += copied_syn;
1882 if (ret == -EINPROGRESS && copied_syn > 0)
1883 goto out;
1884 else if (ret)
1885 goto do_error;
1886 }
1887
1888 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1889
1890 if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1891 ret = sk_stream_wait_connect(sk, &timeo);
1892 if (ret)
1893 goto do_error;
1894 }
1895
1896 ret = -EPIPE;
1897 if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1898 goto do_error;
1899
1900 pfrag = sk_page_frag(sk);
1901
1902 while (msg_data_left(msg)) {
1903 int total_ts, frag_truesize = 0;
1904 struct mptcp_data_frag *dfrag;
1905 bool dfrag_collapsed;
1906 size_t psize, offset;
1907 u32 copy_limit;
1908
1909 /* ensure fitting the notsent_lowat() constraint */
1910 copy_limit = mptcp_send_limit(sk);
1911 if (!copy_limit)
1912 goto wait_for_memory;
1913
1914 /* reuse tail pfrag, if possible, or carve a new one from the
1915 * page allocator
1916 */
1917 dfrag = mptcp_pending_tail(sk);
1918 dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1919 if (!dfrag_collapsed) {
1920 if (!mptcp_page_frag_refill(sk, pfrag))
1921 goto wait_for_memory;
1922
1923 dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1924 frag_truesize = dfrag->overhead;
1925 }
1926
1927 /* we do not bound vs wspace, to allow a single packet.
1928 * memory accounting will prevent execessive memory usage
1929 * anyway
1930 */
1931 offset = dfrag->offset + dfrag->data_len;
1932 psize = pfrag->size - offset;
1933 psize = min_t(size_t, psize, msg_data_left(msg));
1934 psize = min_t(size_t, psize, copy_limit);
1935 total_ts = psize + frag_truesize;
1936
1937 if (!sk_wmem_schedule(sk, total_ts))
1938 goto wait_for_memory;
1939
1940 ret = do_copy_data_nocache(sk, psize, &msg->msg_iter,
1941 page_address(dfrag->page) + offset);
1942 if (ret)
1943 goto do_error;
1944
1945 /* data successfully copied into the write queue */
1946 sk_forward_alloc_add(sk, -total_ts);
1947 copied += psize;
1948 dfrag->data_len += psize;
1949 frag_truesize += psize;
1950 pfrag->offset += frag_truesize;
1951 WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1952
1953 /* charge data on mptcp pending queue to the msk socket
1954 * Note: we charge such data both to sk and ssk
1955 */
1956 sk_wmem_queued_add(sk, frag_truesize);
1957 if (!dfrag_collapsed) {
1958 get_page(dfrag->page);
1959 list_add_tail(&dfrag->list, &msk->rtx_queue);
1960 if (!msk->first_pending)
1961 msk->first_pending = dfrag;
1962 }
1963 pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d\n", msk,
1964 dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1965 !dfrag_collapsed);
1966
1967 continue;
1968
1969 wait_for_memory:
1970 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1971 __mptcp_push_pending(sk, msg->msg_flags);
1972 ret = sk_stream_wait_memory(sk, &timeo);
1973 if (ret)
1974 goto do_error;
1975 }
1976
1977 if (copied)
1978 __mptcp_push_pending(sk, msg->msg_flags);
1979
1980 out:
1981 release_sock(sk);
1982 return copied;
1983
1984 do_error:
1985 if (copied)
1986 goto out;
1987
1988 copied = sk_stream_error(sk, msg->msg_flags, ret);
1989 goto out;
1990 }
1991
1992 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied);
1993
__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)1994 static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg,
1995 size_t len, int flags, int copied_total,
1996 struct scm_timestamping_internal *tss,
1997 int *cmsg_flags)
1998 {
1999 struct mptcp_sock *msk = mptcp_sk(sk);
2000 struct sk_buff *skb, *tmp;
2001 int total_data_len = 0;
2002 int copied = 0;
2003
2004 skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) {
2005 u32 delta, offset = MPTCP_SKB_CB(skb)->offset;
2006 u32 data_len = skb->len - offset;
2007 u32 count;
2008 int err;
2009
2010 if (flags & MSG_PEEK) {
2011 /* skip already peeked skbs */
2012 if (total_data_len + data_len <= copied_total) {
2013 total_data_len += data_len;
2014 continue;
2015 }
2016
2017 /* skip the already peeked data in the current skb */
2018 delta = copied_total - total_data_len;
2019 offset += delta;
2020 data_len -= delta;
2021 }
2022
2023 count = min_t(size_t, len - copied, data_len);
2024 if (!(flags & MSG_TRUNC)) {
2025 err = skb_copy_datagram_msg(skb, offset, msg, count);
2026 if (unlikely(err < 0)) {
2027 if (!copied)
2028 return err;
2029 break;
2030 }
2031 }
2032
2033 if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
2034 tcp_update_recv_tstamps(skb, tss);
2035 *cmsg_flags |= MPTCP_CMSG_TS;
2036 }
2037
2038 copied += count;
2039
2040 if (!(flags & MSG_PEEK)) {
2041 msk->bytes_consumed += count;
2042 if (count < data_len) {
2043 MPTCP_SKB_CB(skb)->offset += count;
2044 MPTCP_SKB_CB(skb)->map_seq += count;
2045 break;
2046 }
2047
2048 /* avoid the indirect call, we know the destructor is sock_rfree */
2049 skb->destructor = NULL;
2050 skb->sk = NULL;
2051 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
2052 sk_mem_uncharge(sk, skb->truesize);
2053 __skb_unlink(skb, &sk->sk_receive_queue);
2054 skb_attempt_defer_free(skb);
2055 }
2056
2057 if (copied >= len)
2058 break;
2059 }
2060
2061 mptcp_rcv_space_adjust(msk, copied);
2062 return copied;
2063 }
2064
2065 /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
2066 *
2067 * Only difference: Use highest rtt estimate of the subflows in use.
2068 */
mptcp_rcv_space_adjust(struct mptcp_sock * msk,int copied)2069 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
2070 {
2071 struct mptcp_subflow_context *subflow;
2072 struct sock *sk = (struct sock *)msk;
2073 u8 scaling_ratio = U8_MAX;
2074 u32 time, advmss = 1;
2075 u64 rtt_us, mstamp;
2076
2077 msk_owned_by_me(msk);
2078
2079 if (copied <= 0)
2080 return;
2081
2082 if (!msk->rcvspace_init)
2083 mptcp_rcv_space_init(msk, msk->first);
2084
2085 msk->rcvq_space.copied += copied;
2086
2087 mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
2088 time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
2089
2090 rtt_us = msk->rcvq_space.rtt_us;
2091 if (rtt_us && time < (rtt_us >> 3))
2092 return;
2093
2094 rtt_us = 0;
2095 mptcp_for_each_subflow(msk, subflow) {
2096 const struct tcp_sock *tp;
2097 u64 sf_rtt_us;
2098 u32 sf_advmss;
2099
2100 tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
2101
2102 sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
2103 sf_advmss = READ_ONCE(tp->advmss);
2104
2105 rtt_us = max(sf_rtt_us, rtt_us);
2106 advmss = max(sf_advmss, advmss);
2107 scaling_ratio = min(tp->scaling_ratio, scaling_ratio);
2108 }
2109
2110 msk->rcvq_space.rtt_us = rtt_us;
2111 msk->scaling_ratio = scaling_ratio;
2112 if (time < (rtt_us >> 3) || rtt_us == 0)
2113 return;
2114
2115 if (msk->rcvq_space.copied <= msk->rcvq_space.space)
2116 goto new_measure;
2117
2118 if (mptcp_rcvbuf_grow(sk, msk->rcvq_space.copied)) {
2119 /* Make subflows follow along. If we do not do this, we
2120 * get drops at subflow level if skbs can't be moved to
2121 * the mptcp rx queue fast enough (announced rcv_win can
2122 * exceed ssk->sk_rcvbuf).
2123 */
2124 mptcp_for_each_subflow(msk, subflow) {
2125 struct sock *ssk;
2126 bool slow;
2127
2128 ssk = mptcp_subflow_tcp_sock(subflow);
2129 slow = lock_sock_fast(ssk);
2130 /* subflows can be added before tcp_init_transfer() */
2131 if (tcp_sk(ssk)->rcvq_space.space)
2132 tcp_rcvbuf_grow(ssk, msk->rcvq_space.copied);
2133 unlock_sock_fast(ssk, slow);
2134 }
2135 }
2136
2137 new_measure:
2138 msk->rcvq_space.copied = 0;
2139 msk->rcvq_space.time = mstamp;
2140 }
2141
__mptcp_move_skbs(struct sock * sk,struct list_head * skbs,u32 * delta)2142 static bool __mptcp_move_skbs(struct sock *sk, struct list_head *skbs, u32 *delta)
2143 {
2144 struct sk_buff *skb = list_first_entry(skbs, struct sk_buff, list);
2145 struct mptcp_sock *msk = mptcp_sk(sk);
2146 bool moved = false;
2147
2148 *delta = 0;
2149 while (1) {
2150 /* If the msk recvbuf is full stop, don't drop */
2151 if (sk_rmem_alloc_get(sk) > sk->sk_rcvbuf)
2152 break;
2153
2154 prefetch(skb->next);
2155 list_del(&skb->list);
2156 *delta += skb->truesize;
2157
2158 moved |= __mptcp_move_skb(sk, skb);
2159 if (list_empty(skbs))
2160 break;
2161
2162 skb = list_first_entry(skbs, struct sk_buff, list);
2163 }
2164
2165 __mptcp_ofo_queue(msk);
2166 if (moved)
2167 mptcp_check_data_fin((struct sock *)msk);
2168 return moved;
2169 }
2170
mptcp_can_spool_backlog(struct sock * sk,struct list_head * skbs)2171 static bool mptcp_can_spool_backlog(struct sock *sk, struct list_head *skbs)
2172 {
2173 struct mptcp_sock *msk = mptcp_sk(sk);
2174
2175 /* After CG initialization, subflows should never add skb before
2176 * gaining the CG themself.
2177 */
2178 DEBUG_NET_WARN_ON_ONCE(msk->backlog_unaccounted && sk->sk_socket &&
2179 mem_cgroup_from_sk(sk));
2180
2181 /* Don't spool the backlog if the rcvbuf is full. */
2182 if (list_empty(&msk->backlog_list) ||
2183 sk_rmem_alloc_get(sk) > sk->sk_rcvbuf)
2184 return false;
2185
2186 INIT_LIST_HEAD(skbs);
2187 list_splice_init(&msk->backlog_list, skbs);
2188 return true;
2189 }
2190
mptcp_backlog_spooled(struct sock * sk,u32 moved,struct list_head * skbs)2191 static void mptcp_backlog_spooled(struct sock *sk, u32 moved,
2192 struct list_head *skbs)
2193 {
2194 struct mptcp_sock *msk = mptcp_sk(sk);
2195
2196 WRITE_ONCE(msk->backlog_len, msk->backlog_len - moved);
2197 list_splice(skbs, &msk->backlog_list);
2198 }
2199
mptcp_move_skbs(struct sock * sk)2200 static bool mptcp_move_skbs(struct sock *sk)
2201 {
2202 struct list_head skbs;
2203 bool enqueued = false;
2204 u32 moved;
2205
2206 mptcp_data_lock(sk);
2207 while (mptcp_can_spool_backlog(sk, &skbs)) {
2208 mptcp_data_unlock(sk);
2209 enqueued |= __mptcp_move_skbs(sk, &skbs, &moved);
2210
2211 mptcp_data_lock(sk);
2212 mptcp_backlog_spooled(sk, moved, &skbs);
2213 }
2214 mptcp_data_unlock(sk);
2215 return enqueued;
2216 }
2217
mptcp_inq_hint(const struct sock * sk)2218 static unsigned int mptcp_inq_hint(const struct sock *sk)
2219 {
2220 const struct mptcp_sock *msk = mptcp_sk(sk);
2221 const struct sk_buff *skb;
2222
2223 skb = skb_peek(&sk->sk_receive_queue);
2224 if (skb) {
2225 u64 hint_val = READ_ONCE(msk->ack_seq) - MPTCP_SKB_CB(skb)->map_seq;
2226
2227 if (hint_val >= INT_MAX)
2228 return INT_MAX;
2229
2230 return (unsigned int)hint_val;
2231 }
2232
2233 if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2234 return 1;
2235
2236 return 0;
2237 }
2238
mptcp_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int flags,int * addr_len)2239 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2240 int flags, int *addr_len)
2241 {
2242 struct mptcp_sock *msk = mptcp_sk(sk);
2243 struct scm_timestamping_internal tss;
2244 int copied = 0, cmsg_flags = 0;
2245 int target;
2246 long timeo;
2247
2248 /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2249 if (unlikely(flags & MSG_ERRQUEUE))
2250 return inet_recv_error(sk, msg, len, addr_len);
2251
2252 lock_sock(sk);
2253 if (unlikely(sk->sk_state == TCP_LISTEN)) {
2254 copied = -ENOTCONN;
2255 goto out_err;
2256 }
2257
2258 mptcp_rps_record_subflows(msk);
2259
2260 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2261
2262 len = min_t(size_t, len, INT_MAX);
2263 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2264
2265 if (unlikely(msk->recvmsg_inq))
2266 cmsg_flags = MPTCP_CMSG_INQ;
2267
2268 while (copied < len) {
2269 int err, bytes_read;
2270
2271 bytes_read = __mptcp_recvmsg_mskq(sk, msg, len - copied, flags,
2272 copied, &tss, &cmsg_flags);
2273 if (unlikely(bytes_read < 0)) {
2274 if (!copied)
2275 copied = bytes_read;
2276 goto out_err;
2277 }
2278
2279 copied += bytes_read;
2280
2281 if (!list_empty(&msk->backlog_list) && mptcp_move_skbs(sk))
2282 continue;
2283
2284 /* only the MPTCP socket status is relevant here. The exit
2285 * conditions mirror closely tcp_recvmsg()
2286 */
2287 if (copied >= target)
2288 break;
2289
2290 if (copied) {
2291 if (sk->sk_err ||
2292 sk->sk_state == TCP_CLOSE ||
2293 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2294 !timeo ||
2295 signal_pending(current))
2296 break;
2297 } else {
2298 if (sk->sk_err) {
2299 copied = sock_error(sk);
2300 break;
2301 }
2302
2303 if (sk->sk_shutdown & RCV_SHUTDOWN)
2304 break;
2305
2306 if (sk->sk_state == TCP_CLOSE) {
2307 copied = -ENOTCONN;
2308 break;
2309 }
2310
2311 if (!timeo) {
2312 copied = -EAGAIN;
2313 break;
2314 }
2315
2316 if (signal_pending(current)) {
2317 copied = sock_intr_errno(timeo);
2318 break;
2319 }
2320 }
2321
2322 pr_debug("block timeout %ld\n", timeo);
2323 mptcp_cleanup_rbuf(msk, copied);
2324 err = sk_wait_data(sk, &timeo, NULL);
2325 if (err < 0) {
2326 err = copied ? : err;
2327 goto out_err;
2328 }
2329 }
2330
2331 mptcp_cleanup_rbuf(msk, copied);
2332
2333 out_err:
2334 if (cmsg_flags && copied >= 0) {
2335 if (cmsg_flags & MPTCP_CMSG_TS)
2336 tcp_recv_timestamp(msg, sk, &tss);
2337
2338 if (cmsg_flags & MPTCP_CMSG_INQ) {
2339 unsigned int inq = mptcp_inq_hint(sk);
2340
2341 put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2342 }
2343 }
2344
2345 pr_debug("msk=%p rx queue empty=%d copied=%d\n",
2346 msk, skb_queue_empty(&sk->sk_receive_queue), copied);
2347
2348 release_sock(sk);
2349 return copied;
2350 }
2351
mptcp_retransmit_timer(struct timer_list * t)2352 static void mptcp_retransmit_timer(struct timer_list *t)
2353 {
2354 struct sock *sk = timer_container_of(sk, t, mptcp_retransmit_timer);
2355 struct mptcp_sock *msk = mptcp_sk(sk);
2356
2357 bh_lock_sock(sk);
2358 if (!sock_owned_by_user(sk)) {
2359 /* we need a process context to retransmit */
2360 if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2361 mptcp_schedule_work(sk);
2362 } else {
2363 /* delegate our work to tcp_release_cb() */
2364 __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
2365 }
2366 bh_unlock_sock(sk);
2367 sock_put(sk);
2368 }
2369
mptcp_tout_timer(struct timer_list * t)2370 static void mptcp_tout_timer(struct timer_list *t)
2371 {
2372 struct inet_connection_sock *icsk =
2373 timer_container_of(icsk, t, mptcp_tout_timer);
2374 struct sock *sk = &icsk->icsk_inet.sk;
2375
2376 mptcp_schedule_work(sk);
2377 sock_put(sk);
2378 }
2379
2380 /* Find an idle subflow. Return NULL if there is unacked data at tcp
2381 * level.
2382 *
2383 * A backup subflow is returned only if that is the only kind available.
2384 */
mptcp_subflow_get_retrans(struct mptcp_sock * msk)2385 struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2386 {
2387 struct sock *backup = NULL, *pick = NULL;
2388 struct mptcp_subflow_context *subflow;
2389 int min_stale_count = INT_MAX;
2390
2391 mptcp_for_each_subflow(msk, subflow) {
2392 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2393
2394 if (!__mptcp_subflow_active(subflow))
2395 continue;
2396
2397 /* still data outstanding at TCP level? skip this */
2398 if (!tcp_rtx_and_write_queues_empty(ssk)) {
2399 mptcp_pm_subflow_chk_stale(msk, ssk);
2400 min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2401 continue;
2402 }
2403
2404 if (subflow->backup || subflow->request_bkup) {
2405 if (!backup)
2406 backup = ssk;
2407 continue;
2408 }
2409
2410 if (!pick)
2411 pick = ssk;
2412 }
2413
2414 if (pick)
2415 return pick;
2416
2417 /* use backup only if there are no progresses anywhere */
2418 return min_stale_count > 1 ? backup : NULL;
2419 }
2420
__mptcp_retransmit_pending_data(struct sock * sk)2421 bool __mptcp_retransmit_pending_data(struct sock *sk)
2422 {
2423 struct mptcp_data_frag *cur, *rtx_head;
2424 struct mptcp_sock *msk = mptcp_sk(sk);
2425
2426 if (__mptcp_check_fallback(msk))
2427 return false;
2428
2429 /* the closing socket has some data untransmitted and/or unacked:
2430 * some data in the mptcp rtx queue has not really xmitted yet.
2431 * keep it simple and re-inject the whole mptcp level rtx queue
2432 */
2433 mptcp_data_lock(sk);
2434 __mptcp_clean_una_wakeup(sk);
2435 rtx_head = mptcp_rtx_head(sk);
2436 if (!rtx_head) {
2437 mptcp_data_unlock(sk);
2438 return false;
2439 }
2440
2441 msk->recovery_snd_nxt = msk->snd_nxt;
2442 msk->recovery = true;
2443 mptcp_data_unlock(sk);
2444
2445 msk->first_pending = rtx_head;
2446 msk->snd_burst = 0;
2447
2448 /* be sure to clear the "sent status" on all re-injected fragments */
2449 list_for_each_entry(cur, &msk->rtx_queue, list) {
2450 if (!cur->already_sent)
2451 break;
2452 cur->already_sent = 0;
2453 }
2454
2455 return true;
2456 }
2457
2458 /* flags for __mptcp_close_ssk() */
2459 #define MPTCP_CF_PUSH BIT(1)
2460
2461 /* be sure to send a reset only if the caller asked for it, also
2462 * clean completely the subflow status when the subflow reaches
2463 * TCP_CLOSE state
2464 */
__mptcp_subflow_disconnect(struct sock * ssk,struct mptcp_subflow_context * subflow,unsigned int flags)2465 static void __mptcp_subflow_disconnect(struct sock *ssk,
2466 struct mptcp_subflow_context *subflow,
2467 unsigned int flags)
2468 {
2469 if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
2470 subflow->send_fastclose) {
2471 /* The MPTCP code never wait on the subflow sockets, TCP-level
2472 * disconnect should never fail
2473 */
2474 WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2475 mptcp_subflow_ctx_reset(subflow);
2476 } else {
2477 tcp_shutdown(ssk, SEND_SHUTDOWN);
2478 }
2479 }
2480
2481 /* subflow sockets can be either outgoing (connect) or incoming
2482 * (accept).
2483 *
2484 * Outgoing subflows use in-kernel sockets.
2485 * Incoming subflows do not have their own 'struct socket' allocated,
2486 * so we need to use tcp_close() after detaching them from the mptcp
2487 * parent socket.
2488 */
__mptcp_close_ssk(struct sock * sk,struct sock * ssk,struct mptcp_subflow_context * subflow,unsigned int flags)2489 static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2490 struct mptcp_subflow_context *subflow,
2491 unsigned int flags)
2492 {
2493 struct mptcp_sock *msk = mptcp_sk(sk);
2494 bool dispose_it, need_push = false;
2495 int fwd_remaining;
2496
2497 /* Do not pass RX data to the msk, even if the subflow socket is not
2498 * going to be freed (i.e. even for the first subflow on graceful
2499 * subflow close.
2500 */
2501 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2502 subflow->closing = 1;
2503
2504 /* Borrow the fwd allocated page left-over; fwd memory for the subflow
2505 * could be negative at this point, but will be reach zero soon - when
2506 * the data allocated using such fragment will be freed.
2507 */
2508 if (subflow->lent_mem_frag) {
2509 fwd_remaining = PAGE_SIZE - subflow->lent_mem_frag;
2510 sk_forward_alloc_add(sk, fwd_remaining);
2511 sk_forward_alloc_add(ssk, -fwd_remaining);
2512 subflow->lent_mem_frag = 0;
2513 }
2514
2515 /* If the first subflow moved to a close state before accept, e.g. due
2516 * to an incoming reset or listener shutdown, the subflow socket is
2517 * already deleted by inet_child_forget() and the mptcp socket can't
2518 * survive too.
2519 */
2520 if (msk->in_accept_queue && msk->first == ssk &&
2521 (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) {
2522 /* ensure later check in mptcp_worker() will dispose the msk */
2523 sock_set_flag(sk, SOCK_DEAD);
2524 mptcp_set_close_tout(sk, tcp_jiffies32 - (mptcp_close_timeout(sk) + 1));
2525 mptcp_subflow_drop_ctx(ssk);
2526 goto out_release;
2527 }
2528
2529 dispose_it = msk->free_first || ssk != msk->first;
2530 if (dispose_it)
2531 list_del(&subflow->node);
2532
2533 if (subflow->send_fastclose && ssk->sk_state != TCP_CLOSE)
2534 tcp_set_state(ssk, TCP_CLOSE);
2535
2536 need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2537 if (!dispose_it) {
2538 __mptcp_subflow_disconnect(ssk, subflow, flags);
2539 release_sock(ssk);
2540
2541 goto out;
2542 }
2543
2544 subflow->disposable = 1;
2545
2546 /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2547 * the ssk has been already destroyed, we just need to release the
2548 * reference owned by msk;
2549 */
2550 if (!inet_csk(ssk)->icsk_ulp_ops) {
2551 WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2552 kfree_rcu(subflow, rcu);
2553 } else {
2554 /* otherwise tcp will dispose of the ssk and subflow ctx */
2555 __tcp_close(ssk, 0);
2556
2557 /* close acquired an extra ref */
2558 __sock_put(ssk);
2559 }
2560
2561 out_release:
2562 __mptcp_subflow_error_report(sk, ssk);
2563 release_sock(ssk);
2564
2565 sock_put(ssk);
2566
2567 if (ssk == msk->first)
2568 WRITE_ONCE(msk->first, NULL);
2569
2570 out:
2571 __mptcp_sync_sndbuf(sk);
2572 if (need_push)
2573 __mptcp_push_pending(sk, 0);
2574
2575 /* Catch every 'all subflows closed' scenario, including peers silently
2576 * closing them, e.g. due to timeout.
2577 * For established sockets, allow an additional timeout before closing,
2578 * as the protocol can still create more subflows.
2579 */
2580 if (list_is_singular(&msk->conn_list) && msk->first &&
2581 inet_sk_state_load(msk->first) == TCP_CLOSE) {
2582 if (sk->sk_state != TCP_ESTABLISHED ||
2583 msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
2584 mptcp_set_state(sk, TCP_CLOSE);
2585 mptcp_close_wake_up(sk);
2586 } else {
2587 mptcp_start_tout_timer(sk);
2588 }
2589 }
2590 }
2591
mptcp_close_ssk(struct sock * sk,struct sock * ssk,struct mptcp_subflow_context * subflow)2592 void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2593 struct mptcp_subflow_context *subflow)
2594 {
2595 struct mptcp_sock *msk = mptcp_sk(sk);
2596 struct sk_buff *skb;
2597
2598 /* The first subflow can already be closed and still in the list */
2599 if (subflow->close_event_done)
2600 return;
2601
2602 subflow->close_event_done = true;
2603
2604 if (sk->sk_state == TCP_ESTABLISHED)
2605 mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2606
2607 /* Remove any reference from the backlog to this ssk; backlog skbs consume
2608 * space in the msk receive queue, no need to touch sk->sk_rmem_alloc
2609 */
2610 list_for_each_entry(skb, &msk->backlog_list, list) {
2611 if (skb->sk != ssk)
2612 continue;
2613
2614 atomic_sub(skb->truesize, &skb->sk->sk_rmem_alloc);
2615 skb->sk = NULL;
2616 }
2617
2618 /* subflow aborted before reaching the fully_established status
2619 * attempt the creation of the next subflow
2620 */
2621 mptcp_pm_subflow_check_next(mptcp_sk(sk), subflow);
2622
2623 __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2624 }
2625
mptcp_sync_mss(struct sock * sk,u32 pmtu)2626 static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2627 {
2628 return 0;
2629 }
2630
__mptcp_close_subflow(struct sock * sk)2631 static void __mptcp_close_subflow(struct sock *sk)
2632 {
2633 struct mptcp_subflow_context *subflow, *tmp;
2634 struct mptcp_sock *msk = mptcp_sk(sk);
2635
2636 might_sleep();
2637
2638 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2639 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2640 int ssk_state = inet_sk_state_load(ssk);
2641
2642 if (ssk_state != TCP_CLOSE &&
2643 (ssk_state != TCP_CLOSE_WAIT ||
2644 inet_sk_state_load(sk) != TCP_ESTABLISHED ||
2645 __mptcp_check_fallback(msk)))
2646 continue;
2647
2648 /* 'subflow_data_ready' will re-sched once rx queue is empty */
2649 if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2650 continue;
2651
2652 mptcp_close_ssk(sk, ssk, subflow);
2653 }
2654
2655 }
2656
mptcp_close_tout_expired(const struct sock * sk)2657 static bool mptcp_close_tout_expired(const struct sock *sk)
2658 {
2659 if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
2660 sk->sk_state == TCP_CLOSE)
2661 return false;
2662
2663 return time_after32(tcp_jiffies32,
2664 inet_csk(sk)->icsk_mtup.probe_timestamp + mptcp_close_timeout(sk));
2665 }
2666
mptcp_check_fastclose(struct mptcp_sock * msk)2667 static void mptcp_check_fastclose(struct mptcp_sock *msk)
2668 {
2669 struct mptcp_subflow_context *subflow, *tmp;
2670 struct sock *sk = (struct sock *)msk;
2671
2672 if (likely(!READ_ONCE(msk->rcv_fastclose)))
2673 return;
2674
2675 mptcp_token_destroy(msk);
2676
2677 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2678 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2679 bool slow;
2680
2681 slow = lock_sock_fast(tcp_sk);
2682 if (tcp_sk->sk_state != TCP_CLOSE) {
2683 mptcp_send_active_reset_reason(tcp_sk);
2684 tcp_set_state(tcp_sk, TCP_CLOSE);
2685 }
2686 unlock_sock_fast(tcp_sk, slow);
2687 }
2688
2689 /* Mirror the tcp_reset() error propagation */
2690 switch (sk->sk_state) {
2691 case TCP_SYN_SENT:
2692 WRITE_ONCE(sk->sk_err, ECONNREFUSED);
2693 break;
2694 case TCP_CLOSE_WAIT:
2695 WRITE_ONCE(sk->sk_err, EPIPE);
2696 break;
2697 case TCP_CLOSE:
2698 return;
2699 default:
2700 WRITE_ONCE(sk->sk_err, ECONNRESET);
2701 }
2702
2703 mptcp_set_state(sk, TCP_CLOSE);
2704 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2705 smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2706 set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2707
2708 /* the calling mptcp_worker will properly destroy the socket */
2709 if (sock_flag(sk, SOCK_DEAD))
2710 return;
2711
2712 sk->sk_state_change(sk);
2713 sk_error_report(sk);
2714 }
2715
__mptcp_retrans(struct sock * sk)2716 static void __mptcp_retrans(struct sock *sk)
2717 {
2718 struct mptcp_sendmsg_info info = { .data_lock_held = true, };
2719 struct mptcp_sock *msk = mptcp_sk(sk);
2720 struct mptcp_subflow_context *subflow;
2721 struct mptcp_data_frag *dfrag;
2722 struct sock *ssk;
2723 int ret, err;
2724 u16 len = 0;
2725
2726 mptcp_clean_una_wakeup(sk);
2727
2728 /* first check ssk: need to kick "stale" logic */
2729 err = mptcp_sched_get_retrans(msk);
2730 dfrag = mptcp_rtx_head(sk);
2731 if (!dfrag) {
2732 if (mptcp_data_fin_enabled(msk)) {
2733 struct inet_connection_sock *icsk = inet_csk(sk);
2734
2735 WRITE_ONCE(icsk->icsk_retransmits,
2736 icsk->icsk_retransmits + 1);
2737 mptcp_set_datafin_timeout(sk);
2738 mptcp_send_ack(msk);
2739
2740 goto reset_timer;
2741 }
2742
2743 if (!mptcp_send_head(sk))
2744 goto clear_scheduled;
2745
2746 goto reset_timer;
2747 }
2748
2749 if (err)
2750 goto reset_timer;
2751
2752 mptcp_for_each_subflow(msk, subflow) {
2753 if (READ_ONCE(subflow->scheduled)) {
2754 u16 copied = 0;
2755
2756 mptcp_subflow_set_scheduled(subflow, false);
2757
2758 ssk = mptcp_subflow_tcp_sock(subflow);
2759
2760 lock_sock(ssk);
2761
2762 /* limit retransmission to the bytes already sent on some subflows */
2763 info.sent = 0;
2764 info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
2765 dfrag->already_sent;
2766
2767 /*
2768 * make the whole retrans decision, xmit, disallow
2769 * fallback atomic
2770 */
2771 spin_lock_bh(&msk->fallback_lock);
2772 if (__mptcp_check_fallback(msk)) {
2773 spin_unlock_bh(&msk->fallback_lock);
2774 release_sock(ssk);
2775 goto clear_scheduled;
2776 }
2777
2778 while (info.sent < info.limit) {
2779 ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2780 if (ret <= 0)
2781 break;
2782
2783 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2784 copied += ret;
2785 info.sent += ret;
2786 }
2787 if (copied) {
2788 len = max(copied, len);
2789 tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2790 info.size_goal);
2791 msk->allow_infinite_fallback = false;
2792 }
2793 spin_unlock_bh(&msk->fallback_lock);
2794
2795 release_sock(ssk);
2796 }
2797 }
2798
2799 msk->bytes_retrans += len;
2800 dfrag->already_sent = max(dfrag->already_sent, len);
2801
2802 reset_timer:
2803 mptcp_check_and_set_pending(sk);
2804
2805 if (!mptcp_rtx_timer_pending(sk))
2806 mptcp_reset_rtx_timer(sk);
2807
2808 clear_scheduled:
2809 /* If no rtx data was available or in case of fallback, there
2810 * could be left-over scheduled subflows; clear them all
2811 * or later xmit could use bad ones
2812 */
2813 mptcp_for_each_subflow(msk, subflow)
2814 if (READ_ONCE(subflow->scheduled))
2815 mptcp_subflow_set_scheduled(subflow, false);
2816 }
2817
2818 /* schedule the timeout timer for the relevant event: either close timeout
2819 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2820 */
mptcp_reset_tout_timer(struct mptcp_sock * msk,unsigned long fail_tout)2821 void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
2822 {
2823 struct sock *sk = (struct sock *)msk;
2824 unsigned long timeout, close_timeout;
2825
2826 if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
2827 return;
2828
2829 close_timeout = (unsigned long)inet_csk(sk)->icsk_mtup.probe_timestamp -
2830 tcp_jiffies32 + jiffies + mptcp_close_timeout(sk);
2831
2832 /* the close timeout takes precedence on the fail one, and here at least one of
2833 * them is active
2834 */
2835 timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
2836
2837 sk_reset_timer(sk, &inet_csk(sk)->mptcp_tout_timer, timeout);
2838 }
2839
mptcp_mp_fail_no_response(struct mptcp_sock * msk)2840 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2841 {
2842 struct sock *ssk = msk->first;
2843 bool slow;
2844
2845 if (!ssk)
2846 return;
2847
2848 pr_debug("MP_FAIL doesn't respond, reset the subflow\n");
2849
2850 slow = lock_sock_fast(ssk);
2851 mptcp_subflow_reset(ssk);
2852 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2853 unlock_sock_fast(ssk, slow);
2854 }
2855
mptcp_backlog_purge(struct sock * sk)2856 static void mptcp_backlog_purge(struct sock *sk)
2857 {
2858 struct mptcp_sock *msk = mptcp_sk(sk);
2859 struct sk_buff *tmp, *skb;
2860 LIST_HEAD(backlog);
2861
2862 mptcp_data_lock(sk);
2863 list_splice_init(&msk->backlog_list, &backlog);
2864 msk->backlog_len = 0;
2865 mptcp_data_unlock(sk);
2866
2867 list_for_each_entry_safe(skb, tmp, &backlog, list) {
2868 mptcp_borrow_fwdmem(sk, skb);
2869 kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_CLOSE);
2870 }
2871 sk_mem_reclaim(sk);
2872 }
2873
mptcp_do_fastclose(struct sock * sk)2874 static void mptcp_do_fastclose(struct sock *sk)
2875 {
2876 struct mptcp_subflow_context *subflow, *tmp;
2877 struct mptcp_sock *msk = mptcp_sk(sk);
2878
2879 mptcp_set_state(sk, TCP_CLOSE);
2880 mptcp_backlog_purge(sk);
2881
2882 /* Explicitly send the fastclose reset as need */
2883 if (__mptcp_check_fallback(msk))
2884 return;
2885
2886 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2887 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2888
2889 lock_sock(ssk);
2890
2891 /* Some subflow socket states don't allow/need a reset.*/
2892 if ((1 << ssk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
2893 goto unlock;
2894
2895 subflow->send_fastclose = 1;
2896
2897 /* Initialize rcv_mss to TCP_MIN_MSS to avoid division by 0
2898 * issue in __tcp_select_window(), see tcp_disconnect().
2899 */
2900 inet_csk(ssk)->icsk_ack.rcv_mss = TCP_MIN_MSS;
2901
2902 tcp_send_active_reset(ssk, ssk->sk_allocation,
2903 SK_RST_REASON_TCP_ABORT_ON_CLOSE);
2904 unlock:
2905 release_sock(ssk);
2906 }
2907 }
2908
mptcp_worker(struct work_struct * work)2909 static void mptcp_worker(struct work_struct *work)
2910 {
2911 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2912 struct sock *sk = (struct sock *)msk;
2913 unsigned long fail_tout;
2914 int state;
2915
2916 lock_sock(sk);
2917 state = sk->sk_state;
2918 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2919 goto unlock;
2920
2921 mptcp_check_fastclose(msk);
2922
2923 mptcp_pm_worker(msk);
2924
2925 mptcp_check_send_data_fin(sk);
2926 mptcp_check_data_fin_ack(sk);
2927 mptcp_check_data_fin(sk);
2928
2929 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2930 __mptcp_close_subflow(sk);
2931
2932 if (mptcp_close_tout_expired(sk)) {
2933 struct mptcp_subflow_context *subflow, *tmp;
2934
2935 mptcp_do_fastclose(sk);
2936 mptcp_for_each_subflow_safe(msk, subflow, tmp)
2937 __mptcp_close_ssk(sk, subflow->tcp_sock, subflow, 0);
2938 mptcp_close_wake_up(sk);
2939 }
2940
2941 if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
2942 __mptcp_destroy_sock(sk);
2943 goto unlock;
2944 }
2945
2946 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2947 __mptcp_retrans(sk);
2948
2949 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2950 if (fail_tout && time_after(jiffies, fail_tout))
2951 mptcp_mp_fail_no_response(msk);
2952
2953 unlock:
2954 release_sock(sk);
2955 sock_put(sk);
2956 }
2957
__mptcp_init_sock(struct sock * sk)2958 static void __mptcp_init_sock(struct sock *sk)
2959 {
2960 struct mptcp_sock *msk = mptcp_sk(sk);
2961
2962 INIT_LIST_HEAD(&msk->conn_list);
2963 INIT_LIST_HEAD(&msk->join_list);
2964 INIT_LIST_HEAD(&msk->rtx_queue);
2965 INIT_LIST_HEAD(&msk->backlog_list);
2966 INIT_WORK(&msk->work, mptcp_worker);
2967 msk->out_of_order_queue = RB_ROOT;
2968 msk->first_pending = NULL;
2969 msk->timer_ival = TCP_RTO_MIN;
2970 msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
2971 msk->backlog_len = 0;
2972
2973 WRITE_ONCE(msk->first, NULL);
2974 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2975 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2976 msk->allow_infinite_fallback = true;
2977 msk->allow_subflows = true;
2978 msk->recovery = false;
2979 msk->subflow_id = 1;
2980 msk->last_data_sent = tcp_jiffies32;
2981 msk->last_data_recv = tcp_jiffies32;
2982 msk->last_ack_recv = tcp_jiffies32;
2983
2984 mptcp_pm_data_init(msk);
2985 spin_lock_init(&msk->fallback_lock);
2986
2987 /* re-use the csk retrans timer for MPTCP-level retrans */
2988 timer_setup(&sk->mptcp_retransmit_timer, mptcp_retransmit_timer, 0);
2989 timer_setup(&msk->sk.mptcp_tout_timer, mptcp_tout_timer, 0);
2990 }
2991
mptcp_ca_reset(struct sock * sk)2992 static void mptcp_ca_reset(struct sock *sk)
2993 {
2994 struct inet_connection_sock *icsk = inet_csk(sk);
2995
2996 tcp_assign_congestion_control(sk);
2997 strscpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name,
2998 sizeof(mptcp_sk(sk)->ca_name));
2999
3000 /* no need to keep a reference to the ops, the name will suffice */
3001 tcp_cleanup_congestion_control(sk);
3002 icsk->icsk_ca_ops = NULL;
3003 }
3004
mptcp_init_sock(struct sock * sk)3005 static int mptcp_init_sock(struct sock *sk)
3006 {
3007 struct net *net = sock_net(sk);
3008 int ret;
3009
3010 __mptcp_init_sock(sk);
3011
3012 if (!mptcp_is_enabled(net))
3013 return -ENOPROTOOPT;
3014
3015 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
3016 return -ENOMEM;
3017
3018 rcu_read_lock();
3019 ret = mptcp_init_sched(mptcp_sk(sk),
3020 mptcp_sched_find(mptcp_get_scheduler(net)));
3021 rcu_read_unlock();
3022 if (ret)
3023 return ret;
3024
3025 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
3026
3027 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
3028 * propagate the correct value
3029 */
3030 mptcp_ca_reset(sk);
3031
3032 sk_sockets_allocated_inc(sk);
3033 sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
3034 sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
3035
3036 return 0;
3037 }
3038
__mptcp_clear_xmit(struct sock * sk)3039 static void __mptcp_clear_xmit(struct sock *sk)
3040 {
3041 struct mptcp_sock *msk = mptcp_sk(sk);
3042 struct mptcp_data_frag *dtmp, *dfrag;
3043
3044 msk->first_pending = NULL;
3045 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
3046 dfrag_clear(sk, dfrag);
3047 }
3048
mptcp_cancel_work(struct sock * sk)3049 void mptcp_cancel_work(struct sock *sk)
3050 {
3051 struct mptcp_sock *msk = mptcp_sk(sk);
3052
3053 if (cancel_work_sync(&msk->work))
3054 __sock_put(sk);
3055 }
3056
mptcp_subflow_shutdown(struct sock * sk,struct sock * ssk,int how)3057 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
3058 {
3059 lock_sock(ssk);
3060
3061 switch (ssk->sk_state) {
3062 case TCP_LISTEN:
3063 if (!(how & RCV_SHUTDOWN))
3064 break;
3065 fallthrough;
3066 case TCP_SYN_SENT:
3067 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
3068 break;
3069 default:
3070 if (__mptcp_check_fallback(mptcp_sk(sk))) {
3071 pr_debug("Fallback\n");
3072 ssk->sk_shutdown |= how;
3073 tcp_shutdown(ssk, how);
3074
3075 /* simulate the data_fin ack reception to let the state
3076 * machine move forward
3077 */
3078 WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
3079 mptcp_schedule_work(sk);
3080 } else {
3081 pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
3082 tcp_send_ack(ssk);
3083 if (!mptcp_rtx_timer_pending(sk))
3084 mptcp_reset_rtx_timer(sk);
3085 }
3086 break;
3087 }
3088
3089 release_sock(ssk);
3090 }
3091
mptcp_set_state(struct sock * sk,int state)3092 void mptcp_set_state(struct sock *sk, int state)
3093 {
3094 int oldstate = sk->sk_state;
3095
3096 switch (state) {
3097 case TCP_ESTABLISHED:
3098 if (oldstate != TCP_ESTABLISHED)
3099 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
3100 break;
3101 case TCP_CLOSE_WAIT:
3102 /* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
3103 * MPTCP "accepted" sockets will be created later on. So no
3104 * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
3105 */
3106 break;
3107 default:
3108 if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
3109 MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
3110 }
3111
3112 inet_sk_state_store(sk, state);
3113 }
3114
3115 static const unsigned char new_state[16] = {
3116 /* current state: new state: action: */
3117 [0 /* (Invalid) */] = TCP_CLOSE,
3118 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
3119 [TCP_SYN_SENT] = TCP_CLOSE,
3120 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
3121 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
3122 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
3123 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
3124 [TCP_CLOSE] = TCP_CLOSE,
3125 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
3126 [TCP_LAST_ACK] = TCP_LAST_ACK,
3127 [TCP_LISTEN] = TCP_CLOSE,
3128 [TCP_CLOSING] = TCP_CLOSING,
3129 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
3130 };
3131
mptcp_close_state(struct sock * sk)3132 static int mptcp_close_state(struct sock *sk)
3133 {
3134 int next = (int)new_state[sk->sk_state];
3135 int ns = next & TCP_STATE_MASK;
3136
3137 mptcp_set_state(sk, ns);
3138
3139 return next & TCP_ACTION_FIN;
3140 }
3141
mptcp_check_send_data_fin(struct sock * sk)3142 static void mptcp_check_send_data_fin(struct sock *sk)
3143 {
3144 struct mptcp_subflow_context *subflow;
3145 struct mptcp_sock *msk = mptcp_sk(sk);
3146
3147 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
3148 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
3149 msk->snd_nxt, msk->write_seq);
3150
3151 /* we still need to enqueue subflows or not really shutting down,
3152 * skip this
3153 */
3154 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
3155 mptcp_send_head(sk))
3156 return;
3157
3158 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3159
3160 mptcp_for_each_subflow(msk, subflow) {
3161 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
3162
3163 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
3164 }
3165 }
3166
__mptcp_wr_shutdown(struct sock * sk)3167 static void __mptcp_wr_shutdown(struct sock *sk)
3168 {
3169 struct mptcp_sock *msk = mptcp_sk(sk);
3170
3171 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
3172 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
3173 !!mptcp_send_head(sk));
3174
3175 /* will be ignored by fallback sockets */
3176 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
3177 WRITE_ONCE(msk->snd_data_fin_enable, 1);
3178
3179 mptcp_check_send_data_fin(sk);
3180 }
3181
__mptcp_destroy_sock(struct sock * sk)3182 static void __mptcp_destroy_sock(struct sock *sk)
3183 {
3184 struct mptcp_sock *msk = mptcp_sk(sk);
3185
3186 pr_debug("msk=%p\n", msk);
3187
3188 might_sleep();
3189
3190 mptcp_stop_rtx_timer(sk);
3191 sk_stop_timer(sk, &inet_csk(sk)->mptcp_tout_timer);
3192 msk->pm.status = 0;
3193 mptcp_release_sched(msk);
3194
3195 sk->sk_prot->destroy(sk);
3196
3197 sk_stream_kill_queues(sk);
3198 xfrm_sk_free_policy(sk);
3199
3200 sock_put(sk);
3201 }
3202
__mptcp_unaccepted_force_close(struct sock * sk)3203 void __mptcp_unaccepted_force_close(struct sock *sk)
3204 {
3205 sock_set_flag(sk, SOCK_DEAD);
3206 mptcp_do_fastclose(sk);
3207 __mptcp_destroy_sock(sk);
3208 }
3209
mptcp_check_readable(struct sock * sk)3210 static __poll_t mptcp_check_readable(struct sock *sk)
3211 {
3212 return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
3213 }
3214
mptcp_check_listen_stop(struct sock * sk)3215 static void mptcp_check_listen_stop(struct sock *sk)
3216 {
3217 struct sock *ssk;
3218
3219 if (inet_sk_state_load(sk) != TCP_LISTEN)
3220 return;
3221
3222 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
3223 ssk = mptcp_sk(sk)->first;
3224 if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
3225 return;
3226
3227 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
3228 tcp_set_state(ssk, TCP_CLOSE);
3229 mptcp_subflow_queue_clean(sk, ssk);
3230 inet_csk_listen_stop(ssk);
3231 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
3232 release_sock(ssk);
3233 }
3234
__mptcp_close(struct sock * sk,long timeout)3235 bool __mptcp_close(struct sock *sk, long timeout)
3236 {
3237 struct mptcp_subflow_context *subflow;
3238 struct mptcp_sock *msk = mptcp_sk(sk);
3239 bool do_cancel_work = false;
3240 int subflows_alive = 0;
3241
3242 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
3243
3244 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
3245 mptcp_check_listen_stop(sk);
3246 mptcp_set_state(sk, TCP_CLOSE);
3247 goto cleanup;
3248 }
3249
3250 if (mptcp_data_avail(msk) || timeout < 0) {
3251 /* If the msk has read data, or the caller explicitly ask it,
3252 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
3253 */
3254 mptcp_do_fastclose(sk);
3255 timeout = 0;
3256 } else if (mptcp_close_state(sk)) {
3257 __mptcp_wr_shutdown(sk);
3258 }
3259
3260 sk_stream_wait_close(sk, timeout);
3261
3262 cleanup:
3263 /* orphan all the subflows */
3264 mptcp_for_each_subflow(msk, subflow) {
3265 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3266 bool slow = lock_sock_fast_nested(ssk);
3267
3268 subflows_alive += ssk->sk_state != TCP_CLOSE;
3269
3270 /* since the close timeout takes precedence on the fail one,
3271 * cancel the latter
3272 */
3273 if (ssk == msk->first)
3274 subflow->fail_tout = 0;
3275
3276 /* detach from the parent socket, but allow data_ready to
3277 * push incoming data into the mptcp stack, to properly ack it
3278 */
3279 ssk->sk_socket = NULL;
3280 ssk->sk_wq = NULL;
3281 unlock_sock_fast(ssk, slow);
3282 }
3283 sock_orphan(sk);
3284
3285 /* all the subflows are closed, only timeout can change the msk
3286 * state, let's not keep resources busy for no reasons
3287 */
3288 if (subflows_alive == 0)
3289 mptcp_set_state(sk, TCP_CLOSE);
3290
3291 sock_hold(sk);
3292 pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
3293 mptcp_pm_connection_closed(msk);
3294
3295 if (sk->sk_state == TCP_CLOSE) {
3296 __mptcp_destroy_sock(sk);
3297 do_cancel_work = true;
3298 } else {
3299 mptcp_start_tout_timer(sk);
3300 }
3301
3302 return do_cancel_work;
3303 }
3304
mptcp_close(struct sock * sk,long timeout)3305 static void mptcp_close(struct sock *sk, long timeout)
3306 {
3307 bool do_cancel_work;
3308
3309 lock_sock(sk);
3310
3311 do_cancel_work = __mptcp_close(sk, timeout);
3312 release_sock(sk);
3313 if (do_cancel_work)
3314 mptcp_cancel_work(sk);
3315
3316 sock_put(sk);
3317 }
3318
mptcp_copy_inaddrs(struct sock * msk,const struct sock * ssk)3319 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3320 {
3321 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3322 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3323 struct ipv6_pinfo *msk6 = inet6_sk(msk);
3324
3325 msk->sk_v6_daddr = ssk->sk_v6_daddr;
3326 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3327
3328 if (msk6 && ssk6) {
3329 msk6->saddr = ssk6->saddr;
3330 msk6->flow_label = ssk6->flow_label;
3331 }
3332 #endif
3333
3334 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3335 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3336 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3337 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3338 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3339 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3340 }
3341
mptcp_destroy_common(struct mptcp_sock * msk)3342 static void mptcp_destroy_common(struct mptcp_sock *msk)
3343 {
3344 struct mptcp_subflow_context *subflow, *tmp;
3345 struct sock *sk = (struct sock *)msk;
3346
3347 __mptcp_clear_xmit(sk);
3348 mptcp_backlog_purge(sk);
3349
3350 /* join list will be eventually flushed (with rst) at sock lock release time */
3351 mptcp_for_each_subflow_safe(msk, subflow, tmp)
3352 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, 0);
3353
3354 __skb_queue_purge(&sk->sk_receive_queue);
3355 skb_rbtree_purge(&msk->out_of_order_queue);
3356
3357 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3358 * inet_sock_destruct() will dispose it
3359 */
3360 mptcp_token_destroy(msk);
3361 mptcp_pm_destroy(msk);
3362 }
3363
mptcp_disconnect(struct sock * sk,int flags)3364 static int mptcp_disconnect(struct sock *sk, int flags)
3365 {
3366 struct mptcp_sock *msk = mptcp_sk(sk);
3367
3368 /* We are on the fastopen error path. We can't call straight into the
3369 * subflows cleanup code due to lock nesting (we are already under
3370 * msk->firstsocket lock).
3371 */
3372 if (msk->fastopening)
3373 return -EBUSY;
3374
3375 mptcp_check_listen_stop(sk);
3376 mptcp_set_state(sk, TCP_CLOSE);
3377
3378 mptcp_stop_rtx_timer(sk);
3379 mptcp_stop_tout_timer(sk);
3380
3381 mptcp_pm_connection_closed(msk);
3382
3383 /* msk->subflow is still intact, the following will not free the first
3384 * subflow
3385 */
3386 mptcp_do_fastclose(sk);
3387 mptcp_destroy_common(msk);
3388
3389 /* The first subflow is already in TCP_CLOSE status, the following
3390 * can't overlap with a fallback anymore
3391 */
3392 spin_lock_bh(&msk->fallback_lock);
3393 msk->allow_subflows = true;
3394 msk->allow_infinite_fallback = true;
3395 WRITE_ONCE(msk->flags, 0);
3396 spin_unlock_bh(&msk->fallback_lock);
3397
3398 msk->cb_flags = 0;
3399 msk->recovery = false;
3400 WRITE_ONCE(msk->can_ack, false);
3401 WRITE_ONCE(msk->fully_established, false);
3402 WRITE_ONCE(msk->rcv_data_fin, false);
3403 WRITE_ONCE(msk->snd_data_fin_enable, false);
3404 WRITE_ONCE(msk->rcv_fastclose, false);
3405 WRITE_ONCE(msk->use_64bit_ack, false);
3406 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3407 mptcp_pm_data_reset(msk);
3408 mptcp_ca_reset(sk);
3409 msk->bytes_consumed = 0;
3410 msk->bytes_acked = 0;
3411 msk->bytes_received = 0;
3412 msk->bytes_sent = 0;
3413 msk->bytes_retrans = 0;
3414 msk->rcvspace_init = 0;
3415
3416 /* for fallback's sake */
3417 WRITE_ONCE(msk->ack_seq, 0);
3418
3419 WRITE_ONCE(sk->sk_shutdown, 0);
3420 sk_error_report(sk);
3421 return 0;
3422 }
3423
3424 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
mptcp_inet6_sk(const struct sock * sk)3425 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3426 {
3427 struct mptcp6_sock *msk6 = container_of(mptcp_sk(sk), struct mptcp6_sock, msk);
3428
3429 return &msk6->np;
3430 }
3431
mptcp_copy_ip6_options(struct sock * newsk,const struct sock * sk)3432 static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
3433 {
3434 const struct ipv6_pinfo *np = inet6_sk(sk);
3435 struct ipv6_txoptions *opt;
3436 struct ipv6_pinfo *newnp;
3437
3438 newnp = inet6_sk(newsk);
3439
3440 rcu_read_lock();
3441 opt = rcu_dereference(np->opt);
3442 if (opt) {
3443 opt = ipv6_dup_options(newsk, opt);
3444 if (!opt)
3445 net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
3446 }
3447 RCU_INIT_POINTER(newnp->opt, opt);
3448 rcu_read_unlock();
3449 }
3450 #endif
3451
mptcp_copy_ip_options(struct sock * newsk,const struct sock * sk)3452 static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
3453 {
3454 struct ip_options_rcu *inet_opt, *newopt = NULL;
3455 const struct inet_sock *inet = inet_sk(sk);
3456 struct inet_sock *newinet;
3457
3458 newinet = inet_sk(newsk);
3459
3460 rcu_read_lock();
3461 inet_opt = rcu_dereference(inet->inet_opt);
3462 if (inet_opt) {
3463 newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) +
3464 inet_opt->opt.optlen, GFP_ATOMIC);
3465 if (!newopt)
3466 net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
3467 }
3468 RCU_INIT_POINTER(newinet->inet_opt, newopt);
3469 rcu_read_unlock();
3470 }
3471
mptcp_sk_clone_init(const struct sock * sk,const struct mptcp_options_received * mp_opt,struct sock * ssk,struct request_sock * req)3472 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3473 const struct mptcp_options_received *mp_opt,
3474 struct sock *ssk,
3475 struct request_sock *req)
3476 {
3477 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3478 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3479 struct mptcp_subflow_context *subflow;
3480 struct mptcp_sock *msk;
3481
3482 if (!nsk)
3483 return NULL;
3484
3485 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3486 if (nsk->sk_family == AF_INET6)
3487 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3488 #endif
3489
3490 __mptcp_init_sock(nsk);
3491
3492 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3493 if (nsk->sk_family == AF_INET6)
3494 mptcp_copy_ip6_options(nsk, sk);
3495 else
3496 #endif
3497 mptcp_copy_ip_options(nsk, sk);
3498
3499 msk = mptcp_sk(nsk);
3500 WRITE_ONCE(msk->local_key, subflow_req->local_key);
3501 WRITE_ONCE(msk->token, subflow_req->token);
3502 msk->in_accept_queue = 1;
3503 WRITE_ONCE(msk->fully_established, false);
3504 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3505 WRITE_ONCE(msk->csum_enabled, true);
3506
3507 WRITE_ONCE(msk->write_seq, subflow_req->idsn + 1);
3508 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3509 WRITE_ONCE(msk->snd_una, msk->write_seq);
3510 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3511 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3512 mptcp_init_sched(msk, mptcp_sk(sk)->sched);
3513
3514 /* passive msk is created after the first/MPC subflow */
3515 msk->subflow_id = 2;
3516
3517 sock_reset_flag(nsk, SOCK_RCU_FREE);
3518 security_inet_csk_clone(nsk, req);
3519
3520 /* this can't race with mptcp_close(), as the msk is
3521 * not yet exposted to user-space
3522 */
3523 mptcp_set_state(nsk, TCP_ESTABLISHED);
3524
3525 /* The msk maintain a ref to each subflow in the connections list */
3526 WRITE_ONCE(msk->first, ssk);
3527 subflow = mptcp_subflow_ctx(ssk);
3528 list_add(&subflow->node, &msk->conn_list);
3529 sock_hold(ssk);
3530
3531 /* new mpc subflow takes ownership of the newly
3532 * created mptcp socket
3533 */
3534 mptcp_token_accept(subflow_req, msk);
3535
3536 /* set msk addresses early to ensure mptcp_pm_get_local_id()
3537 * uses the correct data
3538 */
3539 mptcp_copy_inaddrs(nsk, ssk);
3540 __mptcp_propagate_sndbuf(nsk, ssk);
3541
3542 mptcp_rcv_space_init(msk, ssk);
3543
3544 if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
3545 __mptcp_subflow_fully_established(msk, subflow, mp_opt);
3546 bh_unlock_sock(nsk);
3547
3548 /* note: the newly allocated socket refcount is 2 now */
3549 return nsk;
3550 }
3551
mptcp_rcv_space_init(struct mptcp_sock * msk,const struct sock * ssk)3552 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3553 {
3554 const struct tcp_sock *tp = tcp_sk(ssk);
3555
3556 msk->rcvspace_init = 1;
3557 msk->rcvq_space.copied = 0;
3558 msk->rcvq_space.rtt_us = 0;
3559
3560 msk->rcvq_space.time = tp->tcp_mstamp;
3561
3562 /* initial rcv_space offering made to peer */
3563 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3564 TCP_INIT_CWND * tp->advmss);
3565 if (msk->rcvq_space.space == 0)
3566 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3567 }
3568
mptcp_destroy(struct sock * sk)3569 static void mptcp_destroy(struct sock *sk)
3570 {
3571 struct mptcp_sock *msk = mptcp_sk(sk);
3572
3573 /* allow the following to close even the initial subflow */
3574 msk->free_first = 1;
3575 mptcp_destroy_common(msk);
3576 sk_sockets_allocated_dec(sk);
3577 }
3578
__mptcp_data_acked(struct sock * sk)3579 void __mptcp_data_acked(struct sock *sk)
3580 {
3581 if (!sock_owned_by_user(sk))
3582 __mptcp_clean_una(sk);
3583 else
3584 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3585 }
3586
__mptcp_check_push(struct sock * sk,struct sock * ssk)3587 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3588 {
3589 if (!sock_owned_by_user(sk))
3590 __mptcp_subflow_push_pending(sk, ssk, false);
3591 else
3592 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3593 }
3594
3595 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3596 BIT(MPTCP_RETRANSMIT) | \
3597 BIT(MPTCP_FLUSH_JOIN_LIST))
3598
3599 /* processes deferred events and flush wmem */
mptcp_release_cb(struct sock * sk)3600 static void mptcp_release_cb(struct sock *sk)
3601 __must_hold(&sk->sk_lock.slock)
3602 {
3603 struct mptcp_sock *msk = mptcp_sk(sk);
3604
3605 for (;;) {
3606 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
3607 struct list_head join_list, skbs;
3608 bool spool_bl;
3609 u32 moved;
3610
3611 spool_bl = mptcp_can_spool_backlog(sk, &skbs);
3612 if (!flags && !spool_bl)
3613 break;
3614
3615 INIT_LIST_HEAD(&join_list);
3616 list_splice_init(&msk->join_list, &join_list);
3617
3618 /* the following actions acquire the subflow socket lock
3619 *
3620 * 1) can't be invoked in atomic scope
3621 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3622 * datapath acquires the msk socket spinlock while helding
3623 * the subflow socket lock
3624 */
3625 msk->cb_flags &= ~flags;
3626 spin_unlock_bh(&sk->sk_lock.slock);
3627
3628 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3629 __mptcp_flush_join_list(sk, &join_list);
3630 if (flags & BIT(MPTCP_PUSH_PENDING))
3631 __mptcp_push_pending(sk, 0);
3632 if (flags & BIT(MPTCP_RETRANSMIT))
3633 __mptcp_retrans(sk);
3634 if (spool_bl && __mptcp_move_skbs(sk, &skbs, &moved)) {
3635 /* notify ack seq update */
3636 mptcp_cleanup_rbuf(msk, 0);
3637 sk->sk_data_ready(sk);
3638 }
3639
3640 cond_resched();
3641 spin_lock_bh(&sk->sk_lock.slock);
3642 if (spool_bl)
3643 mptcp_backlog_spooled(sk, moved, &skbs);
3644 }
3645
3646 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3647 __mptcp_clean_una_wakeup(sk);
3648 if (unlikely(msk->cb_flags)) {
3649 /* be sure to sync the msk state before taking actions
3650 * depending on sk_state (MPTCP_ERROR_REPORT)
3651 * On sk release avoid actions depending on the first subflow
3652 */
3653 if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
3654 __mptcp_sync_state(sk, msk->pending_state);
3655 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3656 __mptcp_error_report(sk);
3657 if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
3658 __mptcp_sync_sndbuf(sk);
3659 }
3660 }
3661
3662 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3663 * TCP can't schedule delack timer before the subflow is fully established.
3664 * MPTCP uses the delack timer to do 3rd ack retransmissions
3665 */
schedule_3rdack_retransmission(struct sock * ssk)3666 static void schedule_3rdack_retransmission(struct sock *ssk)
3667 {
3668 struct inet_connection_sock *icsk = inet_csk(ssk);
3669 struct tcp_sock *tp = tcp_sk(ssk);
3670 unsigned long timeout;
3671
3672 if (READ_ONCE(mptcp_subflow_ctx(ssk)->fully_established))
3673 return;
3674
3675 /* reschedule with a timeout above RTT, as we must look only for drop */
3676 if (tp->srtt_us)
3677 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3678 else
3679 timeout = TCP_TIMEOUT_INIT;
3680 timeout += jiffies;
3681
3682 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3683 smp_store_release(&icsk->icsk_ack.pending,
3684 icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER);
3685 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3686 }
3687
mptcp_subflow_process_delegated(struct sock * ssk,long status)3688 void mptcp_subflow_process_delegated(struct sock *ssk, long status)
3689 {
3690 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3691 struct sock *sk = subflow->conn;
3692
3693 if (status & BIT(MPTCP_DELEGATE_SEND)) {
3694 mptcp_data_lock(sk);
3695 if (!sock_owned_by_user(sk))
3696 __mptcp_subflow_push_pending(sk, ssk, true);
3697 else
3698 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3699 mptcp_data_unlock(sk);
3700 }
3701 if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
3702 mptcp_data_lock(sk);
3703 if (!sock_owned_by_user(sk))
3704 __mptcp_sync_sndbuf(sk);
3705 else
3706 __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
3707 mptcp_data_unlock(sk);
3708 }
3709 if (status & BIT(MPTCP_DELEGATE_ACK))
3710 schedule_3rdack_retransmission(ssk);
3711 }
3712
mptcp_hash(struct sock * sk)3713 static int mptcp_hash(struct sock *sk)
3714 {
3715 /* should never be called,
3716 * we hash the TCP subflows not the MPTCP socket
3717 */
3718 WARN_ON_ONCE(1);
3719 return 0;
3720 }
3721
mptcp_unhash(struct sock * sk)3722 static void mptcp_unhash(struct sock *sk)
3723 {
3724 /* called from sk_common_release(), but nothing to do here */
3725 }
3726
mptcp_get_port(struct sock * sk,unsigned short snum)3727 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3728 {
3729 struct mptcp_sock *msk = mptcp_sk(sk);
3730
3731 pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
3732 if (WARN_ON_ONCE(!msk->first))
3733 return -EINVAL;
3734
3735 return inet_csk_get_port(msk->first, snum);
3736 }
3737
mptcp_finish_connect(struct sock * ssk)3738 void mptcp_finish_connect(struct sock *ssk)
3739 {
3740 struct mptcp_subflow_context *subflow;
3741 struct mptcp_sock *msk;
3742 struct sock *sk;
3743
3744 subflow = mptcp_subflow_ctx(ssk);
3745 sk = subflow->conn;
3746 msk = mptcp_sk(sk);
3747
3748 pr_debug("msk=%p, token=%u\n", sk, subflow->token);
3749
3750 subflow->map_seq = subflow->iasn;
3751 subflow->map_subflow_seq = 1;
3752
3753 /* the socket is not connected yet, no msk/subflow ops can access/race
3754 * accessing the field below
3755 */
3756 WRITE_ONCE(msk->local_key, subflow->local_key);
3757
3758 mptcp_pm_new_connection(msk, ssk, 0);
3759 }
3760
mptcp_sock_graft(struct sock * sk,struct socket * parent)3761 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3762 {
3763 write_lock_bh(&sk->sk_callback_lock);
3764 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3765 sk_set_socket(sk, parent);
3766 write_unlock_bh(&sk->sk_callback_lock);
3767 }
3768
3769 /* Can be called without holding the msk socket lock; use the callback lock
3770 * to avoid {READ_,WRITE_}ONCE annotations on sk_socket.
3771 */
mptcp_sock_check_graft(struct sock * sk,struct sock * ssk)3772 static void mptcp_sock_check_graft(struct sock *sk, struct sock *ssk)
3773 {
3774 struct socket *sock;
3775
3776 write_lock_bh(&sk->sk_callback_lock);
3777 sock = sk->sk_socket;
3778 write_unlock_bh(&sk->sk_callback_lock);
3779 if (sock) {
3780 mptcp_sock_graft(ssk, sock);
3781 __mptcp_inherit_cgrp_data(sk, ssk);
3782 __mptcp_inherit_memcg(sk, ssk, GFP_ATOMIC);
3783 }
3784 }
3785
mptcp_finish_join(struct sock * ssk)3786 bool mptcp_finish_join(struct sock *ssk)
3787 {
3788 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3789 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3790 struct sock *parent = (void *)msk;
3791 bool ret = true;
3792
3793 pr_debug("msk=%p, subflow=%p\n", msk, subflow);
3794
3795 /* mptcp socket already closing? */
3796 if (!mptcp_is_fully_established(parent)) {
3797 subflow->reset_reason = MPTCP_RST_EMPTCP;
3798 return false;
3799 }
3800
3801 /* Active subflow, already present inside the conn_list; is grafted
3802 * either by __mptcp_subflow_connect() or accept.
3803 */
3804 if (!list_empty(&subflow->node)) {
3805 spin_lock_bh(&msk->fallback_lock);
3806 if (!msk->allow_subflows) {
3807 spin_unlock_bh(&msk->fallback_lock);
3808 return false;
3809 }
3810 mptcp_subflow_joined(msk, ssk);
3811 spin_unlock_bh(&msk->fallback_lock);
3812 mptcp_propagate_sndbuf(parent, ssk);
3813 return true;
3814 }
3815
3816 if (!mptcp_pm_allow_new_subflow(msk)) {
3817 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_JOINREJECTED);
3818 goto err_prohibited;
3819 }
3820
3821 /* If we can't acquire msk socket lock here, let the release callback
3822 * handle it
3823 */
3824 mptcp_data_lock(parent);
3825 if (!sock_owned_by_user(parent)) {
3826 ret = __mptcp_finish_join(msk, ssk);
3827 if (ret) {
3828 sock_hold(ssk);
3829 list_add_tail(&subflow->node, &msk->conn_list);
3830 mptcp_sock_check_graft(parent, ssk);
3831 }
3832 } else {
3833 sock_hold(ssk);
3834 list_add_tail(&subflow->node, &msk->join_list);
3835 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3836
3837 /* In case of later failures, __mptcp_flush_join_list() will
3838 * properly orphan the ssk via mptcp_close_ssk().
3839 */
3840 mptcp_sock_check_graft(parent, ssk);
3841 }
3842 mptcp_data_unlock(parent);
3843
3844 if (!ret) {
3845 err_prohibited:
3846 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3847 return false;
3848 }
3849
3850 return true;
3851 }
3852
mptcp_shutdown(struct sock * sk,int how)3853 static void mptcp_shutdown(struct sock *sk, int how)
3854 {
3855 pr_debug("sk=%p, how=%d\n", sk, how);
3856
3857 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3858 __mptcp_wr_shutdown(sk);
3859 }
3860
mptcp_ioctl_outq(const struct mptcp_sock * msk,u64 v)3861 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3862 {
3863 const struct sock *sk = (void *)msk;
3864 u64 delta;
3865
3866 if (sk->sk_state == TCP_LISTEN)
3867 return -EINVAL;
3868
3869 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3870 return 0;
3871
3872 delta = msk->write_seq - v;
3873 if (__mptcp_check_fallback(msk) && msk->first) {
3874 struct tcp_sock *tp = tcp_sk(msk->first);
3875
3876 /* the first subflow is disconnected after close - see
3877 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3878 * so ignore that status, too.
3879 */
3880 if (!((1 << msk->first->sk_state) &
3881 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3882 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3883 }
3884 if (delta > INT_MAX)
3885 delta = INT_MAX;
3886
3887 return (int)delta;
3888 }
3889
mptcp_ioctl(struct sock * sk,int cmd,int * karg)3890 static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3891 {
3892 struct mptcp_sock *msk = mptcp_sk(sk);
3893 bool slow;
3894
3895 switch (cmd) {
3896 case SIOCINQ:
3897 if (sk->sk_state == TCP_LISTEN)
3898 return -EINVAL;
3899
3900 lock_sock(sk);
3901 if (mptcp_move_skbs(sk))
3902 mptcp_cleanup_rbuf(msk, 0);
3903 *karg = mptcp_inq_hint(sk);
3904 release_sock(sk);
3905 break;
3906 case SIOCOUTQ:
3907 slow = lock_sock_fast(sk);
3908 *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3909 unlock_sock_fast(sk, slow);
3910 break;
3911 case SIOCOUTQNSD:
3912 slow = lock_sock_fast(sk);
3913 *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3914 unlock_sock_fast(sk, slow);
3915 break;
3916 default:
3917 return -ENOIOCTLCMD;
3918 }
3919
3920 return 0;
3921 }
3922
mptcp_connect(struct sock * sk,struct sockaddr_unsized * uaddr,int addr_len)3923 static int mptcp_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
3924 int addr_len)
3925 {
3926 struct mptcp_subflow_context *subflow;
3927 struct mptcp_sock *msk = mptcp_sk(sk);
3928 int err = -EINVAL;
3929 struct sock *ssk;
3930
3931 ssk = __mptcp_nmpc_sk(msk);
3932 if (IS_ERR(ssk))
3933 return PTR_ERR(ssk);
3934
3935 mptcp_set_state(sk, TCP_SYN_SENT);
3936 subflow = mptcp_subflow_ctx(ssk);
3937 #ifdef CONFIG_TCP_MD5SIG
3938 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3939 * TCP option space.
3940 */
3941 if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
3942 mptcp_early_fallback(msk, subflow, MPTCP_MIB_MD5SIGFALLBACK);
3943 #endif
3944 if (subflow->request_mptcp) {
3945 if (mptcp_active_should_disable(sk))
3946 mptcp_early_fallback(msk, subflow,
3947 MPTCP_MIB_MPCAPABLEACTIVEDISABLED);
3948 else if (mptcp_token_new_connect(ssk) < 0)
3949 mptcp_early_fallback(msk, subflow,
3950 MPTCP_MIB_TOKENFALLBACKINIT);
3951 }
3952
3953 WRITE_ONCE(msk->write_seq, subflow->idsn);
3954 WRITE_ONCE(msk->snd_nxt, subflow->idsn);
3955 WRITE_ONCE(msk->snd_una, subflow->idsn);
3956 if (likely(!__mptcp_check_fallback(msk)))
3957 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3958
3959 /* if reaching here via the fastopen/sendmsg path, the caller already
3960 * acquired the subflow socket lock, too.
3961 */
3962 if (!msk->fastopening)
3963 lock_sock(ssk);
3964
3965 /* the following mirrors closely a very small chunk of code from
3966 * __inet_stream_connect()
3967 */
3968 if (ssk->sk_state != TCP_CLOSE)
3969 goto out;
3970
3971 if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
3972 err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3973 if (err)
3974 goto out;
3975 }
3976
3977 err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3978 if (err < 0)
3979 goto out;
3980
3981 inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3982
3983 out:
3984 if (!msk->fastopening)
3985 release_sock(ssk);
3986
3987 /* on successful connect, the msk state will be moved to established by
3988 * subflow_finish_connect()
3989 */
3990 if (unlikely(err)) {
3991 /* avoid leaving a dangling token in an unconnected socket */
3992 mptcp_token_destroy(msk);
3993 mptcp_set_state(sk, TCP_CLOSE);
3994 return err;
3995 }
3996
3997 mptcp_copy_inaddrs(sk, ssk);
3998 return 0;
3999 }
4000
4001 static struct proto mptcp_prot = {
4002 .name = "MPTCP",
4003 .owner = THIS_MODULE,
4004 .init = mptcp_init_sock,
4005 .connect = mptcp_connect,
4006 .disconnect = mptcp_disconnect,
4007 .close = mptcp_close,
4008 .setsockopt = mptcp_setsockopt,
4009 .getsockopt = mptcp_getsockopt,
4010 .shutdown = mptcp_shutdown,
4011 .destroy = mptcp_destroy,
4012 .sendmsg = mptcp_sendmsg,
4013 .ioctl = mptcp_ioctl,
4014 .recvmsg = mptcp_recvmsg,
4015 .release_cb = mptcp_release_cb,
4016 .hash = mptcp_hash,
4017 .unhash = mptcp_unhash,
4018 .get_port = mptcp_get_port,
4019 .stream_memory_free = mptcp_stream_memory_free,
4020 .sockets_allocated = &mptcp_sockets_allocated,
4021
4022 .memory_allocated = &net_aligned_data.tcp_memory_allocated,
4023 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
4024
4025 .memory_pressure = &tcp_memory_pressure,
4026 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
4027 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
4028 .sysctl_mem = sysctl_tcp_mem,
4029 .obj_size = sizeof(struct mptcp_sock),
4030 .slab_flags = SLAB_TYPESAFE_BY_RCU,
4031 .no_autobind = true,
4032 };
4033
mptcp_bind(struct socket * sock,struct sockaddr_unsized * uaddr,int addr_len)4034 static int mptcp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
4035 {
4036 struct mptcp_sock *msk = mptcp_sk(sock->sk);
4037 struct sock *ssk, *sk = sock->sk;
4038 int err = -EINVAL;
4039
4040 lock_sock(sk);
4041 ssk = __mptcp_nmpc_sk(msk);
4042 if (IS_ERR(ssk)) {
4043 err = PTR_ERR(ssk);
4044 goto unlock;
4045 }
4046
4047 if (sk->sk_family == AF_INET)
4048 err = inet_bind_sk(ssk, uaddr, addr_len);
4049 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4050 else if (sk->sk_family == AF_INET6)
4051 err = inet6_bind_sk(ssk, uaddr, addr_len);
4052 #endif
4053 if (!err)
4054 mptcp_copy_inaddrs(sk, ssk);
4055
4056 unlock:
4057 release_sock(sk);
4058 return err;
4059 }
4060
mptcp_listen(struct socket * sock,int backlog)4061 static int mptcp_listen(struct socket *sock, int backlog)
4062 {
4063 struct mptcp_sock *msk = mptcp_sk(sock->sk);
4064 struct sock *sk = sock->sk;
4065 struct sock *ssk;
4066 int err;
4067
4068 pr_debug("msk=%p\n", msk);
4069
4070 lock_sock(sk);
4071
4072 err = -EINVAL;
4073 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
4074 goto unlock;
4075
4076 ssk = __mptcp_nmpc_sk(msk);
4077 if (IS_ERR(ssk)) {
4078 err = PTR_ERR(ssk);
4079 goto unlock;
4080 }
4081
4082 mptcp_set_state(sk, TCP_LISTEN);
4083 sock_set_flag(sk, SOCK_RCU_FREE);
4084
4085 lock_sock(ssk);
4086 err = __inet_listen_sk(ssk, backlog);
4087 release_sock(ssk);
4088 mptcp_set_state(sk, inet_sk_state_load(ssk));
4089
4090 if (!err) {
4091 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
4092 mptcp_copy_inaddrs(sk, ssk);
4093 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
4094 }
4095
4096 unlock:
4097 release_sock(sk);
4098 return err;
4099 }
4100
mptcp_graft_subflows(struct sock * sk)4101 static void mptcp_graft_subflows(struct sock *sk)
4102 {
4103 struct mptcp_subflow_context *subflow;
4104 struct mptcp_sock *msk = mptcp_sk(sk);
4105
4106 if (mem_cgroup_sockets_enabled) {
4107 LIST_HEAD(join_list);
4108
4109 /* Subflows joining after __inet_accept() will get the
4110 * mem CG properly initialized at mptcp_finish_join() time,
4111 * but subflows pending in join_list need explicit
4112 * initialization before flushing `backlog_unaccounted`
4113 * or MPTCP can later unexpectedly observe unaccounted memory.
4114 */
4115 mptcp_data_lock(sk);
4116 list_splice_init(&msk->join_list, &join_list);
4117 mptcp_data_unlock(sk);
4118
4119 __mptcp_flush_join_list(sk, &join_list);
4120 }
4121
4122 mptcp_for_each_subflow(msk, subflow) {
4123 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4124
4125 lock_sock(ssk);
4126
4127 /* Set ssk->sk_socket of accept()ed flows to mptcp socket.
4128 * This is needed so NOSPACE flag can be set from tcp stack.
4129 */
4130 if (!ssk->sk_socket)
4131 mptcp_sock_graft(ssk, sk->sk_socket);
4132
4133 if (!mem_cgroup_sk_enabled(sk))
4134 goto unlock;
4135
4136 __mptcp_inherit_cgrp_data(sk, ssk);
4137 __mptcp_inherit_memcg(sk, ssk, GFP_KERNEL);
4138
4139 unlock:
4140 release_sock(ssk);
4141 }
4142
4143 if (mem_cgroup_sk_enabled(sk)) {
4144 gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL;
4145 int amt;
4146
4147 /* Account the backlog memory; prior accept() is aware of
4148 * fwd and rmem only.
4149 */
4150 mptcp_data_lock(sk);
4151 amt = sk_mem_pages(sk->sk_forward_alloc +
4152 msk->backlog_unaccounted +
4153 atomic_read(&sk->sk_rmem_alloc)) -
4154 sk_mem_pages(sk->sk_forward_alloc +
4155 atomic_read(&sk->sk_rmem_alloc));
4156 msk->backlog_unaccounted = 0;
4157 mptcp_data_unlock(sk);
4158
4159 if (amt)
4160 mem_cgroup_sk_charge(sk, amt, gfp);
4161 }
4162 }
4163
mptcp_stream_accept(struct socket * sock,struct socket * newsock,struct proto_accept_arg * arg)4164 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
4165 struct proto_accept_arg *arg)
4166 {
4167 struct mptcp_sock *msk = mptcp_sk(sock->sk);
4168 struct sock *ssk, *newsk;
4169
4170 pr_debug("msk=%p\n", msk);
4171
4172 /* Buggy applications can call accept on socket states other then LISTEN
4173 * but no need to allocate the first subflow just to error out.
4174 */
4175 ssk = READ_ONCE(msk->first);
4176 if (!ssk)
4177 return -EINVAL;
4178
4179 pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
4180 newsk = inet_csk_accept(ssk, arg);
4181 if (!newsk)
4182 return arg->err;
4183
4184 pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
4185 if (sk_is_mptcp(newsk)) {
4186 struct mptcp_subflow_context *subflow;
4187 struct sock *new_mptcp_sock;
4188
4189 subflow = mptcp_subflow_ctx(newsk);
4190 new_mptcp_sock = subflow->conn;
4191
4192 /* is_mptcp should be false if subflow->conn is missing, see
4193 * subflow_syn_recv_sock()
4194 */
4195 if (WARN_ON_ONCE(!new_mptcp_sock)) {
4196 tcp_sk(newsk)->is_mptcp = 0;
4197 goto tcpfallback;
4198 }
4199
4200 newsk = new_mptcp_sock;
4201 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
4202
4203 newsk->sk_kern_sock = arg->kern;
4204 lock_sock(newsk);
4205 __inet_accept(sock, newsock, newsk);
4206
4207 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
4208 msk = mptcp_sk(newsk);
4209 msk->in_accept_queue = 0;
4210
4211 mptcp_graft_subflows(newsk);
4212 mptcp_rps_record_subflows(msk);
4213
4214 /* Do late cleanup for the first subflow as necessary. Also
4215 * deal with bad peers not doing a complete shutdown.
4216 */
4217 if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
4218 if (unlikely(list_is_singular(&msk->conn_list)))
4219 mptcp_set_state(newsk, TCP_CLOSE);
4220 mptcp_close_ssk(newsk, msk->first,
4221 mptcp_subflow_ctx(msk->first));
4222 }
4223 } else {
4224 tcpfallback:
4225 newsk->sk_kern_sock = arg->kern;
4226 lock_sock(newsk);
4227 __inet_accept(sock, newsock, newsk);
4228 /* we are being invoked after accepting a non-mp-capable
4229 * flow: sk is a tcp_sk, not an mptcp one.
4230 *
4231 * Hand the socket over to tcp so all further socket ops
4232 * bypass mptcp.
4233 */
4234 WRITE_ONCE(newsock->sk->sk_socket->ops,
4235 mptcp_fallback_tcp_ops(newsock->sk));
4236 }
4237 release_sock(newsk);
4238
4239 return 0;
4240 }
4241
mptcp_check_writeable(struct mptcp_sock * msk)4242 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
4243 {
4244 struct sock *sk = (struct sock *)msk;
4245
4246 if (__mptcp_stream_is_writeable(sk, 1))
4247 return EPOLLOUT | EPOLLWRNORM;
4248
4249 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
4250 smp_mb__after_atomic(); /* NOSPACE is changed by mptcp_write_space() */
4251 if (__mptcp_stream_is_writeable(sk, 1))
4252 return EPOLLOUT | EPOLLWRNORM;
4253
4254 return 0;
4255 }
4256
mptcp_poll(struct file * file,struct socket * sock,struct poll_table_struct * wait)4257 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
4258 struct poll_table_struct *wait)
4259 {
4260 struct sock *sk = sock->sk;
4261 struct mptcp_sock *msk;
4262 __poll_t mask = 0;
4263 u8 shutdown;
4264 int state;
4265
4266 msk = mptcp_sk(sk);
4267 sock_poll_wait(file, sock, wait);
4268
4269 state = inet_sk_state_load(sk);
4270 pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
4271 if (state == TCP_LISTEN) {
4272 struct sock *ssk = READ_ONCE(msk->first);
4273
4274 if (WARN_ON_ONCE(!ssk))
4275 return 0;
4276
4277 return inet_csk_listen_poll(ssk);
4278 }
4279
4280 shutdown = READ_ONCE(sk->sk_shutdown);
4281 if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
4282 mask |= EPOLLHUP;
4283 if (shutdown & RCV_SHUTDOWN)
4284 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
4285
4286 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
4287 mask |= mptcp_check_readable(sk);
4288 if (shutdown & SEND_SHUTDOWN)
4289 mask |= EPOLLOUT | EPOLLWRNORM;
4290 else
4291 mask |= mptcp_check_writeable(msk);
4292 } else if (state == TCP_SYN_SENT &&
4293 inet_test_bit(DEFER_CONNECT, sk)) {
4294 /* cf tcp_poll() note about TFO */
4295 mask |= EPOLLOUT | EPOLLWRNORM;
4296 }
4297
4298 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
4299 smp_rmb();
4300 if (READ_ONCE(sk->sk_err))
4301 mask |= EPOLLERR;
4302
4303 return mask;
4304 }
4305
4306 static const struct proto_ops mptcp_stream_ops = {
4307 .family = PF_INET,
4308 .owner = THIS_MODULE,
4309 .release = inet_release,
4310 .bind = mptcp_bind,
4311 .connect = inet_stream_connect,
4312 .socketpair = sock_no_socketpair,
4313 .accept = mptcp_stream_accept,
4314 .getname = inet_getname,
4315 .poll = mptcp_poll,
4316 .ioctl = inet_ioctl,
4317 .gettstamp = sock_gettstamp,
4318 .listen = mptcp_listen,
4319 .shutdown = inet_shutdown,
4320 .setsockopt = sock_common_setsockopt,
4321 .getsockopt = sock_common_getsockopt,
4322 .sendmsg = inet_sendmsg,
4323 .recvmsg = inet_recvmsg,
4324 .mmap = sock_no_mmap,
4325 .set_rcvlowat = mptcp_set_rcvlowat,
4326 };
4327
4328 static struct inet_protosw mptcp_protosw = {
4329 .type = SOCK_STREAM,
4330 .protocol = IPPROTO_MPTCP,
4331 .prot = &mptcp_prot,
4332 .ops = &mptcp_stream_ops,
4333 .flags = INET_PROTOSW_ICSK,
4334 };
4335
mptcp_napi_poll(struct napi_struct * napi,int budget)4336 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
4337 {
4338 struct mptcp_delegated_action *delegated;
4339 struct mptcp_subflow_context *subflow;
4340 int work_done = 0;
4341
4342 delegated = container_of(napi, struct mptcp_delegated_action, napi);
4343 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
4344 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4345
4346 bh_lock_sock_nested(ssk);
4347 if (!sock_owned_by_user(ssk)) {
4348 mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
4349 } else {
4350 /* tcp_release_cb_override already processed
4351 * the action or will do at next release_sock().
4352 * In both case must dequeue the subflow here - on the same
4353 * CPU that scheduled it.
4354 */
4355 smp_wmb();
4356 clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
4357 }
4358 bh_unlock_sock(ssk);
4359 sock_put(ssk);
4360
4361 if (++work_done == budget)
4362 return budget;
4363 }
4364
4365 /* always provide a 0 'work_done' argument, so that napi_complete_done
4366 * will not try accessing the NULL napi->dev ptr
4367 */
4368 napi_complete_done(napi, 0);
4369 return work_done;
4370 }
4371
mptcp_proto_init(void)4372 void __init mptcp_proto_init(void)
4373 {
4374 struct mptcp_delegated_action *delegated;
4375 int cpu;
4376
4377 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
4378
4379 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4380 panic("Failed to allocate MPTCP pcpu counter\n");
4381
4382 mptcp_napi_dev = alloc_netdev_dummy(0);
4383 if (!mptcp_napi_dev)
4384 panic("Failed to allocate MPTCP dummy netdev\n");
4385 for_each_possible_cpu(cpu) {
4386 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4387 INIT_LIST_HEAD(&delegated->head);
4388 netif_napi_add_tx(mptcp_napi_dev, &delegated->napi,
4389 mptcp_napi_poll);
4390 napi_enable(&delegated->napi);
4391 }
4392
4393 mptcp_subflow_init();
4394 mptcp_pm_init();
4395 mptcp_sched_init();
4396 mptcp_token_init();
4397
4398 if (proto_register(&mptcp_prot, 1) != 0)
4399 panic("Failed to register MPTCP proto.\n");
4400
4401 inet_register_protosw(&mptcp_protosw);
4402
4403 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4404 }
4405
4406 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4407 static const struct proto_ops mptcp_v6_stream_ops = {
4408 .family = PF_INET6,
4409 .owner = THIS_MODULE,
4410 .release = inet6_release,
4411 .bind = mptcp_bind,
4412 .connect = inet_stream_connect,
4413 .socketpair = sock_no_socketpair,
4414 .accept = mptcp_stream_accept,
4415 .getname = inet6_getname,
4416 .poll = mptcp_poll,
4417 .ioctl = inet6_ioctl,
4418 .gettstamp = sock_gettstamp,
4419 .listen = mptcp_listen,
4420 .shutdown = inet_shutdown,
4421 .setsockopt = sock_common_setsockopt,
4422 .getsockopt = sock_common_getsockopt,
4423 .sendmsg = inet6_sendmsg,
4424 .recvmsg = inet6_recvmsg,
4425 .mmap = sock_no_mmap,
4426 #ifdef CONFIG_COMPAT
4427 .compat_ioctl = inet6_compat_ioctl,
4428 #endif
4429 .set_rcvlowat = mptcp_set_rcvlowat,
4430 };
4431
4432 static struct proto mptcp_v6_prot;
4433
4434 static struct inet_protosw mptcp_v6_protosw = {
4435 .type = SOCK_STREAM,
4436 .protocol = IPPROTO_MPTCP,
4437 .prot = &mptcp_v6_prot,
4438 .ops = &mptcp_v6_stream_ops,
4439 .flags = INET_PROTOSW_ICSK,
4440 };
4441
mptcp_proto_v6_init(void)4442 int __init mptcp_proto_v6_init(void)
4443 {
4444 int err;
4445
4446 mptcp_v6_prot = mptcp_prot;
4447 strscpy(mptcp_v6_prot.name, "MPTCPv6", sizeof(mptcp_v6_prot.name));
4448 mptcp_v6_prot.slab = NULL;
4449 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4450 mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4451
4452 err = proto_register(&mptcp_v6_prot, 1);
4453 if (err)
4454 return err;
4455
4456 err = inet6_register_protosw(&mptcp_v6_protosw);
4457 if (err)
4458 proto_unregister(&mptcp_v6_prot);
4459
4460 return err;
4461 }
4462 #endif
4463