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