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