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