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