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