]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/mptcp/subflow.c
mptcp: do not reset MP_CAPABLE subflow on mapping errors
[mirror_ubuntu-hirsute-kernel.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>
21#endif
2303f994 22#include <net/mptcp.h>
4596a2c1 23#include <uapi/linux/mptcp.h>
2303f994 24#include "protocol.h"
fc518953
FW
25#include "mib.h"
26
27static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
28 enum linux_mptcp_mib_field field)
29{
30 MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
31}
2303f994 32
79c0949e
PK
33static void subflow_req_destructor(struct request_sock *req)
34{
35 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
36
37 pr_debug("subflow_req=%p", subflow_req);
38
8fd4de12
PA
39 if (subflow_req->msk)
40 sock_put((struct sock *)subflow_req->msk);
41
2c5ebd00 42 mptcp_token_destroy_request(req);
79c0949e
PK
43 tcp_request_sock_ops.destructor(req);
44}
45
f296234c
PK
46static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
47 void *hmac)
48{
49 u8 msg[8];
50
51 put_unaligned_be32(nonce1, &msg[0]);
52 put_unaligned_be32(nonce2, &msg[4]);
53
54 mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
55}
56
4cf8b7e4
PA
57static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
58{
59 return mptcp_is_fully_established((void *)msk) &&
60 READ_ONCE(msk->pm.accept_subflow);
61}
62
f296234c 63/* validate received token and create truncated hmac and nonce for SYN-ACK */
8fd4de12
PA
64static struct mptcp_sock *subflow_token_join_request(struct request_sock *req,
65 const struct sk_buff *skb)
f296234c
PK
66{
67 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
bd697222 68 u8 hmac[SHA256_DIGEST_SIZE];
f296234c
PK
69 struct mptcp_sock *msk;
70 int local_id;
71
72 msk = mptcp_token_get_sock(subflow_req->token);
73 if (!msk) {
fc518953 74 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
8fd4de12 75 return NULL;
f296234c
PK
76 }
77
78 local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
79 if (local_id < 0) {
80 sock_put((struct sock *)msk);
8fd4de12 81 return NULL;
f296234c
PK
82 }
83 subflow_req->local_id = local_id;
84
85 get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
86
87 subflow_generate_hmac(msk->local_key, msk->remote_key,
88 subflow_req->local_nonce,
89 subflow_req->remote_nonce, hmac);
90
91 subflow_req->thmac = get_unaligned_be64(hmac);
8fd4de12 92 return msk;
f296234c
PK
93}
94
3bdb45f8 95static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
cec37a6e 96{
cec37a6e 97 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
cec37a6e
PK
98
99 subflow_req->mp_capable = 0;
f296234c 100 subflow_req->mp_join = 0;
8fd4de12 101 subflow_req->msk = NULL;
2c5ebd00 102 mptcp_token_init_request(req);
78d8b7bc
FW
103}
104
3ecfbe3e
FW
105/* Init mptcp request socket.
106 *
107 * Returns an error code if a JOIN has failed and a TCP reset
108 * should be sent.
109 */
3bdb45f8
PA
110static int subflow_check_req(struct request_sock *req,
111 const struct sock *sk_listener,
112 struct sk_buff *skb)
78d8b7bc
FW
113{
114 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
115 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
116 struct mptcp_options_received mp_opt;
78d8b7bc
FW
117
118 pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
119
3bdb45f8
PA
120#ifdef CONFIG_TCP_MD5SIG
121 /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
122 * TCP option space.
123 */
124 if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
125 return -EINVAL;
126#endif
78d8b7bc
FW
127
128 mptcp_get_options(skb, &mp_opt);
129
cfde141e 130 if (mp_opt.mp_capable) {
fc518953
FW
131 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
132
cfde141e 133 if (mp_opt.mp_join)
3ecfbe3e 134 return 0;
cfde141e 135 } else if (mp_opt.mp_join) {
fc518953
FW
136 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
137 }
f296234c 138
cfde141e 139 if (mp_opt.mp_capable && listener->request_mptcp) {
535fb815
FW
140 int err, retries = 4;
141
c83a47e5 142 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
535fb815
FW
143again:
144 do {
145 get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
146 } while (subflow_req->local_key == 0);
79c0949e 147
c83a47e5
FW
148 if (unlikely(req->syncookie)) {
149 mptcp_crypto_key_sha(subflow_req->local_key,
150 &subflow_req->token,
151 &subflow_req->idsn);
152 if (mptcp_token_exists(subflow_req->token)) {
153 if (retries-- > 0)
154 goto again;
155 } else {
156 subflow_req->mp_capable = 1;
157 }
3ecfbe3e 158 return 0;
c83a47e5
FW
159 }
160
79c0949e
PK
161 err = mptcp_token_new_request(req);
162 if (err == 0)
163 subflow_req->mp_capable = 1;
535fb815
FW
164 else if (retries-- > 0)
165 goto again;
79c0949e 166
cfde141e 167 } else if (mp_opt.mp_join && listener->request_mptcp) {
ec3edaa7 168 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
f296234c 169 subflow_req->mp_join = 1;
cfde141e
PA
170 subflow_req->backup = mp_opt.backup;
171 subflow_req->remote_id = mp_opt.join_id;
172 subflow_req->token = mp_opt.token;
173 subflow_req->remote_nonce = mp_opt.nonce;
8fd4de12 174 subflow_req->msk = subflow_token_join_request(req, skb);
9466a1cc 175
3ecfbe3e
FW
176 /* Can't fall back to TCP in this case. */
177 if (!subflow_req->msk)
178 return -EPERM;
179
180 if (unlikely(req->syncookie)) {
9466a1cc
FW
181 if (mptcp_can_accept_new_subflow(subflow_req->msk))
182 subflow_init_req_cookie_join_save(subflow_req, skb);
183 }
184
8fd4de12
PA
185 pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
186 subflow_req->remote_nonce, subflow_req->msk);
cec37a6e 187 }
3ecfbe3e
FW
188
189 return 0;
cec37a6e
PK
190}
191
c83a47e5
FW
192int mptcp_subflow_init_cookie_req(struct request_sock *req,
193 const struct sock *sk_listener,
194 struct sk_buff *skb)
195{
196 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
197 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
198 struct mptcp_options_received mp_opt;
199 int err;
200
3bdb45f8 201 subflow_init_req(req, sk_listener);
c83a47e5
FW
202 mptcp_get_options(skb, &mp_opt);
203
204 if (mp_opt.mp_capable && mp_opt.mp_join)
205 return -EINVAL;
206
207 if (mp_opt.mp_capable && listener->request_mptcp) {
208 if (mp_opt.sndr_key == 0)
209 return -EINVAL;
210
211 subflow_req->local_key = mp_opt.rcvr_key;
212 err = mptcp_token_new_request(req);
213 if (err)
214 return err;
215
216 subflow_req->mp_capable = 1;
217 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
9466a1cc
FW
218 } else if (mp_opt.mp_join && listener->request_mptcp) {
219 if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
220 return -EINVAL;
221
222 if (mptcp_can_accept_new_subflow(subflow_req->msk))
223 subflow_req->mp_join = 1;
224
225 subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
c83a47e5
FW
226 }
227
228 return 0;
229}
230EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
231
7ea851d1
FW
232static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
233 struct sk_buff *skb,
234 struct flowi *fl,
235 struct request_sock *req)
cec37a6e 236{
7ea851d1 237 struct dst_entry *dst;
3ecfbe3e 238 int err;
7ea851d1 239
cec37a6e 240 tcp_rsk(req)->is_mptcp = 1;
3bdb45f8 241 subflow_init_req(req, sk);
cec37a6e 242
7ea851d1
FW
243 dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
244 if (!dst)
245 return NULL;
cec37a6e 246
3bdb45f8 247 err = subflow_check_req(req, sk, skb);
3ecfbe3e
FW
248 if (err == 0)
249 return dst;
cec37a6e 250
3ecfbe3e
FW
251 dst_release(dst);
252 if (!req->syncookie)
253 tcp_request_sock_ops.send_reset(sk, skb);
254 return NULL;
cec37a6e
PK
255}
256
257#if IS_ENABLED(CONFIG_MPTCP_IPV6)
7ea851d1
FW
258static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
259 struct sk_buff *skb,
260 struct flowi *fl,
261 struct request_sock *req)
cec37a6e 262{
7ea851d1 263 struct dst_entry *dst;
3ecfbe3e 264 int err;
7ea851d1 265
cec37a6e 266 tcp_rsk(req)->is_mptcp = 1;
3bdb45f8 267 subflow_init_req(req, sk);
cec37a6e 268
7ea851d1
FW
269 dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
270 if (!dst)
271 return NULL;
cec37a6e 272
3bdb45f8 273 err = subflow_check_req(req, sk, skb);
3ecfbe3e
FW
274 if (err == 0)
275 return dst;
276
277 dst_release(dst);
278 if (!req->syncookie)
279 tcp6_request_sock_ops.send_reset(sk, skb);
280 return NULL;
cec37a6e
PK
281}
282#endif
283
ec3edaa7
PK
284/* validate received truncated hmac and create hmac for third ACK */
285static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
286{
bd697222 287 u8 hmac[SHA256_DIGEST_SIZE];
ec3edaa7
PK
288 u64 thmac;
289
290 subflow_generate_hmac(subflow->remote_key, subflow->local_key,
291 subflow->remote_nonce, subflow->local_nonce,
292 hmac);
293
294 thmac = get_unaligned_be64(hmac);
295 pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
296 subflow, subflow->token,
297 (unsigned long long)thmac,
298 (unsigned long long)subflow->thmac);
299
300 return thmac == subflow->thmac;
301}
302
d5824847
PA
303void mptcp_subflow_reset(struct sock *ssk)
304{
0e4f35d7
PA
305 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
306 struct sock *sk = subflow->conn;
307
ab82e996
FW
308 /* must hold: tcp_done() could drop last reference on parent */
309 sock_hold(sk);
310
d5824847
PA
311 tcp_set_state(ssk, TCP_CLOSE);
312 tcp_send_active_reset(ssk, GFP_ATOMIC);
313 tcp_done(ssk);
0e4f35d7
PA
314 if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
315 schedule_work(&mptcp_sk(sk)->work))
ab82e996
FW
316 return; /* worker will put sk for us */
317
318 sock_put(sk);
d5824847
PA
319}
320
cec37a6e
PK
321static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
322{
323 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
cfde141e 324 struct mptcp_options_received mp_opt;
c3c123d1 325 struct sock *parent = subflow->conn;
cec37a6e
PK
326
327 subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
328
1200832c 329 if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
c3c123d1
DC
330 inet_sk_state_store(parent, TCP_ESTABLISHED);
331 parent->sk_state_change(parent);
332 }
333
263e1201
PA
334 /* be sure no special action on any packet other than syn-ack */
335 if (subflow->conn_finished)
336 return;
337
b0977bb2 338 subflow->rel_write_seq = 1;
263e1201 339 subflow->conn_finished = 1;
e1ff9e82
DC
340 subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
341 pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
263e1201 342
cfde141e 343 mptcp_get_options(skb, &mp_opt);
fa25e815
PA
344 if (subflow->request_mptcp) {
345 if (!mp_opt.mp_capable) {
346 MPTCP_INC_STATS(sock_net(sk),
347 MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
348 mptcp_do_fallback(sk);
349 pr_fallback(mptcp_sk(subflow->conn));
350 goto fallback;
351 }
352
263e1201
PA
353 subflow->mp_capable = 1;
354 subflow->can_ack = 1;
cfde141e 355 subflow->remote_key = mp_opt.sndr_key;
263e1201
PA
356 pr_debug("subflow=%p, remote_key=%llu", subflow,
357 subflow->remote_key);
fa25e815
PA
358 mptcp_finish_connect(sk);
359 } else if (subflow->request_join) {
360 u8 hmac[SHA256_DIGEST_SIZE];
361
362 if (!mp_opt.mp_join)
363 goto do_reset;
364
cfde141e
PA
365 subflow->thmac = mp_opt.thmac;
366 subflow->remote_nonce = mp_opt.nonce;
263e1201
PA
367 pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
368 subflow->thmac, subflow->remote_nonce);
263e1201 369
ec3edaa7 370 if (!subflow_thmac_valid(subflow)) {
fc518953 371 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
ec3edaa7
PK
372 goto do_reset;
373 }
374
375 subflow_generate_hmac(subflow->local_key, subflow->remote_key,
376 subflow->local_nonce,
377 subflow->remote_nonce,
bd697222 378 hmac);
bd697222 379 memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
ec3edaa7 380
ec3edaa7
PK
381 if (!mptcp_finish_join(sk))
382 goto do_reset;
383
fa25e815 384 subflow->mp_join = 1;
fc518953 385 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
fa25e815
PA
386 } else if (mptcp_check_fallback(sk)) {
387fallback:
388 mptcp_rcv_space_init(mptcp_sk(parent), sk);
cec37a6e 389 }
fa25e815
PA
390 return;
391
392do_reset:
d5824847 393 mptcp_subflow_reset(sk);
cec37a6e
PK
394}
395
08b8d080
FW
396struct request_sock_ops mptcp_subflow_request_sock_ops;
397EXPORT_SYMBOL_GPL(mptcp_subflow_request_sock_ops);
cec37a6e
PK
398static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops;
399
400static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
401{
402 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
403
404 pr_debug("subflow=%p", subflow);
405
406 /* Never answer to SYNs sent to broadcast or multicast */
407 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
408 goto drop;
409
08b8d080 410 return tcp_conn_request(&mptcp_subflow_request_sock_ops,
cec37a6e
PK
411 &subflow_request_sock_ipv4_ops,
412 sk, skb);
413drop:
414 tcp_listendrop(sk);
415 return 0;
416}
417
418#if IS_ENABLED(CONFIG_MPTCP_IPV6)
419static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops;
420static struct inet_connection_sock_af_ops subflow_v6_specific;
421static struct inet_connection_sock_af_ops subflow_v6m_specific;
422
423static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
424{
425 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
426
427 pr_debug("subflow=%p", subflow);
428
429 if (skb->protocol == htons(ETH_P_IP))
430 return subflow_v4_conn_request(sk, skb);
431
432 if (!ipv6_unicast_destination(skb))
433 goto drop;
434
a1d31087
JK
435 if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
436 __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
437 return 0;
438 }
439
08b8d080 440 return tcp_conn_request(&mptcp_subflow_request_sock_ops,
cec37a6e
PK
441 &subflow_request_sock_ipv6_ops, sk, skb);
442
443drop:
444 tcp_listendrop(sk);
445 return 0; /* don't send reset */
446}
447#endif
448
f296234c
PK
449/* validate hmac received in third ACK */
450static bool subflow_hmac_valid(const struct request_sock *req,
cfde141e 451 const struct mptcp_options_received *mp_opt)
f296234c
PK
452{
453 const struct mptcp_subflow_request_sock *subflow_req;
bd697222 454 u8 hmac[SHA256_DIGEST_SIZE];
f296234c 455 struct mptcp_sock *msk;
f296234c
PK
456
457 subflow_req = mptcp_subflow_rsk(req);
8fd4de12 458 msk = subflow_req->msk;
f296234c
PK
459 if (!msk)
460 return false;
461
462 subflow_generate_hmac(msk->remote_key, msk->local_key,
463 subflow_req->remote_nonce,
464 subflow_req->local_nonce, hmac);
465
8fd4de12 466 return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
f296234c
PK
467}
468
df1036da
FW
469static void mptcp_sock_destruct(struct sock *sk)
470{
471 /* if new mptcp socket isn't accepted, it is free'd
472 * from the tcp listener sockets request queue, linked
473 * from req->sk. The tcp socket is released.
474 * This calls the ULP release function which will
475 * also remove the mptcp socket, via
476 * sock_put(ctx->conn).
477 *
7ee24926
PA
478 * Problem is that the mptcp socket will be in
479 * ESTABLISHED state and will not have the SOCK_DEAD flag.
df1036da
FW
480 * Both result in warnings from inet_sock_destruct.
481 */
c5ed41bc 482 if ((1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
df1036da
FW
483 sk->sk_state = TCP_CLOSE;
484 WARN_ON_ONCE(sk->sk_socket);
485 sock_orphan(sk);
486 }
487
5c8c1640 488 mptcp_destroy_common(mptcp_sk(sk));
df1036da
FW
489 inet_sock_destruct(sk);
490}
491
9f5ca6a5
FW
492static void mptcp_force_close(struct sock *sk)
493{
494 inet_sk_state_store(sk, TCP_CLOSE);
495 sk_common_release(sk);
496}
497
4c8941de
PA
498static void subflow_ulp_fallback(struct sock *sk,
499 struct mptcp_subflow_context *old_ctx)
500{
501 struct inet_connection_sock *icsk = inet_csk(sk);
502
503 mptcp_subflow_tcp_fallback(sk, old_ctx);
504 icsk->icsk_ulp_ops = NULL;
505 rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
506 tcp_sk(sk)->is_mptcp = 0;
507}
508
39884604
PA
509static void subflow_drop_ctx(struct sock *ssk)
510{
511 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
512
513 if (!ctx)
514 return;
515
516 subflow_ulp_fallback(ssk, ctx);
517 if (ctx->conn)
518 sock_put(ctx->conn);
519
520 kfree_rcu(ctx, rcu);
521}
522
b93df08c
PA
523void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
524 struct mptcp_options_received *mp_opt)
525{
526 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
527
528 subflow->remote_key = mp_opt->sndr_key;
529 subflow->fully_established = 1;
530 subflow->can_ack = 1;
531 WRITE_ONCE(msk->fully_established, true);
532}
533
cec37a6e
PK
534static struct sock *subflow_syn_recv_sock(const struct sock *sk,
535 struct sk_buff *skb,
536 struct request_sock *req,
537 struct dst_entry *dst,
538 struct request_sock *req_unhash,
539 bool *own_req)
540{
541 struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
cc7972ea 542 struct mptcp_subflow_request_sock *subflow_req;
cfde141e 543 struct mptcp_options_received mp_opt;
9e365ff5 544 bool fallback, fallback_is_fatal;
58b09919 545 struct sock *new_msk = NULL;
cec37a6e
PK
546 struct sock *child;
547
548 pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
549
9e365ff5
PA
550 /* After child creation we must look for 'mp_capable' even when options
551 * are not parsed
cfde141e
PA
552 */
553 mp_opt.mp_capable = 0;
9e365ff5
PA
554
555 /* hopefully temporary handling for MP_JOIN+syncookie */
556 subflow_req = mptcp_subflow_rsk(req);
b7514694 557 fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
9e365ff5
PA
558 fallback = !tcp_rsk(req)->is_mptcp;
559 if (fallback)
ae2dd716
FW
560 goto create_child;
561
d22f4988 562 /* if the sk is MP_CAPABLE, we try to fetch the client key */
cc7972ea 563 if (subflow_req->mp_capable) {
ffcc3365
PA
564 /* we can receive and accept an in-window, out-of-order pkt,
565 * which may not carry the MP_CAPABLE opt even on mptcp enabled
566 * paths: always try to extract the peer key, and fallback
567 * for packets missing it.
568 * Even OoO DSS packets coming legitly after dropped or
569 * reordered MPC will cause fallback, but we don't have other
570 * options.
571 */
cfde141e
PA
572 mptcp_get_options(skb, &mp_opt);
573 if (!mp_opt.mp_capable) {
4c8941de 574 fallback = true;
58b09919 575 goto create_child;
d22f4988 576 }
58b09919 577
cfde141e 578 new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
58b09919 579 if (!new_msk)
4c8941de 580 fallback = true;
f296234c 581 } else if (subflow_req->mp_join) {
cfde141e 582 mptcp_get_options(skb, &mp_opt);
d3ab7885
PA
583 if (!mp_opt.mp_join || !subflow_hmac_valid(req, &mp_opt) ||
584 !mptcp_can_accept_new_subflow(subflow_req->msk)) {
fc518953 585 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
9e365ff5 586 fallback = true;
fc518953 587 }
cc7972ea 588 }
cec37a6e 589
d22f4988 590create_child:
cec37a6e
PK
591 child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
592 req_unhash, own_req);
593
594 if (child && *own_req) {
79c0949e
PK
595 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
596
90bf4513
PA
597 tcp_rsk(req)->drop_req = false;
598
4c8941de
PA
599 /* we need to fallback on ctx allocation failure and on pre-reqs
600 * checking above. In the latter scenario we additionally need
601 * to reset the context to non MPTCP status.
79c0949e 602 */
4c8941de 603 if (!ctx || fallback) {
f296234c 604 if (fallback_is_fatal)
729cd643 605 goto dispose_child;
4c8941de 606
39884604 607 subflow_drop_ctx(child);
58b09919 608 goto out;
f296234c 609 }
79c0949e
PK
610
611 if (ctx->mp_capable) {
b93df08c
PA
612 /* this can't race with mptcp_close(), as the msk is
613 * not yet exposted to user-space
614 */
615 inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
616
5b950ff4
PA
617 /* record the newly created socket as the first msk
618 * subflow, but don't link it yet into conn_list
619 */
0397c6d8
PA
620 WRITE_ONCE(mptcp_sk(new_msk)->first, child);
621
58b09919
PA
622 /* new mpc subflow takes ownership of the newly
623 * created mptcp socket
624 */
df1036da 625 new_msk->sk_destruct = mptcp_sock_destruct;
1b1c7a0e 626 mptcp_pm_new_connection(mptcp_sk(new_msk), 1);
2c5ebd00 627 mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
58b09919
PA
628 ctx->conn = new_msk;
629 new_msk = NULL;
fca5c82c
PA
630
631 /* with OoO packets we can reach here without ingress
632 * mpc option
633 */
b93df08c
PA
634 if (mp_opt.mp_capable)
635 mptcp_subflow_fully_established(ctx, &mp_opt);
f296234c
PK
636 } else if (ctx->mp_join) {
637 struct mptcp_sock *owner;
638
8fd4de12 639 owner = subflow_req->msk;
f296234c 640 if (!owner)
729cd643 641 goto dispose_child;
f296234c 642
8fd4de12
PA
643 /* move the msk reference ownership to the subflow */
644 subflow_req->msk = NULL;
f296234c
PK
645 ctx->conn = (struct sock *)owner;
646 if (!mptcp_finish_join(child))
729cd643 647 goto dispose_child;
fc518953
FW
648
649 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
90bf4513 650 tcp_rsk(req)->drop_req = true;
cec37a6e
PK
651 }
652 }
653
58b09919
PA
654out:
655 /* dispose of the left over mptcp master, if any */
656 if (unlikely(new_msk))
9f5ca6a5 657 mptcp_force_close(new_msk);
4c8941de
PA
658
659 /* check for expected invariant - should never trigger, just help
660 * catching eariler subtle bugs
661 */
ac2b47fb 662 WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
4c8941de
PA
663 (!mptcp_subflow_ctx(child) ||
664 !mptcp_subflow_ctx(child)->conn));
cec37a6e 665 return child;
f296234c 666
729cd643 667dispose_child:
39884604 668 subflow_drop_ctx(child);
729cd643 669 tcp_rsk(req)->drop_req = true;
729cd643 670 inet_csk_prepare_for_destroy_sock(child);
f296234c 671 tcp_done(child);
97e61751 672 req->rsk_ops->send_reset(sk, skb);
729cd643
PA
673
674 /* The last child reference will be released by the caller */
675 return child;
cec37a6e
PK
676}
677
678static struct inet_connection_sock_af_ops subflow_specific;
679
648ef4b8
MM
680enum mapping_status {
681 MAPPING_OK,
682 MAPPING_INVALID,
683 MAPPING_EMPTY,
e1ff9e82
DC
684 MAPPING_DATA_FIN,
685 MAPPING_DUMMY
648ef4b8
MM
686};
687
688static u64 expand_seq(u64 old_seq, u16 old_data_len, u64 seq)
689{
690 if ((u32)seq == (u32)old_seq)
691 return old_seq;
692
693 /* Assume map covers data not mapped yet. */
694 return seq | ((old_seq + old_data_len + 1) & GENMASK_ULL(63, 32));
695}
696
697static void warn_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
698{
699 WARN_ONCE(1, "Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
700 ssn, subflow->map_subflow_seq, subflow->map_data_len);
701}
702
703static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
704{
705 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
706 unsigned int skb_consumed;
707
708 skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
709 if (WARN_ON_ONCE(skb_consumed >= skb->len))
710 return true;
711
712 return skb->len - skb_consumed <= subflow->map_data_len -
713 mptcp_subflow_get_map_offset(subflow);
714}
715
716static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
717{
718 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
719 u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
720
721 if (unlikely(before(ssn, subflow->map_subflow_seq))) {
722 /* Mapping covers data later in the subflow stream,
723 * currently unsupported.
724 */
725 warn_bad_map(subflow, ssn);
726 return false;
727 }
728 if (unlikely(!before(ssn, subflow->map_subflow_seq +
729 subflow->map_data_len))) {
730 /* Mapping does covers past subflow data, invalid */
731 warn_bad_map(subflow, ssn + skb->len);
732 return false;
733 }
734 return true;
735}
736
43b54c6e
MM
737static enum mapping_status get_mapping_status(struct sock *ssk,
738 struct mptcp_sock *msk)
648ef4b8
MM
739{
740 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
741 struct mptcp_ext *mpext;
742 struct sk_buff *skb;
743 u16 data_len;
744 u64 map_seq;
745
746 skb = skb_peek(&ssk->sk_receive_queue);
747 if (!skb)
748 return MAPPING_EMPTY;
749
e1ff9e82
DC
750 if (mptcp_check_fallback(ssk))
751 return MAPPING_DUMMY;
752
648ef4b8
MM
753 mpext = mptcp_get_ext(skb);
754 if (!mpext || !mpext->use_map) {
755 if (!subflow->map_valid && !skb->len) {
756 /* the TCP stack deliver 0 len FIN pkt to the receive
757 * queue, that is the only 0len pkts ever expected here,
758 * and we can admit no mapping only for 0 len pkts
759 */
760 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
761 WARN_ONCE(1, "0len seq %d:%d flags %x",
762 TCP_SKB_CB(skb)->seq,
763 TCP_SKB_CB(skb)->end_seq,
764 TCP_SKB_CB(skb)->tcp_flags);
765 sk_eat_skb(ssk, skb);
766 return MAPPING_EMPTY;
767 }
768
769 if (!subflow->map_valid)
770 return MAPPING_INVALID;
771
772 goto validate_seq;
773 }
774
775 pr_debug("seq=%llu is64=%d ssn=%u data_len=%u data_fin=%d",
776 mpext->data_seq, mpext->dsn64, mpext->subflow_seq,
777 mpext->data_len, mpext->data_fin);
778
779 data_len = mpext->data_len;
780 if (data_len == 0) {
fc518953 781 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
648ef4b8
MM
782 return MAPPING_INVALID;
783 }
784
785 if (mpext->data_fin == 1) {
786 if (data_len == 1) {
1a49b2c2
MM
787 bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
788 mpext->dsn64);
43b54c6e 789 pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
648ef4b8
MM
790 if (subflow->map_valid) {
791 /* A DATA_FIN might arrive in a DSS
792 * option before the previous mapping
793 * has been fully consumed. Continue
794 * handling the existing mapping.
795 */
796 skb_ext_del(skb, SKB_EXT_MPTCP);
797 return MAPPING_OK;
798 } else {
ef59b195
MM
799 if (updated && schedule_work(&msk->work))
800 sock_hold((struct sock *)msk);
801
648ef4b8
MM
802 return MAPPING_DATA_FIN;
803 }
43b54c6e 804 } else {
017512a0 805 u64 data_fin_seq = mpext->data_seq + data_len - 1;
1a49b2c2
MM
806
807 /* If mpext->data_seq is a 32-bit value, data_fin_seq
808 * must also be limited to 32 bits.
809 */
810 if (!mpext->dsn64)
811 data_fin_seq &= GENMASK_ULL(31, 0);
812
813 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
814 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
815 data_fin_seq, mpext->dsn64);
648ef4b8
MM
816 }
817
818 /* Adjust for DATA_FIN using 1 byte of sequence space */
819 data_len--;
820 }
821
822 if (!mpext->dsn64) {
823 map_seq = expand_seq(subflow->map_seq, subflow->map_data_len,
824 mpext->data_seq);
825 pr_debug("expanded seq=%llu", subflow->map_seq);
826 } else {
827 map_seq = mpext->data_seq;
828 }
37198e93 829 WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
648ef4b8
MM
830
831 if (subflow->map_valid) {
832 /* Allow replacing only with an identical map */
833 if (subflow->map_seq == map_seq &&
834 subflow->map_subflow_seq == mpext->subflow_seq &&
835 subflow->map_data_len == data_len) {
836 skb_ext_del(skb, SKB_EXT_MPTCP);
837 return MAPPING_OK;
838 }
839
840 /* If this skb data are fully covered by the current mapping,
841 * the new map would need caching, which is not supported
842 */
fc518953
FW
843 if (skb_is_fully_mapped(ssk, skb)) {
844 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
648ef4b8 845 return MAPPING_INVALID;
fc518953 846 }
648ef4b8
MM
847
848 /* will validate the next map after consuming the current one */
849 return MAPPING_OK;
850 }
851
852 subflow->map_seq = map_seq;
853 subflow->map_subflow_seq = mpext->subflow_seq;
854 subflow->map_data_len = data_len;
855 subflow->map_valid = 1;
d22f4988 856 subflow->mpc_map = mpext->mpc_map;
648ef4b8
MM
857 pr_debug("new map seq=%llu subflow_seq=%u data_len=%u",
858 subflow->map_seq, subflow->map_subflow_seq,
859 subflow->map_data_len);
860
861validate_seq:
862 /* we revalidate valid mapping on new skb, because we must ensure
863 * the current skb is completely covered by the available mapping
864 */
865 if (!validate_mapping(ssk, skb))
866 return MAPPING_INVALID;
867
868 skb_ext_del(skb, SKB_EXT_MPTCP);
869 return MAPPING_OK;
870}
871
04e4cd4f 872static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1d39cd8c 873 u64 limit)
6719331c
PA
874{
875 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
04e4cd4f
PA
876 bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
877 u32 incr;
878
879 incr = limit >= skb->len ? skb->len + fin : limit;
880
881 pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
882 subflow->map_subflow_seq);
06242e44 883 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
04e4cd4f
PA
884 tcp_sk(ssk)->copied_seq += incr;
885 if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
886 sk_eat_skb(ssk, skb);
887 if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
888 subflow->map_valid = 0;
6719331c
PA
889}
890
648ef4b8
MM
891static bool subflow_check_data_avail(struct sock *ssk)
892{
893 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
894 enum mapping_status status;
895 struct mptcp_sock *msk;
896 struct sk_buff *skb;
897
898 pr_debug("msk=%p ssk=%p data_avail=%d skb=%p", subflow->conn, ssk,
899 subflow->data_avail, skb_peek(&ssk->sk_receive_queue));
47bebdf3
PA
900 if (!skb_peek(&ssk->sk_receive_queue))
901 subflow->data_avail = 0;
648ef4b8
MM
902 if (subflow->data_avail)
903 return true;
904
648ef4b8
MM
905 msk = mptcp_sk(subflow->conn);
906 for (;;) {
648ef4b8
MM
907 u64 ack_seq;
908 u64 old_ack;
909
43b54c6e 910 status = get_mapping_status(ssk, msk);
e86f3f3c
PA
911 if (unlikely(status == MAPPING_INVALID))
912 goto fallback;
913
914 if (unlikely(status == MAPPING_DUMMY))
915 goto fallback;
648ef4b8
MM
916
917 if (status != MAPPING_OK)
918 return false;
919
920 skb = skb_peek(&ssk->sk_receive_queue);
921 if (WARN_ON_ONCE(!skb))
922 return false;
923
d22f4988
CP
924 /* if msk lacks the remote key, this subflow must provide an
925 * MP_CAPABLE-based mapping
926 */
927 if (unlikely(!READ_ONCE(msk->can_ack))) {
e86f3f3c
PA
928 if (!subflow->mpc_map)
929 goto fallback;
d22f4988
CP
930 WRITE_ONCE(msk->remote_key, subflow->remote_key);
931 WRITE_ONCE(msk->ack_seq, subflow->map_seq);
932 WRITE_ONCE(msk->can_ack, true);
933 }
934
648ef4b8
MM
935 old_ack = READ_ONCE(msk->ack_seq);
936 ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
937 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
938 ack_seq);
47bebdf3 939 if (ack_seq == old_ack) {
6719331c
PA
940 subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
941 break;
942 } else if (after64(ack_seq, old_ack)) {
943 subflow->data_avail = MPTCP_SUBFLOW_OOO_DATA;
648ef4b8 944 break;
47bebdf3 945 }
648ef4b8
MM
946
947 /* only accept in-sequence mapping. Old values are spurious
6719331c 948 * retransmission
648ef4b8 949 */
04e4cd4f 950 mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
648ef4b8
MM
951 }
952 return true;
953
e86f3f3c
PA
954fallback:
955 /* RFC 8684 section 3.7. */
956 if (subflow->mp_join || subflow->fully_established) {
957 /* fatal protocol error, close the socket.
958 * subflow_error_report() will introduce the appropriate barriers
959 */
960 ssk->sk_err = EBADMSG;
961 ssk->sk_error_report(ssk);
962 tcp_set_state(ssk, TCP_CLOSE);
963 tcp_send_active_reset(ssk, GFP_ATOMIC);
964 subflow->data_avail = 0;
965 return false;
966 }
967
968 __mptcp_do_fallback(msk);
969 skb = skb_peek(&ssk->sk_receive_queue);
970 subflow->map_valid = 1;
971 subflow->map_seq = READ_ONCE(msk->ack_seq);
972 subflow->map_data_len = skb->len;
973 subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
974 subflow->data_avail = MPTCP_SUBFLOW_DATA_AVAIL;
975 return true;
648ef4b8
MM
976}
977
978bool mptcp_subflow_data_available(struct sock *sk)
979{
980 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
648ef4b8
MM
981
982 /* check if current mapping is still valid */
983 if (subflow->map_valid &&
984 mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
985 subflow->map_valid = 0;
986 subflow->data_avail = 0;
987
988 pr_debug("Done with mapping: seq=%u data_len=%u",
989 subflow->map_subflow_seq,
990 subflow->map_data_len);
991 }
992
47bebdf3 993 return subflow_check_data_avail(sk);
648ef4b8
MM
994}
995
071c8ed6
FW
996/* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
997 * not the ssk one.
998 *
999 * In mptcp, rwin is about the mptcp-level connection data.
1000 *
1001 * Data that is still on the ssk rx queue can thus be ignored,
1002 * as far as mptcp peer is concerened that data is still inflight.
1003 * DSS ACK is updated when skb is moved to the mptcp rx queue.
1004 */
1005void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1006{
1007 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1008 const struct sock *sk = subflow->conn;
1009
ea4ca586 1010 *space = __mptcp_space(sk);
071c8ed6
FW
1011 *full_space = tcp_full_space(sk);
1012}
1013
648ef4b8
MM
1014static void subflow_data_ready(struct sock *sk)
1015{
1016 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
8c728940 1017 u16 state = 1 << inet_sk_state_load(sk);
648ef4b8 1018 struct sock *parent = subflow->conn;
e1ff9e82 1019 struct mptcp_sock *msk;
648ef4b8 1020
e1ff9e82 1021 msk = mptcp_sk(parent);
8c728940 1022 if (state & TCPF_LISTEN) {
36811acc
PA
1023 /* MPJ subflow are removed from accept queue before reaching here,
1024 * avoid stray wakeups
1025 */
1026 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1027 return;
1028
e1ff9e82 1029 set_bit(MPTCP_DATA_READY, &msk->flags);
dc093db5 1030 parent->sk_data_ready(parent);
648ef4b8
MM
1031 return;
1032 }
1033
e1ff9e82 1034 WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
8c728940 1035 !subflow->mp_join && !(state & TCPF_CLOSE));
e1ff9e82 1036
101f6f85 1037 if (mptcp_subflow_data_available(sk))
2e52213c 1038 mptcp_data_ready(parent, sk);
648ef4b8
MM
1039}
1040
6e628cd3 1041static void subflow_write_space(struct sock *ssk)
648ef4b8 1042{
6e628cd3 1043 /* we take action in __mptcp_clean_una() */
648ef4b8
MM
1044}
1045
a5b31c47
PA
1046void __mptcp_error_report(struct sock *sk)
1047{
1048 struct mptcp_subflow_context *subflow;
1049 struct mptcp_sock *msk = mptcp_sk(sk);
1050
1051 mptcp_for_each_subflow(msk, subflow) {
1052 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1053 int err = sock_error(ssk);
1054
1055 if (!err)
1056 continue;
1057
1058 /* only propagate errors on fallen-back sockets or
1059 * on MPC connect
1060 */
1061 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1062 continue;
1063
1064 inet_sk_state_store(sk, inet_sk_state_load(ssk));
1065 sk->sk_err = -err;
1066
1067 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
1068 smp_wmb();
1069 sk->sk_error_report(sk);
1070 break;
1071 }
1072}
1073
1074static void subflow_error_report(struct sock *ssk)
1075{
1076 struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1077
1078 mptcp_data_lock(sk);
1079 if (!sock_owned_by_user(sk))
1080 __mptcp_error_report(sk);
1081 else
1082 set_bit(MPTCP_ERROR_REPORT, &mptcp_sk(sk)->flags);
1083 mptcp_data_unlock(sk);
1084}
1085
cec37a6e
PK
1086static struct inet_connection_sock_af_ops *
1087subflow_default_af_ops(struct sock *sk)
1088{
1089#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1090 if (sk->sk_family == AF_INET6)
1091 return &subflow_v6_specific;
1092#endif
1093 return &subflow_specific;
1094}
1095
cec37a6e 1096#if IS_ENABLED(CONFIG_MPTCP_IPV6)
31484d56
GU
1097void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1098{
cec37a6e
PK
1099 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1100 struct inet_connection_sock *icsk = inet_csk(sk);
1101 struct inet_connection_sock_af_ops *target;
1102
1103 target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1104
1105 pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
edc7e489 1106 subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
cec37a6e
PK
1107
1108 if (likely(icsk->icsk_af_ops == target))
1109 return;
1110
1111 subflow->icsk_af_ops = icsk->icsk_af_ops;
1112 icsk->icsk_af_ops = target;
cec37a6e 1113}
31484d56 1114#endif
cec37a6e 1115
ec3edaa7
PK
1116static void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1117 struct sockaddr_storage *addr)
1118{
1119 memset(addr, 0, sizeof(*addr));
1120 addr->ss_family = info->family;
1121 if (addr->ss_family == AF_INET) {
1122 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1123
1124 in_addr->sin_addr = info->addr;
1125 in_addr->sin_port = info->port;
1126 }
1127#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1128 else if (addr->ss_family == AF_INET6) {
1129 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1130
1131 in6_addr->sin6_addr = info->addr6;
1132 in6_addr->sin6_port = info->port;
1133 }
1134#endif
1135}
1136
ef0da3b8 1137int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
ec3edaa7
PK
1138 const struct mptcp_addr_info *remote)
1139{
1140 struct mptcp_sock *msk = mptcp_sk(sk);
1141 struct mptcp_subflow_context *subflow;
1142 struct sockaddr_storage addr;
2ff0e566 1143 int remote_id = remote->id;
6bad912b 1144 int local_id = loc->id;
ec3edaa7 1145 struct socket *sf;
6bad912b 1146 struct sock *ssk;
ec3edaa7
PK
1147 u32 remote_token;
1148 int addrlen;
1149 int err;
1150
b93df08c 1151 if (!mptcp_is_fully_established(sk))
ec3edaa7
PK
1152 return -ENOTCONN;
1153
1154 err = mptcp_subflow_create_socket(sk, &sf);
1155 if (err)
1156 return err;
1157
6bad912b
PA
1158 ssk = sf->sk;
1159 subflow = mptcp_subflow_ctx(ssk);
1160 do {
1161 get_random_bytes(&subflow->local_nonce, sizeof(u32));
1162 } while (!subflow->local_nonce);
1163
1164 if (!local_id) {
1165 err = mptcp_pm_get_local_id(msk, (struct sock_common *)ssk);
1166 if (err < 0)
1167 goto failed;
1168
1169 local_id = err;
1170 }
1171
ec3edaa7
PK
1172 subflow->remote_key = msk->remote_key;
1173 subflow->local_key = msk->local_key;
1174 subflow->token = msk->token;
1175 mptcp_info2sockaddr(loc, &addr);
1176
1177 addrlen = sizeof(struct sockaddr_in);
1178#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1179 if (loc->family == AF_INET6)
1180 addrlen = sizeof(struct sockaddr_in6);
1181#endif
ef0da3b8 1182 ssk->sk_bound_dev_if = loc->ifindex;
ec3edaa7
PK
1183 err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1184 if (err)
1185 goto failed;
1186
1187 mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
2ff0e566
GT
1188 pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1189 remote_token, local_id, remote_id);
ec3edaa7 1190 subflow->remote_token = remote_token;
6bad912b 1191 subflow->local_id = local_id;
2ff0e566 1192 subflow->remote_id = remote_id;
ec3edaa7 1193 subflow->request_join = 1;
4596a2c1 1194 subflow->request_bkup = !!(loc->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
ec3edaa7
PK
1195 mptcp_info2sockaddr(remote, &addr);
1196
5b950ff4 1197 mptcp_add_pending_subflow(msk, subflow);
ec3edaa7
PK
1198 err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1199 if (err && err != -EINPROGRESS)
5b950ff4 1200 goto failed_unlink;
ec3edaa7 1201
133f0169
PA
1202 /* discard the subflow socket */
1203 mptcp_sock_graft(ssk, sk->sk_socket);
1204 iput(SOCK_INODE(sf));
ec3edaa7
PK
1205 return err;
1206
5b950ff4 1207failed_unlink:
ec3edaa7 1208 spin_lock_bh(&msk->join_list_lock);
5b950ff4 1209 list_del(&subflow->node);
ec3edaa7 1210 spin_unlock_bh(&msk->join_list_lock);
42093e32 1211 sock_put(mptcp_subflow_tcp_sock(subflow));
ec3edaa7 1212
ec3edaa7 1213failed:
e16163b6 1214 subflow->disposable = 1;
ec3edaa7
PK
1215 sock_release(sf);
1216 return err;
1217}
1218
3764b0c5
NR
1219static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1220{
1221#ifdef CONFIG_SOCK_CGROUP_DATA
1222 struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1223 *child_skcd = &child->sk_cgrp_data;
1224
1225 /* only the additional subflows created by kworkers have to be modified */
1226 if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1227 cgroup_id(sock_cgroup_ptr(child_skcd))) {
1228#ifdef CONFIG_MEMCG
1229 struct mem_cgroup *memcg = parent->sk_memcg;
1230
1231 mem_cgroup_sk_free(child);
1232 if (memcg && css_tryget(&memcg->css))
1233 child->sk_memcg = memcg;
1234#endif /* CONFIG_MEMCG */
1235
1236 cgroup_sk_free(child_skcd);
1237 *child_skcd = *parent_skcd;
1238 cgroup_sk_clone(child_skcd);
1239 }
1240#endif /* CONFIG_SOCK_CGROUP_DATA */
1241}
1242
2303f994
PK
1243int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1244{
1245 struct mptcp_subflow_context *subflow;
1246 struct net *net = sock_net(sk);
1247 struct socket *sf;
1248 int err;
1249
adf73410
PA
1250 /* un-accepted server sockets can reach here - on bad configuration
1251 * bail early to avoid greater trouble later
1252 */
1253 if (unlikely(!sk->sk_socket))
1254 return -EINVAL;
1255
cec37a6e
PK
1256 err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1257 &sf);
2303f994
PK
1258 if (err)
1259 return err;
1260
1261 lock_sock(sf->sk);
1262
3764b0c5
NR
1263 /* the newly created socket has to be in the same cgroup as its parent */
1264 mptcp_attach_cgroup(sk, sf->sk);
1265
2303f994
PK
1266 /* kernel sockets do not by default acquire net ref, but TCP timer
1267 * needs it.
1268 */
1269 sf->sk->sk_net_refcnt = 1;
1270 get_net(net);
f6f7d8cf 1271#ifdef CONFIG_PROC_FS
2303f994 1272 this_cpu_add(*net->core.sock_inuse, 1);
f6f7d8cf 1273#endif
2303f994
PK
1274 err = tcp_set_ulp(sf->sk, "mptcp");
1275 release_sock(sf->sk);
1276
b8ad540d
WY
1277 if (err) {
1278 sock_release(sf);
2303f994 1279 return err;
b8ad540d 1280 }
2303f994 1281
7d14b0d2
PA
1282 /* the newly created socket really belongs to the owning MPTCP master
1283 * socket, even if for additional subflows the allocation is performed
1284 * by a kernel workqueue. Adjust inode references, so that the
1285 * procfs/diag interaces really show this one belonging to the correct
1286 * user.
1287 */
1288 SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1289 SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1290 SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1291
2303f994
PK
1292 subflow = mptcp_subflow_ctx(sf->sk);
1293 pr_debug("subflow=%p", subflow);
1294
1295 *new_sock = sf;
79c0949e 1296 sock_hold(sk);
2303f994
PK
1297 subflow->conn = sk;
1298
1299 return 0;
1300}
1301
1302static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1303 gfp_t priority)
1304{
1305 struct inet_connection_sock *icsk = inet_csk(sk);
1306 struct mptcp_subflow_context *ctx;
1307
1308 ctx = kzalloc(sizeof(*ctx), priority);
1309 if (!ctx)
1310 return NULL;
1311
1312 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
cec37a6e 1313 INIT_LIST_HEAD(&ctx->node);
2303f994
PK
1314
1315 pr_debug("subflow=%p", ctx);
1316
1317 ctx->tcp_sock = sk;
1318
1319 return ctx;
1320}
1321
648ef4b8
MM
1322static void __subflow_state_change(struct sock *sk)
1323{
1324 struct socket_wq *wq;
1325
1326 rcu_read_lock();
1327 wq = rcu_dereference(sk->sk_wq);
1328 if (skwq_has_sleeper(wq))
1329 wake_up_interruptible_all(&wq->wait);
1330 rcu_read_unlock();
1331}
1332
1333static bool subflow_is_done(const struct sock *sk)
1334{
1335 return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1336}
1337
1338static void subflow_state_change(struct sock *sk)
1339{
1340 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
dc093db5 1341 struct sock *parent = subflow->conn;
648ef4b8
MM
1342
1343 __subflow_state_change(sk);
1344
8fd73804
DC
1345 if (subflow_simultaneous_connect(sk)) {
1346 mptcp_do_fallback(sk);
a6b118fe 1347 mptcp_rcv_space_init(mptcp_sk(parent), sk);
8fd73804
DC
1348 pr_fallback(mptcp_sk(parent));
1349 subflow->conn_finished = 1;
1350 if (inet_sk_state_load(parent) == TCP_SYN_SENT) {
1351 inet_sk_state_store(parent, TCP_ESTABLISHED);
1352 parent->sk_state_change(parent);
1353 }
1354 }
1355
648ef4b8
MM
1356 /* as recvmsg() does not acquire the subflow socket for ssk selection
1357 * a fin packet carrying a DSS can be unnoticed if we don't trigger
1358 * the data available machinery here.
1359 */
e1ff9e82 1360 if (mptcp_subflow_data_available(sk))
2e52213c 1361 mptcp_data_ready(parent, sk);
648ef4b8 1362
067a0b3d 1363 if (__mptcp_check_fallback(mptcp_sk(parent)) &&
648ef4b8
MM
1364 !subflow->rx_eof && subflow_is_done(sk)) {
1365 subflow->rx_eof = 1;
59832e24 1366 mptcp_subflow_eof(parent);
648ef4b8
MM
1367 }
1368}
1369
2303f994
PK
1370static int subflow_ulp_init(struct sock *sk)
1371{
cec37a6e 1372 struct inet_connection_sock *icsk = inet_csk(sk);
2303f994
PK
1373 struct mptcp_subflow_context *ctx;
1374 struct tcp_sock *tp = tcp_sk(sk);
1375 int err = 0;
1376
1377 /* disallow attaching ULP to a socket unless it has been
1378 * created with sock_create_kern()
1379 */
1380 if (!sk->sk_kern_sock) {
1381 err = -EOPNOTSUPP;
1382 goto out;
1383 }
1384
1385 ctx = subflow_create_ctx(sk, GFP_KERNEL);
1386 if (!ctx) {
1387 err = -ENOMEM;
1388 goto out;
1389 }
1390
1391 pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1392
1393 tp->is_mptcp = 1;
cec37a6e
PK
1394 ctx->icsk_af_ops = icsk->icsk_af_ops;
1395 icsk->icsk_af_ops = subflow_default_af_ops(sk);
648ef4b8
MM
1396 ctx->tcp_data_ready = sk->sk_data_ready;
1397 ctx->tcp_state_change = sk->sk_state_change;
1398 ctx->tcp_write_space = sk->sk_write_space;
a5b31c47 1399 ctx->tcp_error_report = sk->sk_error_report;
648ef4b8
MM
1400 sk->sk_data_ready = subflow_data_ready;
1401 sk->sk_write_space = subflow_write_space;
1402 sk->sk_state_change = subflow_state_change;
a5b31c47 1403 sk->sk_error_report = subflow_error_report;
2303f994
PK
1404out:
1405 return err;
1406}
1407
e16163b6 1408static void subflow_ulp_release(struct sock *ssk)
2303f994 1409{
e16163b6
PA
1410 struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1411 bool release = true;
1412 struct sock *sk;
2303f994
PK
1413
1414 if (!ctx)
1415 return;
1416
e16163b6
PA
1417 sk = ctx->conn;
1418 if (sk) {
1419 /* if the msk has been orphaned, keep the ctx
0597d0f8
PA
1420 * alive, will be freed by __mptcp_close_ssk(),
1421 * when the subflow is still unaccepted
e16163b6 1422 */
0597d0f8 1423 release = ctx->disposable || list_empty(&ctx->node);
e16163b6
PA
1424 sock_put(sk);
1425 }
79c0949e 1426
e16163b6
PA
1427 if (release)
1428 kfree_rcu(ctx, rcu);
2303f994
PK
1429}
1430
cec37a6e
PK
1431static void subflow_ulp_clone(const struct request_sock *req,
1432 struct sock *newsk,
1433 const gfp_t priority)
1434{
1435 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1436 struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1437 struct mptcp_subflow_context *new_ctx;
1438
f296234c
PK
1439 if (!tcp_rsk(req)->is_mptcp ||
1440 (!subflow_req->mp_capable && !subflow_req->mp_join)) {
648ef4b8 1441 subflow_ulp_fallback(newsk, old_ctx);
cec37a6e
PK
1442 return;
1443 }
1444
1445 new_ctx = subflow_create_ctx(newsk, priority);
edc7e489 1446 if (!new_ctx) {
648ef4b8 1447 subflow_ulp_fallback(newsk, old_ctx);
cec37a6e
PK
1448 return;
1449 }
1450
1451 new_ctx->conn_finished = 1;
1452 new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
648ef4b8
MM
1453 new_ctx->tcp_data_ready = old_ctx->tcp_data_ready;
1454 new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1455 new_ctx->tcp_write_space = old_ctx->tcp_write_space;
a5b31c47 1456 new_ctx->tcp_error_report = old_ctx->tcp_error_report;
58b09919
PA
1457 new_ctx->rel_write_seq = 1;
1458 new_ctx->tcp_sock = newsk;
1459
f296234c
PK
1460 if (subflow_req->mp_capable) {
1461 /* see comments in subflow_syn_recv_sock(), MPTCP connection
1462 * is fully established only after we receive the remote key
1463 */
1464 new_ctx->mp_capable = 1;
f296234c
PK
1465 new_ctx->local_key = subflow_req->local_key;
1466 new_ctx->token = subflow_req->token;
1467 new_ctx->ssn_offset = subflow_req->ssn_offset;
1468 new_ctx->idsn = subflow_req->idsn;
1469 } else if (subflow_req->mp_join) {
ec3edaa7 1470 new_ctx->ssn_offset = subflow_req->ssn_offset;
f296234c
PK
1471 new_ctx->mp_join = 1;
1472 new_ctx->fully_established = 1;
1473 new_ctx->backup = subflow_req->backup;
1474 new_ctx->local_id = subflow_req->local_id;
2ff0e566 1475 new_ctx->remote_id = subflow_req->remote_id;
f296234c
PK
1476 new_ctx->token = subflow_req->token;
1477 new_ctx->thmac = subflow_req->thmac;
1478 }
cec37a6e
PK
1479}
1480
2303f994
PK
1481static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1482 .name = "mptcp",
1483 .owner = THIS_MODULE,
1484 .init = subflow_ulp_init,
1485 .release = subflow_ulp_release,
cec37a6e 1486 .clone = subflow_ulp_clone,
2303f994
PK
1487};
1488
cec37a6e
PK
1489static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1490{
1491 subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1492 subflow_ops->slab_name = "request_sock_subflow";
1493
1494 subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1495 subflow_ops->obj_size, 0,
1496 SLAB_ACCOUNT |
1497 SLAB_TYPESAFE_BY_RCU,
1498 NULL);
1499 if (!subflow_ops->slab)
1500 return -ENOMEM;
1501
79c0949e
PK
1502 subflow_ops->destructor = subflow_req_destructor;
1503
cec37a6e
PK
1504 return 0;
1505}
1506
d39dceca 1507void __init mptcp_subflow_init(void)
2303f994 1508{
08b8d080
FW
1509 mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
1510 if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
cec37a6e
PK
1511 panic("MPTCP: failed to init subflow request sock ops\n");
1512
1513 subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
7ea851d1 1514 subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
cec37a6e
PK
1515
1516 subflow_specific = ipv4_specific;
1517 subflow_specific.conn_request = subflow_v4_conn_request;
1518 subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1519 subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1520
1521#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1522 subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
7ea851d1 1523 subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
cec37a6e
PK
1524
1525 subflow_v6_specific = ipv6_specific;
1526 subflow_v6_specific.conn_request = subflow_v6_conn_request;
1527 subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1528 subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1529
1530 subflow_v6m_specific = subflow_v6_specific;
1531 subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1532 subflow_v6m_specific.send_check = ipv4_specific.send_check;
1533 subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1534 subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1535 subflow_v6m_specific.net_frag_header_len = 0;
1536#endif
1537
5147dfb5
DC
1538 mptcp_diag_subflow_init(&subflow_ulp_ops);
1539
2303f994
PK
1540 if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1541 panic("MPTCP: failed to register subflows to ULP\n");
1542}