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