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