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