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 return;
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 return;
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
2733 /* schedule the timeout timer for the relevant event: either close timeout
2734 * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2735 */
mptcp_reset_tout_timer(struct mptcp_sock * msk,unsigned long fail_tout)2736 void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
2737 {
2738 struct sock *sk = (struct sock *)msk;
2739 unsigned long timeout, close_timeout;
2740
2741 if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
2742 return;
2743
2744 close_timeout = (unsigned long)inet_csk(sk)->icsk_mtup.probe_timestamp -
2745 tcp_jiffies32 + jiffies + mptcp_close_timeout(sk);
2746
2747 /* the close timeout takes precedence on the fail one, and here at least one of
2748 * them is active
2749 */
2750 timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
2751
2752 sk_reset_timer(sk, &sk->sk_timer, timeout);
2753 }
2754
mptcp_mp_fail_no_response(struct mptcp_sock * msk)2755 static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2756 {
2757 struct sock *ssk = msk->first;
2758 bool slow;
2759
2760 if (!ssk)
2761 return;
2762
2763 pr_debug("MP_FAIL doesn't respond, reset the subflow\n");
2764
2765 slow = lock_sock_fast(ssk);
2766 mptcp_subflow_reset(ssk);
2767 WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2768 unlock_sock_fast(ssk, slow);
2769 }
2770
mptcp_do_fastclose(struct sock * sk)2771 static void mptcp_do_fastclose(struct sock *sk)
2772 {
2773 struct mptcp_subflow_context *subflow, *tmp;
2774 struct mptcp_sock *msk = mptcp_sk(sk);
2775
2776 mptcp_set_state(sk, TCP_CLOSE);
2777
2778 /* Explicitly send the fastclose reset as need */
2779 if (__mptcp_check_fallback(msk))
2780 return;
2781
2782 mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2783 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2784
2785 lock_sock(ssk);
2786
2787 /* Some subflow socket states don't allow/need a reset.*/
2788 if ((1 << ssk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
2789 goto unlock;
2790
2791 subflow->send_fastclose = 1;
2792 tcp_send_active_reset(ssk, ssk->sk_allocation,
2793 SK_RST_REASON_TCP_ABORT_ON_CLOSE);
2794 unlock:
2795 release_sock(ssk);
2796 }
2797 }
2798
mptcp_worker(struct work_struct * work)2799 static void mptcp_worker(struct work_struct *work)
2800 {
2801 struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2802 struct sock *sk = (struct sock *)msk;
2803 unsigned long fail_tout;
2804 int state;
2805
2806 lock_sock(sk);
2807 state = sk->sk_state;
2808 if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2809 goto unlock;
2810
2811 mptcp_check_fastclose(msk);
2812
2813 mptcp_pm_worker(msk);
2814
2815 mptcp_check_send_data_fin(sk);
2816 mptcp_check_data_fin_ack(sk);
2817 mptcp_check_data_fin(sk);
2818
2819 if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2820 __mptcp_close_subflow(sk);
2821
2822 if (mptcp_close_tout_expired(sk)) {
2823 struct mptcp_subflow_context *subflow, *tmp;
2824
2825 mptcp_do_fastclose(sk);
2826 mptcp_for_each_subflow_safe(msk, subflow, tmp)
2827 __mptcp_close_ssk(sk, subflow->tcp_sock, subflow, 0);
2828 mptcp_close_wake_up(sk);
2829 }
2830
2831 if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
2832 __mptcp_destroy_sock(sk);
2833 goto unlock;
2834 }
2835
2836 if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2837 __mptcp_retrans(sk);
2838
2839 fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2840 if (fail_tout && time_after(jiffies, fail_tout))
2841 mptcp_mp_fail_no_response(msk);
2842
2843 unlock:
2844 release_sock(sk);
2845 sock_put(sk);
2846 }
2847
__mptcp_init_sock(struct sock * sk)2848 static void __mptcp_init_sock(struct sock *sk)
2849 {
2850 struct mptcp_sock *msk = mptcp_sk(sk);
2851
2852 INIT_LIST_HEAD(&msk->conn_list);
2853 INIT_LIST_HEAD(&msk->join_list);
2854 INIT_LIST_HEAD(&msk->rtx_queue);
2855 INIT_WORK(&msk->work, mptcp_worker);
2856 msk->out_of_order_queue = RB_ROOT;
2857 msk->first_pending = NULL;
2858 msk->timer_ival = TCP_RTO_MIN;
2859 msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
2860
2861 WRITE_ONCE(msk->first, NULL);
2862 inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2863 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2864 msk->allow_infinite_fallback = true;
2865 msk->allow_subflows = true;
2866 msk->recovery = false;
2867 msk->subflow_id = 1;
2868 msk->last_data_sent = tcp_jiffies32;
2869 msk->last_data_recv = tcp_jiffies32;
2870 msk->last_ack_recv = tcp_jiffies32;
2871
2872 mptcp_pm_data_init(msk);
2873 spin_lock_init(&msk->fallback_lock);
2874
2875 /* re-use the csk retrans timer for MPTCP-level retrans */
2876 timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2877 timer_setup(&sk->sk_timer, mptcp_tout_timer, 0);
2878 }
2879
mptcp_ca_reset(struct sock * sk)2880 static void mptcp_ca_reset(struct sock *sk)
2881 {
2882 struct inet_connection_sock *icsk = inet_csk(sk);
2883
2884 tcp_assign_congestion_control(sk);
2885 strscpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name,
2886 sizeof(mptcp_sk(sk)->ca_name));
2887
2888 /* no need to keep a reference to the ops, the name will suffice */
2889 tcp_cleanup_congestion_control(sk);
2890 icsk->icsk_ca_ops = NULL;
2891 }
2892
mptcp_init_sock(struct sock * sk)2893 static int mptcp_init_sock(struct sock *sk)
2894 {
2895 struct net *net = sock_net(sk);
2896 int ret;
2897
2898 __mptcp_init_sock(sk);
2899
2900 if (!mptcp_is_enabled(net))
2901 return -ENOPROTOOPT;
2902
2903 if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2904 return -ENOMEM;
2905
2906 rcu_read_lock();
2907 ret = mptcp_init_sched(mptcp_sk(sk),
2908 mptcp_sched_find(mptcp_get_scheduler(net)));
2909 rcu_read_unlock();
2910 if (ret)
2911 return ret;
2912
2913 set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2914
2915 /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2916 * propagate the correct value
2917 */
2918 mptcp_ca_reset(sk);
2919
2920 sk_sockets_allocated_inc(sk);
2921 sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2922 sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2923
2924 return 0;
2925 }
2926
__mptcp_clear_xmit(struct sock * sk)2927 static void __mptcp_clear_xmit(struct sock *sk)
2928 {
2929 struct mptcp_sock *msk = mptcp_sk(sk);
2930 struct mptcp_data_frag *dtmp, *dfrag;
2931
2932 msk->first_pending = NULL;
2933 list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2934 dfrag_clear(sk, dfrag);
2935 }
2936
mptcp_cancel_work(struct sock * sk)2937 void mptcp_cancel_work(struct sock *sk)
2938 {
2939 struct mptcp_sock *msk = mptcp_sk(sk);
2940
2941 if (cancel_work_sync(&msk->work))
2942 __sock_put(sk);
2943 }
2944
mptcp_subflow_shutdown(struct sock * sk,struct sock * ssk,int how)2945 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2946 {
2947 lock_sock(ssk);
2948
2949 switch (ssk->sk_state) {
2950 case TCP_LISTEN:
2951 if (!(how & RCV_SHUTDOWN))
2952 break;
2953 fallthrough;
2954 case TCP_SYN_SENT:
2955 WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2956 break;
2957 default:
2958 if (__mptcp_check_fallback(mptcp_sk(sk))) {
2959 pr_debug("Fallback\n");
2960 ssk->sk_shutdown |= how;
2961 tcp_shutdown(ssk, how);
2962
2963 /* simulate the data_fin ack reception to let the state
2964 * machine move forward
2965 */
2966 WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2967 mptcp_schedule_work(sk);
2968 } else {
2969 pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
2970 tcp_send_ack(ssk);
2971 if (!mptcp_rtx_timer_pending(sk))
2972 mptcp_reset_rtx_timer(sk);
2973 }
2974 break;
2975 }
2976
2977 release_sock(ssk);
2978 }
2979
mptcp_set_state(struct sock * sk,int state)2980 void mptcp_set_state(struct sock *sk, int state)
2981 {
2982 int oldstate = sk->sk_state;
2983
2984 switch (state) {
2985 case TCP_ESTABLISHED:
2986 if (oldstate != TCP_ESTABLISHED)
2987 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
2988 break;
2989 case TCP_CLOSE_WAIT:
2990 /* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
2991 * MPTCP "accepted" sockets will be created later on. So no
2992 * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
2993 */
2994 break;
2995 default:
2996 if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
2997 MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
2998 }
2999
3000 inet_sk_state_store(sk, state);
3001 }
3002
3003 static const unsigned char new_state[16] = {
3004 /* current state: new state: action: */
3005 [0 /* (Invalid) */] = TCP_CLOSE,
3006 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
3007 [TCP_SYN_SENT] = TCP_CLOSE,
3008 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
3009 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
3010 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
3011 [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
3012 [TCP_CLOSE] = TCP_CLOSE,
3013 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
3014 [TCP_LAST_ACK] = TCP_LAST_ACK,
3015 [TCP_LISTEN] = TCP_CLOSE,
3016 [TCP_CLOSING] = TCP_CLOSING,
3017 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
3018 };
3019
mptcp_close_state(struct sock * sk)3020 static int mptcp_close_state(struct sock *sk)
3021 {
3022 int next = (int)new_state[sk->sk_state];
3023 int ns = next & TCP_STATE_MASK;
3024
3025 mptcp_set_state(sk, ns);
3026
3027 return next & TCP_ACTION_FIN;
3028 }
3029
mptcp_check_send_data_fin(struct sock * sk)3030 static void mptcp_check_send_data_fin(struct sock *sk)
3031 {
3032 struct mptcp_subflow_context *subflow;
3033 struct mptcp_sock *msk = mptcp_sk(sk);
3034
3035 pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
3036 msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
3037 msk->snd_nxt, msk->write_seq);
3038
3039 /* we still need to enqueue subflows or not really shutting down,
3040 * skip this
3041 */
3042 if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
3043 mptcp_send_head(sk))
3044 return;
3045
3046 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3047
3048 mptcp_for_each_subflow(msk, subflow) {
3049 struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
3050
3051 mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
3052 }
3053 }
3054
__mptcp_wr_shutdown(struct sock * sk)3055 static void __mptcp_wr_shutdown(struct sock *sk)
3056 {
3057 struct mptcp_sock *msk = mptcp_sk(sk);
3058
3059 pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
3060 msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
3061 !!mptcp_send_head(sk));
3062
3063 /* will be ignored by fallback sockets */
3064 WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
3065 WRITE_ONCE(msk->snd_data_fin_enable, 1);
3066
3067 mptcp_check_send_data_fin(sk);
3068 }
3069
__mptcp_destroy_sock(struct sock * sk)3070 static void __mptcp_destroy_sock(struct sock *sk)
3071 {
3072 struct mptcp_sock *msk = mptcp_sk(sk);
3073
3074 pr_debug("msk=%p\n", msk);
3075
3076 might_sleep();
3077
3078 mptcp_stop_rtx_timer(sk);
3079 sk_stop_timer(sk, &sk->sk_timer);
3080 msk->pm.status = 0;
3081 mptcp_release_sched(msk);
3082
3083 sk->sk_prot->destroy(sk);
3084
3085 sk_stream_kill_queues(sk);
3086 xfrm_sk_free_policy(sk);
3087
3088 sock_put(sk);
3089 }
3090
__mptcp_unaccepted_force_close(struct sock * sk)3091 void __mptcp_unaccepted_force_close(struct sock *sk)
3092 {
3093 sock_set_flag(sk, SOCK_DEAD);
3094 mptcp_do_fastclose(sk);
3095 __mptcp_destroy_sock(sk);
3096 }
3097
mptcp_check_readable(struct sock * sk)3098 static __poll_t mptcp_check_readable(struct sock *sk)
3099 {
3100 return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
3101 }
3102
mptcp_check_listen_stop(struct sock * sk)3103 static void mptcp_check_listen_stop(struct sock *sk)
3104 {
3105 struct sock *ssk;
3106
3107 if (inet_sk_state_load(sk) != TCP_LISTEN)
3108 return;
3109
3110 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
3111 ssk = mptcp_sk(sk)->first;
3112 if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
3113 return;
3114
3115 lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
3116 tcp_set_state(ssk, TCP_CLOSE);
3117 mptcp_subflow_queue_clean(sk, ssk);
3118 inet_csk_listen_stop(ssk);
3119 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
3120 release_sock(ssk);
3121 }
3122
__mptcp_close(struct sock * sk,long timeout)3123 bool __mptcp_close(struct sock *sk, long timeout)
3124 {
3125 struct mptcp_subflow_context *subflow;
3126 struct mptcp_sock *msk = mptcp_sk(sk);
3127 bool do_cancel_work = false;
3128 int subflows_alive = 0;
3129
3130 WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
3131
3132 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
3133 mptcp_check_listen_stop(sk);
3134 mptcp_set_state(sk, TCP_CLOSE);
3135 goto cleanup;
3136 }
3137
3138 if (mptcp_data_avail(msk) || timeout < 0) {
3139 /* If the msk has read data, or the caller explicitly ask it,
3140 * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
3141 */
3142 mptcp_do_fastclose(sk);
3143 timeout = 0;
3144 } else if (mptcp_close_state(sk)) {
3145 __mptcp_wr_shutdown(sk);
3146 }
3147
3148 sk_stream_wait_close(sk, timeout);
3149
3150 cleanup:
3151 /* orphan all the subflows */
3152 mptcp_for_each_subflow(msk, subflow) {
3153 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3154 bool slow = lock_sock_fast_nested(ssk);
3155
3156 subflows_alive += ssk->sk_state != TCP_CLOSE;
3157
3158 /* since the close timeout takes precedence on the fail one,
3159 * cancel the latter
3160 */
3161 if (ssk == msk->first)
3162 subflow->fail_tout = 0;
3163
3164 /* detach from the parent socket, but allow data_ready to
3165 * push incoming data into the mptcp stack, to properly ack it
3166 */
3167 ssk->sk_socket = NULL;
3168 ssk->sk_wq = NULL;
3169 unlock_sock_fast(ssk, slow);
3170 }
3171 sock_orphan(sk);
3172
3173 /* all the subflows are closed, only timeout can change the msk
3174 * state, let's not keep resources busy for no reasons
3175 */
3176 if (subflows_alive == 0)
3177 mptcp_set_state(sk, TCP_CLOSE);
3178
3179 sock_hold(sk);
3180 pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
3181 mptcp_pm_connection_closed(msk);
3182
3183 if (sk->sk_state == TCP_CLOSE) {
3184 __mptcp_destroy_sock(sk);
3185 do_cancel_work = true;
3186 } else {
3187 mptcp_start_tout_timer(sk);
3188 }
3189
3190 return do_cancel_work;
3191 }
3192
mptcp_close(struct sock * sk,long timeout)3193 static void mptcp_close(struct sock *sk, long timeout)
3194 {
3195 bool do_cancel_work;
3196
3197 lock_sock(sk);
3198
3199 do_cancel_work = __mptcp_close(sk, timeout);
3200 release_sock(sk);
3201 if (do_cancel_work)
3202 mptcp_cancel_work(sk);
3203
3204 sock_put(sk);
3205 }
3206
mptcp_copy_inaddrs(struct sock * msk,const struct sock * ssk)3207 static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3208 {
3209 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3210 const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3211 struct ipv6_pinfo *msk6 = inet6_sk(msk);
3212
3213 msk->sk_v6_daddr = ssk->sk_v6_daddr;
3214 msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3215
3216 if (msk6 && ssk6) {
3217 msk6->saddr = ssk6->saddr;
3218 msk6->flow_label = ssk6->flow_label;
3219 }
3220 #endif
3221
3222 inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3223 inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3224 inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3225 inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3226 inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3227 inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3228 }
3229
mptcp_disconnect(struct sock * sk,int flags)3230 static int mptcp_disconnect(struct sock *sk, int flags)
3231 {
3232 struct mptcp_sock *msk = mptcp_sk(sk);
3233
3234 /* We are on the fastopen error path. We can't call straight into the
3235 * subflows cleanup code due to lock nesting (we are already under
3236 * msk->firstsocket lock).
3237 */
3238 if (msk->fastopening)
3239 return -EBUSY;
3240
3241 mptcp_check_listen_stop(sk);
3242 mptcp_set_state(sk, TCP_CLOSE);
3243
3244 mptcp_stop_rtx_timer(sk);
3245 mptcp_stop_tout_timer(sk);
3246
3247 mptcp_pm_connection_closed(msk);
3248
3249 /* msk->subflow is still intact, the following will not free the first
3250 * subflow
3251 */
3252 mptcp_do_fastclose(sk);
3253 mptcp_destroy_common(msk);
3254
3255 /* The first subflow is already in TCP_CLOSE status, the following
3256 * can't overlap with a fallback anymore
3257 */
3258 spin_lock_bh(&msk->fallback_lock);
3259 msk->allow_subflows = true;
3260 msk->allow_infinite_fallback = true;
3261 WRITE_ONCE(msk->flags, 0);
3262 spin_unlock_bh(&msk->fallback_lock);
3263
3264 msk->cb_flags = 0;
3265 msk->recovery = false;
3266 WRITE_ONCE(msk->can_ack, false);
3267 WRITE_ONCE(msk->fully_established, false);
3268 WRITE_ONCE(msk->rcv_data_fin, false);
3269 WRITE_ONCE(msk->snd_data_fin_enable, false);
3270 WRITE_ONCE(msk->rcv_fastclose, false);
3271 WRITE_ONCE(msk->use_64bit_ack, false);
3272 WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3273 mptcp_pm_data_reset(msk);
3274 mptcp_ca_reset(sk);
3275 msk->bytes_consumed = 0;
3276 msk->bytes_acked = 0;
3277 msk->bytes_received = 0;
3278 msk->bytes_sent = 0;
3279 msk->bytes_retrans = 0;
3280 msk->rcvspace_init = 0;
3281
3282 WRITE_ONCE(sk->sk_shutdown, 0);
3283 sk_error_report(sk);
3284 return 0;
3285 }
3286
3287 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
mptcp_inet6_sk(const struct sock * sk)3288 static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3289 {
3290 struct mptcp6_sock *msk6 = container_of(mptcp_sk(sk), struct mptcp6_sock, msk);
3291
3292 return &msk6->np;
3293 }
3294
mptcp_copy_ip6_options(struct sock * newsk,const struct sock * sk)3295 static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
3296 {
3297 const struct ipv6_pinfo *np = inet6_sk(sk);
3298 struct ipv6_txoptions *opt;
3299 struct ipv6_pinfo *newnp;
3300
3301 newnp = inet6_sk(newsk);
3302
3303 rcu_read_lock();
3304 opt = rcu_dereference(np->opt);
3305 if (opt) {
3306 opt = ipv6_dup_options(newsk, opt);
3307 if (!opt)
3308 net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
3309 }
3310 RCU_INIT_POINTER(newnp->opt, opt);
3311 rcu_read_unlock();
3312 }
3313 #endif
3314
mptcp_copy_ip_options(struct sock * newsk,const struct sock * sk)3315 static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
3316 {
3317 struct ip_options_rcu *inet_opt, *newopt = NULL;
3318 const struct inet_sock *inet = inet_sk(sk);
3319 struct inet_sock *newinet;
3320
3321 newinet = inet_sk(newsk);
3322
3323 rcu_read_lock();
3324 inet_opt = rcu_dereference(inet->inet_opt);
3325 if (inet_opt) {
3326 newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) +
3327 inet_opt->opt.optlen, GFP_ATOMIC);
3328 if (!newopt)
3329 net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
3330 }
3331 RCU_INIT_POINTER(newinet->inet_opt, newopt);
3332 rcu_read_unlock();
3333 }
3334
mptcp_sk_clone_init(const struct sock * sk,const struct mptcp_options_received * mp_opt,struct sock * ssk,struct request_sock * req)3335 struct sock *mptcp_sk_clone_init(const struct sock *sk,
3336 const struct mptcp_options_received *mp_opt,
3337 struct sock *ssk,
3338 struct request_sock *req)
3339 {
3340 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3341 struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3342 struct mptcp_subflow_context *subflow;
3343 struct mptcp_sock *msk;
3344
3345 if (!nsk)
3346 return NULL;
3347
3348 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3349 if (nsk->sk_family == AF_INET6)
3350 inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3351 #endif
3352
3353 __mptcp_init_sock(nsk);
3354
3355 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3356 if (nsk->sk_family == AF_INET6)
3357 mptcp_copy_ip6_options(nsk, sk);
3358 else
3359 #endif
3360 mptcp_copy_ip_options(nsk, sk);
3361
3362 msk = mptcp_sk(nsk);
3363 WRITE_ONCE(msk->local_key, subflow_req->local_key);
3364 WRITE_ONCE(msk->token, subflow_req->token);
3365 msk->in_accept_queue = 1;
3366 WRITE_ONCE(msk->fully_established, false);
3367 if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3368 WRITE_ONCE(msk->csum_enabled, true);
3369
3370 WRITE_ONCE(msk->write_seq, subflow_req->idsn + 1);
3371 WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3372 WRITE_ONCE(msk->snd_una, msk->write_seq);
3373 WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3374 msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3375 mptcp_init_sched(msk, mptcp_sk(sk)->sched);
3376
3377 /* passive msk is created after the first/MPC subflow */
3378 msk->subflow_id = 2;
3379
3380 sock_reset_flag(nsk, SOCK_RCU_FREE);
3381 security_inet_csk_clone(nsk, req);
3382
3383 /* this can't race with mptcp_close(), as the msk is
3384 * not yet exposted to user-space
3385 */
3386 mptcp_set_state(nsk, TCP_ESTABLISHED);
3387
3388 /* The msk maintain a ref to each subflow in the connections list */
3389 WRITE_ONCE(msk->first, ssk);
3390 subflow = mptcp_subflow_ctx(ssk);
3391 list_add(&subflow->node, &msk->conn_list);
3392 sock_hold(ssk);
3393
3394 /* new mpc subflow takes ownership of the newly
3395 * created mptcp socket
3396 */
3397 mptcp_token_accept(subflow_req, msk);
3398
3399 /* set msk addresses early to ensure mptcp_pm_get_local_id()
3400 * uses the correct data
3401 */
3402 mptcp_copy_inaddrs(nsk, ssk);
3403 __mptcp_propagate_sndbuf(nsk, ssk);
3404
3405 mptcp_rcv_space_init(msk, ssk);
3406
3407 if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
3408 __mptcp_subflow_fully_established(msk, subflow, mp_opt);
3409 bh_unlock_sock(nsk);
3410
3411 /* note: the newly allocated socket refcount is 2 now */
3412 return nsk;
3413 }
3414
mptcp_rcv_space_init(struct mptcp_sock * msk,const struct sock * ssk)3415 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3416 {
3417 const struct tcp_sock *tp = tcp_sk(ssk);
3418
3419 msk->rcvspace_init = 1;
3420 msk->rcvq_space.copied = 0;
3421 msk->rcvq_space.rtt_us = 0;
3422
3423 msk->rcvq_space.time = tp->tcp_mstamp;
3424
3425 /* initial rcv_space offering made to peer */
3426 msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3427 TCP_INIT_CWND * tp->advmss);
3428 if (msk->rcvq_space.space == 0)
3429 msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3430 }
3431
mptcp_destroy_common(struct mptcp_sock * msk)3432 void mptcp_destroy_common(struct mptcp_sock *msk)
3433 {
3434 struct mptcp_subflow_context *subflow, *tmp;
3435 struct sock *sk = (struct sock *)msk;
3436
3437 __mptcp_clear_xmit(sk);
3438
3439 /* join list will be eventually flushed (with rst) at sock lock release time */
3440 mptcp_for_each_subflow_safe(msk, subflow, tmp)
3441 __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, 0);
3442
3443 __skb_queue_purge(&sk->sk_receive_queue);
3444 skb_rbtree_purge(&msk->out_of_order_queue);
3445
3446 /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3447 * inet_sock_destruct() will dispose it
3448 */
3449 mptcp_token_destroy(msk);
3450 mptcp_pm_destroy(msk);
3451 }
3452
mptcp_destroy(struct sock * sk)3453 static void mptcp_destroy(struct sock *sk)
3454 {
3455 struct mptcp_sock *msk = mptcp_sk(sk);
3456
3457 /* allow the following to close even the initial subflow */
3458 msk->free_first = 1;
3459 mptcp_destroy_common(msk);
3460 sk_sockets_allocated_dec(sk);
3461 }
3462
__mptcp_data_acked(struct sock * sk)3463 void __mptcp_data_acked(struct sock *sk)
3464 {
3465 if (!sock_owned_by_user(sk))
3466 __mptcp_clean_una(sk);
3467 else
3468 __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3469 }
3470
__mptcp_check_push(struct sock * sk,struct sock * ssk)3471 void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3472 {
3473 if (!sock_owned_by_user(sk))
3474 __mptcp_subflow_push_pending(sk, ssk, false);
3475 else
3476 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3477 }
3478
3479 #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3480 BIT(MPTCP_RETRANSMIT) | \
3481 BIT(MPTCP_FLUSH_JOIN_LIST) | \
3482 BIT(MPTCP_DEQUEUE))
3483
3484 /* processes deferred events and flush wmem */
mptcp_release_cb(struct sock * sk)3485 static void mptcp_release_cb(struct sock *sk)
3486 __must_hold(&sk->sk_lock.slock)
3487 {
3488 struct mptcp_sock *msk = mptcp_sk(sk);
3489
3490 for (;;) {
3491 unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
3492 struct list_head join_list;
3493
3494 if (!flags)
3495 break;
3496
3497 INIT_LIST_HEAD(&join_list);
3498 list_splice_init(&msk->join_list, &join_list);
3499
3500 /* the following actions acquire the subflow socket lock
3501 *
3502 * 1) can't be invoked in atomic scope
3503 * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3504 * datapath acquires the msk socket spinlock while helding
3505 * the subflow socket lock
3506 */
3507 msk->cb_flags &= ~flags;
3508 spin_unlock_bh(&sk->sk_lock.slock);
3509
3510 if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3511 __mptcp_flush_join_list(sk, &join_list);
3512 if (flags & BIT(MPTCP_PUSH_PENDING))
3513 __mptcp_push_pending(sk, 0);
3514 if (flags & BIT(MPTCP_RETRANSMIT))
3515 __mptcp_retrans(sk);
3516 if ((flags & BIT(MPTCP_DEQUEUE)) && __mptcp_move_skbs(sk)) {
3517 /* notify ack seq update */
3518 mptcp_cleanup_rbuf(msk, 0);
3519 sk->sk_data_ready(sk);
3520 }
3521
3522 cond_resched();
3523 spin_lock_bh(&sk->sk_lock.slock);
3524 }
3525
3526 if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
3527 __mptcp_clean_una_wakeup(sk);
3528 if (unlikely(msk->cb_flags)) {
3529 /* be sure to sync the msk state before taking actions
3530 * depending on sk_state (MPTCP_ERROR_REPORT)
3531 * On sk release avoid actions depending on the first subflow
3532 */
3533 if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
3534 __mptcp_sync_state(sk, msk->pending_state);
3535 if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
3536 __mptcp_error_report(sk);
3537 if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
3538 __mptcp_sync_sndbuf(sk);
3539 }
3540 }
3541
3542 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3543 * TCP can't schedule delack timer before the subflow is fully established.
3544 * MPTCP uses the delack timer to do 3rd ack retransmissions
3545 */
schedule_3rdack_retransmission(struct sock * ssk)3546 static void schedule_3rdack_retransmission(struct sock *ssk)
3547 {
3548 struct inet_connection_sock *icsk = inet_csk(ssk);
3549 struct tcp_sock *tp = tcp_sk(ssk);
3550 unsigned long timeout;
3551
3552 if (READ_ONCE(mptcp_subflow_ctx(ssk)->fully_established))
3553 return;
3554
3555 /* reschedule with a timeout above RTT, as we must look only for drop */
3556 if (tp->srtt_us)
3557 timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3558 else
3559 timeout = TCP_TIMEOUT_INIT;
3560 timeout += jiffies;
3561
3562 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3563 smp_store_release(&icsk->icsk_ack.pending,
3564 icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER);
3565 sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3566 }
3567
mptcp_subflow_process_delegated(struct sock * ssk,long status)3568 void mptcp_subflow_process_delegated(struct sock *ssk, long status)
3569 {
3570 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3571 struct sock *sk = subflow->conn;
3572
3573 if (status & BIT(MPTCP_DELEGATE_SEND)) {
3574 mptcp_data_lock(sk);
3575 if (!sock_owned_by_user(sk))
3576 __mptcp_subflow_push_pending(sk, ssk, true);
3577 else
3578 __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3579 mptcp_data_unlock(sk);
3580 }
3581 if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
3582 mptcp_data_lock(sk);
3583 if (!sock_owned_by_user(sk))
3584 __mptcp_sync_sndbuf(sk);
3585 else
3586 __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
3587 mptcp_data_unlock(sk);
3588 }
3589 if (status & BIT(MPTCP_DELEGATE_ACK))
3590 schedule_3rdack_retransmission(ssk);
3591 }
3592
mptcp_hash(struct sock * sk)3593 static int mptcp_hash(struct sock *sk)
3594 {
3595 /* should never be called,
3596 * we hash the TCP subflows not the MPTCP socket
3597 */
3598 WARN_ON_ONCE(1);
3599 return 0;
3600 }
3601
mptcp_unhash(struct sock * sk)3602 static void mptcp_unhash(struct sock *sk)
3603 {
3604 /* called from sk_common_release(), but nothing to do here */
3605 }
3606
mptcp_get_port(struct sock * sk,unsigned short snum)3607 static int mptcp_get_port(struct sock *sk, unsigned short snum)
3608 {
3609 struct mptcp_sock *msk = mptcp_sk(sk);
3610
3611 pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
3612 if (WARN_ON_ONCE(!msk->first))
3613 return -EINVAL;
3614
3615 return inet_csk_get_port(msk->first, snum);
3616 }
3617
mptcp_finish_connect(struct sock * ssk)3618 void mptcp_finish_connect(struct sock *ssk)
3619 {
3620 struct mptcp_subflow_context *subflow;
3621 struct mptcp_sock *msk;
3622 struct sock *sk;
3623
3624 subflow = mptcp_subflow_ctx(ssk);
3625 sk = subflow->conn;
3626 msk = mptcp_sk(sk);
3627
3628 pr_debug("msk=%p, token=%u\n", sk, subflow->token);
3629
3630 subflow->map_seq = subflow->iasn;
3631 subflow->map_subflow_seq = 1;
3632
3633 /* the socket is not connected yet, no msk/subflow ops can access/race
3634 * accessing the field below
3635 */
3636 WRITE_ONCE(msk->local_key, subflow->local_key);
3637
3638 mptcp_pm_new_connection(msk, ssk, 0);
3639 }
3640
mptcp_sock_graft(struct sock * sk,struct socket * parent)3641 void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3642 {
3643 write_lock_bh(&sk->sk_callback_lock);
3644 rcu_assign_pointer(sk->sk_wq, &parent->wq);
3645 sk_set_socket(sk, parent);
3646 write_unlock_bh(&sk->sk_callback_lock);
3647 }
3648
mptcp_finish_join(struct sock * ssk)3649 bool mptcp_finish_join(struct sock *ssk)
3650 {
3651 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3652 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3653 struct sock *parent = (void *)msk;
3654 bool ret = true;
3655
3656 pr_debug("msk=%p, subflow=%p\n", msk, subflow);
3657
3658 /* mptcp socket already closing? */
3659 if (!mptcp_is_fully_established(parent)) {
3660 subflow->reset_reason = MPTCP_RST_EMPTCP;
3661 return false;
3662 }
3663
3664 /* active subflow, already present inside the conn_list */
3665 if (!list_empty(&subflow->node)) {
3666 spin_lock_bh(&msk->fallback_lock);
3667 if (!msk->allow_subflows) {
3668 spin_unlock_bh(&msk->fallback_lock);
3669 return false;
3670 }
3671 mptcp_subflow_joined(msk, ssk);
3672 spin_unlock_bh(&msk->fallback_lock);
3673 mptcp_propagate_sndbuf(parent, ssk);
3674 return true;
3675 }
3676
3677 if (!mptcp_pm_allow_new_subflow(msk)) {
3678 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_JOINREJECTED);
3679 goto err_prohibited;
3680 }
3681
3682 /* If we can't acquire msk socket lock here, let the release callback
3683 * handle it
3684 */
3685 mptcp_data_lock(parent);
3686 if (!sock_owned_by_user(parent)) {
3687 ret = __mptcp_finish_join(msk, ssk);
3688 if (ret) {
3689 sock_hold(ssk);
3690 list_add_tail(&subflow->node, &msk->conn_list);
3691 }
3692 } else {
3693 sock_hold(ssk);
3694 list_add_tail(&subflow->node, &msk->join_list);
3695 __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
3696 }
3697 mptcp_data_unlock(parent);
3698
3699 if (!ret) {
3700 err_prohibited:
3701 subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3702 return false;
3703 }
3704
3705 return true;
3706 }
3707
mptcp_shutdown(struct sock * sk,int how)3708 static void mptcp_shutdown(struct sock *sk, int how)
3709 {
3710 pr_debug("sk=%p, how=%d\n", sk, how);
3711
3712 if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3713 __mptcp_wr_shutdown(sk);
3714 }
3715
mptcp_ioctl_outq(const struct mptcp_sock * msk,u64 v)3716 static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3717 {
3718 const struct sock *sk = (void *)msk;
3719 u64 delta;
3720
3721 if (sk->sk_state == TCP_LISTEN)
3722 return -EINVAL;
3723
3724 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3725 return 0;
3726
3727 delta = msk->write_seq - v;
3728 if (__mptcp_check_fallback(msk) && msk->first) {
3729 struct tcp_sock *tp = tcp_sk(msk->first);
3730
3731 /* the first subflow is disconnected after close - see
3732 * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3733 * so ignore that status, too.
3734 */
3735 if (!((1 << msk->first->sk_state) &
3736 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3737 delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3738 }
3739 if (delta > INT_MAX)
3740 delta = INT_MAX;
3741
3742 return (int)delta;
3743 }
3744
mptcp_ioctl(struct sock * sk,int cmd,int * karg)3745 static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3746 {
3747 struct mptcp_sock *msk = mptcp_sk(sk);
3748 bool slow;
3749
3750 switch (cmd) {
3751 case SIOCINQ:
3752 if (sk->sk_state == TCP_LISTEN)
3753 return -EINVAL;
3754
3755 lock_sock(sk);
3756 if (__mptcp_move_skbs(sk))
3757 mptcp_cleanup_rbuf(msk, 0);
3758 *karg = mptcp_inq_hint(sk);
3759 release_sock(sk);
3760 break;
3761 case SIOCOUTQ:
3762 slow = lock_sock_fast(sk);
3763 *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3764 unlock_sock_fast(sk, slow);
3765 break;
3766 case SIOCOUTQNSD:
3767 slow = lock_sock_fast(sk);
3768 *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3769 unlock_sock_fast(sk, slow);
3770 break;
3771 default:
3772 return -ENOIOCTLCMD;
3773 }
3774
3775 return 0;
3776 }
3777
mptcp_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)3778 static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3779 {
3780 struct mptcp_subflow_context *subflow;
3781 struct mptcp_sock *msk = mptcp_sk(sk);
3782 int err = -EINVAL;
3783 struct sock *ssk;
3784
3785 ssk = __mptcp_nmpc_sk(msk);
3786 if (IS_ERR(ssk))
3787 return PTR_ERR(ssk);
3788
3789 mptcp_set_state(sk, TCP_SYN_SENT);
3790 subflow = mptcp_subflow_ctx(ssk);
3791 #ifdef CONFIG_TCP_MD5SIG
3792 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3793 * TCP option space.
3794 */
3795 if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
3796 mptcp_early_fallback(msk, subflow, MPTCP_MIB_MD5SIGFALLBACK);
3797 #endif
3798 if (subflow->request_mptcp) {
3799 if (mptcp_active_should_disable(sk))
3800 mptcp_early_fallback(msk, subflow,
3801 MPTCP_MIB_MPCAPABLEACTIVEDISABLED);
3802 else if (mptcp_token_new_connect(ssk) < 0)
3803 mptcp_early_fallback(msk, subflow,
3804 MPTCP_MIB_TOKENFALLBACKINIT);
3805 }
3806
3807 WRITE_ONCE(msk->write_seq, subflow->idsn);
3808 WRITE_ONCE(msk->snd_nxt, subflow->idsn);
3809 WRITE_ONCE(msk->snd_una, subflow->idsn);
3810 if (likely(!__mptcp_check_fallback(msk)))
3811 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3812
3813 /* if reaching here via the fastopen/sendmsg path, the caller already
3814 * acquired the subflow socket lock, too.
3815 */
3816 if (!msk->fastopening)
3817 lock_sock(ssk);
3818
3819 /* the following mirrors closely a very small chunk of code from
3820 * __inet_stream_connect()
3821 */
3822 if (ssk->sk_state != TCP_CLOSE)
3823 goto out;
3824
3825 if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
3826 err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3827 if (err)
3828 goto out;
3829 }
3830
3831 err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3832 if (err < 0)
3833 goto out;
3834
3835 inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3836
3837 out:
3838 if (!msk->fastopening)
3839 release_sock(ssk);
3840
3841 /* on successful connect, the msk state will be moved to established by
3842 * subflow_finish_connect()
3843 */
3844 if (unlikely(err)) {
3845 /* avoid leaving a dangling token in an unconnected socket */
3846 mptcp_token_destroy(msk);
3847 mptcp_set_state(sk, TCP_CLOSE);
3848 return err;
3849 }
3850
3851 mptcp_copy_inaddrs(sk, ssk);
3852 return 0;
3853 }
3854
3855 static struct proto mptcp_prot = {
3856 .name = "MPTCP",
3857 .owner = THIS_MODULE,
3858 .init = mptcp_init_sock,
3859 .connect = mptcp_connect,
3860 .disconnect = mptcp_disconnect,
3861 .close = mptcp_close,
3862 .setsockopt = mptcp_setsockopt,
3863 .getsockopt = mptcp_getsockopt,
3864 .shutdown = mptcp_shutdown,
3865 .destroy = mptcp_destroy,
3866 .sendmsg = mptcp_sendmsg,
3867 .ioctl = mptcp_ioctl,
3868 .recvmsg = mptcp_recvmsg,
3869 .release_cb = mptcp_release_cb,
3870 .hash = mptcp_hash,
3871 .unhash = mptcp_unhash,
3872 .get_port = mptcp_get_port,
3873 .stream_memory_free = mptcp_stream_memory_free,
3874 .sockets_allocated = &mptcp_sockets_allocated,
3875
3876 .memory_allocated = &net_aligned_data.tcp_memory_allocated,
3877 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3878
3879 .memory_pressure = &tcp_memory_pressure,
3880 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3881 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3882 .sysctl_mem = sysctl_tcp_mem,
3883 .obj_size = sizeof(struct mptcp_sock),
3884 .slab_flags = SLAB_TYPESAFE_BY_RCU,
3885 .no_autobind = true,
3886 };
3887
mptcp_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)3888 static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3889 {
3890 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3891 struct sock *ssk, *sk = sock->sk;
3892 int err = -EINVAL;
3893
3894 lock_sock(sk);
3895 ssk = __mptcp_nmpc_sk(msk);
3896 if (IS_ERR(ssk)) {
3897 err = PTR_ERR(ssk);
3898 goto unlock;
3899 }
3900
3901 if (sk->sk_family == AF_INET)
3902 err = inet_bind_sk(ssk, uaddr, addr_len);
3903 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3904 else if (sk->sk_family == AF_INET6)
3905 err = inet6_bind_sk(ssk, uaddr, addr_len);
3906 #endif
3907 if (!err)
3908 mptcp_copy_inaddrs(sk, ssk);
3909
3910 unlock:
3911 release_sock(sk);
3912 return err;
3913 }
3914
mptcp_listen(struct socket * sock,int backlog)3915 static int mptcp_listen(struct socket *sock, int backlog)
3916 {
3917 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3918 struct sock *sk = sock->sk;
3919 struct sock *ssk;
3920 int err;
3921
3922 pr_debug("msk=%p\n", msk);
3923
3924 lock_sock(sk);
3925
3926 err = -EINVAL;
3927 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
3928 goto unlock;
3929
3930 ssk = __mptcp_nmpc_sk(msk);
3931 if (IS_ERR(ssk)) {
3932 err = PTR_ERR(ssk);
3933 goto unlock;
3934 }
3935
3936 mptcp_set_state(sk, TCP_LISTEN);
3937 sock_set_flag(sk, SOCK_RCU_FREE);
3938
3939 lock_sock(ssk);
3940 err = __inet_listen_sk(ssk, backlog);
3941 release_sock(ssk);
3942 mptcp_set_state(sk, inet_sk_state_load(ssk));
3943
3944 if (!err) {
3945 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3946 mptcp_copy_inaddrs(sk, ssk);
3947 mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
3948 }
3949
3950 unlock:
3951 release_sock(sk);
3952 return err;
3953 }
3954
mptcp_stream_accept(struct socket * sock,struct socket * newsock,struct proto_accept_arg * arg)3955 static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3956 struct proto_accept_arg *arg)
3957 {
3958 struct mptcp_sock *msk = mptcp_sk(sock->sk);
3959 struct sock *ssk, *newsk;
3960
3961 pr_debug("msk=%p\n", msk);
3962
3963 /* Buggy applications can call accept on socket states other then LISTEN
3964 * but no need to allocate the first subflow just to error out.
3965 */
3966 ssk = READ_ONCE(msk->first);
3967 if (!ssk)
3968 return -EINVAL;
3969
3970 pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
3971 newsk = inet_csk_accept(ssk, arg);
3972 if (!newsk)
3973 return arg->err;
3974
3975 pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
3976 if (sk_is_mptcp(newsk)) {
3977 struct mptcp_subflow_context *subflow;
3978 struct sock *new_mptcp_sock;
3979
3980 subflow = mptcp_subflow_ctx(newsk);
3981 new_mptcp_sock = subflow->conn;
3982
3983 /* is_mptcp should be false if subflow->conn is missing, see
3984 * subflow_syn_recv_sock()
3985 */
3986 if (WARN_ON_ONCE(!new_mptcp_sock)) {
3987 tcp_sk(newsk)->is_mptcp = 0;
3988 goto tcpfallback;
3989 }
3990
3991 newsk = new_mptcp_sock;
3992 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3993
3994 newsk->sk_kern_sock = arg->kern;
3995 lock_sock(newsk);
3996 __inet_accept(sock, newsock, newsk);
3997
3998 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3999 msk = mptcp_sk(newsk);
4000 msk->in_accept_queue = 0;
4001
4002 /* set ssk->sk_socket of accept()ed flows to mptcp socket.
4003 * This is needed so NOSPACE flag can be set from tcp stack.
4004 */
4005 mptcp_for_each_subflow(msk, subflow) {
4006 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4007
4008 if (!ssk->sk_socket)
4009 mptcp_sock_graft(ssk, newsock);
4010 }
4011
4012 mptcp_rps_record_subflows(msk);
4013
4014 /* Do late cleanup for the first subflow as necessary. Also
4015 * deal with bad peers not doing a complete shutdown.
4016 */
4017 if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
4018 __mptcp_close_ssk(newsk, msk->first,
4019 mptcp_subflow_ctx(msk->first), 0);
4020 if (unlikely(list_is_singular(&msk->conn_list)))
4021 mptcp_set_state(newsk, TCP_CLOSE);
4022 }
4023 } else {
4024 tcpfallback:
4025 newsk->sk_kern_sock = arg->kern;
4026 lock_sock(newsk);
4027 __inet_accept(sock, newsock, newsk);
4028 /* we are being invoked after accepting a non-mp-capable
4029 * flow: sk is a tcp_sk, not an mptcp one.
4030 *
4031 * Hand the socket over to tcp so all further socket ops
4032 * bypass mptcp.
4033 */
4034 WRITE_ONCE(newsock->sk->sk_socket->ops,
4035 mptcp_fallback_tcp_ops(newsock->sk));
4036 }
4037 release_sock(newsk);
4038
4039 return 0;
4040 }
4041
mptcp_check_writeable(struct mptcp_sock * msk)4042 static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
4043 {
4044 struct sock *sk = (struct sock *)msk;
4045
4046 if (__mptcp_stream_is_writeable(sk, 1))
4047 return EPOLLOUT | EPOLLWRNORM;
4048
4049 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
4050 smp_mb__after_atomic(); /* NOSPACE is changed by mptcp_write_space() */
4051 if (__mptcp_stream_is_writeable(sk, 1))
4052 return EPOLLOUT | EPOLLWRNORM;
4053
4054 return 0;
4055 }
4056
mptcp_poll(struct file * file,struct socket * sock,struct poll_table_struct * wait)4057 static __poll_t mptcp_poll(struct file *file, struct socket *sock,
4058 struct poll_table_struct *wait)
4059 {
4060 struct sock *sk = sock->sk;
4061 struct mptcp_sock *msk;
4062 __poll_t mask = 0;
4063 u8 shutdown;
4064 int state;
4065
4066 msk = mptcp_sk(sk);
4067 sock_poll_wait(file, sock, wait);
4068
4069 state = inet_sk_state_load(sk);
4070 pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
4071 if (state == TCP_LISTEN) {
4072 struct sock *ssk = READ_ONCE(msk->first);
4073
4074 if (WARN_ON_ONCE(!ssk))
4075 return 0;
4076
4077 return inet_csk_listen_poll(ssk);
4078 }
4079
4080 shutdown = READ_ONCE(sk->sk_shutdown);
4081 if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
4082 mask |= EPOLLHUP;
4083 if (shutdown & RCV_SHUTDOWN)
4084 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
4085
4086 if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
4087 mask |= mptcp_check_readable(sk);
4088 if (shutdown & SEND_SHUTDOWN)
4089 mask |= EPOLLOUT | EPOLLWRNORM;
4090 else
4091 mask |= mptcp_check_writeable(msk);
4092 } else if (state == TCP_SYN_SENT &&
4093 inet_test_bit(DEFER_CONNECT, sk)) {
4094 /* cf tcp_poll() note about TFO */
4095 mask |= EPOLLOUT | EPOLLWRNORM;
4096 }
4097
4098 /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
4099 smp_rmb();
4100 if (READ_ONCE(sk->sk_err))
4101 mask |= EPOLLERR;
4102
4103 return mask;
4104 }
4105
4106 static const struct proto_ops mptcp_stream_ops = {
4107 .family = PF_INET,
4108 .owner = THIS_MODULE,
4109 .release = inet_release,
4110 .bind = mptcp_bind,
4111 .connect = inet_stream_connect,
4112 .socketpair = sock_no_socketpair,
4113 .accept = mptcp_stream_accept,
4114 .getname = inet_getname,
4115 .poll = mptcp_poll,
4116 .ioctl = inet_ioctl,
4117 .gettstamp = sock_gettstamp,
4118 .listen = mptcp_listen,
4119 .shutdown = inet_shutdown,
4120 .setsockopt = sock_common_setsockopt,
4121 .getsockopt = sock_common_getsockopt,
4122 .sendmsg = inet_sendmsg,
4123 .recvmsg = inet_recvmsg,
4124 .mmap = sock_no_mmap,
4125 .set_rcvlowat = mptcp_set_rcvlowat,
4126 };
4127
4128 static struct inet_protosw mptcp_protosw = {
4129 .type = SOCK_STREAM,
4130 .protocol = IPPROTO_MPTCP,
4131 .prot = &mptcp_prot,
4132 .ops = &mptcp_stream_ops,
4133 .flags = INET_PROTOSW_ICSK,
4134 };
4135
mptcp_napi_poll(struct napi_struct * napi,int budget)4136 static int mptcp_napi_poll(struct napi_struct *napi, int budget)
4137 {
4138 struct mptcp_delegated_action *delegated;
4139 struct mptcp_subflow_context *subflow;
4140 int work_done = 0;
4141
4142 delegated = container_of(napi, struct mptcp_delegated_action, napi);
4143 while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
4144 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4145
4146 bh_lock_sock_nested(ssk);
4147 if (!sock_owned_by_user(ssk)) {
4148 mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
4149 } else {
4150 /* tcp_release_cb_override already processed
4151 * the action or will do at next release_sock().
4152 * In both case must dequeue the subflow here - on the same
4153 * CPU that scheduled it.
4154 */
4155 smp_wmb();
4156 clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
4157 }
4158 bh_unlock_sock(ssk);
4159 sock_put(ssk);
4160
4161 if (++work_done == budget)
4162 return budget;
4163 }
4164
4165 /* always provide a 0 'work_done' argument, so that napi_complete_done
4166 * will not try accessing the NULL napi->dev ptr
4167 */
4168 napi_complete_done(napi, 0);
4169 return work_done;
4170 }
4171
mptcp_proto_init(void)4172 void __init mptcp_proto_init(void)
4173 {
4174 struct mptcp_delegated_action *delegated;
4175 int cpu;
4176
4177 mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
4178
4179 if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4180 panic("Failed to allocate MPTCP pcpu counter\n");
4181
4182 mptcp_napi_dev = alloc_netdev_dummy(0);
4183 if (!mptcp_napi_dev)
4184 panic("Failed to allocate MPTCP dummy netdev\n");
4185 for_each_possible_cpu(cpu) {
4186 delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4187 INIT_LIST_HEAD(&delegated->head);
4188 netif_napi_add_tx(mptcp_napi_dev, &delegated->napi,
4189 mptcp_napi_poll);
4190 napi_enable(&delegated->napi);
4191 }
4192
4193 mptcp_subflow_init();
4194 mptcp_pm_init();
4195 mptcp_sched_init();
4196 mptcp_token_init();
4197
4198 if (proto_register(&mptcp_prot, 1) != 0)
4199 panic("Failed to register MPTCP proto.\n");
4200
4201 inet_register_protosw(&mptcp_protosw);
4202
4203 BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4204 }
4205
4206 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4207 static const struct proto_ops mptcp_v6_stream_ops = {
4208 .family = PF_INET6,
4209 .owner = THIS_MODULE,
4210 .release = inet6_release,
4211 .bind = mptcp_bind,
4212 .connect = inet_stream_connect,
4213 .socketpair = sock_no_socketpair,
4214 .accept = mptcp_stream_accept,
4215 .getname = inet6_getname,
4216 .poll = mptcp_poll,
4217 .ioctl = inet6_ioctl,
4218 .gettstamp = sock_gettstamp,
4219 .listen = mptcp_listen,
4220 .shutdown = inet_shutdown,
4221 .setsockopt = sock_common_setsockopt,
4222 .getsockopt = sock_common_getsockopt,
4223 .sendmsg = inet6_sendmsg,
4224 .recvmsg = inet6_recvmsg,
4225 .mmap = sock_no_mmap,
4226 #ifdef CONFIG_COMPAT
4227 .compat_ioctl = inet6_compat_ioctl,
4228 #endif
4229 .set_rcvlowat = mptcp_set_rcvlowat,
4230 };
4231
4232 static struct proto mptcp_v6_prot;
4233
4234 static struct inet_protosw mptcp_v6_protosw = {
4235 .type = SOCK_STREAM,
4236 .protocol = IPPROTO_MPTCP,
4237 .prot = &mptcp_v6_prot,
4238 .ops = &mptcp_v6_stream_ops,
4239 .flags = INET_PROTOSW_ICSK,
4240 };
4241
mptcp_proto_v6_init(void)4242 int __init mptcp_proto_v6_init(void)
4243 {
4244 int err;
4245
4246 mptcp_v6_prot = mptcp_prot;
4247 strscpy(mptcp_v6_prot.name, "MPTCPv6", sizeof(mptcp_v6_prot.name));
4248 mptcp_v6_prot.slab = NULL;
4249 mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4250 mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4251
4252 err = proto_register(&mptcp_v6_prot, 1);
4253 if (err)
4254 return err;
4255
4256 err = inet6_register_protosw(&mptcp_v6_protosw);
4257 if (err)
4258 proto_unregister(&mptcp_v6_prot);
4259
4260 return err;
4261 }
4262 #endif
4263