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