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