Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Multipath TCP
0003  *
0004  * Copyright (c) 2017 - 2019, Intel Corporation.
0005  */
0006 
0007 #define pr_fmt(fmt) "MPTCP: " fmt
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/netdevice.h>
0012 #include <crypto/algapi.h>
0013 #include <crypto/sha2.h>
0014 #include <net/sock.h>
0015 #include <net/inet_common.h>
0016 #include <net/inet_hashtables.h>
0017 #include <net/protocol.h>
0018 #include <net/tcp.h>
0019 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
0020 #include <net/ip6_route.h>
0021 #include <net/transp_v6.h>
0022 #endif
0023 #include <net/mptcp.h>
0024 #include <uapi/linux/mptcp.h>
0025 #include "protocol.h"
0026 #include "mib.h"
0027 
0028 #include <trace/events/mptcp.h>
0029 
0030 static void mptcp_subflow_ops_undo_override(struct sock *ssk);
0031 
0032 static void SUBFLOW_REQ_INC_STATS(struct request_sock *req,
0033                   enum linux_mptcp_mib_field field)
0034 {
0035     MPTCP_INC_STATS(sock_net(req_to_sk(req)), field);
0036 }
0037 
0038 static void subflow_req_destructor(struct request_sock *req)
0039 {
0040     struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
0041 
0042     pr_debug("subflow_req=%p", subflow_req);
0043 
0044     if (subflow_req->msk)
0045         sock_put((struct sock *)subflow_req->msk);
0046 
0047     mptcp_token_destroy_request(req);
0048     tcp_request_sock_ops.destructor(req);
0049 }
0050 
0051 static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
0052                   void *hmac)
0053 {
0054     u8 msg[8];
0055 
0056     put_unaligned_be32(nonce1, &msg[0]);
0057     put_unaligned_be32(nonce2, &msg[4]);
0058 
0059     mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
0060 }
0061 
0062 static bool mptcp_can_accept_new_subflow(const struct mptcp_sock *msk)
0063 {
0064     return mptcp_is_fully_established((void *)msk) &&
0065         ((mptcp_pm_is_userspace(msk) &&
0066           mptcp_userspace_pm_active(msk)) ||
0067          READ_ONCE(msk->pm.accept_subflow));
0068 }
0069 
0070 /* validate received token and create truncated hmac and nonce for SYN-ACK */
0071 static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_req)
0072 {
0073     struct mptcp_sock *msk = subflow_req->msk;
0074     u8 hmac[SHA256_DIGEST_SIZE];
0075 
0076     get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
0077 
0078     subflow_generate_hmac(msk->local_key, msk->remote_key,
0079                   subflow_req->local_nonce,
0080                   subflow_req->remote_nonce, hmac);
0081 
0082     subflow_req->thmac = get_unaligned_be64(hmac);
0083 }
0084 
0085 static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
0086 {
0087     struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
0088     struct mptcp_sock *msk;
0089     int local_id;
0090 
0091     msk = mptcp_token_get_sock(sock_net(req_to_sk(req)), subflow_req->token);
0092     if (!msk) {
0093         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINNOTOKEN);
0094         return NULL;
0095     }
0096 
0097     local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
0098     if (local_id < 0) {
0099         sock_put((struct sock *)msk);
0100         return NULL;
0101     }
0102     subflow_req->local_id = local_id;
0103 
0104     return msk;
0105 }
0106 
0107 static void subflow_init_req(struct request_sock *req, const struct sock *sk_listener)
0108 {
0109     struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
0110 
0111     subflow_req->mp_capable = 0;
0112     subflow_req->mp_join = 0;
0113     subflow_req->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk_listener));
0114     subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener));
0115     subflow_req->msk = NULL;
0116     mptcp_token_init_request(req);
0117 }
0118 
0119 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
0120 {
0121     return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
0122 }
0123 
0124 static void subflow_add_reset_reason(struct sk_buff *skb, u8 reason)
0125 {
0126     struct mptcp_ext *mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
0127 
0128     if (mpext) {
0129         memset(mpext, 0, sizeof(*mpext));
0130         mpext->reset_reason = reason;
0131     }
0132 }
0133 
0134 /* Init mptcp request socket.
0135  *
0136  * Returns an error code if a JOIN has failed and a TCP reset
0137  * should be sent.
0138  */
0139 static int subflow_check_req(struct request_sock *req,
0140                  const struct sock *sk_listener,
0141                  struct sk_buff *skb)
0142 {
0143     struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
0144     struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
0145     struct mptcp_options_received mp_opt;
0146     bool opt_mp_capable, opt_mp_join;
0147 
0148     pr_debug("subflow_req=%p, listener=%p", subflow_req, listener);
0149 
0150 #ifdef CONFIG_TCP_MD5SIG
0151     /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
0152      * TCP option space.
0153      */
0154     if (rcu_access_pointer(tcp_sk(sk_listener)->md5sig_info))
0155         return -EINVAL;
0156 #endif
0157 
0158     mptcp_get_options(skb, &mp_opt);
0159 
0160     opt_mp_capable = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPC);
0161     opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ);
0162     if (opt_mp_capable) {
0163         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVE);
0164 
0165         if (opt_mp_join)
0166             return 0;
0167     } else if (opt_mp_join) {
0168         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINSYNRX);
0169     }
0170 
0171     if (opt_mp_capable && listener->request_mptcp) {
0172         int err, retries = MPTCP_TOKEN_MAX_RETRIES;
0173 
0174         subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
0175 again:
0176         do {
0177             get_random_bytes(&subflow_req->local_key, sizeof(subflow_req->local_key));
0178         } while (subflow_req->local_key == 0);
0179 
0180         if (unlikely(req->syncookie)) {
0181             mptcp_crypto_key_sha(subflow_req->local_key,
0182                          &subflow_req->token,
0183                          &subflow_req->idsn);
0184             if (mptcp_token_exists(subflow_req->token)) {
0185                 if (retries-- > 0)
0186                     goto again;
0187                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
0188             } else {
0189                 subflow_req->mp_capable = 1;
0190             }
0191             return 0;
0192         }
0193 
0194         err = mptcp_token_new_request(req);
0195         if (err == 0)
0196             subflow_req->mp_capable = 1;
0197         else if (retries-- > 0)
0198             goto again;
0199         else
0200             SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_TOKENFALLBACKINIT);
0201 
0202     } else if (opt_mp_join && listener->request_mptcp) {
0203         subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
0204         subflow_req->mp_join = 1;
0205         subflow_req->backup = mp_opt.backup;
0206         subflow_req->remote_id = mp_opt.join_id;
0207         subflow_req->token = mp_opt.token;
0208         subflow_req->remote_nonce = mp_opt.nonce;
0209         subflow_req->msk = subflow_token_join_request(req);
0210 
0211         /* Can't fall back to TCP in this case. */
0212         if (!subflow_req->msk) {
0213             subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
0214             return -EPERM;
0215         }
0216 
0217         if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
0218             pr_debug("syn inet_sport=%d %d",
0219                  ntohs(inet_sk(sk_listener)->inet_sport),
0220                  ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
0221             if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
0222                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTSYNRX);
0223                 return -EPERM;
0224             }
0225             SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTSYNRX);
0226         }
0227 
0228         subflow_req_create_thmac(subflow_req);
0229 
0230         if (unlikely(req->syncookie)) {
0231             if (mptcp_can_accept_new_subflow(subflow_req->msk))
0232                 subflow_init_req_cookie_join_save(subflow_req, skb);
0233             else
0234                 return -EPERM;
0235         }
0236 
0237         pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
0238              subflow_req->remote_nonce, subflow_req->msk);
0239     }
0240 
0241     return 0;
0242 }
0243 
0244 int mptcp_subflow_init_cookie_req(struct request_sock *req,
0245                   const struct sock *sk_listener,
0246                   struct sk_buff *skb)
0247 {
0248     struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk_listener);
0249     struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
0250     struct mptcp_options_received mp_opt;
0251     bool opt_mp_capable, opt_mp_join;
0252     int err;
0253 
0254     subflow_init_req(req, sk_listener);
0255     mptcp_get_options(skb, &mp_opt);
0256 
0257     opt_mp_capable = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPC);
0258     opt_mp_join = !!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ);
0259     if (opt_mp_capable && opt_mp_join)
0260         return -EINVAL;
0261 
0262     if (opt_mp_capable && listener->request_mptcp) {
0263         if (mp_opt.sndr_key == 0)
0264             return -EINVAL;
0265 
0266         subflow_req->local_key = mp_opt.rcvr_key;
0267         err = mptcp_token_new_request(req);
0268         if (err)
0269             return err;
0270 
0271         subflow_req->mp_capable = 1;
0272         subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
0273     } else if (opt_mp_join && listener->request_mptcp) {
0274         if (!mptcp_token_join_cookie_init_state(subflow_req, skb))
0275             return -EINVAL;
0276 
0277         subflow_req->mp_join = 1;
0278         subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq - 1;
0279     }
0280 
0281     return 0;
0282 }
0283 EXPORT_SYMBOL_GPL(mptcp_subflow_init_cookie_req);
0284 
0285 static struct dst_entry *subflow_v4_route_req(const struct sock *sk,
0286                           struct sk_buff *skb,
0287                           struct flowi *fl,
0288                           struct request_sock *req)
0289 {
0290     struct dst_entry *dst;
0291     int err;
0292 
0293     tcp_rsk(req)->is_mptcp = 1;
0294     subflow_init_req(req, sk);
0295 
0296     dst = tcp_request_sock_ipv4_ops.route_req(sk, skb, fl, req);
0297     if (!dst)
0298         return NULL;
0299 
0300     err = subflow_check_req(req, sk, skb);
0301     if (err == 0)
0302         return dst;
0303 
0304     dst_release(dst);
0305     if (!req->syncookie)
0306         tcp_request_sock_ops.send_reset(sk, skb);
0307     return NULL;
0308 }
0309 
0310 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
0311 static struct dst_entry *subflow_v6_route_req(const struct sock *sk,
0312                           struct sk_buff *skb,
0313                           struct flowi *fl,
0314                           struct request_sock *req)
0315 {
0316     struct dst_entry *dst;
0317     int err;
0318 
0319     tcp_rsk(req)->is_mptcp = 1;
0320     subflow_init_req(req, sk);
0321 
0322     dst = tcp_request_sock_ipv6_ops.route_req(sk, skb, fl, req);
0323     if (!dst)
0324         return NULL;
0325 
0326     err = subflow_check_req(req, sk, skb);
0327     if (err == 0)
0328         return dst;
0329 
0330     dst_release(dst);
0331     if (!req->syncookie)
0332         tcp6_request_sock_ops.send_reset(sk, skb);
0333     return NULL;
0334 }
0335 #endif
0336 
0337 /* validate received truncated hmac and create hmac for third ACK */
0338 static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
0339 {
0340     u8 hmac[SHA256_DIGEST_SIZE];
0341     u64 thmac;
0342 
0343     subflow_generate_hmac(subflow->remote_key, subflow->local_key,
0344                   subflow->remote_nonce, subflow->local_nonce,
0345                   hmac);
0346 
0347     thmac = get_unaligned_be64(hmac);
0348     pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
0349          subflow, subflow->token, thmac, subflow->thmac);
0350 
0351     return thmac == subflow->thmac;
0352 }
0353 
0354 void mptcp_subflow_reset(struct sock *ssk)
0355 {
0356     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
0357     struct sock *sk = subflow->conn;
0358 
0359     /* must hold: tcp_done() could drop last reference on parent */
0360     sock_hold(sk);
0361 
0362     tcp_set_state(ssk, TCP_CLOSE);
0363     tcp_send_active_reset(ssk, GFP_ATOMIC);
0364     tcp_done(ssk);
0365     if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags) &&
0366         schedule_work(&mptcp_sk(sk)->work))
0367         return; /* worker will put sk for us */
0368 
0369     sock_put(sk);
0370 }
0371 
0372 static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
0373 {
0374     return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
0375 }
0376 
0377 void __mptcp_set_connected(struct sock *sk)
0378 {
0379     if (sk->sk_state == TCP_SYN_SENT) {
0380         inet_sk_state_store(sk, TCP_ESTABLISHED);
0381         sk->sk_state_change(sk);
0382     }
0383 }
0384 
0385 static void mptcp_set_connected(struct sock *sk)
0386 {
0387     mptcp_data_lock(sk);
0388     if (!sock_owned_by_user(sk))
0389         __mptcp_set_connected(sk);
0390     else
0391         __set_bit(MPTCP_CONNECTED, &mptcp_sk(sk)->cb_flags);
0392     mptcp_data_unlock(sk);
0393 }
0394 
0395 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
0396 {
0397     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
0398     struct mptcp_options_received mp_opt;
0399     struct sock *parent = subflow->conn;
0400 
0401     subflow->icsk_af_ops->sk_rx_dst_set(sk, skb);
0402 
0403     /* be sure no special action on any packet other than syn-ack */
0404     if (subflow->conn_finished)
0405         return;
0406 
0407     mptcp_propagate_sndbuf(parent, sk);
0408     subflow->rel_write_seq = 1;
0409     subflow->conn_finished = 1;
0410     subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
0411     pr_debug("subflow=%p synack seq=%x", subflow, subflow->ssn_offset);
0412 
0413     mptcp_get_options(skb, &mp_opt);
0414     if (subflow->request_mptcp) {
0415         if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) {
0416             MPTCP_INC_STATS(sock_net(sk),
0417                     MPTCP_MIB_MPCAPABLEACTIVEFALLBACK);
0418             mptcp_do_fallback(sk);
0419             pr_fallback(mptcp_sk(subflow->conn));
0420             goto fallback;
0421         }
0422 
0423         if (mp_opt.suboptions & OPTION_MPTCP_CSUMREQD)
0424             WRITE_ONCE(mptcp_sk(parent)->csum_enabled, true);
0425         if (mp_opt.deny_join_id0)
0426             WRITE_ONCE(mptcp_sk(parent)->pm.remote_deny_join_id0, true);
0427         subflow->mp_capable = 1;
0428         subflow->can_ack = 1;
0429         subflow->remote_key = mp_opt.sndr_key;
0430         pr_debug("subflow=%p, remote_key=%llu", subflow,
0431              subflow->remote_key);
0432         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEACTIVEACK);
0433         mptcp_finish_connect(sk);
0434         mptcp_set_connected(parent);
0435     } else if (subflow->request_join) {
0436         u8 hmac[SHA256_DIGEST_SIZE];
0437 
0438         if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ)) {
0439             subflow->reset_reason = MPTCP_RST_EMPTCP;
0440             goto do_reset;
0441         }
0442 
0443         subflow->backup = mp_opt.backup;
0444         subflow->thmac = mp_opt.thmac;
0445         subflow->remote_nonce = mp_opt.nonce;
0446         subflow->remote_id = mp_opt.join_id;
0447         pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d",
0448              subflow, subflow->thmac, subflow->remote_nonce,
0449              subflow->backup);
0450 
0451         if (!subflow_thmac_valid(subflow)) {
0452             MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINACKMAC);
0453             subflow->reset_reason = MPTCP_RST_EMPTCP;
0454             goto do_reset;
0455         }
0456 
0457         if (!mptcp_finish_join(sk))
0458             goto do_reset;
0459 
0460         subflow_generate_hmac(subflow->local_key, subflow->remote_key,
0461                       subflow->local_nonce,
0462                       subflow->remote_nonce,
0463                       hmac);
0464         memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
0465 
0466         subflow->mp_join = 1;
0467         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
0468 
0469         if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
0470             pr_debug("synack inet_dport=%d %d",
0471                  ntohs(inet_sk(sk)->inet_dport),
0472                  ntohs(inet_sk(parent)->inet_dport));
0473             MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINPORTSYNACKRX);
0474         }
0475     } else if (mptcp_check_fallback(sk)) {
0476 fallback:
0477         mptcp_rcv_space_init(mptcp_sk(parent), sk);
0478         mptcp_set_connected(parent);
0479     }
0480     return;
0481 
0482 do_reset:
0483     subflow->reset_transient = 0;
0484     mptcp_subflow_reset(sk);
0485 }
0486 
0487 static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id)
0488 {
0489     subflow->local_id = local_id;
0490     subflow->local_id_valid = 1;
0491 }
0492 
0493 static int subflow_chk_local_id(struct sock *sk)
0494 {
0495     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
0496     struct mptcp_sock *msk = mptcp_sk(subflow->conn);
0497     int err;
0498 
0499     if (likely(subflow->local_id_valid))
0500         return 0;
0501 
0502     err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk);
0503     if (err < 0)
0504         return err;
0505 
0506     subflow_set_local_id(subflow, err);
0507     return 0;
0508 }
0509 
0510 static int subflow_rebuild_header(struct sock *sk)
0511 {
0512     int err = subflow_chk_local_id(sk);
0513 
0514     if (unlikely(err < 0))
0515         return err;
0516 
0517     return inet_sk_rebuild_header(sk);
0518 }
0519 
0520 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
0521 static int subflow_v6_rebuild_header(struct sock *sk)
0522 {
0523     int err = subflow_chk_local_id(sk);
0524 
0525     if (unlikely(err < 0))
0526         return err;
0527 
0528     return inet6_sk_rebuild_header(sk);
0529 }
0530 #endif
0531 
0532 struct request_sock_ops mptcp_subflow_request_sock_ops;
0533 static struct tcp_request_sock_ops subflow_request_sock_ipv4_ops __ro_after_init;
0534 
0535 static int subflow_v4_conn_request(struct sock *sk, struct sk_buff *skb)
0536 {
0537     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
0538 
0539     pr_debug("subflow=%p", subflow);
0540 
0541     /* Never answer to SYNs sent to broadcast or multicast */
0542     if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
0543         goto drop;
0544 
0545     return tcp_conn_request(&mptcp_subflow_request_sock_ops,
0546                 &subflow_request_sock_ipv4_ops,
0547                 sk, skb);
0548 drop:
0549     tcp_listendrop(sk);
0550     return 0;
0551 }
0552 
0553 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
0554 static struct tcp_request_sock_ops subflow_request_sock_ipv6_ops __ro_after_init;
0555 static struct inet_connection_sock_af_ops subflow_v6_specific __ro_after_init;
0556 static struct inet_connection_sock_af_ops subflow_v6m_specific __ro_after_init;
0557 static struct proto tcpv6_prot_override;
0558 
0559 static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
0560 {
0561     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
0562 
0563     pr_debug("subflow=%p", subflow);
0564 
0565     if (skb->protocol == htons(ETH_P_IP))
0566         return subflow_v4_conn_request(sk, skb);
0567 
0568     if (!ipv6_unicast_destination(skb))
0569         goto drop;
0570 
0571     if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
0572         __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
0573         return 0;
0574     }
0575 
0576     return tcp_conn_request(&mptcp_subflow_request_sock_ops,
0577                 &subflow_request_sock_ipv6_ops, sk, skb);
0578 
0579 drop:
0580     tcp_listendrop(sk);
0581     return 0; /* don't send reset */
0582 }
0583 #endif
0584 
0585 /* validate hmac received in third ACK */
0586 static bool subflow_hmac_valid(const struct request_sock *req,
0587                    const struct mptcp_options_received *mp_opt)
0588 {
0589     const struct mptcp_subflow_request_sock *subflow_req;
0590     u8 hmac[SHA256_DIGEST_SIZE];
0591     struct mptcp_sock *msk;
0592 
0593     subflow_req = mptcp_subflow_rsk(req);
0594     msk = subflow_req->msk;
0595     if (!msk)
0596         return false;
0597 
0598     subflow_generate_hmac(msk->remote_key, msk->local_key,
0599                   subflow_req->remote_nonce,
0600                   subflow_req->local_nonce, hmac);
0601 
0602     return !crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN);
0603 }
0604 
0605 static void mptcp_force_close(struct sock *sk)
0606 {
0607     /* the msk is not yet exposed to user-space */
0608     inet_sk_state_store(sk, TCP_CLOSE);
0609     sk_common_release(sk);
0610 }
0611 
0612 static void subflow_ulp_fallback(struct sock *sk,
0613                  struct mptcp_subflow_context *old_ctx)
0614 {
0615     struct inet_connection_sock *icsk = inet_csk(sk);
0616 
0617     mptcp_subflow_tcp_fallback(sk, old_ctx);
0618     icsk->icsk_ulp_ops = NULL;
0619     rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
0620     tcp_sk(sk)->is_mptcp = 0;
0621 
0622     mptcp_subflow_ops_undo_override(sk);
0623 }
0624 
0625 static void subflow_drop_ctx(struct sock *ssk)
0626 {
0627     struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
0628 
0629     if (!ctx)
0630         return;
0631 
0632     subflow_ulp_fallback(ssk, ctx);
0633     if (ctx->conn)
0634         sock_put(ctx->conn);
0635 
0636     kfree_rcu(ctx, rcu);
0637 }
0638 
0639 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
0640                      struct mptcp_options_received *mp_opt)
0641 {
0642     struct mptcp_sock *msk = mptcp_sk(subflow->conn);
0643 
0644     subflow->remote_key = mp_opt->sndr_key;
0645     subflow->fully_established = 1;
0646     subflow->can_ack = 1;
0647     WRITE_ONCE(msk->fully_established, true);
0648 }
0649 
0650 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
0651                       struct sk_buff *skb,
0652                       struct request_sock *req,
0653                       struct dst_entry *dst,
0654                       struct request_sock *req_unhash,
0655                       bool *own_req)
0656 {
0657     struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
0658     struct mptcp_subflow_request_sock *subflow_req;
0659     struct mptcp_options_received mp_opt;
0660     bool fallback, fallback_is_fatal;
0661     struct sock *new_msk = NULL;
0662     struct sock *child;
0663 
0664     pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
0665 
0666     /* After child creation we must look for MPC even when options
0667      * are not parsed
0668      */
0669     mp_opt.suboptions = 0;
0670 
0671     /* hopefully temporary handling for MP_JOIN+syncookie */
0672     subflow_req = mptcp_subflow_rsk(req);
0673     fallback_is_fatal = tcp_rsk(req)->is_mptcp && subflow_req->mp_join;
0674     fallback = !tcp_rsk(req)->is_mptcp;
0675     if (fallback)
0676         goto create_child;
0677 
0678     /* if the sk is MP_CAPABLE, we try to fetch the client key */
0679     if (subflow_req->mp_capable) {
0680         /* we can receive and accept an in-window, out-of-order pkt,
0681          * which may not carry the MP_CAPABLE opt even on mptcp enabled
0682          * paths: always try to extract the peer key, and fallback
0683          * for packets missing it.
0684          * Even OoO DSS packets coming legitly after dropped or
0685          * reordered MPC will cause fallback, but we don't have other
0686          * options.
0687          */
0688         mptcp_get_options(skb, &mp_opt);
0689         if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPC)) {
0690             fallback = true;
0691             goto create_child;
0692         }
0693 
0694         new_msk = mptcp_sk_clone(listener->conn, &mp_opt, req);
0695         if (!new_msk)
0696             fallback = true;
0697     } else if (subflow_req->mp_join) {
0698         mptcp_get_options(skb, &mp_opt);
0699         if (!(mp_opt.suboptions & OPTIONS_MPTCP_MPJ) ||
0700             !subflow_hmac_valid(req, &mp_opt) ||
0701             !mptcp_can_accept_new_subflow(subflow_req->msk)) {
0702             SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
0703             fallback = true;
0704         }
0705     }
0706 
0707 create_child:
0708     child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
0709                              req_unhash, own_req);
0710 
0711     if (child && *own_req) {
0712         struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
0713 
0714         tcp_rsk(req)->drop_req = false;
0715 
0716         /* we need to fallback on ctx allocation failure and on pre-reqs
0717          * checking above. In the latter scenario we additionally need
0718          * to reset the context to non MPTCP status.
0719          */
0720         if (!ctx || fallback) {
0721             if (fallback_is_fatal) {
0722                 subflow_add_reset_reason(skb, MPTCP_RST_EMPTCP);
0723                 goto dispose_child;
0724             }
0725 
0726             subflow_drop_ctx(child);
0727             goto out;
0728         }
0729 
0730         /* ssk inherits options of listener sk */
0731         ctx->setsockopt_seq = listener->setsockopt_seq;
0732 
0733         if (ctx->mp_capable) {
0734             /* this can't race with mptcp_close(), as the msk is
0735              * not yet exposted to user-space
0736              */
0737             inet_sk_state_store((void *)new_msk, TCP_ESTABLISHED);
0738 
0739             /* record the newly created socket as the first msk
0740              * subflow, but don't link it yet into conn_list
0741              */
0742             WRITE_ONCE(mptcp_sk(new_msk)->first, child);
0743 
0744             /* new mpc subflow takes ownership of the newly
0745              * created mptcp socket
0746              */
0747             mptcp_sk(new_msk)->setsockopt_seq = ctx->setsockopt_seq;
0748             mptcp_pm_new_connection(mptcp_sk(new_msk), child, 1);
0749             mptcp_token_accept(subflow_req, mptcp_sk(new_msk));
0750             ctx->conn = new_msk;
0751             new_msk = NULL;
0752 
0753             /* with OoO packets we can reach here without ingress
0754              * mpc option
0755              */
0756             if (mp_opt.suboptions & OPTIONS_MPTCP_MPC)
0757                 mptcp_subflow_fully_established(ctx, &mp_opt);
0758         } else if (ctx->mp_join) {
0759             struct mptcp_sock *owner;
0760 
0761             owner = subflow_req->msk;
0762             if (!owner) {
0763                 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
0764                 goto dispose_child;
0765             }
0766 
0767             /* move the msk reference ownership to the subflow */
0768             subflow_req->msk = NULL;
0769             ctx->conn = (struct sock *)owner;
0770 
0771             if (subflow_use_different_sport(owner, sk)) {
0772                 pr_debug("ack inet_sport=%d %d",
0773                      ntohs(inet_sk(sk)->inet_sport),
0774                      ntohs(inet_sk((struct sock *)owner)->inet_sport));
0775                 if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
0776                     SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
0777                     goto dispose_child;
0778                 }
0779                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
0780             }
0781 
0782             if (!mptcp_finish_join(child))
0783                 goto dispose_child;
0784 
0785             SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
0786             tcp_rsk(req)->drop_req = true;
0787         }
0788     }
0789 
0790 out:
0791     /* dispose of the left over mptcp master, if any */
0792     if (unlikely(new_msk))
0793         mptcp_force_close(new_msk);
0794 
0795     /* check for expected invariant - should never trigger, just help
0796      * catching eariler subtle bugs
0797      */
0798     WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
0799              (!mptcp_subflow_ctx(child) ||
0800               !mptcp_subflow_ctx(child)->conn));
0801     return child;
0802 
0803 dispose_child:
0804     subflow_drop_ctx(child);
0805     tcp_rsk(req)->drop_req = true;
0806     inet_csk_prepare_for_destroy_sock(child);
0807     tcp_done(child);
0808     req->rsk_ops->send_reset(sk, skb);
0809 
0810     /* The last child reference will be released by the caller */
0811     return child;
0812 }
0813 
0814 static struct inet_connection_sock_af_ops subflow_specific __ro_after_init;
0815 static struct proto tcp_prot_override;
0816 
0817 enum mapping_status {
0818     MAPPING_OK,
0819     MAPPING_INVALID,
0820     MAPPING_EMPTY,
0821     MAPPING_DATA_FIN,
0822     MAPPING_DUMMY,
0823     MAPPING_BAD_CSUM
0824 };
0825 
0826 static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
0827 {
0828     pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d",
0829          ssn, subflow->map_subflow_seq, subflow->map_data_len);
0830 }
0831 
0832 static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
0833 {
0834     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
0835     unsigned int skb_consumed;
0836 
0837     skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
0838     if (WARN_ON_ONCE(skb_consumed >= skb->len))
0839         return true;
0840 
0841     return skb->len - skb_consumed <= subflow->map_data_len -
0842                       mptcp_subflow_get_map_offset(subflow);
0843 }
0844 
0845 static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
0846 {
0847     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
0848     u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
0849 
0850     if (unlikely(before(ssn, subflow->map_subflow_seq))) {
0851         /* Mapping covers data later in the subflow stream,
0852          * currently unsupported.
0853          */
0854         dbg_bad_map(subflow, ssn);
0855         return false;
0856     }
0857     if (unlikely(!before(ssn, subflow->map_subflow_seq +
0858                   subflow->map_data_len))) {
0859         /* Mapping does covers past subflow data, invalid */
0860         dbg_bad_map(subflow, ssn);
0861         return false;
0862     }
0863     return true;
0864 }
0865 
0866 static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
0867                           bool csum_reqd)
0868 {
0869     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
0870     u32 offset, seq, delta;
0871     __sum16 csum;
0872     int len;
0873 
0874     if (!csum_reqd)
0875         return MAPPING_OK;
0876 
0877     /* mapping already validated on previous traversal */
0878     if (subflow->map_csum_len == subflow->map_data_len)
0879         return MAPPING_OK;
0880 
0881     /* traverse the receive queue, ensuring it contains a full
0882      * DSS mapping and accumulating the related csum.
0883      * Preserve the accoumlate csum across multiple calls, to compute
0884      * the csum only once
0885      */
0886     delta = subflow->map_data_len - subflow->map_csum_len;
0887     for (;;) {
0888         seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
0889         offset = seq - TCP_SKB_CB(skb)->seq;
0890 
0891         /* if the current skb has not been accounted yet, csum its contents
0892          * up to the amount covered by the current DSS
0893          */
0894         if (offset < skb->len) {
0895             __wsum csum;
0896 
0897             len = min(skb->len - offset, delta);
0898             csum = skb_checksum(skb, offset, len, 0);
0899             subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
0900                                 subflow->map_csum_len);
0901 
0902             delta -= len;
0903             subflow->map_csum_len += len;
0904         }
0905         if (delta == 0)
0906             break;
0907 
0908         if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
0909             /* if this subflow is closed, the partial mapping
0910              * will be never completed; flush the pending skbs, so
0911              * that subflow_sched_work_if_closed() can kick in
0912              */
0913             if (unlikely(ssk->sk_state == TCP_CLOSE))
0914                 while ((skb = skb_peek(&ssk->sk_receive_queue)))
0915                     sk_eat_skb(ssk, skb);
0916 
0917             /* not enough data to validate the csum */
0918             return MAPPING_EMPTY;
0919         }
0920 
0921         /* the DSS mapping for next skbs will be validated later,
0922          * when a get_mapping_status call will process such skb
0923          */
0924         skb = skb->next;
0925     }
0926 
0927     /* note that 'map_data_len' accounts only for the carried data, does
0928      * not include the eventual seq increment due to the data fin,
0929      * while the pseudo header requires the original DSS data len,
0930      * including that
0931      */
0932     csum = __mptcp_make_csum(subflow->map_seq,
0933                  subflow->map_subflow_seq,
0934                  subflow->map_data_len + subflow->map_data_fin,
0935                  subflow->map_data_csum);
0936     if (unlikely(csum)) {
0937         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
0938         return MAPPING_BAD_CSUM;
0939     }
0940 
0941     subflow->valid_csum_seen = 1;
0942     return MAPPING_OK;
0943 }
0944 
0945 static enum mapping_status get_mapping_status(struct sock *ssk,
0946                           struct mptcp_sock *msk)
0947 {
0948     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
0949     bool csum_reqd = READ_ONCE(msk->csum_enabled);
0950     struct mptcp_ext *mpext;
0951     struct sk_buff *skb;
0952     u16 data_len;
0953     u64 map_seq;
0954 
0955     skb = skb_peek(&ssk->sk_receive_queue);
0956     if (!skb)
0957         return MAPPING_EMPTY;
0958 
0959     if (mptcp_check_fallback(ssk))
0960         return MAPPING_DUMMY;
0961 
0962     mpext = mptcp_get_ext(skb);
0963     if (!mpext || !mpext->use_map) {
0964         if (!subflow->map_valid && !skb->len) {
0965             /* the TCP stack deliver 0 len FIN pkt to the receive
0966              * queue, that is the only 0len pkts ever expected here,
0967              * and we can admit no mapping only for 0 len pkts
0968              */
0969             if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
0970                 WARN_ONCE(1, "0len seq %d:%d flags %x",
0971                       TCP_SKB_CB(skb)->seq,
0972                       TCP_SKB_CB(skb)->end_seq,
0973                       TCP_SKB_CB(skb)->tcp_flags);
0974             sk_eat_skb(ssk, skb);
0975             return MAPPING_EMPTY;
0976         }
0977 
0978         if (!subflow->map_valid)
0979             return MAPPING_INVALID;
0980 
0981         goto validate_seq;
0982     }
0983 
0984     trace_get_mapping_status(mpext);
0985 
0986     data_len = mpext->data_len;
0987     if (data_len == 0) {
0988         pr_debug("infinite mapping received");
0989         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
0990         subflow->map_data_len = 0;
0991         return MAPPING_INVALID;
0992     }
0993 
0994     if (mpext->data_fin == 1) {
0995         if (data_len == 1) {
0996             bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
0997                                  mpext->dsn64);
0998             pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
0999             if (subflow->map_valid) {
1000                 /* A DATA_FIN might arrive in a DSS
1001                  * option before the previous mapping
1002                  * has been fully consumed. Continue
1003                  * handling the existing mapping.
1004                  */
1005                 skb_ext_del(skb, SKB_EXT_MPTCP);
1006                 return MAPPING_OK;
1007             } else {
1008                 if (updated && schedule_work(&msk->work))
1009                     sock_hold((struct sock *)msk);
1010 
1011                 return MAPPING_DATA_FIN;
1012             }
1013         } else {
1014             u64 data_fin_seq = mpext->data_seq + data_len - 1;
1015 
1016             /* If mpext->data_seq is a 32-bit value, data_fin_seq
1017              * must also be limited to 32 bits.
1018              */
1019             if (!mpext->dsn64)
1020                 data_fin_seq &= GENMASK_ULL(31, 0);
1021 
1022             mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
1023             pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d",
1024                  data_fin_seq, mpext->dsn64);
1025         }
1026 
1027         /* Adjust for DATA_FIN using 1 byte of sequence space */
1028         data_len--;
1029     }
1030 
1031     map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
1032     WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
1033 
1034     if (subflow->map_valid) {
1035         /* Allow replacing only with an identical map */
1036         if (subflow->map_seq == map_seq &&
1037             subflow->map_subflow_seq == mpext->subflow_seq &&
1038             subflow->map_data_len == data_len &&
1039             subflow->map_csum_reqd == mpext->csum_reqd) {
1040             skb_ext_del(skb, SKB_EXT_MPTCP);
1041             goto validate_csum;
1042         }
1043 
1044         /* If this skb data are fully covered by the current mapping,
1045          * the new map would need caching, which is not supported
1046          */
1047         if (skb_is_fully_mapped(ssk, skb)) {
1048             MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
1049             return MAPPING_INVALID;
1050         }
1051 
1052         /* will validate the next map after consuming the current one */
1053         goto validate_csum;
1054     }
1055 
1056     subflow->map_seq = map_seq;
1057     subflow->map_subflow_seq = mpext->subflow_seq;
1058     subflow->map_data_len = data_len;
1059     subflow->map_valid = 1;
1060     subflow->map_data_fin = mpext->data_fin;
1061     subflow->mpc_map = mpext->mpc_map;
1062     subflow->map_csum_reqd = mpext->csum_reqd;
1063     subflow->map_csum_len = 0;
1064     subflow->map_data_csum = csum_unfold(mpext->csum);
1065 
1066     /* Cfr RFC 8684 Section 3.3.0 */
1067     if (unlikely(subflow->map_csum_reqd != csum_reqd))
1068         return MAPPING_INVALID;
1069 
1070     pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
1071          subflow->map_seq, subflow->map_subflow_seq,
1072          subflow->map_data_len, subflow->map_csum_reqd,
1073          subflow->map_data_csum);
1074 
1075 validate_seq:
1076     /* we revalidate valid mapping on new skb, because we must ensure
1077      * the current skb is completely covered by the available mapping
1078      */
1079     if (!validate_mapping(ssk, skb)) {
1080         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
1081         return MAPPING_INVALID;
1082     }
1083 
1084     skb_ext_del(skb, SKB_EXT_MPTCP);
1085 
1086 validate_csum:
1087     return validate_data_csum(ssk, skb, csum_reqd);
1088 }
1089 
1090 static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
1091                        u64 limit)
1092 {
1093     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1094     bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
1095     u32 incr;
1096 
1097     incr = limit >= skb->len ? skb->len + fin : limit;
1098 
1099     pr_debug("discarding=%d len=%d seq=%d", incr, skb->len,
1100          subflow->map_subflow_seq);
1101     MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
1102     tcp_sk(ssk)->copied_seq += incr;
1103     if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
1104         sk_eat_skb(ssk, skb);
1105     if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
1106         subflow->map_valid = 0;
1107 }
1108 
1109 /* sched mptcp worker to remove the subflow if no more data is pending */
1110 static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
1111 {
1112     struct sock *sk = (struct sock *)msk;
1113 
1114     if (likely(ssk->sk_state != TCP_CLOSE))
1115         return;
1116 
1117     if (skb_queue_empty(&ssk->sk_receive_queue) &&
1118         !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) {
1119         sock_hold(sk);
1120         if (!schedule_work(&msk->work))
1121             sock_put(sk);
1122     }
1123 }
1124 
1125 static bool subflow_can_fallback(struct mptcp_subflow_context *subflow)
1126 {
1127     struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1128 
1129     if (subflow->mp_join)
1130         return false;
1131     else if (READ_ONCE(msk->csum_enabled))
1132         return !subflow->valid_csum_seen;
1133     else
1134         return !subflow->fully_established;
1135 }
1136 
1137 static void mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk)
1138 {
1139     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1140     unsigned long fail_tout;
1141 
1142     /* greceful failure can happen only on the MPC subflow */
1143     if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first)))
1144         return;
1145 
1146     /* since the close timeout take precedence on the fail one,
1147      * no need to start the latter when the first is already set
1148      */
1149     if (sock_flag((struct sock *)msk, SOCK_DEAD))
1150         return;
1151 
1152     /* we don't need extreme accuracy here, use a zero fail_tout as special
1153      * value meaning no fail timeout at all;
1154      */
1155     fail_tout = jiffies + TCP_RTO_MAX;
1156     if (!fail_tout)
1157         fail_tout = 1;
1158     WRITE_ONCE(subflow->fail_tout, fail_tout);
1159     tcp_send_ack(ssk);
1160 
1161     mptcp_reset_timeout(msk, subflow->fail_tout);
1162 }
1163 
1164 static bool subflow_check_data_avail(struct sock *ssk)
1165 {
1166     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1167     enum mapping_status status;
1168     struct mptcp_sock *msk;
1169     struct sk_buff *skb;
1170 
1171     if (!skb_peek(&ssk->sk_receive_queue))
1172         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1173     if (subflow->data_avail)
1174         return true;
1175 
1176     msk = mptcp_sk(subflow->conn);
1177     for (;;) {
1178         u64 ack_seq;
1179         u64 old_ack;
1180 
1181         status = get_mapping_status(ssk, msk);
1182         trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
1183         if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY ||
1184                  status == MAPPING_BAD_CSUM))
1185             goto fallback;
1186 
1187         if (status != MAPPING_OK)
1188             goto no_data;
1189 
1190         skb = skb_peek(&ssk->sk_receive_queue);
1191         if (WARN_ON_ONCE(!skb))
1192             goto no_data;
1193 
1194         /* if msk lacks the remote key, this subflow must provide an
1195          * MP_CAPABLE-based mapping
1196          */
1197         if (unlikely(!READ_ONCE(msk->can_ack))) {
1198             if (!subflow->mpc_map)
1199                 goto fallback;
1200             WRITE_ONCE(msk->remote_key, subflow->remote_key);
1201             WRITE_ONCE(msk->ack_seq, subflow->map_seq);
1202             WRITE_ONCE(msk->can_ack, true);
1203         }
1204 
1205         old_ack = READ_ONCE(msk->ack_seq);
1206         ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
1207         pr_debug("msk ack_seq=%llx subflow ack_seq=%llx", old_ack,
1208              ack_seq);
1209         if (unlikely(before64(ack_seq, old_ack))) {
1210             mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
1211             continue;
1212         }
1213 
1214         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1215         break;
1216     }
1217     return true;
1218 
1219 no_data:
1220     subflow_sched_work_if_closed(msk, ssk);
1221     return false;
1222 
1223 fallback:
1224     if (!__mptcp_check_fallback(msk)) {
1225         /* RFC 8684 section 3.7. */
1226         if (status == MAPPING_BAD_CSUM &&
1227             (subflow->mp_join || subflow->valid_csum_seen)) {
1228             subflow->send_mp_fail = 1;
1229 
1230             if (!READ_ONCE(msk->allow_infinite_fallback)) {
1231                 subflow->reset_transient = 0;
1232                 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
1233                 goto reset;
1234             }
1235             mptcp_subflow_fail(msk, ssk);
1236             WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1237             return true;
1238         }
1239 
1240         if (!subflow_can_fallback(subflow) && subflow->map_data_len) {
1241             /* fatal protocol error, close the socket.
1242              * subflow_error_report() will introduce the appropriate barriers
1243              */
1244             subflow->reset_transient = 0;
1245             subflow->reset_reason = MPTCP_RST_EMPTCP;
1246 
1247 reset:
1248             ssk->sk_err = EBADMSG;
1249             tcp_set_state(ssk, TCP_CLOSE);
1250             while ((skb = skb_peek(&ssk->sk_receive_queue)))
1251                 sk_eat_skb(ssk, skb);
1252             tcp_send_active_reset(ssk, GFP_ATOMIC);
1253             WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1254             return false;
1255         }
1256 
1257         mptcp_do_fallback(ssk);
1258     }
1259 
1260     skb = skb_peek(&ssk->sk_receive_queue);
1261     subflow->map_valid = 1;
1262     subflow->map_seq = READ_ONCE(msk->ack_seq);
1263     subflow->map_data_len = skb->len;
1264     subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
1265     WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_DATA_AVAIL);
1266     return true;
1267 }
1268 
1269 bool mptcp_subflow_data_available(struct sock *sk)
1270 {
1271     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1272 
1273     /* check if current mapping is still valid */
1274     if (subflow->map_valid &&
1275         mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
1276         subflow->map_valid = 0;
1277         WRITE_ONCE(subflow->data_avail, MPTCP_SUBFLOW_NODATA);
1278 
1279         pr_debug("Done with mapping: seq=%u data_len=%u",
1280              subflow->map_subflow_seq,
1281              subflow->map_data_len);
1282     }
1283 
1284     return subflow_check_data_avail(sk);
1285 }
1286 
1287 /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
1288  * not the ssk one.
1289  *
1290  * In mptcp, rwin is about the mptcp-level connection data.
1291  *
1292  * Data that is still on the ssk rx queue can thus be ignored,
1293  * as far as mptcp peer is concerned that data is still inflight.
1294  * DSS ACK is updated when skb is moved to the mptcp rx queue.
1295  */
1296 void mptcp_space(const struct sock *ssk, int *space, int *full_space)
1297 {
1298     const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1299     const struct sock *sk = subflow->conn;
1300 
1301     *space = __mptcp_space(sk);
1302     *full_space = tcp_full_space(sk);
1303 }
1304 
1305 void __mptcp_error_report(struct sock *sk)
1306 {
1307     struct mptcp_subflow_context *subflow;
1308     struct mptcp_sock *msk = mptcp_sk(sk);
1309 
1310     mptcp_for_each_subflow(msk, subflow) {
1311         struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1312         int err = sock_error(ssk);
1313 
1314         if (!err)
1315             continue;
1316 
1317         /* only propagate errors on fallen-back sockets or
1318          * on MPC connect
1319          */
1320         if (sk->sk_state != TCP_SYN_SENT && !__mptcp_check_fallback(msk))
1321             continue;
1322 
1323         inet_sk_state_store(sk, inet_sk_state_load(ssk));
1324         sk->sk_err = -err;
1325 
1326         /* This barrier is coupled with smp_rmb() in mptcp_poll() */
1327         smp_wmb();
1328         sk_error_report(sk);
1329         break;
1330     }
1331 }
1332 
1333 static void subflow_error_report(struct sock *ssk)
1334 {
1335     struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1336 
1337     mptcp_data_lock(sk);
1338     if (!sock_owned_by_user(sk))
1339         __mptcp_error_report(sk);
1340     else
1341         __set_bit(MPTCP_ERROR_REPORT,  &mptcp_sk(sk)->cb_flags);
1342     mptcp_data_unlock(sk);
1343 }
1344 
1345 static void subflow_data_ready(struct sock *sk)
1346 {
1347     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1348     u16 state = 1 << inet_sk_state_load(sk);
1349     struct sock *parent = subflow->conn;
1350     struct mptcp_sock *msk;
1351 
1352     msk = mptcp_sk(parent);
1353     if (state & TCPF_LISTEN) {
1354         /* MPJ subflow are removed from accept queue before reaching here,
1355          * avoid stray wakeups
1356          */
1357         if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
1358             return;
1359 
1360         parent->sk_data_ready(parent);
1361         return;
1362     }
1363 
1364     WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
1365              !subflow->mp_join && !(state & TCPF_CLOSE));
1366 
1367     if (mptcp_subflow_data_available(sk))
1368         mptcp_data_ready(parent, sk);
1369     else if (unlikely(sk->sk_err))
1370         subflow_error_report(sk);
1371 }
1372 
1373 static void subflow_write_space(struct sock *ssk)
1374 {
1375     struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
1376 
1377     mptcp_propagate_sndbuf(sk, ssk);
1378     mptcp_write_space(sk);
1379 }
1380 
1381 static const struct inet_connection_sock_af_ops *
1382 subflow_default_af_ops(struct sock *sk)
1383 {
1384 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1385     if (sk->sk_family == AF_INET6)
1386         return &subflow_v6_specific;
1387 #endif
1388     return &subflow_specific;
1389 }
1390 
1391 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1392 void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
1393 {
1394     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1395     struct inet_connection_sock *icsk = inet_csk(sk);
1396     const struct inet_connection_sock_af_ops *target;
1397 
1398     target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
1399 
1400     pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d",
1401          subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
1402 
1403     if (likely(icsk->icsk_af_ops == target))
1404         return;
1405 
1406     subflow->icsk_af_ops = icsk->icsk_af_ops;
1407     icsk->icsk_af_ops = target;
1408 }
1409 #endif
1410 
1411 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
1412              struct sockaddr_storage *addr,
1413              unsigned short family)
1414 {
1415     memset(addr, 0, sizeof(*addr));
1416     addr->ss_family = family;
1417     if (addr->ss_family == AF_INET) {
1418         struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
1419 
1420         if (info->family == AF_INET)
1421             in_addr->sin_addr = info->addr;
1422 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1423         else if (ipv6_addr_v4mapped(&info->addr6))
1424             in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
1425 #endif
1426         in_addr->sin_port = info->port;
1427     }
1428 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1429     else if (addr->ss_family == AF_INET6) {
1430         struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
1431 
1432         if (info->family == AF_INET)
1433             ipv6_addr_set_v4mapped(info->addr.s_addr,
1434                            &in6_addr->sin6_addr);
1435         else
1436             in6_addr->sin6_addr = info->addr6;
1437         in6_addr->sin6_port = info->port;
1438     }
1439 #endif
1440 }
1441 
1442 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
1443                 const struct mptcp_addr_info *remote)
1444 {
1445     struct mptcp_sock *msk = mptcp_sk(sk);
1446     struct mptcp_subflow_context *subflow;
1447     struct sockaddr_storage addr;
1448     int remote_id = remote->id;
1449     int local_id = loc->id;
1450     int err = -ENOTCONN;
1451     struct socket *sf;
1452     struct sock *ssk;
1453     u32 remote_token;
1454     int addrlen;
1455     int ifindex;
1456     u8 flags;
1457 
1458     if (!mptcp_is_fully_established(sk))
1459         goto err_out;
1460 
1461     err = mptcp_subflow_create_socket(sk, &sf);
1462     if (err)
1463         goto err_out;
1464 
1465     ssk = sf->sk;
1466     subflow = mptcp_subflow_ctx(ssk);
1467     do {
1468         get_random_bytes(&subflow->local_nonce, sizeof(u32));
1469     } while (!subflow->local_nonce);
1470 
1471     if (local_id)
1472         subflow_set_local_id(subflow, local_id);
1473 
1474     mptcp_pm_get_flags_and_ifindex_by_id(msk, local_id,
1475                          &flags, &ifindex);
1476     subflow->remote_key = msk->remote_key;
1477     subflow->local_key = msk->local_key;
1478     subflow->token = msk->token;
1479     mptcp_info2sockaddr(loc, &addr, ssk->sk_family);
1480 
1481     addrlen = sizeof(struct sockaddr_in);
1482 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1483     if (addr.ss_family == AF_INET6)
1484         addrlen = sizeof(struct sockaddr_in6);
1485 #endif
1486     mptcp_sockopt_sync(msk, ssk);
1487 
1488     ssk->sk_bound_dev_if = ifindex;
1489     err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
1490     if (err)
1491         goto failed;
1492 
1493     mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
1494     pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d", msk,
1495          remote_token, local_id, remote_id);
1496     subflow->remote_token = remote_token;
1497     subflow->remote_id = remote_id;
1498     subflow->request_join = 1;
1499     subflow->request_bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP);
1500     mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
1501 
1502     sock_hold(ssk);
1503     list_add_tail(&subflow->node, &msk->conn_list);
1504     err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
1505     if (err && err != -EINPROGRESS)
1506         goto failed_unlink;
1507 
1508     /* discard the subflow socket */
1509     mptcp_sock_graft(ssk, sk->sk_socket);
1510     iput(SOCK_INODE(sf));
1511     WRITE_ONCE(msk->allow_infinite_fallback, false);
1512     return 0;
1513 
1514 failed_unlink:
1515     list_del(&subflow->node);
1516     sock_put(mptcp_subflow_tcp_sock(subflow));
1517 
1518 failed:
1519     subflow->disposable = 1;
1520     sock_release(sf);
1521 
1522 err_out:
1523     /* we account subflows before the creation, and this failures will not
1524      * be caught by sk_state_change()
1525      */
1526     mptcp_pm_close_subflow(msk);
1527     return err;
1528 }
1529 
1530 static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
1531 {
1532 #ifdef CONFIG_SOCK_CGROUP_DATA
1533     struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
1534                 *child_skcd = &child->sk_cgrp_data;
1535 
1536     /* only the additional subflows created by kworkers have to be modified */
1537     if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
1538         cgroup_id(sock_cgroup_ptr(child_skcd))) {
1539 #ifdef CONFIG_MEMCG
1540         struct mem_cgroup *memcg = parent->sk_memcg;
1541 
1542         mem_cgroup_sk_free(child);
1543         if (memcg && css_tryget(&memcg->css))
1544             child->sk_memcg = memcg;
1545 #endif /* CONFIG_MEMCG */
1546 
1547         cgroup_sk_free(child_skcd);
1548         *child_skcd = *parent_skcd;
1549         cgroup_sk_clone(child_skcd);
1550     }
1551 #endif /* CONFIG_SOCK_CGROUP_DATA */
1552 }
1553 
1554 static void mptcp_subflow_ops_override(struct sock *ssk)
1555 {
1556 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1557     if (ssk->sk_prot == &tcpv6_prot)
1558         ssk->sk_prot = &tcpv6_prot_override;
1559     else
1560 #endif
1561         ssk->sk_prot = &tcp_prot_override;
1562 }
1563 
1564 static void mptcp_subflow_ops_undo_override(struct sock *ssk)
1565 {
1566 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1567     if (ssk->sk_prot == &tcpv6_prot_override)
1568         ssk->sk_prot = &tcpv6_prot;
1569     else
1570 #endif
1571         ssk->sk_prot = &tcp_prot;
1572 }
1573 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)
1574 {
1575     struct mptcp_subflow_context *subflow;
1576     struct net *net = sock_net(sk);
1577     struct socket *sf;
1578     int err;
1579 
1580     /* un-accepted server sockets can reach here - on bad configuration
1581      * bail early to avoid greater trouble later
1582      */
1583     if (unlikely(!sk->sk_socket))
1584         return -EINVAL;
1585 
1586     err = sock_create_kern(net, sk->sk_family, SOCK_STREAM, IPPROTO_TCP,
1587                    &sf);
1588     if (err)
1589         return err;
1590 
1591     lock_sock(sf->sk);
1592 
1593     /* the newly created socket has to be in the same cgroup as its parent */
1594     mptcp_attach_cgroup(sk, sf->sk);
1595 
1596     /* kernel sockets do not by default acquire net ref, but TCP timer
1597      * needs it.
1598      */
1599     sf->sk->sk_net_refcnt = 1;
1600     get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL);
1601     sock_inuse_add(net, 1);
1602     err = tcp_set_ulp(sf->sk, "mptcp");
1603     release_sock(sf->sk);
1604 
1605     if (err) {
1606         sock_release(sf);
1607         return err;
1608     }
1609 
1610     /* the newly created socket really belongs to the owning MPTCP master
1611      * socket, even if for additional subflows the allocation is performed
1612      * by a kernel workqueue. Adjust inode references, so that the
1613      * procfs/diag interfaces really show this one belonging to the correct
1614      * user.
1615      */
1616     SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
1617     SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
1618     SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
1619 
1620     subflow = mptcp_subflow_ctx(sf->sk);
1621     pr_debug("subflow=%p", subflow);
1622 
1623     *new_sock = sf;
1624     sock_hold(sk);
1625     subflow->conn = sk;
1626     mptcp_subflow_ops_override(sf->sk);
1627 
1628     return 0;
1629 }
1630 
1631 static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
1632                             gfp_t priority)
1633 {
1634     struct inet_connection_sock *icsk = inet_csk(sk);
1635     struct mptcp_subflow_context *ctx;
1636 
1637     ctx = kzalloc(sizeof(*ctx), priority);
1638     if (!ctx)
1639         return NULL;
1640 
1641     rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
1642     INIT_LIST_HEAD(&ctx->node);
1643     INIT_LIST_HEAD(&ctx->delegated_node);
1644 
1645     pr_debug("subflow=%p", ctx);
1646 
1647     ctx->tcp_sock = sk;
1648 
1649     return ctx;
1650 }
1651 
1652 static void __subflow_state_change(struct sock *sk)
1653 {
1654     struct socket_wq *wq;
1655 
1656     rcu_read_lock();
1657     wq = rcu_dereference(sk->sk_wq);
1658     if (skwq_has_sleeper(wq))
1659         wake_up_interruptible_all(&wq->wait);
1660     rcu_read_unlock();
1661 }
1662 
1663 static bool subflow_is_done(const struct sock *sk)
1664 {
1665     return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
1666 }
1667 
1668 static void subflow_state_change(struct sock *sk)
1669 {
1670     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1671     struct sock *parent = subflow->conn;
1672 
1673     __subflow_state_change(sk);
1674 
1675     if (subflow_simultaneous_connect(sk)) {
1676         mptcp_propagate_sndbuf(parent, sk);
1677         mptcp_do_fallback(sk);
1678         mptcp_rcv_space_init(mptcp_sk(parent), sk);
1679         pr_fallback(mptcp_sk(parent));
1680         subflow->conn_finished = 1;
1681         mptcp_set_connected(parent);
1682     }
1683 
1684     /* as recvmsg() does not acquire the subflow socket for ssk selection
1685      * a fin packet carrying a DSS can be unnoticed if we don't trigger
1686      * the data available machinery here.
1687      */
1688     if (mptcp_subflow_data_available(sk))
1689         mptcp_data_ready(parent, sk);
1690     else if (unlikely(sk->sk_err))
1691         subflow_error_report(sk);
1692 
1693     subflow_sched_work_if_closed(mptcp_sk(parent), sk);
1694 
1695     if (__mptcp_check_fallback(mptcp_sk(parent)) &&
1696         !subflow->rx_eof && subflow_is_done(sk)) {
1697         subflow->rx_eof = 1;
1698         mptcp_subflow_eof(parent);
1699     }
1700 }
1701 
1702 void mptcp_subflow_queue_clean(struct sock *listener_ssk)
1703 {
1704     struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1705     struct mptcp_sock *msk, *next, *head = NULL;
1706     struct request_sock *req;
1707 
1708     /* build a list of all unaccepted mptcp sockets */
1709     spin_lock_bh(&queue->rskq_lock);
1710     for (req = queue->rskq_accept_head; req; req = req->dl_next) {
1711         struct mptcp_subflow_context *subflow;
1712         struct sock *ssk = req->sk;
1713         struct mptcp_sock *msk;
1714 
1715         if (!sk_is_mptcp(ssk))
1716             continue;
1717 
1718         subflow = mptcp_subflow_ctx(ssk);
1719         if (!subflow || !subflow->conn)
1720             continue;
1721 
1722         /* skip if already in list */
1723         msk = mptcp_sk(subflow->conn);
1724         if (msk->dl_next || msk == head)
1725             continue;
1726 
1727         msk->dl_next = head;
1728         head = msk;
1729     }
1730     spin_unlock_bh(&queue->rskq_lock);
1731     if (!head)
1732         return;
1733 
1734     /* can't acquire the msk socket lock under the subflow one,
1735      * or will cause ABBA deadlock
1736      */
1737     release_sock(listener_ssk);
1738 
1739     for (msk = head; msk; msk = next) {
1740         struct sock *sk = (struct sock *)msk;
1741         bool slow, do_cancel_work;
1742 
1743         sock_hold(sk);
1744         slow = lock_sock_fast_nested(sk);
1745         next = msk->dl_next;
1746         msk->first = NULL;
1747         msk->dl_next = NULL;
1748 
1749         do_cancel_work = __mptcp_close(sk, 0);
1750         unlock_sock_fast(sk, slow);
1751         if (do_cancel_work)
1752             mptcp_cancel_work(sk);
1753         sock_put(sk);
1754     }
1755 
1756     /* we are still under the listener msk socket lock */
1757     lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1758 }
1759 
1760 static int subflow_ulp_init(struct sock *sk)
1761 {
1762     struct inet_connection_sock *icsk = inet_csk(sk);
1763     struct mptcp_subflow_context *ctx;
1764     struct tcp_sock *tp = tcp_sk(sk);
1765     int err = 0;
1766 
1767     /* disallow attaching ULP to a socket unless it has been
1768      * created with sock_create_kern()
1769      */
1770     if (!sk->sk_kern_sock) {
1771         err = -EOPNOTSUPP;
1772         goto out;
1773     }
1774 
1775     ctx = subflow_create_ctx(sk, GFP_KERNEL);
1776     if (!ctx) {
1777         err = -ENOMEM;
1778         goto out;
1779     }
1780 
1781     pr_debug("subflow=%p, family=%d", ctx, sk->sk_family);
1782 
1783     tp->is_mptcp = 1;
1784     ctx->icsk_af_ops = icsk->icsk_af_ops;
1785     icsk->icsk_af_ops = subflow_default_af_ops(sk);
1786     ctx->tcp_state_change = sk->sk_state_change;
1787     ctx->tcp_error_report = sk->sk_error_report;
1788 
1789     WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable);
1790     WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space);
1791 
1792     sk->sk_data_ready = subflow_data_ready;
1793     sk->sk_write_space = subflow_write_space;
1794     sk->sk_state_change = subflow_state_change;
1795     sk->sk_error_report = subflow_error_report;
1796 out:
1797     return err;
1798 }
1799 
1800 static void subflow_ulp_release(struct sock *ssk)
1801 {
1802     struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
1803     bool release = true;
1804     struct sock *sk;
1805 
1806     if (!ctx)
1807         return;
1808 
1809     sk = ctx->conn;
1810     if (sk) {
1811         /* if the msk has been orphaned, keep the ctx
1812          * alive, will be freed by __mptcp_close_ssk(),
1813          * when the subflow is still unaccepted
1814          */
1815         release = ctx->disposable || list_empty(&ctx->node);
1816         sock_put(sk);
1817     }
1818 
1819     mptcp_subflow_ops_undo_override(ssk);
1820     if (release)
1821         kfree_rcu(ctx, rcu);
1822 }
1823 
1824 static void subflow_ulp_clone(const struct request_sock *req,
1825                   struct sock *newsk,
1826                   const gfp_t priority)
1827 {
1828     struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
1829     struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
1830     struct mptcp_subflow_context *new_ctx;
1831 
1832     if (!tcp_rsk(req)->is_mptcp ||
1833         (!subflow_req->mp_capable && !subflow_req->mp_join)) {
1834         subflow_ulp_fallback(newsk, old_ctx);
1835         return;
1836     }
1837 
1838     new_ctx = subflow_create_ctx(newsk, priority);
1839     if (!new_ctx) {
1840         subflow_ulp_fallback(newsk, old_ctx);
1841         return;
1842     }
1843 
1844     new_ctx->conn_finished = 1;
1845     new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
1846     new_ctx->tcp_state_change = old_ctx->tcp_state_change;
1847     new_ctx->tcp_error_report = old_ctx->tcp_error_report;
1848     new_ctx->rel_write_seq = 1;
1849     new_ctx->tcp_sock = newsk;
1850 
1851     if (subflow_req->mp_capable) {
1852         /* see comments in subflow_syn_recv_sock(), MPTCP connection
1853          * is fully established only after we receive the remote key
1854          */
1855         new_ctx->mp_capable = 1;
1856         new_ctx->local_key = subflow_req->local_key;
1857         new_ctx->token = subflow_req->token;
1858         new_ctx->ssn_offset = subflow_req->ssn_offset;
1859         new_ctx->idsn = subflow_req->idsn;
1860 
1861         /* this is the first subflow, id is always 0 */
1862         new_ctx->local_id_valid = 1;
1863     } else if (subflow_req->mp_join) {
1864         new_ctx->ssn_offset = subflow_req->ssn_offset;
1865         new_ctx->mp_join = 1;
1866         new_ctx->fully_established = 1;
1867         new_ctx->backup = subflow_req->backup;
1868         new_ctx->remote_id = subflow_req->remote_id;
1869         new_ctx->token = subflow_req->token;
1870         new_ctx->thmac = subflow_req->thmac;
1871 
1872         /* the subflow req id is valid, fetched via subflow_check_req()
1873          * and subflow_token_join_request()
1874          */
1875         subflow_set_local_id(new_ctx, subflow_req->local_id);
1876     }
1877 }
1878 
1879 static void tcp_release_cb_override(struct sock *ssk)
1880 {
1881     struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1882 
1883     if (mptcp_subflow_has_delegated_action(subflow))
1884         mptcp_subflow_process_delegated(ssk);
1885 
1886     tcp_release_cb(ssk);
1887 }
1888 
1889 static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
1890     .name       = "mptcp",
1891     .owner      = THIS_MODULE,
1892     .init       = subflow_ulp_init,
1893     .release    = subflow_ulp_release,
1894     .clone      = subflow_ulp_clone,
1895 };
1896 
1897 static int subflow_ops_init(struct request_sock_ops *subflow_ops)
1898 {
1899     subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
1900     subflow_ops->slab_name = "request_sock_subflow";
1901 
1902     subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
1903                           subflow_ops->obj_size, 0,
1904                           SLAB_ACCOUNT |
1905                           SLAB_TYPESAFE_BY_RCU,
1906                           NULL);
1907     if (!subflow_ops->slab)
1908         return -ENOMEM;
1909 
1910     subflow_ops->destructor = subflow_req_destructor;
1911 
1912     return 0;
1913 }
1914 
1915 void __init mptcp_subflow_init(void)
1916 {
1917     mptcp_subflow_request_sock_ops = tcp_request_sock_ops;
1918     if (subflow_ops_init(&mptcp_subflow_request_sock_ops) != 0)
1919         panic("MPTCP: failed to init subflow request sock ops\n");
1920 
1921     subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
1922     subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
1923 
1924     subflow_specific = ipv4_specific;
1925     subflow_specific.conn_request = subflow_v4_conn_request;
1926     subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
1927     subflow_specific.sk_rx_dst_set = subflow_finish_connect;
1928     subflow_specific.rebuild_header = subflow_rebuild_header;
1929 
1930     tcp_prot_override = tcp_prot;
1931     tcp_prot_override.release_cb = tcp_release_cb_override;
1932 
1933 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1934     subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
1935     subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
1936 
1937     subflow_v6_specific = ipv6_specific;
1938     subflow_v6_specific.conn_request = subflow_v6_conn_request;
1939     subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
1940     subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
1941     subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header;
1942 
1943     subflow_v6m_specific = subflow_v6_specific;
1944     subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
1945     subflow_v6m_specific.send_check = ipv4_specific.send_check;
1946     subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
1947     subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
1948     subflow_v6m_specific.net_frag_header_len = 0;
1949     subflow_v6m_specific.rebuild_header = subflow_rebuild_header;
1950 
1951     tcpv6_prot_override = tcpv6_prot;
1952     tcpv6_prot_override.release_cb = tcp_release_cb_override;
1953 #endif
1954 
1955     mptcp_diag_subflow_init(&subflow_ulp_ops);
1956 
1957     if (tcp_register_ulp(&subflow_ulp_ops) != 0)
1958         panic("MPTCP: failed to register subflows to ULP\n");
1959 }