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