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