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