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