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