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