Bluetooth: Change authentication requirement.
[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_init(listener->conn, &mp_opt, child, req);
755                         if (!ctx->conn)
756                                 goto fallback;
757
758                         owner = mptcp_sk(ctx->conn);
759                         mptcp_pm_new_connection(owner, child, 1);
760
761                         /* with OoO packets we can reach here without ingress
762                          * mpc option
763                          */
764                         if (mp_opt.suboptions & OPTIONS_MPTCP_MPC) {
765                                 mptcp_subflow_fully_established(ctx, &mp_opt);
766                                 mptcp_pm_fully_established(owner, child, GFP_ATOMIC);
767                                 ctx->pm_notified = 1;
768                         }
769                 } else if (ctx->mp_join) {
770                         owner = subflow_req->msk;
771                         if (!owner) {
772                                 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
773                                 goto dispose_child;
774                         }
775
776                         /* move the msk reference ownership to the subflow */
777                         subflow_req->msk = NULL;
778                         ctx->conn = (struct sock *)owner;
779
780                         if (subflow_use_different_sport(owner, sk)) {
781                                 pr_debug("ack inet_sport=%d %d",
782                                          ntohs(inet_sk(sk)->inet_sport),
783                                          ntohs(inet_sk((struct sock *)owner)->inet_sport));
784                                 if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
785                                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
786                                         goto dispose_child;
787                                 }
788                                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
789                         }
790
791                         if (!mptcp_finish_join(child))
792                                 goto dispose_child;
793
794                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
795                         tcp_rsk(req)->drop_req = true;
796                 }
797         }
798
799         /* check for expected invariant - should never trigger, just help
800          * catching eariler subtle bugs
801          */
802         WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
803                      (!mptcp_subflow_ctx(child) ||
804                       !mptcp_subflow_ctx(child)->conn));
805         return child;
806
807 dispose_child:
808         mptcp_subflow_drop_ctx(child);
809         tcp_rsk(req)->drop_req = true;
810         inet_csk_prepare_for_destroy_sock(child);
811         tcp_done(child);
812         req->rsk_ops->send_reset(sk, skb);
813
814         /* The last child reference will be released by the caller */
815         return child;
816
817 fallback:
818         mptcp_subflow_drop_ctx(child);
819         return child;
820 }
821
822 static struct inet_connection_sock_af_ops subflow_specific __ro_after_init;
823 static struct proto tcp_prot_override __ro_after_init;
824
825 enum mapping_status {
826         MAPPING_OK,
827         MAPPING_INVALID,
828         MAPPING_EMPTY,
829         MAPPING_DATA_FIN,
830         MAPPING_DUMMY,
831         MAPPING_BAD_CSUM
832 };
833
834 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
835 {
836         pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
837                  ssn, subflow->map_subflow_seq, subflow->map_data_len);
838 }
839
840 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
841 {
842         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
843         unsigned int skb_consumed;
844
845         skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
846         if (WARN_ON_ONCE(skb_consumed >= skb->len))
847                 return true;
848
849         return skb->len - skb_consumed <= subflow->map_data_len -
850                                           mptcp_subflow_get_map_offset(subflow);
851 }
852
853 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
854 {
855         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
856         u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
857
858         if (unlikely(before(ssn, subflow->map_subflow_seq))) {
859                 /* Mapping covers data later in the subflow stream,
860                  * currently unsupported.
861                  */
862                 dbg_bad_map(subflow, ssn);
863                 return false;
864         }
865         if (unlikely(!before(ssn, subflow->map_subflow_seq +
866                                   subflow->map_data_len))) {
867                 /* Mapping does covers past subflow data, invalid */
868                 dbg_bad_map(subflow, ssn);
869                 return false;
870         }
871         return true;
872 }
873
874 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
875                                               bool csum_reqd)
876 {
877         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
878         u32 offset, seq, delta;
879         __sum16 csum;
880         int len;
881
882         if (!csum_reqd)
883                 return MAPPING_OK;
884
885         /* mapping already validated on previous traversal */
886         if (subflow->map_csum_len == subflow->map_data_len)
887                 return MAPPING_OK;
888
889         /* traverse the receive queue, ensuring it contains a full
890          * DSS mapping and accumulating the related csum.
891          * Preserve the accoumlate csum across multiple calls, to compute
892          * the csum only once
893          */
894         delta = subflow->map_data_len - subflow->map_csum_len;
895         for (;;) {
896                 seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
897                 offset = seq - TCP_SKB_CB(skb)->seq;
898
899                 /* if the current skb has not been accounted yet, csum its contents
900                  * up to the amount covered by the current DSS
901                  */
902                 if (offset < skb->len) {
903                         __wsum csum;
904
905                         len = min(skb->len - offset, delta);
906                         csum = skb_checksum(skb, offset, len, 0);
907                         subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
908                                                                 subflow->map_csum_len);
909
910                         delta -= len;
911                         subflow->map_csum_len += len;
912                 }
913                 if (delta == 0)
914                         break;
915
916                 if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
917                         /* if this subflow is closed, the partial mapping
918                          * will be never completed; flush the pending skbs, so
919                          * that subflow_sched_work_if_closed() can kick in
920                          */
921                         if (unlikely(ssk->sk_state == TCP_CLOSE))
922                                 while ((skb = skb_peek(&ssk->sk_receive_queue)))
923                                         sk_eat_skb(ssk, skb);
924
925                         /* not enough data to validate the csum */
926                         return MAPPING_EMPTY;
927                 }
928
929                 /* the DSS mapping for next skbs will be validated later,
930                  * when a get_mapping_status call will process such skb
931                  */
932                 skb = skb->next;
933         }
934
935         /* note that 'map_data_len' accounts only for the carried data, does
936          * not include the eventual seq increment due to the data fin,
937          * while the pseudo header requires the original DSS data len,
938          * including that
939          */
940         csum = __mptcp_make_csum(subflow->map_seq,
941                                  subflow->map_subflow_seq,
942                                  subflow->map_data_len + subflow->map_data_fin,
943                                  subflow->map_data_csum);
944         if (unlikely(csum)) {
945                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
946                 return MAPPING_BAD_CSUM;
947         }
948
949         subflow->valid_csum_seen = 1;
950         return MAPPING_OK;
951 }
952
953 static enum mapping_status get_mapping_status(struct sock *ssk,
954                                               struct mptcp_sock *msk)
955 {
956         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
957         bool csum_reqd = READ_ONCE(msk->csum_enabled);
958         struct mptcp_ext *mpext;
959         struct sk_buff *skb;
960         u16 data_len;
961         u64 map_seq;
962
963         skb = skb_peek(&ssk->sk_receive_queue);
964         if (!skb)
965                 return MAPPING_EMPTY;
966
967         if (mptcp_check_fallback(ssk))
968                 return MAPPING_DUMMY;
969
970         mpext = mptcp_get_ext(skb);
971         if (!mpext || !mpext->use_map) {
972                 if (!subflow->map_valid && !skb->len) {
973                         /* the TCP stack deliver 0 len FIN pkt to the receive
974                          * queue, that is the only 0len pkts ever expected here,
975                          * and we can admit no mapping only for 0 len pkts
976                          */
977                         if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
978                                 WARN_ONCE(1, "0len seq %d:%d flags %x",
979                                           TCP_SKB_CB(skb)->seq,
980                                           TCP_SKB_CB(skb)->end_seq,
981                                           TCP_SKB_CB(skb)->tcp_flags);
982                         sk_eat_skb(ssk, skb);
983                         return MAPPING_EMPTY;
984                 }
985
986                 if (!subflow->map_valid)
987                         return MAPPING_INVALID;
988
989                 goto validate_seq;
990         }
991
992         trace_get_mapping_status(mpext);
993
994         data_len = mpext->data_len;
995         if (data_len == 0) {
996                 pr_debug("infinite mapping received");
997                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
998                 subflow->map_data_len = 0;
999                 return MAPPING_INVALID;
1000         }
1001
1002         if (mpext->data_fin == 1) {
1003                 if (data_len == 1) {
1004                         bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
1005                                                                  mpext->dsn64);
1006                         pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
1007                         if (subflow->map_valid) {
1008                                 /* A DATA_FIN might arrive in a DSS
1009                                  * option before the previous mapping
1010                                  * has been fully consumed. Continue
1011                                  * handling the existing mapping.
1012                                  */
1013                                 skb_ext_del(skb, SKB_EXT_MPTCP);
1014                                 return MAPPING_OK;
1015                         } else {
1016                                 if (updated)
1017                                         mptcp_schedule_work((struct sock *)msk);
1018
1019                                 return MAPPING_DATA_FIN;
1020                         }
1021                 } else {
1022                         u64 data_fin_seq = mpext->data_seq + data_len - 1;
1023
1024                         /* If mpext->data_seq is a 32-bit value, data_fin_seq
1025                          * must also be limited to 32 bits.
1026                          */
1027                         if (!mpext->dsn64)
1028                                 data_fin_seq &= GENMASK_ULL(31, 0);
1029
1030                         mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
1031                         pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
1032                                  data_fin_seq, mpext->dsn64);
1033                 }
1034
1035                 /* Adjust for DATA_FIN using 1 byte of sequence space */
1036                 data_len--;
1037         }
1038
1039         map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
1040         WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
1041
1042         if (subflow->map_valid) {
1043                 /* Allow replacing only with an identical map */
1044                 if (subflow->map_seq == map_seq &&
1045                     subflow->map_subflow_seq == mpext->subflow_seq &&
1046                     subflow->map_data_len == data_len &&
1047                     subflow->map_csum_reqd == mpext->csum_reqd) {
1048                         skb_ext_del(skb, SKB_EXT_MPTCP);
1049                         goto validate_csum;
1050                 }
1051
1052                 /* If this skb data are fully covered by the current mapping,
1053                  * the new map would need caching, which is not supported
1054                  */
1055                 if (skb_is_fully_mapped(ssk, skb)) {
1056                         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
1057                         return MAPPING_INVALID;
1058                 }
1059
1060                 /* will validate the next map after consuming the current one */
1061                 goto validate_csum;
1062         }
1063
1064         subflow->map_seq = map_seq;
1065         subflow->map_subflow_seq = mpext->subflow_seq;
1066         subflow->map_data_len = data_len;
1067         subflow->map_valid = 1;
1068         subflow->map_data_fin = mpext->data_fin;
1069         subflow->mpc_map = mpext->mpc_map;
1070         subflow->map_csum_reqd = mpext->csum_reqd;
1071         subflow->map_csum_len = 0;
1072         subflow->map_data_csum = csum_unfold(mpext->csum);
1073
1074         /* Cfr RFC 8684 Section 3.3.0 */
1075         if (unlikely(subflow->map_csum_reqd != csum_reqd))
1076                 return MAPPING_INVALID;
1077
1078         pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
1079                  subflow->map_seq, subflow->map_subflow_seq,
1080                  subflow->map_data_len, subflow->map_csum_reqd,
1081                  subflow->map_data_csum);
1082
1083 validate_seq:
1084         /* we revalidate valid mapping on new skb, because we must ensure
1085          * the current skb is completely covered by the available mapping
1086          */
1087         if (!validate_mapping(ssk, skb)) {
1088                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
1089                 return MAPPING_INVALID;
1090         }
1091
1092         skb_ext_del(skb, SKB_EXT_MPTCP);
1093
1094 validate_csum:
1095         return validate_data_csum(ssk, skb, csum_reqd);
1096 }
1097
1098 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1099                                        u64 limit)
1100 {
1101         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1102         bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
1103         u32 incr;
1104
1105         incr = limit >= skb->len ? skb->len + fin : limit;
1106
1107         pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
1108                  subflow->map_subflow_seq);
1109         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
1110         tcp_sk(ssk)->copied_seq += incr;
1111         if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
1112                 sk_eat_skb(ssk, skb);
1113         if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
1114                 subflow->map_valid = 0;
1115 }
1116
1117 /* sched mptcp worker to remove the subflow if no more data is pending */
1118 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
1119 {
1120         if (likely(ssk->sk_state != TCP_CLOSE))
1121                 return;
1122
1123         if (skb_queue_empty(&ssk->sk_receive_queue) &&
1124             !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
1125                 mptcp_schedule_work((struct sock *)msk);
1126 }
1127
1128 static bool subflow_can_fallback(struct mptcp_subflow_context *subflow)
1129 {
1130         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1131
1132         if (subflow->mp_join)
1133                 return false;
1134         else if (READ_ONCE(msk->csum_enabled))
1135                 return !subflow->valid_csum_seen;
1136         else
1137                 return !subflow->fully_established;
1138 }
1139
1140 static void mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk)
1141 {
1142         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1143         unsigned long fail_tout;
1144
1145         /* greceful failure can happen only on the MPC subflow */
1146         if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first)))
1147                 return;
1148
1149         /* since the close timeout take precedence on the fail one,
1150          * no need to start the latter when the first is already set
1151          */
1152         if (sock_flag((struct sock *)msk, SOCK_DEAD))
1153                 return;
1154
1155         /* we don't need extreme accuracy here, use a zero fail_tout as special
1156          * value meaning no fail timeout at all;
1157          */
1158         fail_tout = jiffies + TCP_RTO_MAX;
1159         if (!fail_tout)
1160                 fail_tout = 1;
1161         WRITE_ONCE(subflow->fail_tout, fail_tout);
1162         tcp_send_ack(ssk);
1163
1164         mptcp_reset_timeout(msk, subflow->fail_tout);
1165 }
1166
1167 static bool subflow_check_data_avail(struct sock *ssk)
1168 {
1169         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1170         enum mapping_status status;
1171         struct mptcp_sock *msk;
1172         struct sk_buff *skb;
1173
1174         if (!skb_peek(&ssk->sk_receive_queue))
1175                 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1176         if (subflow->data_avail)
1177                 return true;
1178
1179         msk = mptcp_sk(subflow->conn);
1180         for (;;) {
1181                 u64 ack_seq;
1182                 u64 old_ack;
1183
1184                 status = get_mapping_status(ssk, msk);
1185                 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1186                 if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY ||
1187                              status == MAPPING_BAD_CSUM))
1188                         goto fallback;
1189
1190                 if (status != MAPPING_OK)
1191                         goto no_data;
1192
1193                 skb = skb_peek(&ssk->sk_receive_queue);
1194                 if (WARN_ON_ONCE(!skb))
1195                         goto no_data;
1196
1197                 /* if msk lacks the remote key, this subflow must provide an
1198                  * MP_CAPABLE-based mapping
1199                  */
1200                 if (unlikely(!READ_ONCE(msk->can_ack))) {
1201                         if (!subflow->mpc_map)
1202                                 goto fallback;
1203                         WRITE_ONCE(msk->remote_key, subflow->remote_key);
1204                         WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1205                         WRITE_ONCE(msk->can_ack, true);
1206                 }
1207
1208                 old_ack = READ_ONCE(msk->ack_seq);
1209                 ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1210                 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1211                          ack_seq);
1212                 if (unlikely(before64(ack_seq, old_ack))) {
1213                         mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1214                         continue;
1215                 }
1216
1217                 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1218                 break;
1219         }
1220         return true;
1221
1222 no_data:
1223         subflow_sched_work_if_closed(msk, ssk);
1224         return false;
1225
1226 fallback:
1227         if (!__mptcp_check_fallback(msk)) {
1228                 /* RFC 8684 section 3.7. */
1229                 if (status == MAPPING_BAD_CSUM &&
1230                     (subflow->mp_join || subflow->valid_csum_seen)) {
1231                         subflow->send_mp_fail = 1;
1232
1233                         if (!READ_ONCE(msk->allow_infinite_fallback)) {
1234                                 subflow->reset_transient = 0;
1235                                 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
1236                                 goto reset;
1237                         }
1238                         mptcp_subflow_fail(msk, ssk);
1239                         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1240                         return true;
1241                 }
1242
1243                 if (!subflow_can_fallback(subflow) && subflow->map_data_len) {
1244                         /* fatal protocol error, close the socket.
1245                          * subflow_error_report() will introduce the appropriate barriers
1246                          */
1247                         subflow->reset_transient = 0;
1248                         subflow->reset_reason = MPTCP_RST_EMPTCP;
1249
1250 reset:
1251                         ssk->sk_err = EBADMSG;
1252                         tcp_set_state(ssk, TCP_CLOSE);
1253                         while ((skb = skb_peek(&ssk->sk_receive_queue)))
1254                                 sk_eat_skb(ssk, skb);
1255                         tcp_send_active_reset(ssk, GFP_ATOMIC);
1256                         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1257                         return false;
1258                 }
1259
1260                 mptcp_do_fallback(ssk);
1261         }
1262
1263         skb = skb_peek(&ssk->sk_receive_queue);
1264         subflow->map_valid = 1;
1265         subflow->map_seq = READ_ONCE(msk->ack_seq);
1266         subflow->map_data_len = skb->len;
1267         subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1268         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1269         return true;
1270 }
1271
1272 bool mptcp_subflow_data_available(struct sock *sk)
1273 {
1274         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1275
1276         /* check if current mapping is still valid */
1277         if (subflow->map_valid &&
1278             mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1279                 subflow->map_valid = 0;
1280                 WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1281
1282                 pr_debug("Done with mapping: seq=%u data_len=%u",
1283                          subflow->map_subflow_seq,
1284                          subflow->map_data_len);
1285         }
1286
1287         return subflow_check_data_avail(sk);
1288 }
1289
1290 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1291  * not the ssk one.
1292  *
1293  * In mptcp, rwin is about the mptcp-level connection data.
1294  *
1295  * Data that is still on the ssk rx queue can thus be ignored,
1296  * as far as mptcp peer is concerned that data is still inflight.
1297  * DSS ACK is updated when skb is moved to the mptcp rx queue.
1298  */
1299 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1300 {
1301         const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1302         const struct sock *sk = subflow->conn;
1303
1304         *space = __mptcp_space(sk);
1305         *full_space = tcp_full_space(sk);
1306 }
1307
1308 void __mptcp_error_report(struct sock *sk)
1309 {
1310         struct mptcp_subflow_context *subflow;
1311         struct mptcp_sock *msk = mptcp_sk(sk);
1312
1313         mptcp_for_each_subflow(msk, subflow) {
1314                 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1315                 int err = sock_error(ssk);
1316                 int ssk_state;
1317
1318                 if (!err)
1319                         continue;
1320
1321                 /* only propagate errors on fallen-back sockets or
1322                  * on MPC connect
1323                  */
1324                 if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1325                         continue;
1326
1327                 /* We need to propagate only transition to CLOSE state.
1328                  * Orphaned socket will see such state change via
1329                  * subflow_sched_work_if_closed() and that path will properly
1330                  * destroy the msk as needed.
1331                  */
1332                 ssk_state = inet_sk_state_load(ssk);
1333                 if (ssk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DEAD))
1334                         inet_sk_state_store(sk, ssk_state);
1335                 sk->sk_err = -err;
1336
1337                 /* This barrier is coupled with smp_rmb() in mptcp_poll() */
1338                 smp_wmb();
1339                 sk_error_report(sk);
1340                 break;
1341         }
1342 }
1343
1344 static void subflow_error_report(struct sock *ssk)
1345 {
1346         struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1347
1348         /* bail early if this is a no-op, so that we avoid introducing a
1349          * problematic lockdep dependency between TCP accept queue lock
1350          * and msk socket spinlock
1351          */
1352         if (!sk->sk_socket)
1353                 return;
1354
1355         mptcp_data_lock(sk);
1356         if (!sock_owned_by_user(sk))
1357                 __mptcp_error_report(sk);
1358         else
1359                 __set_bit(MPTCP_ERROR_REPORT,  &mptcp_sk(sk)->cb_flags);
1360         mptcp_data_unlock(sk);
1361 }
1362
1363 static void subflow_data_ready(struct sock *sk)
1364 {
1365         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1366         u16 state = 1 << inet_sk_state_load(sk);
1367         struct sock *parent = subflow->conn;
1368         struct mptcp_sock *msk;
1369
1370         msk = mptcp_sk(parent);
1371         if (state & TCPF_LISTEN) {
1372                 /* MPJ subflow are removed from accept queue before reaching here,
1373                  * avoid stray wakeups
1374                  */
1375                 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1376                         return;
1377
1378                 parent->sk_data_ready(parent);
1379                 return;
1380         }
1381
1382         WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1383                      !subflow->mp_join && !(state & TCPF_CLOSE));
1384
1385         if (mptcp_subflow_data_available(sk))
1386                 mptcp_data_ready(parent, sk);
1387         else if (unlikely(sk->sk_err))
1388                 subflow_error_report(sk);
1389 }
1390
1391 static void subflow_write_space(struct sock *ssk)
1392 {
1393         struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1394
1395         mptcp_propagate_sndbuf(sk, ssk);
1396         mptcp_write_space(sk);
1397 }
1398
1399 static const struct inet_connection_sock_af_ops *
1400 subflow_default_af_ops(struct sock *sk)
1401 {
1402 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1403         if (sk->sk_family == AF_INET6)
1404                 return &subflow_v6_specific;
1405 #endif
1406         return &subflow_specific;
1407 }
1408
1409 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1410 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1411 {
1412         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1413         struct inet_connection_sock *icsk = inet_csk(sk);
1414         const struct inet_connection_sock_af_ops *target;
1415
1416         target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1417
1418         pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1419                  subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1420
1421         if (likely(icsk->icsk_af_ops == target))
1422                 return;
1423
1424         subflow->icsk_af_ops = icsk->icsk_af_ops;
1425         icsk->icsk_af_ops = target;
1426 }
1427 #endif
1428
1429 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1430                          struct sockaddr_storage *addr,
1431                          unsigned short family)
1432 {
1433         memset(addr, 0, sizeof(*addr));
1434         addr->ss_family = family;
1435         if (addr->ss_family == AF_INET) {
1436                 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1437
1438                 if (info->family == AF_INET)
1439                         in_addr->sin_addr = info->addr;
1440 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1441                 else if (ipv6_addr_v4mapped(&info->addr6))
1442                         in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1443 #endif
1444                 in_addr->sin_port = info->port;
1445         }
1446 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1447         else if (addr->ss_family == AF_INET6) {
1448                 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1449
1450                 if (info->family == AF_INET)
1451                         ipv6_addr_set_v4mapped(info->addr.s_addr,
1452                                                &in6_addr->sin6_addr);
1453                 else
1454                         in6_addr->sin6_addr = info->addr6;
1455                 in6_addr->sin6_port = info->port;
1456         }
1457 #endif
1458 }
1459
1460 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1461                             const struct mptcp_addr_info *remote)
1462 {
1463         struct mptcp_sock *msk = mptcp_sk(sk);
1464         struct mptcp_subflow_context *subflow;
1465         struct sockaddr_storage addr;
1466         int remote_id = remote->id;
1467         int local_id = loc->id;
1468         int err = -ENOTCONN;
1469         struct socket *sf;
1470         struct sock *ssk;
1471         u32 remote_token;
1472         int addrlen;
1473         int ifindex;
1474         u8 flags;
1475
1476         if (!mptcp_is_fully_established(sk))
1477                 goto err_out;
1478
1479         err = mptcp_subflow_create_socket(sk, loc->family, &sf);
1480         if (err)
1481                 goto err_out;
1482
1483         ssk = sf->sk;
1484         subflow = mptcp_subflow_ctx(ssk);
1485         do {
1486                 get_random_bytes(&subflow->local_nonce, sizeof(u32));
1487         } while (!subflow->local_nonce);
1488
1489         if (local_id)
1490                 subflow_set_local_id(subflow, local_id);
1491
1492         mptcp_pm_get_flags_and_ifindex_by_id(msk, local_id,
1493                                              &flags, &ifindex);
1494         subflow->remote_key = msk->remote_key;
1495         subflow->local_key = msk->local_key;
1496         subflow->token = msk->token;
1497         mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1498
1499         addrlen = sizeof(struct sockaddr_in);
1500 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1501         if (addr.ss_family == AF_INET6)
1502                 addrlen = sizeof(struct sockaddr_in6);
1503 #endif
1504         mptcp_sockopt_sync(msk, ssk);
1505
1506         ssk->sk_bound_dev_if = ifindex;
1507         err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1508         if (err)
1509                 goto failed;
1510
1511         mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1512         pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1513                  remote_token, local_id, remote_id);
1514         subflow->remote_token = remote_token;
1515         subflow->remote_id = remote_id;
1516         subflow->request_join = 1;
1517         subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1518         mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1519
1520         sock_hold(ssk);
1521         list_add_tail(&subflow->node, &msk->conn_list);
1522         err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1523         if (err && err != -EINPROGRESS)
1524                 goto failed_unlink;
1525
1526         /* discard the subflow socket */
1527         mptcp_sock_graft(ssk, sk->sk_socket);
1528         iput(SOCK_INODE(sf));
1529         WRITE_ONCE(msk->allow_infinite_fallback, false);
1530         return 0;
1531
1532 failed_unlink:
1533         list_del(&subflow->node);
1534         sock_put(mptcp_subflow_tcp_sock(subflow));
1535
1536 failed:
1537         subflow->disposable = 1;
1538         sock_release(sf);
1539
1540 err_out:
1541         /* we account subflows before the creation, and this failures will not
1542          * be caught by sk_state_change()
1543          */
1544         mptcp_pm_close_subflow(msk);
1545         return err;
1546 }
1547
1548 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1549 {
1550 #ifdef CONFIG_SOCK_CGROUP_DATA
1551         struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1552                                 *child_skcd = &child->sk_cgrp_data;
1553
1554         /* only the additional subflows created by kworkers have to be modified */
1555         if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1556             cgroup_id(sock_cgroup_ptr(child_skcd))) {
1557 #ifdef CONFIG_MEMCG
1558                 struct mem_cgroup *memcg = parent->sk_memcg;
1559
1560                 mem_cgroup_sk_free(child);
1561                 if (memcg && css_tryget(&memcg->css))
1562                         child->sk_memcg = memcg;
1563 #endif /* CONFIG_MEMCG */
1564
1565                 cgroup_sk_free(child_skcd);
1566                 *child_skcd = *parent_skcd;
1567                 cgroup_sk_clone(child_skcd);
1568         }
1569 #endif /* CONFIG_SOCK_CGROUP_DATA */
1570 }
1571
1572 static void mptcp_subflow_ops_override(struct sock *ssk)
1573 {
1574 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1575         if (ssk->sk_prot == &tcpv6_prot)
1576                 ssk->sk_prot = &tcpv6_prot_override;
1577         else
1578 #endif
1579                 ssk->sk_prot = &tcp_prot_override;
1580 }
1581
1582 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1583 {
1584 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1585         if (ssk->sk_prot == &tcpv6_prot_override)
1586                 ssk->sk_prot = &tcpv6_prot;
1587         else
1588 #endif
1589                 ssk->sk_prot = &tcp_prot;
1590 }
1591
1592 int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
1593                                 struct socket **new_sock)
1594 {
1595         struct mptcp_subflow_context *subflow;
1596         struct net *net = sock_net(sk);
1597         struct socket *sf;
1598         int err;
1599
1600         /* un-accepted server sockets can reach here - on bad configuration
1601          * bail early to avoid greater trouble later
1602          */
1603         if (unlikely(!sk->sk_socket))
1604                 return -EINVAL;
1605
1606         err = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, &sf);
1607         if (err)
1608                 return err;
1609
1610         lock_sock_nested(sf->sk, SINGLE_DEPTH_NESTING);
1611
1612         /* the newly created socket has to be in the same cgroup as its parent */
1613         mptcp_attach_cgroup(sk, sf->sk);
1614
1615         /* kernel sockets do not by default acquire net ref, but TCP timer
1616          * needs it.
1617          */
1618         sf->sk->sk_net_refcnt = 1;
1619         get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL);
1620         sock_inuse_add(net, 1);
1621         err = tcp_set_ulp(sf->sk, "mptcp");
1622         release_sock(sf->sk);
1623
1624         if (err) {
1625                 sock_release(sf);
1626                 return err;
1627         }
1628
1629         /* the newly created socket really belongs to the owning MPTCP master
1630          * socket, even if for additional subflows the allocation is performed
1631          * by a kernel workqueue. Adjust inode references, so that the
1632          * procfs/diag interfaces really show this one belonging to the correct
1633          * user.
1634          */
1635         SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1636         SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1637         SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1638
1639         subflow = mptcp_subflow_ctx(sf->sk);
1640         pr_debug("subflow=%p", subflow);
1641
1642         *new_sock = sf;
1643         sock_hold(sk);
1644         subflow->conn = sk;
1645         mptcp_subflow_ops_override(sf->sk);
1646
1647         return 0;
1648 }
1649
1650 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1651                                                         gfp_t priority)
1652 {
1653         struct inet_connection_sock *icsk = inet_csk(sk);
1654         struct mptcp_subflow_context *ctx;
1655
1656         ctx = kzalloc(sizeof(*ctx), priority);
1657         if (!ctx)
1658                 return NULL;
1659
1660         rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1661         INIT_LIST_HEAD(&ctx->node);
1662         INIT_LIST_HEAD(&ctx->delegated_node);
1663
1664         pr_debug("subflow=%p", ctx);
1665
1666         ctx->tcp_sock = sk;
1667
1668         return ctx;
1669 }
1670
1671 static void __subflow_state_change(struct sock *sk)
1672 {
1673         struct socket_wq *wq;
1674
1675         rcu_read_lock();
1676         wq = rcu_dereference(sk->sk_wq);
1677         if (skwq_has_sleeper(wq))
1678                 wake_up_interruptible_all(&wq->wait);
1679         rcu_read_unlock();
1680 }
1681
1682 static bool subflow_is_done(const struct sock *sk)
1683 {
1684         return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1685 }
1686
1687 static void subflow_state_change(struct sock *sk)
1688 {
1689         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1690         struct sock *parent = subflow->conn;
1691         struct mptcp_sock *msk;
1692
1693         __subflow_state_change(sk);
1694
1695         msk = mptcp_sk(parent);
1696         if (subflow_simultaneous_connect(sk)) {
1697                 mptcp_propagate_sndbuf(parent, sk);
1698                 mptcp_do_fallback(sk);
1699                 mptcp_rcv_space_init(msk, sk);
1700                 pr_fallback(msk);
1701                 subflow->conn_finished = 1;
1702                 mptcp_set_connected(parent);
1703         }
1704
1705         /* as recvmsg() does not acquire the subflow socket for ssk selection
1706          * a fin packet carrying a DSS can be unnoticed if we don't trigger
1707          * the data available machinery here.
1708          */
1709         if (mptcp_subflow_data_available(sk))
1710                 mptcp_data_ready(parent, sk);
1711         else if (unlikely(sk->sk_err))
1712                 subflow_error_report(sk);
1713
1714         subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1715
1716         /* when the fallback subflow closes the rx side, trigger a 'dummy'
1717          * ingress data fin, so that the msk state will follow along
1718          */
1719         if (__mptcp_check_fallback(msk) && subflow_is_done(sk) && msk->first == sk &&
1720             mptcp_update_rcv_data_fin(msk, READ_ONCE(msk->ack_seq), true))
1721                 mptcp_schedule_work(parent);
1722 }
1723
1724 void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk)
1725 {
1726         struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1727         struct mptcp_sock *msk, *next, *head = NULL;
1728         struct request_sock *req;
1729         struct sock *sk;
1730
1731         /* build a list of all unaccepted mptcp sockets */
1732         spin_lock_bh(&queue->rskq_lock);
1733         for (req = queue->rskq_accept_head; req; req = req->dl_next) {
1734                 struct mptcp_subflow_context *subflow;
1735                 struct sock *ssk = req->sk;
1736
1737                 if (!sk_is_mptcp(ssk))
1738                         continue;
1739
1740                 subflow = mptcp_subflow_ctx(ssk);
1741                 if (!subflow || !subflow->conn)
1742                         continue;
1743
1744                 /* skip if already in list */
1745                 sk = subflow->conn;
1746                 msk = mptcp_sk(sk);
1747                 if (msk->dl_next || msk == head)
1748                         continue;
1749
1750                 sock_hold(sk);
1751                 msk->dl_next = head;
1752                 head = msk;
1753         }
1754         spin_unlock_bh(&queue->rskq_lock);
1755         if (!head)
1756                 return;
1757
1758         /* can't acquire the msk socket lock under the subflow one,
1759          * or will cause ABBA deadlock
1760          */
1761         release_sock(listener_ssk);
1762
1763         for (msk = head; msk; msk = next) {
1764                 sk = (struct sock *)msk;
1765
1766                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1767                 next = msk->dl_next;
1768                 msk->dl_next = NULL;
1769
1770                 __mptcp_unaccepted_force_close(sk);
1771                 release_sock(sk);
1772
1773                 /* lockdep will report a false positive ABBA deadlock
1774                  * between cancel_work_sync and the listener socket.
1775                  * The involved locks belong to different sockets WRT
1776                  * the existing AB chain.
1777                  * Using a per socket key is problematic as key
1778                  * deregistration requires process context and must be
1779                  * performed at socket disposal time, in atomic
1780                  * context.
1781                  * Just tell lockdep to consider the listener socket
1782                  * released here.
1783                  */
1784                 mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_);
1785                 mptcp_cancel_work(sk);
1786                 mutex_acquire(&listener_sk->sk_lock.dep_map, 0, 0, _RET_IP_);
1787
1788                 sock_put(sk);
1789         }
1790
1791         /* we are still under the listener msk socket lock */
1792         lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1793 }
1794
1795 static int subflow_ulp_init(struct sock *sk)
1796 {
1797         struct inet_connection_sock *icsk = inet_csk(sk);
1798         struct mptcp_subflow_context *ctx;
1799         struct tcp_sock *tp = tcp_sk(sk);
1800         int err = 0;
1801
1802         /* disallow attaching ULP to a socket unless it has been
1803          * created with sock_create_kern()
1804          */
1805         if (!sk->sk_kern_sock) {
1806                 err = -EOPNOTSUPP;
1807                 goto out;
1808         }
1809
1810         ctx = subflow_create_ctx(sk, GFP_KERNEL);
1811         if (!ctx) {
1812                 err = -ENOMEM;
1813                 goto out;
1814         }
1815
1816         pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1817
1818         tp->is_mptcp = 1;
1819         ctx->icsk_af_ops = icsk->icsk_af_ops;
1820         icsk->icsk_af_ops = subflow_default_af_ops(sk);
1821         ctx->tcp_state_change = sk->sk_state_change;
1822         ctx->tcp_error_report = sk->sk_error_report;
1823
1824         WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable);
1825         WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space);
1826
1827         sk->sk_data_ready = subflow_data_ready;
1828         sk->sk_write_space = subflow_write_space;
1829         sk->sk_state_change = subflow_state_change;
1830         sk->sk_error_report = subflow_error_report;
1831 out:
1832         return err;
1833 }
1834
1835 static void subflow_ulp_release(struct sock *ssk)
1836 {
1837         struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1838         bool release = true;
1839         struct sock *sk;
1840
1841         if (!ctx)
1842                 return;
1843
1844         sk = ctx->conn;
1845         if (sk) {
1846                 /* if the msk has been orphaned, keep the ctx
1847                  * alive, will be freed by __mptcp_close_ssk(),
1848                  * when the subflow is still unaccepted
1849                  */
1850                 release = ctx->disposable || list_empty(&ctx->node);
1851
1852                 /* inet_child_forget() does not call sk_state_change(),
1853                  * explicitly trigger the socket close machinery
1854                  */
1855                 if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW,
1856                                                   &mptcp_sk(sk)->flags))
1857                         mptcp_schedule_work(sk);
1858                 sock_put(sk);
1859         }
1860
1861         mptcp_subflow_ops_undo_override(ssk);
1862         if (release)
1863                 kfree_rcu(ctx, rcu);
1864 }
1865
1866 static void subflow_ulp_clone(const struct request_sock *req,
1867                               struct sock *newsk,
1868                               const gfp_t priority)
1869 {
1870         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1871         struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1872         struct mptcp_subflow_context *new_ctx;
1873
1874         if (!tcp_rsk(req)->is_mptcp ||
1875             (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1876                 subflow_ulp_fallback(newsk, old_ctx);
1877                 return;
1878         }
1879
1880         new_ctx = subflow_create_ctx(newsk, priority);
1881         if (!new_ctx) {
1882                 subflow_ulp_fallback(newsk, old_ctx);
1883                 return;
1884         }
1885
1886         new_ctx->conn_finished = 1;
1887         new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1888         new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1889         new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1890         new_ctx->rel_write_seq = 1;
1891         new_ctx->tcp_sock = newsk;
1892
1893         if (subflow_req->mp_capable) {
1894                 /* see comments in subflow_syn_recv_sock(), MPTCP connection
1895                  * is fully established only after we receive the remote key
1896                  */
1897                 new_ctx->mp_capable = 1;
1898                 new_ctx->local_key = subflow_req->local_key;
1899                 new_ctx->token = subflow_req->token;
1900                 new_ctx->ssn_offset = subflow_req->ssn_offset;
1901                 new_ctx->idsn = subflow_req->idsn;
1902
1903                 /* this is the first subflow, id is always 0 */
1904                 new_ctx->local_id_valid = 1;
1905         } else if (subflow_req->mp_join) {
1906                 new_ctx->ssn_offset = subflow_req->ssn_offset;
1907                 new_ctx->mp_join = 1;
1908                 new_ctx->fully_established = 1;
1909                 new_ctx->backup = subflow_req->backup;
1910                 new_ctx->remote_id = subflow_req->remote_id;
1911                 new_ctx->token = subflow_req->token;
1912                 new_ctx->thmac = subflow_req->thmac;
1913
1914                 /* the subflow req id is valid, fetched via subflow_check_req()
1915                  * and subflow_token_join_request()
1916                  */
1917                 subflow_set_local_id(new_ctx, subflow_req->local_id);
1918         }
1919 }
1920
1921 static void tcp_release_cb_override(struct sock *ssk)
1922 {
1923         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1924
1925         if (mptcp_subflow_has_delegated_action(subflow))
1926                 mptcp_subflow_process_delegated(ssk);
1927
1928         tcp_release_cb(ssk);
1929 }
1930
1931 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1932         .name           = "mptcp",
1933         .owner          = THIS_MODULE,
1934         .init           = subflow_ulp_init,
1935         .release        = subflow_ulp_release,
1936         .clone          = subflow_ulp_clone,
1937 };
1938
1939 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1940 {
1941         subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1942
1943         subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1944                                               subflow_ops->obj_size, 0,
1945                                               SLAB_ACCOUNT |
1946                                               SLAB_TYPESAFE_BY_RCU,
1947                                               NULL);
1948         if (!subflow_ops->slab)
1949                 return -ENOMEM;
1950
1951         return 0;
1952 }
1953
1954 void __init mptcp_subflow_init(void)
1955 {
1956         mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops;
1957         mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4";
1958         mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor;
1959
1960         if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0)
1961                 panic("MPTCP: failed to init subflow v4 request sock ops\n");
1962
1963         subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1964         subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1965
1966         subflow_specific = ipv4_specific;
1967         subflow_specific.conn_request = subflow_v4_conn_request;
1968         subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1969         subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1970         subflow_specific.rebuild_header = subflow_rebuild_header;
1971
1972         tcp_prot_override = tcp_prot;
1973         tcp_prot_override.release_cb = tcp_release_cb_override;
1974
1975 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1976         /* In struct mptcp_subflow_request_sock, we assume the TCP request sock
1977          * structures for v4 and v6 have the same size. It should not changed in
1978          * the future but better to make sure to be warned if it is no longer
1979          * the case.
1980          */
1981         BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock));
1982
1983         mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops;
1984         mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6";
1985         mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor;
1986
1987         if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0)
1988                 panic("MPTCP: failed to init subflow v6 request sock ops\n");
1989
1990         subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1991         subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1992
1993         subflow_v6_specific = ipv6_specific;
1994         subflow_v6_specific.conn_request = subflow_v6_conn_request;
1995         subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1996         subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1997         subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header;
1998
1999         subflow_v6m_specific = subflow_v6_specific;
2000         subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
2001         subflow_v6m_specific.send_check = ipv4_specific.send_check;
2002         subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
2003         subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
2004         subflow_v6m_specific.net_frag_header_len = 0;
2005         subflow_v6m_specific.rebuild_header = subflow_rebuild_header;
2006
2007         tcpv6_prot_override = tcpv6_prot;
2008         tcpv6_prot_override.release_cb = tcp_release_cb_override;
2009 #endif
2010
2011         mptcp_diag_subflow_init(&subflow_ulp_ops);
2012
2013         if (tcp_register_ulp(&subflow_ulp_ops) != 0)
2014                 panic("MPTCP: failed to register subflows to ULP\n");
2015 }