LCOV - code coverage report
Current view: top level - mptcp/subflow.c (source / functions) Coverage Total Hit
Test: export-net Lines: 90.0 % 1134 1021
Test Date: 2024-10-18 11:14:13 Functions: 95.5 % 67 64
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 63.1 % 862 544

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

Generated by: LCOV version 2.0-1