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