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