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/sock.h>
15 : : #include <net/inet_common.h>
16 : : #include <net/inet_hashtables.h>
17 : : #include <net/protocol.h>
18 : : #include <net/tcp_states.h>
19 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
20 : : #include <net/transp_v6.h>
21 : : #endif
22 : : #include <net/mptcp.h>
23 : : #include <net/hotdata.h>
24 : : #include <net/xfrm.h>
25 : : #include <asm/ioctls.h>
26 : : #include "protocol.h"
27 : : #include "mib.h"
28 : :
29 : : #define CREATE_TRACE_POINTS
30 : : #include <trace/events/mptcp.h>
31 : :
32 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
33 : : struct mptcp6_sock {
34 : : struct mptcp_sock msk;
35 : : struct ipv6_pinfo np;
36 : : };
37 : : #endif
38 : :
39 : : enum {
40 : : MPTCP_CMSG_TS = BIT(0),
41 : : MPTCP_CMSG_INQ = BIT(1),
42 : : };
43 : :
44 : : static struct percpu_counter mptcp_sockets_allocated ____cacheline_aligned_in_smp;
45 : :
46 : : static void __mptcp_destroy_sock(struct sock *sk);
47 : : static void mptcp_check_send_data_fin(struct sock *sk);
48 : :
49 : : DEFINE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
50 : : static struct net_device *mptcp_napi_dev;
51 : :
52 : : /* Returns end sequence number of the receiver's advertised window */
53 : 344313 : u64 mptcp_wnd_end(const struct mptcp_sock *msk)
54 : : {
55 : 344313 : return READ_ONCE(msk->wnd_end);
56 : : }
57 : :
58 : 94 : static const struct proto_ops *mptcp_fallback_tcp_ops(const struct sock *sk)
59 : : {
60 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
61 [ + + ]: 94 : if (sk->sk_prot == &tcpv6_prot)
62 : : return &inet6_stream_ops;
63 : : #endif
64 [ - + ]: 40 : WARN_ON_ONCE(sk->sk_prot != &tcp_prot);
65 : : return &inet_stream_ops;
66 : : }
67 : :
68 : 2518 : static int __mptcp_socket_create(struct mptcp_sock *msk)
69 : : {
70 : 2518 : struct mptcp_subflow_context *subflow;
71 : 2518 : struct sock *sk = (struct sock *)msk;
72 : 2518 : struct socket *ssock;
73 : 2518 : int err;
74 : :
75 : 2518 : err = mptcp_subflow_create_socket(sk, sk->sk_family, &ssock);
76 [ + - ]: 2518 : if (err)
77 : : return err;
78 : :
79 [ - + ]: 2518 : msk->scaling_ratio = tcp_sk(ssock->sk)->scaling_ratio;
80 : 2518 : WRITE_ONCE(msk->first, ssock->sk);
81 : 2518 : subflow = mptcp_subflow_ctx(ssock->sk);
82 : 2518 : list_add(&subflow->node, &msk->conn_list);
83 : 2518 : sock_hold(ssock->sk);
84 : 2518 : subflow->request_mptcp = 1;
85 : 2518 : subflow->subflow_id = msk->subflow_id++;
86 : :
87 : : /* This is the first subflow, always with id 0 */
88 : 2518 : WRITE_ONCE(subflow->local_id, 0);
89 : 2518 : mptcp_sock_graft(msk->first, sk->sk_socket);
90 : 2518 : iput(SOCK_INODE(ssock));
91 : :
92 : 2518 : return 0;
93 : : }
94 : :
95 : : /* If the MPC handshake is not started, returns the first subflow,
96 : : * eventually allocating it.
97 : : */
98 : 7156 : struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk)
99 : : {
100 : 7156 : struct sock *sk = (struct sock *)msk;
101 : 7156 : int ret;
102 : :
103 [ + + + + ]: 7156 : if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
104 : : return ERR_PTR(-EINVAL);
105 : :
106 [ + + ]: 7138 : if (!msk->first) {
107 : 2518 : ret = __mptcp_socket_create(msk);
108 [ - + ]: 2518 : if (ret)
109 : 0 : return ERR_PTR(ret);
110 : : }
111 : :
112 : 7138 : return msk->first;
113 : : }
114 : :
115 : 0 : static void mptcp_drop(struct sock *sk, struct sk_buff *skb)
116 : : {
117 : 0 : sk_drops_add(sk, skb);
118 : 0 : __kfree_skb(skb);
119 : : }
120 : :
121 : 319444 : static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
122 : : struct sk_buff *from)
123 : : {
124 : 319444 : bool fragstolen;
125 : 319444 : int delta;
126 : :
127 [ + + ]: 319444 : if (unlikely(MPTCP_SKB_CB(to)->cant_coalesce) ||
128 [ + + ]: 319426 : MPTCP_SKB_CB(from)->offset ||
129 [ + + + + ]: 622558 : ((to->len + from->len) > (sk->sk_rcvbuf >> 3)) ||
130 : 303153 : !skb_try_coalesce(to, from, &fragstolen, &delta))
131 : 90855 : return false;
132 : :
133 [ - + ]: 228589 : pr_debug("colesced seq %llx into %llx new len %d new end seq %llx\n",
134 : : MPTCP_SKB_CB(from)->map_seq, MPTCP_SKB_CB(to)->map_seq,
135 : : to->len, MPTCP_SKB_CB(from)->end_seq);
136 : 228589 : MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
137 : :
138 : : /* note the fwd memory can reach a negative value after accounting
139 : : * for the delta, but the later skb free will restore a non
140 : : * negative one
141 : : */
142 : 228589 : atomic_add(delta, &sk->sk_rmem_alloc);
143 [ + - ]: 228589 : sk_mem_charge(sk, delta);
144 [ - + ]: 228589 : kfree_skb_partial(from, fragstolen);
145 : :
146 : 228589 : return true;
147 : : }
148 : :
149 : 25448 : static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
150 : : struct sk_buff *from)
151 : : {
152 [ + + ]: 42953 : if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
153 : : return false;
154 : :
155 : 107389 : return mptcp_try_coalesce((struct sock *)msk, to, from);
156 : : }
157 : :
158 : : /* "inspired" by tcp_data_queue_ofo(), main differences:
159 : : * - use mptcp seqs
160 : : * - don't cope with sacks
161 : : */
162 : 92818 : static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
163 : : {
164 : 92818 : struct sock *sk = (struct sock *)msk;
165 : 92818 : struct rb_node **p, *parent;
166 : 92818 : u64 seq, end_seq, max_seq;
167 : 92818 : struct sk_buff *skb1;
168 : :
169 : 92818 : seq = MPTCP_SKB_CB(skb)->map_seq;
170 : 92818 : end_seq = MPTCP_SKB_CB(skb)->end_seq;
171 [ - + ]: 92818 : max_seq = atomic64_read(&msk->rcv_wnd_sent);
172 : :
173 [ - + ]: 92818 : pr_debug("msk=%p seq=%llx limit=%llx empty=%d\n", msk, seq, max_seq,
174 : : RB_EMPTY_ROOT(&msk->out_of_order_queue));
175 [ - + ]: 92818 : if (after64(end_seq, max_seq)) {
176 : : /* out of window */
177 : 0 : mptcp_drop(sk, skb);
178 [ # # ]: 0 : pr_debug("oow by %lld, rcv_wnd_sent %llu\n",
179 : : (unsigned long long)end_seq - (unsigned long)max_seq,
180 : : (unsigned long long)atomic64_read(&msk->rcv_wnd_sent));
181 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_NODSSWINDOW);
182 : 0 : return;
183 : : }
184 : :
185 : 92818 : p = &msk->out_of_order_queue.rb_node;
186 [ + - ]: 92818 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
187 [ + + ]: 92818 : if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
188 : 12337 : rb_link_node(&skb->rbnode, NULL, p);
189 : 12337 : rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
190 : 12337 : msk->ooo_last_skb = skb;
191 : 12337 : goto end;
192 : : }
193 : :
194 : : /* with 2 subflows, adding at end of ooo queue is quite likely
195 : : * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
196 : : */
197 [ + + + + ]: 137050 : if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
198 [ + - ]: 42609 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
199 [ + - ]: 42609 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
200 : 42609 : return;
201 : : }
202 : :
203 : : /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
204 [ + + ]: 37872 : if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
205 [ + - ]: 28005 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
206 : 28005 : parent = &msk->ooo_last_skb->rbnode;
207 : 28005 : p = &parent->rb_right;
208 : 28005 : goto insert;
209 : : }
210 : :
211 : : /* Find place to insert this segment. Handle overlaps on the way. */
212 : : parent = NULL;
213 [ + + ]: 41065 : while (*p) {
214 : 38353 : parent = *p;
215 : 38353 : skb1 = rb_to_skb(parent);
216 [ + + ]: 38353 : if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
217 : 10057 : p = &parent->rb_left;
218 : 10057 : continue;
219 : : }
220 [ + + ]: 28296 : if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
221 [ + + ]: 302 : if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
222 : : /* All the bits are present. Drop. */
223 : 141 : mptcp_drop(sk, skb);
224 [ + - ]: 141 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
225 : 141 : return;
226 : : }
227 [ + + ]: 161 : if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
228 : : /* partial overlap:
229 : : * | skb |
230 : : * | skb1 |
231 : : * continue traversing
232 : : */
233 : : } else {
234 : : /* skb's seq == skb1's seq and skb covers skb1.
235 : : * Replace skb1 with skb.
236 : : */
237 : 10 : rb_replace_node(&skb1->rbnode, &skb->rbnode,
238 : : &msk->out_of_order_queue);
239 : 10 : mptcp_drop(sk, skb1);
240 [ # # ]: 10 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
241 : 10 : goto merge_right;
242 : : }
243 [ + + + + ]: 36327 : } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
244 [ + - ]: 7004 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
245 : 7004 : return;
246 : : }
247 : 21141 : p = &parent->rb_right;
248 : : }
249 : :
250 : 2712 : insert:
251 : : /* Insert segment into RB tree. */
252 : 30717 : rb_link_node(&skb->rbnode, parent, p);
253 : 30717 : rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
254 : :
255 : : merge_right:
256 : : /* Remove other segments covered by skb. */
257 [ + + ]: 30772 : while ((skb1 = skb_rb_next(skb)) != NULL) {
258 [ + + ]: 2766 : if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
259 : : break;
260 : 45 : rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
261 : 45 : mptcp_drop(sk, skb1);
262 [ + - ]: 7581 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
263 : : }
264 : : /* If there is no skb after us, we are the last_skb ! */
265 [ + + ]: 30727 : if (!skb1)
266 : 28006 : msk->ooo_last_skb = skb;
267 : :
268 : 2721 : end:
269 : 43064 : skb_condense(skb);
270 : 43064 : skb_set_owner_r(skb, sk);
271 : : }
272 : :
273 : 473072 : static bool __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
274 : : struct sk_buff *skb, unsigned int offset,
275 : : size_t copy_len)
276 : : {
277 : 473072 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
278 : 473072 : struct sock *sk = (struct sock *)msk;
279 : 473072 : struct sk_buff *tail;
280 : 473072 : bool has_rxtstamp;
281 : :
282 : 473072 : __skb_unlink(skb, &ssk->sk_receive_queue);
283 : :
284 : 473072 : skb_ext_reset(skb);
285 : 473072 : skb_orphan(skb);
286 : :
287 : : /* try to fetch required memory from subflow */
288 [ - + ]: 473072 : if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
289 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RCVPRUNED);
290 : 0 : goto drop;
291 : : }
292 : :
293 : 473072 : has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
294 : :
295 : : /* the skb map_seq accounts for the skb offset:
296 : : * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq
297 : : * value
298 : : */
299 : 473072 : MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
300 : 473072 : MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
301 : 473072 : MPTCP_SKB_CB(skb)->offset = offset;
302 : 473072 : MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
303 : 473072 : MPTCP_SKB_CB(skb)->cant_coalesce = 0;
304 : :
305 [ + + ]: 473072 : if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
306 : : /* in sequence */
307 : 380202 : msk->bytes_received += copy_len;
308 : 380202 : WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
309 [ + + ]: 380202 : tail = skb_peek_tail(&sk->sk_receive_queue);
310 [ + - + + ]: 212055 : if (tail && mptcp_try_coalesce(sk, tail, skb))
311 : : return true;
312 : :
313 : 226242 : skb_set_owner_r(skb, sk);
314 : 226242 : __skb_queue_tail(&sk->sk_receive_queue, skb);
315 : 226242 : return true;
316 [ + + ]: 92870 : } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
317 : 92818 : mptcp_data_queue_ofo(msk, skb);
318 : 92818 : return false;
319 : : }
320 : :
321 : : /* old data, keep it simple and drop the whole pkt, sender
322 : : * will retransmit as needed, if needed.
323 : : */
324 [ + - ]: 52 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
325 : 52 : drop:
326 : 52 : mptcp_drop(sk, skb);
327 : 52 : return false;
328 : : }
329 : :
330 : 34607 : static void mptcp_stop_rtx_timer(struct sock *sk)
331 : : {
332 : 34607 : struct inet_connection_sock *icsk = inet_csk(sk);
333 : :
334 : 34607 : sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
335 [ - + ]: 34607 : mptcp_sk(sk)->timer_ival = 0;
336 : 34607 : }
337 : :
338 : 4381 : static void mptcp_close_wake_up(struct sock *sk)
339 : : {
340 [ + + ]: 4381 : if (sock_flag(sk, SOCK_DEAD))
341 : : return;
342 : :
343 : 3009 : sk->sk_state_change(sk);
344 [ + + ]: 3009 : if (sk->sk_shutdown == SHUTDOWN_MASK ||
345 [ + + ]: 1446 : sk->sk_state == TCP_CLOSE)
346 : 1615 : sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
347 : : else
348 : 1394 : sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
349 : : }
350 : :
351 : : /* called under the msk socket lock */
352 : 239829 : static bool mptcp_pending_data_fin_ack(struct sock *sk)
353 : : {
354 [ - + ]: 239829 : struct mptcp_sock *msk = mptcp_sk(sk);
355 : :
356 : 111783 : return ((1 << sk->sk_state) &
357 [ + + + + ]: 239829 : (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
358 [ + + ]: 29512 : msk->write_seq == READ_ONCE(msk->snd_una);
359 : : }
360 : :
361 : 13337 : static void mptcp_check_data_fin_ack(struct sock *sk)
362 : : {
363 [ - + ]: 13337 : struct mptcp_sock *msk = mptcp_sk(sk);
364 : :
365 : : /* Look for an acknowledged DATA_FIN */
366 [ + + ]: 13337 : if (mptcp_pending_data_fin_ack(sk)) {
367 : 1764 : WRITE_ONCE(msk->snd_data_fin_enable, 0);
368 : :
369 [ + + - ]: 1764 : switch (sk->sk_state) {
370 : 948 : case TCP_FIN_WAIT1:
371 : 948 : mptcp_set_state(sk, TCP_FIN_WAIT2);
372 : 948 : break;
373 : 816 : case TCP_CLOSING:
374 : : case TCP_LAST_ACK:
375 : 816 : mptcp_set_state(sk, TCP_CLOSE);
376 : 816 : break;
377 : : }
378 : :
379 : 1764 : mptcp_close_wake_up(sk);
380 : : }
381 : 13337 : }
382 : :
383 : : /* can be called with no lock acquired */
384 : 449089 : static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
385 : : {
386 [ - + ]: 449089 : struct mptcp_sock *msk = mptcp_sk(sk);
387 : :
388 [ + + + + : 467914 : if (READ_ONCE(msk->rcv_data_fin) &&
+ + + + ]
389 [ + + ]: 166 : ((1 << inet_sk_state_load(sk)) &
390 : : (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
391 : 18262 : u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
392 : :
393 [ + + ]: 18262 : if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
394 [ + + ]: 2014 : if (seq)
395 : 1733 : *seq = rcv_data_fin_seq;
396 : :
397 : 2014 : return true;
398 : : }
399 : : }
400 : :
401 : : return false;
402 : : }
403 : :
404 : 1151 : static void mptcp_set_datafin_timeout(struct sock *sk)
405 : : {
406 : 1151 : struct inet_connection_sock *icsk = inet_csk(sk);
407 : 1151 : u32 retransmits;
408 : :
409 : 1151 : retransmits = min_t(u32, icsk->icsk_retransmits,
410 : : ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
411 : :
412 [ - + ]: 1151 : mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
413 : 1151 : }
414 : :
415 : 697427 : static void __mptcp_set_timeout(struct sock *sk, long tout)
416 : : {
417 [ + + - + ]: 1186034 : mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
418 : 697427 : }
419 : :
420 : 289141 : static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
421 : : {
422 : 1157163 : const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
423 : :
424 [ + + + + ]: 956501 : return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
425 [ + + + + ]: 1967093 : inet_csk(ssk)->icsk_timeout - jiffies : 0;
426 : : }
427 : :
428 : 300789 : void mptcp_set_timeout(struct sock *sk)
429 : : {
430 : 300789 : struct mptcp_subflow_context *subflow;
431 : 300789 : long tout = 0;
432 : :
433 [ - + - + : 819688 : mptcp_for_each_subflow(mptcp_sk(sk), subflow)
+ + ]
434 [ + + ]: 958344 : tout = max(tout, mptcp_timeout_from_subflow(subflow));
435 : 300789 : __mptcp_set_timeout(sk, tout);
436 : 300789 : }
437 : :
438 : 139005 : static inline bool tcp_can_send_ack(const struct sock *ssk)
439 : : {
440 [ - + ]: 302635 : return !((1 << inet_sk_state_load(ssk)) &
441 : : (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
442 : : }
443 : :
444 : 4653 : void __mptcp_subflow_send_ack(struct sock *ssk)
445 : : {
446 [ + + ]: 4653 : if (tcp_can_send_ack(ssk))
447 : 4577 : tcp_send_ack(ssk);
448 : 4653 : }
449 : :
450 : 3764 : static void mptcp_subflow_send_ack(struct sock *ssk)
451 : : {
452 : 3764 : bool slow;
453 : :
454 : 3764 : slow = lock_sock_fast(ssk);
455 : 3764 : __mptcp_subflow_send_ack(ssk);
456 : 3764 : unlock_sock_fast(ssk, slow);
457 : 3764 : }
458 : :
459 : 2815 : static void mptcp_send_ack(struct mptcp_sock *msk)
460 : : {
461 : 2815 : struct mptcp_subflow_context *subflow;
462 : :
463 [ + + ]: 6579 : mptcp_for_each_subflow(msk, subflow)
464 : 3764 : mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
465 : 2815 : }
466 : :
467 : 205589 : static void mptcp_subflow_cleanup_rbuf(struct sock *ssk, int copied)
468 : : {
469 : 205589 : bool slow;
470 : :
471 : 205589 : slow = lock_sock_fast(ssk);
472 [ + + ]: 205589 : if (tcp_can_send_ack(ssk))
473 : 198573 : tcp_cleanup_rbuf(ssk, copied);
474 : 205589 : unlock_sock_fast(ssk, slow);
475 : 205589 : }
476 : :
477 : 1180837 : static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
478 : : {
479 : 1180837 : const struct inet_connection_sock *icsk = inet_csk(ssk);
480 : 1180837 : u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
481 [ - + ]: 1180837 : const struct tcp_sock *tp = tcp_sk(ssk);
482 : :
483 [ + + ]: 1180837 : return (ack_pending & ICSK_ACK_SCHED) &&
484 : 533239 : ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
485 [ + + + + ]: 533239 : READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
486 [ + + ]: 54519 : (rx_empty && ack_pending &
487 : : (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
488 : : }
489 : :
490 : 729480 : static void mptcp_cleanup_rbuf(struct mptcp_sock *msk, int copied)
491 : : {
492 : 729480 : int old_space = READ_ONCE(msk->old_wspace);
493 : 729480 : struct mptcp_subflow_context *subflow;
494 : 729480 : struct sock *sk = (struct sock *)msk;
495 : 729480 : int space = __mptcp_space(sk);
496 : 729480 : bool cleanup, rx_empty;
497 : :
498 [ + + + + : 729480 : cleanup = (space > 0) && (space >= (old_space << 1)) && copied;
+ + ]
499 [ + + + + ]: 729480 : rx_empty = !sk_rmem_alloc_get(sk) && copied;
500 : :
501 [ + + ]: 1976205 : mptcp_for_each_subflow(msk, subflow) {
502 [ + + ]: 1246725 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
503 : :
504 [ + + + + ]: 1246725 : if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
505 : 205589 : mptcp_subflow_cleanup_rbuf(ssk, copied);
506 : : }
507 : 729480 : }
508 : :
509 : 64968 : static bool mptcp_check_data_fin(struct sock *sk)
510 : : {
511 [ - + ]: 64968 : struct mptcp_sock *msk = mptcp_sk(sk);
512 : 64968 : u64 rcv_data_fin_seq;
513 : 64968 : bool ret = false;
514 : :
515 : : /* Need to ack a DATA_FIN received from a peer while this side
516 : : * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
517 : : * msk->rcv_data_fin was set when parsing the incoming options
518 : : * at the subflow level and the msk lock was not held, so this
519 : : * is the first opportunity to act on the DATA_FIN and change
520 : : * the msk state.
521 : : *
522 : : * If we are caught up to the sequence number of the incoming
523 : : * DATA_FIN, send the DATA_ACK now and do state transition. If
524 : : * not caught up, do nothing and let the recv code send DATA_ACK
525 : : * when catching up.
526 : : */
527 : :
528 [ + + ]: 64968 : if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
529 : 1733 : WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
530 : 1733 : WRITE_ONCE(msk->rcv_data_fin, 0);
531 : :
532 : 1733 : WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
533 : 1733 : smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
534 : :
535 [ + + + - ]: 1733 : switch (sk->sk_state) {
536 : : case TCP_ESTABLISHED:
537 : 720 : mptcp_set_state(sk, TCP_CLOSE_WAIT);
538 : 720 : break;
539 : 259 : case TCP_FIN_WAIT1:
540 : 259 : mptcp_set_state(sk, TCP_CLOSING);
541 : 259 : break;
542 : 754 : case TCP_FIN_WAIT2:
543 : 754 : mptcp_set_state(sk, TCP_CLOSE);
544 : 754 : break;
545 : : default:
546 : : /* Other states not expected */
547 : 0 : WARN_ON_ONCE(1);
548 : 0 : break;
549 : : }
550 : :
551 : 1733 : ret = true;
552 [ + + ]: 1733 : if (!__mptcp_check_fallback(msk))
553 : 1664 : mptcp_send_ack(msk);
554 : 1733 : mptcp_close_wake_up(sk);
555 : : }
556 : 64968 : return ret;
557 : : }
558 : :
559 : 0 : static void mptcp_dss_corruption(struct mptcp_sock *msk, struct sock *ssk)
560 : : {
561 [ # # # # ]: 0 : if (READ_ONCE(msk->allow_infinite_fallback)) {
562 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk),
563 : : MPTCP_MIB_DSSCORRUPTIONFALLBACK);
564 : 0 : mptcp_do_fallback(ssk);
565 : : } else {
566 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSCORRUPTIONRESET);
567 : 0 : mptcp_subflow_reset(ssk);
568 : : }
569 : 0 : }
570 : :
571 : 448475 : static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
572 : : struct sock *ssk)
573 : : {
574 [ - + ]: 448475 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
575 : 448475 : struct sock *sk = (struct sock *)msk;
576 : 448475 : bool more_data_avail;
577 : 448475 : struct tcp_sock *tp;
578 : 448475 : bool ret = false;
579 : :
580 [ - + ]: 448475 : pr_debug("msk=%p ssk=%p\n", msk, ssk);
581 [ - + ]: 448475 : tp = tcp_sk(ssk);
582 : 475567 : do {
583 : 475567 : u32 map_remaining, offset;
584 : 475567 : u32 seq = tp->copied_seq;
585 : 475567 : struct sk_buff *skb;
586 : 475567 : bool fin;
587 : :
588 [ + + ]: 475567 : if (sk_rmem_alloc_get(sk) > sk->sk_rcvbuf)
589 : : break;
590 : :
591 : : /* try to move as much data as available */
592 : 946206 : map_remaining = subflow->map_data_len -
593 : 473103 : mptcp_subflow_get_map_offset(subflow);
594 : :
595 [ + - ]: 473103 : skb = skb_peek(&ssk->sk_receive_queue);
596 [ + - ]: 473103 : if (unlikely(!skb))
597 : : break;
598 : :
599 [ + + ]: 473103 : if (__mptcp_check_fallback(msk)) {
600 : : /* Under fallback skbs have no MPTCP extension and TCP could
601 : : * collapse them between the dummy map creation and the
602 : : * current dequeue. Be sure to adjust the map size.
603 : : */
604 : 4461 : map_remaining = skb->len;
605 : 4461 : subflow->map_data_len = skb->len;
606 : : }
607 : :
608 : 473103 : offset = seq - TCP_SKB_CB(skb)->seq;
609 : 473103 : fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
610 [ + + ]: 473103 : if (fin)
611 : 70 : seq++;
612 : :
613 [ + + ]: 473103 : if (offset < skb->len) {
614 : 473072 : size_t len = skb->len - offset;
615 : :
616 [ + + + + ]: 473072 : ret = __mptcp_move_skb(msk, ssk, skb, offset, len) || ret;
617 : 473072 : seq += len;
618 : :
619 [ - + ]: 473072 : if (unlikely(map_remaining < len)) {
620 : 0 : DEBUG_NET_WARN_ON_ONCE(1);
621 : 0 : mptcp_dss_corruption(msk, ssk);
622 : : }
623 : : } else {
624 [ - + ]: 31 : if (unlikely(!fin)) {
625 : 0 : DEBUG_NET_WARN_ON_ONCE(1);
626 : 0 : mptcp_dss_corruption(msk, ssk);
627 : : }
628 : :
629 : 31 : sk_eat_skb(ssk, skb);
630 : : }
631 : :
632 : 473103 : WRITE_ONCE(tp->copied_seq, seq);
633 : 473103 : more_data_avail = mptcp_subflow_data_available(ssk);
634 : :
635 [ + + ]: 473103 : } while (more_data_avail);
636 : :
637 [ + + ]: 448475 : if (ret)
638 : 359875 : msk->last_data_recv = tcp_jiffies32;
639 : 448475 : return ret;
640 : : }
641 : :
642 : 632085 : static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
643 : : {
644 : 632085 : struct sock *sk = (struct sock *)msk;
645 : 632085 : struct sk_buff *skb, *tail;
646 : 632085 : bool moved = false;
647 : 632085 : struct rb_node *p;
648 : 632085 : u64 end_seq;
649 : :
650 : 632085 : p = rb_first(&msk->out_of_order_queue);
651 [ - + ]: 632085 : pr_debug("msk=%p empty=%d\n", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
652 [ + + ]: 675094 : while (p) {
653 : 224977 : skb = rb_to_skb(p);
654 [ + + ]: 224977 : if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
655 : : break;
656 : :
657 : 43009 : p = rb_next(p);
658 : 43009 : rb_erase(&skb->rbnode, &msk->out_of_order_queue);
659 : :
660 [ + + ]: 43009 : if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
661 : : msk->ack_seq))) {
662 : 56 : mptcp_drop(sk, skb);
663 [ + - ]: 56 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
664 : 56 : continue;
665 : : }
666 : :
667 : 42953 : end_seq = MPTCP_SKB_CB(skb)->end_seq;
668 [ + - ]: 42953 : tail = skb_peek_tail(&sk->sk_receive_queue);
669 [ + - + + ]: 85440 : if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
670 : 17937 : int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
671 : :
672 : : /* skip overlapping data, if any */
673 [ - + ]: 17937 : pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d\n",
674 : : MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
675 : : delta);
676 : 17937 : MPTCP_SKB_CB(skb)->offset += delta;
677 : 17937 : MPTCP_SKB_CB(skb)->map_seq += delta;
678 : 17937 : __skb_queue_tail(&sk->sk_receive_queue, skb);
679 : : }
680 : 42953 : msk->bytes_received += end_seq - msk->ack_seq;
681 : 42953 : WRITE_ONCE(msk->ack_seq, end_seq);
682 : 42953 : moved = true;
683 : : }
684 : 632085 : return moved;
685 : : }
686 : :
687 : 5546 : static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
688 : : {
689 [ + + ]: 5546 : int err = sock_error(ssk);
690 : 3320 : int ssk_state;
691 : :
692 [ + + ]: 3198 : if (!err)
693 : 2348 : return false;
694 : :
695 : : /* only propagate errors on fallen-back sockets or
696 : : * on MPC connect
697 : : */
698 [ + + - + : 910 : if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(mptcp_sk(sk)))
+ + ]
699 : : return false;
700 : :
701 : : /* We need to propagate only transition to CLOSE state.
702 : : * Orphaned socket will see such state change via
703 : : * subflow_sched_work_if_closed() and that path will properly
704 : : * destroy the msk as needed.
705 : : */
706 : 45 : ssk_state = inet_sk_state_load(ssk);
707 [ + - + + ]: 45 : if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
708 : 27 : mptcp_set_state(sk, ssk_state);
709 : 45 : WRITE_ONCE(sk->sk_err, -err);
710 : :
711 : : /* This barrier is coupled with smp_rmb() in mptcp_poll() */
712 : 45 : smp_wmb();
713 : 45 : sk_error_report(sk);
714 : 45 : return true;
715 : : }
716 : :
717 : 828 : void __mptcp_error_report(struct sock *sk)
718 : : {
719 : 828 : struct mptcp_subflow_context *subflow;
720 [ - + ]: 828 : struct mptcp_sock *msk = mptcp_sk(sk);
721 : :
722 [ + + ]: 1716 : mptcp_for_each_subflow(msk, subflow)
723 [ + + ]: 915 : if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
724 : : break;
725 : 828 : }
726 : :
727 : : /* In most cases we will be able to lock the mptcp socket. If its already
728 : : * owned, we need to defer to the work queue to avoid ABBA deadlock.
729 : : */
730 : 384121 : static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
731 : : {
732 : 384121 : struct sock *sk = (struct sock *)msk;
733 : 384121 : bool moved;
734 : :
735 : 384121 : moved = __mptcp_move_skbs_from_subflow(msk, ssk);
736 : 384121 : __mptcp_ofo_queue(msk);
737 [ - + ]: 384121 : if (unlikely(ssk->sk_err)) {
738 [ # # ]: 0 : if (!sock_owned_by_user(sk))
739 : 0 : __mptcp_error_report(sk);
740 : : else
741 [ # # # # : 0 : __set_bit(MPTCP_ERROR_REPORT, &msk->cb_flags);
# # ]
742 : : }
743 : :
744 : : /* If the moves have caught up with the DATA_FIN sequence number
745 : : * it's time to ack the DATA_FIN and change socket state, but
746 : : * this is not a good place to change state. Let the workqueue
747 : : * do it.
748 : : */
749 [ + + ]: 384121 : if (mptcp_pending_data_fin(sk, NULL))
750 : 281 : mptcp_schedule_work(sk);
751 : 384121 : return moved;
752 : : }
753 : :
754 : 179202 : static void __mptcp_rcvbuf_update(struct sock *sk, struct sock *ssk)
755 : : {
756 : 847162 : if (unlikely(ssk->sk_rcvbuf > sk->sk_rcvbuf))
757 : 0 : WRITE_ONCE(sk->sk_rcvbuf, ssk->sk_rcvbuf);
758 : : }
759 : :
760 : 384121 : static void __mptcp_data_ready(struct sock *sk, struct sock *ssk)
761 : : {
762 [ - + ]: 384121 : struct mptcp_sock *msk = mptcp_sk(sk);
763 : :
764 [ - + ]: 384121 : __mptcp_rcvbuf_update(sk, ssk);
765 : :
766 : : /* Wake-up the reader only for in-sequence data */
767 [ + + + - ]: 384121 : if (move_skbs_to_msk(msk, ssk) && mptcp_epollin_ready(sk))
768 : 302039 : sk->sk_data_ready(sk);
769 : 384121 : }
770 : :
771 : 492887 : void mptcp_data_ready(struct sock *sk, struct sock *ssk)
772 : : {
773 [ + - ]: 492887 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
774 : :
775 : : /* The peer can send data while we are shutting down this
776 : : * subflow at msk destruction time, but we must avoid enqueuing
777 : : * more data to the msk receive queue
778 : : */
779 [ + - ]: 492887 : if (unlikely(subflow->disposable))
780 : : return;
781 : :
782 : 492887 : mptcp_data_lock(sk);
783 [ + + ]: 492887 : if (!sock_owned_by_user(sk))
784 : 384121 : __mptcp_data_ready(sk, ssk);
785 : : else
786 [ - + ]: 108766 : __set_bit(MPTCP_DEQUEUE, &mptcp_sk(sk)->cb_flags);
787 : 492887 : mptcp_data_unlock(sk);
788 : : }
789 : :
790 : 994 : static void mptcp_subflow_joined(struct mptcp_sock *msk, struct sock *ssk)
791 : : {
792 : 994 : mptcp_subflow_ctx(ssk)->map_seq = READ_ONCE(msk->ack_seq);
793 : 994 : WRITE_ONCE(msk->allow_infinite_fallback, false);
794 : 994 : mptcp_event(MPTCP_EVENT_SUB_ESTABLISHED, msk, ssk, GFP_ATOMIC);
795 : 994 : }
796 : :
797 : 506 : static bool __mptcp_finish_join(struct mptcp_sock *msk, struct sock *ssk)
798 : : {
799 : 506 : struct sock *sk = (struct sock *)msk;
800 : :
801 [ + - ]: 506 : if (sk->sk_state != TCP_ESTABLISHED)
802 : : return false;
803 : :
804 : : /* attach to msk socket only after we are sure we will deal with it
805 : : * at close time
806 : : */
807 [ + + + - ]: 506 : if (sk->sk_socket && !ssk->sk_socket)
808 : 474 : mptcp_sock_graft(ssk, sk->sk_socket);
809 : :
810 : 506 : mptcp_subflow_ctx(ssk)->subflow_id = msk->subflow_id++;
811 : 506 : mptcp_sockopt_sync_locked(msk, ssk);
812 : 506 : mptcp_subflow_joined(msk, ssk);
813 : 506 : mptcp_stop_tout_timer(sk);
814 [ + - ]: 506 : __mptcp_propagate_sndbuf(sk, ssk);
815 : : return true;
816 : : }
817 : :
818 : 38 : static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
819 : : {
820 : 38 : struct mptcp_subflow_context *tmp, *subflow;
821 [ - + ]: 38 : struct mptcp_sock *msk = mptcp_sk(sk);
822 : :
823 [ + + ]: 78 : list_for_each_entry_safe(subflow, tmp, join_list, node) {
824 : 40 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
825 : 40 : bool slow = lock_sock_fast(ssk);
826 : :
827 : 40 : list_move_tail(&subflow->node, &msk->conn_list);
828 [ - + ]: 40 : if (!__mptcp_finish_join(msk, ssk))
829 : 0 : mptcp_subflow_reset(ssk);
830 : 40 : unlock_sock_fast(ssk, slow);
831 : : }
832 : 38 : }
833 : :
834 : 269189 : static bool mptcp_rtx_timer_pending(struct sock *sk)
835 : : {
836 : 610372 : return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
837 : : }
838 : :
839 : 225111 : static void mptcp_reset_rtx_timer(struct sock *sk)
840 : : {
841 : 225111 : struct inet_connection_sock *icsk = inet_csk(sk);
842 : 225111 : unsigned long tout;
843 : :
844 : : /* prevent rescheduling on close */
845 [ + + ]: 225111 : if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
846 : : return;
847 : :
848 [ - + ]: 225030 : tout = mptcp_sk(sk)->timer_ival;
849 : 225030 : sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
850 : : }
851 : :
852 : 15013 : bool mptcp_schedule_work(struct sock *sk)
853 : : {
854 [ + + + + ]: 29080 : if (inet_sk_state_load(sk) != TCP_CLOSE &&
855 [ - + + + ]: 14706 : schedule_work(&mptcp_sk(sk)->work)) {
856 : : /* each subflow already holds a reference to the sk, and the
857 : : * workqueue is invoked by a subflow, so sk can't go away here.
858 : : */
859 : 13487 : sock_hold(sk);
860 : 13487 : return true;
861 : : }
862 : : return false;
863 : : }
864 : :
865 : 529829 : static bool mptcp_skb_can_collapse_to(u64 write_seq,
866 : : const struct sk_buff *skb,
867 : : const struct mptcp_ext *mpext)
868 : : {
869 [ + + ]: 529829 : if (!tcp_skb_can_collapse_to(skb))
870 : : return false;
871 : :
872 : : /* can collapse only if MPTCP level sequence is in order and this
873 : : * mapping has not been xmitted yet
874 : : */
875 [ + - + + ]: 1036890 : return mpext && mpext->data_seq + mpext->data_len == write_seq &&
876 [ + + ]: 459368 : !mpext->frozen;
877 : : }
878 : :
879 : : /* we can append data to the given data frag if:
880 : : * - there is space available in the backing page_frag
881 : : * - the data frag tail matches the current page_frag free offset
882 : : * - the data frag end sequence number matches the current write seq
883 : : */
884 : 600598 : static bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
885 : : const struct page_frag *pfrag,
886 : : const struct mptcp_data_frag *df)
887 : : {
888 [ + - ]: 239380 : return df && pfrag->page == df->page &&
889 [ + + ]: 239380 : pfrag->size - pfrag->offset > 0 &&
890 [ + + + - ]: 738797 : pfrag->offset == (df->offset + df->data_len) &&
891 [ - + ]: 138199 : df->data_seq + df->data_len == msk->write_seq;
892 : : }
893 : :
894 : 213295 : static void dfrag_uncharge(struct sock *sk, int len)
895 : : {
896 : 586647 : sk_mem_uncharge(sk, len);
897 : 586647 : sk_wmem_queued_add(sk, -len);
898 : 124254 : }
899 : :
900 : 462393 : static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
901 : : {
902 : 462393 : int len = dfrag->data_len + dfrag->overhead;
903 : :
904 : 462393 : list_del(&dfrag->list);
905 : 462393 : dfrag_uncharge(sk, len);
906 : 462393 : put_page(dfrag->page);
907 : 462393 : }
908 : :
909 : : /* called under both the msk socket lock and the data lock */
910 : 226492 : static void __mptcp_clean_una(struct sock *sk)
911 : : {
912 [ - + ]: 226492 : struct mptcp_sock *msk = mptcp_sk(sk);
913 : 226492 : struct mptcp_data_frag *dtmp, *dfrag;
914 : 226492 : u64 snd_una;
915 : :
916 : 226492 : snd_una = msk->snd_una;
917 [ + + ]: 688655 : list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
918 [ + + ]: 649043 : if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
919 : : break;
920 : :
921 [ - + ]: 462163 : if (unlikely(dfrag == msk->first_pending)) {
922 : : /* in recovery mode can see ack after the current snd head */
923 [ # # # # ]: 0 : if (WARN_ON_ONCE(!msk->recovery))
924 : : break;
925 : :
926 : 0 : WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
927 : : }
928 : :
929 : 462163 : dfrag_clear(sk, dfrag);
930 : : }
931 : :
932 : 226492 : dfrag = mptcp_rtx_head(sk);
933 [ + + + + ]: 226492 : if (dfrag && after64(snd_una, dfrag->data_seq)) {
934 : 124254 : u64 delta = snd_una - dfrag->data_seq;
935 : :
936 : : /* prevent wrap around in recovery mode */
937 [ - + ]: 124254 : if (unlikely(delta > dfrag->already_sent)) {
938 [ # # # # ]: 0 : if (WARN_ON_ONCE(!msk->recovery))
939 : 0 : goto out;
940 [ # # # # ]: 0 : if (WARN_ON_ONCE(delta > dfrag->data_len))
941 : 0 : goto out;
942 : 0 : dfrag->already_sent += delta - dfrag->already_sent;
943 : : }
944 : :
945 : 124254 : dfrag->data_seq += delta;
946 : 124254 : dfrag->offset += delta;
947 : 124254 : dfrag->data_len -= delta;
948 : 124254 : dfrag->already_sent -= delta;
949 : :
950 : 124254 : dfrag_uncharge(sk, delta);
951 : : }
952 : :
953 : : /* all retransmitted data acked, recovery completed */
954 [ + + + + : 226492 : if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
+ + ]
955 : 33 : msk->recovery = false;
956 : :
957 : 192478 : out:
958 [ + + + + ]: 226492 : if (snd_una == msk->snd_nxt && snd_una == msk->write_seq) {
959 [ + + + + ]: 38235 : if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
960 : 30935 : mptcp_stop_rtx_timer(sk);
961 : : } else {
962 : 188257 : mptcp_reset_rtx_timer(sk);
963 : : }
964 : :
965 [ + + ]: 226492 : if (mptcp_pending_data_fin_ack(sk))
966 : 1731 : mptcp_schedule_work(sk);
967 : 226492 : }
968 : :
969 : 53970 : static void __mptcp_clean_una_wakeup(struct sock *sk)
970 : : {
971 [ + - - + ]: 68212 : lockdep_assert_held_once(&sk->sk_lock.slock);
972 : :
973 : 68212 : __mptcp_clean_una(sk);
974 : 53411 : mptcp_write_space(sk);
975 : 64007 : }
976 : :
977 : 11297 : static void mptcp_clean_una_wakeup(struct sock *sk)
978 : : {
979 : 11297 : mptcp_data_lock(sk);
980 : 11297 : __mptcp_clean_una_wakeup(sk);
981 : 11297 : mptcp_data_unlock(sk);
982 : 11297 : }
983 : :
984 : 0 : static void mptcp_enter_memory_pressure(struct sock *sk)
985 : : {
986 : 0 : struct mptcp_subflow_context *subflow;
987 [ # # ]: 0 : struct mptcp_sock *msk = mptcp_sk(sk);
988 : 0 : bool first = true;
989 : :
990 [ # # ]: 0 : mptcp_for_each_subflow(msk, subflow) {
991 [ # # ]: 0 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
992 : :
993 [ # # ]: 0 : if (first)
994 : 0 : tcp_enter_memory_pressure(ssk);
995 : 0 : sk_stream_moderate_sndbuf(ssk);
996 : :
997 : 0 : first = false;
998 : : }
999 : 0 : __mptcp_sync_sndbuf(sk);
1000 : 0 : }
1001 : :
1002 : : /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
1003 : : * data
1004 : : */
1005 : 462399 : static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1006 : : {
1007 [ - + ]: 462399 : if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
1008 : : pfrag, sk->sk_allocation)))
1009 : : return true;
1010 : :
1011 : 0 : mptcp_enter_memory_pressure(sk);
1012 : 0 : return false;
1013 : : }
1014 : :
1015 : : static struct mptcp_data_frag *
1016 : 462399 : mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1017 : : int orig_offset)
1018 : : {
1019 : 462399 : int offset = ALIGN(orig_offset, sizeof(long));
1020 : 462399 : struct mptcp_data_frag *dfrag;
1021 : :
1022 : 462399 : dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1023 : 462399 : dfrag->data_len = 0;
1024 : 462399 : dfrag->data_seq = msk->write_seq;
1025 : 462399 : dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1026 : 462399 : dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1027 : 462399 : dfrag->already_sent = 0;
1028 : 462399 : dfrag->page = pfrag->page;
1029 : :
1030 : 462399 : return dfrag;
1031 : : }
1032 : :
1033 : : struct mptcp_sendmsg_info {
1034 : : int mss_now;
1035 : : int size_goal;
1036 : : u16 limit;
1037 : : u16 sent;
1038 : : unsigned int flags;
1039 : : bool data_lock_held;
1040 : : };
1041 : :
1042 : 690826 : static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1043 : : u64 data_seq, int avail_size)
1044 : : {
1045 : 690826 : u64 window_end = mptcp_wnd_end(msk);
1046 : 690826 : u64 mptcp_snd_wnd;
1047 : :
1048 [ + + ]: 690826 : if (__mptcp_check_fallback(msk))
1049 : : return avail_size;
1050 : :
1051 : 653207 : mptcp_snd_wnd = window_end - data_seq;
1052 : 653207 : avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1053 : :
1054 [ - + + + ]: 653207 : if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1055 [ - + ]: 3407 : tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1056 [ + - ]: 3407 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1057 : : }
1058 : :
1059 : : return avail_size;
1060 : : }
1061 : :
1062 : 259128 : static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1063 : : {
1064 : 259128 : struct skb_ext *mpext = __skb_ext_alloc(gfp);
1065 : :
1066 [ + - ]: 259128 : if (!mpext)
1067 : : return false;
1068 : 259128 : __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1069 : 259128 : return true;
1070 : : }
1071 : :
1072 : 259128 : static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1073 : : {
1074 : 259128 : struct sk_buff *skb;
1075 : :
1076 : 259128 : skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1077 [ + - ]: 259128 : if (likely(skb)) {
1078 [ + - ]: 259128 : if (likely(__mptcp_add_ext(skb, gfp))) {
1079 : 259128 : skb_reserve(skb, MAX_TCP_HEADER);
1080 : 259128 : skb->ip_summed = CHECKSUM_PARTIAL;
1081 : 259128 : INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1082 : 259128 : return skb;
1083 : : }
1084 : 0 : __kfree_skb(skb);
1085 : : } else {
1086 : 0 : mptcp_enter_memory_pressure(sk);
1087 : : }
1088 : : return NULL;
1089 : : }
1090 : :
1091 : 259128 : static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1092 : : {
1093 : 259128 : struct sk_buff *skb;
1094 : :
1095 : 259128 : skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1096 [ - + ]: 259128 : if (!skb)
1097 : : return NULL;
1098 : :
1099 [ + - ]: 259128 : if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1100 : 259128 : tcp_skb_entail(ssk, skb);
1101 : 259128 : return skb;
1102 : : }
1103 : 0 : tcp_skb_tsorted_anchor_cleanup(skb);
1104 : 0 : kfree_skb(skb);
1105 : 0 : return NULL;
1106 : : }
1107 : :
1108 : 75521 : static struct sk_buff *mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, bool data_lock_held)
1109 : : {
1110 : 225867 : gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1111 : :
1112 : 259128 : return __mptcp_alloc_tx_skb(sk, ssk, gfp);
1113 : : }
1114 : :
1115 : : /* note: this always recompute the csum on the whole skb, even
1116 : : * if we just appended a single frag. More status info needed
1117 : : */
1118 : 869 : static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1119 : : {
1120 [ + - ]: 869 : struct mptcp_ext *mpext = mptcp_get_ext(skb);
1121 : 869 : __wsum csum = ~csum_unfold(mpext->csum);
1122 : 869 : int offset = skb->len - added;
1123 : :
1124 [ # # ]: 869 : mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1125 : 869 : }
1126 : :
1127 : 2 : static void mptcp_update_infinite_map(struct mptcp_sock *msk,
1128 : : struct sock *ssk,
1129 : : struct mptcp_ext *mpext)
1130 : : {
1131 [ + - ]: 2 : if (!mpext)
1132 : : return;
1133 : :
1134 : 2 : mpext->infinite_map = 1;
1135 : 2 : mpext->data_len = 0;
1136 : :
1137 [ + - ]: 2 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPTX);
1138 [ - + ]: 2 : mptcp_subflow_ctx(ssk)->send_infinite_map = 0;
1139 [ - + ]: 2 : pr_fallback(msk);
1140 : 2 : mptcp_do_fallback(ssk);
1141 : : }
1142 : :
1143 : : #define MPTCP_MAX_GSO_SIZE (GSO_LEGACY_MAX_SIZE - (MAX_TCP_HEADER + 1))
1144 : :
1145 : 690826 : static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
1146 : : struct mptcp_data_frag *dfrag,
1147 : : struct mptcp_sendmsg_info *info)
1148 : : {
1149 : 690826 : u64 data_seq = dfrag->data_seq + info->sent;
1150 : 690826 : int offset = dfrag->offset + info->sent;
1151 [ - + ]: 690826 : struct mptcp_sock *msk = mptcp_sk(sk);
1152 : 690826 : bool zero_window_probe = false;
1153 : 690826 : struct mptcp_ext *mpext = NULL;
1154 : 690826 : bool can_coalesce = false;
1155 : 690826 : bool reuse_skb = true;
1156 : 690826 : struct sk_buff *skb;
1157 : 690826 : size_t copy;
1158 : 690826 : int i;
1159 : :
1160 [ - + ]: 690826 : pr_debug("msk=%p ssk=%p sending dfrag at seq=%llu len=%u already sent=%u\n",
1161 : : msk, ssk, dfrag->data_seq, dfrag->data_len, info->sent);
1162 : :
1163 [ + - - + ]: 690826 : if (WARN_ON_ONCE(info->sent > info->limit ||
1164 : : info->limit > dfrag->data_len))
1165 : 0 : return 0;
1166 : :
1167 [ + - ]: 690826 : if (unlikely(!__tcp_can_send(ssk)))
1168 : : return -EAGAIN;
1169 : :
1170 : : /* compute send limit */
1171 [ - + ]: 690826 : if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE))
1172 : 0 : ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE;
1173 : 690826 : info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1174 : 690826 : copy = info->size_goal;
1175 : :
1176 [ + + ]: 690826 : skb = tcp_write_queue_tail(ssk);
1177 [ + - + + ]: 563549 : if (skb && copy > skb->len) {
1178 : : /* Limit the write to the size available in the
1179 : : * current skb, if any, so that we create at most a new skb.
1180 : : * Explicitly tells TCP internals to avoid collapsing on later
1181 : : * queue management operation, to avoid breaking the ext <->
1182 : : * SSN association set here
1183 : : */
1184 [ + - ]: 529829 : mpext = mptcp_get_ext(skb);
1185 [ + + ]: 529829 : if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1186 : 92769 : TCP_SKB_CB(skb)->eor = 1;
1187 [ - + ]: 92769 : tcp_mark_push(tcp_sk(ssk), skb);
1188 : 92769 : goto alloc_skb;
1189 : : }
1190 : :
1191 : 437060 : i = skb_shinfo(skb)->nr_frags;
1192 : 437060 : can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1193 [ + + + + ]: 437060 : if (!can_coalesce && i >= READ_ONCE(net_hotdata.sysctl_max_skb_frags)) {
1194 [ - + ]: 5362 : tcp_mark_push(tcp_sk(ssk), skb);
1195 : 5362 : goto alloc_skb;
1196 : : }
1197 : :
1198 : 431698 : copy -= skb->len;
1199 : : } else {
1200 [ + + ]: 75521 : alloc_skb:
1201 [ + + + + ]: 259128 : skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
1202 [ - + ]: 259128 : if (!skb)
1203 : : return -ENOMEM;
1204 : :
1205 [ + - ]: 259128 : i = skb_shinfo(skb)->nr_frags;
1206 : 259128 : reuse_skb = false;
1207 [ + - ]: 259128 : mpext = mptcp_get_ext(skb);
1208 : : }
1209 : :
1210 : : /* Zero window and all data acked? Probe. */
1211 : 690826 : copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1212 [ + + ]: 690826 : if (copy == 0) {
1213 : 154206 : u64 snd_una = READ_ONCE(msk->snd_una);
1214 : :
1215 [ + + + - ]: 165323 : if (snd_una != msk->snd_nxt || tcp_write_queue_tail(ssk)) {
1216 : 154206 : tcp_remove_empty_skb(ssk);
1217 : 154206 : return 0;
1218 : : }
1219 : :
1220 : 0 : zero_window_probe = true;
1221 : 0 : data_seq = snd_una - 1;
1222 : 0 : copy = 1;
1223 : : }
1224 : :
1225 : 536620 : copy = min_t(size_t, copy, info->limit - info->sent);
1226 [ - + ]: 536620 : if (!sk_wmem_schedule(ssk, copy)) {
1227 : 0 : tcp_remove_empty_skb(ssk);
1228 : 0 : return -ENOMEM;
1229 : : }
1230 : :
1231 [ + + ]: 536620 : if (can_coalesce) {
1232 : 19143 : skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1233 : : } else {
1234 : 517477 : get_page(dfrag->page);
1235 : 517477 : skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1236 : : }
1237 : :
1238 : 536620 : skb->len += copy;
1239 : 536620 : skb->data_len += copy;
1240 : 536620 : skb->truesize += copy;
1241 [ + - ]: 536620 : sk_wmem_queued_add(ssk, copy);
1242 [ + - ]: 536620 : sk_mem_charge(ssk, copy);
1243 [ - + - + ]: 536620 : WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1244 : 536620 : TCP_SKB_CB(skb)->end_seq += copy;
1245 [ + + ]: 536620 : tcp_skb_pcount_set(skb, 0);
1246 : :
1247 : : /* on skb reuse we just need to update the DSS len */
1248 [ + + ]: 536620 : if (reuse_skb) {
1249 : 323251 : TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1250 : 323251 : mpext->data_len += copy;
1251 : 323251 : goto out;
1252 : : }
1253 : :
1254 : 213369 : memset(mpext, 0, sizeof(*mpext));
1255 : 213369 : mpext->data_seq = data_seq;
1256 [ - + ]: 213369 : mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1257 : 213369 : mpext->data_len = copy;
1258 : 213369 : mpext->use_map = 1;
1259 : 213369 : mpext->dsn64 = 1;
1260 : :
1261 [ - + ]: 213369 : pr_debug("data_seq=%llu subflow_seq=%u data_len=%u dsn64=%d\n",
1262 : : mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1263 : : mpext->dsn64);
1264 : :
1265 [ - + ]: 213369 : if (zero_window_probe) {
1266 [ # # ]: 0 : mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1267 : 0 : mpext->frozen = 1;
1268 [ # # # # ]: 0 : if (READ_ONCE(msk->csum_enabled))
1269 : 0 : mptcp_update_data_checksum(skb, copy);
1270 : 0 : tcp_push_pending_frames(ssk);
1271 : 0 : return 0;
1272 : : }
1273 : 213369 : out:
1274 [ + + + + ]: 536620 : if (READ_ONCE(msk->csum_enabled))
1275 : 869 : mptcp_update_data_checksum(skb, copy);
1276 [ + + ]: 536620 : if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1277 : 2 : mptcp_update_infinite_map(msk, ssk, mpext);
1278 : 536620 : trace_mptcp_sendmsg_frag(mpext);
1279 : 536620 : mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1280 : 536620 : return copy;
1281 : : }
1282 : :
1283 : : #define MPTCP_SEND_BURST_SIZE ((1 << 16) - \
1284 : : sizeof(struct tcphdr) - \
1285 : : MAX_TCP_OPTION_SPACE - \
1286 : : sizeof(struct ipv6hdr) - \
1287 : : sizeof(struct frag_hdr))
1288 : :
1289 : : struct subflow_send_info {
1290 : : struct sock *ssk;
1291 : : u64 linger_time;
1292 : : };
1293 : :
1294 : 100 : void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1295 : : {
1296 [ - + ]: 100 : if (!subflow->stale)
1297 : : return;
1298 : :
1299 : 0 : subflow->stale = 0;
1300 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(mptcp_subflow_tcp_sock(subflow)), MPTCP_MIB_SUBFLOWRECOVER);
1301 : : }
1302 : :
1303 : 1054657 : bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1304 : : {
1305 [ + + ]: 1054657 : if (unlikely(subflow->stale)) {
1306 [ - + ]: 71864 : u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1307 : :
1308 [ - + ]: 71864 : if (subflow->stale_rcv_tstamp == rcv_tstamp)
1309 : : return false;
1310 : :
1311 : 0 : mptcp_subflow_set_active(subflow);
1312 : : }
1313 : 982793 : return __mptcp_subflow_active(subflow);
1314 : : }
1315 : :
1316 : : #define SSK_MODE_ACTIVE 0
1317 : : #define SSK_MODE_BACKUP 1
1318 : : #define SSK_MODE_MAX 2
1319 : :
1320 : : /* implement the mptcp packet scheduler;
1321 : : * returns the subflow that will transmit the next DSS
1322 : : * additionally updates the rtx timeout
1323 : : */
1324 : 396638 : struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1325 : : {
1326 : 396638 : struct subflow_send_info send_info[SSK_MODE_MAX];
1327 : 396638 : struct mptcp_subflow_context *subflow;
1328 : 396638 : struct sock *sk = (struct sock *)msk;
1329 : 396638 : u32 pace, burst, wmem;
1330 : 396638 : int i, nr_active = 0;
1331 : 396638 : struct sock *ssk;
1332 : 396638 : u64 linger_time;
1333 : 396638 : long tout = 0;
1334 : :
1335 : : /* pick the subflow with the lower wmem/wspace ratio */
1336 [ + + ]: 1189914 : for (i = 0; i < SSK_MODE_MAX; ++i) {
1337 : 793276 : send_info[i].ssk = NULL;
1338 : 793276 : send_info[i].linger_time = -1;
1339 : : }
1340 : :
1341 [ + + ]: 1108194 : mptcp_for_each_subflow(msk, subflow) {
1342 : 711556 : bool backup = subflow->backup || subflow->request_bkup;
1343 : :
1344 : 711556 : trace_mptcp_subflow_get_send(subflow);
1345 : 711556 : ssk = mptcp_subflow_tcp_sock(subflow);
1346 [ + + ]: 711556 : if (!mptcp_subflow_active(subflow))
1347 : 73292 : continue;
1348 : :
1349 [ + + ]: 638264 : tout = max(tout, mptcp_timeout_from_subflow(subflow));
1350 : 638264 : nr_active += !backup;
1351 : 638264 : pace = subflow->avg_pacing_rate;
1352 [ + + ]: 638264 : if (unlikely(!pace)) {
1353 : : /* init pacing rate from socket */
1354 : 2490 : subflow->avg_pacing_rate = READ_ONCE(ssk->sk_pacing_rate);
1355 : 2490 : pace = subflow->avg_pacing_rate;
1356 [ - + ]: 2490 : if (!pace)
1357 : 0 : continue;
1358 : : }
1359 : :
1360 [ + + ]: 638264 : linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1361 [ + + ]: 638264 : if (linger_time < send_info[backup].linger_time) {
1362 : 502546 : send_info[backup].ssk = ssk;
1363 : 502546 : send_info[backup].linger_time = linger_time;
1364 : : }
1365 : : }
1366 : 396638 : __mptcp_set_timeout(sk, tout);
1367 : :
1368 : : /* pick the best backup if no other subflow is active */
1369 [ + + ]: 396638 : if (!nr_active)
1370 : 24472 : send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
1371 : :
1372 : : /* According to the blest algorithm, to avoid HoL blocking for the
1373 : : * faster flow, we need to:
1374 : : * - estimate the faster flow linger time
1375 : : * - use the above to estimate the amount of byte transferred
1376 : : * by the faster flow
1377 : : * - check that the amount of queued data is greter than the above,
1378 : : * otherwise do not use the picked, slower, subflow
1379 : : * We select the subflow with the shorter estimated time to flush
1380 : : * the queued mem, which basically ensure the above. We just need
1381 : : * to check that subflow has a non empty cwin.
1382 : : */
1383 : 396638 : ssk = send_info[SSK_MODE_ACTIVE].ssk;
1384 [ + + + + ]: 793270 : if (!ssk || !sk_stream_memory_free(ssk))
1385 : 70328 : return NULL;
1386 : :
1387 : 326310 : burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1388 : 326310 : wmem = READ_ONCE(ssk->sk_wmem_queued);
1389 [ + + ]: 326310 : if (!burst)
1390 : : return ssk;
1391 : :
1392 : 220734 : subflow = mptcp_subflow_ctx(ssk);
1393 : 220734 : subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1394 : 220734 : READ_ONCE(ssk->sk_pacing_rate) * burst,
1395 : : burst + wmem);
1396 : 220734 : msk->snd_burst = burst;
1397 : 220734 : return ssk;
1398 : : }
1399 : :
1400 : 498789 : static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1401 : : {
1402 [ - + ]: 498789 : tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1403 : 498789 : release_sock(ssk);
1404 : 498789 : }
1405 : :
1406 : 532749 : static void mptcp_update_post_push(struct mptcp_sock *msk,
1407 : : struct mptcp_data_frag *dfrag,
1408 : : u32 sent)
1409 : : {
1410 : 532749 : u64 snd_nxt_new = dfrag->data_seq;
1411 : :
1412 : 532749 : dfrag->already_sent += sent;
1413 : :
1414 : 532749 : msk->snd_burst -= sent;
1415 : :
1416 : 532749 : snd_nxt_new += dfrag->already_sent;
1417 : :
1418 : : /* snd_nxt_new can be smaller than snd_nxt in case mptcp
1419 : : * is recovering after a failover. In that event, this re-sends
1420 : : * old segments.
1421 : : *
1422 : : * Thus compute snd_nxt_new candidate based on
1423 : : * the dfrag->data_seq that was sent and the data
1424 : : * that has been handed to the subflow for transmission
1425 : : * and skip update in case it was old dfrag.
1426 : : */
1427 [ + + ]: 532749 : if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
1428 : 528614 : msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
1429 : 528614 : WRITE_ONCE(msk->snd_nxt, snd_nxt_new);
1430 : : }
1431 : 532749 : }
1432 : :
1433 : 10355 : void mptcp_check_and_set_pending(struct sock *sk)
1434 : : {
1435 [ + + ]: 10355 : if (mptcp_send_head(sk)) {
1436 : 1955 : mptcp_data_lock(sk);
1437 [ - + ]: 1955 : mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING);
1438 : 1955 : mptcp_data_unlock(sk);
1439 : : }
1440 : 10355 : }
1441 : :
1442 : 593845 : static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
1443 : : struct mptcp_sendmsg_info *info)
1444 : : {
1445 [ - + ]: 593845 : struct mptcp_sock *msk = mptcp_sk(sk);
1446 : 337868 : struct mptcp_data_frag *dfrag;
1447 : 337868 : int len, copied = 0, err = 0;
1448 : :
1449 [ + + ]: 852241 : while ((dfrag = mptcp_send_head(sk))) {
1450 : 620419 : info->sent = dfrag->already_sent;
1451 : 620419 : info->limit = dfrag->data_len;
1452 : 620419 : len = dfrag->data_len - dfrag->already_sent;
1453 [ + + ]: 1153168 : while (len > 0) {
1454 : 686955 : int ret = 0;
1455 : :
1456 : 686955 : ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
1457 [ + + ]: 686955 : if (ret <= 0) {
1458 [ + + ]: 154206 : err = copied ? : ret;
1459 : 154206 : goto out;
1460 : : }
1461 : :
1462 : 532749 : info->sent += ret;
1463 : 532749 : copied += ret;
1464 : 532749 : len -= ret;
1465 : :
1466 : 532749 : mptcp_update_post_push(msk, dfrag, ret);
1467 : : }
1468 : 466213 : WRITE_ONCE(msk->first_pending, mptcp_send_next(sk));
1469 : :
1470 [ + + + + ]: 670642 : if (msk->snd_burst <= 0 ||
1471 [ + + - + ]: 318601 : !sk_stream_memory_free(ssk) ||
1472 : 258396 : !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
1473 : 207817 : err = copied;
1474 : 207817 : goto out;
1475 : : }
1476 : 258396 : mptcp_set_timeout(sk);
1477 : : }
1478 : : err = copied;
1479 : :
1480 : 593845 : out:
1481 [ + + ]: 593845 : if (err > 0)
1482 : 432282 : msk->last_data_sent = tcp_jiffies32;
1483 : 593845 : return err;
1484 : : }
1485 : :
1486 : 519866 : void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1487 : : {
1488 : 519866 : struct sock *prev_ssk = NULL, *ssk = NULL;
1489 [ - + ]: 519866 : struct mptcp_sock *msk = mptcp_sk(sk);
1490 : 519866 : struct mptcp_sendmsg_info info = {
1491 : : .flags = flags,
1492 : : };
1493 : 519866 : bool do_check_data_fin = false;
1494 : 519866 : int push_count = 1;
1495 : :
1496 [ + + + + ]: 995998 : while (mptcp_send_head(sk) && (push_count > 0)) {
1497 : 531208 : struct mptcp_subflow_context *subflow;
1498 : 531208 : int ret = 0;
1499 : :
1500 [ + + ]: 531208 : if (mptcp_sched_get_send(msk))
1501 : : break;
1502 : :
1503 : 476132 : push_count = 0;
1504 : :
1505 [ + + ]: 1292442 : mptcp_for_each_subflow(msk, subflow) {
1506 [ + + + + ]: 816310 : if (READ_ONCE(subflow->scheduled)) {
1507 : 518020 : mptcp_subflow_set_scheduled(subflow, false);
1508 : :
1509 : 518020 : prev_ssk = ssk;
1510 [ + + ]: 518020 : ssk = mptcp_subflow_tcp_sock(subflow);
1511 [ + + ]: 518020 : if (ssk != prev_ssk) {
1512 : : /* First check. If the ssk has changed since
1513 : : * the last round, release prev_ssk
1514 : : */
1515 [ + + ]: 498789 : if (prev_ssk)
1516 : 44258 : mptcp_push_release(prev_ssk, &info);
1517 : :
1518 : : /* Need to lock the new subflow only if different
1519 : : * from the previous one, otherwise we are still
1520 : : * helding the relevant lock
1521 : : */
1522 : 498789 : lock_sock(ssk);
1523 : : }
1524 : :
1525 : 518020 : push_count++;
1526 : :
1527 : 518020 : ret = __subflow_push_pending(sk, ssk, &info);
1528 [ + + ]: 518020 : if (ret <= 0) {
1529 [ - + - - ]: 133239 : if (ret != -EAGAIN ||
1530 [ # # ]: 0 : (1 << ssk->sk_state) &
1531 : : (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
1532 : 71555 : push_count--;
1533 : 133239 : continue;
1534 : : }
1535 : : do_check_data_fin = true;
1536 : : }
1537 : : }
1538 : : }
1539 : :
1540 : : /* at this point we held the socket lock for the last subflow we used */
1541 [ + + ]: 519866 : if (ssk)
1542 : 454531 : mptcp_push_release(ssk, &info);
1543 : :
1544 : : /* ensure the rtx timer is running */
1545 [ + + ]: 519866 : if (!mptcp_rtx_timer_pending(sk))
1546 : 35050 : mptcp_reset_rtx_timer(sk);
1547 [ + + ]: 519866 : if (do_check_data_fin)
1548 : 365899 : mptcp_check_send_data_fin(sk);
1549 : 519866 : }
1550 : :
1551 : 77231 : static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
1552 : : {
1553 [ - + ]: 77231 : struct mptcp_sock *msk = mptcp_sk(sk);
1554 : 77231 : struct mptcp_sendmsg_info info = {
1555 : : .data_lock_held = true,
1556 : : };
1557 : 77231 : bool keep_pushing = true;
1558 : 77231 : struct sock *xmit_ssk;
1559 : 77231 : int copied = 0;
1560 : :
1561 : 77231 : info.flags = 0;
1562 [ + + + + ]: 155775 : while (mptcp_send_head(sk) && keep_pushing) {
1563 [ + + ]: 122304 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1564 : 122304 : int ret = 0;
1565 : :
1566 : : /* check for a different subflow usage only after
1567 : : * spooling the first chunk of data
1568 : : */
1569 [ + + ]: 122304 : if (first) {
1570 : 3501 : mptcp_subflow_set_scheduled(subflow, false);
1571 : 3501 : ret = __subflow_push_pending(sk, ssk, &info);
1572 : 3501 : first = false;
1573 [ + + ]: 3501 : if (ret <= 0)
1574 : : break;
1575 : 2107 : copied += ret;
1576 : 2107 : continue;
1577 : : }
1578 : :
1579 [ + + ]: 118803 : if (mptcp_sched_get_send(msk))
1580 : 42366 : goto out;
1581 : :
1582 [ + + + + ]: 76437 : if (READ_ONCE(subflow->scheduled)) {
1583 : 72324 : mptcp_subflow_set_scheduled(subflow, false);
1584 : 72324 : ret = __subflow_push_pending(sk, ssk, &info);
1585 [ + + ]: 72324 : if (ret <= 0)
1586 : 26930 : keep_pushing = false;
1587 : 72324 : copied += ret;
1588 : : }
1589 : :
1590 [ + + ]: 183579 : mptcp_for_each_subflow(msk, subflow) {
1591 [ + + + + ]: 107142 : if (READ_ONCE(subflow->scheduled)) {
1592 [ + - ]: 4113 : xmit_ssk = mptcp_subflow_tcp_sock(subflow);
1593 [ + - ]: 4113 : if (xmit_ssk != ssk) {
1594 : 4113 : mptcp_subflow_delegate(subflow,
1595 : : MPTCP_DELEGATE_SEND);
1596 : 4113 : keep_pushing = false;
1597 : : }
1598 : : }
1599 : : }
1600 : : }
1601 : :
1602 : 34865 : out:
1603 : : /* __mptcp_alloc_tx_skb could have released some wmem and we are
1604 : : * not going to flush it via release_sock()
1605 : : */
1606 [ + + ]: 77231 : if (copied) {
1607 [ - + ]: 39039 : tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1608 : : info.size_goal);
1609 [ + + ]: 39039 : if (!mptcp_rtx_timer_pending(sk))
1610 : 91 : mptcp_reset_rtx_timer(sk);
1611 : :
1612 [ + + + + ]: 39039 : if (msk->snd_data_fin_enable &&
1613 [ + + ]: 5796 : msk->snd_nxt + 1 == msk->write_seq)
1614 : 288 : mptcp_schedule_work(sk);
1615 : : }
1616 : 77231 : }
1617 : :
1618 : : static int mptcp_disconnect(struct sock *sk, int flags);
1619 : :
1620 : 88 : static int mptcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1621 : : size_t len, int *copied_syn)
1622 : : {
1623 : 88 : unsigned int saved_flags = msg->msg_flags;
1624 [ - + ]: 88 : struct mptcp_sock *msk = mptcp_sk(sk);
1625 : 88 : struct sock *ssk;
1626 : 88 : int ret;
1627 : :
1628 : : /* on flags based fastopen the mptcp is supposed to create the
1629 : : * first subflow right now. Otherwise we are in the defer_connect
1630 : : * path, and the first subflow must be already present.
1631 : : * Since the defer_connect flag is cleared after the first succsful
1632 : : * fastopen attempt, no need to check for additional subflow status.
1633 : : */
1634 [ + + ]: 88 : if (msg->msg_flags & MSG_FASTOPEN) {
1635 : 52 : ssk = __mptcp_nmpc_sk(msk);
1636 [ + + ]: 52 : if (IS_ERR(ssk))
1637 : 6 : return PTR_ERR(ssk);
1638 : : }
1639 [ + - ]: 82 : if (!msk->first)
1640 : : return -EINVAL;
1641 : :
1642 : 82 : ssk = msk->first;
1643 : :
1644 : 82 : lock_sock(ssk);
1645 : 82 : msg->msg_flags |= MSG_DONTWAIT;
1646 : 82 : msk->fastopening = 1;
1647 : 82 : ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1648 : 82 : msk->fastopening = 0;
1649 : 82 : msg->msg_flags = saved_flags;
1650 : 82 : release_sock(ssk);
1651 : :
1652 : : /* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1653 [ + - + + ]: 82 : if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1654 : 22 : ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1655 : : msg->msg_namelen, msg->msg_flags, 1);
1656 : :
1657 : : /* Keep the same behaviour of plain TCP: zero the copied bytes in
1658 : : * case of any error, except timeout or signal
1659 : : */
1660 [ - + - - ]: 22 : if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1661 : 0 : *copied_syn = 0;
1662 [ + - ]: 60 : } else if (ret && ret != -EINPROGRESS) {
1663 : : /* The disconnect() op called by tcp_sendmsg_fastopen()/
1664 : : * __inet_stream_connect() can fail, due to looking check,
1665 : : * see mptcp_disconnect().
1666 : : * Attempt it again outside the problematic scope.
1667 : : */
1668 [ # # ]: 0 : if (!mptcp_disconnect(sk, 0)) {
1669 : 0 : sk->sk_disconnects++;
1670 : 0 : sk->sk_socket->state = SS_UNCONNECTED;
1671 : : }
1672 : : }
1673 : 82 : inet_clear_bit(DEFER_CONNECT, sk);
1674 : :
1675 : 82 : return ret;
1676 : : }
1677 : :
1678 : 600598 : static int do_copy_data_nocache(struct sock *sk, int copy,
1679 : : struct iov_iter *from, char *to)
1680 : : {
1681 [ - + ]: 600598 : if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
1682 [ # # ]: 0 : if (!copy_from_iter_full_nocache(to, copy, from))
1683 : 0 : return -EFAULT;
1684 [ - + ]: 600598 : } else if (!copy_from_iter_full(to, copy, from)) {
1685 : 0 : return -EFAULT;
1686 : : }
1687 : : return 0;
1688 : : }
1689 : :
1690 : : /* open-code sk_stream_memory_free() plus sent limit computation to
1691 : : * avoid indirect calls in fast-path.
1692 : : * Called under the msk socket lock, so we can avoid a bunch of ONCE
1693 : : * annotations.
1694 : : */
1695 : 603051 : static u32 mptcp_send_limit(const struct sock *sk)
1696 : : {
1697 [ - + ]: 603051 : const struct mptcp_sock *msk = mptcp_sk(sk);
1698 : 603051 : u32 limit, not_sent;
1699 : :
1700 [ + + ]: 603051 : if (sk->sk_wmem_queued >= READ_ONCE(sk->sk_sndbuf))
1701 : : return 0;
1702 : :
1703 : 600598 : limit = mptcp_notsent_lowat(sk);
1704 [ - + ]: 600598 : if (limit == UINT_MAX)
1705 : : return UINT_MAX;
1706 : :
1707 : 0 : not_sent = msk->write_seq - msk->snd_nxt;
1708 [ # # ]: 0 : if (not_sent >= limit)
1709 : : return 0;
1710 : :
1711 : 0 : return limit - not_sent;
1712 : : }
1713 : :
1714 : 498589 : static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1715 : : {
1716 [ - + ]: 498589 : struct mptcp_sock *msk = mptcp_sk(sk);
1717 : 498589 : struct page_frag *pfrag;
1718 : 498589 : size_t copied = 0;
1719 : 498589 : int ret = 0;
1720 : 498589 : long timeo;
1721 : :
1722 : : /* silently ignore everything else */
1723 : 498589 : msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1724 : :
1725 : 498589 : lock_sock(sk);
1726 : :
1727 [ + + + + ]: 498589 : if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
1728 : : msg->msg_flags & MSG_FASTOPEN)) {
1729 : 88 : int copied_syn = 0;
1730 : :
1731 : 88 : ret = mptcp_sendmsg_fastopen(sk, msg, len, &copied_syn);
1732 : 88 : copied += copied_syn;
1733 [ + + + + ]: 88 : if (ret == -EINPROGRESS && copied_syn > 0)
1734 : 42 : goto out;
1735 [ + + ]: 46 : else if (ret)
1736 : 24 : goto do_error;
1737 : : }
1738 : :
1739 [ + + ]: 498523 : timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1740 : :
1741 [ + + + + ]: 498523 : if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
1742 : 58 : ret = sk_stream_wait_connect(sk, &timeo);
1743 [ + - ]: 58 : if (ret)
1744 : 58 : goto do_error;
1745 : : }
1746 : :
1747 : 498465 : ret = -EPIPE;
1748 [ + - - + ]: 498465 : if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1749 : 0 : goto do_error;
1750 : :
1751 [ + - ]: 498465 : pfrag = sk_page_frag(sk);
1752 : :
1753 [ + + ]: 1101515 : while (msg_data_left(msg)) {
1754 : 603051 : int total_ts, frag_truesize = 0;
1755 : 603051 : struct mptcp_data_frag *dfrag;
1756 : 603051 : bool dfrag_collapsed;
1757 : 603051 : size_t psize, offset;
1758 : 603051 : u32 copy_limit;
1759 : :
1760 : : /* ensure fitting the notsent_lowat() constraint */
1761 : 603051 : copy_limit = mptcp_send_limit(sk);
1762 [ + + ]: 603051 : if (!copy_limit)
1763 : 2453 : goto wait_for_memory;
1764 : :
1765 : : /* reuse tail pfrag, if possible, or carve a new one from the
1766 : : * page allocator
1767 : : */
1768 : 600598 : dfrag = mptcp_pending_tail(sk);
1769 : 600598 : dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1770 [ + + ]: 600598 : if (!dfrag_collapsed) {
1771 [ - + ]: 462399 : if (!mptcp_page_frag_refill(sk, pfrag))
1772 : 0 : goto wait_for_memory;
1773 : :
1774 : 462399 : dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1775 : 462399 : frag_truesize = dfrag->overhead;
1776 : : }
1777 : :
1778 : : /* we do not bound vs wspace, to allow a single packet.
1779 : : * memory accounting will prevent execessive memory usage
1780 : : * anyway
1781 : : */
1782 : 600598 : offset = dfrag->offset + dfrag->data_len;
1783 : 600598 : psize = pfrag->size - offset;
1784 : 600598 : psize = min_t(size_t, psize, msg_data_left(msg));
1785 : 600598 : psize = min_t(size_t, psize, copy_limit);
1786 : 600598 : total_ts = psize + frag_truesize;
1787 : :
1788 [ - + ]: 600598 : if (!sk_wmem_schedule(sk, total_ts))
1789 : 0 : goto wait_for_memory;
1790 : :
1791 : 862232 : ret = do_copy_data_nocache(sk, psize, &msg->msg_iter,
1792 : 600598 : page_address(dfrag->page) + offset);
1793 [ - + ]: 600598 : if (ret)
1794 : 0 : goto do_error;
1795 : :
1796 : : /* data successfully copied into the write queue */
1797 [ + + ]: 600598 : sk_forward_alloc_add(sk, -total_ts);
1798 : 600598 : copied += psize;
1799 : 600598 : dfrag->data_len += psize;
1800 : 600598 : frag_truesize += psize;
1801 : 600598 : pfrag->offset += frag_truesize;
1802 : 600598 : WRITE_ONCE(msk->write_seq, msk->write_seq + psize);
1803 : :
1804 : : /* charge data on mptcp pending queue to the msk socket
1805 : : * Note: we charge such data both to sk and ssk
1806 : : */
1807 [ + + ]: 600598 : sk_wmem_queued_add(sk, frag_truesize);
1808 [ + + ]: 600598 : if (!dfrag_collapsed) {
1809 : 462399 : get_page(dfrag->page);
1810 [ + + ]: 462399 : list_add_tail(&dfrag->list, &msk->rtx_queue);
1811 [ + + ]: 462399 : if (!msk->first_pending)
1812 : 361218 : WRITE_ONCE(msk->first_pending, dfrag);
1813 : : }
1814 [ - + ]: 600598 : pr_debug("msk=%p dfrag at seq=%llu len=%u sent=%u new=%d\n", msk,
1815 : : dfrag->data_seq, dfrag->data_len, dfrag->already_sent,
1816 : : !dfrag_collapsed);
1817 : :
1818 : 600598 : continue;
1819 : :
1820 : 2453 : wait_for_memory:
1821 : 2453 : set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1822 : 2453 : __mptcp_push_pending(sk, msg->msg_flags);
1823 : 2453 : ret = sk_stream_wait_memory(sk, &timeo);
1824 [ + + ]: 2453 : if (ret)
1825 : 1 : goto do_error;
1826 : : }
1827 : :
1828 [ + + ]: 498464 : if (copied)
1829 : 498451 : __mptcp_push_pending(sk, msg->msg_flags);
1830 : :
1831 : 13 : out:
1832 : 498589 : release_sock(sk);
1833 : 498589 : return copied;
1834 : :
1835 : 83 : do_error:
1836 [ + + ]: 83 : if (copied)
1837 : 1 : goto out;
1838 : :
1839 : 82 : copied = sk_stream_error(sk, msg->msg_flags, ret);
1840 : 82 : goto out;
1841 : : }
1842 : :
1843 : : static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied);
1844 : :
1845 : 683383 : static int __mptcp_recvmsg_mskq(struct sock *sk,
1846 : : struct msghdr *msg,
1847 : : size_t len, int flags,
1848 : : struct scm_timestamping_internal *tss,
1849 : : int *cmsg_flags)
1850 : : {
1851 [ - + ]: 683383 : struct mptcp_sock *msk = mptcp_sk(sk);
1852 : 683383 : struct sk_buff *skb, *tmp;
1853 : 683383 : int copied = 0;
1854 : :
1855 [ + + ]: 871221 : skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) {
1856 : 724317 : u32 offset = MPTCP_SKB_CB(skb)->offset;
1857 : 724317 : u32 data_len = skb->len - offset;
1858 : 724317 : u32 count = min_t(size_t, len - copied, data_len);
1859 : 724317 : int err;
1860 : :
1861 [ + - ]: 724317 : if (!(flags & MSG_TRUNC)) {
1862 : 724317 : err = skb_copy_datagram_msg(skb, offset, msg, count);
1863 [ - + ]: 724317 : if (unlikely(err < 0)) {
1864 [ # # ]: 0 : if (!copied)
1865 : : return err;
1866 : : break;
1867 : : }
1868 : : }
1869 : :
1870 [ + + ]: 724317 : if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
1871 : 598 : tcp_update_recv_tstamps(skb, tss);
1872 : 598 : *cmsg_flags |= MPTCP_CMSG_TS;
1873 : : }
1874 : :
1875 : 724317 : copied += count;
1876 : :
1877 [ + + ]: 724317 : if (count < data_len) {
1878 [ + + ]: 477678 : if (!(flags & MSG_PEEK)) {
1879 : 457123 : MPTCP_SKB_CB(skb)->offset += count;
1880 : 457123 : MPTCP_SKB_CB(skb)->map_seq += count;
1881 : 457123 : msk->bytes_consumed += count;
1882 : : }
1883 : : break;
1884 : : }
1885 : :
1886 [ + + ]: 246639 : if (!(flags & MSG_PEEK)) {
1887 : : /* avoid the indirect call, we know the destructor is sock_wfree */
1888 : 243883 : skb->destructor = NULL;
1889 : 243883 : atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
1890 : 243883 : sk_mem_uncharge(sk, skb->truesize);
1891 : 243883 : __skb_unlink(skb, &sk->sk_receive_queue);
1892 : 243883 : __kfree_skb(skb);
1893 : 243883 : msk->bytes_consumed += count;
1894 : : }
1895 : :
1896 [ + + ]: 246639 : if (copied >= len)
1897 : : break;
1898 : : }
1899 : :
1900 : 683383 : mptcp_rcv_space_adjust(msk, copied);
1901 : 683383 : return copied;
1902 : : }
1903 : :
1904 : : /* receive buffer autotuning. See tcp_rcv_space_adjust for more information.
1905 : : *
1906 : : * Only difference: Use highest rtt estimate of the subflows in use.
1907 : : */
1908 : 683383 : static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
1909 : : {
1910 : 683383 : struct mptcp_subflow_context *subflow;
1911 : 683383 : struct sock *sk = (struct sock *)msk;
1912 : 683383 : u8 scaling_ratio = U8_MAX;
1913 : 683383 : u32 time, advmss = 1;
1914 : 683383 : u64 rtt_us, mstamp;
1915 : :
1916 : 683383 : msk_owned_by_me(msk);
1917 : :
1918 [ + + ]: 683383 : if (copied <= 0)
1919 : : return;
1920 : :
1921 [ - + ]: 662292 : if (!msk->rcvspace_init)
1922 : 0 : mptcp_rcv_space_init(msk, msk->first);
1923 : :
1924 : 662292 : msk->rcvq_space.copied += copied;
1925 : :
1926 [ + + ]: 662292 : mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
1927 [ + + ]: 662292 : time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
1928 : :
1929 : 662292 : rtt_us = msk->rcvq_space.rtt_us;
1930 [ + + + + ]: 662292 : if (rtt_us && time < (rtt_us >> 3))
1931 : : return;
1932 : :
1933 : 95282 : rtt_us = 0;
1934 [ + + ]: 210822 : mptcp_for_each_subflow(msk, subflow) {
1935 : 115540 : const struct tcp_sock *tp;
1936 : 115540 : u64 sf_rtt_us;
1937 : 115540 : u32 sf_advmss;
1938 : :
1939 [ - + ]: 115540 : tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
1940 : :
1941 : 115540 : sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
1942 : 115540 : sf_advmss = READ_ONCE(tp->advmss);
1943 : :
1944 : 115540 : rtt_us = max(sf_rtt_us, rtt_us);
1945 : 115540 : advmss = max(sf_advmss, advmss);
1946 : 115540 : scaling_ratio = min(tp->scaling_ratio, scaling_ratio);
1947 : : }
1948 : :
1949 : 95282 : msk->rcvq_space.rtt_us = rtt_us;
1950 : 95282 : msk->scaling_ratio = scaling_ratio;
1951 [ + + + + ]: 95282 : if (time < (rtt_us >> 3) || rtt_us == 0)
1952 : : return;
1953 : :
1954 [ + + ]: 82746 : if (msk->rcvq_space.copied <= msk->rcvq_space.space)
1955 : 79890 : goto new_measure;
1956 : :
1957 [ + - ]: 2856 : if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
1958 [ + - ]: 2856 : !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1959 : 2856 : u64 rcvwin, grow;
1960 : 2856 : int rcvbuf;
1961 : :
1962 : 2856 : rcvwin = ((u64)msk->rcvq_space.copied << 1) + 16 * advmss;
1963 : :
1964 : 2856 : grow = rcvwin * (msk->rcvq_space.copied - msk->rcvq_space.space);
1965 : :
1966 : 2856 : do_div(grow, msk->rcvq_space.space);
1967 : 2856 : rcvwin += (grow << 1);
1968 : :
1969 [ + + ]: 2856 : rcvbuf = min_t(u64, mptcp_space_from_win(sk, rcvwin),
1970 : : READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
1971 : :
1972 [ + + ]: 2856 : if (rcvbuf > sk->sk_rcvbuf) {
1973 : 1200 : u32 window_clamp;
1974 : :
1975 : 1200 : window_clamp = mptcp_win_from_space(sk, rcvbuf);
1976 : 1200 : WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
1977 : :
1978 : : /* Make subflows follow along. If we do not do this, we
1979 : : * get drops at subflow level if skbs can't be moved to
1980 : : * the mptcp rx queue fast enough (announced rcv_win can
1981 : : * exceed ssk->sk_rcvbuf).
1982 : : */
1983 [ + + ]: 2543 : mptcp_for_each_subflow(msk, subflow) {
1984 : 1343 : struct sock *ssk;
1985 : 1343 : bool slow;
1986 : :
1987 : 1343 : ssk = mptcp_subflow_tcp_sock(subflow);
1988 : 1343 : slow = lock_sock_fast(ssk);
1989 : 1343 : WRITE_ONCE(ssk->sk_rcvbuf, rcvbuf);
1990 [ - + ]: 1343 : WRITE_ONCE(tcp_sk(ssk)->window_clamp, window_clamp);
1991 [ + + ]: 1343 : if (tcp_can_send_ack(ssk))
1992 : 1340 : tcp_cleanup_rbuf(ssk, 1);
1993 : 1343 : unlock_sock_fast(ssk, slow);
1994 : : }
1995 : : }
1996 : : }
1997 : :
1998 : 2856 : msk->rcvq_space.space = msk->rcvq_space.copied;
1999 : 82746 : new_measure:
2000 : 82746 : msk->rcvq_space.copied = 0;
2001 : 82746 : msk->rcvq_space.time = mstamp;
2002 : : }
2003 : :
2004 : : static struct mptcp_subflow_context *
2005 : 182132 : __mptcp_first_ready_from(struct mptcp_sock *msk,
2006 : : struct mptcp_subflow_context *subflow)
2007 : : {
2008 : 182132 : struct mptcp_subflow_context *start_subflow = subflow;
2009 : :
2010 [ + + + + ]: 531173 : while (!READ_ONCE(subflow->data_avail)) {
2011 [ + + ]: 466819 : subflow = mptcp_next_subflow(msk, subflow);
2012 [ + + ]: 466819 : if (subflow == start_subflow)
2013 : : return NULL;
2014 : : }
2015 : : return subflow;
2016 : : }
2017 : :
2018 : 247971 : static bool __mptcp_move_skbs(struct sock *sk)
2019 : : {
2020 : 247971 : struct mptcp_subflow_context *subflow;
2021 [ - + ]: 247971 : struct mptcp_sock *msk = mptcp_sk(sk);
2022 : 247971 : bool ret = false;
2023 : :
2024 [ + + ]: 247971 : if (list_empty(&msk->conn_list))
2025 : : return false;
2026 : :
2027 : : /* verify we can move any data from the subflow, eventually updating */
2028 [ + - ]: 247964 : if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
2029 [ + + ]: 711005 : mptcp_for_each_subflow(msk, subflow)
2030 [ + + ]: 463041 : __mptcp_rcvbuf_update(sk, subflow->tcp_sock);
2031 : :
2032 : 247964 : subflow = list_first_entry(&msk->conn_list,
2033 : : struct mptcp_subflow_context, node);
2034 : 312318 : for (;;) {
2035 : 312318 : struct sock *ssk;
2036 : 312318 : bool slowpath;
2037 : :
2038 : : /*
2039 : : * As an optimization avoid traversing the subflows list
2040 : : * and ev. acquiring the subflow socket lock before baling out
2041 : : */
2042 [ + + ]: 312318 : if (sk_rmem_alloc_get(sk) > sk->sk_rcvbuf)
2043 : : break;
2044 : :
2045 : 232925 : subflow = __mptcp_first_ready_from(msk, subflow);
2046 [ + + ]: 308553 : if (!subflow)
2047 : : break;
2048 : :
2049 : 64354 : ssk = mptcp_subflow_tcp_sock(subflow);
2050 : 64354 : slowpath = lock_sock_fast(ssk);
2051 [ + + + + ]: 64354 : ret = __mptcp_move_skbs_from_subflow(msk, ssk) || ret;
2052 [ - + ]: 64354 : if (unlikely(ssk->sk_err))
2053 : 0 : __mptcp_error_report(sk);
2054 : 64354 : unlock_sock_fast(ssk, slowpath);
2055 : :
2056 [ + + ]: 64354 : subflow = mptcp_next_subflow(msk, subflow);
2057 : : }
2058 : :
2059 : 247964 : __mptcp_ofo_queue(msk);
2060 [ + + ]: 247964 : if (ret)
2061 : 51631 : mptcp_check_data_fin((struct sock *)msk);
2062 : : return ret;
2063 : : }
2064 : :
2065 : 432 : static unsigned int mptcp_inq_hint(const struct sock *sk)
2066 : : {
2067 [ - + ]: 432 : const struct mptcp_sock *msk = mptcp_sk(sk);
2068 : 432 : const struct sk_buff *skb;
2069 : :
2070 [ + + ]: 432 : skb = skb_peek(&sk->sk_receive_queue);
2071 [ + - ]: 1 : if (skb) {
2072 : 1 : u64 hint_val = READ_ONCE(msk->ack_seq) - MPTCP_SKB_CB(skb)->map_seq;
2073 : :
2074 [ + - ]: 1 : if (hint_val >= INT_MAX)
2075 : : return INT_MAX;
2076 : :
2077 : 1 : return (unsigned int)hint_val;
2078 : : }
2079 : :
2080 [ + + + + ]: 431 : if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2081 : 8 : return 1;
2082 : :
2083 : : return 0;
2084 : : }
2085 : :
2086 : 661334 : static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2087 : : int flags, int *addr_len)
2088 : : {
2089 [ - + ]: 661334 : struct mptcp_sock *msk = mptcp_sk(sk);
2090 : 661334 : struct scm_timestamping_internal tss;
2091 : 661334 : int copied = 0, cmsg_flags = 0;
2092 : 661334 : int target;
2093 : 661334 : long timeo;
2094 : :
2095 : : /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2096 [ - + ]: 661334 : if (unlikely(flags & MSG_ERRQUEUE))
2097 : 0 : return inet_recv_error(sk, msg, len, addr_len);
2098 : :
2099 : 661334 : lock_sock(sk);
2100 [ - + ]: 661334 : if (unlikely(sk->sk_state == TCP_LISTEN)) {
2101 : 0 : copied = -ENOTCONN;
2102 : 0 : goto out_err;
2103 : : }
2104 : :
2105 [ + + ]: 661334 : timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2106 : :
2107 : 661334 : len = min_t(size_t, len, INT_MAX);
2108 [ + - ]: 661334 : target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2109 : :
2110 [ + + ]: 661334 : if (unlikely(msk->recvmsg_inq))
2111 : 432 : cmsg_flags = MPTCP_CMSG_INQ;
2112 : :
2113 [ + + ]: 684005 : while (copied < len) {
2114 : 683383 : int err, bytes_read;
2115 : :
2116 : 683383 : bytes_read = __mptcp_recvmsg_mskq(sk, msg, len - copied, flags, &tss, &cmsg_flags);
2117 [ - + ]: 683383 : if (unlikely(bytes_read < 0)) {
2118 [ # # ]: 0 : if (!copied)
2119 : 0 : copied = bytes_read;
2120 : 0 : goto out_err;
2121 : : }
2122 : :
2123 : 683383 : copied += bytes_read;
2124 : :
2125 [ + + + + ]: 683383 : if (skb_queue_empty(&sk->sk_receive_queue) && __mptcp_move_skbs(sk))
2126 : 3078 : continue;
2127 : :
2128 : : /* only the MPTCP socket status is relevant here. The exit
2129 : : * conditions mirror closely tcp_recvmsg()
2130 : : */
2131 [ + + ]: 680305 : if (copied >= target)
2132 : : break;
2133 : :
2134 [ - + ]: 21015 : if (copied) {
2135 [ # # ]: 0 : if (sk->sk_err ||
2136 [ # # ]: 0 : sk->sk_state == TCP_CLOSE ||
2137 [ # # ]: 0 : (sk->sk_shutdown & RCV_SHUTDOWN) ||
2138 [ # # # # ]: 0 : !timeo ||
2139 : 0 : signal_pending(current))
2140 : : break;
2141 : : } else {
2142 [ + + ]: 21015 : if (sk->sk_err) {
2143 : 2 : copied = sock_error(sk);
2144 : 2 : break;
2145 : : }
2146 : :
2147 [ + + ]: 21013 : if (sk->sk_shutdown & RCV_SHUTDOWN) {
2148 : : /* race breaker: the shutdown could be after the
2149 : : * previous receive queue check
2150 : : */
2151 [ - + ]: 1402 : if (__mptcp_move_skbs(sk))
2152 : 0 : continue;
2153 : : break;
2154 : : }
2155 : :
2156 [ + + ]: 19611 : if (sk->sk_state == TCP_CLOSE) {
2157 : : copied = -ENOTCONN;
2158 : : break;
2159 : : }
2160 : :
2161 [ + - ]: 19593 : if (!timeo) {
2162 : : copied = -EAGAIN;
2163 : : break;
2164 : : }
2165 : :
2166 [ - + ]: 19593 : if (signal_pending(current)) {
2167 [ # # ]: 0 : copied = sock_intr_errno(timeo);
2168 : : break;
2169 : : }
2170 : : }
2171 : :
2172 [ - + ]: 19593 : pr_debug("block timeout %ld\n", timeo);
2173 : 19593 : mptcp_cleanup_rbuf(msk, copied);
2174 : 19593 : err = sk_wait_data(sk, &timeo, NULL);
2175 [ - + ]: 19593 : if (err < 0) {
2176 : 0 : err = copied ? : err;
2177 : 0 : goto out_err;
2178 : : }
2179 : : }
2180 : :
2181 : 661334 : mptcp_cleanup_rbuf(msk, copied);
2182 : :
2183 : 661334 : out_err:
2184 [ + + + - ]: 661334 : if (cmsg_flags && copied >= 0) {
2185 [ + + ]: 582 : if (cmsg_flags & MPTCP_CMSG_TS)
2186 : 574 : tcp_recv_timestamp(msg, sk, &tss);
2187 : :
2188 [ + + ]: 582 : if (cmsg_flags & MPTCP_CMSG_INQ) {
2189 : 432 : unsigned int inq = mptcp_inq_hint(sk);
2190 : :
2191 : 432 : put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2192 : : }
2193 : : }
2194 : :
2195 [ - + ]: 661334 : pr_debug("msk=%p rx queue empty=%d copied=%d\n",
2196 : : msk, skb_queue_empty(&sk->sk_receive_queue), copied);
2197 : :
2198 : 661334 : release_sock(sk);
2199 : 661334 : return copied;
2200 : : }
2201 : :
2202 : 11617 : static void mptcp_retransmit_timer(struct timer_list *t)
2203 : : {
2204 : 11617 : struct inet_connection_sock *icsk = from_timer(icsk, t,
2205 : : icsk_retransmit_timer);
2206 : 11617 : struct sock *sk = &icsk->icsk_inet.sk;
2207 [ - + ]: 11617 : struct mptcp_sock *msk = mptcp_sk(sk);
2208 : :
2209 : 11617 : bh_lock_sock(sk);
2210 [ + + ]: 11617 : if (!sock_owned_by_user(sk)) {
2211 : : /* we need a process context to retransmit */
2212 [ + + ]: 12589 : if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2213 : 8145 : mptcp_schedule_work(sk);
2214 : : } else {
2215 : : /* delegate our work to tcp_release_cb() */
2216 [ - + - - : 3468 : __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
- - ]
2217 : : }
2218 : 11617 : bh_unlock_sock(sk);
2219 : 11617 : sock_put(sk);
2220 : 11617 : }
2221 : :
2222 : 114 : static void mptcp_tout_timer(struct timer_list *t)
2223 : : {
2224 : 114 : struct sock *sk = from_timer(sk, t, sk_timer);
2225 : :
2226 : 114 : mptcp_schedule_work(sk);
2227 : 114 : sock_put(sk);
2228 : 114 : }
2229 : :
2230 : : /* Find an idle subflow. Return NULL if there is unacked data at tcp
2231 : : * level.
2232 : : *
2233 : : * A backup subflow is returned only if that is the only kind available.
2234 : : */
2235 : 1953 : struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2236 : : {
2237 : 1953 : struct sock *backup = NULL, *pick = NULL;
2238 : 1953 : struct mptcp_subflow_context *subflow;
2239 : 1953 : int min_stale_count = INT_MAX;
2240 : :
2241 [ + + ]: 4636 : mptcp_for_each_subflow(msk, subflow) {
2242 : 2683 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2243 : :
2244 [ + + ]: 2683 : if (!__mptcp_subflow_active(subflow))
2245 : 105 : continue;
2246 : :
2247 : : /* still data outstanding at TCP level? skip this */
2248 [ + + ]: 2578 : if (!tcp_rtx_and_write_queues_empty(ssk)) {
2249 : 558 : mptcp_pm_subflow_chk_stale(msk, ssk);
2250 : 558 : min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2251 : 558 : continue;
2252 : : }
2253 : :
2254 [ + + ]: 2020 : if (subflow->backup || subflow->request_bkup) {
2255 [ + + ]: 82 : if (!backup)
2256 : 65 : backup = ssk;
2257 : 82 : continue;
2258 : : }
2259 : :
2260 [ + + ]: 1938 : if (!pick)
2261 : 1469 : pick = ssk;
2262 : : }
2263 : :
2264 [ + + ]: 1953 : if (pick)
2265 : : return pick;
2266 : :
2267 : : /* use backup only if there are no progresses anywhere */
2268 [ + + ]: 484 : return min_stale_count > 1 ? backup : NULL;
2269 : : }
2270 : :
2271 : 767 : bool __mptcp_retransmit_pending_data(struct sock *sk)
2272 : : {
2273 : 767 : struct mptcp_data_frag *cur, *rtx_head;
2274 [ - + ]: 767 : struct mptcp_sock *msk = mptcp_sk(sk);
2275 : :
2276 [ + + ]: 767 : if (__mptcp_check_fallback(msk))
2277 : : return false;
2278 : :
2279 : : /* the closing socket has some data untransmitted and/or unacked:
2280 : : * some data in the mptcp rtx queue has not really xmitted yet.
2281 : : * keep it simple and re-inject the whole mptcp level rtx queue
2282 : : */
2283 : 730 : mptcp_data_lock(sk);
2284 : 730 : __mptcp_clean_una_wakeup(sk);
2285 : 730 : rtx_head = mptcp_rtx_head(sk);
2286 [ + + ]: 730 : if (!rtx_head) {
2287 : 689 : mptcp_data_unlock(sk);
2288 : 689 : return false;
2289 : : }
2290 : :
2291 : 41 : msk->recovery_snd_nxt = msk->snd_nxt;
2292 : 41 : msk->recovery = true;
2293 : 41 : mptcp_data_unlock(sk);
2294 : :
2295 : 41 : msk->first_pending = rtx_head;
2296 : 41 : msk->snd_burst = 0;
2297 : :
2298 : : /* be sure to clear the "sent status" on all re-injected fragments */
2299 [ + + ]: 3873 : list_for_each_entry(cur, &msk->rtx_queue, list) {
2300 [ + + ]: 3837 : if (!cur->already_sent)
2301 : : break;
2302 : 3832 : cur->already_sent = 0;
2303 : : }
2304 : :
2305 : : return true;
2306 : : }
2307 : :
2308 : : /* flags for __mptcp_close_ssk() */
2309 : : #define MPTCP_CF_PUSH BIT(1)
2310 : : #define MPTCP_CF_FASTCLOSE BIT(2)
2311 : :
2312 : : /* be sure to send a reset only if the caller asked for it, also
2313 : : * clean completely the subflow status when the subflow reaches
2314 : : * TCP_CLOSE state
2315 : : */
2316 : 824 : static void __mptcp_subflow_disconnect(struct sock *ssk,
2317 : : struct mptcp_subflow_context *subflow,
2318 : : unsigned int flags)
2319 : : {
2320 [ + + + + : 824 : if (((1 << ssk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
+ + ]
2321 : : (flags & MPTCP_CF_FASTCLOSE)) {
2322 : : /* The MPTCP code never wait on the subflow sockets, TCP-level
2323 : : * disconnect should never fail
2324 : : */
2325 [ - + ]: 800 : WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2326 : 800 : mptcp_subflow_ctx_reset(subflow);
2327 : : } else {
2328 : 24 : tcp_shutdown(ssk, SEND_SHUTDOWN);
2329 : : }
2330 : 824 : }
2331 : :
2332 : : /* subflow sockets can be either outgoing (connect) or incoming
2333 : : * (accept).
2334 : : *
2335 : : * Outgoing subflows use in-kernel sockets.
2336 : : * Incoming subflows do not have their own 'struct socket' allocated,
2337 : : * so we need to use tcp_close() after detaching them from the mptcp
2338 : : * parent socket.
2339 : : */
2340 : 5455 : static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2341 : : struct mptcp_subflow_context *subflow,
2342 : : unsigned int flags)
2343 : : {
2344 [ - + ]: 5455 : struct mptcp_sock *msk = mptcp_sk(sk);
2345 : 5455 : bool dispose_it, need_push = false;
2346 : :
2347 : : /* If the first subflow moved to a close state before accept, e.g. due
2348 : : * to an incoming reset or listener shutdown, the subflow socket is
2349 : : * already deleted by inet_child_forget() and the mptcp socket can't
2350 : : * survive too.
2351 : : */
2352 [ + + + + : 5497 : if (msk->in_accept_queue && msk->first == ssk &&
- + ]
2353 [ - - ]: 42 : (sock_flag(sk, SOCK_DEAD) || sock_flag(ssk, SOCK_DEAD))) {
2354 : : /* ensure later check in mptcp_worker() will dispose the msk */
2355 : 42 : sock_set_flag(sk, SOCK_DEAD);
2356 : 42 : mptcp_set_close_tout(sk, tcp_jiffies32 - (mptcp_close_timeout(sk) + 1));
2357 : 42 : lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2358 : 42 : mptcp_subflow_drop_ctx(ssk);
2359 : 42 : goto out_release;
2360 : : }
2361 : :
2362 [ + + + + ]: 5413 : dispose_it = msk->free_first || ssk != msk->first;
2363 : : if (dispose_it)
2364 : 4589 : list_del(&subflow->node);
2365 : :
2366 : 5413 : lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2367 : :
2368 [ + + + + ]: 5413 : if ((flags & MPTCP_CF_FASTCLOSE) && !__mptcp_check_fallback(msk)) {
2369 : : /* be sure to force the tcp_close path
2370 : : * to generate the egress reset
2371 : : */
2372 : 442 : ssk->sk_lingertime = 0;
2373 : 442 : sock_set_flag(ssk, SOCK_LINGER);
2374 : 442 : subflow->send_fastclose = 1;
2375 : : }
2376 : :
2377 [ + + + + ]: 5413 : need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2378 [ + + ]: 5413 : if (!dispose_it) {
2379 : 824 : __mptcp_subflow_disconnect(ssk, subflow, flags);
2380 : 824 : release_sock(ssk);
2381 : :
2382 : 824 : goto out;
2383 : : }
2384 : :
2385 : 4589 : subflow->disposable = 1;
2386 : :
2387 : : /* if ssk hit tcp_done(), tcp_cleanup_ulp() cleared the related ops
2388 : : * the ssk has been already destroyed, we just need to release the
2389 : : * reference owned by msk;
2390 : : */
2391 [ - + ]: 4589 : if (!inet_csk(ssk)->icsk_ulp_ops) {
2392 [ # # ]: 0 : WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
2393 [ # # ]: 0 : kfree_rcu(subflow, rcu);
2394 : : } else {
2395 : : /* otherwise tcp will dispose of the ssk and subflow ctx */
2396 : 4589 : __tcp_close(ssk, 0);
2397 : :
2398 : : /* close acquired an extra ref */
2399 : 4589 : __sock_put(ssk);
2400 : : }
2401 : :
2402 : 4631 : out_release:
2403 : 4631 : __mptcp_subflow_error_report(sk, ssk);
2404 : 4631 : release_sock(ssk);
2405 : :
2406 : 4631 : sock_put(ssk);
2407 : :
2408 [ + + ]: 4631 : if (ssk == msk->first)
2409 : 3602 : WRITE_ONCE(msk->first, NULL);
2410 : :
2411 : 1029 : out:
2412 : 5455 : __mptcp_sync_sndbuf(sk);
2413 [ + + ]: 5455 : if (need_push)
2414 : 27 : __mptcp_push_pending(sk, 0);
2415 : :
2416 : : /* Catch every 'all subflows closed' scenario, including peers silently
2417 : : * closing them, e.g. due to timeout.
2418 : : * For established sockets, allow an additional timeout before closing,
2419 : : * as the protocol can still create more subflows.
2420 : : */
2421 [ + + + + : 7721 : if (list_is_singular(&msk->conn_list) && msk->first &&
+ + ]
2422 [ + - ]: 30 : inet_sk_state_load(msk->first) == TCP_CLOSE) {
2423 [ + + + - ]: 802 : if (sk->sk_state != TCP_ESTABLISHED ||
2424 [ - + ]: 32 : msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
2425 : 770 : mptcp_set_state(sk, TCP_CLOSE);
2426 : 770 : mptcp_close_wake_up(sk);
2427 : : } else {
2428 : 32 : mptcp_start_tout_timer(sk);
2429 : : }
2430 : : }
2431 : 5455 : }
2432 : :
2433 : 791 : void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2434 : : struct mptcp_subflow_context *subflow)
2435 : : {
2436 : : /* The first subflow can already be closed and still in the list */
2437 [ + + ]: 791 : if (subflow->close_event_done)
2438 : : return;
2439 : :
2440 : 753 : subflow->close_event_done = true;
2441 : :
2442 [ + + ]: 753 : if (sk->sk_state == TCP_ESTABLISHED)
2443 [ - + ]: 322 : mptcp_event(MPTCP_EVENT_SUB_CLOSED, mptcp_sk(sk), ssk, GFP_KERNEL);
2444 : :
2445 : : /* subflow aborted before reaching the fully_established status
2446 : : * attempt the creation of the next subflow
2447 : : */
2448 [ - + ]: 753 : mptcp_pm_subflow_check_next(mptcp_sk(sk), subflow);
2449 : :
2450 : 753 : __mptcp_close_ssk(sk, ssk, subflow, MPTCP_CF_PUSH);
2451 : : }
2452 : :
2453 : 0 : static unsigned int mptcp_sync_mss(struct sock *sk, u32 pmtu)
2454 : : {
2455 : 0 : return 0;
2456 : : }
2457 : :
2458 : 595 : static void __mptcp_close_subflow(struct sock *sk)
2459 : : {
2460 : 595 : struct mptcp_subflow_context *subflow, *tmp;
2461 [ - + ]: 595 : struct mptcp_sock *msk = mptcp_sk(sk);
2462 : :
2463 : 595 : might_sleep();
2464 : :
2465 [ + + ]: 1511 : mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2466 : 916 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2467 : 916 : int ssk_state = inet_sk_state_load(ssk);
2468 : :
2469 [ + + + + ]: 916 : if (ssk_state != TCP_CLOSE &&
2470 [ + + ]: 76 : (ssk_state != TCP_CLOSE_WAIT ||
2471 [ # # ]: 0 : inet_sk_state_load(sk) != TCP_ESTABLISHED))
2472 : 317 : continue;
2473 : :
2474 : : /* 'subflow_data_ready' will re-sched once rx queue is empty */
2475 [ - + ]: 599 : if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2476 : 0 : continue;
2477 : :
2478 : 599 : mptcp_close_ssk(sk, ssk, subflow);
2479 : : }
2480 : :
2481 : 595 : }
2482 : :
2483 : 13337 : static bool mptcp_close_tout_expired(const struct sock *sk)
2484 : : {
2485 [ + + ]: 13337 : if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
2486 [ + + ]: 2710 : sk->sk_state == TCP_CLOSE)
2487 : : return false;
2488 : :
2489 : 1834 : return time_after32(tcp_jiffies32,
2490 : : inet_csk(sk)->icsk_mtup.probe_timestamp + mptcp_close_timeout(sk));
2491 : : }
2492 : :
2493 : 13337 : static void mptcp_check_fastclose(struct mptcp_sock *msk)
2494 : : {
2495 : 13337 : struct mptcp_subflow_context *subflow, *tmp;
2496 : 13337 : struct sock *sk = (struct sock *)msk;
2497 : :
2498 [ + + + + ]: 13337 : if (likely(!READ_ONCE(msk->rcv_fastclose)))
2499 : : return;
2500 : :
2501 : 229 : mptcp_token_destroy(msk);
2502 : :
2503 [ + + ]: 524 : mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2504 : 295 : struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2505 : 295 : bool slow;
2506 : :
2507 : 295 : slow = lock_sock_fast(tcp_sk);
2508 [ + + ]: 295 : if (tcp_sk->sk_state != TCP_CLOSE) {
2509 : 39 : mptcp_send_active_reset_reason(tcp_sk);
2510 : 39 : tcp_set_state(tcp_sk, TCP_CLOSE);
2511 : : }
2512 : 295 : unlock_sock_fast(tcp_sk, slow);
2513 : : }
2514 : :
2515 : : /* Mirror the tcp_reset() error propagation */
2516 [ - + + - ]: 229 : switch (sk->sk_state) {
2517 : : case TCP_SYN_SENT:
2518 : 0 : WRITE_ONCE(sk->sk_err, ECONNREFUSED);
2519 : 0 : break;
2520 : : case TCP_CLOSE_WAIT:
2521 : 16 : WRITE_ONCE(sk->sk_err, EPIPE);
2522 : 16 : break;
2523 : : case TCP_CLOSE:
2524 : : return;
2525 : : default:
2526 : 213 : WRITE_ONCE(sk->sk_err, ECONNRESET);
2527 : : }
2528 : :
2529 : 229 : mptcp_set_state(sk, TCP_CLOSE);
2530 : 229 : WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2531 : 229 : smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2532 : 229 : set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2533 : :
2534 : : /* the calling mptcp_worker will properly destroy the socket */
2535 [ + + ]: 229 : if (sock_flag(sk, SOCK_DEAD))
2536 : : return;
2537 : :
2538 : 107 : sk->sk_state_change(sk);
2539 : 107 : sk_error_report(sk);
2540 : : }
2541 : :
2542 : 11297 : static void __mptcp_retrans(struct sock *sk)
2543 : : {
2544 [ - + ]: 11297 : struct mptcp_sock *msk = mptcp_sk(sk);
2545 : 11297 : struct mptcp_subflow_context *subflow;
2546 : 11297 : struct mptcp_sendmsg_info info = {};
2547 : 11297 : struct mptcp_data_frag *dfrag;
2548 : 11297 : struct sock *ssk;
2549 : 11297 : int ret, err;
2550 : 11297 : u16 len = 0;
2551 : :
2552 : 11297 : mptcp_clean_una_wakeup(sk);
2553 : :
2554 : : /* first check ssk: need to kick "stale" logic */
2555 : 11297 : err = mptcp_sched_get_retrans(msk);
2556 : 11297 : dfrag = mptcp_rtx_head(sk);
2557 [ + + ]: 11297 : if (!dfrag) {
2558 [ + + ]: 3445 : if (mptcp_data_fin_enabled(msk)) {
2559 : 1151 : struct inet_connection_sock *icsk = inet_csk(sk);
2560 : :
2561 : 1151 : icsk->icsk_retransmits++;
2562 : 1151 : mptcp_set_datafin_timeout(sk);
2563 : 1151 : mptcp_send_ack(msk);
2564 : :
2565 : 1151 : goto reset_timer;
2566 : : }
2567 : :
2568 [ + + ]: 2294 : if (!mptcp_send_head(sk))
2569 : 960 : return;
2570 : :
2571 : 1334 : goto reset_timer;
2572 : : }
2573 : :
2574 [ + + ]: 7852 : if (err)
2575 : 4347 : goto reset_timer;
2576 : :
2577 [ + + ]: 10165 : mptcp_for_each_subflow(msk, subflow) {
2578 [ + + + + ]: 6660 : if (READ_ONCE(subflow->scheduled)) {
2579 : 3867 : u16 copied = 0;
2580 : :
2581 : 3867 : mptcp_subflow_set_scheduled(subflow, false);
2582 : :
2583 : 3867 : ssk = mptcp_subflow_tcp_sock(subflow);
2584 : :
2585 : 3867 : lock_sock(ssk);
2586 : :
2587 : : /* limit retransmission to the bytes already sent on some subflows */
2588 : 3867 : info.sent = 0;
2589 [ - + - + ]: 3867 : info.limit = READ_ONCE(msk->csum_enabled) ? dfrag->data_len :
2590 : : dfrag->already_sent;
2591 [ + + ]: 7738 : while (info.sent < info.limit) {
2592 : 3871 : ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2593 [ + - ]: 3871 : if (ret <= 0)
2594 : : break;
2595 : :
2596 [ + - ]: 3871 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2597 : 3871 : copied += ret;
2598 : 3871 : info.sent += ret;
2599 : : }
2600 [ + - ]: 3867 : if (copied) {
2601 : 3867 : len = max(copied, len);
2602 [ - + ]: 3867 : tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2603 : : info.size_goal);
2604 : 3867 : WRITE_ONCE(msk->allow_infinite_fallback, false);
2605 : : }
2606 : :
2607 : 3867 : release_sock(ssk);
2608 : : }
2609 : : }
2610 : :
2611 : 3505 : msk->bytes_retrans += len;
2612 : 3505 : dfrag->already_sent = max(dfrag->already_sent, len);
2613 : :
2614 : 10337 : reset_timer:
2615 : 10337 : mptcp_check_and_set_pending(sk);
2616 : :
2617 [ + + ]: 10337 : if (!mptcp_rtx_timer_pending(sk))
2618 : 86 : mptcp_reset_rtx_timer(sk);
2619 : : }
2620 : :
2621 : : /* schedule the timeout timer for the relevant event: either close timeout
2622 : : * or mp_fail timeout. The close timeout takes precedence on the mp_fail one
2623 : : */
2624 : 1074 : void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
2625 : : {
2626 : 1074 : struct sock *sk = (struct sock *)msk;
2627 : 1074 : unsigned long timeout, close_timeout;
2628 : :
2629 [ + + + - ]: 1074 : if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
2630 : : return;
2631 : :
2632 : 2148 : close_timeout = (unsigned long)inet_csk(sk)->icsk_mtup.probe_timestamp -
2633 : 1074 : tcp_jiffies32 + jiffies + mptcp_close_timeout(sk);
2634 : :
2635 : : /* the close timeout takes precedence on the fail one, and here at least one of
2636 : : * them is active
2637 : : */
2638 [ + + ]: 1074 : timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
2639 : :
2640 : 1074 : sk_reset_timer(sk, &sk->sk_timer, timeout);
2641 : : }
2642 : :
2643 : 0 : static void mptcp_mp_fail_no_response(struct mptcp_sock *msk)
2644 : : {
2645 : 0 : struct sock *ssk = msk->first;
2646 : 0 : bool slow;
2647 : :
2648 [ # # ]: 0 : if (!ssk)
2649 : : return;
2650 : :
2651 [ # # ]: 0 : pr_debug("MP_FAIL doesn't respond, reset the subflow\n");
2652 : :
2653 : 0 : slow = lock_sock_fast(ssk);
2654 : 0 : mptcp_subflow_reset(ssk);
2655 : 0 : WRITE_ONCE(mptcp_subflow_ctx(ssk)->fail_tout, 0);
2656 : 0 : unlock_sock_fast(ssk, slow);
2657 : : }
2658 : :
2659 : 387 : static void mptcp_do_fastclose(struct sock *sk)
2660 : : {
2661 : 387 : struct mptcp_subflow_context *subflow, *tmp;
2662 [ - + ]: 387 : struct mptcp_sock *msk = mptcp_sk(sk);
2663 : :
2664 : 387 : mptcp_set_state(sk, TCP_CLOSE);
2665 [ + + ]: 853 : mptcp_for_each_subflow_safe(msk, subflow, tmp)
2666 : 466 : __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow),
2667 : : subflow, MPTCP_CF_FASTCLOSE);
2668 : 387 : }
2669 : :
2670 : 13458 : static void mptcp_worker(struct work_struct *work)
2671 : : {
2672 : 13458 : struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2673 : 13458 : struct sock *sk = (struct sock *)msk;
2674 : 13458 : unsigned long fail_tout;
2675 : 13458 : int state;
2676 : :
2677 : 13458 : lock_sock(sk);
2678 : 13458 : state = sk->sk_state;
2679 [ + + + + ]: 13458 : if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
2680 : 121 : goto unlock;
2681 : :
2682 : 13337 : mptcp_check_fastclose(msk);
2683 : :
2684 : 13337 : mptcp_pm_nl_work(msk);
2685 : :
2686 : 13337 : mptcp_check_send_data_fin(sk);
2687 : 13337 : mptcp_check_data_fin_ack(sk);
2688 : 13337 : mptcp_check_data_fin(sk);
2689 : :
2690 [ + + ]: 20330 : if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2691 : 595 : __mptcp_close_subflow(sk);
2692 : :
2693 [ + + ]: 13337 : if (mptcp_close_tout_expired(sk)) {
2694 : 114 : mptcp_do_fastclose(sk);
2695 : 114 : mptcp_close_wake_up(sk);
2696 : : }
2697 : :
2698 [ + + + + ]: 13337 : if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
2699 : 978 : __mptcp_destroy_sock(sk);
2700 : 978 : goto unlock;
2701 : : }
2702 : :
2703 [ + + ]: 18838 : if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2704 : 7830 : __mptcp_retrans(sk);
2705 : :
2706 [ + - + - ]: 12359 : fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2707 [ + - - - ]: 12359 : if (fail_tout && time_after(jiffies, fail_tout))
2708 : 0 : mptcp_mp_fail_no_response(msk);
2709 : :
2710 : 0 : unlock:
2711 : 13458 : release_sock(sk);
2712 : 13458 : sock_put(sk);
2713 : 13458 : }
2714 : :
2715 : 3696 : static void __mptcp_init_sock(struct sock *sk)
2716 : : {
2717 [ - + ]: 3696 : struct mptcp_sock *msk = mptcp_sk(sk);
2718 : :
2719 : 3696 : INIT_LIST_HEAD(&msk->conn_list);
2720 : 3696 : INIT_LIST_HEAD(&msk->join_list);
2721 : 3696 : INIT_LIST_HEAD(&msk->rtx_queue);
2722 : 3696 : INIT_WORK(&msk->work, mptcp_worker);
2723 : 3696 : msk->out_of_order_queue = RB_ROOT;
2724 : 3696 : msk->first_pending = NULL;
2725 : 3696 : msk->timer_ival = TCP_RTO_MIN;
2726 : 3696 : msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
2727 : :
2728 : 3696 : WRITE_ONCE(msk->first, NULL);
2729 : 3696 : inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2730 : 3696 : WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2731 : 3696 : WRITE_ONCE(msk->allow_infinite_fallback, true);
2732 : 3696 : msk->recovery = false;
2733 : 3696 : msk->subflow_id = 1;
2734 : 3696 : msk->last_data_sent = tcp_jiffies32;
2735 : 3696 : msk->last_data_recv = tcp_jiffies32;
2736 : 3696 : msk->last_ack_recv = tcp_jiffies32;
2737 : :
2738 : 3696 : mptcp_pm_data_init(msk);
2739 : :
2740 : : /* re-use the csk retrans timer for MPTCP-level retrans */
2741 : 3696 : timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2742 : 3696 : timer_setup(&sk->sk_timer, mptcp_tout_timer, 0);
2743 : 3696 : }
2744 : :
2745 : 2588 : static void mptcp_ca_reset(struct sock *sk)
2746 : : {
2747 : 2588 : struct inet_connection_sock *icsk = inet_csk(sk);
2748 : :
2749 : 2588 : tcp_assign_congestion_control(sk);
2750 [ - + ]: 2588 : strscpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name,
2751 : : sizeof(mptcp_sk(sk)->ca_name));
2752 : :
2753 : : /* no need to keep a reference to the ops, the name will suffice */
2754 : 2588 : tcp_cleanup_congestion_control(sk);
2755 : 2588 : icsk->icsk_ca_ops = NULL;
2756 : 2588 : }
2757 : :
2758 : 2534 : static int mptcp_init_sock(struct sock *sk)
2759 : : {
2760 : 2534 : struct net *net = sock_net(sk);
2761 : 2534 : int ret;
2762 : :
2763 : 2534 : __mptcp_init_sock(sk);
2764 : :
2765 [ + + ]: 2534 : if (!mptcp_is_enabled(net))
2766 : : return -ENOPROTOOPT;
2767 : :
2768 [ + + + - ]: 2530 : if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2769 : : return -ENOMEM;
2770 : :
2771 : 2530 : rcu_read_lock();
2772 [ - + ]: 2530 : ret = mptcp_init_sched(mptcp_sk(sk),
2773 : : mptcp_sched_find(mptcp_get_scheduler(net)));
2774 : 2530 : rcu_read_unlock();
2775 [ + - ]: 2530 : if (ret)
2776 : : return ret;
2777 : :
2778 : 2530 : set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2779 : :
2780 : : /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2781 : : * propagate the correct value
2782 : : */
2783 : 2530 : mptcp_ca_reset(sk);
2784 : :
2785 : 2530 : sk_sockets_allocated_inc(sk);
2786 : 2530 : sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2787 : 2530 : sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2788 : :
2789 : 2530 : return 0;
2790 : : }
2791 : :
2792 : 3676 : static void __mptcp_clear_xmit(struct sock *sk)
2793 : : {
2794 [ - + ]: 3676 : struct mptcp_sock *msk = mptcp_sk(sk);
2795 : 3676 : struct mptcp_data_frag *dtmp, *dfrag;
2796 : :
2797 : 3676 : WRITE_ONCE(msk->first_pending, NULL);
2798 [ + + ]: 3906 : list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2799 : 230 : dfrag_clear(sk, dfrag);
2800 : 3676 : }
2801 : :
2802 : 2636 : void mptcp_cancel_work(struct sock *sk)
2803 : : {
2804 [ - + ]: 2636 : struct mptcp_sock *msk = mptcp_sk(sk);
2805 : :
2806 [ + + ]: 2636 : if (cancel_work_sync(&msk->work))
2807 : 29 : __sock_put(sk);
2808 : 2636 : }
2809 : :
2810 : 3004 : void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2811 : : {
2812 : 3004 : lock_sock(ssk);
2813 : :
2814 [ - + + ]: 3004 : switch (ssk->sk_state) {
2815 : 0 : case TCP_LISTEN:
2816 [ # # ]: 0 : if (!(how & RCV_SHUTDOWN))
2817 : : break;
2818 : 16 : fallthrough;
2819 : : case TCP_SYN_SENT:
2820 [ - + ]: 16 : WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2821 : : break;
2822 : 2988 : default:
2823 [ - + + + ]: 2988 : if (__mptcp_check_fallback(mptcp_sk(sk))) {
2824 [ - + ]: 93 : pr_debug("Fallback\n");
2825 : 93 : ssk->sk_shutdown |= how;
2826 : 93 : tcp_shutdown(ssk, how);
2827 : :
2828 : : /* simulate the data_fin ack reception to let the state
2829 : : * machine move forward
2830 : : */
2831 [ - + - + ]: 93 : WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2832 : 93 : mptcp_schedule_work(sk);
2833 : : } else {
2834 [ - + ]: 2895 : pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
2835 : 2895 : tcp_send_ack(ssk);
2836 [ + + ]: 2895 : if (!mptcp_rtx_timer_pending(sk))
2837 : 1627 : mptcp_reset_rtx_timer(sk);
2838 : : }
2839 : : break;
2840 : : }
2841 : :
2842 : 3004 : release_sock(ssk);
2843 : 3004 : }
2844 : :
2845 : 17040 : void mptcp_set_state(struct sock *sk, int state)
2846 : : {
2847 : 17707 : int oldstate = sk->sk_state;
2848 : :
2849 [ + + - ]: 16987 : switch (state) {
2850 : 2376 : case TCP_ESTABLISHED:
2851 [ + - ]: 2376 : if (oldstate != TCP_ESTABLISHED)
2852 [ + - ]: 2376 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
2853 : : break;
2854 : : case TCP_CLOSE_WAIT:
2855 : : /* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
2856 : : * MPTCP "accepted" sockets will be created later on. So no
2857 : : * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
2858 : : */
2859 : : break;
2860 : 14611 : default:
2861 [ + + ]: 14611 : if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
2862 [ + - ]: 2376 : MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
2863 : : }
2864 : :
2865 : 17707 : inet_sk_state_store(sk, state);
2866 : 720 : }
2867 : :
2868 : : static const unsigned char new_state[16] = {
2869 : : /* current state: new state: action: */
2870 : : [0 /* (Invalid) */] = TCP_CLOSE,
2871 : : [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2872 : : [TCP_SYN_SENT] = TCP_CLOSE,
2873 : : [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
2874 : : [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
2875 : : [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
2876 : : [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
2877 : : [TCP_CLOSE] = TCP_CLOSE,
2878 : : [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
2879 : : [TCP_LAST_ACK] = TCP_LAST_ACK,
2880 : : [TCP_LISTEN] = TCP_CLOSE,
2881 : : [TCP_CLOSING] = TCP_CLOSING,
2882 : : [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
2883 : : };
2884 : :
2885 : 2579 : static int mptcp_close_state(struct sock *sk)
2886 : : {
2887 : 2579 : int next = (int)new_state[sk->sk_state];
2888 : 2579 : int ns = next & TCP_STATE_MASK;
2889 : :
2890 : 2579 : mptcp_set_state(sk, ns);
2891 : :
2892 : 2579 : return next & TCP_ACTION_FIN;
2893 : : }
2894 : :
2895 : 381384 : static void mptcp_check_send_data_fin(struct sock *sk)
2896 : : {
2897 : 381384 : struct mptcp_subflow_context *subflow;
2898 [ - + ]: 381384 : struct mptcp_sock *msk = mptcp_sk(sk);
2899 : :
2900 [ - + - - ]: 381384 : pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
2901 : : msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
2902 : : msk->snd_nxt, msk->write_seq);
2903 : :
2904 : : /* we still need to enqueue subflows or not really shutting down,
2905 : : * skip this
2906 : : */
2907 [ + + + + : 383532 : if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
+ + - + ]
2908 : 2148 : mptcp_send_head(sk))
2909 : 379236 : return;
2910 : :
2911 : 2148 : WRITE_ONCE(msk->snd_nxt, msk->write_seq);
2912 : :
2913 [ + + ]: 4960 : mptcp_for_each_subflow(msk, subflow) {
2914 : 2812 : struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2915 : :
2916 : 2812 : mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
2917 : : }
2918 : : }
2919 : :
2920 : 2148 : static void __mptcp_wr_shutdown(struct sock *sk)
2921 : : {
2922 [ - + ]: 2148 : struct mptcp_sock *msk = mptcp_sk(sk);
2923 : :
2924 [ - + - - ]: 2148 : pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
2925 : : msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
2926 : : !!mptcp_send_head(sk));
2927 : :
2928 : : /* will be ignored by fallback sockets */
2929 : 2148 : WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
2930 : 2148 : WRITE_ONCE(msk->snd_data_fin_enable, 1);
2931 : :
2932 : 2148 : mptcp_check_send_data_fin(sk);
2933 : 2148 : }
2934 : :
2935 : 3614 : static void __mptcp_destroy_sock(struct sock *sk)
2936 : : {
2937 [ - + ]: 3614 : struct mptcp_sock *msk = mptcp_sk(sk);
2938 : :
2939 [ - + ]: 3614 : pr_debug("msk=%p\n", msk);
2940 : :
2941 : 3614 : might_sleep();
2942 : :
2943 : 3614 : mptcp_stop_rtx_timer(sk);
2944 : 3614 : sk_stop_timer(sk, &sk->sk_timer);
2945 : 3614 : msk->pm.status = 0;
2946 : 3614 : mptcp_release_sched(msk);
2947 : :
2948 : 3614 : sk->sk_prot->destroy(sk);
2949 : :
2950 : 3614 : sk_stream_kill_queues(sk);
2951 : 3614 : xfrm_sk_free_policy(sk);
2952 : :
2953 : 3614 : sock_put(sk);
2954 : 3614 : }
2955 : :
2956 : 42 : void __mptcp_unaccepted_force_close(struct sock *sk)
2957 : : {
2958 : 42 : sock_set_flag(sk, SOCK_DEAD);
2959 : 42 : mptcp_do_fastclose(sk);
2960 : 42 : __mptcp_destroy_sock(sk);
2961 : 42 : }
2962 : :
2963 : 0 : static __poll_t mptcp_check_readable(struct sock *sk)
2964 : : {
2965 [ + + ]: 585636 : return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
2966 : : }
2967 : :
2968 : 2369 : static void mptcp_check_listen_stop(struct sock *sk)
2969 : : {
2970 : 2369 : struct sock *ssk;
2971 : :
2972 [ + + ]: 2369 : if (inet_sk_state_load(sk) != TCP_LISTEN)
2973 : : return;
2974 : :
2975 : 1246 : sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
2976 [ - + ]: 1246 : ssk = mptcp_sk(sk)->first;
2977 [ + - - + ]: 2492 : if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
2978 : 0 : return;
2979 : :
2980 : 1246 : lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2981 : 1246 : tcp_set_state(ssk, TCP_CLOSE);
2982 : 1246 : mptcp_subflow_queue_clean(sk, ssk);
2983 : 1246 : inet_csk_listen_stop(ssk);
2984 : 1246 : mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
2985 : 1246 : release_sock(ssk);
2986 : : }
2987 : :
2988 : 3634 : bool __mptcp_close(struct sock *sk, long timeout)
2989 : : {
2990 : 3634 : struct mptcp_subflow_context *subflow;
2991 [ - + ]: 3634 : struct mptcp_sock *msk = mptcp_sk(sk);
2992 : 3634 : bool do_cancel_work = false;
2993 : 3634 : int subflows_alive = 0;
2994 : :
2995 : 3634 : WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2996 : :
2997 [ + + + + ]: 3634 : if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2998 : 2311 : mptcp_check_listen_stop(sk);
2999 : 2311 : mptcp_set_state(sk, TCP_CLOSE);
3000 : 2311 : goto cleanup;
3001 : : }
3002 : :
3003 [ + + - + ]: 1323 : if (mptcp_data_avail(msk) || timeout < 0) {
3004 : : /* If the msk has read data, or the caller explicitly ask it,
3005 : : * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
3006 : : */
3007 : 231 : mptcp_do_fastclose(sk);
3008 : 231 : timeout = 0;
3009 [ + + ]: 1092 : } else if (mptcp_close_state(sk)) {
3010 : 753 : __mptcp_wr_shutdown(sk);
3011 : : }
3012 : :
3013 : 1323 : sk_stream_wait_close(sk, timeout);
3014 : :
3015 : 3634 : cleanup:
3016 : : /* orphan all the subflows */
3017 [ + + ]: 7920 : mptcp_for_each_subflow(msk, subflow) {
3018 : 4286 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3019 : 4286 : bool slow = lock_sock_fast_nested(ssk);
3020 : :
3021 : 4286 : subflows_alive += ssk->sk_state != TCP_CLOSE;
3022 : :
3023 : : /* since the close timeout takes precedence on the fail one,
3024 : : * cancel the latter
3025 : : */
3026 [ + + ]: 4286 : if (ssk == msk->first)
3027 : 3622 : subflow->fail_tout = 0;
3028 : :
3029 : : /* detach from the parent socket, but allow data_ready to
3030 : : * push incoming data into the mptcp stack, to properly ack it
3031 : : */
3032 : 4286 : ssk->sk_socket = NULL;
3033 : 4286 : ssk->sk_wq = NULL;
3034 : 4286 : unlock_sock_fast(ssk, slow);
3035 : : }
3036 : 3634 : sock_orphan(sk);
3037 : :
3038 : : /* all the subflows are closed, only timeout can change the msk
3039 : : * state, let's not keep resources busy for no reasons
3040 : : */
3041 [ + + ]: 3634 : if (subflows_alive == 0)
3042 : 1705 : mptcp_set_state(sk, TCP_CLOSE);
3043 : :
3044 : 3634 : sock_hold(sk);
3045 [ - + ]: 3634 : pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
3046 : 3634 : mptcp_pm_connection_closed(msk);
3047 : :
3048 [ + + ]: 3634 : if (sk->sk_state == TCP_CLOSE) {
3049 : 2594 : __mptcp_destroy_sock(sk);
3050 : 2594 : do_cancel_work = true;
3051 : : } else {
3052 : 1040 : mptcp_start_tout_timer(sk);
3053 : : }
3054 : :
3055 : 3634 : return do_cancel_work;
3056 : : }
3057 : :
3058 : 3634 : static void mptcp_close(struct sock *sk, long timeout)
3059 : : {
3060 : 3634 : bool do_cancel_work;
3061 : :
3062 : 3634 : lock_sock(sk);
3063 : :
3064 : 3634 : do_cancel_work = __mptcp_close(sk, timeout);
3065 : 3634 : release_sock(sk);
3066 [ + + ]: 3634 : if (do_cancel_work)
3067 : 2594 : mptcp_cancel_work(sk);
3068 : :
3069 : 3634 : sock_put(sk);
3070 : 3634 : }
3071 : :
3072 : 4924 : static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3073 : : {
3074 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3075 [ + - ]: 4924 : const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3076 [ + - ]: 4924 : struct ipv6_pinfo *msk6 = inet6_sk(msk);
3077 : :
3078 : 4924 : msk->sk_v6_daddr = ssk->sk_v6_daddr;
3079 : 4924 : msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3080 : :
3081 [ + + ]: 4924 : if (msk6 && ssk6) {
3082 : 1928 : msk6->saddr = ssk6->saddr;
3083 : 1928 : msk6->flow_label = ssk6->flow_label;
3084 : : }
3085 : : #endif
3086 : :
3087 : 4924 : inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3088 : 4924 : inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3089 : 4924 : inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3090 : 4924 : inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3091 : 4924 : inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3092 : 4924 : inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3093 : 4924 : }
3094 : :
3095 : 58 : static int mptcp_disconnect(struct sock *sk, int flags)
3096 : : {
3097 [ - + ]: 58 : struct mptcp_sock *msk = mptcp_sk(sk);
3098 : :
3099 : : /* We are on the fastopen error path. We can't call straight into the
3100 : : * subflows cleanup code due to lock nesting (we are already under
3101 : : * msk->firstsocket lock).
3102 : : */
3103 [ + - ]: 58 : if (msk->fastopening)
3104 : : return -EBUSY;
3105 : :
3106 : 58 : mptcp_check_listen_stop(sk);
3107 : 58 : mptcp_set_state(sk, TCP_CLOSE);
3108 : :
3109 : 58 : mptcp_stop_rtx_timer(sk);
3110 : 58 : mptcp_stop_tout_timer(sk);
3111 : :
3112 : 58 : mptcp_pm_connection_closed(msk);
3113 : :
3114 : : /* msk->subflow is still intact, the following will not free the first
3115 : : * subflow
3116 : : */
3117 : 58 : mptcp_destroy_common(msk, MPTCP_CF_FASTCLOSE);
3118 : 58 : WRITE_ONCE(msk->flags, 0);
3119 : 58 : msk->cb_flags = 0;
3120 : 58 : msk->recovery = false;
3121 : 58 : WRITE_ONCE(msk->can_ack, false);
3122 : 58 : WRITE_ONCE(msk->fully_established, false);
3123 : 58 : WRITE_ONCE(msk->rcv_data_fin, false);
3124 : 58 : WRITE_ONCE(msk->snd_data_fin_enable, false);
3125 : 58 : WRITE_ONCE(msk->rcv_fastclose, false);
3126 : 58 : WRITE_ONCE(msk->use_64bit_ack, false);
3127 : 58 : WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3128 : 58 : mptcp_pm_data_reset(msk);
3129 : 58 : mptcp_ca_reset(sk);
3130 : 58 : msk->bytes_consumed = 0;
3131 : 58 : msk->bytes_acked = 0;
3132 : 58 : msk->bytes_received = 0;
3133 : 58 : msk->bytes_sent = 0;
3134 : 58 : msk->bytes_retrans = 0;
3135 : 58 : msk->rcvspace_init = 0;
3136 : :
3137 : 58 : WRITE_ONCE(sk->sk_shutdown, 0);
3138 : 58 : sk_error_report(sk);
3139 : 58 : return 0;
3140 : : }
3141 : :
3142 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3143 : 4 : static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3144 : : {
3145 : 496 : unsigned int offset = sizeof(struct mptcp6_sock) - sizeof(struct ipv6_pinfo);
3146 : :
3147 : 496 : return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
3148 : : }
3149 : :
3150 : 496 : static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
3151 : : {
3152 [ + - ]: 496 : const struct ipv6_pinfo *np = inet6_sk(sk);
3153 : 496 : struct ipv6_txoptions *opt;
3154 : 496 : struct ipv6_pinfo *newnp;
3155 : :
3156 [ + - ]: 496 : newnp = inet6_sk(newsk);
3157 : :
3158 : 496 : rcu_read_lock();
3159 [ + - - + : 496 : opt = rcu_dereference(np->opt);
- - - - -
- ]
3160 [ + - ]: 496 : if (opt) {
3161 : 0 : opt = ipv6_dup_options(newsk, opt);
3162 [ # # ]: 0 : if (!opt)
3163 [ # # ]: 0 : net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
3164 : : }
3165 : 496 : RCU_INIT_POINTER(newnp->opt, opt);
3166 : 496 : rcu_read_unlock();
3167 : 496 : }
3168 : : #endif
3169 : :
3170 : 666 : static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
3171 : : {
3172 : 666 : struct ip_options_rcu *inet_opt, *newopt = NULL;
3173 : 666 : const struct inet_sock *inet = inet_sk(sk);
3174 : 666 : struct inet_sock *newinet;
3175 : :
3176 : 666 : newinet = inet_sk(newsk);
3177 : :
3178 : 666 : rcu_read_lock();
3179 [ + - - + : 666 : inet_opt = rcu_dereference(inet->inet_opt);
- - - - -
- ]
3180 [ + - ]: 666 : if (inet_opt) {
3181 : 0 : newopt = sock_kmalloc(newsk, sizeof(*inet_opt) +
3182 : 0 : inet_opt->opt.optlen, GFP_ATOMIC);
3183 [ # # ]: 0 : if (newopt)
3184 : 0 : memcpy(newopt, inet_opt, sizeof(*inet_opt) +
3185 : 0 : inet_opt->opt.optlen);
3186 : : else
3187 [ # # ]: 0 : net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
3188 : : }
3189 : 666 : RCU_INIT_POINTER(newinet->inet_opt, newopt);
3190 : 666 : rcu_read_unlock();
3191 : 666 : }
3192 : :
3193 : 1162 : struct sock *mptcp_sk_clone_init(const struct sock *sk,
3194 : : const struct mptcp_options_received *mp_opt,
3195 : : struct sock *ssk,
3196 : : struct request_sock *req)
3197 : : {
3198 : 1162 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3199 : 1162 : struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3200 : 1162 : struct mptcp_subflow_context *subflow;
3201 : 1162 : struct mptcp_sock *msk;
3202 : :
3203 [ + - ]: 1162 : if (!nsk)
3204 : : return NULL;
3205 : :
3206 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3207 [ + + ]: 1162 : if (nsk->sk_family == AF_INET6)
3208 : 496 : inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3209 : : #endif
3210 : :
3211 : 1162 : __mptcp_init_sock(nsk);
3212 : :
3213 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3214 [ + + ]: 1162 : if (nsk->sk_family == AF_INET6)
3215 : 496 : mptcp_copy_ip6_options(nsk, sk);
3216 : : else
3217 : : #endif
3218 : 666 : mptcp_copy_ip_options(nsk, sk);
3219 : :
3220 [ - + ]: 1162 : msk = mptcp_sk(nsk);
3221 : 1162 : WRITE_ONCE(msk->local_key, subflow_req->local_key);
3222 : 1162 : WRITE_ONCE(msk->token, subflow_req->token);
3223 : 1162 : msk->in_accept_queue = 1;
3224 : 1162 : WRITE_ONCE(msk->fully_established, false);
3225 [ + + ]: 1162 : if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3226 : 10 : WRITE_ONCE(msk->csum_enabled, true);
3227 : :
3228 : 1162 : WRITE_ONCE(msk->write_seq, subflow_req->idsn + 1);
3229 : 1162 : WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3230 : 1162 : WRITE_ONCE(msk->snd_una, msk->write_seq);
3231 [ - + ]: 1162 : WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3232 [ - + ]: 1162 : msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3233 [ - + ]: 1162 : mptcp_init_sched(msk, mptcp_sk(sk)->sched);
3234 : :
3235 : : /* passive msk is created after the first/MPC subflow */
3236 : 1162 : msk->subflow_id = 2;
3237 : :
3238 : 1162 : sock_reset_flag(nsk, SOCK_RCU_FREE);
3239 : 1162 : security_inet_csk_clone(nsk, req);
3240 : :
3241 : : /* this can't race with mptcp_close(), as the msk is
3242 : : * not yet exposted to user-space
3243 : : */
3244 : 1162 : mptcp_set_state(nsk, TCP_ESTABLISHED);
3245 : :
3246 : : /* The msk maintain a ref to each subflow in the connections list */
3247 : 1162 : WRITE_ONCE(msk->first, ssk);
3248 : 1162 : subflow = mptcp_subflow_ctx(ssk);
3249 : 1162 : list_add(&subflow->node, &msk->conn_list);
3250 : 1162 : sock_hold(ssk);
3251 : :
3252 : : /* new mpc subflow takes ownership of the newly
3253 : : * created mptcp socket
3254 : : */
3255 : 1162 : mptcp_token_accept(subflow_req, msk);
3256 : :
3257 : : /* set msk addresses early to ensure mptcp_pm_get_local_id()
3258 : : * uses the correct data
3259 : : */
3260 : 1162 : mptcp_copy_inaddrs(nsk, ssk);
3261 [ + - ]: 1162 : __mptcp_propagate_sndbuf(nsk, ssk);
3262 : :
3263 : 1162 : mptcp_rcv_space_init(msk, ssk);
3264 : :
3265 [ + + ]: 1162 : if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
3266 : 1124 : __mptcp_subflow_fully_established(msk, subflow, mp_opt);
3267 : 1162 : bh_unlock_sock(nsk);
3268 : :
3269 : : /* note: the newly allocated socket refcount is 2 now */
3270 : 1162 : return nsk;
3271 : : }
3272 : :
3273 : 2376 : void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3274 : : {
3275 [ - + ]: 2376 : const struct tcp_sock *tp = tcp_sk(ssk);
3276 : :
3277 : 2376 : msk->rcvspace_init = 1;
3278 : 2376 : msk->rcvq_space.copied = 0;
3279 : 2376 : msk->rcvq_space.rtt_us = 0;
3280 : :
3281 : 2376 : msk->rcvq_space.time = tp->tcp_mstamp;
3282 : :
3283 : : /* initial rcv_space offering made to peer */
3284 : 2376 : msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3285 : : TCP_INIT_CWND * tp->advmss);
3286 [ - + ]: 2376 : if (msk->rcvq_space.space == 0)
3287 : 0 : msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3288 : 2376 : }
3289 : :
3290 : 3676 : void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
3291 : : {
3292 : 3676 : struct mptcp_subflow_context *subflow, *tmp;
3293 : 3676 : struct sock *sk = (struct sock *)msk;
3294 : :
3295 : 3676 : __mptcp_clear_xmit(sk);
3296 : :
3297 : : /* join list will be eventually flushed (with rst) at sock lock release time */
3298 [ + + ]: 7906 : mptcp_for_each_subflow_safe(msk, subflow, tmp)
3299 : 4230 : __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, flags);
3300 : :
3301 : 3676 : __skb_queue_purge(&sk->sk_receive_queue);
3302 : 3676 : skb_rbtree_purge(&msk->out_of_order_queue);
3303 : :
3304 : : /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3305 : : * inet_sock_destruct() will dispose it
3306 : : */
3307 : 3676 : mptcp_token_destroy(msk);
3308 : 3676 : mptcp_pm_free_anno_list(msk);
3309 : 3676 : mptcp_free_local_addr_list(msk);
3310 : 3676 : }
3311 : :
3312 : 3618 : static void mptcp_destroy(struct sock *sk)
3313 : : {
3314 [ - + ]: 3618 : struct mptcp_sock *msk = mptcp_sk(sk);
3315 : :
3316 : : /* allow the following to close even the initial subflow */
3317 : 3618 : msk->free_first = 1;
3318 : 3618 : mptcp_destroy_common(msk, 0);
3319 : 3618 : sk_sockets_allocated_dec(sk);
3320 : 3618 : }
3321 : :
3322 : 231638 : void __mptcp_data_acked(struct sock *sk)
3323 : : {
3324 [ + + ]: 231638 : if (!sock_owned_by_user(sk))
3325 : 158280 : __mptcp_clean_una(sk);
3326 : : else
3327 [ - + ]: 73358 : __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3328 : 231638 : }
3329 : :
3330 : 778878 : void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3331 : : {
3332 [ + + ]: 778878 : if (!mptcp_send_head(sk))
3333 : : return;
3334 : :
3335 [ + + ]: 98807 : if (!sock_owned_by_user(sk))
3336 : 73697 : __mptcp_subflow_push_pending(sk, ssk, false);
3337 : : else
3338 [ - + ]: 25110 : __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3339 : : }
3340 : :
3341 : : #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3342 : : BIT(MPTCP_RETRANSMIT) | \
3343 : : BIT(MPTCP_FLUSH_JOIN_LIST) | \
3344 : : BIT(MPTCP_DEQUEUE))
3345 : :
3346 : : /* processes deferred events and flush wmem */
3347 : 1225659 : static void mptcp_release_cb(struct sock *sk)
3348 : : __must_hold(&sk->sk_lock.slock)
3349 : : {
3350 [ - + ]: 1225659 : struct mptcp_sock *msk = mptcp_sk(sk);
3351 : :
3352 : 638586 : for (;;) {
3353 : 1303125 : unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
3354 : 1303125 : struct list_head join_list;
3355 : :
3356 [ + + ]: 1303125 : if (!flags)
3357 : : break;
3358 : :
3359 [ + + ]: 77466 : INIT_LIST_HEAD(&join_list);
3360 [ + + ]: 77466 : list_splice_init(&msk->join_list, &join_list);
3361 : :
3362 : : /* the following actions acquire the subflow socket lock
3363 : : *
3364 : : * 1) can't be invoked in atomic scope
3365 : : * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3366 : : * datapath acquires the msk socket spinlock while helding
3367 : : * the subflow socket lock
3368 : : */
3369 : 77466 : msk->cb_flags &= ~flags;
3370 : 77466 : spin_unlock_bh(&sk->sk_lock.slock);
3371 : :
3372 [ + + ]: 77466 : if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3373 : 38 : __mptcp_flush_join_list(sk, &join_list);
3374 [ + + ]: 77466 : if (flags & BIT(MPTCP_PUSH_PENDING))
3375 : 18921 : __mptcp_push_pending(sk, 0);
3376 [ + + ]: 77466 : if (flags & BIT(MPTCP_RETRANSMIT))
3377 : 3467 : __mptcp_retrans(sk);
3378 [ + + + + ]: 77466 : if ((flags & BIT(MPTCP_DEQUEUE)) && __mptcp_move_skbs(sk)) {
3379 : : /* notify ack seq update */
3380 : 48553 : mptcp_cleanup_rbuf(msk, 0);
3381 : 48553 : sk->sk_data_ready(sk);
3382 : : }
3383 : :
3384 : 77466 : cond_resched();
3385 : 77466 : spin_lock_bh(&sk->sk_lock.slock);
3386 : : }
3387 : :
3388 [ - + - - : 1928726 : if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
- - + + ]
3389 : 56185 : __mptcp_clean_una_wakeup(sk);
3390 [ + + ]: 1225659 : if (unlikely(msk->cb_flags)) {
3391 : : /* be sure to sync the msk state before taking actions
3392 : : * depending on sk_state (MPTCP_ERROR_REPORT)
3393 : : * On sk release avoid actions depending on the first subflow
3394 : : */
3395 [ - + - - : 6215 : if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
- - + + +
- ]
3396 : 881 : __mptcp_sync_state(sk, msk->pending_state);
3397 [ - + - - : 6215 : if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
- - + + ]
3398 : 444 : __mptcp_error_report(sk);
3399 [ - + - - : 6215 : if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
- - + + ]
3400 : 1839 : __mptcp_sync_sndbuf(sk);
3401 : : }
3402 : 1225659 : }
3403 : :
3404 : : /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3405 : : * TCP can't schedule delack timer before the subflow is fully established.
3406 : : * MPTCP uses the delack timer to do 3rd ack retransmissions
3407 : : */
3408 : 500 : static void schedule_3rdack_retransmission(struct sock *ssk)
3409 : : {
3410 : 500 : struct inet_connection_sock *icsk = inet_csk(ssk);
3411 [ - + ]: 500 : struct tcp_sock *tp = tcp_sk(ssk);
3412 : 500 : unsigned long timeout;
3413 : :
3414 [ + + + + ]: 500 : if (READ_ONCE(mptcp_subflow_ctx(ssk)->fully_established))
3415 : : return;
3416 : :
3417 : : /* reschedule with a timeout above RTT, as we must look only for drop */
3418 [ + - ]: 90 : if (tp->srtt_us)
3419 [ - + ]: 90 : timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3420 : : else
3421 : : timeout = TCP_TIMEOUT_INIT;
3422 : 90 : timeout += jiffies;
3423 : :
3424 [ - + ]: 90 : WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3425 : 90 : smp_store_release(&icsk->icsk_ack.pending,
3426 : : icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER);
3427 : 90 : icsk->icsk_ack.timeout = timeout;
3428 : 90 : sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3429 : : }
3430 : :
3431 : 27185 : void mptcp_subflow_process_delegated(struct sock *ssk, long status)
3432 : : {
3433 [ + + ]: 27185 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3434 : 27185 : struct sock *sk = subflow->conn;
3435 : :
3436 [ + + ]: 27185 : if (status & BIT(MPTCP_DELEGATE_SEND)) {
3437 : 3807 : mptcp_data_lock(sk);
3438 [ + + ]: 3807 : if (!sock_owned_by_user(sk))
3439 : 3534 : __mptcp_subflow_push_pending(sk, ssk, true);
3440 : : else
3441 [ - + ]: 273 : __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3442 : 3807 : mptcp_data_unlock(sk);
3443 : : }
3444 [ + + ]: 27185 : if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
3445 : 23035 : mptcp_data_lock(sk);
3446 [ + + ]: 23035 : if (!sock_owned_by_user(sk))
3447 : 20866 : __mptcp_sync_sndbuf(sk);
3448 : : else
3449 [ - + ]: 2169 : __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
3450 : 23035 : mptcp_data_unlock(sk);
3451 : : }
3452 [ + + ]: 27185 : if (status & BIT(MPTCP_DELEGATE_ACK))
3453 : 500 : schedule_3rdack_retransmission(ssk);
3454 : 27185 : }
3455 : :
3456 : 0 : static int mptcp_hash(struct sock *sk)
3457 : : {
3458 : : /* should never be called,
3459 : : * we hash the TCP subflows not the MPTCP socket
3460 : : */
3461 : 0 : WARN_ON_ONCE(1);
3462 : 0 : return 0;
3463 : : }
3464 : :
3465 : 4 : static void mptcp_unhash(struct sock *sk)
3466 : : {
3467 : : /* called from sk_common_release(), but nothing to do here */
3468 : 4 : }
3469 : :
3470 : 0 : static int mptcp_get_port(struct sock *sk, unsigned short snum)
3471 : : {
3472 [ # # ]: 0 : struct mptcp_sock *msk = mptcp_sk(sk);
3473 : :
3474 [ # # ]: 0 : pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
3475 [ # # ]: 0 : if (WARN_ON_ONCE(!msk->first))
3476 : 0 : return -EINVAL;
3477 : :
3478 : 0 : return inet_csk_get_port(msk->first, snum);
3479 : : }
3480 : :
3481 : 1124 : void mptcp_finish_connect(struct sock *ssk)
3482 : : {
3483 : 1124 : struct mptcp_subflow_context *subflow;
3484 : 1124 : struct mptcp_sock *msk;
3485 : 1124 : struct sock *sk;
3486 : :
3487 [ - + ]: 1124 : subflow = mptcp_subflow_ctx(ssk);
3488 : 1124 : sk = subflow->conn;
3489 [ - + ]: 1124 : msk = mptcp_sk(sk);
3490 : :
3491 [ - + ]: 1124 : pr_debug("msk=%p, token=%u\n", sk, subflow->token);
3492 : :
3493 : 1124 : subflow->map_seq = subflow->iasn;
3494 : 1124 : subflow->map_subflow_seq = 1;
3495 : :
3496 : : /* the socket is not connected yet, no msk/subflow ops can access/race
3497 : : * accessing the field below
3498 : : */
3499 : 1124 : WRITE_ONCE(msk->local_key, subflow->local_key);
3500 : :
3501 : 1124 : mptcp_pm_new_connection(msk, ssk, 0);
3502 : 1124 : }
3503 : :
3504 : 4657 : void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3505 : : {
3506 : 4657 : write_lock_bh(&sk->sk_callback_lock);
3507 : 4657 : rcu_assign_pointer(sk->sk_wq, &parent->wq);
3508 : 4657 : sk_set_socket(sk, parent);
3509 : 4657 : sk->sk_uid = SOCK_INODE(parent)->i_uid;
3510 : 4657 : write_unlock_bh(&sk->sk_callback_lock);
3511 : 4657 : }
3512 : :
3513 : 994 : bool mptcp_finish_join(struct sock *ssk)
3514 : : {
3515 [ - + ]: 994 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3516 [ - + ]: 994 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3517 : 994 : struct sock *parent = (void *)msk;
3518 : 994 : bool ret = true;
3519 : :
3520 [ - + ]: 994 : pr_debug("msk=%p, subflow=%p\n", msk, subflow);
3521 : :
3522 : : /* mptcp socket already closing? */
3523 [ - + ]: 994 : if (!mptcp_is_fully_established(parent)) {
3524 : 0 : subflow->reset_reason = MPTCP_RST_EMPTCP;
3525 : 0 : return false;
3526 : : }
3527 : :
3528 : : /* active subflow, already present inside the conn_list */
3529 [ + + ]: 994 : if (!list_empty(&subflow->node)) {
3530 : 488 : mptcp_subflow_joined(msk, ssk);
3531 : 488 : mptcp_propagate_sndbuf(parent, ssk);
3532 : 488 : return true;
3533 : : }
3534 : :
3535 [ - + ]: 506 : if (!mptcp_pm_allow_new_subflow(msk))
3536 : 0 : goto err_prohibited;
3537 : :
3538 : : /* If we can't acquire msk socket lock here, let the release callback
3539 : : * handle it
3540 : : */
3541 : 506 : mptcp_data_lock(parent);
3542 [ + + ]: 506 : if (!sock_owned_by_user(parent)) {
3543 : 466 : ret = __mptcp_finish_join(msk, ssk);
3544 [ + - ]: 466 : if (ret) {
3545 : 466 : sock_hold(ssk);
3546 : 466 : list_add_tail(&subflow->node, &msk->conn_list);
3547 : : }
3548 : : } else {
3549 : 40 : sock_hold(ssk);
3550 [ - + ]: 40 : list_add_tail(&subflow->node, &msk->join_list);
3551 [ - + - - : 40 : __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
- - ]
3552 : : }
3553 : 506 : mptcp_data_unlock(parent);
3554 : :
3555 [ - + ]: 506 : if (!ret) {
3556 : 0 : err_prohibited:
3557 : 0 : subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3558 : 0 : return false;
3559 : : }
3560 : :
3561 : : return true;
3562 : : }
3563 : :
3564 : 1487 : static void mptcp_shutdown(struct sock *sk, int how)
3565 : : {
3566 [ - + ]: 1487 : pr_debug("sk=%p, how=%d\n", sk, how);
3567 : :
3568 [ + - + + ]: 1487 : if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3569 : 1395 : __mptcp_wr_shutdown(sk);
3570 : 1487 : }
3571 : :
3572 : 16 : static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3573 : : {
3574 : 16 : const struct sock *sk = (void *)msk;
3575 : 16 : u64 delta;
3576 : :
3577 [ + - ]: 16 : if (sk->sk_state == TCP_LISTEN)
3578 : : return -EINVAL;
3579 : :
3580 [ + + + - ]: 16 : if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
3581 : : return 0;
3582 : :
3583 : 16 : delta = msk->write_seq - v;
3584 [ + + + + ]: 16 : if (__mptcp_check_fallback(msk) && msk->first) {
3585 [ - + ]: 16 : struct tcp_sock *tp = tcp_sk(msk->first);
3586 : :
3587 : : /* the first subflow is disconnected after close - see
3588 : : * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3589 : : * so ignore that status, too.
3590 : : */
3591 [ + + - + ]: 16 : if (!((1 << msk->first->sk_state) &
3592 : : (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3593 : 16 : delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3594 : : }
3595 : 16 : if (delta > INT_MAX)
3596 : : delta = INT_MAX;
3597 : :
3598 : 16 : return (int)delta;
3599 : : }
3600 : :
3601 : 16 : static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3602 : : {
3603 [ - + ]: 16 : struct mptcp_sock *msk = mptcp_sk(sk);
3604 : 16 : bool slow;
3605 : :
3606 [ - + + - ]: 16 : switch (cmd) {
3607 : 0 : case SIOCINQ:
3608 [ # # ]: 0 : if (sk->sk_state == TCP_LISTEN)
3609 : : return -EINVAL;
3610 : :
3611 : 0 : lock_sock(sk);
3612 [ # # ]: 0 : if (__mptcp_move_skbs(sk))
3613 : 0 : mptcp_cleanup_rbuf(msk, 0);
3614 : 0 : *karg = mptcp_inq_hint(sk);
3615 : 0 : release_sock(sk);
3616 : 0 : break;
3617 : 4 : case SIOCOUTQ:
3618 : 8 : slow = lock_sock_fast(sk);
3619 : 8 : *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3620 : 8 : unlock_sock_fast(sk, slow);
3621 : 8 : break;
3622 : 4 : case SIOCOUTQNSD:
3623 : 8 : slow = lock_sock_fast(sk);
3624 : 8 : *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3625 : 8 : unlock_sock_fast(sk, slow);
3626 : 8 : break;
3627 : : default:
3628 : : return -ENOIOCTLCMD;
3629 : : }
3630 : :
3631 : : return 0;
3632 : : }
3633 : :
3634 : 1282 : static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3635 : : {
3636 : 1282 : struct mptcp_subflow_context *subflow;
3637 [ - + ]: 1282 : struct mptcp_sock *msk = mptcp_sk(sk);
3638 : 1282 : int err = -EINVAL;
3639 : 1282 : struct sock *ssk;
3640 : :
3641 : 1282 : ssk = __mptcp_nmpc_sk(msk);
3642 [ - + ]: 1282 : if (IS_ERR(ssk))
3643 : 0 : return PTR_ERR(ssk);
3644 : :
3645 : 1282 : mptcp_set_state(sk, TCP_SYN_SENT);
3646 [ + - ]: 1282 : subflow = mptcp_subflow_ctx(ssk);
3647 : : #ifdef CONFIG_TCP_MD5SIG
3648 : : /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3649 : : * TCP option space.
3650 : : */
3651 : : if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
3652 : : mptcp_subflow_early_fallback(msk, subflow);
3653 : : #endif
3654 [ + - ]: 1282 : if (subflow->request_mptcp) {
3655 [ + + ]: 1282 : if (mptcp_active_should_disable(sk)) {
3656 [ + - ]: 6 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEACTIVEDISABLED);
3657 : 6 : mptcp_subflow_early_fallback(msk, subflow);
3658 [ - + ]: 1276 : } else if (mptcp_token_new_connect(ssk) < 0) {
3659 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_TOKENFALLBACKINIT);
3660 : 0 : mptcp_subflow_early_fallback(msk, subflow);
3661 : : }
3662 : : }
3663 : :
3664 : 1282 : WRITE_ONCE(msk->write_seq, subflow->idsn);
3665 : 1282 : WRITE_ONCE(msk->snd_nxt, subflow->idsn);
3666 : 1282 : WRITE_ONCE(msk->snd_una, subflow->idsn);
3667 [ + + ]: 1282 : if (likely(!__mptcp_check_fallback(msk)))
3668 [ + - ]: 1276 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3669 : :
3670 : : /* if reaching here via the fastopen/sendmsg path, the caller already
3671 : : * acquired the subflow socket lock, too.
3672 : : */
3673 [ + + ]: 1282 : if (!msk->fastopening)
3674 : 1236 : lock_sock(ssk);
3675 : :
3676 : : /* the following mirrors closely a very small chunk of code from
3677 : : * __inet_stream_connect()
3678 : : */
3679 [ - + ]: 1282 : if (ssk->sk_state != TCP_CLOSE)
3680 : 0 : goto out;
3681 : :
3682 [ + - - + : 1282 : if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
- - ]
3683 : 0 : err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3684 [ - - ]: 0 : if (err)
3685 : 0 : goto out;
3686 : : }
3687 : :
3688 : 1282 : err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3689 [ - + ]: 1282 : if (err < 0)
3690 : 0 : goto out;
3691 : :
3692 [ + + ]: 1282 : inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3693 : :
3694 : 1282 : out:
3695 [ + + ]: 1282 : if (!msk->fastopening)
3696 : 1236 : release_sock(ssk);
3697 : :
3698 : : /* on successful connect, the msk state will be moved to established by
3699 : : * subflow_finish_connect()
3700 : : */
3701 [ - + ]: 1282 : if (unlikely(err)) {
3702 : : /* avoid leaving a dangling token in an unconnected socket */
3703 : 0 : mptcp_token_destroy(msk);
3704 : 0 : mptcp_set_state(sk, TCP_CLOSE);
3705 : 0 : return err;
3706 : : }
3707 : :
3708 : 1282 : mptcp_copy_inaddrs(sk, ssk);
3709 : 1282 : return 0;
3710 : : }
3711 : :
3712 : : static struct proto mptcp_prot = {
3713 : : .name = "MPTCP",
3714 : : .owner = THIS_MODULE,
3715 : : .init = mptcp_init_sock,
3716 : : .connect = mptcp_connect,
3717 : : .disconnect = mptcp_disconnect,
3718 : : .close = mptcp_close,
3719 : : .setsockopt = mptcp_setsockopt,
3720 : : .getsockopt = mptcp_getsockopt,
3721 : : .shutdown = mptcp_shutdown,
3722 : : .destroy = mptcp_destroy,
3723 : : .sendmsg = mptcp_sendmsg,
3724 : : .ioctl = mptcp_ioctl,
3725 : : .recvmsg = mptcp_recvmsg,
3726 : : .release_cb = mptcp_release_cb,
3727 : : .hash = mptcp_hash,
3728 : : .unhash = mptcp_unhash,
3729 : : .get_port = mptcp_get_port,
3730 : : .stream_memory_free = mptcp_stream_memory_free,
3731 : : .sockets_allocated = &mptcp_sockets_allocated,
3732 : :
3733 : : .memory_allocated = &tcp_memory_allocated,
3734 : : .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3735 : :
3736 : : .memory_pressure = &tcp_memory_pressure,
3737 : : .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3738 : : .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3739 : : .sysctl_mem = sysctl_tcp_mem,
3740 : : .obj_size = sizeof(struct mptcp_sock),
3741 : : .slab_flags = SLAB_TYPESAFE_BY_RCU,
3742 : : .no_autobind = true,
3743 : : };
3744 : :
3745 : 1246 : static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3746 : : {
3747 [ - + ]: 1246 : struct mptcp_sock *msk = mptcp_sk(sock->sk);
3748 : 1246 : struct sock *ssk, *sk = sock->sk;
3749 : 1246 : int err = -EINVAL;
3750 : :
3751 : 1246 : lock_sock(sk);
3752 : 1246 : ssk = __mptcp_nmpc_sk(msk);
3753 [ - + ]: 1246 : if (IS_ERR(ssk)) {
3754 : 0 : err = PTR_ERR(ssk);
3755 : 0 : goto unlock;
3756 : : }
3757 : :
3758 [ + + ]: 1246 : if (sk->sk_family == AF_INET)
3759 : 704 : err = inet_bind_sk(ssk, uaddr, addr_len);
3760 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3761 [ + - ]: 542 : else if (sk->sk_family == AF_INET6)
3762 : 542 : err = inet6_bind_sk(ssk, uaddr, addr_len);
3763 : : #endif
3764 [ + + ]: 1246 : if (!err)
3765 : 1240 : mptcp_copy_inaddrs(sk, ssk);
3766 : :
3767 : 6 : unlock:
3768 : 1246 : release_sock(sk);
3769 : 1246 : return err;
3770 : : }
3771 : :
3772 : 1240 : static int mptcp_listen(struct socket *sock, int backlog)
3773 : : {
3774 [ - + ]: 1240 : struct mptcp_sock *msk = mptcp_sk(sock->sk);
3775 : 1240 : struct sock *sk = sock->sk;
3776 : 1240 : struct sock *ssk;
3777 : 1240 : int err;
3778 : :
3779 [ - + ]: 1240 : pr_debug("msk=%p\n", msk);
3780 : :
3781 : 1240 : lock_sock(sk);
3782 : :
3783 : 1240 : err = -EINVAL;
3784 [ - + ]: 1240 : if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
3785 : 0 : goto unlock;
3786 : :
3787 : 1240 : ssk = __mptcp_nmpc_sk(msk);
3788 [ - + ]: 1240 : if (IS_ERR(ssk)) {
3789 : 0 : err = PTR_ERR(ssk);
3790 : 0 : goto unlock;
3791 : : }
3792 : :
3793 : 1240 : mptcp_set_state(sk, TCP_LISTEN);
3794 : 1240 : sock_set_flag(sk, SOCK_RCU_FREE);
3795 : :
3796 : 1240 : lock_sock(ssk);
3797 : 1240 : err = __inet_listen_sk(ssk, backlog);
3798 : 1240 : release_sock(ssk);
3799 : 1240 : mptcp_set_state(sk, inet_sk_state_load(ssk));
3800 : :
3801 [ - + ]: 1240 : if (!err) {
3802 : 1240 : sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3803 : 1240 : mptcp_copy_inaddrs(sk, ssk);
3804 : 1240 : mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
3805 : : }
3806 : :
3807 : 0 : unlock:
3808 : 1240 : release_sock(sk);
3809 : 1240 : return err;
3810 : : }
3811 : :
3812 : 1220 : static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3813 : : struct proto_accept_arg *arg)
3814 : : {
3815 [ - + ]: 1220 : struct mptcp_sock *msk = mptcp_sk(sock->sk);
3816 : 1220 : struct sock *ssk, *newsk;
3817 : :
3818 [ - + ]: 1220 : pr_debug("msk=%p\n", msk);
3819 : :
3820 : : /* Buggy applications can call accept on socket states other then LISTEN
3821 : : * but no need to allocate the first subflow just to error out.
3822 : : */
3823 : 1220 : ssk = READ_ONCE(msk->first);
3824 [ + - ]: 1220 : if (!ssk)
3825 : : return -EINVAL;
3826 : :
3827 [ - + ]: 1220 : pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
3828 : 1220 : newsk = inet_csk_accept(ssk, arg);
3829 [ + + ]: 1220 : if (!newsk)
3830 : 6 : return arg->err;
3831 : :
3832 [ - + - - ]: 1214 : pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
3833 [ + + + + ]: 1214 : if (sk_is_mptcp(newsk)) {
3834 : 1120 : struct mptcp_subflow_context *subflow;
3835 : 1120 : struct sock *new_mptcp_sock;
3836 : :
3837 [ - + ]: 1120 : subflow = mptcp_subflow_ctx(newsk);
3838 : 1120 : new_mptcp_sock = subflow->conn;
3839 : :
3840 : : /* is_mptcp should be false if subflow->conn is missing, see
3841 : : * subflow_syn_recv_sock()
3842 : : */
3843 [ - + ]: 1120 : if (WARN_ON_ONCE(!new_mptcp_sock)) {
3844 [ # # ]: 0 : tcp_sk(newsk)->is_mptcp = 0;
3845 : 0 : goto tcpfallback;
3846 : : }
3847 : :
3848 : 1120 : newsk = new_mptcp_sock;
3849 [ + - ]: 1120 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
3850 : :
3851 [ - + ]: 1120 : newsk->sk_kern_sock = arg->kern;
3852 : 1120 : lock_sock(newsk);
3853 : 1120 : __inet_accept(sock, newsock, newsk);
3854 : :
3855 : 1120 : set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
3856 [ - + ]: 1120 : msk = mptcp_sk(newsk);
3857 : 1120 : msk->in_accept_queue = 0;
3858 : :
3859 : : /* set ssk->sk_socket of accept()ed flows to mptcp socket.
3860 : : * This is needed so NOSPACE flag can be set from tcp stack.
3861 : : */
3862 [ + + ]: 2248 : mptcp_for_each_subflow(msk, subflow) {
3863 [ + - ]: 1128 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3864 : :
3865 [ + - ]: 1128 : if (!ssk->sk_socket)
3866 : 1128 : mptcp_sock_graft(ssk, newsock);
3867 : : }
3868 : :
3869 : : /* Do late cleanup for the first subflow as necessary. Also
3870 : : * deal with bad peers not doing a complete shutdown.
3871 : : */
3872 [ + + ]: 1120 : if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
3873 : 6 : __mptcp_close_ssk(newsk, msk->first,
3874 : 6 : mptcp_subflow_ctx(msk->first), 0);
3875 [ - + ]: 6 : if (unlikely(list_is_singular(&msk->conn_list)))
3876 : 6 : mptcp_set_state(newsk, TCP_CLOSE);
3877 : : }
3878 : : } else {
3879 : 94 : tcpfallback:
3880 [ - + ]: 94 : newsk->sk_kern_sock = arg->kern;
3881 : 94 : lock_sock(newsk);
3882 : 94 : __inet_accept(sock, newsock, newsk);
3883 : : /* we are being invoked after accepting a non-mp-capable
3884 : : * flow: sk is a tcp_sk, not an mptcp one.
3885 : : *
3886 : : * Hand the socket over to tcp so all further socket ops
3887 : : * bypass mptcp.
3888 : : */
3889 : 94 : WRITE_ONCE(newsock->sk->sk_socket->ops,
3890 : : mptcp_fallback_tcp_ops(newsock->sk));
3891 : : }
3892 : 1214 : release_sock(newsk);
3893 : :
3894 : 1214 : return 0;
3895 : : }
3896 : :
3897 : 338750 : static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
3898 : : {
3899 : 338750 : struct sock *sk = (struct sock *)msk;
3900 : :
3901 [ + + ]: 338750 : if (__mptcp_stream_is_writeable(sk, 1))
3902 : : return EPOLLOUT | EPOLLWRNORM;
3903 : :
3904 : 99676 : set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
3905 : 99676 : smp_mb__after_atomic(); /* NOSPACE is changed by mptcp_write_space() */
3906 [ + + ]: 99676 : if (__mptcp_stream_is_writeable(sk, 1))
3907 : : return EPOLLOUT | EPOLLWRNORM;
3908 : :
3909 : : return 0;
3910 : : }
3911 : :
3912 : 587524 : static __poll_t mptcp_poll(struct file *file, struct socket *sock,
3913 : : struct poll_table_struct *wait)
3914 : : {
3915 : 587524 : struct sock *sk = sock->sk;
3916 : 587524 : struct mptcp_sock *msk;
3917 : 587524 : __poll_t mask = 0;
3918 : 587524 : u8 shutdown;
3919 : 587524 : int state;
3920 : :
3921 [ - + ]: 587524 : msk = mptcp_sk(sk);
3922 : 587524 : sock_poll_wait(file, sock, wait);
3923 : :
3924 : 587524 : state = inet_sk_state_load(sk);
3925 [ - + ]: 587524 : pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
3926 [ + + ]: 587524 : if (state == TCP_LISTEN) {
3927 : 1888 : struct sock *ssk = READ_ONCE(msk->first);
3928 : :
3929 [ - + ]: 1888 : if (WARN_ON_ONCE(!ssk))
3930 : 0 : return 0;
3931 : :
3932 [ + + ]: 2832 : return inet_csk_listen_poll(ssk);
3933 : : }
3934 : :
3935 : 585636 : shutdown = READ_ONCE(sk->sk_shutdown);
3936 [ + + ]: 585636 : if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
3937 : 6276 : mask |= EPOLLHUP;
3938 [ + + ]: 585636 : if (shutdown & RCV_SHUTDOWN)
3939 : 164372 : mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
3940 : :
3941 [ + - ]: 585636 : if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
3942 : 585636 : mask |= mptcp_check_readable(sk);
3943 [ + + ]: 585636 : if (shutdown & SEND_SHUTDOWN)
3944 : 246886 : mask |= EPOLLOUT | EPOLLWRNORM;
3945 : : else
3946 : 338750 : mask |= mptcp_check_writeable(msk);
3947 [ # # # # ]: 0 : } else if (state == TCP_SYN_SENT &&
3948 [ # # ]: 0 : inet_test_bit(DEFER_CONNECT, sk)) {
3949 : : /* cf tcp_poll() note about TFO */
3950 : 0 : mask |= EPOLLOUT | EPOLLWRNORM;
3951 : : }
3952 : :
3953 : : /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
3954 : 585636 : smp_rmb();
3955 [ + + ]: 585636 : if (READ_ONCE(sk->sk_err))
3956 : 23 : mask |= EPOLLERR;
3957 : :
3958 : : return mask;
3959 : : }
3960 : :
3961 : : static const struct proto_ops mptcp_stream_ops = {
3962 : : .family = PF_INET,
3963 : : .owner = THIS_MODULE,
3964 : : .release = inet_release,
3965 : : .bind = mptcp_bind,
3966 : : .connect = inet_stream_connect,
3967 : : .socketpair = sock_no_socketpair,
3968 : : .accept = mptcp_stream_accept,
3969 : : .getname = inet_getname,
3970 : : .poll = mptcp_poll,
3971 : : .ioctl = inet_ioctl,
3972 : : .gettstamp = sock_gettstamp,
3973 : : .listen = mptcp_listen,
3974 : : .shutdown = inet_shutdown,
3975 : : .setsockopt = sock_common_setsockopt,
3976 : : .getsockopt = sock_common_getsockopt,
3977 : : .sendmsg = inet_sendmsg,
3978 : : .recvmsg = inet_recvmsg,
3979 : : .mmap = sock_no_mmap,
3980 : : .set_rcvlowat = mptcp_set_rcvlowat,
3981 : : };
3982 : :
3983 : : static struct inet_protosw mptcp_protosw = {
3984 : : .type = SOCK_STREAM,
3985 : : .protocol = IPPROTO_MPTCP,
3986 : : .prot = &mptcp_prot,
3987 : : .ops = &mptcp_stream_ops,
3988 : : .flags = INET_PROTOSW_ICSK,
3989 : : };
3990 : :
3991 : 32822 : static int mptcp_napi_poll(struct napi_struct *napi, int budget)
3992 : : {
3993 : 32822 : struct mptcp_delegated_action *delegated;
3994 : 32822 : struct mptcp_subflow_context *subflow;
3995 : 32822 : int work_done = 0;
3996 : :
3997 : 32822 : delegated = container_of(napi, struct mptcp_delegated_action, napi);
3998 [ + + ]: 66618 : while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
3999 : 33796 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4000 : :
4001 : 33796 : bh_lock_sock_nested(ssk);
4002 [ + + ]: 33796 : if (!sock_owned_by_user(ssk)) {
4003 : 25038 : mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
4004 : : } else {
4005 : : /* tcp_release_cb_override already processed
4006 : : * the action or will do at next release_sock().
4007 : : * In both case must dequeue the subflow here - on the same
4008 : : * CPU that scheduled it.
4009 : : */
4010 : 8758 : smp_wmb();
4011 : 8758 : clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
4012 : : }
4013 : 33796 : bh_unlock_sock(ssk);
4014 : 33796 : sock_put(ssk);
4015 : :
4016 [ + - ]: 33796 : if (++work_done == budget)
4017 : : return budget;
4018 : : }
4019 : :
4020 : : /* always provide a 0 'work_done' argument, so that napi_complete_done
4021 : : * will not try accessing the NULL napi->dev ptr
4022 : : */
4023 : 32822 : napi_complete_done(napi, 0);
4024 : 32822 : return work_done;
4025 : : }
4026 : :
4027 : 4 : void __init mptcp_proto_init(void)
4028 : : {
4029 : 4 : struct mptcp_delegated_action *delegated;
4030 : 4 : int cpu;
4031 : :
4032 : 4 : mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
4033 : :
4034 [ - + ]: 4 : if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4035 : 0 : panic("Failed to allocate MPTCP pcpu counter\n");
4036 : :
4037 : 4 : mptcp_napi_dev = alloc_netdev_dummy(0);
4038 [ - + ]: 4 : if (!mptcp_napi_dev)
4039 : 0 : panic("Failed to allocate MPTCP dummy netdev\n");
4040 [ + - + - ]: 36 : for_each_possible_cpu(cpu) {
4041 : 16 : delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4042 : 16 : INIT_LIST_HEAD(&delegated->head);
4043 : 16 : netif_napi_add_tx(mptcp_napi_dev, &delegated->napi,
4044 : : mptcp_napi_poll);
4045 : 16 : napi_enable(&delegated->napi);
4046 : : }
4047 : :
4048 : 4 : mptcp_subflow_init();
4049 : 4 : mptcp_pm_init();
4050 : 4 : mptcp_sched_init();
4051 : 4 : mptcp_token_init();
4052 : :
4053 [ - + ]: 4 : if (proto_register(&mptcp_prot, 1) != 0)
4054 : 0 : panic("Failed to register MPTCP proto.\n");
4055 : :
4056 : 4 : inet_register_protosw(&mptcp_protosw);
4057 : :
4058 : 4 : BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4059 : 4 : }
4060 : :
4061 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4062 : : static const struct proto_ops mptcp_v6_stream_ops = {
4063 : : .family = PF_INET6,
4064 : : .owner = THIS_MODULE,
4065 : : .release = inet6_release,
4066 : : .bind = mptcp_bind,
4067 : : .connect = inet_stream_connect,
4068 : : .socketpair = sock_no_socketpair,
4069 : : .accept = mptcp_stream_accept,
4070 : : .getname = inet6_getname,
4071 : : .poll = mptcp_poll,
4072 : : .ioctl = inet6_ioctl,
4073 : : .gettstamp = sock_gettstamp,
4074 : : .listen = mptcp_listen,
4075 : : .shutdown = inet_shutdown,
4076 : : .setsockopt = sock_common_setsockopt,
4077 : : .getsockopt = sock_common_getsockopt,
4078 : : .sendmsg = inet6_sendmsg,
4079 : : .recvmsg = inet6_recvmsg,
4080 : : .mmap = sock_no_mmap,
4081 : : #ifdef CONFIG_COMPAT
4082 : : .compat_ioctl = inet6_compat_ioctl,
4083 : : #endif
4084 : : .set_rcvlowat = mptcp_set_rcvlowat,
4085 : : };
4086 : :
4087 : : static struct proto mptcp_v6_prot;
4088 : :
4089 : : static struct inet_protosw mptcp_v6_protosw = {
4090 : : .type = SOCK_STREAM,
4091 : : .protocol = IPPROTO_MPTCP,
4092 : : .prot = &mptcp_v6_prot,
4093 : : .ops = &mptcp_v6_stream_ops,
4094 : : .flags = INET_PROTOSW_ICSK,
4095 : : };
4096 : :
4097 : 4 : int __init mptcp_proto_v6_init(void)
4098 : : {
4099 : 4 : int err;
4100 : :
4101 : 4 : mptcp_v6_prot = mptcp_prot;
4102 : 4 : strscpy(mptcp_v6_prot.name, "MPTCPv6", sizeof(mptcp_v6_prot.name));
4103 : 4 : mptcp_v6_prot.slab = NULL;
4104 : 4 : mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4105 : 4 : mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4106 : :
4107 : 4 : err = proto_register(&mptcp_v6_prot, 1);
4108 [ + - ]: 4 : if (err)
4109 : : return err;
4110 : :
4111 : 4 : err = inet6_register_protosw(&mptcp_v6_protosw);
4112 [ - + ]: 4 : if (err)
4113 : 0 : proto_unregister(&mptcp_v6_prot);
4114 : :
4115 : : return err;
4116 : : }
4117 : : #endif
|