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