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