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 : 1047676 : 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 : 455525 : static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
155 : : struct sk_buff *from, bool *fragstolen,
156 : : int *delta)
157 : : {
158 : 455525 : int limit = READ_ONCE(sk->sk_rcvbuf);
159 : :
160 [ + + ]: 455525 : if (unlikely(MPTCP_SKB_CB(to)->cant_coalesce) ||
161 [ + + ]: 455499 : MPTCP_SKB_CB(from)->offset ||
162 [ + + + + ]: 871078 : ((to->len + from->len) > (limit >> 3)) ||
163 : 415585 : !skb_try_coalesce(to, from, fragstolen, delta))
164 : 112824 : return false;
165 : :
166 [ - + ]: 342701 : 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 : 342701 : MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;
170 : 342701 : return true;
171 : : }
172 : :
173 : 455525 : static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
174 : : struct sk_buff *from)
175 : : {
176 : 455525 : bool fragstolen;
177 : 455525 : int delta;
178 : :
179 [ + + ]: 455525 : 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 : 342701 : atomic_add(delta, &sk->sk_rmem_alloc);
187 [ + - ]: 342701 : sk_mem_charge(sk, delta);
188 [ - + ]: 342701 : kfree_skb_partial(from, fragstolen);
189 : :
190 : 342701 : 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 [ + + ]: 32526 : if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
197 : : return false;
198 : :
199 : 50527 : 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 : 36689 : static bool mptcp_rcvbuf_grow(struct sock *sk, u32 newval)
207 : : {
208 [ - + ]: 36689 : struct mptcp_sock *msk = mptcp_sk(sk);
209 [ + - ]: 36689 : const struct net *net = sock_net(sk);
210 : 36689 : u32 rcvwin, rcvbuf, cap, oldval;
211 : 36689 : u64 grow;
212 : :
213 : 36689 : oldval = msk->rcvq_space.space;
214 : 36689 : msk->rcvq_space.space = newval;
215 [ + - ]: 36689 : if (!READ_ONCE(net->ipv4.sysctl_tcp_moderate_rcvbuf) ||
216 [ - + ]: 36689 : (sk->sk_userlocks & SOCK_RCVBUF_LOCK))
217 : : return false;
218 : :
219 : : /* DRS is always one RTT late. */
220 : 36689 : rcvwin = newval << 1;
221 : :
222 : : /* slow start: allow the sender to double its rate. */
223 : 36689 : grow = (u64)rcvwin * (newval - oldval);
224 : 36689 : do_div(grow, oldval);
225 : 36689 : rcvwin += grow << 1;
226 : :
227 [ + + ]: 36689 : if (!RB_EMPTY_ROOT(&msk->out_of_order_queue))
228 : 32987 : rcvwin += MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq - msk->ack_seq;
229 : :
230 : 36689 : cap = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]);
231 : :
232 : 36689 : rcvbuf = min_t(u32, mptcp_space_from_win(sk, rcvwin), cap);
233 [ + + ]: 36689 : if (rcvbuf > sk->sk_rcvbuf) {
234 : 1878 : WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
235 : 1878 : 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 : 90973 : static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
245 : : {
246 : 90973 : struct sock *sk = (struct sock *)msk;
247 : 90973 : struct rb_node **p, *parent;
248 : 90973 : u64 seq, end_seq, max_seq;
249 : 90973 : struct sk_buff *skb1;
250 : :
251 : 90973 : seq = MPTCP_SKB_CB(skb)->map_seq;
252 : 90973 : end_seq = MPTCP_SKB_CB(skb)->end_seq;
253 [ - + ]: 90973 : max_seq = atomic64_read(&msk->rcv_wnd_sent);
254 : :
255 [ - + ]: 90973 : 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 [ - + ]: 90973 : 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 : 90973 : p = &msk->out_of_order_queue.rb_node;
268 [ + - ]: 90973 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUE);
269 [ + + ]: 90973 : if (RB_EMPTY_ROOT(&msk->out_of_order_queue)) {
270 : 3586 : rb_link_node(&skb->rbnode, NULL, p);
271 : 3586 : rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
272 : 3586 : msk->ooo_last_skb = skb;
273 : 3586 : 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 [ + + + + ]: 150585 : if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
280 [ + - ]: 51729 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
281 [ + - ]: 51729 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
282 : 51729 : return;
283 : : }
284 : :
285 : : /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
286 [ + + ]: 35658 : if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
287 [ + - ]: 25116 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
288 : 25116 : parent = &msk->ooo_last_skb->rbnode;
289 : 25116 : p = &parent->rb_right;
290 : 25116 : goto insert;
291 : : }
292 : :
293 : : /* Find place to insert this segment. Handle overlaps on the way. */
294 : : parent = NULL;
295 [ + + ]: 53967 : while (*p) {
296 : 49984 : parent = *p;
297 : 49984 : skb1 = rb_to_skb(parent);
298 [ + + ]: 49984 : if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
299 : 12537 : p = &parent->rb_left;
300 : 12537 : continue;
301 : : }
302 [ + + ]: 37447 : if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
303 [ + + ]: 328 : if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
304 : : /* All the bits are present. Drop. */
305 : 138 : mptcp_drop(sk, skb);
306 [ + - ]: 138 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
307 : 138 : return;
308 : : }
309 [ + + ]: 190 : 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 : 4 : rb_replace_node(&skb1->rbnode, &skb->rbnode,
320 : : &msk->out_of_order_queue);
321 : 4 : mptcp_drop(sk, skb1);
322 [ + - ]: 4 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
323 : 4 : goto merge_right;
324 : : }
325 [ + + + + ]: 46135 : } else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
326 [ + - ]: 6417 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
327 : 6417 : return;
328 : : }
329 : 30888 : p = &parent->rb_right;
330 : : }
331 : :
332 : 3983 : insert:
333 : : /* Insert segment into RB tree. */
334 : 29099 : rb_link_node(&skb->rbnode, parent, p);
335 : 29099 : rb_insert_color(&skb->rbnode, &msk->out_of_order_queue);
336 : :
337 : : merge_right:
338 : : /* Remove other segments covered by skb. */
339 [ + + ]: 29176 : while ((skb1 = skb_rb_next(skb)) != NULL) {
340 [ + + ]: 4059 : if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
341 : : break;
342 : 73 : rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
343 : 73 : mptcp_drop(sk, skb1);
344 [ + - ]: 73 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
345 : : }
346 : : /* If there is no skb after us, we are the last_skb ! */
347 [ + + ]: 29103 : if (!skb1)
348 : 25117 : msk->ooo_last_skb = skb;
349 : :
350 : 3986 : end:
351 : 32689 : skb_condense(skb);
352 : 32689 : skb_set_owner_r(skb, sk);
353 : : /* do not grow rcvbuf for not-yet-accepted or orphaned sockets. */
354 [ + - ]: 32689 : if (sk->sk_socket)
355 : 32689 : mptcp_rcvbuf_grow(sk, msk->rcvq_space.space);
356 : : }
357 : :
358 : 628121 : static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset,
359 : : int copy_len)
360 : : {
361 : 628121 : const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
362 : 628121 : 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 : 628121 : MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
369 : 628121 : MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
370 : 628121 : MPTCP_SKB_CB(skb)->offset = offset;
371 : 628121 : MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
372 : 628121 : MPTCP_SKB_CB(skb)->cant_coalesce = 0;
373 : :
374 : 628121 : __skb_unlink(skb, &ssk->sk_receive_queue);
375 : :
376 : 628121 : skb_ext_reset(skb);
377 [ - + ]: 628121 : skb_dst_drop(skb);
378 : 628121 : }
379 : :
380 : 628121 : static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)
381 : : {
382 : 628121 : u64 copy_len = MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq;
383 [ - + ]: 628121 : struct mptcp_sock *msk = mptcp_sk(sk);
384 : 628121 : struct sk_buff *tail;
385 : :
386 : : /* try to fetch required memory from subflow */
387 [ - + ]: 628121 : 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 [ + + ]: 628121 : if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
393 : : /* in sequence */
394 : 537148 : msk->bytes_received += copy_len;
395 : 537148 : WRITE_ONCE(msk->ack_seq, msk->ack_seq + copy_len);
396 [ + + ]: 537148 : tail = skb_peek_tail(&sk->sk_receive_queue);
397 [ + - + + ]: 351338 : if (tail && mptcp_try_coalesce(sk, tail, skb))
398 : : return true;
399 : :
400 : 275034 : skb_set_owner_r(skb, sk);
401 : 275034 : __skb_queue_tail(&sk->sk_receive_queue, skb);
402 : 275034 : return true;
403 [ + - ]: 90973 : } else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
404 : 90973 : mptcp_data_queue_ofo(msk, skb);
405 : 90973 : 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 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
412 : 0 : drop:
413 : 0 : mptcp_drop(sk, skb);
414 : 0 : return false;
415 : : }
416 : :
417 : 44359 : static void mptcp_stop_rtx_timer(struct sock *sk)
418 : : {
419 : 44359 : struct inet_connection_sock *icsk = inet_csk(sk);
420 : :
421 : 44359 : sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
422 [ - + ]: 44359 : mptcp_sk(sk)->timer_ival = 0;
423 : 44359 : }
424 : :
425 : 5798 : static void mptcp_close_wake_up(struct sock *sk)
426 : : {
427 [ + + ]: 5798 : if (sock_flag(sk, SOCK_DEAD))
428 : : return;
429 : :
430 : 3789 : sk->sk_state_change(sk);
431 [ + + ]: 3789 : if (sk->sk_shutdown == SHUTDOWN_MASK ||
432 [ + + ]: 1850 : sk->sk_state == TCP_CLOSE)
433 : 1991 : sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
434 : : else
435 : 1798 : sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
436 : : }
437 : :
438 : 2013 : static void mptcp_shutdown_subflows(struct mptcp_sock *msk)
439 : : {
440 : 2013 : struct mptcp_subflow_context *subflow;
441 : :
442 [ + + ]: 4596 : mptcp_for_each_subflow(msk, subflow) {
443 : 2583 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
444 : 2583 : bool slow;
445 : :
446 : 2583 : slow = lock_sock_fast(ssk);
447 : 2583 : tcp_shutdown(ssk, SEND_SHUTDOWN);
448 : 2583 : unlock_sock_fast(ssk, slow);
449 : : }
450 : 2013 : }
451 : :
452 : : /* called under the msk socket lock */
453 : 279255 : static bool mptcp_pending_data_fin_ack(struct sock *sk)
454 : : {
455 [ - + ]: 279255 : struct mptcp_sock *msk = mptcp_sk(sk);
456 : :
457 : 106614 : return ((1 << sk->sk_state) &
458 [ - + + + ]: 279255 : (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
[ + + ]
459 [ + + ]: 44905 : msk->write_seq == READ_ONCE(msk->snd_una);
460 : : }
461 : :
462 : 19850 : static void mptcp_check_data_fin_ack(struct sock *sk)
463 : : {
464 [ - + ]: 19850 : struct mptcp_sock *msk = mptcp_sk(sk);
465 : :
466 : : /* Look for an acknowledged DATA_FIN */
467 [ + + ]: 19850 : if (mptcp_pending_data_fin_ack(sk)) {
468 : 2183 : WRITE_ONCE(msk->snd_data_fin_enable, 0);
469 : :
470 [ + + - ]: 2183 : switch (sk->sk_state) {
471 : 1175 : case TCP_FIN_WAIT1:
472 : 1175 : mptcp_set_state(sk, TCP_FIN_WAIT2);
473 : 1175 : break;
474 : 1008 : case TCP_CLOSING:
475 : : case TCP_LAST_ACK:
476 : 1008 : mptcp_shutdown_subflows(msk);
477 : 1008 : mptcp_set_state(sk, TCP_CLOSE);
478 : 1008 : break;
479 : : }
480 : :
481 : 2183 : mptcp_close_wake_up(sk);
482 : : }
483 : 19850 : }
484 : :
485 : : /* can be called with no lock acquired */
486 : 562202 : static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
487 : : {
488 [ - + ]: 562202 : struct mptcp_sock *msk = mptcp_sk(sk);
489 : :
490 [ - + + + : 582875 : if (READ_ONCE(msk->rcv_data_fin) &&
- + + + ]
[ - + + +
+ + ][ + + ]
[ + + + + ]
491 [ + + ]: 31 : ((1 << inet_sk_state_load(sk)) &
492 : : (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
493 : 19946 : u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
494 : :
495 [ + + ]: 19946 : if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
496 [ + + ]: 2598 : if (seq)
497 : 2140 : *seq = rcv_data_fin_seq;
498 : :
499 : 2598 : return true;
500 : : }
501 : : }
502 : :
503 : : return false;
504 : : }
505 : :
506 : 1278 : static void mptcp_set_datafin_timeout(struct sock *sk)
507 : : {
508 : 1278 : struct inet_connection_sock *icsk = inet_csk(sk);
509 : 1278 : u32 retransmits;
510 : :
511 : 1278 : retransmits = min_t(u32, icsk->icsk_retransmits,
512 : : ilog2(TCP_RTO_MAX / TCP_RTO_MIN));
513 : :
514 [ - + ]: 1278 : mptcp_sk(sk)->timer_ival = TCP_RTO_MIN << retransmits;
515 : 1278 : }
516 : :
517 : 784488 : static void __mptcp_set_timeout(struct sock *sk, long tout)
518 : : {
519 [ + + - + ]: 1261764 : mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
520 : 784488 : }
521 : :
522 : 24 : static long mptcp_timeout_from_subflow(const struct mptcp_subflow_context *subflow)
523 : : {
524 : 1165152 : const struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
525 : :
526 [ + + + + ]: 898776 : return inet_csk(ssk)->icsk_pending && !subflow->stale_count ?
527 [ + + + + ]: 1870033 : icsk_timeout(inet_csk(ssk)) - jiffies : 0;
528 : : }
529 : :
530 : 279171 : static void mptcp_set_timeout(struct sock *sk)
531 : : {
532 : 279171 : struct mptcp_subflow_context *subflow;
533 : 279171 : long tout = 0;
534 : :
535 [ - + - + : 688397 : mptcp_for_each_subflow(mptcp_sk(sk), subflow)
+ + ]
536 [ + + ]: 720872 : tout = max(tout, mptcp_timeout_from_subflow(subflow));
537 : 279171 : __mptcp_set_timeout(sk, tout);
538 : 279171 : }
539 : :
540 : 205583 : static inline bool tcp_can_send_ack(const struct sock *ssk)
541 : : {
542 [ - + ]: 454319 : return !((1 << inet_sk_state_load(ssk)) &
543 : : (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_TIME_WAIT | TCPF_CLOSE | TCPF_LISTEN));
544 : : }
545 : :
546 : 5227 : void __mptcp_subflow_send_ack(struct sock *ssk)
547 : : {
548 [ + + ]: 5227 : if (tcp_can_send_ack(ssk))
549 : 5049 : tcp_send_ack(ssk);
550 : 5227 : }
551 : :
552 : 4293 : static void mptcp_subflow_send_ack(struct sock *ssk)
553 : : {
554 : 4293 : bool slow;
555 : :
556 : 4293 : slow = lock_sock_fast(ssk);
557 : 4293 : __mptcp_subflow_send_ack(ssk);
558 : 4293 : unlock_sock_fast(ssk, slow);
559 : 4293 : }
560 : :
561 : 3295 : static void mptcp_send_ack(struct mptcp_sock *msk)
562 : : {
563 : 3295 : struct mptcp_subflow_context *subflow;
564 : :
565 [ + + ]: 7588 : mptcp_for_each_subflow(msk, subflow)
566 : 4293 : mptcp_subflow_send_ack(mptcp_subflow_tcp_sock(subflow));
567 : 3295 : }
568 : :
569 : 324743 : static void mptcp_subflow_cleanup_rbuf(struct sock *ssk, int copied)
570 : : {
571 : 324743 : bool slow;
572 : :
573 : 324743 : slow = lock_sock_fast(ssk);
574 [ + + ]: 324743 : if (tcp_can_send_ack(ssk))
575 : 318854 : tcp_cleanup_rbuf(ssk, copied);
576 : 324743 : unlock_sock_fast(ssk, slow);
577 : 324743 : }
578 : :
579 : 637819 : static bool mptcp_subflow_could_cleanup(const struct sock *ssk, bool rx_empty)
580 : : {
581 : 637819 : const struct inet_connection_sock *icsk = inet_csk(ssk);
582 : 637819 : u8 ack_pending = READ_ONCE(icsk->icsk_ack.pending);
583 [ - + ]: 637819 : const struct tcp_sock *tp = tcp_sk(ssk);
584 : :
585 [ + + ]: 637819 : return (ack_pending & ICSK_ACK_SCHED) &&
586 : 228772 : ((READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->rcv_wup) >
587 [ + + + + ]: 228772 : READ_ONCE(icsk->icsk_ack.rcv_mss)) ||
588 [ + + ]: 30345 : (rx_empty && ack_pending &
589 : : (ICSK_ACK_PUSHED2 | ICSK_ACK_PUSHED)));
590 : : }
591 : :
592 : 583986 : static void mptcp_cleanup_rbuf(struct mptcp_sock *msk, int copied)
593 : : {
594 : 583986 : int old_space = READ_ONCE(msk->old_wspace);
595 : 583986 : struct mptcp_subflow_context *subflow;
596 : 583986 : struct sock *sk = (struct sock *)msk;
597 : 583986 : int space = __mptcp_space(sk);
598 : 583986 : bool cleanup, rx_empty;
599 : :
600 [ + + + + : 583986 : cleanup = (space > 0) && (space >= (old_space << 1)) && copied;
+ + ]
601 [ + + + + ]: 583986 : rx_empty = !sk_rmem_alloc_get(sk) && copied;
602 : :
603 [ + + ]: 1416883 : mptcp_for_each_subflow(msk, subflow) {
604 [ + + ]: 832897 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
605 : :
606 [ + + + + ]: 832897 : if (cleanup || mptcp_subflow_could_cleanup(ssk, rx_empty))
607 : 324743 : mptcp_subflow_cleanup_rbuf(ssk, copied);
608 : : }
609 : 583986 : }
610 : :
611 : 68599 : static void mptcp_check_data_fin(struct sock *sk)
612 : : {
613 [ - + ]: 68599 : struct mptcp_sock *msk = mptcp_sk(sk);
614 : 68599 : 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 [ + + ]: 68599 : if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
630 : 2140 : WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
631 : 2140 : WRITE_ONCE(msk->rcv_data_fin, 0);
632 : :
633 : 2140 : WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
634 : 2140 : smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
635 : :
636 [ + + + - ]: 2140 : switch (sk->sk_state) {
637 : : case TCP_ESTABLISHED:
638 : 856 : mptcp_set_state(sk, TCP_CLOSE_WAIT);
639 : 856 : break;
640 : 279 : case TCP_FIN_WAIT1:
641 : 279 : mptcp_set_state(sk, TCP_CLOSING);
642 : 279 : break;
643 : 1005 : case TCP_FIN_WAIT2:
644 : 1005 : mptcp_shutdown_subflows(msk);
645 : 1005 : mptcp_set_state(sk, TCP_CLOSE);
646 : 1005 : break;
647 : : default:
648 : : /* Other states not expected */
649 : 0 : WARN_ON_ONCE(1);
650 : 0 : break;
651 : : }
652 : :
653 [ + + ]: 2140 : if (!__mptcp_check_fallback(msk))
654 : 2017 : mptcp_send_ack(msk);
655 : 2140 : mptcp_close_wake_up(sk);
656 : : }
657 : 68599 : }
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 : 545308 : static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
668 : : struct sock *ssk)
669 : : {
670 [ - + ]: 545308 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
671 : 545308 : struct sock *sk = (struct sock *)msk;
672 : 545308 : bool more_data_avail;
673 : 545308 : struct tcp_sock *tp;
674 : 545308 : bool ret = false;
675 : :
676 [ - + ]: 545308 : pr_debug("msk=%p ssk=%p\n", msk, ssk);
677 [ - + ]: 545308 : tp = tcp_sk(ssk);
678 : 631103 : do {
679 : 631103 : u32 map_remaining, offset;
680 : 631103 : u32 seq = tp->copied_seq;
681 : 631103 : struct sk_buff *skb;
682 : 631103 : bool fin;
683 : :
684 [ + + ]: 631103 : if (sk_rmem_alloc_get(sk) > sk->sk_rcvbuf)
685 : : break;
686 : :
687 : : /* try to move as much data as available */
688 : 1256298 : map_remaining = subflow->map_data_len -
689 : 628149 : mptcp_subflow_get_map_offset(subflow);
690 : :
691 [ + - ]: 628149 : skb = skb_peek(&ssk->sk_receive_queue);
692 [ + - ]: 628149 : if (unlikely(!skb))
693 : : break;
694 : :
695 [ + + ]: 628149 : 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 : 9919 : map_remaining = skb->len;
701 : 9919 : subflow->map_data_len = skb->len;
702 : : }
703 : :
704 : 628149 : offset = seq - TCP_SKB_CB(skb)->seq;
705 : 628149 : fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
706 [ + + ]: 628149 : if (fin)
707 : 124 : seq++;
708 : :
709 [ + + ]: 628149 : if (offset < skb->len) {
710 : 628121 : size_t len = skb->len - offset;
711 : :
712 : 628121 : mptcp_init_skb(ssk, skb, offset, len);
713 : 628121 : skb_orphan(skb);
714 [ + + + + ]: 628121 : ret = __mptcp_move_skb(sk, skb) || ret;
715 : 628121 : seq += len;
716 : :
717 [ - + ]: 628121 : if (unlikely(map_remaining < len)) {
718 : 0 : DEBUG_NET_WARN_ON_ONCE(1);
719 : 0 : mptcp_dss_corruption(msk, ssk);
720 : : }
721 : : } else {
722 [ - + ]: 28 : if (unlikely(!fin)) {
723 : 0 : DEBUG_NET_WARN_ON_ONCE(1);
724 : 0 : mptcp_dss_corruption(msk, ssk);
725 : : }
726 : :
727 : 28 : sk_eat_skb(ssk, skb);
728 : : }
729 : :
730 : 628149 : WRITE_ONCE(tp->copied_seq, seq);
731 : 628149 : more_data_avail = mptcp_subflow_data_available(ssk);
732 : :
733 [ + + ]: 628149 : } while (more_data_avail);
734 : :
735 [ + + ]: 545308 : if (ret)
736 : 453015 : msk->last_data_recv = tcp_jiffies32;
737 : 545308 : return ret;
738 : : }
739 : :
740 : 741766 : static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
741 : : {
742 : 741766 : struct sock *sk = (struct sock *)msk;
743 : 741766 : struct sk_buff *skb, *tail;
744 : 741766 : bool moved = false;
745 : 741766 : struct rb_node *p;
746 : 741766 : u64 end_seq;
747 : :
748 : 741766 : p = rb_first(&msk->out_of_order_queue);
749 [ - + ]: 741766 : pr_debug("msk=%p empty=%d\n", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
750 [ + + ]: 774378 : while (p) {
751 : 251167 : skb = rb_to_skb(p);
752 [ + + ]: 251167 : if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
753 : : break;
754 : :
755 : 32612 : p = rb_next(p);
756 : 32612 : rb_erase(&skb->rbnode, &msk->out_of_order_queue);
757 : :
758 [ + + ]: 32612 : if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
759 : : msk->ack_seq))) {
760 : 86 : mptcp_drop(sk, skb);
761 [ + - ]: 86 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
762 : 86 : continue;
763 : : }
764 : :
765 : 32526 : end_seq = MPTCP_SKB_CB(skb)->end_seq;
766 [ + - ]: 32526 : tail = skb_peek_tail(&sk->sk_receive_queue);
767 [ + - + + ]: 64499 : if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
768 : 10085 : int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
769 : :
770 : : /* skip overlapping data, if any */
771 [ - + ]: 10085 : pr_debug("uncoalesced seq=%llx ack seq=%llx delta=%d\n",
772 : : MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq,
773 : : delta);
774 : 10085 : MPTCP_SKB_CB(skb)->offset += delta;
775 : 10085 : MPTCP_SKB_CB(skb)->map_seq += delta;
776 : 10085 : __skb_queue_tail(&sk->sk_receive_queue, skb);
777 : : }
778 : 32526 : msk->bytes_received += end_seq - msk->ack_seq;
779 : 32526 : WRITE_ONCE(msk->ack_seq, end_seq);
780 : 32526 : moved = true;
781 : : }
782 : 741766 : return moved;
783 : : }
784 : :
785 : 6467 : static bool __mptcp_subflow_error_report(struct sock *sk, struct sock *ssk)
786 : : {
787 [ + + ]: 6467 : int err = sock_error(ssk);
788 : 3538 : int ssk_state;
789 : :
790 [ + + ]: 3502 : if (!err)
791 : 2965 : return false;
792 : :
793 : : /* only propagate errors on fallen-back sockets or
794 : : * on MPC connect
795 : : */
796 [ + + - + : 505 : 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 : 27 : ssk_state = inet_sk_state_load(ssk);
805 [ + - + - ]: 27 : if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
806 : 27 : mptcp_set_state(sk, ssk_state);
807 : 27 : WRITE_ONCE(sk->sk_err, -err);
808 : :
809 : : /* This barrier is coupled with smp_rmb() in mptcp_poll() */
810 : 27 : smp_wmb();
811 : 27 : sk_error_report(sk);
812 : 27 : return true;
813 : : }
814 : :
815 : 955 : void __mptcp_error_report(struct sock *sk)
816 : : {
817 : 955 : struct mptcp_subflow_context *subflow;
818 [ - + ]: 955 : struct mptcp_sock *msk = mptcp_sk(sk);
819 : :
820 [ + + ]: 2143 : mptcp_for_each_subflow(msk, subflow)
821 [ + + ]: 1215 : if (__mptcp_subflow_error_report(sk, mptcp_subflow_tcp_sock(subflow)))
822 : : break;
823 : 955 : }
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 : 493603 : static bool move_skbs_to_msk(struct mptcp_sock *msk, struct sock *ssk)
829 : : {
830 : 493603 : struct sock *sk = (struct sock *)msk;
831 : 493603 : bool moved;
832 : :
833 : 493603 : moved = __mptcp_move_skbs_from_subflow(msk, ssk);
834 : 493603 : __mptcp_ofo_queue(msk);
835 [ - + ]: 493603 : 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 [ + + ]: 493603 : if (mptcp_pending_data_fin(sk, NULL))
844 : 458 : mptcp_schedule_work(sk);
845 : 493603 : return moved;
846 : : }
847 : :
848 : 493603 : static void __mptcp_data_ready(struct sock *sk, struct sock *ssk)
849 : : {
850 [ - + ]: 493603 : struct mptcp_sock *msk = mptcp_sk(sk);
851 : :
852 : : /* Wake-up the reader only for in-sequence data */
853 [ + + + - ]: 493603 : if (move_skbs_to_msk(msk, ssk) && mptcp_epollin_ready(sk))
854 : 404157 : sk->sk_data_ready(sk);
855 : 493603 : }
856 : :
857 : 577889 : void mptcp_data_ready(struct sock *sk, struct sock *ssk)
858 : : {
859 [ + - ]: 577889 : 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 [ + - ]: 577889 : if (unlikely(subflow->disposable))
866 : : return;
867 : :
868 : 577889 : mptcp_data_lock(sk);
869 [ + + ]: 577889 : if (!sock_owned_by_user(sk))
870 : 493603 : __mptcp_data_ready(sk, ssk);
871 : : else
872 [ - + ]: 84286 : __set_bit(MPTCP_DEQUEUE, &mptcp_sk(sk)->cb_flags);
873 : 577889 : 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 : 447 : 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 : 10 : static void __mptcp_flush_join_list(struct sock *sk, struct list_head *join_list)
912 : : {
913 : 10 : struct mptcp_subflow_context *tmp, *subflow;
914 [ - + ]: 10 : struct mptcp_sock *msk = mptcp_sk(sk);
915 : :
916 [ + + ]: 20 : list_for_each_entry_safe(subflow, tmp, join_list, node) {
917 : 10 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
918 : 10 : bool slow = lock_sock_fast(ssk);
919 : :
920 : 10 : list_move_tail(&subflow->node, &msk->conn_list);
921 [ - + ]: 10 : if (!__mptcp_finish_join(msk, ssk))
922 : 0 : mptcp_subflow_reset(ssk);
923 : 10 : unlock_sock_fast(ssk, slow);
924 : : }
925 : 10 : }
926 : :
927 : 0 : static bool mptcp_rtx_timer_pending(struct sock *sk)
928 : : {
929 : 353810 : return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
930 : : }
931 : :
932 : 251670 : static void mptcp_reset_rtx_timer(struct sock *sk)
933 : : {
934 : 251670 : struct inet_connection_sock *icsk = inet_csk(sk);
935 : 251670 : unsigned long tout;
936 : :
937 : : /* prevent rescheduling on close */
938 [ + + ]: 251670 : if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
939 : : return;
940 : :
941 [ - + ]: 251629 : tout = mptcp_sk(sk)->timer_ival;
942 : 251629 : sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
943 : : }
944 : :
945 : 23200 : bool mptcp_schedule_work(struct sock *sk)
946 : : {
947 [ + + ]: 23200 : 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 : 22206 : sock_hold(sk);
955 : :
956 [ - + + + ]: 22206 : if (schedule_work(&mptcp_sk(sk)->work))
957 : : return true;
958 : :
959 : 1368 : sock_put(sk);
960 : 1368 : return false;
961 : : }
962 : :
963 : 475164 : static bool mptcp_skb_can_collapse_to(u64 write_seq,
964 : : const struct sk_buff *skb,
965 : : const struct mptcp_ext *mpext)
966 : : {
967 [ + + ]: 475164 : 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 [ + - + + ]: 867106 : return mpext && mpext->data_seq + mpext->data_len == write_seq &&
974 [ + + ]: 399759 : !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 : 484937 : 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 [ + - ]: 255229 : return df && pfrag->page == df->page &&
987 [ + + ]: 255229 : pfrag->size - pfrag->offset > 0 &&
988 [ + + + - ]: 632222 : pfrag->offset == (df->offset + df->data_len) &&
989 [ - + ]: 147285 : df->data_seq + df->data_len == msk->write_seq;
990 : : }
991 : :
992 : 0 : static void dfrag_uncharge(struct sock *sk, int len)
993 : : {
994 : 469956 : sk_mem_uncharge(sk, len);
995 : 469956 : sk_wmem_queued_add(sk, -len);
996 : 132325 : }
997 : :
998 : 337643 : static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
999 : : {
1000 : 337643 : int len = dfrag->data_len + dfrag->overhead;
1001 : :
1002 : 337643 : list_del(&dfrag->list);
1003 : 337643 : dfrag_uncharge(sk, len);
1004 : 337643 : put_page(dfrag->page);
1005 : 337643 : }
1006 : :
1007 : : /* called under both the msk socket lock and the data lock */
1008 : 259405 : static void __mptcp_clean_una(struct sock *sk)
1009 : : {
1010 [ - + ]: 259405 : struct mptcp_sock *msk = mptcp_sk(sk);
1011 : 259405 : struct mptcp_data_frag *dtmp, *dfrag;
1012 : 259405 : u64 snd_una;
1013 : :
1014 : 259405 : snd_una = msk->snd_una;
1015 [ + + ]: 596913 : list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
1016 [ + + ]: 544324 : if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
1017 : : break;
1018 : :
1019 [ + + ]: 337508 : if (unlikely(dfrag == msk->first_pending)) {
1020 : : /* in recovery mode can see ack after the current snd head */
1021 [ - + - + ]: 60 : if (WARN_ON_ONCE(!msk->recovery))
[ # # ]
1022 : : break;
1023 : :
1024 : 60 : msk->first_pending = mptcp_send_next(sk);
1025 : : }
1026 : :
1027 : 337508 : dfrag_clear(sk, dfrag);
1028 : : }
1029 : :
1030 : 259405 : dfrag = mptcp_rtx_head(sk);
1031 [ + + + + ]: 259405 : if (dfrag && after64(snd_una, dfrag->data_seq)) {
1032 : 132325 : u64 delta = snd_una - dfrag->data_seq;
1033 : :
1034 : : /* prevent wrap around in recovery mode */
1035 [ - + ]: 132325 : 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 : 132325 : dfrag->data_seq += delta;
1044 : 132325 : dfrag->offset += delta;
1045 : 132325 : dfrag->data_len -= delta;
1046 : 132325 : dfrag->already_sent -= delta;
1047 : :
1048 : 132325 : dfrag_uncharge(sk, delta);
1049 : : }
1050 : :
1051 : : /* all retransmitted data acked, recovery completed */
1052 [ - + + + : 259405 : if (unlikely(msk->recovery) && after64(msk->snd_una, msk->recovery_snd_nxt))
+ + ]
[ + + + + ]
1053 : 34 : msk->recovery = false;
1054 : :
1055 : 56302 : out:
1056 [ + + + + ]: 259405 : if (snd_una == msk->snd_nxt && snd_una == msk->write_seq) {
1057 [ + + + + ]: 50960 : if (mptcp_rtx_timer_pending(sk) && !mptcp_data_fin_enabled(msk))
[ + + ]
1058 : 39965 : mptcp_stop_rtx_timer(sk);
1059 : : } else {
1060 : 208445 : mptcp_reset_rtx_timer(sk);
1061 : : }
1062 : :
1063 [ + + ]: 259405 : if (mptcp_pending_data_fin_ack(sk))
1064 : 2115 : mptcp_schedule_work(sk);
1065 : 259405 : }
1066 : :
1067 : 57146 : static void __mptcp_clean_una_wakeup(struct sock *sk)
1068 : : {
1069 [ + - - + ]: 76442 : lockdep_assert_held_once(&sk->sk_lock.slock);
1070 : :
1071 : 76442 : __mptcp_clean_una(sk);
1072 : 76442 : mptcp_write_space(sk);
1073 : 72334 : }
1074 : :
1075 : 14154 : static void mptcp_clean_una_wakeup(struct sock *sk)
1076 : : {
1077 : 14154 : mptcp_data_lock(sk);
1078 : 14154 : __mptcp_clean_una_wakeup(sk);
1079 : 14154 : mptcp_data_unlock(sk);
1080 : 14154 : }
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 : 337652 : static bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1104 : : {
1105 [ - + ]: 337652 : 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 : 337652 : mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
1115 : : int orig_offset)
1116 : : {
1117 : 337652 : int offset = ALIGN(orig_offset, sizeof(long));
1118 : 337652 : struct mptcp_data_frag *dfrag;
1119 : :
1120 : 337652 : dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
1121 : 337652 : dfrag->data_len = 0;
1122 : 337652 : dfrag->data_seq = msk->write_seq;
1123 : 337652 : dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
1124 : 337652 : dfrag->offset = offset + sizeof(struct mptcp_data_frag);
1125 : 337652 : dfrag->already_sent = 0;
1126 : 337652 : dfrag->page = pfrag->page;
1127 : :
1128 : 337652 : 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 : 628980 : static int mptcp_check_allowed_size(const struct mptcp_sock *msk, struct sock *ssk,
1141 : : u64 data_seq, int avail_size)
1142 : : {
1143 : 628980 : u64 window_end = mptcp_wnd_end(msk);
1144 : 628980 : u64 mptcp_snd_wnd;
1145 : :
1146 [ + + ]: 628980 : if (__mptcp_check_fallback(msk))
1147 : : return avail_size;
1148 : :
1149 : 580088 : mptcp_snd_wnd = window_end - data_seq;
1150 : 580088 : avail_size = min_t(unsigned int, mptcp_snd_wnd, avail_size);
1151 : :
1152 [ - + + + ]: 580088 : if (unlikely(tcp_sk(ssk)->snd_wnd < mptcp_snd_wnd)) {
1153 [ - + ]: 3749 : tcp_sk(ssk)->snd_wnd = min_t(u64, U32_MAX, mptcp_snd_wnd);
1154 [ + - ]: 3749 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_SNDWNDSHARED);
1155 : : }
1156 : :
1157 : : return avail_size;
1158 : : }
1159 : :
1160 : 298142 : static bool __mptcp_add_ext(struct sk_buff *skb, gfp_t gfp)
1161 : : {
1162 : 298142 : struct skb_ext *mpext = __skb_ext_alloc(gfp);
1163 : :
1164 [ + - ]: 298142 : if (!mpext)
1165 : : return false;
1166 : 298142 : __skb_ext_set(skb, SKB_EXT_MPTCP, mpext);
1167 : 298142 : return true;
1168 : : }
1169 : :
1170 : 298142 : static struct sk_buff *__mptcp_do_alloc_tx_skb(struct sock *sk, gfp_t gfp)
1171 : : {
1172 : 298142 : struct sk_buff *skb;
1173 : :
1174 : 298142 : skb = alloc_skb_fclone(MAX_TCP_HEADER, gfp);
1175 [ + - ]: 298142 : if (likely(skb)) {
1176 [ + - ]: 298142 : if (likely(__mptcp_add_ext(skb, gfp))) {
1177 : 298142 : skb_reserve(skb, MAX_TCP_HEADER);
1178 : 298142 : skb->ip_summed = CHECKSUM_PARTIAL;
1179 : 298142 : INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
1180 : 298142 : return skb;
1181 : : }
1182 : 0 : __kfree_skb(skb);
1183 : : } else {
1184 : 0 : mptcp_enter_memory_pressure(sk);
1185 : : }
1186 : : return NULL;
1187 : : }
1188 : :
1189 : 298142 : static struct sk_buff *__mptcp_alloc_tx_skb(struct sock *sk, struct sock *ssk, gfp_t gfp)
1190 : : {
1191 : 298142 : struct sk_buff *skb;
1192 : :
1193 : 298142 : skb = __mptcp_do_alloc_tx_skb(sk, gfp);
1194 [ - + ]: 298142 : if (!skb)
1195 : : return NULL;
1196 : :
1197 [ + - ]: 298142 : if (likely(sk_wmem_schedule(ssk, skb->truesize))) {
1198 : 298142 : tcp_skb_entail(ssk, skb);
1199 : 298142 : 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 : 253809 : gfp_t gfp = data_lock_held ? GFP_ATOMIC : sk->sk_allocation;
1209 : :
1210 : 298142 : 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 : 176985 : static void mptcp_update_data_checksum(struct sk_buff *skb, int added)
1217 : : {
1218 [ + - ]: 176985 : struct mptcp_ext *mpext = mptcp_get_ext(skb);
1219 : 176985 : __wsum csum = ~csum_unfold(mpext->csum);
1220 : 176985 : int offset = skb->len - added;
1221 : :
1222 [ # # ]: 176985 : mpext->csum = csum_fold(csum_block_add(csum, skb_checksum(skb, offset, added, 0), offset));
1223 : 176985 : }
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 : 628980 : 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 : 628980 : u64 data_seq = dfrag->data_seq + info->sent;
1251 : 628980 : int offset = dfrag->offset + info->sent;
1252 [ - + ]: 628980 : struct mptcp_sock *msk = mptcp_sk(sk);
1253 : 628980 : bool zero_window_probe = false;
1254 : 628980 : struct mptcp_ext *mpext = NULL;
1255 : 628980 : bool can_coalesce = false;
1256 : 628980 : bool reuse_skb = true;
1257 : 628980 : struct sk_buff *skb;
1258 : 628980 : size_t copy;
1259 : 628980 : int i;
1260 : :
1261 [ - + ]: 628980 : 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 [ + - - + ]: 628980 : if (WARN_ON_ONCE(info->sent > info->limit ||
1265 : : info->limit > dfrag->data_len))
1266 : 0 : return 0;
1267 : :
1268 [ + - ]: 628980 : if (unlikely(!__tcp_can_send(ssk)))
1269 : : return -EAGAIN;
1270 : :
1271 : : /* compute send limit */
1272 [ - + ]: 628980 : if (unlikely(ssk->sk_gso_max_size > MPTCP_MAX_GSO_SIZE))
1273 : 0 : ssk->sk_gso_max_size = MPTCP_MAX_GSO_SIZE;
1274 : 628980 : info->mss_now = tcp_send_mss(ssk, &info->size_goal, info->flags);
1275 : 628980 : copy = info->size_goal;
1276 : :
1277 [ + + ]: 628980 : skb = tcp_write_queue_tail(ssk);
1278 [ + - + + ]: 505707 : 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 [ + - ]: 475164 : mpext = mptcp_get_ext(skb);
1286 [ + + ]: 475164 : if (!mptcp_skb_can_collapse_to(data_seq, skb, mpext)) {
1287 : 143927 : TCP_SKB_CB(skb)->eor = 1;
1288 [ - + ]: 143927 : tcp_mark_push(tcp_sk(ssk), skb);
1289 : 143927 : goto alloc_skb;
1290 : : }
1291 : :
1292 [ + + ]: 331237 : i = skb_shinfo(skb)->nr_frags;
1293 [ + + ]: 331237 : can_coalesce = skb_can_coalesce(skb, i, dfrag->page, offset);
1294 [ + + + + ]: 331237 : if (!can_coalesce && i >= READ_ONCE(net_hotdata.sysctl_max_skb_frags)) {
1295 [ - + ]: 399 : tcp_mark_push(tcp_sk(ssk), skb);
1296 : 399 : goto alloc_skb;
1297 : : }
1298 : :
1299 : 330838 : copy -= skb->len;
1300 : : } else {
1301 [ + + ]: 12 : alloc_skb:
1302 [ - + + + ]: 298142 : skb = mptcp_alloc_tx_skb(sk, ssk, info->data_lock_held);
[ + + ]
1303 [ - + ]: 298142 : if (!skb)
1304 : : return -ENOMEM;
1305 : :
1306 [ + - ]: 298142 : i = skb_shinfo(skb)->nr_frags;
1307 : 298142 : reuse_skb = false;
1308 [ + - ]: 298142 : mpext = mptcp_get_ext(skb);
1309 : : }
1310 : :
1311 : : /* Zero window and all data acked? Probe. */
1312 : 628980 : copy = mptcp_check_allowed_size(msk, ssk, data_seq, copy);
1313 [ + + ]: 628980 : if (copy == 0) {
1314 : 206538 : 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 [ + + + - : 229021 : if (snd_una != msk->snd_nxt || skb->len ||
+ + ]
[ # # # # ]
1321 [ # # ]: 0 : skb != tcp_send_head(ssk)) {
1322 : 202132 : tcp_remove_empty_skb(ssk);
1323 : 202132 : return 0;
1324 : : }
1325 : :
1326 : 4406 : zero_window_probe = true;
1327 : 4406 : data_seq = snd_una - 1;
1328 : 4406 : copy = 1;
1329 : : }
1330 : :
1331 : 426848 : copy = min_t(size_t, copy, info->limit - info->sent);
1332 [ - + ]: 426848 : if (!sk_wmem_schedule(ssk, copy)) {
1333 : 0 : tcp_remove_empty_skb(ssk);
1334 : 0 : return -ENOMEM;
1335 : : }
1336 : :
1337 [ + + ]: 426848 : if (can_coalesce) {
1338 : 17727 : skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1339 : : } else {
1340 : 409121 : get_page(dfrag->page);
1341 : 409121 : skb_fill_page_desc(skb, i, dfrag->page, offset, copy);
1342 : : }
1343 : :
1344 : 426848 : skb->len += copy;
1345 : 426848 : skb->data_len += copy;
1346 : 426848 : skb->truesize += copy;
1347 [ + - ]: 426848 : sk_wmem_queued_add(ssk, copy);
1348 [ + - ]: 426848 : sk_mem_charge(ssk, copy);
1349 [ - + - + ]: 426848 : WRITE_ONCE(tcp_sk(ssk)->write_seq, tcp_sk(ssk)->write_seq + copy);
1350 : 426848 : TCP_SKB_CB(skb)->end_seq += copy;
1351 [ + + ]: 426848 : tcp_skb_pcount_set(skb, 0);
1352 : :
1353 : : /* on skb reuse we just need to update the DSS len */
1354 [ + + ]: 426848 : if (reuse_skb) {
1355 : 211511 : TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
1356 : 211511 : mpext->data_len += copy;
1357 : 211511 : goto out;
1358 : : }
1359 : :
1360 : 215337 : memset(mpext, 0, sizeof(*mpext));
1361 : 215337 : mpext->data_seq = data_seq;
1362 [ - + ]: 215337 : mpext->subflow_seq = mptcp_subflow_ctx(ssk)->rel_write_seq;
1363 : 215337 : mpext->data_len = copy;
1364 : 215337 : mpext->use_map = 1;
1365 : 215337 : mpext->dsn64 = 1;
1366 : :
1367 [ - + ]: 215337 : 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 [ + + ]: 215337 : if (zero_window_probe) {
1372 [ + - ]: 4406 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_WINPROBE);
1373 [ + + ]: 4406 : mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1374 : 4406 : mpext->frozen = 1;
1375 [ - + + + ]: 4406 : if (READ_ONCE(msk->csum_enabled))
[ + + ]
1376 : 3930 : mptcp_update_data_checksum(skb, copy);
1377 : 4406 : tcp_push_pending_frames(ssk);
1378 : 4406 : return 0;
1379 : : }
1380 : 210931 : out:
1381 [ - + + + ]: 422442 : if (READ_ONCE(msk->csum_enabled))
[ + + ]
1382 : 173055 : mptcp_update_data_checksum(skb, copy);
1383 [ + + ]: 422442 : if (mptcp_subflow_ctx(ssk)->send_infinite_map)
1384 : 2 : mptcp_update_infinite_map(msk, ssk, mpext);
1385 : 422442 : trace_mptcp_sendmsg_frag(mpext);
1386 : 422442 : mptcp_subflow_ctx(ssk)->rel_write_seq += copy;
1387 : 422442 : 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 : 137 : void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow)
1402 : : {
1403 [ - + ]: 137 : 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 : 1105875 : bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
1411 : : {
1412 [ + + ]: 1105875 : if (unlikely(subflow->stale)) {
1413 [ - + ]: 67639 : u32 rcv_tstamp = READ_ONCE(tcp_sk(mptcp_subflow_tcp_sock(subflow))->rcv_tstamp);
1414 : :
1415 [ - + ]: 67639 : if (subflow->stale_rcv_tstamp == rcv_tstamp)
1416 : : return false;
1417 : :
1418 : 0 : mptcp_subflow_set_active(subflow);
1419 : : }
1420 : 1038236 : 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 : 505317 : struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
1432 : : {
1433 : 505317 : struct subflow_send_info send_info[SSK_MODE_MAX];
1434 : 505317 : struct mptcp_subflow_context *subflow;
1435 : 505317 : struct sock *sk = (struct sock *)msk;
1436 : 505317 : u32 pace, burst, wmem;
1437 : 505317 : int i, nr_active = 0;
1438 : 505317 : struct sock *ssk;
1439 : 505317 : u64 linger_time;
1440 : 505317 : long tout = 0;
1441 : :
1442 : : /* pick the subflow with the lower wmem/wspace ratio */
1443 [ + + ]: 1515951 : for (i = 0; i < SSK_MODE_MAX; ++i) {
1444 : 1010634 : send_info[i].ssk = NULL;
1445 : 1010634 : send_info[i].linger_time = -1;
1446 : : }
1447 : :
1448 [ + + ]: 1330533 : mptcp_for_each_subflow(msk, subflow) {
1449 [ + + + + ]: 825216 : bool backup = subflow->backup || subflow->request_bkup;
1450 : :
1451 : 825216 : trace_mptcp_subflow_get_send(subflow);
1452 : 825216 : ssk = mptcp_subflow_tcp_sock(subflow);
1453 [ + + ]: 825216 : if (!mptcp_subflow_active(subflow))
1454 : 69290 : continue;
1455 : :
1456 [ + + ]: 755926 : tout = max(tout, mptcp_timeout_from_subflow(subflow));
1457 : 755926 : nr_active += !backup;
1458 : 755926 : pace = subflow->avg_pacing_rate;
1459 [ + + ]: 755926 : 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 [ + + ]: 755926 : linger_time = div_u64((u64)READ_ONCE(ssk->sk_wmem_queued) << 32, pace);
1468 [ + + ]: 755926 : if (linger_time < send_info[backup].linger_time) {
1469 : 613051 : send_info[backup].ssk = ssk;
1470 : 613051 : send_info[backup].linger_time = linger_time;
1471 : : }
1472 : : }
1473 : 505317 : __mptcp_set_timeout(sk, tout);
1474 : :
1475 : : /* pick the best backup if no other subflow is active */
1476 [ + + ]: 505317 : if (!nr_active)
1477 : 22434 : 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 : 505317 : ssk = send_info[SSK_MODE_ACTIVE].ssk;
1491 [ + + + + ]: 1010622 : if (!ssk || !sk_stream_memory_free(ssk))
1492 : 86621 : return NULL;
1493 : :
1494 : 418696 : burst = min_t(int, MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
1495 : 418696 : wmem = READ_ONCE(ssk->sk_wmem_queued);
1496 [ + + ]: 418696 : if (!burst)
1497 : : return ssk;
1498 : :
1499 : 260724 : subflow = mptcp_subflow_ctx(ssk);
1500 : 260724 : subflow->avg_pacing_rate = div_u64((u64)subflow->avg_pacing_rate * wmem +
1501 : 260724 : READ_ONCE(ssk->sk_pacing_rate) * burst,
1502 : : burst + wmem);
1503 : 260724 : msk->snd_burst = burst;
1504 : 260724 : return ssk;
1505 : : }
1506 : :
1507 : 345216 : static void mptcp_push_release(struct sock *ssk, struct mptcp_sendmsg_info *info)
1508 : : {
1509 [ - + ]: 345216 : tcp_push(ssk, 0, info->mss_now, tcp_sk(ssk)->nonagle, info->size_goal);
1510 : 345216 : release_sock(ssk);
1511 : 345216 : }
1512 : :
1513 : 421403 : static void mptcp_update_post_push(struct mptcp_sock *msk,
1514 : : struct mptcp_data_frag *dfrag,
1515 : : u32 sent)
1516 : : {
1517 : 421403 : u64 snd_nxt_new = dfrag->data_seq;
1518 : :
1519 : 421403 : dfrag->already_sent += sent;
1520 : :
1521 : 421403 : msk->snd_burst -= sent;
1522 : :
1523 : 421403 : 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 [ + + ]: 421403 : if (likely(after64(snd_nxt_new, msk->snd_nxt))) {
1535 : 416398 : msk->bytes_sent += snd_nxt_new - msk->snd_nxt;
1536 : 416398 : WRITE_ONCE(msk->snd_nxt, snd_nxt_new);
1537 : : }
1538 : 421403 : }
1539 : :
1540 : 13128 : void mptcp_check_and_set_pending(struct sock *sk)
1541 : : {
1542 [ + + ]: 13128 : if (mptcp_send_head(sk)) {
1543 : 4929 : mptcp_data_lock(sk);
1544 [ - + ]: 4929 : mptcp_sk(sk)->cb_flags |= BIT(MPTCP_PUSH_PENDING);
1545 : 4929 : mptcp_data_unlock(sk);
1546 : : }
1547 : 13128 : }
1548 : :
1549 : 462174 : static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
1550 : : struct mptcp_sendmsg_info *info)
1551 : : {
1552 [ - + ]: 462174 : struct mptcp_sock *msk = mptcp_sk(sk);
1553 : 24 : struct mptcp_data_frag *dfrag;
1554 : 24 : int len, copied = 0, err = 0;
1555 : :
1556 [ + + ]: 741345 : while ((dfrag = mptcp_send_head(sk))) {
1557 : 548932 : info->sent = dfrag->already_sent;
1558 : 548932 : info->limit = dfrag->data_len;
1559 : 548932 : len = dfrag->data_len - dfrag->already_sent;
1560 [ + + ]: 970335 : while (len > 0) {
1561 : 627935 : int ret = 0;
1562 : :
1563 : 627935 : ret = mptcp_sendmsg_frag(sk, ssk, dfrag, info);
1564 [ + + ]: 627935 : if (ret <= 0) {
1565 [ + + ]: 206532 : err = copied ? : ret;
1566 : 206532 : goto out;
1567 : : }
1568 : :
1569 : 421403 : info->sent += ret;
1570 : 421403 : copied += ret;
1571 : 421403 : len -= ret;
1572 : :
1573 : 421403 : mptcp_update_post_push(msk, dfrag, ret);
1574 : : }
1575 : 342400 : msk->first_pending = mptcp_send_next(sk);
1576 : :
1577 [ + + + + ]: 628561 : if (msk->snd_burst <= 0 ||
[ + - ]
1578 [ - + ]: 279183 : !sk_stream_memory_free(ssk) ||
[ + - - + ]
1579 : 279171 : !mptcp_subflow_active(mptcp_subflow_ctx(ssk))) {
1580 : 63229 : err = copied;
1581 : 63229 : goto out;
1582 : : }
1583 : 279171 : mptcp_set_timeout(sk);
1584 : : }
1585 : : err = copied;
1586 : :
1587 : 462174 : out:
1588 [ + + ]: 462174 : if (err > 0)
1589 : 304678 : msk->last_data_sent = tcp_jiffies32;
1590 : 462174 : return err;
1591 : : }
1592 : :
1593 : 478995 : void __mptcp_push_pending(struct sock *sk, unsigned int flags)
1594 : : {
1595 : 478995 : struct sock *prev_ssk = NULL, *ssk = NULL;
1596 [ - + ]: 478995 : struct mptcp_sock *msk = mptcp_sk(sk);
1597 : 478995 : struct mptcp_sendmsg_info info = {
1598 : : .flags = flags,
1599 : : };
1600 : 478995 : bool copied = false;
1601 : 478995 : int push_count = 1;
1602 : :
1603 [ + + + + ]: 843935 : while (mptcp_send_head(sk) && (push_count > 0)) {
1604 : 411607 : struct mptcp_subflow_context *subflow;
1605 : 411607 : int ret = 0;
1606 : :
1607 [ + + ]: 411607 : if (mptcp_sched_get_send(msk))
1608 : : break;
1609 : :
1610 : 364940 : push_count = 0;
1611 : :
1612 [ + + ]: 850692 : mptcp_for_each_subflow(msk, subflow) {
1613 [ - + + + ]: 485752 : if (READ_ONCE(subflow->scheduled)) {
[ + + ]
1614 : 364940 : mptcp_subflow_set_scheduled(subflow, false);
1615 : :
1616 : 364940 : prev_ssk = ssk;
1617 [ + + ]: 364940 : ssk = mptcp_subflow_tcp_sock(subflow);
1618 [ + + ]: 364940 : if (ssk != prev_ssk) {
1619 : : /* First check. If the ssk has changed since
1620 : : * the last round, release prev_ssk
1621 : : */
1622 [ + + ]: 345216 : if (prev_ssk)
1623 : 548 : 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 : 345216 : lock_sock(ssk);
1630 : : }
1631 : :
1632 : 364940 : push_count++;
1633 : :
1634 : 364940 : ret = __subflow_push_pending(sk, ssk, &info);
1635 [ + + ]: 364940 : if (ret <= 0) {
1636 [ - + - - ]: 116648 : if (ret != -EAGAIN ||
[ - + ]
1637 [ # # ]: 0 : (1 << ssk->sk_state) &
1638 : : (TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | TCPF_CLOSE))
1639 : 36706 : push_count--;
1640 : 116648 : 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 [ + + ]: 478995 : if (ssk)
1649 : 344668 : 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 [ + + ]: 478995 : if (copied) {
1655 [ + + ]: 235703 : if (!mptcp_rtx_timer_pending(sk))
1656 : 41353 : mptcp_reset_rtx_timer(sk);
1657 : 235703 : mptcp_check_send_data_fin(sk);
1658 : : }
1659 : 478995 : }
1660 : :
1661 : 825280 : static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk, bool first)
1662 : : {
1663 [ - + ]: 825280 : struct mptcp_sock *msk = mptcp_sk(sk);
1664 : 825280 : struct mptcp_sendmsg_info info = {
1665 : : .data_lock_held = true,
1666 : : };
1667 : 825280 : bool keep_pushing = true;
1668 : 825280 : struct sock *xmit_ssk;
1669 : 825280 : int copied = 0;
1670 : :
1671 : 825280 : info.flags = 0;
1672 [ + + + + ]: 925345 : while (mptcp_send_head(sk) && keep_pushing) {
1673 [ + + ]: 152317 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1674 : 152317 : int ret = 0;
1675 : :
1676 : : /* check for a different subflow usage only after
1677 : : * spooling the first chunk of data
1678 : : */
1679 [ + + ]: 152317 : if (first) {
1680 : 3477 : mptcp_subflow_set_scheduled(subflow, false);
1681 : 3477 : ret = __subflow_push_pending(sk, ssk, &info);
1682 : 3477 : first = false;
1683 [ + + ]: 3477 : if (ret <= 0)
1684 : : break;
1685 : 2212 : copied += ret;
1686 : 2212 : continue;
1687 : : }
1688 : :
1689 [ + + ]: 148840 : if (mptcp_sched_get_send(msk))
1690 : 50987 : goto out;
1691 : :
1692 [ - + + + ]: 97853 : if (READ_ONCE(subflow->scheduled)) {
[ + + ]
1693 : 93757 : mptcp_subflow_set_scheduled(subflow, false);
1694 : 93757 : ret = __subflow_push_pending(sk, ssk, &info);
1695 [ + + ]: 93757 : if (ret <= 0)
1696 : 39583 : keep_pushing = false;
1697 : 93757 : copied += ret;
1698 : : }
1699 : :
1700 [ + + ]: 221049 : mptcp_for_each_subflow(msk, subflow) {
1701 [ - + + + ]: 123196 : if (READ_ONCE(subflow->scheduled)) {
[ + + ]
1702 [ + - ]: 4096 : xmit_ssk = mptcp_subflow_tcp_sock(subflow);
1703 [ + - ]: 4096 : if (xmit_ssk != ssk) {
1704 : 4096 : mptcp_subflow_delegate(subflow,
1705 : : MPTCP_DELEGATE_SEND);
1706 : 4096 : keep_pushing = false;
1707 : : }
1708 : : }
1709 : : }
1710 : : }
1711 : :
1712 : 774250 : 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 [ + + ]: 825280 : if (copied) {
1717 [ - + ]: 50913 : tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
1718 : : info.size_goal);
1719 [ + + ]: 50913 : if (!mptcp_rtx_timer_pending(sk))
1720 : 151 : mptcp_reset_rtx_timer(sk);
1721 : :
1722 [ - + + + ]: 50913 : if (msk->snd_data_fin_enable &&
[ + + ]
1723 [ + + ]: 8865 : msk->snd_nxt + 1 == msk->write_seq)
1724 : 519 : mptcp_schedule_work(sk);
1725 : : }
1726 : 825280 : }
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 : 484937 : static int do_copy_data_nocache(struct sock *sk, int copy,
1789 : : struct iov_iter *from, char *to)
1790 : : {
1791 [ - + ]: 484937 : 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 [ - + ]: 484937 : } 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 : 487555 : static u32 mptcp_send_limit(const struct sock *sk)
1806 : : {
1807 [ - + ]: 487555 : const struct mptcp_sock *msk = mptcp_sk(sk);
1808 : 487555 : u32 limit, not_sent;
1809 : :
1810 [ + + ]: 487555 : if (sk->sk_wmem_queued >= READ_ONCE(sk->sk_sndbuf))
1811 : : return 0;
1812 : :
1813 : 484937 : limit = mptcp_notsent_lowat(sk);
1814 [ - + ]: 484937 : 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 : 906783 : static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
1825 : : {
1826 : 906783 : struct mptcp_subflow_context *subflow;
1827 : :
1828 [ - + ]: 906783 : 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 : 375959 : static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1839 : : {
1840 [ - + ]: 375959 : struct mptcp_sock *msk = mptcp_sk(sk);
1841 : 375959 : struct page_frag *pfrag;
1842 : 375959 : size_t copied = 0;
1843 : 375959 : int ret = 0;
1844 : 375959 : long timeo;
1845 : :
1846 : : /* silently ignore everything else */
1847 : 375959 : msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_FASTOPEN;
1848 : :
1849 : 375959 : lock_sock(sk);
1850 : :
1851 : 375959 : mptcp_rps_record_subflows(msk);
1852 : :
1853 [ + + + + ]: 375959 : 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 [ + + ]: 375893 : timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1866 : :
1867 [ - + + + ]: 375893 : 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 : 375848 : ret = -EPIPE;
1874 [ + - - + ]: 375848 : if (unlikely(sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)))
1875 : 0 : goto do_error;
1876 : :
1877 [ + - ]: 375848 : pfrag = sk_page_frag(sk);
1878 : :
1879 [ + + ]: 863403 : while (msg_data_left(msg)) {
1880 : 487555 : int total_ts, frag_truesize = 0;
1881 : 487555 : struct mptcp_data_frag *dfrag;
1882 : 487555 : bool dfrag_collapsed;
1883 : 487555 : size_t psize, offset;
1884 : 487555 : u32 copy_limit;
1885 : :
1886 : : /* ensure fitting the notsent_lowat() constraint */
1887 : 487555 : copy_limit = mptcp_send_limit(sk);
1888 [ + + ]: 487555 : if (!copy_limit)
1889 : 2618 : goto wait_for_memory;
1890 : :
1891 : : /* reuse tail pfrag, if possible, or carve a new one from the
1892 : : * page allocator
1893 : : */
1894 : 484937 : dfrag = mptcp_pending_tail(sk);
1895 : 484937 : dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
1896 [ + + ]: 484937 : if (!dfrag_collapsed) {
1897 [ - + ]: 337652 : if (!mptcp_page_frag_refill(sk, pfrag))
1898 : 0 : goto wait_for_memory;
1899 : :
1900 : 337652 : dfrag = mptcp_carve_data_frag(msk, pfrag, pfrag->offset);
1901 : 337652 : 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 : 484937 : offset = dfrag->offset + dfrag->data_len;
1909 : 484937 : psize = pfrag->size - offset;
1910 : 484937 : psize = min_t(size_t, psize, msg_data_left(msg));
1911 : 484937 : psize = min_t(size_t, psize, copy_limit);
1912 : 484937 : total_ts = psize + frag_truesize;
1913 : :
1914 [ - + ]: 484937 : if (!sk_wmem_schedule(sk, total_ts))
1915 : 0 : goto wait_for_memory;
1916 : :
1917 : 484949 : ret = do_copy_data_nocache(sk, psize, &msg->msg_iter,
1918 : 484937 : page_address(dfrag->page) + offset);
1919 [ - + ]: 484937 : if (ret)
1920 : 0 : goto do_error;
1921 : :
1922 : : /* data successfully copied into the write queue */
1923 [ + + ]: 484937 : sk_forward_alloc_add(sk, -total_ts);
1924 : 484937 : copied += psize;
1925 : 484937 : dfrag->data_len += psize;
1926 : 484937 : frag_truesize += psize;
1927 : 484937 : pfrag->offset += frag_truesize;
1928 : 484937 : 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 [ + + ]: 484937 : sk_wmem_queued_add(sk, frag_truesize);
1934 [ + + ]: 484937 : if (!dfrag_collapsed) {
1935 : 337652 : get_page(dfrag->page);
1936 [ + + ]: 337652 : list_add_tail(&dfrag->list, &msk->rtx_queue);
1937 [ + + ]: 337652 : if (!msk->first_pending)
1938 : 229708 : msk->first_pending = dfrag;
1939 : : }
1940 [ - + ]: 484937 : 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 : 484937 : continue;
1945 : :
1946 : 2618 : wait_for_memory:
1947 : 2618 : set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1948 : 2618 : __mptcp_push_pending(sk, msg->msg_flags);
1949 : 2618 : ret = sk_stream_wait_memory(sk, &timeo);
1950 [ - + ]: 2618 : if (ret)
1951 : 0 : goto do_error;
1952 : : }
1953 : :
1954 [ + + ]: 375848 : if (copied)
1955 : 375821 : __mptcp_push_pending(sk, msg->msg_flags);
1956 : :
1957 : 27 : out:
1958 : 375959 : release_sock(sk);
1959 : 375959 : 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 : 538152 : 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 [ - + ]: 538152 : struct mptcp_sock *msk = mptcp_sk(sk);
1977 : 538152 : struct sk_buff *skb, *tmp;
1978 : 538152 : int total_data_len = 0;
1979 : 538152 : int copied = 0;
1980 : :
1981 [ + + ]: 798058 : skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) {
1982 : 633564 : u32 delta, offset = MPTCP_SKB_CB(skb)->offset;
1983 : 633564 : u32 data_len = skb->len - offset;
1984 : 633564 : u32 count;
1985 : 633564 : int err;
1986 : :
1987 [ + + ]: 633564 : if (flags & MSG_PEEK) {
1988 : : /* skip already peeked skbs */
1989 [ - + ]: 37004 : 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 : 37004 : delta = copied_total - total_data_len;
1996 : 37004 : offset += delta;
1997 : 37004 : data_len -= delta;
1998 : : }
1999 : :
2000 : 633564 : count = min_t(size_t, len - copied, data_len);
2001 [ + - ]: 633564 : if (!(flags & MSG_TRUNC)) {
2002 : 633564 : err = skb_copy_datagram_msg(skb, offset, msg, count);
2003 [ - + ]: 633564 : if (unlikely(err < 0)) {
2004 [ # # ]: 0 : if (!copied)
2005 : : return err;
2006 : : break;
2007 : : }
2008 : : }
2009 : :
2010 [ + + ]: 633564 : if (MPTCP_SKB_CB(skb)->has_rxtstamp) {
2011 : 622 : tcp_update_recv_tstamps(skb, tss);
2012 : 622 : *cmsg_flags |= MPTCP_CMSG_TS;
2013 : : }
2014 : :
2015 : 633564 : copied += count;
2016 : :
2017 [ + + ]: 633564 : if (!(flags & MSG_PEEK)) {
2018 : 596560 : msk->bytes_consumed += count;
2019 [ + + ]: 596560 : if (count < data_len) {
2020 : 311753 : MPTCP_SKB_CB(skb)->offset += count;
2021 : 311753 : MPTCP_SKB_CB(skb)->map_seq += count;
2022 : 311753 : break;
2023 : : }
2024 : :
2025 : : /* avoid the indirect call, we know the destructor is sock_rfree */
2026 : 284807 : skb->destructor = NULL;
2027 : 284807 : skb->sk = NULL;
2028 : 284807 : atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
2029 : 284807 : sk_mem_uncharge(sk, skb->truesize);
2030 : 284807 : __skb_unlink(skb, &sk->sk_receive_queue);
2031 : 284807 : skb_attempt_defer_free(skb);
2032 : : }
2033 : :
2034 [ + + ]: 321811 : if (copied >= len)
2035 : : break;
2036 : : }
2037 : :
2038 : 538152 : mptcp_rcv_space_adjust(msk, copied);
2039 : 538152 : 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 : 538152 : static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
2047 : : {
2048 : 538152 : struct mptcp_subflow_context *subflow;
2049 : 538152 : struct sock *sk = (struct sock *)msk;
2050 : 538152 : u8 scaling_ratio = U8_MAX;
2051 : 538152 : u32 time, advmss = 1;
2052 : 538152 : u64 rtt_us, mstamp;
2053 : :
2054 : 538152 : msk_owned_by_me(msk);
2055 : :
2056 [ + + ]: 538152 : if (copied <= 0)
2057 : : return;
2058 : :
2059 [ - + ]: 528927 : if (!msk->rcvspace_init)
2060 : 0 : mptcp_rcv_space_init(msk, msk->first);
2061 : :
2062 : 528927 : msk->rcvq_space.copied += copied;
2063 : :
2064 [ - + ]: 528927 : mstamp = div_u64(tcp_clock_ns(), NSEC_PER_USEC);
2065 [ + + ]: 528927 : time = tcp_stamp_us_delta(mstamp, msk->rcvq_space.time);
2066 : :
2067 : 528927 : rtt_us = msk->rcvq_space.rtt_us;
2068 [ + + + + ]: 528927 : if (rtt_us && time < (rtt_us >> 3))
2069 : : return;
2070 : :
2071 : 140892 : rtt_us = 0;
2072 [ + + ]: 300135 : mptcp_for_each_subflow(msk, subflow) {
2073 : 159243 : const struct tcp_sock *tp;
2074 : 159243 : u64 sf_rtt_us;
2075 : 159243 : u32 sf_advmss;
2076 : :
2077 [ - + ]: 159243 : tp = tcp_sk(mptcp_subflow_tcp_sock(subflow));
2078 : :
2079 : 159243 : sf_rtt_us = READ_ONCE(tp->rcv_rtt_est.rtt_us);
2080 : 159243 : sf_advmss = READ_ONCE(tp->advmss);
2081 : :
2082 : 159243 : rtt_us = max(sf_rtt_us, rtt_us);
2083 : 159243 : advmss = max(sf_advmss, advmss);
2084 : 159243 : scaling_ratio = min(tp->scaling_ratio, scaling_ratio);
2085 : : }
2086 : :
2087 : 140892 : msk->rcvq_space.rtt_us = rtt_us;
2088 : 140892 : msk->scaling_ratio = scaling_ratio;
2089 [ + + + + ]: 140892 : if (time < (rtt_us >> 3) || rtt_us == 0)
2090 : : return;
2091 : :
2092 [ + + ]: 125589 : if (msk->rcvq_space.copied <= msk->rcvq_space.space)
2093 : 121589 : goto new_measure;
2094 : :
2095 [ + + ]: 4000 : 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 [ + + ]: 3550 : mptcp_for_each_subflow(msk, subflow) {
2102 : 1815 : struct sock *ssk;
2103 : 1815 : bool slow;
2104 : :
2105 : 1815 : ssk = mptcp_subflow_tcp_sock(subflow);
2106 : 1815 : slow = lock_sock_fast(ssk);
2107 : : /* subflows can be added before tcp_init_transfer() */
2108 [ - + + - ]: 1815 : if (tcp_sk(ssk)->rcvq_space.space)
2109 : 1815 : tcp_rcvbuf_grow(ssk, msk->rcvq_space.copied);
2110 : 1815 : unlock_sock_fast(ssk, slow);
2111 : : }
2112 : : }
2113 : :
2114 : 4000 : new_measure:
2115 : 125589 : msk->rcvq_space.copied = 0;
2116 : 125589 : msk->rcvq_space.time = mstamp;
2117 : : }
2118 : :
2119 : : static struct mptcp_subflow_context *
2120 : 208400 : __mptcp_first_ready_from(struct mptcp_sock *msk,
2121 : : struct mptcp_subflow_context *subflow)
2122 : : {
2123 : 208400 : struct mptcp_subflow_context *start_subflow = subflow;
2124 : :
2125 [ - + + + ]: 449629 : while (!READ_ONCE(subflow->data_avail)) {
[ + + ]
2126 [ + + ]: 397924 : subflow = mptcp_next_subflow(msk, subflow);
2127 [ + + ]: 397924 : if (subflow == start_subflow)
2128 : : return NULL;
2129 : : }
2130 : : return subflow;
2131 : : }
2132 : :
2133 : 248170 : static bool __mptcp_move_skbs(struct sock *sk)
2134 : : {
2135 : 248170 : struct mptcp_subflow_context *subflow;
2136 [ - + ]: 248170 : struct mptcp_sock *msk = mptcp_sk(sk);
2137 : 248170 : bool ret = false;
2138 : :
2139 [ + + ]: 248170 : if (list_empty(&msk->conn_list))
2140 : : return false;
2141 : :
2142 : 248163 : subflow = list_first_entry(&msk->conn_list,
2143 : : struct mptcp_subflow_context, node);
2144 : 299868 : for (;;) {
2145 : 299868 : struct sock *ssk;
2146 : 299868 : 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 [ + + ]: 299868 : if (sk_rmem_alloc_get(sk) > sk->sk_rcvbuf)
2153 : : break;
2154 : :
2155 : 208403 : subflow = __mptcp_first_ready_from(msk, subflow);
2156 [ + + ]: 292269 : if (!subflow)
2157 : : break;
2158 : :
2159 : 51705 : ssk = mptcp_subflow_tcp_sock(subflow);
2160 : 51705 : slowpath = lock_sock_fast(ssk);
2161 [ + + + + ]: 51705 : ret = __mptcp_move_skbs_from_subflow(msk, ssk) || ret;
2162 [ - + ]: 51705 : if (unlikely(ssk->sk_err))
2163 : 0 : __mptcp_error_report(sk);
2164 : 51705 : unlock_sock_fast(ssk, slowpath);
2165 : :
2166 [ + + ]: 51705 : subflow = mptcp_next_subflow(msk, subflow);
2167 : : }
2168 : :
2169 : 248163 : __mptcp_ofo_queue(msk);
2170 [ + + ]: 248163 : if (ret)
2171 : 48749 : mptcp_check_data_fin((struct sock *)msk);
2172 : : return ret;
2173 : : }
2174 : :
2175 : 432 : static unsigned int mptcp_inq_hint(const struct sock *sk)
2176 : : {
2177 [ - + ]: 432 : const struct mptcp_sock *msk = mptcp_sk(sk);
2178 : 432 : const struct sk_buff *skb;
2179 : :
2180 [ - + ]: 432 : 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 [ + + + + ]: 432 : if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
2191 : 8 : return 1;
2192 : :
2193 : : return 0;
2194 : : }
2195 : :
2196 : 529482 : static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2197 : : int flags, int *addr_len)
2198 : : {
2199 [ - + ]: 529482 : struct mptcp_sock *msk = mptcp_sk(sk);
2200 : 529482 : struct scm_timestamping_internal tss;
2201 : 529482 : int copied = 0, cmsg_flags = 0;
2202 : 529482 : int target;
2203 : 529482 : long timeo;
2204 : :
2205 : : /* MSG_ERRQUEUE is really a no-op till we support IP_RECVERR */
2206 [ - + ]: 529482 : if (unlikely(flags & MSG_ERRQUEUE))
2207 : 0 : return inet_recv_error(sk, msg, len, addr_len);
2208 : :
2209 : 529482 : lock_sock(sk);
2210 [ - + ]: 529482 : if (unlikely(sk->sk_state == TCP_LISTEN)) {
2211 : 0 : copied = -ENOTCONN;
2212 : 0 : goto out_err;
2213 : : }
2214 : :
2215 : 529482 : mptcp_rps_record_subflows(msk);
2216 : :
2217 [ + + ]: 529482 : timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2218 : :
2219 : 529482 : len = min_t(size_t, len, INT_MAX);
2220 [ + - ]: 529482 : target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2221 : :
2222 [ + + ]: 529482 : if (unlikely(msk->recvmsg_inq))
2223 : 432 : cmsg_flags = MPTCP_CMSG_INQ;
2224 : :
2225 [ + + ]: 538249 : while (copied < len) {
2226 : 538152 : int err, bytes_read;
2227 : :
2228 : 538152 : bytes_read = __mptcp_recvmsg_mskq(sk, msg, len - copied, flags,
2229 : : copied, &tss, &cmsg_flags);
2230 [ - + ]: 538152 : if (unlikely(bytes_read < 0)) {
2231 [ # # ]: 0 : if (!copied)
2232 : 0 : copied = bytes_read;
2233 : 0 : goto out_err;
2234 : : }
2235 : :
2236 : 538152 : copied += bytes_read;
2237 : :
2238 [ + + + + ]: 538152 : if (skb_queue_empty(&sk->sk_receive_queue) && __mptcp_move_skbs(sk))
2239 : 1506 : continue;
2240 : :
2241 : : /* only the MPTCP socket status is relevant here. The exit
2242 : : * conditions mirror closely tcp_recvmsg()
2243 : : */
2244 [ + + ]: 536646 : if (copied >= target)
2245 : : break;
2246 : :
2247 [ - + ]: 9211 : 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 [ + + ]: 9211 : if (sk->sk_err) {
2256 : 1 : copied = sock_error(sk);
2257 : 1 : break;
2258 : : }
2259 : :
2260 [ + + ]: 9210 : if (sk->sk_shutdown & RCV_SHUTDOWN)
2261 : : break;
2262 : :
2263 [ + + ]: 7279 : if (sk->sk_state == TCP_CLOSE) {
2264 : : copied = -ENOTCONN;
2265 : : break;
2266 : : }
2267 : :
2268 [ + - ]: 7261 : if (!timeo) {
2269 : : copied = -EAGAIN;
2270 : : break;
2271 : : }
2272 : :
2273 [ - + ]: 7261 : if (signal_pending(current)) {
2274 [ # # ]: 0 : copied = sock_intr_errno(timeo);
2275 : : break;
2276 : : }
2277 : : }
2278 : :
2279 [ - + ]: 7261 : pr_debug("block timeout %ld\n", timeo);
2280 : 7261 : mptcp_cleanup_rbuf(msk, copied);
2281 : 7261 : err = sk_wait_data(sk, &timeo, NULL);
2282 [ - + ]: 7261 : if (err < 0) {
2283 : 0 : err = copied ? : err;
2284 : 0 : goto out_err;
2285 : : }
2286 : : }
2287 : :
2288 : 529482 : mptcp_cleanup_rbuf(msk, copied);
2289 : :
2290 : 529482 : out_err:
2291 [ + + + - ]: 529482 : if (cmsg_flags && copied >= 0) {
2292 [ + + ]: 606 : if (cmsg_flags & MPTCP_CMSG_TS)
2293 : 598 : tcp_recv_timestamp(msg, sk, &tss);
2294 : :
2295 [ + + ]: 606 : if (cmsg_flags & MPTCP_CMSG_INQ) {
2296 : 432 : unsigned int inq = mptcp_inq_hint(sk);
2297 : :
2298 : 432 : put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
2299 : : }
2300 : : }
2301 : :
2302 [ - + ]: 529482 : pr_debug("msk=%p rx queue empty=%d copied=%d\n",
2303 : : msk, skb_queue_empty(&sk->sk_receive_queue), copied);
2304 : :
2305 : 529482 : release_sock(sk);
2306 : 529482 : return copied;
2307 : : }
2308 : :
2309 : 14357 : static void mptcp_retransmit_timer(struct timer_list *t)
2310 : : {
2311 : 14357 : struct inet_connection_sock *icsk = timer_container_of(icsk, t,
2312 : : icsk_retransmit_timer);
2313 : 14357 : struct sock *sk = &icsk->icsk_inet.sk;
2314 [ - + ]: 14357 : struct mptcp_sock *msk = mptcp_sk(sk);
2315 : :
2316 : 14357 : bh_lock_sock(sk);
2317 [ + + ]: 14357 : if (!sock_owned_by_user(sk)) {
2318 : : /* we need a process context to retransmit */
2319 [ + + ]: 23495 : if (!test_and_set_bit(MPTCP_WORK_RTX, &msk->flags))
2320 : 13367 : mptcp_schedule_work(sk);
2321 : : } else {
2322 : : /* delegate our work to tcp_release_cb() */
2323 [ - + - - : 977 : __set_bit(MPTCP_RETRANSMIT, &msk->cb_flags);
- - ]
2324 : : }
2325 : 14357 : bh_unlock_sock(sk);
2326 : 14357 : sock_put(sk);
2327 : 14357 : }
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 : 5389 : struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
2343 : : {
2344 : 5389 : struct sock *backup = NULL, *pick = NULL;
2345 : 5389 : struct mptcp_subflow_context *subflow;
2346 : 5389 : int min_stale_count = INT_MAX;
2347 : :
2348 [ + + ]: 13295 : mptcp_for_each_subflow(msk, subflow) {
2349 : 7906 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2350 : :
2351 [ + + ]: 7906 : if (!__mptcp_subflow_active(subflow))
2352 : 1821 : continue;
2353 : :
2354 : : /* still data outstanding at TCP level? skip this */
2355 [ + + ]: 6085 : if (!tcp_rtx_and_write_queues_empty(ssk)) {
2356 : 2564 : mptcp_pm_subflow_chk_stale(msk, ssk);
2357 : 2564 : min_stale_count = min_t(int, min_stale_count, subflow->stale_count);
2358 : 2564 : continue;
2359 : : }
2360 : :
2361 [ + + + + ]: 3521 : if (subflow->backup || subflow->request_bkup) {
2362 [ + + ]: 67 : if (!backup)
2363 : 60 : backup = ssk;
2364 : 67 : continue;
2365 : : }
2366 : :
2367 [ + + ]: 3454 : if (!pick)
2368 : 2799 : pick = ssk;
2369 : : }
2370 : :
2371 [ + + ]: 5389 : if (pick)
2372 : : return pick;
2373 : :
2374 : : /* use backup only if there are no progresses anywhere */
2375 [ + + ]: 2590 : return min_stale_count > 1 ? backup : NULL;
2376 : : }
2377 : :
2378 : 1819 : bool __mptcp_retransmit_pending_data(struct sock *sk)
2379 : : {
2380 : 1819 : struct mptcp_data_frag *cur, *rtx_head;
2381 [ - + ]: 1819 : struct mptcp_sock *msk = mptcp_sk(sk);
2382 : :
2383 [ + + ]: 1819 : 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 : 1759 : mptcp_data_lock(sk);
2391 : 1759 : __mptcp_clean_una_wakeup(sk);
2392 : 1759 : rtx_head = mptcp_rtx_head(sk);
2393 [ + + ]: 1759 : if (!rtx_head) {
2394 : 1714 : mptcp_data_unlock(sk);
2395 : 1714 : 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 [ + + ]: 4875 : list_for_each_entry(cur, &msk->rtx_queue, list) {
2407 [ + + ]: 4838 : if (!cur->already_sent)
2408 : : break;
2409 : 4830 : 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 : 1432 : static void __mptcp_subflow_disconnect(struct sock *ssk,
2423 : : struct mptcp_subflow_context *subflow,
2424 : : unsigned int flags)
2425 : : {
2426 [ - + + + : 1432 : 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 [ - + ]: 1387 : WARN_ON_ONCE(tcp_disconnect(ssk, 0));
2432 : 1387 : mptcp_subflow_ctx_reset(subflow);
2433 : : } else {
2434 : 45 : tcp_shutdown(ssk, SEND_SHUTDOWN);
2435 : : }
2436 : 1432 : }
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 : 6684 : static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
2447 : : struct mptcp_subflow_context *subflow,
2448 : : unsigned int flags)
2449 : : {
2450 [ - + ]: 6684 : struct mptcp_sock *msk = mptcp_sk(sk);
2451 : 6684 : 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 [ + + + + : 6721 : if (msk->in_accept_queue && msk->first == ssk &&
+ + ]
2459 [ - + ]: 38 : (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 [ + + + + ]: 6648 : dispose_it = msk->free_first || ssk != msk->first;
2469 : : if (dispose_it)
2470 : 5216 : list_del(&subflow->node);
2471 : :
2472 : 6648 : lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
2473 : :
2474 [ + + + + ]: 6648 : if (subflow->send_fastclose && ssk->sk_state != TCP_CLOSE)
2475 : 392 : tcp_set_state(ssk, TCP_CLOSE);
2476 : :
2477 [ + + + + ]: 6648 : need_push = (flags & MPTCP_CF_PUSH) && __mptcp_retransmit_pending_data(sk);
2478 [ + + ]: 6648 : if (!dispose_it) {
2479 : 1432 : __mptcp_subflow_disconnect(ssk, subflow, flags);
2480 : 1432 : release_sock(ssk);
2481 : :
2482 : 1432 : goto out;
2483 : : }
2484 : :
2485 : 5216 : 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 [ - + ]: 5216 : 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 : 5216 : __tcp_close(ssk, 0);
2497 : :
2498 : : /* close acquired an extra ref */
2499 : 5216 : __sock_put(ssk);
2500 : : }
2501 : :
2502 : 5252 : out_release:
2503 : 5252 : __mptcp_subflow_error_report(sk, ssk);
2504 : 5252 : release_sock(ssk);
2505 : :
2506 : 5252 : sock_put(ssk);
2507 : :
2508 [ + + ]: 5252 : if (ssk == msk->first)
2509 : 4292 : WRITE_ONCE(msk->first, NULL);
2510 : :
2511 : 960 : out:
2512 : 6684 : __mptcp_sync_sndbuf(sk);
2513 [ + + ]: 6684 : 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 [ + + + + : 9944 : if (list_is_singular(&msk->conn_list) && msk->first &&
+ + ]
[ + + + + ]
2522 [ + - ]: 30 : inet_sk_state_load(msk->first) == TCP_CLOSE) {
2523 [ + + + + ]: 1372 : if (sk->sk_state != TCP_ESTABLISHED ||
2524 [ - + ]: 33 : msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
2525 : 1339 : mptcp_set_state(sk, TCP_CLOSE);
2526 : 1339 : mptcp_close_wake_up(sk);
2527 : : } else {
2528 : 33 : mptcp_start_tout_timer(sk);
2529 : : }
2530 : : }
2531 : 6684 : }
2532 : :
2533 : 1847 : 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 [ + + ]: 1847 : if (subflow->close_event_done)
2538 : : return;
2539 : :
2540 : 1805 : subflow->close_event_done = true;
2541 : :
2542 [ + + ]: 1805 : if (sk->sk_state == TCP_ESTABLISHED)
2543 [ - + ]: 317 : 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 [ - + ]: 1805 : mptcp_pm_subflow_check_next(mptcp_sk(sk), subflow);
2549 : :
2550 : 1805 : __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 : 1372 : static void __mptcp_close_subflow(struct sock *sk)
2559 : : {
2560 : 1372 : struct mptcp_subflow_context *subflow, *tmp;
2561 [ - + ]: 1372 : struct mptcp_sock *msk = mptcp_sk(sk);
2562 : :
2563 : 1372 : might_sleep();
2564 : :
2565 [ + + ]: 3344 : mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2566 : 1972 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2567 : 1972 : int ssk_state = inet_sk_state_load(ssk);
2568 : :
2569 [ + + + + ]: 1972 : if (ssk_state != TCP_CLOSE &&
2570 [ + + ]: 69 : (ssk_state != TCP_CLOSE_WAIT ||
2571 [ - + ]: 42 : inet_sk_state_load(sk) != TCP_ESTABLISHED ||
[ # # # # ]
2572 : 42 : __mptcp_check_fallback(msk)))
2573 : 316 : continue;
2574 : :
2575 : : /* 'subflow_data_ready' will re-sched once rx queue is empty */
2576 [ + + ]: 1656 : if (!skb_queue_empty_lockless(&ssk->sk_receive_queue))
2577 : 1 : continue;
2578 : :
2579 : 1655 : mptcp_close_ssk(sk, ssk, subflow);
2580 : : }
2581 : :
2582 : 1372 : }
2583 : :
2584 : 19850 : static bool mptcp_close_tout_expired(const struct sock *sk)
2585 : : {
2586 [ + + ]: 19850 : if (!inet_csk(sk)->icsk_mtup.probe_timestamp ||
2587 [ + + ]: 4752 : sk->sk_state == TCP_CLOSE)
2588 : : return false;
2589 : :
2590 : 3725 : return time_after32(tcp_jiffies32,
2591 : : inet_csk(sk)->icsk_mtup.probe_timestamp + mptcp_close_timeout(sk));
2592 : : }
2593 : :
2594 : 19850 : static void mptcp_check_fastclose(struct mptcp_sock *msk)
2595 : : {
2596 : 19850 : struct mptcp_subflow_context *subflow, *tmp;
2597 : 19850 : struct sock *sk = (struct sock *)msk;
2598 : :
2599 [ - + + + ]: 19850 : if (likely(!READ_ONCE(msk->rcv_fastclose)))
[ + + ]
2600 : : return;
2601 : :
2602 : 237 : mptcp_token_destroy(msk);
2603 : :
2604 [ + + ]: 518 : mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2605 : 281 : struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
2606 : 281 : bool slow;
2607 : :
2608 : 281 : slow = lock_sock_fast(tcp_sk);
2609 [ + + ]: 281 : if (tcp_sk->sk_state != TCP_CLOSE) {
2610 : 34 : mptcp_send_active_reset_reason(tcp_sk);
2611 : 34 : tcp_set_state(tcp_sk, TCP_CLOSE);
2612 : : }
2613 : 281 : unlock_sock_fast(tcp_sk, slow);
2614 : : }
2615 : :
2616 : : /* Mirror the tcp_reset() error propagation */
2617 [ - + + - ]: 237 : 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 : 14 : WRITE_ONCE(sk->sk_err, EPIPE);
2623 : 14 : break;
2624 : : case TCP_CLOSE:
2625 : : return;
2626 : : default:
2627 : 223 : WRITE_ONCE(sk->sk_err, ECONNRESET);
2628 : : }
2629 : :
2630 : 237 : mptcp_set_state(sk, TCP_CLOSE);
2631 : 237 : WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
2632 : 237 : smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
2633 : 237 : set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags);
2634 : :
2635 : : /* the calling mptcp_worker will properly destroy the socket */
2636 [ + + ]: 237 : if (sock_flag(sk, SOCK_DEAD))
2637 : : return;
2638 : :
2639 : 135 : sk->sk_state_change(sk);
2640 : 135 : sk_error_report(sk);
2641 : : }
2642 : :
2643 : 14154 : static void __mptcp_retrans(struct sock *sk)
2644 : : {
2645 : 14154 : struct mptcp_sendmsg_info info = { .data_lock_held = true, };
2646 [ - + ]: 14154 : struct mptcp_sock *msk = mptcp_sk(sk);
2647 : 14154 : struct mptcp_subflow_context *subflow;
2648 : 14154 : struct mptcp_data_frag *dfrag;
2649 : 14154 : struct sock *ssk;
2650 : 14154 : int ret, err;
2651 : 14154 : u16 len = 0;
2652 : :
2653 : 14154 : mptcp_clean_una_wakeup(sk);
2654 : :
2655 : : /* first check ssk: need to kick "stale" logic */
2656 : 14154 : err = mptcp_sched_get_retrans(msk);
2657 : 14154 : dfrag = mptcp_rtx_head(sk);
2658 [ + + ]: 14154 : if (!dfrag) {
2659 [ + + ]: 5922 : if (mptcp_data_fin_enabled(msk)) {
2660 : 1278 : struct inet_connection_sock *icsk = inet_csk(sk);
2661 : :
2662 : 1278 : WRITE_ONCE(icsk->icsk_retransmits,
2663 : : icsk->icsk_retransmits + 1);
2664 : 1278 : mptcp_set_datafin_timeout(sk);
2665 : 1278 : mptcp_send_ack(msk);
2666 : :
2667 : 1278 : goto reset_timer;
2668 : : }
2669 : :
2670 [ + + ]: 4644 : if (!mptcp_send_head(sk))
2671 : 1044 : goto clear_scheduled;
2672 : :
2673 : 3600 : goto reset_timer;
2674 : : }
2675 : :
2676 [ + + ]: 8232 : if (err)
2677 : 7198 : goto reset_timer;
2678 : :
2679 [ + + ]: 2461 : mptcp_for_each_subflow(msk, subflow) {
2680 [ - + + + ]: 1427 : if (READ_ONCE(subflow->scheduled)) {
[ + + ]
2681 : 1034 : u16 copied = 0;
2682 : :
2683 : 1034 : mptcp_subflow_set_scheduled(subflow, false);
2684 : :
2685 : 1034 : ssk = mptcp_subflow_tcp_sock(subflow);
2686 : :
2687 : 1034 : lock_sock(ssk);
2688 : :
2689 : : /* limit retransmission to the bytes already sent on some subflows */
2690 : 1034 : info.sent = 0;
2691 [ - + + + ]: 1034 : 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 : 1034 : spin_lock_bh(&msk->fallback_lock);
2699 [ - + ]: 1034 : 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 [ + + ]: 2073 : while (info.sent < info.limit) {
2706 : 1045 : ret = mptcp_sendmsg_frag(sk, ssk, dfrag, &info);
2707 [ + + ]: 1045 : if (ret <= 0)
2708 : : break;
2709 : :
2710 [ + - ]: 1039 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RETRANSSEGS);
2711 : 1039 : copied += ret;
2712 : 1039 : info.sent += ret;
2713 : : }
2714 [ + - ]: 1034 : if (copied) {
2715 : 1034 : len = max(copied, len);
2716 [ - + ]: 1034 : tcp_push(ssk, 0, info.mss_now, tcp_sk(ssk)->nonagle,
2717 : : info.size_goal);
2718 : 1034 : msk->allow_infinite_fallback = false;
2719 : : }
2720 : 1034 : spin_unlock_bh(&msk->fallback_lock);
2721 : :
2722 : 1034 : release_sock(ssk);
2723 : : }
2724 : : }
2725 : :
2726 : 1034 : msk->bytes_retrans += len;
2727 : 1034 : dfrag->already_sent = max(dfrag->already_sent, len);
2728 : :
2729 : 13110 : reset_timer:
2730 : 13110 : mptcp_check_and_set_pending(sk);
2731 : :
2732 [ + + ]: 13110 : if (!mptcp_rtx_timer_pending(sk))
2733 : 64 : mptcp_reset_rtx_timer(sk);
2734 : :
2735 : 13046 : 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 [ + + ]: 30825 : mptcp_for_each_subflow(msk, subflow)
2741 [ - + + + ]: 16671 : if (READ_ONCE(subflow->scheduled))
[ + + ]
2742 : 1785 : mptcp_subflow_set_scheduled(subflow, false);
2743 : 14154 : }
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 : 1250 : void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout)
2749 : : {
2750 : 1250 : struct sock *sk = (struct sock *)msk;
2751 : 1250 : unsigned long timeout, close_timeout;
2752 : :
2753 [ + + + - ]: 1250 : if (!fail_tout && !inet_csk(sk)->icsk_mtup.probe_timestamp)
2754 : : return;
2755 : :
2756 : 2500 : close_timeout = (unsigned long)inet_csk(sk)->icsk_mtup.probe_timestamp -
2757 : 1250 : 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 [ + + ]: 1250 : timeout = inet_csk(sk)->icsk_mtup.probe_timestamp ? close_timeout : fail_tout;
2763 : :
2764 : 1250 : 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 : 511 : static void mptcp_do_fastclose(struct sock *sk)
2784 : : {
2785 : 511 : struct mptcp_subflow_context *subflow, *tmp;
2786 [ - + ]: 511 : struct mptcp_sock *msk = mptcp_sk(sk);
2787 : :
2788 : 511 : mptcp_set_state(sk, TCP_CLOSE);
2789 : :
2790 : : /* Explicitly send the fastclose reset as need */
2791 [ + + ]: 511 : if (__mptcp_check_fallback(msk))
2792 : : return;
2793 : :
2794 [ + + ]: 956 : mptcp_for_each_subflow_safe(msk, subflow, tmp) {
2795 : 516 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
2796 : :
2797 : 516 : lock_sock(ssk);
2798 : :
2799 : : /* Some subflow socket states don't allow/need a reset.*/
2800 [ - + + + ]: 516 : if ((1 << ssk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
[ + + ]
2801 : 90 : goto unlock;
2802 : :
2803 : 426 : subflow->send_fastclose = 1;
2804 : 426 : tcp_send_active_reset(ssk, ssk->sk_allocation,
2805 : : SK_RST_REASON_TCP_ABORT_ON_CLOSE);
2806 : 516 : unlock:
2807 : 516 : release_sock(ssk);
2808 : : }
2809 : : }
2810 : :
2811 : 20812 : static void mptcp_worker(struct work_struct *work)
2812 : : {
2813 : 20812 : struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work);
2814 : 20812 : struct sock *sk = (struct sock *)msk;
2815 : 20812 : unsigned long fail_tout;
2816 : 20812 : int state;
2817 : :
2818 : 20812 : lock_sock(sk);
2819 : 20812 : state = sk->sk_state;
2820 [ - + + + ]: 20812 : if (unlikely((1 << state) & (TCPF_CLOSE | TCPF_LISTEN)))
[ + + ]
2821 : 962 : goto unlock;
2822 : :
2823 : 19850 : mptcp_check_fastclose(msk);
2824 : :
2825 : 19850 : mptcp_pm_worker(msk);
2826 : :
2827 : 19850 : mptcp_check_send_data_fin(sk);
2828 : 19850 : mptcp_check_data_fin_ack(sk);
2829 : 19850 : mptcp_check_data_fin(sk);
2830 : :
2831 [ + + ]: 33089 : if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
2832 : 1372 : __mptcp_close_subflow(sk);
2833 : :
2834 [ + + ]: 19850 : if (mptcp_close_tout_expired(sk)) {
2835 : 136 : struct mptcp_subflow_context *subflow, *tmp;
2836 : :
2837 : 136 : mptcp_do_fastclose(sk);
2838 [ + + ]: 294 : mptcp_for_each_subflow_safe(msk, subflow, tmp)
2839 : 158 : __mptcp_close_ssk(sk, subflow->tcp_sock, subflow, 0);
2840 : 136 : mptcp_close_wake_up(sk);
2841 : : }
2842 : :
2843 [ + + + + ]: 19850 : if (sock_flag(sk, SOCK_DEAD) && sk->sk_state == TCP_CLOSE) {
2844 : 1151 : __mptcp_destroy_sock(sk);
2845 : 1151 : goto unlock;
2846 : : }
2847 : :
2848 [ + + ]: 31339 : if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
2849 : 13177 : __mptcp_retrans(sk);
2850 : :
2851 [ + - + - ]: 18699 : fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
2852 [ + - - - ]: 18699 : if (fail_tout && time_after(jiffies, fail_tout))
2853 : 0 : mptcp_mp_fail_no_response(msk);
2854 : :
2855 : 0 : unlock:
2856 : 20812 : release_sock(sk);
2857 : 20812 : sock_put(sk);
2858 : 20812 : }
2859 : :
2860 : 4392 : static void __mptcp_init_sock(struct sock *sk)
2861 : : {
2862 [ - + ]: 4392 : struct mptcp_sock *msk = mptcp_sk(sk);
2863 : :
2864 : 4392 : INIT_LIST_HEAD(&msk->conn_list);
2865 : 4392 : INIT_LIST_HEAD(&msk->join_list);
2866 : 4392 : INIT_LIST_HEAD(&msk->rtx_queue);
2867 : 4392 : INIT_WORK(&msk->work, mptcp_worker);
2868 : 4392 : msk->out_of_order_queue = RB_ROOT;
2869 : 4392 : msk->first_pending = NULL;
2870 : 4392 : msk->timer_ival = TCP_RTO_MIN;
2871 : 4392 : msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
2872 : :
2873 : 4392 : WRITE_ONCE(msk->first, NULL);
2874 : 4392 : inet_csk(sk)->icsk_sync_mss = mptcp_sync_mss;
2875 : 4392 : WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
2876 : 4392 : msk->allow_infinite_fallback = true;
2877 : 4392 : msk->allow_subflows = true;
2878 : 4392 : msk->recovery = false;
2879 : 4392 : msk->subflow_id = 1;
2880 : 4392 : msk->last_data_sent = tcp_jiffies32;
2881 : 4392 : msk->last_data_recv = tcp_jiffies32;
2882 : 4392 : msk->last_ack_recv = tcp_jiffies32;
2883 : :
2884 : 4392 : mptcp_pm_data_init(msk);
2885 : 4392 : spin_lock_init(&msk->fallback_lock);
2886 : :
2887 : : /* re-use the csk retrans timer for MPTCP-level retrans */
2888 : 4392 : timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
2889 : 4392 : timer_setup(&sk->sk_timer, mptcp_tout_timer, 0);
2890 : 4392 : }
2891 : :
2892 : 3096 : static void mptcp_ca_reset(struct sock *sk)
2893 : : {
2894 : 3096 : struct inet_connection_sock *icsk = inet_csk(sk);
2895 : :
2896 : 3096 : tcp_assign_congestion_control(sk);
2897 [ - + ]: 3096 : strscpy(mptcp_sk(sk)->ca_name, icsk->icsk_ca_ops->name,
2898 : : sizeof(mptcp_sk(sk)->ca_name));
2899 : :
2900 : : /* no need to keep a reference to the ops, the name will suffice */
2901 : 3096 : tcp_cleanup_congestion_control(sk);
2902 : 3096 : icsk->icsk_ca_ops = NULL;
2903 : 3096 : }
2904 : :
2905 : 3014 : static int mptcp_init_sock(struct sock *sk)
2906 : : {
2907 : 3014 : struct net *net = sock_net(sk);
2908 : 3014 : int ret;
2909 : :
2910 : 3014 : __mptcp_init_sock(sk);
2911 : :
2912 [ + + ]: 3014 : if (!mptcp_is_enabled(net))
2913 : : return -ENOPROTOOPT;
2914 : :
2915 [ + + + - ]: 3006 : if (unlikely(!net->mib.mptcp_statistics) && !mptcp_mib_alloc(net))
2916 : : return -ENOMEM;
2917 : :
2918 : 3006 : rcu_read_lock();
2919 [ - + ]: 3006 : ret = mptcp_init_sched(mptcp_sk(sk),
2920 : : mptcp_sched_find(mptcp_get_scheduler(net)));
2921 : 3006 : rcu_read_unlock();
2922 [ + - ]: 3006 : if (ret)
2923 : : return ret;
2924 : :
2925 : 3006 : set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
2926 : :
2927 : : /* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
2928 : : * propagate the correct value
2929 : : */
2930 : 3006 : mptcp_ca_reset(sk);
2931 : :
2932 : 3006 : sk_sockets_allocated_inc(sk);
2933 : 3006 : sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2934 : 3006 : sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
2935 : :
2936 : 3006 : return 0;
2937 : : }
2938 : :
2939 : 4402 : static void __mptcp_clear_xmit(struct sock *sk)
2940 : : {
2941 [ - + ]: 4402 : struct mptcp_sock *msk = mptcp_sk(sk);
2942 : 4402 : struct mptcp_data_frag *dtmp, *dfrag;
2943 : :
2944 : 4402 : msk->first_pending = NULL;
2945 [ + + ]: 4537 : list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list)
2946 : 135 : dfrag_clear(sk, dfrag);
2947 : 4402 : }
2948 : :
2949 : 3153 : void mptcp_cancel_work(struct sock *sk)
2950 : : {
2951 [ - + ]: 3153 : struct mptcp_sock *msk = mptcp_sk(sk);
2952 : :
2953 [ + + ]: 3153 : if (cancel_work_sync(&msk->work))
2954 : 26 : __sock_put(sk);
2955 : 3153 : }
2956 : :
2957 : 3418 : void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
2958 : : {
2959 : 3418 : lock_sock(ssk);
2960 : :
2961 [ - + + ]: 3418 : switch (ssk->sk_state) {
2962 : 0 : case TCP_LISTEN:
2963 [ # # ]: 0 : if (!(how & RCV_SHUTDOWN))
2964 : : break;
2965 : 18 : fallthrough;
2966 : : case TCP_SYN_SENT:
2967 [ - + ]: 18 : WARN_ON_ONCE(tcp_disconnect(ssk, O_NONBLOCK));
2968 : : break;
2969 : 3400 : default:
2970 [ - + + + ]: 3400 : if (__mptcp_check_fallback(mptcp_sk(sk))) {
2971 [ - + ]: 154 : pr_debug("Fallback\n");
2972 : 154 : ssk->sk_shutdown |= how;
2973 : 154 : tcp_shutdown(ssk, how);
2974 : :
2975 : : /* simulate the data_fin ack reception to let the state
2976 : : * machine move forward
2977 : : */
2978 [ - + - + ]: 154 : WRITE_ONCE(mptcp_sk(sk)->snd_una, mptcp_sk(sk)->snd_nxt);
2979 : 154 : mptcp_schedule_work(sk);
2980 : : } else {
2981 [ - + ]: 3246 : pr_debug("Sending DATA_FIN on subflow %p\n", ssk);
2982 : 3246 : tcp_send_ack(ssk);
2983 [ + + ]: 3246 : if (!mptcp_rtx_timer_pending(sk))
2984 : 1657 : mptcp_reset_rtx_timer(sk);
2985 : : }
2986 : : break;
2987 : : }
2988 : :
2989 : 3418 : release_sock(ssk);
2990 : 3418 : }
2991 : :
2992 : 21514 : void mptcp_set_state(struct sock *sk, int state)
2993 : : {
2994 : 22348 : int oldstate = sk->sk_state;
2995 : :
2996 [ + + - ]: 21492 : switch (state) {
2997 : 2862 : case TCP_ESTABLISHED:
2998 [ + - ]: 2862 : if (oldstate != TCP_ESTABLISHED)
2999 [ + - ]: 2862 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
3000 : : break;
3001 : : case TCP_CLOSE_WAIT:
3002 : : /* Unlike TCP, MPTCP sk would not have the TCP_SYN_RECV state:
3003 : : * MPTCP "accepted" sockets will be created later on. So no
3004 : : * transition from TCP_SYN_RECV to TCP_CLOSE_WAIT.
3005 : : */
3006 : : break;
3007 : 18630 : default:
3008 [ + + ]: 18630 : if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
3009 [ + - ]: 2862 : MPTCP_DEC_STATS(sock_net(sk), MPTCP_MIB_CURRESTAB);
3010 : : }
3011 : :
3012 : 22348 : inet_sk_state_store(sk, state);
3013 : 856 : }
3014 : :
3015 : : static const unsigned char new_state[16] = {
3016 : : /* current state: new state: action: */
3017 : : [0 /* (Invalid) */] = TCP_CLOSE,
3018 : : [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
3019 : : [TCP_SYN_SENT] = TCP_CLOSE,
3020 : : [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
3021 : : [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
3022 : : [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
3023 : : [TCP_TIME_WAIT] = TCP_CLOSE, /* should not happen ! */
3024 : : [TCP_CLOSE] = TCP_CLOSE,
3025 : : [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
3026 : : [TCP_LAST_ACK] = TCP_LAST_ACK,
3027 : : [TCP_LISTEN] = TCP_CLOSE,
3028 : : [TCP_CLOSING] = TCP_CLOSING,
3029 : : [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
3030 : : };
3031 : :
3032 : 3044 : static int mptcp_close_state(struct sock *sk)
3033 : : {
3034 : 3044 : int next = (int)new_state[sk->sk_state];
3035 : 3044 : int ns = next & TCP_STATE_MASK;
3036 : :
3037 : 3044 : mptcp_set_state(sk, ns);
3038 : :
3039 : 3044 : return next & TCP_ACTION_FIN;
3040 : : }
3041 : :
3042 : 258151 : static void mptcp_check_send_data_fin(struct sock *sk)
3043 : : {
3044 : 258151 : struct mptcp_subflow_context *subflow;
3045 [ - + ]: 258151 : struct mptcp_sock *msk = mptcp_sk(sk);
3046 : :
3047 [ - + - - ]: 258151 : pr_debug("msk=%p snd_data_fin_enable=%d pending=%d snd_nxt=%llu write_seq=%llu\n",
[ - + ]
3048 : : msk, msk->snd_data_fin_enable, !!mptcp_send_head(sk),
3049 : : msk->snd_nxt, msk->write_seq);
3050 : :
3051 : : /* we still need to enqueue subflows or not really shutting down,
3052 : : * skip this
3053 : : */
3054 [ - + + + : 260749 : if (!msk->snd_data_fin_enable || msk->snd_nxt + 1 != msk->write_seq ||
+ + - + ]
[ + + + +
- + ]
3055 : 2598 : mptcp_send_head(sk))
3056 : 255553 : return;
3057 : :
3058 : 2598 : WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3059 : :
3060 [ + + ]: 5824 : mptcp_for_each_subflow(msk, subflow) {
3061 : 3226 : struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
3062 : :
3063 : 3226 : mptcp_subflow_shutdown(sk, tcp_sk, SEND_SHUTDOWN);
3064 : : }
3065 : : }
3066 : :
3067 : 2598 : static void __mptcp_wr_shutdown(struct sock *sk)
3068 : : {
3069 [ - + ]: 2598 : struct mptcp_sock *msk = mptcp_sk(sk);
3070 : :
3071 [ - + - - ]: 2598 : pr_debug("msk=%p snd_data_fin_enable=%d shutdown=%x state=%d pending=%d\n",
[ - + ]
3072 : : msk, msk->snd_data_fin_enable, sk->sk_shutdown, sk->sk_state,
3073 : : !!mptcp_send_head(sk));
3074 : :
3075 : : /* will be ignored by fallback sockets */
3076 : 2598 : WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
3077 : 2598 : WRITE_ONCE(msk->snd_data_fin_enable, 1);
3078 : :
3079 : 2598 : mptcp_check_send_data_fin(sk);
3080 : 2598 : }
3081 : :
3082 : 4304 : static void __mptcp_destroy_sock(struct sock *sk)
3083 : : {
3084 [ - + ]: 4304 : struct mptcp_sock *msk = mptcp_sk(sk);
3085 : :
3086 [ - + ]: 4304 : pr_debug("msk=%p\n", msk);
3087 : :
3088 : 4304 : might_sleep();
3089 : :
3090 : 4304 : mptcp_stop_rtx_timer(sk);
3091 : 4304 : sk_stop_timer(sk, &sk->sk_timer);
3092 : 4304 : msk->pm.status = 0;
3093 : 4304 : mptcp_release_sched(msk);
3094 : :
3095 : 4304 : sk->sk_prot->destroy(sk);
3096 : :
3097 : 4304 : sk_stream_kill_queues(sk);
3098 : 4304 : xfrm_sk_free_policy(sk);
3099 : :
3100 : 4304 : sock_put(sk);
3101 : 4304 : }
3102 : :
3103 : 36 : void __mptcp_unaccepted_force_close(struct sock *sk)
3104 : : {
3105 : 36 : sock_set_flag(sk, SOCK_DEAD);
3106 : 36 : mptcp_do_fastclose(sk);
3107 : 36 : __mptcp_destroy_sock(sk);
3108 : 36 : }
3109 : :
3110 : 0 : static __poll_t mptcp_check_readable(struct sock *sk)
3111 : : {
3112 [ + + ]: 769428 : return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
3113 : : }
3114 : :
3115 : 2904 : static void mptcp_check_listen_stop(struct sock *sk)
3116 : : {
3117 : 2904 : struct sock *ssk;
3118 : :
3119 [ + + ]: 2904 : if (inet_sk_state_load(sk) != TCP_LISTEN)
3120 : : return;
3121 : :
3122 : 1486 : sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
3123 [ - + ]: 1486 : ssk = mptcp_sk(sk)->first;
3124 [ + - - + ]: 2972 : if (WARN_ON_ONCE(!ssk || inet_sk_state_load(ssk) != TCP_LISTEN))
3125 : 0 : return;
3126 : :
3127 : 1486 : lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
3128 : 1486 : tcp_set_state(ssk, TCP_CLOSE);
3129 : 1486 : mptcp_subflow_queue_clean(sk, ssk);
3130 : 1486 : inet_csk_listen_stop(ssk);
3131 : 1486 : mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
3132 : 1486 : release_sock(ssk);
3133 : : }
3134 : :
3135 : 4332 : bool __mptcp_close(struct sock *sk, long timeout)
3136 : : {
3137 : 4332 : struct mptcp_subflow_context *subflow;
3138 [ - + ]: 4332 : struct mptcp_sock *msk = mptcp_sk(sk);
3139 : 4332 : bool do_cancel_work = false;
3140 : 4332 : int subflows_alive = 0;
3141 : :
3142 : 4332 : WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
3143 : :
3144 [ - + + + ]: 4332 : if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
[ + + ]
3145 : 2814 : mptcp_check_listen_stop(sk);
3146 : 2814 : mptcp_set_state(sk, TCP_CLOSE);
3147 : 2814 : goto cleanup;
3148 : : }
3149 : :
3150 [ + + - + ]: 1518 : if (mptcp_data_avail(msk) || timeout < 0) {
3151 : : /* If the msk has read data, or the caller explicitly ask it,
3152 : : * do the MPTCP equivalent of TCP reset, aka MPTCP fastclose
3153 : : */
3154 : 249 : mptcp_do_fastclose(sk);
3155 : 249 : timeout = 0;
3156 [ + + ]: 1269 : } else if (mptcp_close_state(sk)) {
3157 : 973 : __mptcp_wr_shutdown(sk);
3158 : : }
3159 : :
3160 : 1518 : sk_stream_wait_close(sk, timeout);
3161 : :
3162 : 4332 : cleanup:
3163 : : /* orphan all the subflows */
3164 [ + + ]: 9120 : mptcp_for_each_subflow(msk, subflow) {
3165 : 4788 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
3166 : 4788 : bool slow = lock_sock_fast_nested(ssk);
3167 : :
3168 : 4788 : subflows_alive += ssk->sk_state != TCP_CLOSE;
3169 : :
3170 : : /* since the close timeout takes precedence on the fail one,
3171 : : * cancel the latter
3172 : : */
3173 [ + + ]: 4788 : if (ssk == msk->first)
3174 : 4320 : subflow->fail_tout = 0;
3175 : :
3176 : : /* detach from the parent socket, but allow data_ready to
3177 : : * push incoming data into the mptcp stack, to properly ack it
3178 : : */
3179 : 4788 : ssk->sk_socket = NULL;
3180 : 4788 : ssk->sk_wq = NULL;
3181 : 4788 : unlock_sock_fast(ssk, slow);
3182 : : }
3183 : 4332 : sock_orphan(sk);
3184 : :
3185 : : /* all the subflows are closed, only timeout can change the msk
3186 : : * state, let's not keep resources busy for no reasons
3187 : : */
3188 [ + + ]: 4332 : if (subflows_alive == 0)
3189 : 2583 : mptcp_set_state(sk, TCP_CLOSE);
3190 : :
3191 : 4332 : sock_hold(sk);
3192 [ - + ]: 4332 : pr_debug("msk=%p state=%d\n", sk, sk->sk_state);
3193 : 4332 : mptcp_pm_connection_closed(msk);
3194 : :
3195 [ + + ]: 4332 : if (sk->sk_state == TCP_CLOSE) {
3196 : 3117 : __mptcp_destroy_sock(sk);
3197 : 3117 : do_cancel_work = true;
3198 : : } else {
3199 : 1215 : mptcp_start_tout_timer(sk);
3200 : : }
3201 : :
3202 : 4332 : return do_cancel_work;
3203 : : }
3204 : :
3205 : 4332 : static void mptcp_close(struct sock *sk, long timeout)
3206 : : {
3207 : 4332 : bool do_cancel_work;
3208 : :
3209 : 4332 : lock_sock(sk);
3210 : :
3211 : 4332 : do_cancel_work = __mptcp_close(sk, timeout);
3212 : 4332 : release_sock(sk);
3213 [ + + ]: 4332 : if (do_cancel_work)
3214 : 3117 : mptcp_cancel_work(sk);
3215 : :
3216 : 4332 : sock_put(sk);
3217 : 4332 : }
3218 : :
3219 : 5890 : static void mptcp_copy_inaddrs(struct sock *msk, const struct sock *ssk)
3220 : : {
3221 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3222 [ + - ]: 5890 : const struct ipv6_pinfo *ssk6 = inet6_sk(ssk);
3223 [ + - ]: 5890 : struct ipv6_pinfo *msk6 = inet6_sk(msk);
3224 : :
3225 : 5890 : msk->sk_v6_daddr = ssk->sk_v6_daddr;
3226 : 5890 : msk->sk_v6_rcv_saddr = ssk->sk_v6_rcv_saddr;
3227 : :
3228 [ + + ]: 5890 : if (msk6 && ssk6) {
3229 : 2506 : msk6->saddr = ssk6->saddr;
3230 : 2506 : msk6->flow_label = ssk6->flow_label;
3231 : : }
3232 : : #endif
3233 : :
3234 : 5890 : inet_sk(msk)->inet_num = inet_sk(ssk)->inet_num;
3235 : 5890 : inet_sk(msk)->inet_dport = inet_sk(ssk)->inet_dport;
3236 : 5890 : inet_sk(msk)->inet_sport = inet_sk(ssk)->inet_sport;
3237 : 5890 : inet_sk(msk)->inet_daddr = inet_sk(ssk)->inet_daddr;
3238 : 5890 : inet_sk(msk)->inet_saddr = inet_sk(ssk)->inet_saddr;
3239 : 5890 : inet_sk(msk)->inet_rcv_saddr = inet_sk(ssk)->inet_rcv_saddr;
3240 : 5890 : }
3241 : :
3242 : 90 : static int mptcp_disconnect(struct sock *sk, int flags)
3243 : : {
3244 [ - + ]: 90 : struct mptcp_sock *msk = mptcp_sk(sk);
3245 : :
3246 : : /* We are on the fastopen error path. We can't call straight into the
3247 : : * subflows cleanup code due to lock nesting (we are already under
3248 : : * msk->firstsocket lock).
3249 : : */
3250 [ + - ]: 90 : if (msk->fastopening)
3251 : : return -EBUSY;
3252 : :
3253 : 90 : mptcp_check_listen_stop(sk);
3254 : 90 : mptcp_set_state(sk, TCP_CLOSE);
3255 : :
3256 : 90 : mptcp_stop_rtx_timer(sk);
3257 : 90 : mptcp_stop_tout_timer(sk);
3258 : :
3259 : 90 : mptcp_pm_connection_closed(msk);
3260 : :
3261 : : /* msk->subflow is still intact, the following will not free the first
3262 : : * subflow
3263 : : */
3264 : 90 : mptcp_do_fastclose(sk);
3265 : 90 : mptcp_destroy_common(msk);
3266 : :
3267 : : /* The first subflow is already in TCP_CLOSE status, the following
3268 : : * can't overlap with a fallback anymore
3269 : : */
3270 : 90 : spin_lock_bh(&msk->fallback_lock);
3271 : 90 : msk->allow_subflows = true;
3272 : 90 : msk->allow_infinite_fallback = true;
3273 : 90 : WRITE_ONCE(msk->flags, 0);
3274 : 90 : spin_unlock_bh(&msk->fallback_lock);
3275 : :
3276 : 90 : msk->cb_flags = 0;
3277 : 90 : msk->recovery = false;
3278 : 90 : WRITE_ONCE(msk->can_ack, false);
3279 : 90 : WRITE_ONCE(msk->fully_established, false);
3280 : 90 : WRITE_ONCE(msk->rcv_data_fin, false);
3281 : 90 : WRITE_ONCE(msk->snd_data_fin_enable, false);
3282 : 90 : WRITE_ONCE(msk->rcv_fastclose, false);
3283 : 90 : WRITE_ONCE(msk->use_64bit_ack, false);
3284 : 90 : WRITE_ONCE(msk->csum_enabled, mptcp_is_checksum_enabled(sock_net(sk)));
3285 : 90 : mptcp_pm_data_reset(msk);
3286 : 90 : mptcp_ca_reset(sk);
3287 : 90 : msk->bytes_consumed = 0;
3288 : 90 : msk->bytes_acked = 0;
3289 : 90 : msk->bytes_received = 0;
3290 : 90 : msk->bytes_sent = 0;
3291 : 90 : msk->bytes_retrans = 0;
3292 : 90 : msk->rcvspace_init = 0;
3293 : :
3294 : 90 : WRITE_ONCE(sk->sk_shutdown, 0);
3295 : 90 : sk_error_report(sk);
3296 : 90 : return 0;
3297 : : }
3298 : :
3299 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3300 : 632 : static struct ipv6_pinfo *mptcp_inet6_sk(const struct sock *sk)
3301 : : {
3302 [ - + ]: 632 : struct mptcp6_sock *msk6 = container_of(mptcp_sk(sk), struct mptcp6_sock, msk);
3303 : :
3304 : 632 : return &msk6->np;
3305 : : }
3306 : :
3307 : 632 : static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
3308 : : {
3309 [ + - ]: 632 : const struct ipv6_pinfo *np = inet6_sk(sk);
3310 : 632 : struct ipv6_txoptions *opt;
3311 : 632 : struct ipv6_pinfo *newnp;
3312 : :
3313 [ + - ]: 632 : newnp = inet6_sk(newsk);
3314 : :
3315 : 632 : rcu_read_lock();
3316 [ + - - + : 632 : opt = rcu_dereference(np->opt);
- - - - -
- ]
3317 [ + - ]: 632 : if (opt) {
3318 : 0 : opt = ipv6_dup_options(newsk, opt);
3319 [ # # ]: 0 : if (!opt)
3320 [ # # ]: 0 : net_warn_ratelimited("%s: Failed to copy ip6 options\n", __func__);
3321 : : }
3322 : 632 : RCU_INIT_POINTER(newnp->opt, opt);
3323 : 632 : rcu_read_unlock();
3324 : 632 : }
3325 : : #endif
3326 : :
3327 : 746 : static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
3328 : : {
3329 : 746 : struct ip_options_rcu *inet_opt, *newopt = NULL;
3330 : 746 : const struct inet_sock *inet = inet_sk(sk);
3331 : 746 : struct inet_sock *newinet;
3332 : :
3333 : 746 : newinet = inet_sk(newsk);
3334 : :
3335 : 746 : rcu_read_lock();
3336 [ + - - + : 746 : inet_opt = rcu_dereference(inet->inet_opt);
- - - - -
- ]
3337 [ + - ]: 746 : if (inet_opt) {
3338 : 0 : newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) +
3339 : 0 : inet_opt->opt.optlen, GFP_ATOMIC);
3340 [ # # ]: 0 : if (!newopt)
3341 [ # # ]: 0 : net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
3342 : : }
3343 : 746 : RCU_INIT_POINTER(newinet->inet_opt, newopt);
3344 : 746 : rcu_read_unlock();
3345 : 746 : }
3346 : :
3347 : 1378 : struct sock *mptcp_sk_clone_init(const struct sock *sk,
3348 : : const struct mptcp_options_received *mp_opt,
3349 : : struct sock *ssk,
3350 : : struct request_sock *req)
3351 : : {
3352 : 1378 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
3353 : 1378 : struct sock *nsk = sk_clone_lock(sk, GFP_ATOMIC);
3354 : 1378 : struct mptcp_subflow_context *subflow;
3355 : 1378 : struct mptcp_sock *msk;
3356 : :
3357 [ + - ]: 1378 : if (!nsk)
3358 : : return NULL;
3359 : :
3360 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3361 [ + + ]: 1378 : if (nsk->sk_family == AF_INET6)
3362 : 632 : inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
3363 : : #endif
3364 : :
3365 : 1378 : __mptcp_init_sock(nsk);
3366 : :
3367 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3368 [ + + ]: 1378 : if (nsk->sk_family == AF_INET6)
3369 : 632 : mptcp_copy_ip6_options(nsk, sk);
3370 : : else
3371 : : #endif
3372 : 746 : mptcp_copy_ip_options(nsk, sk);
3373 : :
3374 [ - + ]: 1378 : msk = mptcp_sk(nsk);
3375 : 1378 : WRITE_ONCE(msk->local_key, subflow_req->local_key);
3376 : 1378 : WRITE_ONCE(msk->token, subflow_req->token);
3377 : 1378 : msk->in_accept_queue = 1;
3378 : 1378 : WRITE_ONCE(msk->fully_established, false);
3379 [ + + ]: 1378 : if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
3380 : 118 : WRITE_ONCE(msk->csum_enabled, true);
3381 : :
3382 : 1378 : WRITE_ONCE(msk->write_seq, subflow_req->idsn + 1);
3383 : 1378 : WRITE_ONCE(msk->snd_nxt, msk->write_seq);
3384 : 1378 : WRITE_ONCE(msk->snd_una, msk->write_seq);
3385 [ - + ]: 1378 : WRITE_ONCE(msk->wnd_end, msk->snd_nxt + tcp_sk(ssk)->snd_wnd);
3386 [ - + ]: 1378 : msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
3387 [ - + ]: 1378 : mptcp_init_sched(msk, mptcp_sk(sk)->sched);
3388 : :
3389 : : /* passive msk is created after the first/MPC subflow */
3390 : 1378 : msk->subflow_id = 2;
3391 : :
3392 : 1378 : sock_reset_flag(nsk, SOCK_RCU_FREE);
3393 : 1378 : security_inet_csk_clone(nsk, req);
3394 : :
3395 : : /* this can't race with mptcp_close(), as the msk is
3396 : : * not yet exposted to user-space
3397 : : */
3398 : 1378 : mptcp_set_state(nsk, TCP_ESTABLISHED);
3399 : :
3400 : : /* The msk maintain a ref to each subflow in the connections list */
3401 : 1378 : WRITE_ONCE(msk->first, ssk);
3402 : 1378 : subflow = mptcp_subflow_ctx(ssk);
3403 : 1378 : list_add(&subflow->node, &msk->conn_list);
3404 : 1378 : sock_hold(ssk);
3405 : :
3406 : : /* new mpc subflow takes ownership of the newly
3407 : : * created mptcp socket
3408 : : */
3409 : 1378 : mptcp_token_accept(subflow_req, msk);
3410 : :
3411 : : /* set msk addresses early to ensure mptcp_pm_get_local_id()
3412 : : * uses the correct data
3413 : : */
3414 : 1378 : mptcp_copy_inaddrs(nsk, ssk);
3415 [ + - ]: 1378 : __mptcp_propagate_sndbuf(nsk, ssk);
3416 : :
3417 : 1378 : mptcp_rcv_space_init(msk, ssk);
3418 : :
3419 [ + + ]: 1378 : if (mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
3420 : 1332 : __mptcp_subflow_fully_established(msk, subflow, mp_opt);
3421 : 1378 : bh_unlock_sock(nsk);
3422 : :
3423 : : /* note: the newly allocated socket refcount is 2 now */
3424 : 1378 : return nsk;
3425 : : }
3426 : :
3427 : 2862 : void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk)
3428 : : {
3429 [ - + ]: 2862 : const struct tcp_sock *tp = tcp_sk(ssk);
3430 : :
3431 : 2862 : msk->rcvspace_init = 1;
3432 : 2862 : msk->rcvq_space.copied = 0;
3433 : 2862 : msk->rcvq_space.rtt_us = 0;
3434 : :
3435 : 2862 : msk->rcvq_space.time = tp->tcp_mstamp;
3436 : :
3437 : : /* initial rcv_space offering made to peer */
3438 : 2862 : msk->rcvq_space.space = min_t(u32, tp->rcv_wnd,
3439 : : TCP_INIT_CWND * tp->advmss);
3440 [ - + ]: 2862 : if (msk->rcvq_space.space == 0)
3441 : 0 : msk->rcvq_space.space = TCP_INIT_CWND * TCP_MSS_DEFAULT;
3442 : 2862 : }
3443 : :
3444 : 4402 : void mptcp_destroy_common(struct mptcp_sock *msk)
3445 : : {
3446 : 4402 : struct mptcp_subflow_context *subflow, *tmp;
3447 : 4402 : struct sock *sk = (struct sock *)msk;
3448 : :
3449 : 4402 : __mptcp_clear_xmit(sk);
3450 : :
3451 : : /* join list will be eventually flushed (with rst) at sock lock release time */
3452 [ + + ]: 9117 : mptcp_for_each_subflow_safe(msk, subflow, tmp)
3453 : 4715 : __mptcp_close_ssk(sk, mptcp_subflow_tcp_sock(subflow), subflow, 0);
3454 : :
3455 : 4402 : __skb_queue_purge(&sk->sk_receive_queue);
3456 : 4402 : skb_rbtree_purge(&msk->out_of_order_queue);
3457 : :
3458 : : /* move all the rx fwd alloc into the sk_mem_reclaim_final in
3459 : : * inet_sock_destruct() will dispose it
3460 : : */
3461 : 4402 : mptcp_token_destroy(msk);
3462 : 4402 : mptcp_pm_destroy(msk);
3463 : 4402 : }
3464 : :
3465 : 4312 : static void mptcp_destroy(struct sock *sk)
3466 : : {
3467 [ - + ]: 4312 : struct mptcp_sock *msk = mptcp_sk(sk);
3468 : :
3469 : : /* allow the following to close even the initial subflow */
3470 : 4312 : msk->free_first = 1;
3471 : 4312 : mptcp_destroy_common(msk);
3472 : 4312 : sk_sockets_allocated_dec(sk);
3473 : 4312 : }
3474 : :
3475 : 280262 : void __mptcp_data_acked(struct sock *sk)
3476 : : {
3477 [ + + ]: 280262 : if (!sock_owned_by_user(sk))
3478 : 182925 : __mptcp_clean_una(sk);
3479 : : else
3480 [ - + ]: 97337 : __set_bit(MPTCP_CLEAN_UNA, &mptcp_sk(sk)->cb_flags);
3481 : 280262 : }
3482 : :
3483 : 1009511 : void __mptcp_check_push(struct sock *sk, struct sock *ssk)
3484 : : {
3485 [ + + ]: 1009511 : if (!sock_owned_by_user(sk))
3486 : 821777 : __mptcp_subflow_push_pending(sk, ssk, false);
3487 : : else
3488 [ - + ]: 187734 : __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3489 : 1009511 : }
3490 : :
3491 : : #define MPTCP_FLAGS_PROCESS_CTX_NEED (BIT(MPTCP_PUSH_PENDING) | \
3492 : : BIT(MPTCP_RETRANSMIT) | \
3493 : : BIT(MPTCP_FLUSH_JOIN_LIST) | \
3494 : : BIT(MPTCP_DEQUEUE))
3495 : :
3496 : : /* processes deferred events and flush wmem */
3497 : 972791 : static void mptcp_release_cb(struct sock *sk)
3498 : : __must_hold(&sk->sk_lock.slock)
3499 : : {
3500 [ - + ]: 972791 : struct mptcp_sock *msk = mptcp_sk(sk);
3501 : :
3502 : 117270 : for (;;) {
3503 : 1089535 : unsigned long flags = (msk->cb_flags & MPTCP_FLAGS_PROCESS_CTX_NEED);
3504 : 1089535 : struct list_head join_list;
3505 : :
3506 [ + + ]: 1089535 : if (!flags)
3507 : : break;
3508 : :
3509 [ + + ]: 116744 : INIT_LIST_HEAD(&join_list);
3510 [ + + ]: 116744 : list_splice_init(&msk->join_list, &join_list);
3511 : :
3512 : : /* the following actions acquire the subflow socket lock
3513 : : *
3514 : : * 1) can't be invoked in atomic scope
3515 : : * 2) must avoid ABBA deadlock with msk socket spinlock: the RX
3516 : : * datapath acquires the msk socket spinlock while helding
3517 : : * the subflow socket lock
3518 : : */
3519 : 116744 : msk->cb_flags &= ~flags;
3520 : 116744 : spin_unlock_bh(&sk->sk_lock.slock);
3521 : :
3522 [ + + ]: 116744 : if (flags & BIT(MPTCP_FLUSH_JOIN_LIST))
3523 : 10 : __mptcp_flush_join_list(sk, &join_list);
3524 [ + + ]: 116744 : if (flags & BIT(MPTCP_PUSH_PENDING))
3525 : 100511 : __mptcp_push_pending(sk, 0);
3526 [ + + ]: 116744 : if (flags & BIT(MPTCP_RETRANSMIT))
3527 : 977 : __mptcp_retrans(sk);
3528 [ + + + + ]: 116744 : if ((flags & BIT(MPTCP_DEQUEUE)) && __mptcp_move_skbs(sk)) {
3529 : : /* notify ack seq update */
3530 : 47243 : mptcp_cleanup_rbuf(msk, 0);
3531 : 47243 : sk->sk_data_ready(sk);
3532 : : }
3533 : :
3534 : 116744 : cond_resched();
3535 : 116744 : spin_lock_bh(&sk->sk_lock.slock);
3536 : : }
3537 : :
3538 [ - + - - : 1945105 : if (__test_and_clear_bit(MPTCP_CLEAN_UNA, &msk->cb_flags))
- - + + ]
3539 : 60567 : __mptcp_clean_una_wakeup(sk);
3540 [ + + ]: 972791 : if (unlikely(msk->cb_flags)) {
3541 : : /* be sure to sync the msk state before taking actions
3542 : : * depending on sk_state (MPTCP_ERROR_REPORT)
3543 : : * On sk release avoid actions depending on the first subflow
3544 : : */
3545 [ - + - - : 8326 : if (__test_and_clear_bit(MPTCP_SYNC_STATE, &msk->cb_flags) && msk->first)
- - + + +
- ]
3546 : 1027 : __mptcp_sync_state(sk, msk->pending_state);
3547 [ - + - - : 8326 : if (__test_and_clear_bit(MPTCP_ERROR_REPORT, &msk->cb_flags))
- - + + ]
3548 : 515 : __mptcp_error_report(sk);
3549 [ - + - - : 8326 : if (__test_and_clear_bit(MPTCP_SYNC_SNDBUF, &msk->cb_flags))
- - + + ]
3550 : 2638 : __mptcp_sync_sndbuf(sk);
3551 : : }
3552 : 972791 : }
3553 : :
3554 : : /* MP_JOIN client subflow must wait for 4th ack before sending any data:
3555 : : * TCP can't schedule delack timer before the subflow is fully established.
3556 : : * MPTCP uses the delack timer to do 3rd ack retransmissions
3557 : : */
3558 : 466 : static void schedule_3rdack_retransmission(struct sock *ssk)
3559 : : {
3560 : 466 : struct inet_connection_sock *icsk = inet_csk(ssk);
3561 [ - + ]: 466 : struct tcp_sock *tp = tcp_sk(ssk);
3562 : 466 : unsigned long timeout;
3563 : :
3564 [ - + + + ]: 466 : if (READ_ONCE(mptcp_subflow_ctx(ssk)->fully_established))
[ + + ]
3565 : : return;
3566 : :
3567 : : /* reschedule with a timeout above RTT, as we must look only for drop */
3568 [ + - ]: 96 : if (tp->srtt_us)
3569 [ - + ]: 96 : timeout = usecs_to_jiffies(tp->srtt_us >> (3 - 1));
3570 : : else
3571 : : timeout = TCP_TIMEOUT_INIT;
3572 : 96 : timeout += jiffies;
3573 : :
3574 [ - + ]: 96 : WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
3575 : 96 : smp_store_release(&icsk->icsk_ack.pending,
3576 : : icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER);
3577 : 96 : sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout);
3578 : : }
3579 : :
3580 : 26209 : void mptcp_subflow_process_delegated(struct sock *ssk, long status)
3581 : : {
3582 [ + + ]: 26209 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3583 : 26209 : struct sock *sk = subflow->conn;
3584 : :
3585 [ + + ]: 26209 : if (status & BIT(MPTCP_DELEGATE_SEND)) {
3586 : 3821 : mptcp_data_lock(sk);
3587 [ + + ]: 3821 : if (!sock_owned_by_user(sk))
3588 : 3503 : __mptcp_subflow_push_pending(sk, ssk, true);
3589 : : else
3590 [ - + ]: 318 : __set_bit(MPTCP_PUSH_PENDING, &mptcp_sk(sk)->cb_flags);
3591 : 3821 : mptcp_data_unlock(sk);
3592 : : }
3593 [ + + ]: 26209 : if (status & BIT(MPTCP_DELEGATE_SNDBUF)) {
3594 : 21551 : mptcp_data_lock(sk);
3595 [ + + ]: 21551 : if (!sock_owned_by_user(sk))
3596 : 18499 : __mptcp_sync_sndbuf(sk);
3597 : : else
3598 [ - + ]: 3052 : __set_bit(MPTCP_SYNC_SNDBUF, &mptcp_sk(sk)->cb_flags);
3599 : 21551 : mptcp_data_unlock(sk);
3600 : : }
3601 [ + + ]: 26209 : if (status & BIT(MPTCP_DELEGATE_ACK))
3602 : 466 : schedule_3rdack_retransmission(ssk);
3603 : 26209 : }
3604 : :
3605 : 0 : static int mptcp_hash(struct sock *sk)
3606 : : {
3607 : : /* should never be called,
3608 : : * we hash the TCP subflows not the MPTCP socket
3609 : : */
3610 : 0 : WARN_ON_ONCE(1);
3611 : 0 : return 0;
3612 : : }
3613 : :
3614 : 8 : static void mptcp_unhash(struct sock *sk)
3615 : : {
3616 : : /* called from sk_common_release(), but nothing to do here */
3617 : 8 : }
3618 : :
3619 : 0 : static int mptcp_get_port(struct sock *sk, unsigned short snum)
3620 : : {
3621 [ # # ]: 0 : struct mptcp_sock *msk = mptcp_sk(sk);
3622 : :
3623 [ # # ]: 0 : pr_debug("msk=%p, ssk=%p\n", msk, msk->first);
3624 [ # # ]: 0 : if (WARN_ON_ONCE(!msk->first))
3625 : 0 : return -EINVAL;
3626 : :
3627 : 0 : return inet_csk_get_port(msk->first, snum);
3628 : : }
3629 : :
3630 : 1334 : void mptcp_finish_connect(struct sock *ssk)
3631 : : {
3632 : 1334 : struct mptcp_subflow_context *subflow;
3633 : 1334 : struct mptcp_sock *msk;
3634 : 1334 : struct sock *sk;
3635 : :
3636 [ - + ]: 1334 : subflow = mptcp_subflow_ctx(ssk);
3637 : 1334 : sk = subflow->conn;
3638 [ - + ]: 1334 : msk = mptcp_sk(sk);
3639 : :
3640 [ - + ]: 1334 : pr_debug("msk=%p, token=%u\n", sk, subflow->token);
3641 : :
3642 : 1334 : subflow->map_seq = subflow->iasn;
3643 : 1334 : subflow->map_subflow_seq = 1;
3644 : :
3645 : : /* the socket is not connected yet, no msk/subflow ops can access/race
3646 : : * accessing the field below
3647 : : */
3648 : 1334 : WRITE_ONCE(msk->local_key, subflow->local_key);
3649 : :
3650 : 1334 : mptcp_pm_new_connection(msk, ssk, 0);
3651 : 1334 : }
3652 : :
3653 : 5306 : void mptcp_sock_graft(struct sock *sk, struct socket *parent)
3654 : : {
3655 : 5306 : write_lock_bh(&sk->sk_callback_lock);
3656 : 5306 : rcu_assign_pointer(sk->sk_wq, &parent->wq);
3657 [ + - ]: 5306 : sk_set_socket(sk, parent);
3658 : 5306 : write_unlock_bh(&sk->sk_callback_lock);
3659 : 5306 : }
3660 : :
3661 : 932 : bool mptcp_finish_join(struct sock *ssk)
3662 : : {
3663 [ - + ]: 932 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
3664 [ - + ]: 932 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
3665 : 932 : struct sock *parent = (void *)msk;
3666 : 932 : bool ret = true;
3667 : :
3668 [ - + ]: 932 : pr_debug("msk=%p, subflow=%p\n", msk, subflow);
3669 : :
3670 : : /* mptcp socket already closing? */
3671 [ - + ]: 932 : if (!mptcp_is_fully_established(parent)) {
3672 : 0 : subflow->reset_reason = MPTCP_RST_EMPTCP;
3673 : 0 : return false;
3674 : : }
3675 : :
3676 : : /* active subflow, already present inside the conn_list */
3677 [ + + ]: 932 : if (!list_empty(&subflow->node)) {
3678 : 454 : spin_lock_bh(&msk->fallback_lock);
3679 [ - + - + ]: 454 : if (!msk->allow_subflows) {
[ - + ]
3680 : 0 : spin_unlock_bh(&msk->fallback_lock);
3681 : 0 : return false;
3682 : : }
3683 : 454 : mptcp_subflow_joined(msk, ssk);
3684 : 454 : spin_unlock_bh(&msk->fallback_lock);
3685 : 454 : mptcp_propagate_sndbuf(parent, ssk);
3686 : 454 : return true;
3687 : : }
3688 : :
3689 [ - + ]: 478 : if (!mptcp_pm_allow_new_subflow(msk)) {
3690 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_JOINREJECTED);
3691 : 0 : goto err_prohibited;
3692 : : }
3693 : :
3694 : : /* If we can't acquire msk socket lock here, let the release callback
3695 : : * handle it
3696 : : */
3697 : 478 : mptcp_data_lock(parent);
3698 [ + + ]: 478 : if (!sock_owned_by_user(parent)) {
3699 : 468 : ret = __mptcp_finish_join(msk, ssk);
3700 [ + - ]: 468 : if (ret) {
3701 : 468 : sock_hold(ssk);
3702 : 468 : list_add_tail(&subflow->node, &msk->conn_list);
3703 : : }
3704 : : } else {
3705 : 10 : sock_hold(ssk);
3706 [ - + ]: 10 : list_add_tail(&subflow->node, &msk->join_list);
3707 [ - + - - : 10 : __set_bit(MPTCP_FLUSH_JOIN_LIST, &msk->cb_flags);
- - ]
3708 : : }
3709 : 478 : mptcp_data_unlock(parent);
3710 : :
3711 [ - + ]: 478 : if (!ret) {
3712 : 0 : err_prohibited:
3713 : 0 : subflow->reset_reason = MPTCP_RST_EPROHIBIT;
3714 : 0 : return false;
3715 : : }
3716 : :
3717 : : return true;
3718 : : }
3719 : :
3720 : 1775 : static void mptcp_shutdown(struct sock *sk, int how)
3721 : : {
3722 [ - + ]: 1775 : pr_debug("sk=%p, how=%d\n", sk, how);
3723 : :
3724 [ + - + + ]: 1775 : if ((how & SEND_SHUTDOWN) && mptcp_close_state(sk))
3725 : 1625 : __mptcp_wr_shutdown(sk);
3726 : 1775 : }
3727 : :
3728 : 16 : static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)
3729 : : {
3730 : 16 : const struct sock *sk = (void *)msk;
3731 : 16 : u64 delta;
3732 : :
3733 [ + - ]: 16 : if (sk->sk_state == TCP_LISTEN)
3734 : : return -EINVAL;
3735 : :
3736 [ - + + - ]: 16 : if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
[ + - ]
3737 : : return 0;
3738 : :
3739 : 16 : delta = msk->write_seq - v;
3740 [ + + + + ]: 16 : if (__mptcp_check_fallback(msk) && msk->first) {
3741 [ - + ]: 16 : struct tcp_sock *tp = tcp_sk(msk->first);
3742 : :
3743 : : /* the first subflow is disconnected after close - see
3744 : : * __mptcp_close_ssk(). tcp_disconnect() moves the write_seq
3745 : : * so ignore that status, too.
3746 : : */
3747 [ - + - + ]: 16 : if (!((1 << msk->first->sk_state) &
[ + - ]
3748 : : (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)))
3749 : 16 : delta += READ_ONCE(tp->write_seq) - tp->snd_una;
3750 : : }
3751 : 16 : if (delta > INT_MAX)
3752 : : delta = INT_MAX;
3753 : :
3754 : 16 : return (int)delta;
3755 : : }
3756 : :
3757 : 16 : static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)
3758 : : {
3759 [ - + ]: 16 : struct mptcp_sock *msk = mptcp_sk(sk);
3760 : 16 : bool slow;
3761 : :
3762 [ - + + - ]: 16 : switch (cmd) {
3763 : 0 : case SIOCINQ:
3764 [ # # ]: 0 : if (sk->sk_state == TCP_LISTEN)
3765 : : return -EINVAL;
3766 : :
3767 : 0 : lock_sock(sk);
3768 [ # # ]: 0 : if (__mptcp_move_skbs(sk))
3769 : 0 : mptcp_cleanup_rbuf(msk, 0);
3770 : 0 : *karg = mptcp_inq_hint(sk);
3771 : 0 : release_sock(sk);
3772 : 0 : break;
3773 : 4 : case SIOCOUTQ:
3774 : 8 : slow = lock_sock_fast(sk);
3775 : 8 : *karg = mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una));
3776 : 8 : unlock_sock_fast(sk, slow);
3777 : 8 : break;
3778 : 4 : case SIOCOUTQNSD:
3779 : 8 : slow = lock_sock_fast(sk);
3780 : 8 : *karg = mptcp_ioctl_outq(msk, msk->snd_nxt);
3781 : 8 : unlock_sock_fast(sk, slow);
3782 : 8 : break;
3783 : : default:
3784 : : return -ENOIOCTLCMD;
3785 : : }
3786 : :
3787 : : return 0;
3788 : : }
3789 : :
3790 : 1552 : static int mptcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
3791 : : {
3792 : 1552 : struct mptcp_subflow_context *subflow;
3793 [ - + ]: 1552 : struct mptcp_sock *msk = mptcp_sk(sk);
3794 : 1552 : int err = -EINVAL;
3795 : 1552 : struct sock *ssk;
3796 : :
3797 : 1552 : ssk = __mptcp_nmpc_sk(msk);
3798 [ - + ]: 1552 : if (IS_ERR(ssk))
3799 : 0 : return PTR_ERR(ssk);
3800 : :
3801 : 1552 : mptcp_set_state(sk, TCP_SYN_SENT);
3802 [ + - ]: 1552 : subflow = mptcp_subflow_ctx(ssk);
3803 : : #ifdef CONFIG_TCP_MD5SIG
3804 : : /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
3805 : : * TCP option space.
3806 : : */
3807 : : if (rcu_access_pointer(tcp_sk(ssk)->md5sig_info))
3808 : : mptcp_early_fallback(msk, subflow, MPTCP_MIB_MD5SIGFALLBACK);
3809 : : #endif
3810 [ + - ]: 1552 : if (subflow->request_mptcp) {
3811 [ + + ]: 1552 : if (mptcp_active_should_disable(sk))
3812 : 6 : mptcp_early_fallback(msk, subflow,
3813 : : MPTCP_MIB_MPCAPABLEACTIVEDISABLED);
3814 [ - + ]: 1546 : else if (mptcp_token_new_connect(ssk) < 0)
3815 : 0 : mptcp_early_fallback(msk, subflow,
3816 : : MPTCP_MIB_TOKENFALLBACKINIT);
3817 : : }
3818 : :
3819 : 1552 : WRITE_ONCE(msk->write_seq, subflow->idsn);
3820 : 1552 : WRITE_ONCE(msk->snd_nxt, subflow->idsn);
3821 : 1552 : WRITE_ONCE(msk->snd_una, subflow->idsn);
3822 [ + + ]: 1552 : if (likely(!__mptcp_check_fallback(msk)))
3823 [ + - ]: 1546 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVE);
3824 : :
3825 : : /* if reaching here via the fastopen/sendmsg path, the caller already
3826 : : * acquired the subflow socket lock, too.
3827 : : */
3828 [ + + ]: 1552 : if (!msk->fastopening)
3829 : 1490 : lock_sock(ssk);
3830 : :
3831 : : /* the following mirrors closely a very small chunk of code from
3832 : : * __inet_stream_connect()
3833 : : */
3834 [ - + ]: 1552 : if (ssk->sk_state != TCP_CLOSE)
3835 : 0 : goto out;
3836 : :
3837 [ + - - + : 1552 : if (BPF_CGROUP_PRE_CONNECT_ENABLED(ssk)) {
- - ]
3838 : 0 : err = ssk->sk_prot->pre_connect(ssk, uaddr, addr_len);
3839 [ - - ]: 0 : if (err)
3840 : 0 : goto out;
3841 : : }
3842 : :
3843 : 1552 : err = ssk->sk_prot->connect(ssk, uaddr, addr_len);
3844 [ - + ]: 1552 : if (err < 0)
3845 : 0 : goto out;
3846 : :
3847 [ + + ]: 1552 : inet_assign_bit(DEFER_CONNECT, sk, inet_test_bit(DEFER_CONNECT, ssk));
3848 : :
3849 : 1552 : out:
3850 [ + + ]: 1552 : if (!msk->fastopening)
3851 : 1490 : release_sock(ssk);
3852 : :
3853 : : /* on successful connect, the msk state will be moved to established by
3854 : : * subflow_finish_connect()
3855 : : */
3856 [ - + ]: 1552 : if (unlikely(err)) {
3857 : : /* avoid leaving a dangling token in an unconnected socket */
3858 : 0 : mptcp_token_destroy(msk);
3859 : 0 : mptcp_set_state(sk, TCP_CLOSE);
3860 : 0 : return err;
3861 : : }
3862 : :
3863 : 1552 : mptcp_copy_inaddrs(sk, ssk);
3864 : 1552 : return 0;
3865 : : }
3866 : :
3867 : : static struct proto mptcp_prot = {
3868 : : .name = "MPTCP",
3869 : : .owner = THIS_MODULE,
3870 : : .init = mptcp_init_sock,
3871 : : .connect = mptcp_connect,
3872 : : .disconnect = mptcp_disconnect,
3873 : : .close = mptcp_close,
3874 : : .setsockopt = mptcp_setsockopt,
3875 : : .getsockopt = mptcp_getsockopt,
3876 : : .shutdown = mptcp_shutdown,
3877 : : .destroy = mptcp_destroy,
3878 : : .sendmsg = mptcp_sendmsg,
3879 : : .ioctl = mptcp_ioctl,
3880 : : .recvmsg = mptcp_recvmsg,
3881 : : .release_cb = mptcp_release_cb,
3882 : : .hash = mptcp_hash,
3883 : : .unhash = mptcp_unhash,
3884 : : .get_port = mptcp_get_port,
3885 : : .stream_memory_free = mptcp_stream_memory_free,
3886 : : .sockets_allocated = &mptcp_sockets_allocated,
3887 : :
3888 : : .memory_allocated = &net_aligned_data.tcp_memory_allocated,
3889 : : .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
3890 : :
3891 : : .memory_pressure = &tcp_memory_pressure,
3892 : : .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
3893 : : .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
3894 : : .sysctl_mem = sysctl_tcp_mem,
3895 : : .obj_size = sizeof(struct mptcp_sock),
3896 : : .slab_flags = SLAB_TYPESAFE_BY_RCU,
3897 : : .no_autobind = true,
3898 : : };
3899 : :
3900 : 1484 : static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3901 : : {
3902 [ - + ]: 1484 : struct mptcp_sock *msk = mptcp_sk(sock->sk);
3903 : 1484 : struct sock *ssk, *sk = sock->sk;
3904 : 1484 : int err = -EINVAL;
3905 : :
3906 : 1484 : lock_sock(sk);
3907 : 1484 : ssk = __mptcp_nmpc_sk(msk);
3908 [ - + ]: 1484 : if (IS_ERR(ssk)) {
3909 : 0 : err = PTR_ERR(ssk);
3910 : 0 : goto unlock;
3911 : : }
3912 : :
3913 [ + + ]: 1484 : if (sk->sk_family == AF_INET)
3914 : 798 : err = inet_bind_sk(ssk, uaddr, addr_len);
3915 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
3916 [ + - ]: 686 : else if (sk->sk_family == AF_INET6)
3917 : 686 : err = inet6_bind_sk(ssk, uaddr, addr_len);
3918 : : #endif
3919 [ + + ]: 1484 : if (!err)
3920 : 1480 : mptcp_copy_inaddrs(sk, ssk);
3921 : :
3922 : 4 : unlock:
3923 : 1484 : release_sock(sk);
3924 : 1484 : return err;
3925 : : }
3926 : :
3927 : 1480 : static int mptcp_listen(struct socket *sock, int backlog)
3928 : : {
3929 [ - + ]: 1480 : struct mptcp_sock *msk = mptcp_sk(sock->sk);
3930 : 1480 : struct sock *sk = sock->sk;
3931 : 1480 : struct sock *ssk;
3932 : 1480 : int err;
3933 : :
3934 [ - + ]: 1480 : pr_debug("msk=%p\n", msk);
3935 : :
3936 : 1480 : lock_sock(sk);
3937 : :
3938 : 1480 : err = -EINVAL;
3939 [ + - - + ]: 1480 : if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
3940 : 0 : goto unlock;
3941 : :
3942 : 1480 : ssk = __mptcp_nmpc_sk(msk);
3943 [ - + ]: 1480 : if (IS_ERR(ssk)) {
3944 : 0 : err = PTR_ERR(ssk);
3945 : 0 : goto unlock;
3946 : : }
3947 : :
3948 : 1480 : mptcp_set_state(sk, TCP_LISTEN);
3949 : 1480 : sock_set_flag(sk, SOCK_RCU_FREE);
3950 : :
3951 : 1480 : lock_sock(ssk);
3952 : 1480 : err = __inet_listen_sk(ssk, backlog);
3953 : 1480 : release_sock(ssk);
3954 : 1480 : mptcp_set_state(sk, inet_sk_state_load(ssk));
3955 : :
3956 [ - + ]: 1480 : if (!err) {
3957 : 1480 : sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3958 : 1480 : mptcp_copy_inaddrs(sk, ssk);
3959 : 1480 : mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CREATED);
3960 : : }
3961 : :
3962 : 0 : unlock:
3963 : 1480 : release_sock(sk);
3964 : 1480 : return err;
3965 : : }
3966 : :
3967 : 1502 : static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
3968 : : struct proto_accept_arg *arg)
3969 : : {
3970 [ - + ]: 1502 : struct mptcp_sock *msk = mptcp_sk(sock->sk);
3971 : 1502 : struct sock *ssk, *newsk;
3972 : :
3973 [ - + ]: 1502 : pr_debug("msk=%p\n", msk);
3974 : :
3975 : : /* Buggy applications can call accept on socket states other then LISTEN
3976 : : * but no need to allocate the first subflow just to error out.
3977 : : */
3978 : 1502 : ssk = READ_ONCE(msk->first);
3979 [ + - ]: 1502 : if (!ssk)
3980 : : return -EINVAL;
3981 : :
3982 [ - + ]: 1502 : pr_debug("ssk=%p, listener=%p\n", ssk, mptcp_subflow_ctx(ssk));
3983 : 1502 : newsk = inet_csk_accept(ssk, arg);
3984 [ + + ]: 1502 : if (!newsk)
3985 : 6 : return arg->err;
3986 : :
3987 [ - + - - ]: 1496 : pr_debug("newsk=%p, subflow is mptcp=%d\n", newsk, sk_is_mptcp(newsk));
[ - + ]
3988 [ - + + + ]: 1496 : if (sk_is_mptcp(newsk)) {
[ + + ]
3989 : 1342 : struct mptcp_subflow_context *subflow;
3990 : 1342 : struct sock *new_mptcp_sock;
3991 : :
3992 [ - + ]: 1342 : subflow = mptcp_subflow_ctx(newsk);
3993 : 1342 : new_mptcp_sock = subflow->conn;
3994 : :
3995 : : /* is_mptcp should be false if subflow->conn is missing, see
3996 : : * subflow_syn_recv_sock()
3997 : : */
3998 [ - + ]: 1342 : if (WARN_ON_ONCE(!new_mptcp_sock)) {
3999 [ # # ]: 0 : tcp_sk(newsk)->is_mptcp = 0;
4000 : 0 : goto tcpfallback;
4001 : : }
4002 : :
4003 : 1342 : newsk = new_mptcp_sock;
4004 [ + - ]: 1342 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPCAPABLEPASSIVEACK);
4005 : :
4006 [ - + ]: 1342 : newsk->sk_kern_sock = arg->kern;
4007 : 1342 : lock_sock(newsk);
4008 : 1342 : __inet_accept(sock, newsock, newsk);
4009 : :
4010 : 1342 : set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags);
4011 [ - + ]: 1342 : msk = mptcp_sk(newsk);
4012 : 1342 : msk->in_accept_queue = 0;
4013 : :
4014 : : /* set ssk->sk_socket of accept()ed flows to mptcp socket.
4015 : : * This is needed so NOSPACE flag can be set from tcp stack.
4016 : : */
4017 [ + + ]: 2709 : mptcp_for_each_subflow(msk, subflow) {
4018 [ + - ]: 1367 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4019 : :
4020 [ + - ]: 1367 : if (!ssk->sk_socket)
4021 : 1367 : mptcp_sock_graft(ssk, newsock);
4022 : : }
4023 : :
4024 : 1342 : mptcp_rps_record_subflows(msk);
4025 : :
4026 : : /* Do late cleanup for the first subflow as necessary. Also
4027 : : * deal with bad peers not doing a complete shutdown.
4028 : : */
4029 [ + + ]: 1342 : if (unlikely(inet_sk_state_load(msk->first) == TCP_CLOSE)) {
4030 : 6 : __mptcp_close_ssk(newsk, msk->first,
4031 : 6 : mptcp_subflow_ctx(msk->first), 0);
4032 [ - + ]: 6 : if (unlikely(list_is_singular(&msk->conn_list)))
4033 : 6 : mptcp_set_state(newsk, TCP_CLOSE);
4034 : : }
4035 : : } else {
4036 : 154 : tcpfallback:
4037 [ - + ]: 154 : newsk->sk_kern_sock = arg->kern;
4038 : 154 : lock_sock(newsk);
4039 : 154 : __inet_accept(sock, newsock, newsk);
4040 : : /* we are being invoked after accepting a non-mp-capable
4041 : : * flow: sk is a tcp_sk, not an mptcp one.
4042 : : *
4043 : : * Hand the socket over to tcp so all further socket ops
4044 : : * bypass mptcp.
4045 : : */
4046 : 154 : WRITE_ONCE(newsock->sk->sk_socket->ops,
4047 : : mptcp_fallback_tcp_ops(newsock->sk));
4048 : : }
4049 : 1496 : release_sock(newsk);
4050 : :
4051 : 1496 : return 0;
4052 : : }
4053 : :
4054 : 474808 : static __poll_t mptcp_check_writeable(struct mptcp_sock *msk)
4055 : : {
4056 : 474808 : struct sock *sk = (struct sock *)msk;
4057 : :
4058 [ + + ]: 474808 : if (__mptcp_stream_is_writeable(sk, 1))
4059 : : return EPOLLOUT | EPOLLWRNORM;
4060 : :
4061 : 122892 : set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
4062 : 122892 : smp_mb__after_atomic(); /* NOSPACE is changed by mptcp_write_space() */
4063 [ + + ]: 122892 : if (__mptcp_stream_is_writeable(sk, 1))
4064 : : return EPOLLOUT | EPOLLWRNORM;
4065 : :
4066 : : return 0;
4067 : : }
4068 : :
4069 : 771868 : static __poll_t mptcp_poll(struct file *file, struct socket *sock,
4070 : : struct poll_table_struct *wait)
4071 : : {
4072 : 771868 : struct sock *sk = sock->sk;
4073 : 771868 : struct mptcp_sock *msk;
4074 : 771868 : __poll_t mask = 0;
4075 : 771868 : u8 shutdown;
4076 : 771868 : int state;
4077 : :
4078 [ - + ]: 771868 : msk = mptcp_sk(sk);
4079 : 771868 : sock_poll_wait(file, sock, wait);
4080 : :
4081 : 771868 : state = inet_sk_state_load(sk);
4082 [ - + ]: 771868 : pr_debug("msk=%p state=%d flags=%lx\n", msk, state, msk->flags);
4083 [ + + ]: 771868 : if (state == TCP_LISTEN) {
4084 : 2440 : struct sock *ssk = READ_ONCE(msk->first);
4085 : :
4086 [ - + ]: 2440 : if (WARN_ON_ONCE(!ssk))
4087 : 0 : return 0;
4088 : :
4089 [ + + ]: 3660 : return inet_csk_listen_poll(ssk);
4090 : : }
4091 : :
4092 : 769428 : shutdown = READ_ONCE(sk->sk_shutdown);
4093 [ + + ]: 769428 : if (shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
4094 : 8941 : mask |= EPOLLHUP;
4095 [ + + ]: 769428 : if (shutdown & RCV_SHUTDOWN)
4096 : 174910 : mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
4097 : :
4098 [ + - ]: 769428 : if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
4099 : 769428 : mask |= mptcp_check_readable(sk);
4100 [ + + ]: 769428 : if (shutdown & SEND_SHUTDOWN)
4101 : 294620 : mask |= EPOLLOUT | EPOLLWRNORM;
4102 : : else
4103 : 474808 : mask |= mptcp_check_writeable(msk);
4104 [ # # # # ]: 0 : } else if (state == TCP_SYN_SENT &&
[ # # ]
4105 [ # # ]: 0 : inet_test_bit(DEFER_CONNECT, sk)) {
4106 : : /* cf tcp_poll() note about TFO */
4107 : 0 : mask |= EPOLLOUT | EPOLLWRNORM;
4108 : : }
4109 : :
4110 : : /* This barrier is coupled with smp_wmb() in __mptcp_error_report() */
4111 : 769428 : smp_rmb();
4112 [ + + ]: 769428 : if (READ_ONCE(sk->sk_err))
4113 : 9 : mask |= EPOLLERR;
4114 : :
4115 : : return mask;
4116 : : }
4117 : :
4118 : : static const struct proto_ops mptcp_stream_ops = {
4119 : : .family = PF_INET,
4120 : : .owner = THIS_MODULE,
4121 : : .release = inet_release,
4122 : : .bind = mptcp_bind,
4123 : : .connect = inet_stream_connect,
4124 : : .socketpair = sock_no_socketpair,
4125 : : .accept = mptcp_stream_accept,
4126 : : .getname = inet_getname,
4127 : : .poll = mptcp_poll,
4128 : : .ioctl = inet_ioctl,
4129 : : .gettstamp = sock_gettstamp,
4130 : : .listen = mptcp_listen,
4131 : : .shutdown = inet_shutdown,
4132 : : .setsockopt = sock_common_setsockopt,
4133 : : .getsockopt = sock_common_getsockopt,
4134 : : .sendmsg = inet_sendmsg,
4135 : : .recvmsg = inet_recvmsg,
4136 : : .mmap = sock_no_mmap,
4137 : : .set_rcvlowat = mptcp_set_rcvlowat,
4138 : : };
4139 : :
4140 : : static struct inet_protosw mptcp_protosw = {
4141 : : .type = SOCK_STREAM,
4142 : : .protocol = IPPROTO_MPTCP,
4143 : : .prot = &mptcp_prot,
4144 : : .ops = &mptcp_stream_ops,
4145 : : .flags = INET_PROTOSW_ICSK,
4146 : : };
4147 : :
4148 : 34815 : static int mptcp_napi_poll(struct napi_struct *napi, int budget)
4149 : : {
4150 : 34815 : struct mptcp_delegated_action *delegated;
4151 : 34815 : struct mptcp_subflow_context *subflow;
4152 : 34815 : int work_done = 0;
4153 : :
4154 : 34815 : delegated = container_of(napi, struct mptcp_delegated_action, napi);
4155 [ + + ]: 70660 : while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
4156 : 35845 : struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
4157 : :
4158 : 35845 : bh_lock_sock_nested(ssk);
4159 [ + + ]: 35845 : if (!sock_owned_by_user(ssk)) {
4160 : 23002 : mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
4161 : : } else {
4162 : : /* tcp_release_cb_override already processed
4163 : : * the action or will do at next release_sock().
4164 : : * In both case must dequeue the subflow here - on the same
4165 : : * CPU that scheduled it.
4166 : : */
4167 : 12843 : smp_wmb();
4168 : 12843 : clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
4169 : : }
4170 : 35845 : bh_unlock_sock(ssk);
4171 : 35845 : sock_put(ssk);
4172 : :
4173 [ + - ]: 35845 : if (++work_done == budget)
4174 : : return budget;
4175 : : }
4176 : :
4177 : : /* always provide a 0 'work_done' argument, so that napi_complete_done
4178 : : * will not try accessing the NULL napi->dev ptr
4179 : : */
4180 : 34815 : napi_complete_done(napi, 0);
4181 : 34815 : return work_done;
4182 : : }
4183 : :
4184 : 6 : void __init mptcp_proto_init(void)
4185 : : {
4186 : 6 : struct mptcp_delegated_action *delegated;
4187 : 6 : int cpu;
4188 : :
4189 : 6 : mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
4190 : :
4191 [ - + ]: 6 : if (percpu_counter_init(&mptcp_sockets_allocated, 0, GFP_KERNEL))
4192 : 0 : panic("Failed to allocate MPTCP pcpu counter\n");
4193 : :
4194 : 6 : mptcp_napi_dev = alloc_netdev_dummy(0);
4195 [ - + ]: 6 : if (!mptcp_napi_dev)
4196 : 0 : panic("Failed to allocate MPTCP dummy netdev\n");
4197 [ + - + - ]: 54 : for_each_possible_cpu(cpu) {
4198 : 24 : delegated = per_cpu_ptr(&mptcp_delegated_actions, cpu);
4199 : 24 : INIT_LIST_HEAD(&delegated->head);
4200 : 24 : netif_napi_add_tx(mptcp_napi_dev, &delegated->napi,
4201 : : mptcp_napi_poll);
4202 : 24 : napi_enable(&delegated->napi);
4203 : : }
4204 : :
4205 : 6 : mptcp_subflow_init();
4206 : 6 : mptcp_pm_init();
4207 : 6 : mptcp_sched_init();
4208 : 6 : mptcp_token_init();
4209 : :
4210 [ - + ]: 6 : if (proto_register(&mptcp_prot, 1) != 0)
4211 : 0 : panic("Failed to register MPTCP proto.\n");
4212 : :
4213 : 6 : inet_register_protosw(&mptcp_protosw);
4214 : :
4215 : 6 : BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
4216 : 6 : }
4217 : :
4218 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4219 : : static const struct proto_ops mptcp_v6_stream_ops = {
4220 : : .family = PF_INET6,
4221 : : .owner = THIS_MODULE,
4222 : : .release = inet6_release,
4223 : : .bind = mptcp_bind,
4224 : : .connect = inet_stream_connect,
4225 : : .socketpair = sock_no_socketpair,
4226 : : .accept = mptcp_stream_accept,
4227 : : .getname = inet6_getname,
4228 : : .poll = mptcp_poll,
4229 : : .ioctl = inet6_ioctl,
4230 : : .gettstamp = sock_gettstamp,
4231 : : .listen = mptcp_listen,
4232 : : .shutdown = inet_shutdown,
4233 : : .setsockopt = sock_common_setsockopt,
4234 : : .getsockopt = sock_common_getsockopt,
4235 : : .sendmsg = inet6_sendmsg,
4236 : : .recvmsg = inet6_recvmsg,
4237 : : .mmap = sock_no_mmap,
4238 : : #ifdef CONFIG_COMPAT
4239 : : .compat_ioctl = inet6_compat_ioctl,
4240 : : #endif
4241 : : .set_rcvlowat = mptcp_set_rcvlowat,
4242 : : };
4243 : :
4244 : : static struct proto mptcp_v6_prot;
4245 : :
4246 : : static struct inet_protosw mptcp_v6_protosw = {
4247 : : .type = SOCK_STREAM,
4248 : : .protocol = IPPROTO_MPTCP,
4249 : : .prot = &mptcp_v6_prot,
4250 : : .ops = &mptcp_v6_stream_ops,
4251 : : .flags = INET_PROTOSW_ICSK,
4252 : : };
4253 : :
4254 : 6 : int __init mptcp_proto_v6_init(void)
4255 : : {
4256 : 6 : int err;
4257 : :
4258 : 6 : mptcp_v6_prot = mptcp_prot;
4259 : 6 : strscpy(mptcp_v6_prot.name, "MPTCPv6", sizeof(mptcp_v6_prot.name));
4260 : 6 : mptcp_v6_prot.slab = NULL;
4261 : 6 : mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock);
4262 : 6 : mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np);
4263 : :
4264 : 6 : err = proto_register(&mptcp_v6_prot, 1);
4265 [ + - ]: 6 : if (err)
4266 : : return err;
4267 : :
4268 : 6 : err = inet6_register_protosw(&mptcp_v6_protosw);
4269 [ - + ]: 6 : if (err)
4270 : 0 : proto_unregister(&mptcp_v6_prot);
4271 : :
4272 : : return err;
4273 : : }
4274 : : #endif
|