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