LCOV - code coverage report
Current view: top level - mptcp/subflow.c (source / functions) Coverage Total Hit
Test: export Lines: 90.2 % 1130 1019
Test Date: 2025-08-27 07:32:45 Functions: 95.5 % 66 63
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 61.5 % 886 545

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

Generated by: LCOV version 2.3.1-1