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