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