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