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