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