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