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 <crypto/sha2.h>
13 : : #include <crypto/utils.h>
14 : : #include <net/sock.h>
15 : : #include <net/inet_common.h>
16 : : #include <net/inet_hashtables.h>
17 : : #include <net/protocol.h>
18 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
19 : : #include <net/ip6_route.h>
20 : : #include <net/transp_v6.h>
21 : : #endif
22 : : #include <net/mptcp.h>
23 : :
24 : : #include "protocol.h"
25 : : #include "mib.h"
26 : :
27 : : #include <trace/events/mptcp.h>
28 : : #include <trace/events/sock.h>
29 : :
30 : : static void mptcp_subflow_ops_undo_override(struct sock *ssk);
31 : :
32 : 0 : static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
33 : : enum linux_mptcp_mib_field field)
34 : : {
35 [ - - - - : 78 : MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
- - - - +
- - - + -
+ - - - -
- - - - -
- - - - -
- - - -
- ]
36 : : }
37 : :
38 : 2594 : static void subflow_req_destructor(struct request_sock *req)
39 : : {
40 : 2594 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
41 : :
42 [ - + - - ]: 2594 : pr_debug("subflow_req=%p\n", subflow_req);
43 : :
44 [ + + ]: 2594 : if (subflow_req->msk)
45 : 30 : sock_put((struct sock *)subflow_req->msk);
46 : :
47 : 2594 : mptcp_token_destroy_request(req);
48 : 2594 : }
49 : :
50 : 0 : void mptcp_subflow_reqsk_clone(struct request_sock *req,
51 : : struct request_sock *new_req)
52 : : {
53 : 0 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
54 : 0 : struct mptcp_subflow_request_sock *new_subflow_req;
55 : 0 : struct mptcp_sock *msk;
56 : :
57 : 0 : new_subflow_req = mptcp_subflow_rsk(new_req);
58 [ # # # # : 0 : RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
# # # # #
# ]
59 : : "MPTCP reqsk clone called without RCU");
60 : :
61 : : /* A non-NULL ->msk means the request owns one reference. The clone
62 : : * copied only the pointer, while the original request can concurrently
63 : : * transfer its reference to the child. Acquire a reference for the
64 : : * clone, then verify that the original request still owns the same msk.
65 : : * MPTCP sockets use SLAB_TYPESAFE_BY_RCU and all clone callers run in
66 : : * an RCU read-side critical section, keeping the memory stable here.
67 : : */
68 : 0 : msk = READ_ONCE(subflow_req->msk);
69 [ # # ]: 0 : if (msk) {
70 : 0 : struct sock *sk = (struct sock *)msk;
71 : :
72 [ # # ]: 0 : if (!refcount_inc_not_zero_acquire(&sk->sk_refcnt)) {
73 : : msk = NULL;
74 [ # # ]: 0 : } else if (READ_ONCE(subflow_req->msk) != msk) {
75 : 0 : sock_put(sk);
76 : 0 : msk = NULL;
77 : : }
78 : : }
79 : :
80 : 0 : new_subflow_req->msk = msk;
81 : :
82 : 0 : mptcp_token_move_request(req, new_req);
83 : 0 : }
84 : :
85 : 2574 : static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
86 : : void *hmac)
87 : : {
88 : 2574 : u8 msg[8];
89 : :
90 : 2574 : put_unaligned_be32(nonce1, &msg[0]);
91 : 2574 : put_unaligned_be32(nonce2, &msg[4]);
92 : :
93 : 2574 : mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
94 : 2574 : }
95 : :
96 : 678 : static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
97 : : {
98 [ + - + + ]: 678 : return mptcp_is_fully_established((void *)msk) &&
[ + - ]
99 [ + + ]: 68 : ((mptcp_pm_is_userspace(msk) &&
[ - + - - ]
100 : 26 : mptcp_userspace_pm_active(msk)) ||
101 [ - + + + ]: 656 : READ_ONCE(msk->pm.accept_subflow));
[ + + ]
102 : : }
103 : :
104 : : /* validate received token and create truncated hmac and nonce for SYN-ACK */
105 : 658 : static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req)
106 : : {
107 : 658 : struct mptcp_sock *msk = subflow_req->msk;
108 : 658 : u8 hmac[SHA256_DIGEST_SIZE];
109 : :
110 : 658 : subflow_req->local_nonce = get_random_u32();
111 : :
112 : 658 : subflow_generate_hmac(READ_ONCE(msk->local_key),
113 : 658 : READ_ONCE(msk->remote_key),
114 : : subflow_req->local_nonce,
115 : : subflow_req->remote_nonce, hmac);
116 : :
117 : 658 : subflow_req->thmac = get_unaligned_be64(hmac);
118 : 658 : }
119 : :
120 : 664 : static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
121 : : {
122 : 664 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
123 : 664 : struct mptcp_sock *msk;
124 : 664 : int local_id;
125 : :
126 : 664 : msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token);
127 [ + + ]: 664 : if (!msk) {
128 [ + - ]: 6 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
129 : 6 : return NULL;
130 : : }
131 : :
132 : 658 : local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
133 [ - + ]: 658 : if (local_id < 0) {
134 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPJOINNOIDFOUND);
135 : 0 : sock_put((struct sock *)msk);
136 : 0 : return NULL;
137 : : }
138 : 658 : subflow_req->local_id = local_id;
139 : 658 : subflow_req->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)req);
140 : :
141 : 658 : return msk;
142 : : }
143 : :
144 : 2594 : static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
145 : : {
146 : 2594 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
147 : :
148 : 2594 : subflow_req->mp_capable = 0;
149 : 2594 : subflow_req->mp_join = 0;
150 : 2594 : subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener));
151 : 2594 : subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener));
152 : 2594 : subflow_req->msk = NULL;
153 : 2594 : mptcp_token_init_request(req);
154 : 2594 : }
155 : :
156 : 84 : static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
157 : : {
158 : 1306 : return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
159 : : }
160 : :
161 : 18 : static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
162 : : {
163 : 18 : struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
164 : :
165 [ + - ]: 18 : if (mpext) {
166 : 18 : memset(mpext, 0, sizeof(*mpext));
167 : 18 : mpext->reset_reason = reason;
168 : : }
169 : 18 : }
170 : :
171 : 2 : static int subflow_reset_req_endp(struct request_sock *req, struct sk_buff *skb)
172 : : {
173 [ + - ]: 2 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEENDPATTEMPT);
174 : 2 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
175 : 2 : return -EPERM;
176 : : }
177 : :
178 : : /* Init mptcp request socket.
179 : : *
180 : : * Returns an error code if a JOIN has failed and a TCP reset
181 : : * should be sent.
182 : : */
183 : 2522 : static int subflow_check_req(struct request_sock *req,
184 : : const struct sock *sk_listener,
185 : : struct sk_buff *skb)
186 : : {
187 [ - + ]: 2522 : struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
188 : 2522 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
189 : 2522 : struct mptcp_options_received mp_opt;
190 : 2522 : bool opt_mp_capable, opt_mp_join;
191 : :
192 [ - + - - ]: 2522 : pr_debug("subflow_req=%p, listener=%p\n", subflow_req, listener);
193 : :
194 : : #ifdef CONFIG_TCP_MD5SIG
195 : : /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
196 : : * TCP option space.
197 : : */
198 : : if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info)) {
199 : : MPTCP_INC_STATS(sock_net(sk_listener), MPTCP_MIB_MD5SIGRESET);
200 : : subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
201 : : return -EINVAL;
202 : : }
203 : : #endif
204 : :
205 : 2522 : mptcp_get_options(skb, &mp_opt);
206 : :
207 : 2522 : opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYN);
208 : 2522 : opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYN);
209 [ + + ]: 2522 : if (opt_mp_capable) {
210 [ + - ]: 1698 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
211 : :
212 [ - + + + ]: 1698 : if (unlikely(listener->pm_listener))
[ + + ]
213 : 2 : return subflow_reset_req_endp(req, skb);
214 [ + + ]: 824 : } else if (opt_mp_join) {
215 [ + - ]: 664 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
216 : :
217 [ + + ]: 664 : if (mp_opt.backup)
218 [ + - ]: 42 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNBACKUPRX);
219 [ - + - + ]: 160 : } else if (unlikely(listener->pm_listener)) {
[ - + ]
220 : 0 : return subflow_reset_req_endp(req, skb);
221 : : }
222 : :
223 [ + + + - ]: 2520 : if (opt_mp_capable && listener->request_mptcp) {
224 : 1696 : int err, retries = MPTCP_TOKEN_MAX_RETRIES;
225 : :
226 : 1696 : subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
227 : : again:
228 : 1696 : do {
229 : 1696 : get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
230 [ - + ]: 1696 : } while (subflow_req->local_key == 0);
231 : :
232 [ + + ]: 1696 : if (unlikely(req->syncookie)) {
233 : 52 : mptcp_crypto_key_sha(subflow_req->local_key,
234 : : &subflow_req->token,
235 : : &subflow_req->idsn);
236 [ - + ]: 52 : if (mptcp_token_exists(subflow_req->token)) {
237 [ # # ]: 0 : if (retries-- > 0)
238 : 0 : goto again;
239 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
240 : : } else {
241 : 52 : subflow_req->mp_capable = 1;
242 : : }
243 : 52 : return 0;
244 : : }
245 : :
246 : 1644 : err = mptcp_token_new_request(req);
247 [ + - ]: 1644 : if (err == 0)
248 : 1644 : subflow_req->mp_capable = 1;
249 [ # # ]: 0 : else if (retries-- > 0)
250 : 0 : goto again;
251 : : else
252 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
253 : :
254 [ + + - + ]: 824 : } else if (opt_mp_join && listener->request_mptcp) {
255 : 664 : subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
256 : 664 : subflow_req->mp_join = 1;
257 : 664 : subflow_req->backup = mp_opt.backup;
258 : 664 : subflow_req->remote_id = mp_opt.join_id;
259 : 664 : subflow_req->token = mp_opt.token;
260 : 664 : subflow_req->remote_nonce = mp_opt.nonce;
261 : 664 : subflow_req->msk = subflow_token_join_request(req);
262 : :
263 : : /* Can't fall back to TCP in this case. */
264 [ + + ]: 664 : if (!subflow_req->msk) {
265 : 6 : subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
266 : 6 : return -EPERM;
267 : : }
268 : :
269 [ + + ]: 658 : if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
270 [ - + - - ]: 26 : pr_debug("syn inet_sport=%d %d\n",
271 : : ntohs(inet_sk(sk_listener)->inet_sport),
272 : : ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
273 [ - + ]: 26 : if (!mptcp_pm_announced_has_ssk(subflow_req->msk, sk_listener)) {
274 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
275 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
276 : 0 : return -EPERM;
277 : : }
278 [ + - ]: 26 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
279 : : }
280 : :
281 : 658 : subflow_req_create_thmac(subflow_req);
282 : :
283 [ + + ]: 658 : if (unlikely(req->syncookie)) {
284 [ + + ]: 22 : if (!mptcp_can_accept_new_subflow(subflow_req->msk)) {
285 [ + - ]: 2 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED);
286 : 2 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
287 : 2 : return -EPERM;
288 : : }
289 : :
290 : 20 : subflow_init_req_cookie_join_save(subflow_req, skb);
291 : : }
292 : :
293 [ + - - - ]: 656 : pr_debug("token=%u, remote_nonce=%u msk=%p\n", subflow_req->token,
294 : : subflow_req->remote_nonce, subflow_req->msk);
295 : : }
296 : :
297 : : return 0;
298 : : }
299 : :
300 : 72 : int mptcp_subflow_init_cookie_req(struct request_sock *req,
301 : : const struct sock *sk_listener,
302 : : struct sk_buff *skb)
303 : : {
304 : 72 : struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
305 : 72 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
306 : 72 : struct mptcp_options_received mp_opt;
307 : 72 : bool opt_mp_capable, opt_mp_join;
308 : 72 : int err;
309 : :
310 : 72 : subflow_init_req(req, sk_listener);
311 : 72 : mptcp_get_options(skb, &mp_opt);
312 : :
313 : 72 : opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_ACK);
314 : 72 : opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK);
315 [ + + + - ]: 72 : if (opt_mp_capable && listener->request_mptcp) {
316 [ + - ]: 52 : if (mp_opt.sndr_key == 0)
317 : : return -EINVAL;
318 : :
319 : 52 : subflow_req->local_key = mp_opt.rcvr_key;
320 : 52 : err = mptcp_token_new_request(req);
321 [ + - ]: 52 : if (err)
322 : : return err;
323 : :
324 : 52 : subflow_req->mp_capable = 1;
325 : 52 : subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
326 [ - + - + ]: 20 : } else if (opt_mp_join && listener->request_mptcp) {
327 [ + - ]: 20 : if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
328 : : return -EINVAL;
329 : :
330 : 20 : subflow_req->mp_join = 1;
331 : 20 : subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
332 : : }
333 : :
334 : : return 0;
335 : : }
336 : : EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
337 : :
338 : 16 : static enum sk_rst_reason mptcp_get_rst_reason(const struct sk_buff *skb)
339 : : {
340 [ + - ]: 16 : const struct mptcp_ext *mpext = mptcp_get_ext(skb);
341 : :
342 [ + - ]: 16 : if (!mpext)
343 : : return SK_RST_REASON_NOT_SPECIFIED;
344 : :
345 [ + - ]: 16 : return sk_rst_convert_mptcp_reason(mpext->reset_reason);
346 : : }
347 : :
348 : 1952 : static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
349 : : struct sk_buff *skb,
350 : : struct flowi *fl,
351 : : struct request_sock *req,
352 : : u32 tw_isn)
353 : : {
354 : 1952 : struct dst_entry *dst;
355 : 1952 : int err;
356 : :
357 : 1952 : tcp_rsk(req)->is_mptcp = 1;
358 : 1952 : subflow_init_req(req, sk);
359 : :
360 : 1952 : dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req, tw_isn);
361 [ - + ]: 1952 : if (!dst)
362 : : return NULL;
363 : :
364 : 1952 : err = subflow_check_req(req, sk, skb);
365 [ + + ]: 1952 : if (err == 0)
366 : : return dst;
367 : :
368 : 8 : dst_release(dst);
369 [ + + ]: 8 : if (!req->syncookie)
370 : 6 : tcp_request_sock_ops.send_reset(sk, skb,
371 : : mptcp_get_rst_reason(skb));
372 : : return NULL;
373 : : }
374 : :
375 : 2526 : static void subflow_prep_synack(const struct sock *sk, struct request_sock *req,
376 : : struct tcp_fastopen_cookie *foc,
377 : : enum tcp_synack_type synack_type)
378 : : {
379 [ + + ]: 2526 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
380 : 2526 : struct inet_request_sock *ireq = inet_rsk(req);
381 : :
382 : : /* clear tstamp_ok, as needed depending on cookie */
383 [ + + + + ]: 2526 : if (foc && foc->len > -1)
384 : 32 : ireq->tstamp_ok = 0;
385 : :
386 [ + + ]: 2526 : if (synack_type == TCP_SYNACK_FASTOPEN)
387 : 56 : mptcp_fastopen_subflow_synack_set_params(subflow, req);
388 : 2526 : }
389 : :
390 : 1948 : static int subflow_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
391 : : struct flowi *fl,
392 : : struct request_sock *req,
393 : : struct tcp_fastopen_cookie *foc,
394 : : enum tcp_synack_type synack_type,
395 : : struct sk_buff *syn_skb)
396 : : {
397 : 1948 : subflow_prep_synack(sk, req, foc, synack_type);
398 : :
399 : 1948 : return tcp_request_sock_ipv4_ops.send_synack(sk, dst, fl, req, foc,
400 : : synack_type, syn_skb);
401 : : }
402 : :
403 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
404 : 578 : static int subflow_v6_send_synack(const struct sock *sk, struct dst_entry *dst,
405 : : struct flowi *fl,
406 : : struct request_sock *req,
407 : : struct tcp_fastopen_cookie *foc,
408 : : enum tcp_synack_type synack_type,
409 : : struct sk_buff *syn_skb)
410 : : {
411 : 578 : subflow_prep_synack(sk, req, foc, synack_type);
412 : :
413 : 578 : return tcp_request_sock_ipv6_ops.send_synack(sk, dst, fl, req, foc,
414 : : synack_type, syn_skb);
415 : : }
416 : :
417 : 570 : static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
418 : : struct sk_buff *skb,
419 : : struct flowi *fl,
420 : : struct request_sock *req,
421 : : u32 tw_isn)
422 : : {
423 : 570 : struct dst_entry *dst;
424 : 570 : int err;
425 : :
426 : 570 : tcp_rsk(req)->is_mptcp = 1;
427 : 570 : subflow_init_req(req, sk);
428 : :
429 : 570 : dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req, tw_isn);
430 [ - + ]: 570 : if (!dst)
431 : : return NULL;
432 : :
433 : 570 : err = subflow_check_req(req, sk, skb);
434 [ + + ]: 570 : if (err == 0)
435 : : return dst;
436 : :
437 : 2 : dst_release(dst);
438 [ - + ]: 2 : if (!req->syncookie)
439 : 2 : tcp6_request_sock_ops.send_reset(sk, skb,
440 : : mptcp_get_rst_reason(skb));
441 : : return NULL;
442 : : }
443 : : #endif
444 : :
445 : : /* validate received truncated hmac and create hmac for third ACK */
446 : 630 : static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow,
447 : : u64 thmac)
448 : : {
449 : 630 : u8 hmac[SHA256_DIGEST_SIZE];
450 : 630 : u64 expected_thmac;
451 : :
452 : 630 : subflow_generate_hmac(subflow->remote_key, subflow->local_key,
453 : : subflow->remote_nonce, subflow->local_nonce,
454 : : hmac);
455 : :
456 [ - + ]: 630 : expected_thmac = get_unaligned_be64(hmac);
457 [ - + - - ]: 630 : pr_debug("subflow=%p, token=%u, expected_thmac=%llu, thmac=%llu\n",
458 : : subflow, subflow->token, expected_thmac, thmac);
459 : :
460 : 630 : return expected_thmac == thmac;
461 : : }
462 : :
463 : 10 : void mptcp_subflow_reset(struct sock *ssk)
464 : : {
465 [ + - ]: 10 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
466 : 10 : struct sock *sk = subflow->conn;
467 : :
468 : : /* mptcp_mp_fail_no_response() can reach here on an already closed
469 : : * socket
470 : : */
471 [ + - ]: 10 : if (ssk->sk_state == TCP_CLOSE)
472 : : return;
473 : :
474 : : /* must hold: tcp_done() could drop last reference on parent */
475 : 10 : sock_hold(sk);
476 : :
477 : 10 : subflow->resetting = 1;
478 : :
479 : : /* No need to delay the actual close for to-be discarded data. */
480 : 10 : __skb_queue_purge(&ssk->sk_receive_queue);
481 : 10 : mptcp_send_active_reset_reason(ssk);
482 : 10 : tcp_done(ssk);
483 [ - + + - ]: 15 : if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags))
484 : 10 : mptcp_schedule_work(sk);
485 : :
486 : 10 : sock_put(sk);
487 : : }
488 : :
489 : 42 : static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
490 : : {
491 : 630 : return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
492 : : }
493 : :
494 : 1700 : void __mptcp_sync_state(struct sock *sk, int state)
495 : : {
496 : 1700 : struct mptcp_subflow_context *subflow;
497 [ - + ]: 1700 : struct mptcp_sock *msk = mptcp_sk(sk);
498 : 1700 : struct sock *ssk = msk->first;
499 : :
500 [ + + ]: 1700 : subflow = mptcp_subflow_ctx(ssk);
501 [ + + ]: 1700 : __mptcp_propagate_sndbuf(sk, ssk);
502 : :
503 [ + - ]: 1700 : if (sk->sk_state == TCP_SYN_SENT) {
504 : : /* subflow->idsn is always available is TCP_SYN_SENT state,
505 : : * even for the FASTOPEN scenarios
506 : : */
507 : 1700 : WRITE_ONCE(msk->write_seq, subflow->idsn + 1);
508 : 1700 : WRITE_ONCE(msk->snd_nxt, msk->write_seq);
509 : 1700 : mptcp_set_state(sk, state);
510 : 1700 : sk->sk_state_change(sk);
511 : : }
512 : 1700 : }
513 : :
514 : 5250 : static void subflow_set_remote_key(struct mptcp_sock *msk,
515 : : struct mptcp_subflow_context *subflow,
516 : : const struct mptcp_options_received *mp_opt)
517 : : {
518 : : /* active MPC subflow will reach here multiple times:
519 : : * at subflow_finish_connect() time and at 4th ack time
520 : : */
521 [ + + ]: 5250 : if (subflow->remote_key_valid)
522 : : return;
523 : :
524 : 3180 : subflow->remote_key_valid = 1;
525 : 3180 : subflow->remote_key = mp_opt->sndr_key;
526 : 3180 : mptcp_crypto_key_sha(subflow->remote_key, NULL, &subflow->iasn);
527 : 3180 : subflow->iasn++;
528 : :
529 : : /* for fallback's sake */
530 : 3180 : subflow->map_seq = subflow->iasn;
531 : :
532 : 3180 : WRITE_ONCE(msk->remote_key, subflow->remote_key);
533 : 3180 : WRITE_ONCE(msk->ack_seq, subflow->iasn);
534 : 3180 : WRITE_ONCE(msk->can_ack, true);
535 : 3180 : atomic64_set(&msk->rcv_wnd_sent, subflow->iasn);
536 : : }
537 : :
538 : 1700 : static void mptcp_propagate_state(struct sock *sk, struct sock *ssk,
539 : : struct mptcp_subflow_context *subflow,
540 : : const struct mptcp_options_received *mp_opt)
541 : : {
542 [ - + ]: 1700 : struct mptcp_sock *msk = mptcp_sk(sk);
543 : :
544 : 1700 : mptcp_data_lock(sk);
545 [ + + ]: 1700 : if (mp_opt) {
546 : : /* Options are available only in the non fallback cases
547 : : * avoid updating rx path fields otherwise
548 : : */
549 : 1520 : WRITE_ONCE(msk->snd_una, subflow->idsn + 1);
550 [ - + ]: 1520 : WRITE_ONCE(msk->wnd_end, subflow->idsn + 1 + tcp_sk(ssk)->snd_wnd);
551 : 1520 : subflow_set_remote_key(msk, subflow, mp_opt);
552 : : }
553 : :
554 [ + + ]: 1700 : if (!sock_owned_by_user(sk)) {
555 : 530 : __mptcp_sync_state(sk, ssk->sk_state);
556 : : } else {
557 : 1170 : msk->pending_state = ssk->sk_state;
558 [ - + - - : 1170 : __set_bit(MPTCP_SYNC_STATE, &msk->cb_flags);
- - ]
559 : : }
560 : 1700 : mptcp_data_unlock(sk);
561 : 1700 : }
562 : :
563 : 36971 : static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
564 : : {
565 : 36971 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
566 : 36971 : struct mptcp_options_received mp_opt;
567 : 36971 : struct sock *parent = subflow->conn;
568 : 36971 : struct mptcp_sock *msk;
569 : :
570 : 36971 : subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
571 : :
572 : : /* be sure no special action on any packet other than syn-ack */
573 [ + + ]: 36971 : if (subflow->conn_finished)
574 : 36969 : return;
575 : :
576 [ - + ]: 2332 : msk = mptcp_sk(parent);
577 : 2332 : subflow->rel_write_seq = 1;
578 : 2332 : subflow->conn_finished = 1;
579 : 2332 : subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
580 [ - + - - ]: 2332 : pr_debug("subflow=%p synack seq=%x\n", subflow, subflow->ssn_offset);
581 : :
582 : 2332 : mptcp_get_options(skb, &mp_opt);
583 [ + + ]: 2332 : if (subflow->request_mptcp) {
584 [ + + ]: 1670 : if (!(mp_opt.suboptions & OPTION_MPTCP_MPC_SYNACK)) {
585 [ - + ]: 150 : if (!mptcp_try_fallback(sk,
586 : : MPTCP_MIB_MPCAPABLEACTIVEFALLBACK)) {
587 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(sk),
588 : : MPTCP_MIB_FALLBACKFAILED);
589 : 0 : goto do_reset;
590 : : }
591 : :
592 : 150 : goto fallback;
593 : : }
594 : :
595 [ + + ]: 1520 : if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD)
596 : 116 : WRITE_ONCE(msk->csum_enabled, true);
597 [ + + ]: 1520 : if (mp_opt.deny_join_id0)
598 : 8 : WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
599 : 1520 : subflow->mp_capable = 1;
600 [ + - ]: 1520 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
601 : 1520 : mptcp_finish_connect(sk);
602 : 1520 : mptcp_active_enable(parent);
603 : 1520 : mptcp_propagate_state(parent, sk, subflow, &mp_opt);
604 [ + + ]: 662 : } else if (subflow->request_join) {
605 : 632 : u8 hmac[SHA256_DIGEST_SIZE];
606 : :
607 [ + + ]: 632 : if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_SYNACK)) {
608 [ + - ]: 2 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPJOINSYNACKNOMPJOIN);
609 : 2 : subflow->reset_reason = MPTCP_RST_EMPTCP;
610 : 2 : goto do_reset;
611 : : }
612 : :
613 : 630 : subflow->backup = mp_opt.backup;
614 : 630 : subflow->remote_nonce = mp_opt.nonce;
615 : 630 : WRITE_ONCE(subflow->remote_id, mp_opt.join_id);
616 [ - + - - ]: 630 : pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d\n",
617 : : subflow, mp_opt.thmac, subflow->remote_nonce,
618 : : subflow->backup);
619 : :
620 [ - + ]: 630 : if (!subflow_thmac_valid(subflow, mp_opt.thmac)) {
621 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKMAC);
622 : 0 : subflow->reset_reason = MPTCP_RST_EMPTCP;
623 : 0 : goto do_reset;
624 : : }
625 : :
626 [ - + ]: 630 : if (!mptcp_finish_join(sk))
627 : 0 : goto do_reset;
628 : :
629 : 630 : subflow_generate_hmac(subflow->local_key, subflow->remote_key,
630 : : subflow->local_nonce,
631 : : subflow->remote_nonce,
632 : : hmac);
633 : 630 : memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
634 : :
635 : 630 : subflow->mp_join = 1;
636 [ + - ]: 630 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
637 : :
638 [ + + ]: 630 : if (subflow->backup)
639 [ + - ]: 30 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKBACKUPRX);
640 : :
641 [ + + ]: 630 : if (subflow_use_different_dport(msk, sk)) {
642 [ - + - - ]: 26 : pr_debug("synack inet_dport=%d %d\n",
643 : : ntohs(inet_sk(sk)->inet_dport),
644 : : ntohs(inet_sk(parent)->inet_dport));
645 [ + - ]: 68 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
646 : : }
647 [ + - ]: 30 : } else if (mptcp_check_fallback(sk)) {
648 : : /* It looks like MPTCP is blocked, while TCP is not */
649 [ + + ]: 30 : if (subflow->mpc_drop)
650 : 18 : mptcp_active_disable(parent);
651 : 12 : fallback:
652 : 180 : mptcp_propagate_state(parent, sk, subflow, NULL);
653 : : }
654 : : return;
655 : :
656 : 2 : do_reset:
657 : 2 : subflow->reset_transient = 0;
658 : 2 : mptcp_subflow_reset(sk);
659 : : }
660 : :
661 : 1510 : static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id)
662 : : {
663 [ - + ]: 1438 : WARN_ON_ONCE(local_id < 0 || local_id > 255);
664 : 3128 : WRITE_ONCE(subflow->local_id, local_id);
665 : 3128 : }
666 : :
667 : 11832 : static int subflow_chk_local_id(struct sock *sk)
668 : : {
669 [ - + ]: 11832 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
670 [ - + ]: 11832 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
671 : 11832 : int err;
672 : :
673 [ + + ]: 11832 : if (likely(subflow->local_id >= 0))
674 : : return 0;
675 : :
676 : 324 : err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk);
677 [ + - ]: 324 : if (err < 0)
678 : : return err;
679 : :
680 : 324 : subflow_set_local_id(subflow, err);
681 : 324 : subflow->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)sk);
682 : :
683 : 324 : return 0;
684 : : }
685 : :
686 : 8824 : static int subflow_rebuild_header(struct sock *sk)
687 : : {
688 : 8824 : int err = subflow_chk_local_id(sk);
689 : :
690 [ + - ]: 8824 : if (unlikely(err < 0))
691 : : return err;
692 : :
693 : 8824 : return inet_sk_rebuild_header(sk);
694 : : }
695 : :
696 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
697 : 3008 : static int subflow_v6_rebuild_header(struct sock *sk)
698 : : {
699 : 3008 : int err = subflow_chk_local_id(sk);
700 : :
701 [ + - ]: 3008 : if (unlikely(err < 0))
702 : : return err;
703 : :
704 : 3008 : return inet6_sk_rebuild_header(sk);
705 : : }
706 : : #endif
707 : :
708 : : static struct request_sock_ops mptcp_subflow_v4_request_sock_ops __ro_after_init;
709 : : static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init;
710 : :
711 : 1952 : static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
712 : : {
713 [ - + ]: 1952 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
714 : :
715 [ - + - - ]: 1952 : pr_debug("subflow=%p\n", subflow);
716 : :
717 : : /* Never answer to SYNs sent to broadcast or multicast */
718 [ - + ]: 1952 : if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
719 : 0 : goto drop;
720 : :
721 : 1952 : return tcp_conn_request(&mptcp_subflow_v4_request_sock_ops,
722 : : &subflow_request_sock_ipv4_ops,
723 : : sk, skb);
724 : 0 : drop:
725 : 0 : tcp_listendrop(sk);
726 : 0 : return 0;
727 : : }
728 : :
729 : 2004 : static void subflow_v4_req_destructor(struct request_sock *req)
730 : : {
731 : 2004 : subflow_req_destructor(req);
732 : 2004 : tcp_request_sock_ops.destructor(req);
733 : 2004 : }
734 : :
735 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
736 : : static struct request_sock_ops mptcp_subflow_v6_request_sock_ops __ro_after_init;
737 : : static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init;
738 : : static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init;
739 : : static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init;
740 : : static struct proto tcpv6_prot_override __ro_after_init;
741 : :
742 : 1419 : static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
743 : : {
744 [ - + ]: 1419 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
745 : :
746 [ - + - - ]: 1419 : pr_debug("subflow=%p\n", subflow);
747 : :
748 [ + + ]: 1419 : if (skb->protocol == htons(ETH_P_IP))
749 : 849 : return subflow_v4_conn_request(sk, skb);
750 : :
751 [ - + ]: 570 : if (!ipv6_unicast_destination(skb))
752 : 0 : goto drop;
753 : :
754 [ - + ]: 570 : if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
755 : 0 : __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
756 : 0 : return 0;
757 : : }
758 : :
759 : 570 : return tcp_conn_request(&mptcp_subflow_v6_request_sock_ops,
760 : : &subflow_request_sock_ipv6_ops, sk, skb);
761 : :
762 : 0 : drop:
763 : 0 : tcp_listendrop(sk);
764 : 0 : return 0; /* don't send reset */
765 : : }
766 : :
767 : 590 : static void subflow_v6_req_destructor(struct request_sock *req)
768 : : {
769 : 590 : subflow_req_destructor(req);
770 : 590 : tcp6_request_sock_ops.destructor(req);
771 : 590 : }
772 : : #endif
773 : :
774 : 72 : struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
775 : : struct sock *sk_listener,
776 : : bool attach_listener)
777 : : {
778 [ + + ]: 72 : if (ops->family == AF_INET)
779 : : ops = &mptcp_subflow_v4_request_sock_ops;
780 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
781 [ + - ]: 20 : else if (ops->family == AF_INET6)
782 : 20 : ops = &mptcp_subflow_v6_request_sock_ops;
783 : : #endif
784 : :
785 : 72 : return inet_reqsk_alloc(ops, sk_listener, attach_listener);
786 : : }
787 : : EXPORT_SYMBOL(mptcp_subflow_reqsk_alloc);
788 : :
789 : : /* validate hmac received in third ACK */
790 : 656 : static bool subflow_hmac_valid(const struct mptcp_subflow_request_sock *subflow_req,
791 : : const struct mptcp_options_received *mp_opt)
792 : : {
793 : 656 : struct mptcp_sock *msk = subflow_req->msk;
794 : 656 : u8 hmac[SHA256_DIGEST_SIZE];
795 : :
796 : 656 : subflow_generate_hmac(READ_ONCE(msk->remote_key),
797 : 656 : READ_ONCE(msk->local_key),
798 : 656 : subflow_req->remote_nonce,
799 : 656 : subflow_req->local_nonce, hmac);
800 : :
801 : 656 : return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
802 : : }
803 : :
804 : 228 : static void subflow_ulp_fallback(struct sock *sk,
805 : : struct mptcp_subflow_context *old_ctx)
806 : : {
807 : 228 : struct inet_connection_sock *icsk = inet_csk(sk);
808 : :
809 [ - + ]: 228 : mptcp_subflow_tcp_fallback(sk, old_ctx);
810 : 228 : icsk->icsk_ulp_ops = NULL;
811 : 228 : rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
812 [ - + ]: 228 : tcp_sk(sk)->is_mptcp = 0;
813 : :
814 : 264 : mptcp_subflow_ops_undo_override(sk);
815 : 228 : }
816 : :
817 : 228 : void mptcp_subflow_drop_ctx(struct sock *ssk)
818 : : {
819 [ + + ]: 228 : struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
820 : :
821 [ + + ]: 228 : if (!ctx)
822 : : return;
823 : :
824 [ + - ]: 68 : list_del(&mptcp_subflow_ctx(ssk)->node);
825 [ + - ]: 68 : if (inet_csk(ssk)->icsk_ulp_ops) {
826 : 68 : subflow_ulp_fallback(ssk, ctx);
827 [ + + ]: 68 : if (ctx->conn)
828 : 42 : sock_put(ctx->conn);
829 : : }
830 : :
831 : 68 : kfree_rcu(ctx, rcu);
832 : : }
833 : :
834 : 3730 : void __mptcp_subflow_fully_established(struct mptcp_sock *msk,
835 : : struct mptcp_subflow_context *subflow,
836 : : const struct mptcp_options_received *mp_opt)
837 : : {
838 : 3730 : subflow_set_remote_key(msk, subflow, mp_opt);
839 : 3730 : WRITE_ONCE(subflow->fully_established, true);
840 : 3730 : WRITE_ONCE(msk->fully_established, true);
841 : 3730 : }
842 : :
843 : 2506 : static struct sock *subflow_syn_recv_sock(const struct sock *sk,
844 : : struct sk_buff *skb,
845 : : struct request_sock *req,
846 : : struct dst_entry *dst,
847 : : struct request_sock *req_unhash,
848 : : bool *own_req,
849 : : void (*opt_child_init)(struct sock *newsk,
850 : : const struct sock *sk))
851 : : {
852 [ - + ]: 2506 : struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
853 : 2506 : struct mptcp_subflow_request_sock *subflow_req;
854 : 2506 : struct mptcp_options_received mp_opt;
855 : 2506 : bool fallback, fallback_is_fatal;
856 : 2506 : enum sk_rst_reason reason;
857 : 2506 : struct mptcp_sock *owner;
858 : 2506 : struct sock *child;
859 : :
860 [ - + - - ]: 2506 : pr_debug("listener=%p, req=%p, conn=%p\n", listener, req, listener->conn);
861 : :
862 : : /* After child creation we must look for MPC even when options
863 : : * are not parsed
864 : : */
865 : 2506 : mp_opt.suboptions = 0;
866 : :
867 : : /* hopefully temporary handling for MP_JOIN+syncookie */
868 : 2506 : subflow_req = mptcp_subflow_rsk(req);
869 [ - + + - : 2506 : fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
+ + ]
[ + - + + ]
870 [ - + ]: 2506 : fallback = !tcp_rsk(req)->is_mptcp;
871 [ - + ]: 2506 : if (fallback)
872 : 0 : goto create_child;
873 : :
874 : : /* if the sk is MP_CAPABLE, we try to fetch the client key */
875 [ + + ]: 2506 : if (subflow_req->mp_capable) {
876 : : /* we can receive and accept an in-window, out-of-order pkt,
877 : : * which may not carry the MP_CAPABLE opt even on mptcp enabled
878 : : * paths: always try to extract the peer key, and fallback
879 : : * for packets missing it.
880 : : * Even OoO DSS packets coming legitly after dropped or
881 : : * reordered MPC will cause fallback, but we don't have other
882 : : * options.
883 : : */
884 : 1690 : mptcp_get_options(skb, &mp_opt);
885 [ + + ]: 1690 : if (!(mp_opt.suboptions &
886 : : (OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_ACK)))
887 : 0 : fallback = true;
888 : :
889 [ + + ]: 816 : } else if (subflow_req->mp_join) {
890 : 656 : mptcp_get_options(skb, &mp_opt);
891 [ - + ]: 656 : if (!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK))
892 : 0 : fallback = true;
893 : : }
894 : :
895 : 322 : create_child:
896 : 2506 : child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
897 : : req_unhash, own_req, opt_child_init);
898 : :
899 [ + - - + : 2506 : if (child && *own_req) {
+ - ]
[ + - + - ]
900 [ + + ]: 2506 : struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
901 : :
902 : 2506 : tcp_rsk(req)->drop_req = false;
903 : :
904 : : /* we need to fallback on ctx allocation failure and on pre-reqs
905 : : * checking above. In the latter scenario we additionally need
906 : : * to reset the context to non MPTCP status.
907 : : */
908 [ + + ]: 2506 : if (!ctx || fallback) {
909 [ - + ]: 178 : if (fallback_is_fatal) {
910 [ # # ]: 0 : if (!ctx)
911 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(sk),
912 : : MPTCP_MIB_MPJOINACKNOCTX);
913 : : else
914 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(sk),
915 : : MPTCP_MIB_MPJOINACKNOMPJOIN);
916 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
917 : 0 : goto dispose_child;
918 : : }
919 : 178 : goto fallback;
920 : : }
921 : :
922 : : /* ssk inherits options of listener sk */
923 : 2328 : ctx->setsockopt_seq = listener->setsockopt_seq;
924 : :
925 [ + + ]: 2328 : if (ctx->mp_capable) {
926 : 1672 : ctx->conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req);
927 [ - + ]: 1672 : if (!ctx->conn) {
928 : 0 : fallback = true;
929 : 0 : goto fallback;
930 : : }
931 : :
932 : 1672 : ctx->subflow_id = 1;
933 [ - + ]: 1672 : owner = mptcp_sk(ctx->conn);
934 : :
935 [ + + ]: 1672 : if (mp_opt.deny_join_id0)
936 : 10 : WRITE_ONCE(owner->pm.remote_deny_join_id0, true);
937 : :
938 : 1672 : mptcp_pm_new_connection(owner, child, 1);
939 : :
940 : : /* with OoO packets we can reach here without ingress
941 : : * mpc option
942 : : */
943 [ + + ]: 1672 : if (mp_opt.suboptions & OPTION_MPTCP_MPC_ACK) {
944 : 1616 : mptcp_pm_fully_established(owner, child);
945 : 1616 : ctx->pm_notified = 1;
946 : : }
947 [ + - ]: 656 : } else if (ctx->mp_join) {
948 : 656 : owner = subflow_req->msk;
949 [ - + ]: 656 : if (!owner) {
950 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
951 : 0 : goto dispose_child;
952 : : }
953 : :
954 [ - + ]: 656 : if (!subflow_hmac_valid(subflow_req, &mp_opt)) {
955 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
956 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
957 : 0 : goto dispose_child;
958 : : }
959 : :
960 [ + + ]: 656 : if (!mptcp_can_accept_new_subflow(owner)) {
961 [ + - ]: 8 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED);
962 : 8 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
963 : 8 : goto dispose_child;
964 : : }
965 : :
966 : : /* move the msk reference ownership to the subflow */
967 : 648 : WRITE_ONCE(subflow_req->msk, NULL);
968 : 648 : ctx->conn = (struct sock *)owner;
969 : :
970 [ + + ]: 648 : if (subflow_use_different_sport(owner, sk)) {
971 [ - + - - ]: 26 : pr_debug("ack inet_sport=%d %d\n",
972 : : ntohs(inet_sk(sk)->inet_sport),
973 : : ntohs(inet_sk((struct sock *)owner)->inet_sport));
974 [ - + ]: 26 : if (!mptcp_pm_announced_has_ssk(owner, sk)) {
975 [ # # ]: 0 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
976 : 0 : subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
977 : 0 : goto dispose_child;
978 : : }
979 [ + - ]: 26 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
980 : : }
981 : :
982 [ - + ]: 648 : if (!mptcp_finish_join(child)) {
983 : 0 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(child);
984 : :
985 : 0 : subflow_add_reset_reason(skb, subflow->reset_reason);
986 : 0 : goto dispose_child;
987 : : }
988 : :
989 [ + - ]: 648 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
990 : 648 : tcp_rsk(req)->drop_req = true;
991 : : }
992 : : }
993 : :
994 : : /* check for expected invariant - should never trigger, just help
995 : : * catching earlier subtle bugs
996 : : */
997 [ - + + - : 2320 : WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
- + - + +
- + - -
+ ][ + - -
+ + - + -
- + ]
998 : : (!mptcp_subflow_ctx(child) ||
999 : : !mptcp_subflow_ctx(child)->conn));
1000 : 2320 : return child;
1001 : :
1002 : 8 : dispose_child:
1003 : 8 : mptcp_subflow_drop_ctx(child);
1004 : 8 : tcp_rsk(req)->drop_req = true;
1005 : 8 : inet_csk_prepare_for_destroy_sock(child);
1006 : 8 : tcp_done(child);
1007 : 8 : reason = mptcp_get_rst_reason(skb);
1008 : 8 : req->rsk_ops->send_reset(sk, skb, reason);
1009 : :
1010 : : /* The last child reference will be released by the caller */
1011 : 8 : return child;
1012 : :
1013 : 12 : fallback:
1014 [ + + ]: 178 : if (fallback)
1015 [ + - ]: 18 : SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
1016 : 178 : mptcp_subflow_drop_ctx(child);
1017 : 178 : return child;
1018 : : }
1019 : :
1020 : : static struct inet_connection_sock_af_ops subflow_specific __ro_after_init;
1021 : : static struct proto tcp_prot_override __ro_after_init;
1022 : :
1023 : : enum mapping_status {
1024 : : MAPPING_OK,
1025 : : MAPPING_INVALID,
1026 : : MAPPING_EMPTY,
1027 : : MAPPING_DATA_FIN,
1028 : : MAPPING_DUMMY,
1029 : : MAPPING_BAD_CSUM,
1030 : : MAPPING_NODSS
1031 : : };
1032 : :
1033 : 0 : static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
1034 : : {
1035 [ # # # # ]: 0 : pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d\n",
1036 : : ssn, subflow->map_subflow_seq, subflow->map_data_len);
1037 : 0 : }
1038 : :
1039 : 0 : static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
1040 : : {
1041 [ # # ]: 0 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1042 : 0 : unsigned int skb_consumed;
1043 : :
1044 [ # # ]: 0 : skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
1045 [ # # ]: 0 : if (unlikely(skb_consumed >= skb->len)) {
1046 : 0 : DEBUG_NET_WARN_ON_ONCE(1);
1047 : 0 : return true;
1048 : : }
1049 : :
1050 : 0 : return skb->len - skb_consumed <= subflow->map_data_len -
1051 : 0 : mptcp_subflow_get_map_offset(subflow);
1052 : : }
1053 : :
1054 : 682477 : static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
1055 : : {
1056 [ - + ]: 682477 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1057 [ - + ]: 682477 : u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1058 : :
1059 [ - + ]: 682477 : if (unlikely(before(ssn, subflow->map_subflow_seq))) {
1060 : : /* Mapping covers data later in the subflow stream,
1061 : : * currently unsupported.
1062 : : */
1063 : 0 : dbg_bad_map(subflow, ssn);
1064 : 0 : return false;
1065 : : }
1066 [ - + ]: 682477 : if (unlikely(!before(ssn, subflow->map_subflow_seq +
1067 : : subflow->map_data_len))) {
1068 : : /* Mapping does covers past subflow data, invalid */
1069 : 0 : dbg_bad_map(subflow, ssn);
1070 : 0 : return false;
1071 : : }
1072 : : return true;
1073 : : }
1074 : :
1075 : 1552321 : static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
1076 : : bool csum_reqd)
1077 : : {
1078 [ + + ]: 1552321 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1079 : 1552321 : u32 offset, seq, delta;
1080 : 1552321 : __sum16 csum;
1081 : 1552321 : int len;
1082 : :
1083 [ + + ]: 1552321 : if (!csum_reqd)
1084 : : return MAPPING_OK;
1085 : :
1086 : : /* mapping already validated on previous traversal */
1087 [ + + ]: 198215 : if (subflow->map_csum_len == subflow->map_data_len)
1088 : : return MAPPING_OK;
1089 : :
1090 : : /* traverse the receive queue, ensuring it contains a full
1091 : : * DSS mapping and accumulating the related csum.
1092 : : * Preserve the accoumlate csum across multiple calls, to compute
1093 : : * the csum only once
1094 : : */
1095 : 198208 : delta = subflow->map_data_len - subflow->map_csum_len;
1096 : 376411 : for (;;) {
1097 [ - + ]: 376411 : seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
1098 : 376411 : offset = seq - TCP_SKB_CB(skb)->seq;
1099 : :
1100 : : /* if the current skb has not been accounted yet, csum its contents
1101 : : * up to the amount covered by the current DSS
1102 : : */
1103 [ + + ]: 376411 : if (offset < skb->len) {
1104 : 198625 : __wsum csum;
1105 : :
1106 : 198625 : len = min(skb->len - offset, delta);
1107 : 198625 : csum = skb_checksum(skb, offset, len, 0);
1108 : 397250 : subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
1109 [ - + ]: 198625 : subflow->map_csum_len);
1110 : :
1111 : 198625 : delta -= len;
1112 : 198625 : subflow->map_csum_len += len;
1113 : : }
1114 [ + + ]: 376411 : if (delta == 0)
1115 : : break;
1116 : :
1117 [ + + ]: 317462 : if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
1118 : : /* if this subflow is closed, the partial mapping
1119 : : * will be never completed; flush the pending skbs, so
1120 : : * that subflow_sched_work_if_closed() can kick in
1121 : : */
1122 [ - + ]: 139259 : if (unlikely(ssk->sk_state == TCP_CLOSE))
1123 [ # # # # ]: 0 : while ((skb = skb_peek(&ssk->sk_receive_queue)))
1124 : 0 : sk_eat_skb(ssk, skb);
1125 : :
1126 : : /* not enough data to validate the csum */
1127 : 139259 : return MAPPING_EMPTY;
1128 : : }
1129 : :
1130 : : /* the DSS mapping for next skbs will be validated later,
1131 : : * when a get_mapping_status call will process such skb
1132 : : */
1133 : : skb = skb->next;
1134 : : }
1135 : :
1136 : : /* note that 'map_data_len' accounts only for the carried data, does
1137 : : * not include the eventual seq increment due to the data fin,
1138 : : * while the pseudo header requires the original DSS data len,
1139 : : * including that
1140 : : */
1141 : 58949 : csum = __mptcp_make_csum(subflow->map_seq,
1142 : : subflow->map_subflow_seq,
1143 : 58949 : subflow->map_data_len + subflow->map_data_fin,
1144 : : subflow->map_data_csum);
1145 [ + + ]: 58949 : if (unlikely(csum)) {
1146 [ + - ]: 4 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
1147 : 4 : return MAPPING_BAD_CSUM;
1148 : : }
1149 : :
1150 : 58945 : subflow->valid_csum_seen = 1;
1151 : 58945 : return MAPPING_OK;
1152 : : }
1153 : :
1154 : 2992576 : static enum mapping_status get_mapping_status(struct sock *ssk,
1155 : : struct mptcp_sock *msk)
1156 : : {
1157 [ + + ]: 2992576 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1158 [ - + ]: 2992576 : bool csum_reqd = READ_ONCE(msk->csum_enabled);
1159 : 2992576 : struct mptcp_ext *mpext;
1160 : 2992576 : struct sk_buff *skb;
1161 : 2992576 : u16 data_len;
1162 : 2992576 : u64 map_seq;
1163 : :
1164 [ + + ]: 2992654 : skb = skb_peek(&ssk->sk_receive_queue);
1165 [ - + ]: 1567464 : if (!skb)
1166 : : return MAPPING_EMPTY;
1167 : :
1168 [ + + ]: 1567464 : if (mptcp_check_fallback(ssk))
1169 : : return MAPPING_DUMMY;
1170 : :
1171 [ + + ]: 1554939 : mpext = mptcp_get_ext(skb);
1172 [ + + + + ]: 1543610 : if (!mpext || !mpext->use_map) {
1173 [ + + + + ]: 142586 : if (!subflow->map_valid && !skb->len) {
1174 : : /* the TCP stack deliver 0 len FIN pkt to the receive
1175 : : * queue, that is the only 0len pkts ever expected here,
1176 : : * and we can admit no mapping only for 0 len pkts
1177 : : */
1178 [ - + ]: 2586 : if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
1179 : 0 : WARN_ONCE(1, "0len seq %d:%d flags %x",
1180 : : TCP_SKB_CB(skb)->seq,
1181 : : TCP_SKB_CB(skb)->end_seq,
1182 : : TCP_SKB_CB(skb)->tcp_flags);
1183 : 2586 : sk_eat_skb(ssk, skb);
1184 : 2586 : return MAPPING_EMPTY;
1185 : : }
1186 : :
1187 : : /* If the required DSS has likely been dropped by a middlebox */
1188 [ + + ]: 140000 : if (!subflow->map_valid)
1189 : : return MAPPING_NODSS;
1190 : :
1191 : 139982 : goto validate_seq;
1192 : : }
1193 : :
1194 : 1412353 : trace_get_mapping_status(mpext);
1195 : :
1196 : 1412353 : data_len = mpext->data_len;
1197 [ + + ]: 1412353 : if (data_len == 0) {
1198 [ - + - - ]: 2 : pr_debug("infinite mapping received\n");
1199 [ + - ]: 2 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
1200 : 2 : return MAPPING_INVALID;
1201 : : }
1202 : :
1203 [ + + ]: 1412351 : if (mpext->data_fin == 1) {
1204 : 2525 : u64 data_fin_seq;
1205 : :
1206 [ + + ]: 2525 : if (data_len == 1) {
1207 : 12 : bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
1208 : 12 : mpext->dsn64);
1209 [ - + - - ]: 12 : pr_debug("DATA_FIN with no payload seq=%llu\n", mpext->data_seq);
1210 [ - + ]: 12 : if (subflow->map_valid) {
1211 : : /* A DATA_FIN might arrive in a DSS
1212 : : * option before the previous mapping
1213 : : * has been fully consumed. Continue
1214 : : * handling the existing mapping.
1215 : : */
1216 : 0 : skb_ext_del(skb, SKB_EXT_MPTCP);
1217 : 0 : return MAPPING_OK;
1218 : : }
1219 : :
1220 [ + + ]: 12 : if (updated)
1221 : 6 : mptcp_schedule_work((struct sock *)msk);
1222 : :
1223 : 12 : return MAPPING_DATA_FIN;
1224 : : }
1225 : :
1226 : 2513 : data_fin_seq = mpext->data_seq + data_len - 1;
1227 : :
1228 : : /* If mpext->data_seq is a 32-bit value, data_fin_seq must also
1229 : : * be limited to 32 bits.
1230 : : */
1231 [ - + ]: 2513 : if (!mpext->dsn64)
1232 : 0 : data_fin_seq &= GENMASK_ULL(31, 0);
1233 : :
1234 : 2513 : mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
1235 [ - + - - ]: 2513 : pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d\n",
1236 : : data_fin_seq, mpext->dsn64);
1237 : :
1238 : : /* Adjust for DATA_FIN using 1 byte of sequence space */
1239 : 2513 : data_len--;
1240 : : }
1241 : :
1242 [ + + ]: 1412339 : map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
1243 [ - + ]: 1412339 : WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
1244 : :
1245 [ + + ]: 1412339 : if (subflow->map_valid) {
1246 : : /* Allow replacing only with an identical map */
1247 [ + - ]: 869844 : if (subflow->map_seq == map_seq &&
1248 [ + - ]: 869844 : subflow->map_subflow_seq == mpext->subflow_seq &&
1249 [ + - ]: 869844 : subflow->map_data_len == data_len &&
1250 [ + - ]: 869844 : subflow->map_csum_reqd == mpext->csum_reqd) {
1251 : 869844 : skb_ext_del(skb, SKB_EXT_MPTCP);
1252 : 869844 : goto validate_csum;
1253 : : }
1254 : :
1255 : : /* If this skb data are fully covered by the current mapping,
1256 : : * the new map would need caching, which is not supported
1257 : : */
1258 [ # # ]: 0 : if (skb_is_fully_mapped(ssk, skb)) {
1259 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
1260 : 0 : return MAPPING_INVALID;
1261 : : }
1262 : :
1263 : : /* will validate the next map after consuming the current one */
1264 : 0 : goto validate_csum;
1265 : : }
1266 : :
1267 : 542495 : subflow->map_seq = map_seq;
1268 : 542495 : subflow->map_subflow_seq = mpext->subflow_seq;
1269 : 542495 : subflow->map_data_len = data_len;
1270 : 542495 : subflow->map_valid = 1;
1271 : 542495 : subflow->map_data_fin = mpext->data_fin;
1272 : 542495 : subflow->mpc_map = mpext->mpc_map;
1273 : 542495 : subflow->map_csum_reqd = mpext->csum_reqd;
1274 : 542495 : subflow->map_csum_len = 0;
1275 [ - + ]: 542495 : subflow->map_data_csum = csum_unfold(mpext->csum);
1276 : :
1277 : : /* Cfr RFC 8684 Section 3.3.0 */
1278 [ - + ]: 542495 : if (unlikely(subflow->map_csum_reqd != csum_reqd))
1279 : : return MAPPING_INVALID;
1280 : :
1281 [ - + - - ]: 542495 : pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
1282 : : subflow->map_seq, subflow->map_subflow_seq,
1283 : : subflow->map_data_len, subflow->map_csum_reqd,
1284 : : subflow->map_data_csum);
1285 : :
1286 : 682477 : validate_seq:
1287 : : /* we revalidate valid mapping on new skb, because we must ensure
1288 : : * the current skb is completely covered by the available mapping
1289 : : */
1290 [ - + ]: 682477 : if (!validate_mapping(ssk, skb)) {
1291 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
1292 : 0 : return MAPPING_INVALID;
1293 : : }
1294 : :
1295 : 682477 : skb_ext_del(skb, SKB_EXT_MPTCP);
1296 : :
1297 : 1552321 : validate_csum:
1298 : 1552321 : return validate_data_csum(ssk, skb, csum_reqd);
1299 : : }
1300 : :
1301 : 8404 : static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1302 : : u64 limit)
1303 : : {
1304 [ - + ]: 8404 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1305 : 8404 : bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
1306 [ - + ]: 8404 : struct tcp_sock *tp = tcp_sk(ssk);
1307 : 8404 : u32 offset, incr, avail_len;
1308 : :
1309 : 8404 : offset = tp->copied_seq - TCP_SKB_CB(skb)->seq;
1310 [ - + - + ]: 8404 : if (WARN_ON_ONCE(offset > skb->len))
1311 : 0 : goto out;
1312 : :
1313 : 8404 : avail_len = skb->len - offset;
1314 [ + + ]: 8404 : incr = limit >= avail_len ? avail_len + fin : limit;
1315 : :
1316 [ - + - - ]: 8404 : pr_debug("discarding=%d len=%d offset=%d seq=%d\n", incr, skb->len,
1317 : : offset, subflow->map_subflow_seq);
1318 [ + - ]: 8404 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
1319 [ - + ]: 8404 : tcp_sk(ssk)->copied_seq += incr;
1320 : :
1321 : 8404 : out:
1322 [ - + + + ]: 8404 : if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
1323 : 7681 : sk_eat_skb(ssk, skb);
1324 [ + + ]: 8404 : if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
1325 : 6406 : subflow->map_valid = 0;
1326 : 8404 : }
1327 : :
1328 : 0 : static bool subflow_is_done(const struct sock *sk)
1329 : : {
1330 [ - + - - ]: 795 : return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1331 : : }
1332 : :
1333 : : /* sched mptcp worker for subflow cleanup if no more data is pending */
1334 : 1578336 : static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
1335 : : {
1336 [ + + ]: 1578336 : const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1337 : 1578336 : struct sock *sk = (struct sock *)msk;
1338 : :
1339 [ + + + + : 1583079 : if (likely(ssk->sk_state != TCP_CLOSE &&
+ + ]
1340 : : (ssk->sk_state != TCP_CLOSE_WAIT ||
1341 : : inet_sk_state_load(sk) != TCP_ESTABLISHED)))
1342 : 1569123 : return;
1343 : :
1344 [ + + ]: 9213 : if (!skb_queue_empty(&ssk->sk_receive_queue))
1345 : : return;
1346 : :
1347 [ + + ]: 13312 : if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
1348 : 2273 : mptcp_schedule_work(sk);
1349 : :
1350 : : /* when the fallback subflow closes the rx side, trigger a 'dummy'
1351 : : * ingress data fin, so that the msk state will follow along
1352 : : */
1353 [ + + ]: 9195 : if (__mptcp_check_fallback(msk) && subflow_is_done(ssk) &&
1354 [ + - + + ]: 1590 : msk->first == ssk &&
1355 : 795 : mptcp_update_rcv_data_fin(msk, subflow->map_seq +
1356 : 795 : subflow->map_data_len, true))
1357 : 191 : mptcp_schedule_work(sk);
1358 : : }
1359 : :
1360 : 4 : static bool mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk)
1361 : : {
1362 : 4 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1363 : 4 : unsigned long fail_tout;
1364 : :
1365 : : /* we are really failing, prevent any later subflow join */
1366 : 4 : spin_lock_bh(&msk->fallback_lock);
1367 [ - + + + ]: 4 : if (!msk->allow_infinite_fallback) {
[ + + ]
1368 : 2 : spin_unlock_bh(&msk->fallback_lock);
1369 : 2 : return false;
1370 : : }
1371 : 2 : msk->allow_subflows = false;
1372 : 2 : spin_unlock_bh(&msk->fallback_lock);
1373 : :
1374 : : /* graceful failure can happen only on the MPC subflow */
1375 [ - + - + ]: 2 : if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first)))
1376 : : return false;
1377 : :
1378 : : /* since the close timeout take precedence on the fail one,
1379 : : * no need to start the latter when the first is already set
1380 : : */
1381 [ - + ]: 2 : if (sock_flag((struct sock *)msk, SOCK_DEAD))
1382 : : return true;
1383 : :
1384 : : /* we don't need extreme accuracy here, use a zero fail_tout as special
1385 : : * value meaning no fail timeout at all;
1386 : : */
1387 : 2 : fail_tout = jiffies + TCP_RTO_MAX;
1388 : 2 : if (!fail_tout)
1389 : : fail_tout = 1;
1390 : 2 : WRITE_ONCE(subflow->fail_tout, fail_tout);
1391 : 2 : tcp_send_ack(ssk);
1392 : :
1393 : 2 : mptcp_reset_tout_timer(msk, subflow->fail_tout);
1394 : 2 : return true;
1395 : : }
1396 : :
1397 : 3035556 : static bool subflow_check_data_avail(struct sock *ssk)
1398 : : {
1399 [ + + ]: 3035556 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1400 : 3035556 : enum mapping_status status;
1401 : 3035556 : struct mptcp_sock *msk;
1402 : 3035556 : struct sk_buff *skb;
1403 : :
1404 [ + + - + ]: 3035556 : if (!skb_peek(&ssk->sk_receive_queue))
1405 : 1417445 : WRITE_ONCE(subflow->data_avail, false);
1406 [ - + + + ]: 3035556 : if (subflow->data_avail)
[ + + ]
1407 : : return true;
1408 : :
1409 [ - + ]: 2984172 : msk = mptcp_sk(subflow->conn);
1410 : 3000980 : for (;;) {
1411 : 2992576 : u64 ack_seq;
1412 : 2992576 : u64 old_ack;
1413 : :
1414 : 2992576 : status = get_mapping_status(ssk, msk);
1415 [ + + ]: 4420274 : trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1416 [ + + + + ]: 2992576 : if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY ||
1417 : : status == MAPPING_BAD_CSUM || status == MAPPING_NODSS))
1418 : 12549 : goto fallback;
1419 : :
1420 [ + + ]: 2980027 : if (status != MAPPING_OK)
1421 : 1566969 : goto no_data;
1422 : :
1423 [ + - ]: 1413058 : skb = skb_peek(&ssk->sk_receive_queue);
1424 [ - + ]: 1413058 : if (WARN_ON_ONCE(!skb))
1425 : 0 : goto no_data;
1426 : :
1427 [ - + - + ]: 1413058 : if (unlikely(!READ_ONCE(msk->can_ack)))
[ - + ]
1428 : 0 : goto fallback;
1429 : :
1430 : 1413058 : old_ack = READ_ONCE(msk->ack_seq);
1431 : 1413058 : ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1432 [ - + - - ]: 1413058 : pr_debug("msk ack_seq=%llx subflow ack_seq=%llx\n", old_ack,
1433 : : ack_seq);
1434 [ + + ]: 1413058 : if (unlikely(before64(ack_seq, old_ack))) {
1435 : 8404 : mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1436 : 8404 : continue;
1437 : : }
1438 : :
1439 : 1404654 : WRITE_ONCE(subflow->data_avail, true);
1440 : 1404654 : break;
1441 : : }
1442 : 1404654 : return true;
1443 : :
1444 : 1566969 : no_data:
1445 : 1566969 : subflow_sched_work_if_closed(msk, ssk);
1446 : 1566969 : return false;
1447 : :
1448 : 12549 : fallback:
1449 [ + + ]: 12549 : if (!__mptcp_check_fallback(msk)) {
1450 : : /* RFC 8684 section 3.7. */
1451 [ + + ]: 24 : if (status == MAPPING_BAD_CSUM &&
1452 [ + + + - ]: 4 : (subflow->mp_join || subflow->valid_csum_seen)) {
1453 : 4 : subflow->send_mp_fail = 1;
1454 : :
1455 [ + + ]: 4 : if (!mptcp_subflow_fail(msk, ssk)) {
1456 : 2 : subflow->reset_transient = 0;
1457 : 2 : subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
1458 : 2 : goto reset;
1459 : : }
1460 : 2 : WRITE_ONCE(subflow->data_avail, true);
1461 : 2 : return true;
1462 : : }
1463 : :
1464 [ + + ]: 20 : if (!mptcp_try_fallback(ssk, MPTCP_MIB_DSSFALLBACK)) {
1465 : : /* fatal protocol error, close the socket.
1466 : : * subflow_error_report() will introduce the appropriate barriers
1467 : : */
1468 : 12 : subflow->reset_transient = 0;
1469 [ + - ]: 12 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSRESET);
1470 : 12 : subflow->reset_reason = status == MAPPING_NODSS ?
1471 [ - + ]: 12 : MPTCP_RST_EMIDDLEBOX :
1472 : : MPTCP_RST_EMPTCP;
1473 : :
1474 : 14 : reset:
1475 : 14 : WRITE_ONCE(ssk->sk_err, EBADMSG);
1476 : 14 : tcp_set_state(ssk, TCP_CLOSE);
1477 [ + + + - ]: 30 : while ((skb = skb_peek(&ssk->sk_receive_queue)))
1478 : 16 : sk_eat_skb(ssk, skb);
1479 : 14 : mptcp_send_active_reset_reason(ssk);
1480 : 14 : WRITE_ONCE(subflow->data_avail, false);
1481 : 14 : return false;
1482 : : }
1483 : : }
1484 : :
1485 [ - + ]: 12533 : skb = skb_peek(&ssk->sk_receive_queue);
1486 : 12533 : subflow->map_valid = 1;
1487 : 12533 : subflow->map_data_len = skb->len;
1488 [ - + ]: 12533 : subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1489 : 25066 : subflow->map_seq = __mptcp_expand_seq(subflow->map_seq,
1490 : 12533 : subflow->iasn +
1491 : 12533 : TCP_SKB_CB(skb)->seq -
1492 : 12533 : subflow->ssn_offset - 1);
1493 : 12533 : WRITE_ONCE(subflow->data_avail, true);
1494 : 12533 : return true;
1495 : : }
1496 : :
1497 : 3035556 : bool mptcp_subflow_data_available(struct sock *sk)
1498 : : {
1499 [ + + ]: 3035556 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1500 : :
1501 : : /* check if current mapping is still valid */
1502 [ + + ]: 3035556 : if (subflow->map_valid &&
1503 [ + + ]: 2477672 : mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1504 : 548619 : subflow->map_valid = 0;
1505 : 548619 : WRITE_ONCE(subflow->data_avail, false);
1506 : :
1507 [ - + - - ]: 548619 : pr_debug("Done with mapping: seq=%u data_len=%u\n",
1508 : : subflow->map_subflow_seq,
1509 : : subflow->map_data_len);
1510 : : }
1511 : :
1512 : 3035556 : return subflow_check_data_avail(sk);
1513 : : }
1514 : :
1515 : : /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1516 : : * not the ssk one.
1517 : : *
1518 : : * In mptcp, rwin is about the mptcp-level connection data.
1519 : : *
1520 : : * Data that is still on the ssk rx queue can thus be ignored,
1521 : : * as far as mptcp peer is concerned that data is still inflight.
1522 : : * DSS ACK is updated when skb is moved to the mptcp rx queue.
1523 : : */
1524 : 2985341 : void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1525 : : {
1526 : 2985341 : const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1527 : 2985341 : const struct sock *sk = subflow->conn;
1528 : :
1529 : 2985341 : *space = __mptcp_space(sk);
1530 : 2985341 : *full_space = mptcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
1531 : 2985341 : }
1532 : :
1533 : 2820 : static void subflow_error_report(struct sock *ssk)
1534 : : {
1535 [ + + ]: 2820 : struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1536 : :
1537 : : /* bail early if this is a no-op, so that we avoid introducing a
1538 : : * problematic lockdep dependency between TCP accept queue lock
1539 : : * and msk socket spinlock
1540 : : */
1541 [ + + ]: 2820 : if (!sk->sk_socket)
1542 : : return;
1543 : :
1544 : 1226 : mptcp_data_lock(sk);
1545 [ + + ]: 1226 : if (!sock_owned_by_user(sk))
1546 : 432 : __mptcp_error_report(sk);
1547 : : else
1548 [ - + ]: 794 : __set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->cb_flags);
1549 : 1226 : mptcp_data_unlock(sk);
1550 : : }
1551 : :
1552 : 1558114 : static void subflow_data_ready(struct sock *sk)
1553 : : {
1554 : 1558114 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1555 [ - + ]: 1558114 : u16 state = 1 << inet_sk_state_load(sk);
1556 : 1558114 : struct sock *parent = subflow->conn;
1557 : 1558114 : struct mptcp_sock *msk;
1558 : :
1559 : 1558114 : trace_sk_data_ready(sk);
1560 : :
1561 [ - + ]: 1558114 : msk = mptcp_sk(parent);
1562 [ + + ]: 1558114 : if (state & TCPF_LISTEN) {
1563 : : /* MPJ subflow are removed from accept queue before reaching here,
1564 : : * avoid stray wakeups
1565 : : */
1566 [ + + ]: 2498 : if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1567 : : return;
1568 : :
1569 : 1868 : parent->sk_data_ready(parent);
1570 : 1868 : return;
1571 : : }
1572 : :
1573 [ + + + + : 1555616 : WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
- + - - ]
1574 : : !subflow->mp_join && !(state & TCPF_CLOSE));
1575 : :
1576 [ + + ]: 1555616 : if (mptcp_subflow_data_available(sk)) {
1577 : 1406110 : mptcp_data_ready(parent, sk);
1578 : :
1579 : : /* subflow-level lowat test are not relevant.
1580 : : * respect the msk-level threshold eventually mandating an immediate ack
1581 : : */
1582 [ + + ]: 1406110 : if (mptcp_data_avail(msk) < parent->sk_rcvlowat &&
1583 [ - + - + : 390196 : (tcp_sk(sk)->rcv_nxt - tcp_sk(sk)->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss)
+ + ]
1584 : 214421 : inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW;
1585 [ + + ]: 149506 : } else if (unlikely(READ_ONCE(sk->sk_err))) {
1586 : 14 : subflow_error_report(sk);
1587 : : }
1588 : : }
1589 : :
1590 : 1059023 : static void subflow_write_space(struct sock *ssk)
1591 : : {
1592 : 1059023 : struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1593 : :
1594 : 1059023 : mptcp_propagate_sndbuf(sk, ssk);
1595 : 1059023 : mptcp_write_space(sk);
1596 : 1059023 : }
1597 : :
1598 : : static const struct inet_connection_sock_af_ops *
1599 : 0 : subflow_default_af_ops(struct sock *sk)
1600 : : {
1601 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1602 [ # # ]: 0 : if (sk->sk_family == AF_INET6)
1603 : 0 : return &subflow_v6_specific;
1604 : : #endif
1605 : : return &subflow_specific;
1606 : : }
1607 : :
1608 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1609 : 957 : void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1610 : : {
1611 [ - + ]: 957 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1612 : 957 : struct inet_connection_sock *icsk = inet_csk(sk);
1613 : 957 : const struct inet_connection_sock_af_ops *target;
1614 : :
1615 [ - + ]: 957 : target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1616 : :
1617 [ - + - - ]: 957 : pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d\n",
1618 : : subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1619 : :
1620 [ + - ]: 957 : if (likely(icsk->icsk_af_ops == target))
1621 : : return;
1622 : :
1623 : 957 : subflow->icsk_af_ops = icsk->icsk_af_ops;
1624 : 957 : icsk->icsk_af_ops = target;
1625 : : }
1626 : : #endif
1627 : :
1628 : 1500 : void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1629 : : struct sockaddr_storage *addr,
1630 : : unsigned short family)
1631 : : {
1632 : 1500 : memset(addr, 0, sizeof(*addr));
1633 : 1500 : addr->ss_family = family;
1634 [ + + ]: 1500 : if (addr->ss_family == AF_INET) {
1635 : 1330 : struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1636 : :
1637 [ + + ]: 1330 : if (info->family == AF_INET)
1638 : 1305 : in_addr->sin_addr = info->addr;
1639 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1640 [ + - ]: 25 : else if (ipv6_addr_v4mapped(&info->addr6))
1641 : 25 : in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1642 : : #endif
1643 : 1330 : in_addr->sin_port = info->port;
1644 : : }
1645 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1646 [ + - ]: 170 : else if (addr->ss_family == AF_INET6) {
1647 : 170 : struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1648 : :
1649 [ + + ]: 170 : if (info->family == AF_INET)
1650 [ - + ]: 2 : ipv6_addr_set_v4mapped(info->addr.s_addr,
1651 : : &in6_addr->sin6_addr);
1652 : : else
1653 : 168 : in6_addr->sin6_addr = info->addr6;
1654 : 170 : in6_addr->sin6_port = info->port;
1655 : : }
1656 : : #endif
1657 : 1500 : }
1658 : :
1659 : 772 : int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local,
1660 : : const struct mptcp_addr_info *remote)
1661 : : {
1662 [ - + ]: 772 : struct mptcp_sock *msk = mptcp_sk(sk);
1663 : 772 : struct mptcp_subflow_context *subflow;
1664 : 772 : int local_id = local->addr.id;
1665 : 772 : struct sockaddr_storage addr;
1666 : 772 : int remote_id = remote->id;
1667 : 772 : int err = -ENOTCONN;
1668 : 772 : struct socket *sf;
1669 : 772 : struct sock *ssk;
1670 : 772 : u32 remote_token;
1671 : 772 : int addrlen;
1672 : :
1673 : : /* The userspace PM sent the request too early? */
1674 [ - + ]: 772 : if (!mptcp_is_fully_established(sk))
1675 : 0 : goto err_out;
1676 : :
1677 : 772 : err = mptcp_subflow_create_socket(sk, local->addr.family, &sf);
1678 [ + + ]: 772 : if (err) {
1679 [ + - ]: 48 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCREATSKERR);
1680 [ - + - - ]: 48 : pr_debug("msk=%p local=%d remote=%d create sock error: %d\n",
1681 : : msk, local_id, remote_id, err);
1682 : 48 : goto err_out;
1683 : : }
1684 : :
1685 : 724 : ssk = sf->sk;
1686 : 724 : subflow = mptcp_subflow_ctx(ssk);
1687 : 724 : do {
1688 : 724 : subflow->local_nonce = get_random_u32();
1689 [ - + ]: 724 : } while (!subflow->local_nonce);
1690 : :
1691 : : /* if 'IPADDRANY', the ID will be set later, after the routing */
1692 [ + + ]: 724 : if (local->addr.family == AF_INET) {
1693 [ + + ]: 650 : if (!local->addr.addr.s_addr)
1694 : : local_id = -1;
1695 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1696 [ + + ]: 74 : } else if (sk->sk_family == AF_INET6) {
1697 [ + + ]: 72 : if (ipv6_addr_any(&local->addr.addr6))
1698 : : local_id = -1;
1699 : : #endif
1700 : : }
1701 : :
1702 : 458 : if (local_id >= 0)
1703 : 458 : subflow_set_local_id(subflow, local_id);
1704 : :
1705 : 724 : subflow->remote_key_valid = 1;
1706 : 724 : subflow->remote_key = READ_ONCE(msk->remote_key);
1707 : 724 : subflow->local_key = READ_ONCE(msk->local_key);
1708 : 724 : subflow->token = msk->token;
1709 : 724 : mptcp_info2sockaddr(&local->addr, &addr, ssk->sk_family);
1710 : :
1711 : 724 : addrlen = sizeof(struct sockaddr_in);
1712 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1713 [ + + ]: 724 : if (addr.ss_family == AF_INET6)
1714 : 74 : addrlen = sizeof(struct sockaddr_in6);
1715 : : #endif
1716 : 724 : ssk->sk_bound_dev_if = local->ifindex;
1717 : 724 : err = kernel_bind(sf, (struct sockaddr_unsized *)&addr, addrlen);
1718 [ + + ]: 724 : if (err) {
1719 [ + - ]: 4 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXBINDERR);
1720 [ - + - - ]: 4 : pr_debug("msk=%p local=%d remote=%d bind error: %d\n",
1721 : : msk, local_id, remote_id, err);
1722 : 4 : goto failed;
1723 : : }
1724 : :
1725 : 720 : mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1726 [ - + - - ]: 720 : pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d\n", msk,
1727 : : remote_token, local_id, remote_id);
1728 : 720 : subflow->remote_token = remote_token;
1729 : 720 : WRITE_ONCE(subflow->remote_id, remote_id);
1730 : 720 : subflow->request_join = 1;
1731 : 720 : subflow->request_bkup = !!(local->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1732 : 720 : subflow->subflow_id = msk->subflow_id++;
1733 : 720 : mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1734 : :
1735 : 720 : sock_hold(ssk);
1736 : 720 : list_add_tail(&subflow->node, &msk->conn_list);
1737 : 720 : err = kernel_connect(sf, (struct sockaddr_unsized *)&addr, addrlen, O_NONBLOCK);
1738 [ + + ]: 720 : if (err && err != -EINPROGRESS) {
1739 [ + - ]: 28 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCONNECTERR);
1740 [ - + - - ]: 28 : pr_debug("msk=%p local=%d remote=%d connect error: %d\n",
1741 : : msk, local_id, remote_id, err);
1742 : 28 : goto failed_unlink;
1743 : : }
1744 : :
1745 [ + - ]: 692 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTX);
1746 : :
1747 : : /* discard the subflow socket */
1748 : 692 : mptcp_sock_graft(ssk, sk->sk_socket);
1749 : 692 : iput(SOCK_INODE(sf));
1750 : 692 : mptcp_stop_tout_timer(sk);
1751 : 692 : return 0;
1752 : :
1753 : 0 : failed_unlink:
1754 : 28 : list_del(&subflow->node);
1755 : 28 : sock_put(mptcp_subflow_tcp_sock(subflow));
1756 : :
1757 : 32 : failed:
1758 : 32 : subflow->disposable = 1;
1759 : 32 : sock_release(sf);
1760 : :
1761 : 80 : err_out:
1762 : : /* we account subflows before the creation, and this failures will not
1763 : : * be caught by sk_state_change()
1764 : : */
1765 : 80 : mptcp_pm_close_subflow(msk);
1766 : 80 : return err;
1767 : : }
1768 : :
1769 : 670 : void __mptcp_inherit_memcg(struct sock *sk, struct sock *ssk, gfp_t gfp)
1770 : : {
1771 : : /* Only if the msk has been accepted already (and not orphaned).*/
1772 [ + + + - ]: 670 : if (!mem_cgroup_sockets_enabled || !sk->sk_socket)
1773 : : return;
1774 : :
1775 : 78 : mem_cgroup_sk_inherit(sk, ssk);
1776 : 78 : __sk_charge(ssk, gfp);
1777 : : }
1778 : :
1779 : 4938 : void __mptcp_inherit_cgrp_data(struct sock *sk, struct sock *ssk)
1780 : : {
1781 : : #ifdef CONFIG_SOCK_CGROUP_DATA
1782 : 4938 : struct sock_cgroup_data *sk_cd = &sk->sk_cgrp_data,
1783 : 4938 : *ssk_cd = &ssk->sk_cgrp_data;
1784 : :
1785 : : /* only the additional subflows created by kworkers have to be modified */
1786 [ + + ]: 4938 : if (cgroup_id(sock_cgroup_ptr(sk_cd)) !=
1787 [ + + ]: 265 : cgroup_id(sock_cgroup_ptr(ssk_cd))) {
1788 : 43 : cgroup_sk_free(ssk_cd);
1789 : 43 : *ssk_cd = *sk_cd;
1790 : 43 : cgroup_sk_clone(sk_cd);
1791 : : }
1792 : : #endif /* CONFIG_SOCK_CGROUP_DATA */
1793 : 4938 : }
1794 : :
1795 : 4268 : static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1796 : : {
1797 : 4268 : __mptcp_inherit_cgrp_data(parent, child);
1798 [ + + ]: 4268 : if (mem_cgroup_sockets_enabled)
1799 : 181 : mem_cgroup_sk_inherit(parent, child);
1800 : 4268 : }
1801 : :
1802 : 187 : static void mptcp_subflow_ops_override(struct sock *ssk)
1803 : : {
1804 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1805 : 4268 : if (ssk->sk_prot == &tcpv6_prot)
1806 : 1504 : ssk->sk_prot = &tcpv6_prot_override;
1807 : : else
1808 : : #endif
1809 : 2764 : ssk->sk_prot = &tcp_prot_override;
1810 : : }
1811 : :
1812 : 313 : static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1813 : : {
1814 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1815 [ + + ]: 228 : if (ssk->sk_prot == &tcpv6_prot_override)
1816 : 2813 : ssk->sk_prot = &tcpv6_prot;
1817 : : else
1818 : : #endif
1819 : 2993 : ssk->sk_prot = &tcp_prot;
1820 : : }
1821 : :
1822 : 4316 : int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
1823 : : struct socket **new_sock)
1824 : : {
1825 : 4316 : struct mptcp_subflow_context *subflow;
1826 [ + + ]: 4316 : struct net *net = sock_net(sk);
1827 : 4316 : struct socket *sf;
1828 : 4316 : int err;
1829 : :
1830 : : /* un-accepted server sockets can reach here - on bad configuration
1831 : : * bail early to avoid greater trouble later
1832 : : */
1833 [ + + ]: 4316 : if (unlikely(!sk->sk_socket))
1834 : : return -EINVAL;
1835 : :
1836 : 4268 : err = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, &sf);
1837 [ + - ]: 4268 : if (err)
1838 : : return err;
1839 : :
1840 : 4268 : lock_sock_nested(sf->sk, SINGLE_DEPTH_NESTING);
1841 : :
1842 : 4268 : err = security_mptcp_add_subflow(sk, sf->sk);
1843 [ - + ]: 4268 : if (err)
1844 : 0 : goto err_free;
1845 : :
1846 : : /* the newly created socket has to be in the same cgroup as its parent */
1847 : 4268 : mptcp_attach_cgroup(sk, sf->sk);
1848 : :
1849 : : /* kernel sockets do not by default acquire net ref, but TCP timer
1850 : : * needs it.
1851 : : * Update ns_tracker to current stack trace and refcounted tracker.
1852 : : */
1853 : 4268 : sk_net_refcnt_upgrade(sf->sk);
1854 : 4268 : err = tcp_set_ulp(sf->sk, "mptcp");
1855 [ - + ]: 4268 : if (err)
1856 : 0 : goto err_free;
1857 : :
1858 [ - + ]: 4268 : mptcp_sockopt_sync_locked(mptcp_sk(sk), sf->sk);
1859 : 4268 : release_sock(sf->sk);
1860 : :
1861 : : /* the newly created socket really belongs to the owning MPTCP
1862 : : * socket, even if for additional subflows the allocation is performed
1863 : : * by a kernel workqueue. Adjust inode references, so that the
1864 : : * procfs/diag interfaces really show this one belonging to the correct
1865 : : * user.
1866 : : */
1867 : 4268 : SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1868 : 4268 : SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1869 : 4268 : SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1870 : :
1871 [ - + ]: 4268 : subflow = mptcp_subflow_ctx(sf->sk);
1872 [ - + - - ]: 4268 : pr_debug("subflow=%p\n", subflow);
1873 : :
1874 : 4268 : *new_sock = sf;
1875 : 4268 : sock_hold(sk);
1876 : 4268 : subflow->conn = sk;
1877 [ + + ]: 4268 : mptcp_subflow_ops_override(sf->sk);
1878 : :
1879 : : return 0;
1880 : :
1881 : 0 : err_free:
1882 : 0 : release_sock(sf->sk);
1883 : 0 : sock_release(sf);
1884 : 0 : return err;
1885 : : }
1886 : :
1887 : 6614 : static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1888 : : gfp_t priority)
1889 : : {
1890 : 6614 : struct inet_connection_sock *icsk = inet_csk(sk);
1891 : 6614 : struct mptcp_subflow_context *ctx;
1892 : :
1893 : 6614 : ctx = kzalloc_obj(*ctx, priority);
1894 [ + - ]: 6614 : if (!ctx)
1895 : : return NULL;
1896 : :
1897 : 6614 : rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1898 [ - + ]: 6614 : INIT_LIST_HEAD(&ctx->node);
1899 : 6614 : INIT_LIST_HEAD(&ctx->delegated_node);
1900 : :
1901 [ - + - - ]: 6614 : pr_debug("subflow=%p\n", ctx);
1902 : :
1903 : 6614 : ctx->tcp_sock = sk;
1904 : 6614 : WRITE_ONCE(ctx->local_id, -1);
1905 : :
1906 : 6614 : return ctx;
1907 : : }
1908 : :
1909 : 11379 : static void __subflow_state_change(struct sock *sk)
1910 : : {
1911 : 11379 : struct socket_wq *wq;
1912 : :
1913 : 11379 : rcu_read_lock();
1914 [ + - - + : 11379 : wq = rcu_dereference(sk->sk_wq);
- - - - -
- ]
1915 [ + + ]: 11379 : if (skwq_has_sleeper(wq))
1916 : 2425 : wake_up_interruptible_all(&wq->wait);
1917 : 11379 : rcu_read_unlock();
1918 : 11379 : }
1919 : :
1920 : 11379 : static void subflow_state_change(struct sock *sk)
1921 : : {
1922 : 11379 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1923 : 11379 : struct sock *parent = subflow->conn;
1924 : :
1925 : 11379 : __subflow_state_change(sk);
1926 : :
1927 : : /* Rx queue processing is unneeded, error reporting will take place at
1928 : : * __mptcp_close_ssk() time and subflow reset can't happen in case of
1929 : : * fallback: subflow_sched_work_if_closed() would be a no-op.
1930 : : */
1931 [ + + ]: 11379 : if (subflow->resetting)
1932 : : return;
1933 : :
1934 : : /* as recvmsg() does not acquire the subflow socket for ssk selection
1935 : : * a fin packet carrying a DSS can be unnoticed if we don't trigger
1936 : : * the data available machinery here.
1937 : : */
1938 [ + + ]: 11367 : if (mptcp_subflow_data_available(sk))
1939 : 160 : mptcp_data_ready(parent, sk);
1940 [ + + ]: 11207 : else if (unlikely(READ_ONCE(sk->sk_err)))
1941 : 553 : subflow_error_report(sk);
1942 : :
1943 [ - + ]: 11367 : subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1944 : : }
1945 : :
1946 : 1796 : void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk)
1947 : : {
1948 : 1796 : struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1949 : 1796 : struct request_sock *req, *head, *tail;
1950 : 1796 : struct mptcp_subflow_context *subflow;
1951 : 1796 : struct sock *sk, *ssk;
1952 : :
1953 : : /* Due to lock dependencies no relevant lock can be acquired under rskq_lock.
1954 : : * Splice the req list, so that accept() can not reach the pending ssk after
1955 : : * the listener socket is released below.
1956 : : */
1957 : 1796 : spin_lock_bh(&queue->rskq_lock);
1958 : 1796 : head = queue->rskq_accept_head;
1959 : 1796 : tail = queue->rskq_accept_tail;
1960 : 1796 : queue->rskq_accept_head = NULL;
1961 : 1796 : queue->rskq_accept_tail = NULL;
1962 : 1796 : spin_unlock_bh(&queue->rskq_lock);
1963 : :
1964 [ + + ]: 1796 : if (!head)
1965 : : return;
1966 : :
1967 : : /* can't acquire the msk socket lock under the subflow one,
1968 : : * or will cause ABBA deadlock
1969 : : */
1970 : 42 : release_sock(listener_ssk);
1971 : :
1972 [ + + ]: 126 : for (req = head; req; req = req->dl_next) {
1973 : 42 : ssk = req->sk;
1974 [ - + - + ]: 42 : if (!sk_is_mptcp(ssk))
[ - + ]
1975 : 0 : continue;
1976 : :
1977 [ + - ]: 42 : subflow = mptcp_subflow_ctx(ssk);
1978 [ + - - + ]: 42 : if (!subflow || !subflow->conn)
1979 : 0 : continue;
1980 : :
1981 : 42 : sk = subflow->conn;
1982 : 42 : sock_hold(sk);
1983 : :
1984 : 42 : lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1985 : 42 : __mptcp_unaccepted_force_close(sk);
1986 : 42 : release_sock(sk);
1987 : :
1988 : : /* lockdep will report a false positive ABBA deadlock
1989 : : * between cancel_work_sync and the listener socket.
1990 : : * The involved locks belong to different sockets WRT
1991 : : * the existing AB chain.
1992 : : * Using a per socket key is problematic as key
1993 : : * deregistration requires process context and must be
1994 : : * performed at socket disposal time, in atomic
1995 : : * context.
1996 : : * Just tell lockdep to consider the listener socket
1997 : : * released here.
1998 : : */
1999 : 42 : mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_);
2000 : 42 : mptcp_cancel_work(sk);
2001 : 42 : mutex_acquire(&listener_sk->sk_lock.dep_map, 0, 0, _RET_IP_);
2002 : :
2003 : 42 : sock_put(sk);
2004 : : }
2005 : :
2006 : : /* we are still under the listener msk socket lock */
2007 : 42 : lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
2008 : :
2009 : : /* restore the listener queue, to let the TCP code clean it up */
2010 : 42 : spin_lock_bh(&queue->rskq_lock);
2011 [ - + ]: 42 : WARN_ON_ONCE(queue->rskq_accept_head);
2012 : 42 : queue->rskq_accept_head = head;
2013 : 42 : queue->rskq_accept_tail = tail;
2014 : 42 : spin_unlock_bh(&queue->rskq_lock);
2015 : : }
2016 : :
2017 : 5042 : static int subflow_ulp_init(struct sock *sk)
2018 : : {
2019 : 5042 : struct inet_connection_sock *icsk = inet_csk(sk);
2020 : 5042 : struct mptcp_subflow_context *ctx;
2021 [ - + ]: 5042 : struct tcp_sock *tp = tcp_sk(sk);
2022 : 5042 : int err = 0;
2023 : :
2024 : : /* disallow attaching ULP to a socket unless it has been
2025 : : * created with sock_create_kern()
2026 : : */
2027 [ + + ]: 5042 : if (!sk->sk_kern_sock) {
2028 : 774 : err = -EOPNOTSUPP;
2029 : 774 : goto out;
2030 : : }
2031 : :
2032 : 4268 : ctx = subflow_create_ctx(sk, GFP_KERNEL);
2033 [ - + ]: 4268 : if (!ctx) {
2034 : 0 : err = -ENOMEM;
2035 : 0 : goto out;
2036 : : }
2037 : :
2038 [ - + - - ]: 4268 : pr_debug("subflow=%p, family=%d\n", ctx, sk->sk_family);
2039 : :
2040 : 4268 : tp->is_mptcp = 1;
2041 : : /* Subflows share the MPTCP socket, and thus its SOCK_NOSPACE bit,
2042 : : * which tcp_check_space() can not mirror. Pin the mirror so that
2043 : : * __tcp_check_space() always tests the shared bit.
2044 : : */
2045 : 4268 : tp->tcp_nospace = 1;
2046 : 4268 : ctx->icsk_af_ops = icsk->icsk_af_ops;
2047 [ + + ]: 4268 : icsk->icsk_af_ops = subflow_default_af_ops(sk);
2048 : 4268 : ctx->tcp_state_change = sk->sk_state_change;
2049 : 4268 : ctx->tcp_error_report = sk->sk_error_report;
2050 : :
2051 [ - + ]: 4268 : WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable);
2052 [ - + ]: 4268 : WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space);
2053 : :
2054 : 4268 : sk->sk_data_ready = subflow_data_ready;
2055 : 4268 : sk->sk_write_space = subflow_write_space;
2056 : 4268 : sk->sk_state_change = subflow_state_change;
2057 : 4268 : sk->sk_error_report = subflow_error_report;
2058 : 5042 : out:
2059 : 5042 : return err;
2060 : : }
2061 : :
2062 : 6383 : static void subflow_ulp_release(struct sock *ssk)
2063 : : {
2064 [ + - ]: 6383 : struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
2065 : 6383 : bool release = true;
2066 : 6383 : struct sock *sk;
2067 : :
2068 [ + - ]: 6383 : if (!ctx)
2069 : : return;
2070 : :
2071 : 6383 : sk = ctx->conn;
2072 [ + - ]: 6383 : if (sk) {
2073 : : /* if the msk has been orphaned, keep the ctx
2074 : : * alive, will be freed by __mptcp_close_ssk(),
2075 : : * when the subflow is still unaccepted
2076 : : */
2077 [ - + - - ]: 6383 : release = ctx->disposable || list_empty(&ctx->node);
2078 : :
2079 : : /* inet_child_forget() does not call sk_state_change(),
2080 : : * explicitly trigger the socket close machinery
2081 : : */
2082 [ # # ]: 0 : if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW,
2083 [ # # ]: 0 : &mptcp_sk(sk)->flags))
2084 : 0 : mptcp_schedule_work(sk);
2085 : 6383 : sock_put(sk);
2086 : : }
2087 : :
2088 [ + + ]: 6383 : mptcp_subflow_ops_undo_override(ssk);
2089 [ + - ]: 6383 : if (release)
2090 : 6383 : kfree_rcu(ctx, rcu);
2091 : : }
2092 : :
2093 : 2506 : static void subflow_ulp_clone(const struct request_sock *req,
2094 : : struct sock *newsk,
2095 : : const gfp_t priority)
2096 : : {
2097 : 2506 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
2098 [ + + ]: 2506 : struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
2099 : 2506 : struct mptcp_subflow_context *new_ctx;
2100 : :
2101 [ - + + - ]: 2506 : if (!tcp_rsk(req)->is_mptcp ||
[ + - ]
2102 [ + + + + ]: 2506 : (!subflow_req->mp_capable && !subflow_req->mp_join)) {
2103 : 160 : subflow_ulp_fallback(newsk, old_ctx);
2104 : 160 : return;
2105 : : }
2106 : :
2107 : 2346 : new_ctx = subflow_create_ctx(newsk, priority);
2108 [ - + ]: 2346 : if (!new_ctx) {
2109 : 0 : subflow_ulp_fallback(newsk, old_ctx);
2110 : 0 : return;
2111 : : }
2112 : :
2113 : 2346 : new_ctx->conn_finished = 1;
2114 : 2346 : new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
2115 : 2346 : new_ctx->tcp_state_change = old_ctx->tcp_state_change;
2116 : 2346 : new_ctx->tcp_error_report = old_ctx->tcp_error_report;
2117 : 2346 : new_ctx->rel_write_seq = 1;
2118 : :
2119 [ + + ]: 2346 : if (subflow_req->mp_capable) {
2120 : : /* see comments in subflow_syn_recv_sock(), MPTCP connection
2121 : : * is fully established only after we receive the remote key
2122 : : */
2123 : 1690 : new_ctx->mp_capable = 1;
2124 : 1690 : new_ctx->local_key = subflow_req->local_key;
2125 : 1690 : new_ctx->token = subflow_req->token;
2126 : 1690 : new_ctx->ssn_offset = subflow_req->ssn_offset;
2127 : 1690 : new_ctx->idsn = subflow_req->idsn;
2128 : :
2129 : : /* this is the first subflow, id is always 0 */
2130 : 1690 : subflow_set_local_id(new_ctx, 0);
2131 [ + - ]: 656 : } else if (subflow_req->mp_join) {
2132 : 656 : new_ctx->ssn_offset = subflow_req->ssn_offset;
2133 : 656 : new_ctx->mp_join = 1;
2134 : 656 : WRITE_ONCE(new_ctx->fully_established, true);
2135 : 656 : new_ctx->remote_key_valid = 1;
2136 : 656 : new_ctx->backup = subflow_req->backup;
2137 : 656 : new_ctx->request_bkup = subflow_req->request_bkup;
2138 : 656 : WRITE_ONCE(new_ctx->remote_id, subflow_req->remote_id);
2139 : 656 : new_ctx->token = subflow_req->token;
2140 : :
2141 : : /* the subflow req id is valid, fetched via subflow_check_req()
2142 : : * and subflow_token_join_request()
2143 : : */
2144 : 656 : subflow_set_local_id(new_ctx, subflow_req->local_id);
2145 : : }
2146 : : }
2147 : :
2148 : 652579 : static void tcp_release_cb_override(struct sock *ssk)
2149 : : {
2150 : 652579 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
2151 : 652579 : long status;
2152 : :
2153 : : /* process and clear all the pending actions, but leave the subflow into
2154 : : * the napi queue. To respect locking, only the same CPU that originated
2155 : : * the action can touch the list. mptcp_napi_poll will take care of it.
2156 : : */
2157 [ - + - + ]: 652579 : status = set_mask_bits(&subflow->delegated_status, MPTCP_DELEGATE_ACTIONS_MASK, 0);
2158 [ + + ]: 652579 : if (status)
2159 : 5843 : mptcp_subflow_process_delegated(ssk, status);
2160 : :
2161 : 652579 : tcp_release_cb(ssk);
2162 : 652579 : }
2163 : :
2164 : 0 : static int tcp_abort_override(struct sock *ssk, int err)
2165 : : {
2166 : : /* closing a listener subflow requires a great deal of care.
2167 : : * keep it simple and just prevent such operation
2168 : : */
2169 [ # # ]: 0 : if (inet_sk_state_load(ssk) == TCP_LISTEN)
2170 : : return -EINVAL;
2171 : :
2172 : 0 : return tcp_abort(ssk, err);
2173 : : }
2174 : :
2175 : : static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
2176 : : .name = "mptcp",
2177 : : .owner = THIS_MODULE,
2178 : : .init = subflow_ulp_init,
2179 : : .release = subflow_ulp_release,
2180 : : .clone = subflow_ulp_clone,
2181 : : };
2182 : :
2183 : 12 : static int subflow_ops_init(struct request_sock_ops *subflow_ops)
2184 : : {
2185 : 12 : subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
2186 : :
2187 : 12 : subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
2188 : : subflow_ops->obj_size, 0,
2189 : : SLAB_ACCOUNT |
2190 : : SLAB_TYPESAFE_BY_RCU,
2191 : : NULL);
2192 [ - + ]: 12 : if (!subflow_ops->slab)
2193 : 0 : return -ENOMEM;
2194 : :
2195 : : return 0;
2196 : : }
2197 : :
2198 : 6 : void __init mptcp_subflow_init(void)
2199 : : {
2200 : 6 : mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops;
2201 : 6 : mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4";
2202 : 6 : mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor;
2203 : :
2204 [ - + ]: 6 : if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0)
2205 : 0 : panic("MPTCP: failed to init subflow v4 request sock ops\n");
2206 : :
2207 : 6 : subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
2208 : 6 : subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
2209 : 6 : subflow_request_sock_ipv4_ops.send_synack = subflow_v4_send_synack;
2210 : :
2211 : 6 : subflow_specific = ipv4_specific;
2212 : 6 : subflow_specific.conn_request = subflow_v4_conn_request;
2213 : 6 : subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
2214 : 6 : subflow_specific.sk_rx_dst_set = subflow_finish_connect;
2215 : 6 : subflow_specific.rebuild_header = subflow_rebuild_header;
2216 : :
2217 : 6 : tcp_prot_override = tcp_prot;
2218 : 6 : tcp_prot_override.release_cb = tcp_release_cb_override;
2219 : 6 : tcp_prot_override.diag_destroy = tcp_abort_override;
2220 : : #ifdef CONFIG_BPF_SYSCALL
2221 : : /* Disable sockmap processing for subflows */
2222 : 6 : tcp_prot_override.psock_update_sk_prot = NULL;
2223 : : #endif
2224 : :
2225 : 6 : mptcp_diag_subflow_init(&subflow_ulp_ops);
2226 : :
2227 [ - + ]: 6 : if (tcp_register_ulp(&subflow_ulp_ops) != 0)
2228 : 0 : panic("MPTCP: failed to register subflows to ULP\n");
2229 : 6 : }
2230 : :
2231 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2232 : 6 : void __init mptcp_subflow_v6_init(void)
2233 : : {
2234 : : /* In struct mptcp_subflow_request_sock, we assume the TCP request sock
2235 : : * structures for v4 and v6 have the same size. It should not changed in
2236 : : * the future but better to make sure to be warned if it is no longer
2237 : : * the case.
2238 : : */
2239 : 6 : BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock));
2240 : :
2241 : 6 : mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops;
2242 : 6 : mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6";
2243 : 6 : mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor;
2244 : :
2245 [ - + ]: 6 : if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0)
2246 : 0 : panic("MPTCP: failed to init subflow v6 request sock ops\n");
2247 : :
2248 : 6 : subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
2249 : 6 : subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
2250 : 6 : subflow_request_sock_ipv6_ops.send_synack = subflow_v6_send_synack;
2251 : :
2252 : 6 : subflow_v6_specific = ipv6_specific;
2253 : 6 : subflow_v6_specific.conn_request = subflow_v6_conn_request;
2254 : 6 : subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
2255 : 6 : subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
2256 : 6 : subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header;
2257 : :
2258 : 6 : subflow_v6m_specific = subflow_v6_specific;
2259 : 6 : subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
2260 : 6 : subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
2261 : 6 : subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
2262 : 6 : subflow_v6m_specific.rebuild_header = subflow_rebuild_header;
2263 : :
2264 : 6 : tcpv6_prot_override = tcpv6_prot;
2265 : 6 : tcpv6_prot_override.release_cb = tcp_release_cb_override;
2266 : 6 : tcpv6_prot_override.diag_destroy = tcp_abort_override;
2267 : : #ifdef CONFIG_BPF_SYSCALL
2268 : : /* Disable sockmap processing for subflows */
2269 : 6 : tcpv6_prot_override.psock_update_sk_prot = NULL;
2270 : : #endif
2271 : 6 : }
2272 : : #endif
|