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