LCOV - code coverage report
Current view: top level - mptcp/subflow.c (source / functions) Coverage Total Hit
Test: export Lines: 90.2 % 1132 1021
Test Date: 2025-09-18 07:21:50 Functions: 95.5 % 66 63
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 61.6 % 888 547

             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                 :        2042 : 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         [ +  + ]:        2042 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     348                 :        2042 :         struct inet_request_sock *ireq = inet_rsk(req);
     349                 :             : 
     350                 :             :         /* clear tstamp_ok, as needed depending on cookie */
     351   [ +  +  +  + ]:        2042 :         if (foc && foc->len > -1)
     352                 :          28 :                 ireq->tstamp_ok = 0;
     353                 :             : 
     354         [ +  + ]:        2042 :         if (synack_type == TCP_SYNACK_FASTOPEN)
     355                 :          46 :                 mptcp_fastopen_subflow_synack_set_params(subflow, req);
     356                 :        2042 : }
     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                 :         442 : 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                 :         442 :         subflow_prep_synack(sk, req, foc, synack_type);
     380                 :             : 
     381                 :         442 :         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                 :         424 :                 __mptcp_sync_state(sk, ssk->sk_state);
     518                 :             :         } else {
     519                 :        1066 :                 msk->pending_state = ssk->sk_state;
     520   [ -  +  -  -  :        1066 :                 __set_bit(MPTCP_SYNC_STATE, &msk->cb_flags);
                   -  - ]
     521                 :             :         }
     522                 :        1490 :         mptcp_data_unlock(sk);
     523                 :        1490 : }
     524                 :             : 
     525                 :        6124 : static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
     526                 :             : {
     527                 :        6124 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     528                 :        6124 :         struct mptcp_options_received mp_opt;
     529                 :        6124 :         struct sock *parent = subflow->conn;
     530                 :        6124 :         struct mptcp_sock *msk;
     531                 :             : 
     532                 :        6124 :         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         [ +  + ]:        6124 :         if (subflow->conn_finished)
     536                 :        6124 :                 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                 :        1159 : static void subflow_set_local_id(struct mptcp_subflow_context *subflow, int local_id)
     624                 :             : {
     625         [ -  + ]:        1099 :         WARN_ON_ONCE(local_id < 0 || local_id > 255);
     626                 :        2501 :         WRITE_ONCE(subflow->local_id, local_id);
     627                 :        2501 : }
     628                 :             : 
     629                 :        9113 : static int subflow_chk_local_id(struct sock *sk)
     630                 :             : {
     631         [ -  + ]:        9113 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     632         [ -  + ]:        9113 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
     633                 :        9113 :         int err;
     634                 :             : 
     635         [ +  + ]:        9113 :         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                 :        6602 : static int subflow_rebuild_header(struct sock *sk)
     649                 :             : {
     650                 :        6602 :         int err = subflow_chk_local_id(sk);
     651                 :             : 
     652         [ +  - ]:        6602 :         if (unlikely(err < 0))
     653                 :             :                 return err;
     654                 :             : 
     655                 :        6602 :         return inet_sk_rebuild_header(sk);
     656                 :             : }
     657                 :             : 
     658                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
     659                 :        2511 : static int subflow_v6_rebuild_header(struct sock *sk)
     660                 :             : {
     661                 :        2511 :         int err = subflow_chk_local_id(sk);
     662                 :             : 
     663         [ +  - ]:        2511 :         if (unlikely(err < 0))
     664                 :             :                 return err;
     665                 :             : 
     666                 :        2511 :         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                 :        1087 : static int subflow_v6_conn_request(struct sock *sk, struct sk_buff *skb)
     705                 :             : {
     706         [ -  + ]:        1087 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     707                 :             : 
     708         [ -  + ]:        1087 :         pr_debug("subflow=%p\n", subflow);
     709                 :             : 
     710         [ +  + ]:        1087 :         if (skb->protocol == htons(ETH_P_IP))
     711                 :         645 :                 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                 :             : 
     887         [ +  + ]:        1384 :                         if (mp_opt.deny_join_id0)
     888                 :          10 :                                 WRITE_ONCE(owner->pm.remote_deny_join_id0, true);
     889                 :             : 
     890                 :        1384 :                         mptcp_pm_new_connection(owner, child, 1);
     891                 :             : 
     892                 :             :                         /* with OoO packets we can reach here without ingress
     893                 :             :                          * mpc option
     894                 :             :                          */
     895         [ +  + ]:        1384 :                         if (mp_opt.suboptions & OPTION_MPTCP_MPC_ACK) {
     896                 :        1338 :                                 mptcp_pm_fully_established(owner, child);
     897                 :        1338 :                                 ctx->pm_notified = 1;
     898                 :             :                         }
     899         [ +  - ]:         508 :                 } else if (ctx->mp_join) {
     900                 :         508 :                         owner = subflow_req->msk;
     901         [ -  + ]:         508 :                         if (!owner) {
     902                 :           0 :                                 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
     903                 :           0 :                                 goto dispose_child;
     904                 :             :                         }
     905                 :             : 
     906         [ -  + ]:         508 :                         if (!subflow_hmac_valid(subflow_req, &mp_opt)) {
     907         [ #  # ]:           0 :                                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKMAC);
     908                 :           0 :                                 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
     909                 :           0 :                                 goto dispose_child;
     910                 :             :                         }
     911                 :             : 
     912         [ +  + ]:         508 :                         if (!mptcp_can_accept_new_subflow(owner)) {
     913         [ +  - ]:           8 :                                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINREJECTED);
     914                 :           8 :                                 subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
     915                 :           8 :                                 goto dispose_child;
     916                 :             :                         }
     917                 :             : 
     918                 :             :                         /* move the msk reference ownership to the subflow */
     919                 :         500 :                         subflow_req->msk = NULL;
     920                 :         500 :                         ctx->conn = (struct sock *)owner;
     921                 :             : 
     922         [ +  + ]:         500 :                         if (subflow_use_different_sport(owner, sk)) {
     923         [ -  + ]:          24 :                                 pr_debug("ack inet_sport=%d %d\n",
     924                 :             :                                          ntohs(inet_sk(sk)->inet_sport),
     925                 :             :                                          ntohs(inet_sk((struct sock *)owner)->inet_sport));
     926         [ -  + ]:          24 :                                 if (!mptcp_pm_sport_in_anno_list(owner, sk)) {
     927         [ #  # ]:           0 :                                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MISMATCHPORTACKRX);
     928                 :           0 :                                         subflow_add_reset_reason(skb, MPTCP_RST_EPROHIBIT);
     929                 :           0 :                                         goto dispose_child;
     930                 :             :                                 }
     931         [ +  - ]:          24 :                                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINPORTACKRX);
     932                 :             :                         }
     933                 :             : 
     934         [ -  + ]:         500 :                         if (!mptcp_finish_join(child)) {
     935                 :           0 :                                 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(child);
     936                 :             : 
     937                 :           0 :                                 subflow_add_reset_reason(skb, subflow->reset_reason);
     938                 :           0 :                                 goto dispose_child;
     939                 :             :                         }
     940                 :             : 
     941         [ +  - ]:         500 :                         SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
     942                 :         500 :                         tcp_rsk(req)->drop_req = true;
     943                 :             :                 }
     944                 :             :         }
     945                 :             : 
     946                 :             :         /* check for expected invariant - should never trigger, just help
     947                 :             :          * catching earlier subtle bugs
     948                 :             :          */
     949   [ -  +  +  -  :        1884 :         WARN_ON_ONCE(child && *own_req && tcp_sk(child)->is_mptcp &&
          -  +  -  +  +  
             -  +  -  -  
           + ][ +  -  -  
          +  +  -  +  -  
                   -  + ]
     950                 :             :                      (!mptcp_subflow_ctx(child) ||
     951                 :             :                       !mptcp_subflow_ctx(child)->conn));
     952                 :        1884 :         return child;
     953                 :             : 
     954                 :           8 : dispose_child:
     955                 :           8 :         mptcp_subflow_drop_ctx(child);
     956                 :           8 :         tcp_rsk(req)->drop_req = true;
     957                 :           8 :         inet_csk_prepare_for_destroy_sock(child);
     958                 :           8 :         tcp_done(child);
     959                 :           8 :         reason = mptcp_get_rst_reason(skb);
     960                 :           8 :         req->rsk_ops->send_reset(sk, skb, reason);
     961                 :             : 
     962                 :             :         /* The last child reference will be released by the caller */
     963                 :           8 :         return child;
     964                 :             : 
     965                 :         142 : fallback:
     966         [ +  + ]:         142 :         if (fallback)
     967         [ +  - ]:          18 :                 SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPCAPABLEPASSIVEFALLBACK);
     968                 :         142 :         mptcp_subflow_drop_ctx(child);
     969                 :         142 :         return child;
     970                 :             : }
     971                 :             : 
     972                 :             : static struct inet_connection_sock_af_ops subflow_specific __ro_after_init;
     973                 :             : static struct proto tcp_prot_override __ro_after_init;
     974                 :             : 
     975                 :             : enum mapping_status {
     976                 :             :         MAPPING_OK,
     977                 :             :         MAPPING_INVALID,
     978                 :             :         MAPPING_EMPTY,
     979                 :             :         MAPPING_DATA_FIN,
     980                 :             :         MAPPING_DUMMY,
     981                 :             :         MAPPING_BAD_CSUM,
     982                 :             :         MAPPING_NODSS
     983                 :             : };
     984                 :             : 
     985                 :           0 : static void dbg_bad_map(struct mptcp_subflow_context *subflow, u32 ssn)
     986                 :             : {
     987         [ #  # ]:           0 :         pr_debug("Bad mapping: ssn=%d map_seq=%d map_data_len=%d\n",
     988                 :             :                  ssn, subflow->map_subflow_seq, subflow->map_data_len);
     989                 :           0 : }
     990                 :             : 
     991                 :           0 : static bool skb_is_fully_mapped(struct sock *ssk, struct sk_buff *skb)
     992                 :             : {
     993         [ #  # ]:           0 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
     994                 :           0 :         unsigned int skb_consumed;
     995                 :             : 
     996         [ #  # ]:           0 :         skb_consumed = tcp_sk(ssk)->copied_seq - TCP_SKB_CB(skb)->seq;
     997         [ #  # ]:           0 :         if (unlikely(skb_consumed >= skb->len)) {
     998                 :           0 :                 DEBUG_NET_WARN_ON_ONCE(1);
     999                 :           0 :                 return true;
    1000                 :             :         }
    1001                 :             : 
    1002                 :           0 :         return skb->len - skb_consumed <= subflow->map_data_len -
    1003                 :           0 :                                           mptcp_subflow_get_map_offset(subflow);
    1004                 :             : }
    1005                 :             : 
    1006                 :      494053 : static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
    1007                 :             : {
    1008         [ -  + ]:      494053 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1009         [ -  + ]:      494053 :         u32 ssn = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
    1010                 :             : 
    1011         [ -  + ]:      494053 :         if (unlikely(before(ssn, subflow->map_subflow_seq))) {
    1012                 :             :                 /* Mapping covers data later in the subflow stream,
    1013                 :             :                  * currently unsupported.
    1014                 :             :                  */
    1015                 :           0 :                 dbg_bad_map(subflow, ssn);
    1016                 :           0 :                 return false;
    1017                 :             :         }
    1018         [ -  + ]:      494053 :         if (unlikely(!before(ssn, subflow->map_subflow_seq +
    1019                 :             :                                   subflow->map_data_len))) {
    1020                 :             :                 /* Mapping does covers past subflow data, invalid */
    1021                 :           0 :                 dbg_bad_map(subflow, ssn);
    1022                 :           0 :                 return false;
    1023                 :             :         }
    1024                 :             :         return true;
    1025                 :             : }
    1026                 :             : 
    1027                 :      959428 : static enum mapping_status validate_data_csum(struct sock *ssk, struct sk_buff *skb,
    1028                 :             :                                               bool csum_reqd)
    1029                 :             : {
    1030         [ +  + ]:      959428 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1031                 :      959428 :         u32 offset, seq, delta;
    1032                 :      959428 :         __sum16 csum;
    1033                 :      959428 :         int len;
    1034                 :             : 
    1035         [ +  + ]:      959428 :         if (!csum_reqd)
    1036                 :             :                 return MAPPING_OK;
    1037                 :             : 
    1038                 :             :         /* mapping already validated on previous traversal */
    1039         [ +  + ]:      249828 :         if (subflow->map_csum_len == subflow->map_data_len)
    1040                 :             :                 return MAPPING_OK;
    1041                 :             : 
    1042                 :             :         /* traverse the receive queue, ensuring it contains a full
    1043                 :             :          * DSS mapping and accumulating the related csum.
    1044                 :             :          * Preserve the accoumlate csum across multiple calls, to compute
    1045                 :             :          * the csum only once
    1046                 :             :          */
    1047                 :      249785 :         delta = subflow->map_data_len - subflow->map_csum_len;
    1048                 :      495180 :         for (;;) {
    1049         [ -  + ]:      495180 :                 seq = tcp_sk(ssk)->copied_seq + subflow->map_csum_len;
    1050                 :      495180 :                 offset = seq - TCP_SKB_CB(skb)->seq;
    1051                 :             : 
    1052                 :             :                 /* if the current skb has not been accounted yet, csum its contents
    1053                 :             :                  * up to the amount covered by the current DSS
    1054                 :             :                  */
    1055         [ +  + ]:      495180 :                 if (offset < skb->len) {
    1056                 :      251627 :                         __wsum csum;
    1057                 :             : 
    1058                 :      251627 :                         len = min(skb->len - offset, delta);
    1059                 :      251627 :                         csum = skb_checksum(skb, offset, len, 0);
    1060                 :      503254 :                         subflow->map_data_csum = csum_block_add(subflow->map_data_csum, csum,
    1061         [ -  + ]:      251627 :                                                                 subflow->map_csum_len);
    1062                 :             : 
    1063                 :      251627 :                         delta -= len;
    1064                 :      251627 :                         subflow->map_csum_len += len;
    1065                 :             :                 }
    1066         [ +  + ]:      495180 :                 if (delta == 0)
    1067                 :             :                         break;
    1068                 :             : 
    1069         [ +  + ]:      404693 :                 if (skb_queue_is_last(&ssk->sk_receive_queue, skb)) {
    1070                 :             :                         /* if this subflow is closed, the partial mapping
    1071                 :             :                          * will be never completed; flush the pending skbs, so
    1072                 :             :                          * that subflow_sched_work_if_closed() can kick in
    1073                 :             :                          */
    1074         [ -  + ]:      159298 :                         if (unlikely(ssk->sk_state == TCP_CLOSE))
    1075   [ #  #  #  # ]:           0 :                                 while ((skb = skb_peek(&ssk->sk_receive_queue)))
    1076                 :           0 :                                         sk_eat_skb(ssk, skb);
    1077                 :             : 
    1078                 :             :                         /* not enough data to validate the csum */
    1079                 :      159298 :                         return MAPPING_EMPTY;
    1080                 :             :                 }
    1081                 :             : 
    1082                 :             :                 /* the DSS mapping for next skbs will be validated later,
    1083                 :             :                  * when a get_mapping_status call will process such skb
    1084                 :             :                  */
    1085                 :             :                 skb = skb->next;
    1086                 :             :         }
    1087                 :             : 
    1088                 :             :         /* note that 'map_data_len' accounts only for the carried data, does
    1089                 :             :          * not include the eventual seq increment due to the data fin,
    1090                 :             :          * while the pseudo header requires the original DSS data len,
    1091                 :             :          * including that
    1092                 :             :          */
    1093                 :       90487 :         csum = __mptcp_make_csum(subflow->map_seq,
    1094                 :             :                                  subflow->map_subflow_seq,
    1095                 :       90487 :                                  subflow->map_data_len + subflow->map_data_fin,
    1096                 :             :                                  subflow->map_data_csum);
    1097         [ +  + ]:       90487 :         if (unlikely(csum)) {
    1098         [ +  - ]:           4 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DATACSUMERR);
    1099                 :           4 :                 return MAPPING_BAD_CSUM;
    1100                 :             :         }
    1101                 :             : 
    1102                 :       90483 :         subflow->valid_csum_seen = 1;
    1103                 :       90483 :         return MAPPING_OK;
    1104                 :             : }
    1105                 :             : 
    1106                 :     1763707 : static enum mapping_status get_mapping_status(struct sock *ssk,
    1107                 :             :                                               struct mptcp_sock *msk)
    1108                 :             : {
    1109         [ +  + ]:     1763707 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1110         [ -  + ]:     1763707 :         bool csum_reqd = READ_ONCE(msk->csum_enabled);
    1111                 :     1763707 :         struct mptcp_ext *mpext;
    1112                 :     1763707 :         struct sk_buff *skb;
    1113                 :     1763707 :         u16 data_len;
    1114                 :     1763707 :         u64 map_seq;
    1115                 :             : 
    1116         [ +  + ]:     1763779 :         skb = skb_peek(&ssk->sk_receive_queue);
    1117         [ -  + ]:      979063 :         if (!skb)
    1118                 :             :                 return MAPPING_EMPTY;
    1119                 :             : 
    1120         [ +  + ]:      979063 :         if (mptcp_check_fallback(ssk))
    1121                 :             :                 return MAPPING_DUMMY;
    1122                 :             : 
    1123         [ +  + ]:      961670 :         mpext = mptcp_get_ext(skb);
    1124   [ +  +  +  + ]:      822528 :         if (!mpext || !mpext->use_map) {
    1125   [ +  +  +  + ]:      161765 :                 if (!subflow->map_valid && !skb->len) {
    1126                 :             :                         /* the TCP stack deliver 0 len FIN pkt to the receive
    1127                 :             :                          * queue, that is the only 0len pkts ever expected here,
    1128                 :             :                          * and we can admit no mapping only for 0 len pkts
    1129                 :             :                          */
    1130         [ -  + ]:        2210 :                         if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
    1131   [ #  #  #  # ]:           0 :                                 WARN_ONCE(1, "0len seq %d:%d flags %x",
                 [ #  # ]
    1132                 :             :                                           TCP_SKB_CB(skb)->seq,
    1133                 :             :                                           TCP_SKB_CB(skb)->end_seq,
    1134                 :             :                                           TCP_SKB_CB(skb)->tcp_flags);
    1135                 :        2210 :                         sk_eat_skb(ssk, skb);
    1136                 :        2210 :                         return MAPPING_EMPTY;
    1137                 :             :                 }
    1138                 :             : 
    1139                 :             :                 /* If the required DSS has likely been dropped by a middlebox */
    1140         [ +  + ]:      159555 :                 if (!subflow->map_valid)
    1141                 :             :                         return MAPPING_NODSS;
    1142                 :             : 
    1143                 :      159537 :                 goto validate_seq;
    1144                 :             :         }
    1145                 :             : 
    1146                 :      799905 :         trace_get_mapping_status(mpext);
    1147                 :             : 
    1148                 :      799905 :         data_len = mpext->data_len;
    1149         [ +  + ]:      799905 :         if (data_len == 0) {
    1150         [ -  + ]:           2 :                 pr_debug("infinite mapping received\n");
    1151         [ +  - ]:           2 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_INFINITEMAPRX);
    1152                 :           2 :                 return MAPPING_INVALID;
    1153                 :             :         }
    1154                 :             : 
    1155         [ +  + ]:      799903 :         if (mpext->data_fin == 1) {
    1156                 :        1227 :                 u64 data_fin_seq;
    1157                 :             : 
    1158         [ +  + ]:        1227 :                 if (data_len == 1) {
    1159                 :          12 :                         bool updated = mptcp_update_rcv_data_fin(msk, mpext->data_seq,
    1160                 :          12 :                                                                  mpext->dsn64);
    1161         [ -  + ]:          12 :                         pr_debug("DATA_FIN with no payload seq=%llu\n", mpext->data_seq);
    1162         [ -  + ]:          12 :                         if (subflow->map_valid) {
    1163                 :             :                                 /* A DATA_FIN might arrive in a DSS
    1164                 :             :                                  * option before the previous mapping
    1165                 :             :                                  * has been fully consumed. Continue
    1166                 :             :                                  * handling the existing mapping.
    1167                 :             :                                  */
    1168                 :           0 :                                 skb_ext_del(skb, SKB_EXT_MPTCP);
    1169                 :           0 :                                 return MAPPING_OK;
    1170                 :             :                         }
    1171                 :             : 
    1172         [ +  + ]:          12 :                         if (updated)
    1173                 :           6 :                                 mptcp_schedule_work((struct sock *)msk);
    1174                 :             : 
    1175                 :          12 :                         return MAPPING_DATA_FIN;
    1176                 :             :                 }
    1177                 :             : 
    1178                 :        1215 :                 data_fin_seq = mpext->data_seq + data_len - 1;
    1179                 :             : 
    1180                 :             :                 /* If mpext->data_seq is a 32-bit value, data_fin_seq must also
    1181                 :             :                  * be limited to 32 bits.
    1182                 :             :                  */
    1183         [ -  + ]:        1215 :                 if (!mpext->dsn64)
    1184                 :           0 :                         data_fin_seq &= GENMASK_ULL(31, 0);
    1185                 :             : 
    1186                 :        1215 :                 mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
    1187         [ -  + ]:        1215 :                 pr_debug("DATA_FIN with mapping seq=%llu dsn64=%d\n",
    1188                 :             :                          data_fin_seq, mpext->dsn64);
    1189                 :             : 
    1190                 :             :                 /* Adjust for DATA_FIN using 1 byte of sequence space */
    1191                 :        1215 :                 data_len--;
    1192                 :             :         }
    1193                 :             : 
    1194         [ +  + ]:      799891 :         map_seq = mptcp_expand_seq(READ_ONCE(msk->ack_seq), mpext->data_seq, mpext->dsn64);
    1195         [ -  + ]:      799891 :         WRITE_ONCE(mptcp_sk(subflow->conn)->use_64bit_ack, !!mpext->dsn64);
    1196                 :             : 
    1197         [ +  + ]:      799891 :         if (subflow->map_valid) {
    1198                 :             :                 /* Allow replacing only with an identical map */
    1199         [ +  - ]:      465375 :                 if (subflow->map_seq == map_seq &&
    1200         [ +  - ]:      465375 :                     subflow->map_subflow_seq == mpext->subflow_seq &&
    1201         [ +  - ]:      465375 :                     subflow->map_data_len == data_len &&
    1202         [ +  - ]:      465375 :                     subflow->map_csum_reqd == mpext->csum_reqd) {
    1203                 :      465375 :                         skb_ext_del(skb, SKB_EXT_MPTCP);
    1204                 :      465375 :                         goto validate_csum;
    1205                 :             :                 }
    1206                 :             : 
    1207                 :             :                 /* If this skb data are fully covered by the current mapping,
    1208                 :             :                  * the new map would need caching, which is not supported
    1209                 :             :                  */
    1210         [ #  # ]:           0 :                 if (skb_is_fully_mapped(ssk, skb)) {
    1211         [ #  # ]:           0 :                         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSNOMATCH);
    1212                 :           0 :                         return MAPPING_INVALID;
    1213                 :             :                 }
    1214                 :             : 
    1215                 :             :                 /* will validate the next map after consuming the current one */
    1216                 :           0 :                 goto validate_csum;
    1217                 :             :         }
    1218                 :             : 
    1219                 :      334516 :         subflow->map_seq = map_seq;
    1220                 :      334516 :         subflow->map_subflow_seq = mpext->subflow_seq;
    1221                 :      334516 :         subflow->map_data_len = data_len;
    1222                 :      334516 :         subflow->map_valid = 1;
    1223                 :      334516 :         subflow->map_data_fin = mpext->data_fin;
    1224                 :      334516 :         subflow->mpc_map = mpext->mpc_map;
    1225                 :      334516 :         subflow->map_csum_reqd = mpext->csum_reqd;
    1226                 :      334516 :         subflow->map_csum_len = 0;
    1227         [ -  + ]:      334516 :         subflow->map_data_csum = csum_unfold(mpext->csum);
    1228                 :             : 
    1229                 :             :         /* Cfr RFC 8684 Section 3.3.0 */
    1230         [ -  + ]:      334516 :         if (unlikely(subflow->map_csum_reqd != csum_reqd))
    1231                 :             :                 return MAPPING_INVALID;
    1232                 :             : 
    1233         [ -  + ]:      334516 :         pr_debug("new map seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
    1234                 :             :                  subflow->map_seq, subflow->map_subflow_seq,
    1235                 :             :                  subflow->map_data_len, subflow->map_csum_reqd,
    1236                 :             :                  subflow->map_data_csum);
    1237                 :             : 
    1238                 :      494053 : validate_seq:
    1239                 :             :         /* we revalidate valid mapping on new skb, because we must ensure
    1240                 :             :          * the current skb is completely covered by the available mapping
    1241                 :             :          */
    1242         [ -  + ]:      494053 :         if (!validate_mapping(ssk, skb)) {
    1243         [ #  # ]:           0 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DSSTCPMISMATCH);
    1244                 :           0 :                 return MAPPING_INVALID;
    1245                 :             :         }
    1246                 :             : 
    1247                 :      494053 :         skb_ext_del(skb, SKB_EXT_MPTCP);
    1248                 :             : 
    1249                 :      959428 : validate_csum:
    1250                 :      959428 :         return validate_data_csum(ssk, skb, csum_reqd);
    1251                 :             : }
    1252                 :             : 
    1253                 :        7550 : static void mptcp_subflow_discard_data(struct sock *ssk, struct sk_buff *skb,
    1254                 :             :                                        u64 limit)
    1255                 :             : {
    1256         [ -  + ]:        7550 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1257                 :        7550 :         bool fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
    1258         [ -  + ]:        7550 :         struct tcp_sock *tp = tcp_sk(ssk);
    1259                 :        7550 :         u32 offset, incr, avail_len;
    1260                 :             : 
    1261                 :        7550 :         offset = tp->copied_seq - TCP_SKB_CB(skb)->seq;
    1262   [ -  +  -  + ]:        7550 :         if (WARN_ON_ONCE(offset > skb->len))
    1263                 :           0 :                 goto out;
    1264                 :             : 
    1265                 :        7550 :         avail_len = skb->len - offset;
    1266         [ +  + ]:        7550 :         incr = limit >= avail_len ? avail_len + fin : limit;
    1267                 :             : 
    1268         [ -  + ]:        7550 :         pr_debug("discarding=%d len=%d offset=%d seq=%d\n", incr, skb->len,
    1269                 :             :                  offset, subflow->map_subflow_seq);
    1270         [ +  - ]:        7550 :         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_DUPDATA);
    1271         [ -  + ]:        7550 :         tcp_sk(ssk)->copied_seq += incr;
    1272                 :             : 
    1273                 :        7550 : out:
    1274   [ -  +  +  + ]:        7550 :         if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
    1275                 :        7311 :                 sk_eat_skb(ssk, skb);
    1276         [ +  + ]:        7550 :         if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
    1277                 :        6172 :                 subflow->map_valid = 0;
    1278                 :        7550 : }
    1279                 :             : 
    1280                 :           0 : static bool subflow_is_done(const struct sock *sk)
    1281                 :             : {
    1282   [ -  +  -  - ]:         389 :         return sk->sk_shutdown & RCV_SHUTDOWN || sk->sk_state == TCP_CLOSE;
    1283                 :             : }
    1284                 :             : 
    1285                 :             : /* sched mptcp worker for subflow cleanup if no more data is pending */
    1286                 :      955819 : static void subflow_sched_work_if_closed(struct mptcp_sock *msk, struct sock *ssk)
    1287                 :             : {
    1288                 :      955819 :         struct sock *sk = (struct sock *)msk;
    1289                 :             : 
    1290   [ +  +  +  +  :      959705 :         if (likely(ssk->sk_state != TCP_CLOSE &&
                   +  + ]
    1291                 :             :                    (ssk->sk_state != TCP_CLOSE_WAIT ||
    1292                 :             :                     inet_sk_state_load(sk) != TCP_ESTABLISHED)))
    1293                 :      947821 :                 return;
    1294                 :             : 
    1295         [ +  + ]:        7998 :         if (!skb_queue_empty(&ssk->sk_receive_queue))
    1296                 :             :                 return;
    1297                 :             : 
    1298         [ +  + ]:       11807 :         if (!test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags))
    1299                 :        2145 :                 mptcp_schedule_work(sk);
    1300                 :             : 
    1301                 :             :         /* when the fallback subflow closes the rx side, trigger a 'dummy'
    1302                 :             :          * ingress data fin, so that the msk state will follow along
    1303                 :             :          */
    1304         [ +  + ]:        7903 :         if (__mptcp_check_fallback(msk) && subflow_is_done(ssk) &&
    1305   [ +  -  +  + ]:         778 :             msk->first == ssk &&
    1306                 :         389 :             mptcp_update_rcv_data_fin(msk, READ_ONCE(msk->ack_seq), true))
    1307                 :         130 :                 mptcp_schedule_work(sk);
    1308                 :             : }
    1309                 :             : 
    1310                 :           4 : static bool mptcp_subflow_fail(struct mptcp_sock *msk, struct sock *ssk)
    1311                 :             : {
    1312                 :           4 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1313                 :           4 :         unsigned long fail_tout;
    1314                 :             : 
    1315                 :             :         /* we are really failing, prevent any later subflow join */
    1316                 :           4 :         spin_lock_bh(&msk->fallback_lock);
    1317   [ -  +  +  + ]:           4 :         if (!msk->allow_infinite_fallback) {
                 [ +  + ]
    1318                 :           2 :                 spin_unlock_bh(&msk->fallback_lock);
    1319                 :           2 :                 return false;
    1320                 :             :         }
    1321                 :           2 :         msk->allow_subflows = false;
    1322                 :           2 :         spin_unlock_bh(&msk->fallback_lock);
    1323                 :             : 
    1324                 :             :         /* graceful failure can happen only on the MPC subflow */
    1325   [ -  +  -  + ]:           2 :         if (WARN_ON_ONCE(ssk != READ_ONCE(msk->first)))
    1326                 :             :                 return false;
    1327                 :             : 
    1328                 :             :         /* since the close timeout take precedence on the fail one,
    1329                 :             :          * no need to start the latter when the first is already set
    1330                 :             :          */
    1331         [ -  + ]:           2 :         if (sock_flag((struct sock *)msk, SOCK_DEAD))
    1332                 :             :                 return true;
    1333                 :             : 
    1334                 :             :         /* we don't need extreme accuracy here, use a zero fail_tout as special
    1335                 :             :          * value meaning no fail timeout at all;
    1336                 :             :          */
    1337                 :           2 :         fail_tout = jiffies + TCP_RTO_MAX;
    1338                 :           2 :         if (!fail_tout)
    1339                 :             :                 fail_tout = 1;
    1340                 :           2 :         WRITE_ONCE(subflow->fail_tout, fail_tout);
    1341                 :           2 :         tcp_send_ack(ssk);
    1342                 :             : 
    1343                 :           2 :         mptcp_reset_tout_timer(msk, subflow->fail_tout);
    1344                 :           2 :         return true;
    1345                 :             : }
    1346                 :             : 
    1347                 :     1906909 : static bool subflow_check_data_avail(struct sock *ssk)
    1348                 :             : {
    1349         [ +  + ]:     1906909 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1350                 :     1906909 :         enum mapping_status status;
    1351                 :     1906909 :         struct mptcp_sock *msk;
    1352                 :     1906909 :         struct sk_buff *skb;
    1353                 :             : 
    1354   [ +  +  -  + ]:     1906909 :         if (!skb_peek(&ssk->sk_receive_queue))
    1355                 :      777442 :                 WRITE_ONCE(subflow->data_avail, false);
    1356   [ -  +  +  + ]:     1906909 :         if (subflow->data_avail)
                 [ +  + ]
    1357                 :             :                 return true;
    1358                 :             : 
    1359         [ -  + ]:     1756157 :         msk = mptcp_sk(subflow->conn);
    1360                 :     1771257 :         for (;;) {
    1361                 :     1763707 :                 u64 ack_seq;
    1362                 :     1763707 :                 u64 old_ack;
    1363                 :             : 
    1364                 :     1763707 :                 status = get_mapping_status(ssk, msk);
    1365         [ +  + ]:     2550561 :                 trace_subflow_check_data_avail(status, skb_peek(&ssk->sk_receive_queue));
    1366   [ +  +  +  + ]:     1763707 :                 if (unlikely(status == MAPPING_INVALID || status == MAPPING_DUMMY ||
    1367                 :             :                              status == MAPPING_BAD_CSUM || status == MAPPING_NODSS))
    1368                 :       17417 :                         goto fallback;
    1369                 :             : 
    1370         [ +  + ]:     1746290 :                 if (status != MAPPING_OK)
    1371                 :      946164 :                         goto no_data;
    1372                 :             : 
    1373         [ +  - ]:      800126 :                 skb = skb_peek(&ssk->sk_receive_queue);
    1374         [ -  + ]:      800126 :                 if (WARN_ON_ONCE(!skb))
    1375                 :           0 :                         goto no_data;
    1376                 :             : 
    1377   [ -  +  -  + ]:      800126 :                 if (unlikely(!READ_ONCE(msk->can_ack)))
                 [ -  + ]
    1378                 :           0 :                         goto fallback;
    1379                 :             : 
    1380                 :      800126 :                 old_ack = READ_ONCE(msk->ack_seq);
    1381                 :      800126 :                 ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
    1382         [ -  + ]:      800126 :                 pr_debug("msk ack_seq=%llx subflow ack_seq=%llx\n", old_ack,
    1383                 :             :                          ack_seq);
    1384         [ +  + ]:      800126 :                 if (unlikely(before64(ack_seq, old_ack))) {
    1385                 :        7550 :                         mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
    1386                 :        7550 :                         continue;
    1387                 :             :                 }
    1388                 :             : 
    1389                 :      792576 :                 WRITE_ONCE(subflow->data_avail, true);
    1390                 :      792576 :                 break;
    1391                 :             :         }
    1392                 :      792576 :         return true;
    1393                 :             : 
    1394                 :      946164 : no_data:
    1395                 :      946164 :         subflow_sched_work_if_closed(msk, ssk);
    1396                 :      946164 :         return false;
    1397                 :             : 
    1398                 :       17417 : fallback:
    1399         [ +  + ]:       17417 :         if (!__mptcp_check_fallback(msk)) {
    1400                 :             :                 /* RFC 8684 section 3.7. */
    1401         [ +  + ]:          24 :                 if (status == MAPPING_BAD_CSUM &&
    1402         [ +  - ]:           4 :                     (subflow->mp_join || subflow->valid_csum_seen)) {
    1403                 :           4 :                         subflow->send_mp_fail = 1;
    1404                 :             : 
    1405         [ +  + ]:           4 :                         if (!mptcp_subflow_fail(msk, ssk)) {
    1406                 :           2 :                                 subflow->reset_transient = 0;
    1407                 :           2 :                                 subflow->reset_reason = MPTCP_RST_EMIDDLEBOX;
    1408                 :           2 :                                 goto reset;
    1409                 :             :                         }
    1410                 :           2 :                         WRITE_ONCE(subflow->data_avail, true);
    1411                 :           2 :                         return true;
    1412                 :             :                 }
    1413                 :             : 
    1414         [ +  + ]:          20 :                 if (!mptcp_try_fallback(ssk, MPTCP_MIB_DSSFALLBACK)) {
    1415                 :             :                         /* fatal protocol error, close the socket.
    1416                 :             :                          * subflow_error_report() will introduce the appropriate barriers
    1417                 :             :                          */
    1418                 :          12 :                         subflow->reset_transient = 0;
    1419                 :          12 :                         subflow->reset_reason = status == MAPPING_NODSS ?
    1420         [ -  + ]:          12 :                                                 MPTCP_RST_EMIDDLEBOX :
    1421                 :             :                                                 MPTCP_RST_EMPTCP;
    1422                 :             : 
    1423                 :          14 : reset:
    1424                 :          14 :                         WRITE_ONCE(ssk->sk_err, EBADMSG);
    1425                 :          14 :                         tcp_set_state(ssk, TCP_CLOSE);
    1426   [ +  +  +  - ]:          30 :                         while ((skb = skb_peek(&ssk->sk_receive_queue)))
    1427                 :          16 :                                 sk_eat_skb(ssk, skb);
    1428                 :          14 :                         mptcp_send_active_reset_reason(ssk);
    1429                 :          14 :                         WRITE_ONCE(subflow->data_avail, false);
    1430                 :          14 :                         return false;
    1431                 :             :                 }
    1432                 :             :         }
    1433                 :             : 
    1434         [ -  + ]:       17401 :         skb = skb_peek(&ssk->sk_receive_queue);
    1435                 :       17401 :         subflow->map_valid = 1;
    1436                 :       17401 :         subflow->map_seq = READ_ONCE(msk->ack_seq);
    1437                 :       17401 :         subflow->map_data_len = skb->len;
    1438         [ -  + ]:       17401 :         subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
    1439                 :       17401 :         WRITE_ONCE(subflow->data_avail, true);
    1440                 :       17401 :         return true;
    1441                 :             : }
    1442                 :             : 
    1443                 :     1906909 : bool mptcp_subflow_data_available(struct sock *sk)
    1444                 :             : {
    1445         [ +  + ]:     1906909 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
    1446                 :             : 
    1447                 :             :         /* check if current mapping is still valid */
    1448         [ +  + ]:     1906909 :         if (subflow->map_valid &&
    1449         [ +  + ]:     1585365 :             mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len) {
    1450                 :      345742 :                 subflow->map_valid = 0;
    1451                 :      345742 :                 WRITE_ONCE(subflow->data_avail, false);
    1452                 :             : 
    1453         [ -  + ]:      345742 :                 pr_debug("Done with mapping: seq=%u data_len=%u\n",
    1454                 :             :                          subflow->map_subflow_seq,
    1455                 :             :                          subflow->map_data_len);
    1456                 :             :         }
    1457                 :             : 
    1458                 :     1906909 :         return subflow_check_data_avail(sk);
    1459                 :             : }
    1460                 :             : 
    1461                 :             : /* If ssk has an mptcp parent socket, use the mptcp rcvbuf occupancy,
    1462                 :             :  * not the ssk one.
    1463                 :             :  *
    1464                 :             :  * In mptcp, rwin is about the mptcp-level connection data.
    1465                 :             :  *
    1466                 :             :  * Data that is still on the ssk rx queue can thus be ignored,
    1467                 :             :  * as far as mptcp peer is concerned that data is still inflight.
    1468                 :             :  * DSS ACK is updated when skb is moved to the mptcp rx queue.
    1469                 :             :  */
    1470                 :     2141287 : void mptcp_space(const struct sock *ssk, int *space, int *full_space)
    1471                 :             : {
    1472                 :     2141287 :         const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1473                 :     2141287 :         const struct sock *sk = subflow->conn;
    1474                 :             : 
    1475                 :     2141287 :         *space = __mptcp_space(sk);
    1476                 :     2141287 :         *full_space = mptcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
    1477                 :     2141287 : }
    1478                 :             : 
    1479                 :        2755 : static void subflow_error_report(struct sock *ssk)
    1480                 :             : {
    1481         [ +  + ]:        2755 :         struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
    1482                 :             : 
    1483                 :             :         /* bail early if this is a no-op, so that we avoid introducing a
    1484                 :             :          * problematic lockdep dependency between TCP accept queue lock
    1485                 :             :          * and msk socket spinlock
    1486                 :             :          */
    1487         [ +  + ]:        2755 :         if (!sk->sk_socket)
    1488                 :             :                 return;
    1489                 :             : 
    1490                 :        1417 :         mptcp_data_lock(sk);
    1491         [ +  + ]:        1417 :         if (!sock_owned_by_user(sk))
    1492                 :         466 :                 __mptcp_error_report(sk);
    1493                 :             :         else
    1494         [ -  + ]:         951 :                 __set_bit(MPTCP_ERROR_REPORT,  &mptcp_sk(sk)->cb_flags);
    1495                 :        1417 :         mptcp_data_unlock(sk);
    1496                 :             : }
    1497                 :             : 
    1498                 :     1027112 : static void subflow_data_ready(struct sock *sk)
    1499                 :             : {
    1500                 :     1027112 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
    1501         [ -  + ]:     1027112 :         u16 state = 1 << inet_sk_state_load(sk);
    1502                 :     1027112 :         struct sock *parent = subflow->conn;
    1503                 :     1027112 :         struct mptcp_sock *msk;
    1504                 :             : 
    1505                 :     1027112 :         trace_sk_data_ready(sk);
    1506                 :             : 
    1507         [ -  + ]:     1027112 :         msk = mptcp_sk(parent);
    1508         [ +  + ]:     1027112 :         if (state & TCPF_LISTEN) {
    1509                 :             :                 /* MPJ subflow are removed from accept queue before reaching here,
    1510                 :             :                  * avoid stray wakeups
    1511                 :             :                  */
    1512         [ +  + ]:        2026 :                 if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
    1513                 :             :                         return;
    1514                 :             : 
    1515                 :        1543 :                 parent->sk_data_ready(parent);
    1516                 :        1543 :                 return;
    1517                 :             :         }
    1518                 :             : 
    1519   [ +  +  -  +  :     1025086 :         WARN_ON_ONCE(!__mptcp_check_fallback(msk) && !subflow->mp_capable &&
                   -  - ]
    1520                 :             :                      !subflow->mp_join && !(state & TCPF_CLOSE));
    1521                 :             : 
    1522         [ +  + ]:     1025086 :         if (mptcp_subflow_data_available(sk)) {
    1523                 :      858093 :                 mptcp_data_ready(parent, sk);
    1524                 :             : 
    1525                 :             :                 /* subflow-level lowat test are not relevant.
    1526                 :             :                  * respect the msk-level threshold eventually mandating an immediate ack
    1527                 :             :                  */
    1528         [ +  + ]:      858093 :                 if (mptcp_data_avail(msk) < parent->sk_rcvlowat &&
    1529   [ -  +  -  +  :      109617 :                     (tcp_sk(sk)->rcv_nxt - tcp_sk(sk)->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss)
                   +  + ]
    1530                 :       62681 :                         inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW;
    1531         [ +  + ]:      166993 :         } else if (unlikely(sk->sk_err)) {
    1532                 :          14 :                 subflow_error_report(sk);
    1533                 :             :         }
    1534                 :             : }
    1535                 :             : 
    1536                 :      486623 : static void subflow_write_space(struct sock *ssk)
    1537                 :             : {
    1538                 :      486623 :         struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
    1539                 :             : 
    1540                 :      486623 :         mptcp_propagate_sndbuf(sk, ssk);
    1541                 :      486623 :         mptcp_write_space(sk);
    1542                 :      486623 : }
    1543                 :             : 
    1544                 :             : static const struct inet_connection_sock_af_ops *
    1545                 :           0 : subflow_default_af_ops(struct sock *sk)
    1546                 :             : {
    1547                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1548         [ #  # ]:           0 :         if (sk->sk_family == AF_INET6)
    1549                 :           0 :                 return &subflow_v6_specific;
    1550                 :             : #endif
    1551                 :             :         return &subflow_specific;
    1552                 :             : }
    1553                 :             : 
    1554                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1555                 :         741 : void mptcpv6_handle_mapped(struct sock *sk, bool mapped)
    1556                 :             : {
    1557         [ -  + ]:         741 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
    1558                 :         741 :         struct inet_connection_sock *icsk = inet_csk(sk);
    1559                 :         741 :         const struct inet_connection_sock_af_ops *target;
    1560                 :             : 
    1561         [ -  + ]:         741 :         target = mapped ? &subflow_v6m_specific : subflow_default_af_ops(sk);
    1562                 :             : 
    1563         [ -  + ]:         741 :         pr_debug("subflow=%p family=%d ops=%p target=%p mapped=%d\n",
    1564                 :             :                  subflow, sk->sk_family, icsk->icsk_af_ops, target, mapped);
    1565                 :             : 
    1566         [ +  - ]:         741 :         if (likely(icsk->icsk_af_ops == target))
    1567                 :             :                 return;
    1568                 :             : 
    1569                 :         741 :         subflow->icsk_af_ops = icsk->icsk_af_ops;
    1570                 :         741 :         icsk->icsk_af_ops = target;
    1571                 :             : }
    1572                 :             : #endif
    1573                 :             : 
    1574                 :        1126 : void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
    1575                 :             :                          struct sockaddr_storage *addr,
    1576                 :             :                          unsigned short family)
    1577                 :             : {
    1578                 :        1126 :         memset(addr, 0, sizeof(*addr));
    1579                 :        1126 :         addr->ss_family = family;
    1580         [ +  + ]:        1126 :         if (addr->ss_family == AF_INET) {
    1581                 :        1002 :                 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
    1582                 :             : 
    1583         [ +  + ]:        1002 :                 if (info->family == AF_INET)
    1584                 :         981 :                         in_addr->sin_addr = info->addr;
    1585                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1586         [ +  - ]:          21 :                 else if (ipv6_addr_v4mapped(&info->addr6))
    1587                 :          21 :                         in_addr->sin_addr.s_addr = info->addr6.s6_addr32[3];
    1588                 :             : #endif
    1589                 :        1002 :                 in_addr->sin_port = info->port;
    1590                 :             :         }
    1591                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1592         [ +  - ]:         124 :         else if (addr->ss_family == AF_INET6) {
    1593                 :         124 :                 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)addr;
    1594                 :             : 
    1595         [ +  + ]:         124 :                 if (info->family == AF_INET)
    1596         [ -  + ]:           2 :                         ipv6_addr_set_v4mapped(info->addr.s_addr,
    1597                 :             :                                                &in6_addr->sin6_addr);
    1598                 :             :                 else
    1599                 :         122 :                         in6_addr->sin6_addr = info->addr6;
    1600                 :         124 :                 in6_addr->sin6_port = info->port;
    1601                 :             :         }
    1602                 :             : #endif
    1603                 :        1126 : }
    1604                 :             : 
    1605                 :         598 : int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local,
    1606                 :             :                             const struct mptcp_addr_info *remote)
    1607                 :             : {
    1608         [ -  + ]:         598 :         struct mptcp_sock *msk = mptcp_sk(sk);
    1609                 :         598 :         struct mptcp_subflow_context *subflow;
    1610                 :         598 :         int local_id = local->addr.id;
    1611                 :         598 :         struct sockaddr_storage addr;
    1612                 :         598 :         int remote_id = remote->id;
    1613                 :         598 :         int err = -ENOTCONN;
    1614                 :         598 :         struct socket *sf;
    1615                 :         598 :         struct sock *ssk;
    1616                 :         598 :         u32 remote_token;
    1617                 :         598 :         int addrlen;
    1618                 :             : 
    1619                 :             :         /* The userspace PM sent the request too early? */
    1620         [ -  + ]:         598 :         if (!mptcp_is_fully_established(sk))
    1621                 :           0 :                 goto err_out;
    1622                 :             : 
    1623                 :         598 :         err = mptcp_subflow_create_socket(sk, local->addr.family, &sf);
    1624         [ +  + ]:         598 :         if (err) {
    1625         [ +  - ]:          45 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCREATSKERR);
    1626         [ -  + ]:          45 :                 pr_debug("msk=%p local=%d remote=%d create sock error: %d\n",
    1627                 :             :                          msk, local_id, remote_id, err);
    1628                 :          45 :                 goto err_out;
    1629                 :             :         }
    1630                 :             : 
    1631                 :         553 :         ssk = sf->sk;
    1632                 :         553 :         subflow = mptcp_subflow_ctx(ssk);
    1633                 :         553 :         do {
    1634                 :         553 :                 get_random_bytes(&subflow->local_nonce, sizeof(u32));
    1635         [ -  + ]:         553 :         } while (!subflow->local_nonce);
    1636                 :             : 
    1637                 :             :         /* if 'IPADDRANY', the ID will be set later, after the routing */
    1638         [ +  + ]:         553 :         if (local->addr.family == AF_INET) {
    1639         [ +  + ]:         491 :                 if (!local->addr.addr.s_addr)
    1640                 :             :                         local_id = -1;
    1641                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1642         [ +  + ]:          62 :         } else if (sk->sk_family == AF_INET6) {
    1643         [ +  + ]:          60 :                 if (ipv6_addr_any(&local->addr.addr6))
    1644                 :             :                         local_id = -1;
    1645                 :             : #endif
    1646                 :             :         }
    1647                 :             : 
    1648                 :         301 :         if (local_id >= 0)
    1649                 :         301 :                 subflow_set_local_id(subflow, local_id);
    1650                 :             : 
    1651                 :         553 :         subflow->remote_key_valid = 1;
    1652                 :         553 :         subflow->remote_key = READ_ONCE(msk->remote_key);
    1653                 :         553 :         subflow->local_key = READ_ONCE(msk->local_key);
    1654                 :         553 :         subflow->token = msk->token;
    1655                 :         553 :         mptcp_info2sockaddr(&local->addr, &addr, ssk->sk_family);
    1656                 :             : 
    1657                 :         553 :         addrlen = sizeof(struct sockaddr_in);
    1658                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1659         [ +  + ]:         553 :         if (addr.ss_family == AF_INET6)
    1660                 :          62 :                 addrlen = sizeof(struct sockaddr_in6);
    1661                 :             : #endif
    1662                 :         553 :         ssk->sk_bound_dev_if = local->ifindex;
    1663                 :         553 :         err = kernel_bind(sf, (struct sockaddr *)&addr, addrlen);
    1664         [ +  + ]:         553 :         if (err) {
    1665         [ +  - ]:           2 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXBINDERR);
    1666         [ -  + ]:           2 :                 pr_debug("msk=%p local=%d remote=%d bind error: %d\n",
    1667                 :             :                          msk, local_id, remote_id, err);
    1668                 :           2 :                 goto failed;
    1669                 :             :         }
    1670                 :             : 
    1671                 :         551 :         mptcp_crypto_key_sha(subflow->remote_key, &remote_token, NULL);
    1672         [ -  + ]:         551 :         pr_debug("msk=%p remote_token=%u local_id=%d remote_id=%d\n", msk,
    1673                 :             :                  remote_token, local_id, remote_id);
    1674                 :         551 :         subflow->remote_token = remote_token;
    1675                 :         551 :         WRITE_ONCE(subflow->remote_id, remote_id);
    1676                 :         551 :         subflow->request_join = 1;
    1677                 :         551 :         subflow->request_bkup = !!(local->flags & MPTCP_PM_ADDR_FLAG_BACKUP);
    1678                 :         551 :         subflow->subflow_id = msk->subflow_id++;
    1679                 :         551 :         mptcp_info2sockaddr(remote, &addr, ssk->sk_family);
    1680                 :             : 
    1681                 :         551 :         sock_hold(ssk);
    1682                 :         551 :         list_add_tail(&subflow->node, &msk->conn_list);
    1683                 :         551 :         err = kernel_connect(sf, (struct sockaddr *)&addr, addrlen, O_NONBLOCK);
    1684         [ +  + ]:         551 :         if (err && err != -EINPROGRESS) {
    1685         [ +  - ]:          26 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTXCONNECTERR);
    1686         [ -  + ]:          26 :                 pr_debug("msk=%p local=%d remote=%d connect error: %d\n",
    1687                 :             :                          msk, local_id, remote_id, err);
    1688                 :          26 :                 goto failed_unlink;
    1689                 :             :         }
    1690                 :             : 
    1691         [ +  - ]:         525 :         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNTX);
    1692                 :             : 
    1693                 :             :         /* discard the subflow socket */
    1694                 :         525 :         mptcp_sock_graft(ssk, sk->sk_socket);
    1695                 :         525 :         iput(SOCK_INODE(sf));
    1696                 :         525 :         mptcp_stop_tout_timer(sk);
    1697                 :         525 :         return 0;
    1698                 :             : 
    1699                 :           0 : failed_unlink:
    1700                 :          26 :         list_del(&subflow->node);
    1701                 :          26 :         sock_put(mptcp_subflow_tcp_sock(subflow));
    1702                 :             : 
    1703                 :          28 : failed:
    1704                 :          28 :         subflow->disposable = 1;
    1705                 :          28 :         sock_release(sf);
    1706                 :             : 
    1707                 :          73 : err_out:
    1708                 :             :         /* we account subflows before the creation, and this failures will not
    1709                 :             :          * be caught by sk_state_change()
    1710                 :             :          */
    1711                 :          73 :         mptcp_pm_close_subflow(msk);
    1712                 :          73 :         return err;
    1713                 :             : }
    1714                 :             : 
    1715                 :        3553 : static void mptcp_attach_cgroup(struct sock *parent, struct sock *child)
    1716                 :             : {
    1717                 :             : #ifdef CONFIG_SOCK_CGROUP_DATA
    1718                 :        3553 :         struct sock_cgroup_data *parent_skcd = &parent->sk_cgrp_data,
    1719                 :        3553 :                                 *child_skcd = &child->sk_cgrp_data;
    1720                 :             : 
    1721                 :             :         /* only the additional subflows created by kworkers have to be modified */
    1722         [ +  + ]:        3553 :         if (cgroup_id(sock_cgroup_ptr(parent_skcd)) !=
    1723         [ +  + ]:         156 :             cgroup_id(sock_cgroup_ptr(child_skcd))) {
    1724                 :          42 :                 cgroup_sk_free(child_skcd);
    1725                 :          42 :                 *child_skcd = *parent_skcd;
    1726                 :          42 :                 cgroup_sk_clone(child_skcd);
    1727                 :             :         }
    1728                 :             : #endif /* CONFIG_SOCK_CGROUP_DATA */
    1729                 :             : 
    1730                 :        3553 :         if (mem_cgroup_sockets_enabled)
    1731                 :             :                 mem_cgroup_sk_inherit(parent, child);
    1732                 :        3553 : }
    1733                 :             : 
    1734                 :         156 : static void mptcp_subflow_ops_override(struct sock *ssk)
    1735                 :             : {
    1736                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1737                 :        3553 :         if (ssk->sk_prot == &tcpv6_prot)
    1738                 :        1192 :                 ssk->sk_prot = &tcpv6_prot_override;
    1739                 :             :         else
    1740                 :             : #endif
    1741                 :        2361 :                 ssk->sk_prot = &tcp_prot_override;
    1742                 :             : }
    1743                 :             : 
    1744                 :         258 : static void mptcp_subflow_ops_undo_override(struct sock *ssk)
    1745                 :             : {
    1746                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1747         [ +  + ]:         186 :         if (ssk->sk_prot == &tcpv6_prot_override)
    1748                 :        2209 :                 ssk->sk_prot = &tcpv6_prot;
    1749                 :             :         else
    1750                 :             : #endif
    1751                 :        3268 :                 ssk->sk_prot = &tcp_prot;
    1752                 :             : }
    1753                 :             : 
    1754                 :        3598 : int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
    1755                 :             :                                 struct socket **new_sock)
    1756                 :             : {
    1757                 :        3598 :         struct mptcp_subflow_context *subflow;
    1758         [ +  + ]:        3598 :         struct net *net = sock_net(sk);
    1759                 :        3598 :         struct socket *sf;
    1760                 :        3598 :         int err;
    1761                 :             : 
    1762                 :             :         /* un-accepted server sockets can reach here - on bad configuration
    1763                 :             :          * bail early to avoid greater trouble later
    1764                 :             :          */
    1765         [ +  + ]:        3598 :         if (unlikely(!sk->sk_socket))
    1766                 :             :                 return -EINVAL;
    1767                 :             : 
    1768                 :        3553 :         err = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, &sf);
    1769         [ +  - ]:        3553 :         if (err)
    1770                 :             :                 return err;
    1771                 :             : 
    1772                 :        3553 :         lock_sock_nested(sf->sk, SINGLE_DEPTH_NESTING);
    1773                 :             : 
    1774                 :        3553 :         err = security_mptcp_add_subflow(sk, sf->sk);
    1775         [ -  + ]:        3553 :         if (err)
    1776                 :           0 :                 goto err_free;
    1777                 :             : 
    1778                 :             :         /* the newly created socket has to be in the same cgroup as its parent */
    1779                 :        3553 :         mptcp_attach_cgroup(sk, sf->sk);
    1780                 :             : 
    1781                 :             :         /* kernel sockets do not by default acquire net ref, but TCP timer
    1782                 :             :          * needs it.
    1783                 :             :          * Update ns_tracker to current stack trace and refcounted tracker.
    1784                 :             :          */
    1785                 :        3553 :         sk_net_refcnt_upgrade(sf->sk);
    1786                 :        3553 :         err = tcp_set_ulp(sf->sk, "mptcp");
    1787         [ -  + ]:        3553 :         if (err)
    1788                 :           0 :                 goto err_free;
    1789                 :             : 
    1790         [ -  + ]:        3553 :         mptcp_sockopt_sync_locked(mptcp_sk(sk), sf->sk);
    1791                 :        3553 :         release_sock(sf->sk);
    1792                 :             : 
    1793                 :             :         /* the newly created socket really belongs to the owning MPTCP
    1794                 :             :          * socket, even if for additional subflows the allocation is performed
    1795                 :             :          * by a kernel workqueue. Adjust inode references, so that the
    1796                 :             :          * procfs/diag interfaces really show this one belonging to the correct
    1797                 :             :          * user.
    1798                 :             :          */
    1799         [ -  + ]:        3553 :         SOCK_INODE(sf)->i_ino = SOCK_INODE(sk->sk_socket)->i_ino;
    1800                 :        3553 :         SOCK_INODE(sf)->i_uid = SOCK_INODE(sk->sk_socket)->i_uid;
    1801                 :        3553 :         SOCK_INODE(sf)->i_gid = SOCK_INODE(sk->sk_socket)->i_gid;
    1802                 :             : 
    1803         [ -  + ]:        3553 :         subflow = mptcp_subflow_ctx(sf->sk);
    1804         [ -  + ]:        3553 :         pr_debug("subflow=%p\n", subflow);
    1805                 :             : 
    1806                 :        3553 :         *new_sock = sf;
    1807                 :        3553 :         sock_hold(sk);
    1808                 :        3553 :         subflow->conn = sk;
    1809         [ +  + ]:        3553 :         mptcp_subflow_ops_override(sf->sk);
    1810                 :             : 
    1811                 :             :         return 0;
    1812                 :             : 
    1813                 :           0 : err_free:
    1814                 :           0 :         release_sock(sf->sk);
    1815                 :           0 :         sock_release(sf);
    1816                 :           0 :         return err;
    1817                 :             : }
    1818                 :             : 
    1819                 :        5463 : static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
    1820                 :             :                                                         gfp_t priority)
    1821                 :             : {
    1822                 :        5463 :         struct inet_connection_sock *icsk = inet_csk(sk);
    1823                 :        5463 :         struct mptcp_subflow_context *ctx;
    1824                 :             : 
    1825                 :        5463 :         ctx = kzalloc(sizeof(*ctx), priority);
    1826         [ +  - ]:        5463 :         if (!ctx)
    1827                 :             :                 return NULL;
    1828                 :             : 
    1829                 :        5463 :         rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
    1830         [ -  + ]:        5463 :         INIT_LIST_HEAD(&ctx->node);
    1831                 :        5463 :         INIT_LIST_HEAD(&ctx->delegated_node);
    1832                 :             : 
    1833         [ -  + ]:        5463 :         pr_debug("subflow=%p\n", ctx);
    1834                 :             : 
    1835                 :        5463 :         ctx->tcp_sock = sk;
    1836                 :        5463 :         WRITE_ONCE(ctx->local_id, -1);
    1837                 :             : 
    1838                 :        5463 :         return ctx;
    1839                 :             : }
    1840                 :             : 
    1841                 :        9655 : static void __subflow_state_change(struct sock *sk)
    1842                 :             : {
    1843                 :        9655 :         struct socket_wq *wq;
    1844                 :             : 
    1845                 :        9655 :         rcu_read_lock();
    1846   [ +  -  -  +  :        9655 :         wq = rcu_dereference(sk->sk_wq);
          -  -  -  -  -  
                      - ]
    1847         [ +  + ]:        9655 :         if (skwq_has_sleeper(wq))
    1848                 :        1409 :                 wake_up_interruptible_all(&wq->wait);
    1849                 :        9655 :         rcu_read_unlock();
    1850                 :        9655 : }
    1851                 :             : 
    1852                 :        9655 : static void subflow_state_change(struct sock *sk)
    1853                 :             : {
    1854                 :        9655 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
    1855                 :        9655 :         struct sock *parent = subflow->conn;
    1856                 :             : 
    1857                 :        9655 :         __subflow_state_change(sk);
    1858                 :             : 
    1859         [ +  + ]:        9655 :         if (subflow_simultaneous_connect(sk)) {
    1860         [ -  + ]:           6 :                 WARN_ON_ONCE(!mptcp_try_fallback(sk, MPTCP_MIB_SIMULTCONNFALLBACK));
    1861                 :           6 :                 subflow->conn_finished = 1;
    1862                 :           6 :                 mptcp_propagate_state(parent, sk, subflow, NULL);
    1863                 :             :         }
    1864                 :             : 
    1865                 :             :         /* as recvmsg() does not acquire the subflow socket for ssk selection
    1866                 :             :          * a fin packet carrying a DSS can be unnoticed if we don't trigger
    1867                 :             :          * the data available machinery here.
    1868                 :             :          */
    1869         [ +  + ]:        9655 :         if (mptcp_subflow_data_available(sk))
    1870                 :         152 :                 mptcp_data_ready(parent, sk);
    1871         [ +  + ]:        9503 :         else if (unlikely(sk->sk_err))
    1872                 :         480 :                 subflow_error_report(sk);
    1873                 :             : 
    1874         [ -  + ]:        9655 :         subflow_sched_work_if_closed(mptcp_sk(parent), sk);
    1875                 :        9655 : }
    1876                 :             : 
    1877                 :        1488 : void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk)
    1878                 :             : {
    1879                 :        1488 :         struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
    1880                 :        1488 :         struct request_sock *req, *head, *tail;
    1881                 :        1488 :         struct mptcp_subflow_context *subflow;
    1882                 :        1488 :         struct sock *sk, *ssk;
    1883                 :             : 
    1884                 :             :         /* Due to lock dependencies no relevant lock can be acquired under rskq_lock.
    1885                 :             :          * Splice the req list, so that accept() can not reach the pending ssk after
    1886                 :             :          * the listener socket is released below.
    1887                 :             :          */
    1888                 :        1488 :         spin_lock_bh(&queue->rskq_lock);
    1889                 :        1488 :         head = queue->rskq_accept_head;
    1890                 :        1488 :         tail = queue->rskq_accept_tail;
    1891                 :        1488 :         queue->rskq_accept_head = NULL;
    1892                 :        1488 :         queue->rskq_accept_tail = NULL;
    1893                 :        1488 :         spin_unlock_bh(&queue->rskq_lock);
    1894                 :             : 
    1895         [ +  + ]:        1488 :         if (!head)
    1896                 :             :                 return;
    1897                 :             : 
    1898                 :             :         /* can't acquire the msk socket lock under the subflow one,
    1899                 :             :          * or will cause ABBA deadlock
    1900                 :             :          */
    1901                 :          36 :         release_sock(listener_ssk);
    1902                 :             : 
    1903         [ +  + ]:         108 :         for (req = head; req; req = req->dl_next) {
    1904                 :          36 :                 ssk = req->sk;
    1905   [ -  +  -  + ]:          36 :                 if (!sk_is_mptcp(ssk))
                 [ -  + ]
    1906                 :           0 :                         continue;
    1907                 :             : 
    1908         [ +  - ]:          36 :                 subflow = mptcp_subflow_ctx(ssk);
    1909   [ +  -  -  + ]:          36 :                 if (!subflow || !subflow->conn)
    1910                 :           0 :                         continue;
    1911                 :             : 
    1912                 :          36 :                 sk = subflow->conn;
    1913                 :          36 :                 sock_hold(sk);
    1914                 :             : 
    1915                 :          36 :                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
    1916                 :          36 :                 __mptcp_unaccepted_force_close(sk);
    1917                 :          36 :                 release_sock(sk);
    1918                 :             : 
    1919                 :             :                 /* lockdep will report a false positive ABBA deadlock
    1920                 :             :                  * between cancel_work_sync and the listener socket.
    1921                 :             :                  * The involved locks belong to different sockets WRT
    1922                 :             :                  * the existing AB chain.
    1923                 :             :                  * Using a per socket key is problematic as key
    1924                 :             :                  * deregistration requires process context and must be
    1925                 :             :                  * performed at socket disposal time, in atomic
    1926                 :             :                  * context.
    1927                 :             :                  * Just tell lockdep to consider the listener socket
    1928                 :             :                  * released here.
    1929                 :             :                  */
    1930                 :          36 :                 mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_);
    1931                 :          36 :                 mptcp_cancel_work(sk);
    1932                 :          36 :                 mutex_acquire(&listener_sk->sk_lock.dep_map, 0, 0, _RET_IP_);
    1933                 :             : 
    1934                 :          36 :                 sock_put(sk);
    1935                 :             :         }
    1936                 :             : 
    1937                 :             :         /* we are still under the listener msk socket lock */
    1938                 :          36 :         lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
    1939                 :             : 
    1940                 :             :         /* restore the listener queue, to let the TCP code clean it up */
    1941                 :          36 :         spin_lock_bh(&queue->rskq_lock);
    1942         [ -  + ]:          36 :         WARN_ON_ONCE(queue->rskq_accept_head);
    1943                 :          36 :         queue->rskq_accept_head = head;
    1944                 :          36 :         queue->rskq_accept_tail = tail;
    1945                 :          36 :         spin_unlock_bh(&queue->rskq_lock);
    1946                 :             : }
    1947                 :             : 
    1948                 :        4169 : static int subflow_ulp_init(struct sock *sk)
    1949                 :             : {
    1950                 :        4169 :         struct inet_connection_sock *icsk = inet_csk(sk);
    1951                 :        4169 :         struct mptcp_subflow_context *ctx;
    1952         [ -  + ]:        4169 :         struct tcp_sock *tp = tcp_sk(sk);
    1953                 :        4169 :         int err = 0;
    1954                 :             : 
    1955                 :             :         /* disallow attaching ULP to a socket unless it has been
    1956                 :             :          * created with sock_create_kern()
    1957                 :             :          */
    1958         [ +  + ]:        4169 :         if (!sk->sk_kern_sock) {
    1959                 :         616 :                 err = -EOPNOTSUPP;
    1960                 :         616 :                 goto out;
    1961                 :             :         }
    1962                 :             : 
    1963                 :        3553 :         ctx = subflow_create_ctx(sk, GFP_KERNEL);
    1964         [ -  + ]:        3553 :         if (!ctx) {
    1965                 :           0 :                 err = -ENOMEM;
    1966                 :           0 :                 goto out;
    1967                 :             :         }
    1968                 :             : 
    1969         [ -  + ]:        3553 :         pr_debug("subflow=%p, family=%d\n", ctx, sk->sk_family);
    1970                 :             : 
    1971                 :        3553 :         tp->is_mptcp = 1;
    1972                 :        3553 :         ctx->icsk_af_ops = icsk->icsk_af_ops;
    1973         [ +  + ]:        3553 :         icsk->icsk_af_ops = subflow_default_af_ops(sk);
    1974                 :        3553 :         ctx->tcp_state_change = sk->sk_state_change;
    1975                 :        3553 :         ctx->tcp_error_report = sk->sk_error_report;
    1976                 :             : 
    1977         [ -  + ]:        3553 :         WARN_ON_ONCE(sk->sk_data_ready != sock_def_readable);
    1978         [ -  + ]:        3553 :         WARN_ON_ONCE(sk->sk_write_space != sk_stream_write_space);
    1979                 :             : 
    1980                 :        3553 :         sk->sk_data_ready = subflow_data_ready;
    1981                 :        3553 :         sk->sk_write_space = subflow_write_space;
    1982                 :        3553 :         sk->sk_state_change = subflow_state_change;
    1983                 :        3553 :         sk->sk_error_report = subflow_error_report;
    1984                 :        4169 : out:
    1985                 :        4169 :         return err;
    1986                 :             : }
    1987                 :             : 
    1988                 :        5299 : static void subflow_ulp_release(struct sock *ssk)
    1989                 :             : {
    1990         [ +  - ]:        5299 :         struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
    1991                 :        5299 :         bool release = true;
    1992                 :        5299 :         struct sock *sk;
    1993                 :             : 
    1994         [ +  - ]:        5299 :         if (!ctx)
    1995                 :             :                 return;
    1996                 :             : 
    1997                 :        5299 :         sk = ctx->conn;
    1998         [ +  - ]:        5299 :         if (sk) {
    1999                 :             :                 /* if the msk has been orphaned, keep the ctx
    2000                 :             :                  * alive, will be freed by __mptcp_close_ssk(),
    2001                 :             :                  * when the subflow is still unaccepted
    2002                 :             :                  */
    2003   [ -  +  -  - ]:        5299 :                 release = ctx->disposable || list_empty(&ctx->node);
    2004                 :             : 
    2005                 :             :                 /* inet_child_forget() does not call sk_state_change(),
    2006                 :             :                  * explicitly trigger the socket close machinery
    2007                 :             :                  */
    2008         [ #  # ]:           0 :                 if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW,
    2009         [ #  # ]:           0 :                                                   &mptcp_sk(sk)->flags))
    2010                 :           0 :                         mptcp_schedule_work(sk);
    2011                 :        5299 :                 sock_put(sk);
    2012                 :             :         }
    2013                 :             : 
    2014         [ +  + ]:        5299 :         mptcp_subflow_ops_undo_override(ssk);
    2015         [ +  - ]:        5299 :         if (release)
    2016                 :        5299 :                 kfree_rcu(ctx, rcu);
    2017                 :             : }
    2018                 :             : 
    2019                 :        2034 : static void subflow_ulp_clone(const struct request_sock *req,
    2020                 :             :                               struct sock *newsk,
    2021                 :             :                               const gfp_t priority)
    2022                 :             : {
    2023                 :        2034 :         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
    2024         [ +  + ]:        2034 :         struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);
    2025                 :        2034 :         struct mptcp_subflow_context *new_ctx;
    2026                 :             : 
    2027   [ -  +  +  - ]:        2034 :         if (!tcp_rsk(req)->is_mptcp ||
                 [ +  - ]
    2028         [ +  + ]:        2034 :             (!subflow_req->mp_capable && !subflow_req->mp_join)) {
    2029                 :         124 :                 subflow_ulp_fallback(newsk, old_ctx);
    2030                 :         124 :                 return;
    2031                 :             :         }
    2032                 :             : 
    2033                 :        1910 :         new_ctx = subflow_create_ctx(newsk, priority);
    2034         [ -  + ]:        1910 :         if (!new_ctx) {
    2035                 :           0 :                 subflow_ulp_fallback(newsk, old_ctx);
    2036                 :           0 :                 return;
    2037                 :             :         }
    2038                 :             : 
    2039                 :        1910 :         new_ctx->conn_finished = 1;
    2040                 :        1910 :         new_ctx->icsk_af_ops = old_ctx->icsk_af_ops;
    2041                 :        1910 :         new_ctx->tcp_state_change = old_ctx->tcp_state_change;
    2042                 :        1910 :         new_ctx->tcp_error_report = old_ctx->tcp_error_report;
    2043                 :        1910 :         new_ctx->rel_write_seq = 1;
    2044                 :             : 
    2045         [ +  + ]:        1910 :         if (subflow_req->mp_capable) {
    2046                 :             :                 /* see comments in subflow_syn_recv_sock(), MPTCP connection
    2047                 :             :                  * is fully established only after we receive the remote key
    2048                 :             :                  */
    2049                 :        1402 :                 new_ctx->mp_capable = 1;
    2050                 :        1402 :                 new_ctx->local_key = subflow_req->local_key;
    2051                 :        1402 :                 new_ctx->token = subflow_req->token;
    2052                 :        1402 :                 new_ctx->ssn_offset = subflow_req->ssn_offset;
    2053                 :        1402 :                 new_ctx->idsn = subflow_req->idsn;
    2054                 :             : 
    2055                 :             :                 /* this is the first subflow, id is always 0 */
    2056                 :        1402 :                 subflow_set_local_id(new_ctx, 0);
    2057         [ +  - ]:         508 :         } else if (subflow_req->mp_join) {
    2058                 :         508 :                 new_ctx->ssn_offset = subflow_req->ssn_offset;
    2059                 :         508 :                 new_ctx->mp_join = 1;
    2060                 :         508 :                 WRITE_ONCE(new_ctx->fully_established, true);
    2061                 :         508 :                 new_ctx->remote_key_valid = 1;
    2062                 :         508 :                 new_ctx->backup = subflow_req->backup;
    2063                 :         508 :                 new_ctx->request_bkup = subflow_req->request_bkup;
    2064                 :         508 :                 WRITE_ONCE(new_ctx->remote_id, subflow_req->remote_id);
    2065                 :         508 :                 new_ctx->token = subflow_req->token;
    2066                 :         508 :                 new_ctx->thmac = subflow_req->thmac;
    2067                 :             : 
    2068                 :             :                 /* the subflow req id is valid, fetched via subflow_check_req()
    2069                 :             :                  * and subflow_token_join_request()
    2070                 :             :                  */
    2071                 :         508 :                 subflow_set_local_id(new_ctx, subflow_req->local_id);
    2072                 :             :         }
    2073                 :             : }
    2074                 :             : 
    2075                 :      753755 : static void tcp_release_cb_override(struct sock *ssk)
    2076                 :             : {
    2077                 :      753755 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    2078                 :      753755 :         long status;
    2079                 :             : 
    2080                 :             :         /* process and clear all the pending actions, but leave the subflow into
    2081                 :             :          * the napi queue. To respect locking, only the same CPU that originated
    2082                 :             :          * the action can touch the list. mptcp_napi_poll will take care of it.
    2083                 :             :          */
    2084   [ -  +  -  + ]:      753755 :         status = set_mask_bits(&subflow->delegated_status, MPTCP_DELEGATE_ACTIONS_MASK, 0);
    2085         [ +  + ]:      753755 :         if (status)
    2086                 :        3381 :                 mptcp_subflow_process_delegated(ssk, status);
    2087                 :             : 
    2088                 :      753755 :         tcp_release_cb(ssk);
    2089                 :      753755 : }
    2090                 :             : 
    2091                 :           0 : static int tcp_abort_override(struct sock *ssk, int err)
    2092                 :             : {
    2093                 :             :         /* closing a listener subflow requires a great deal of care.
    2094                 :             :          * keep it simple and just prevent such operation
    2095                 :             :          */
    2096         [ #  # ]:           0 :         if (inet_sk_state_load(ssk) == TCP_LISTEN)
    2097                 :             :                 return -EINVAL;
    2098                 :             : 
    2099                 :           0 :         return tcp_abort(ssk, err);
    2100                 :             : }
    2101                 :             : 
    2102                 :             : static struct tcp_ulp_ops subflow_ulp_ops __read_mostly = {
    2103                 :             :         .name           = "mptcp",
    2104                 :             :         .owner          = THIS_MODULE,
    2105                 :             :         .init           = subflow_ulp_init,
    2106                 :             :         .release        = subflow_ulp_release,
    2107                 :             :         .clone          = subflow_ulp_clone,
    2108                 :             : };
    2109                 :             : 
    2110                 :           8 : static int subflow_ops_init(struct request_sock_ops *subflow_ops)
    2111                 :             : {
    2112                 :           8 :         subflow_ops->obj_size = sizeof(struct mptcp_subflow_request_sock);
    2113                 :             : 
    2114                 :           8 :         subflow_ops->slab = kmem_cache_create(subflow_ops->slab_name,
    2115                 :             :                                               subflow_ops->obj_size, 0,
    2116                 :             :                                               SLAB_ACCOUNT |
    2117                 :             :                                               SLAB_TYPESAFE_BY_RCU,
    2118                 :             :                                               NULL);
    2119         [ -  + ]:           8 :         if (!subflow_ops->slab)
    2120                 :           0 :                 return -ENOMEM;
    2121                 :             : 
    2122                 :             :         return 0;
    2123                 :             : }
    2124                 :             : 
    2125                 :           4 : void __init mptcp_subflow_init(void)
    2126                 :             : {
    2127                 :           4 :         mptcp_subflow_v4_request_sock_ops = tcp_request_sock_ops;
    2128                 :           4 :         mptcp_subflow_v4_request_sock_ops.slab_name = "request_sock_subflow_v4";
    2129                 :           4 :         mptcp_subflow_v4_request_sock_ops.destructor = subflow_v4_req_destructor;
    2130                 :             : 
    2131         [ -  + ]:           4 :         if (subflow_ops_init(&mptcp_subflow_v4_request_sock_ops) != 0)
    2132                 :           0 :                 panic("MPTCP: failed to init subflow v4 request sock ops\n");
    2133                 :             : 
    2134                 :           4 :         subflow_request_sock_ipv4_ops = tcp_request_sock_ipv4_ops;
    2135                 :           4 :         subflow_request_sock_ipv4_ops.route_req = subflow_v4_route_req;
    2136                 :           4 :         subflow_request_sock_ipv4_ops.send_synack = subflow_v4_send_synack;
    2137                 :             : 
    2138                 :           4 :         subflow_specific = ipv4_specific;
    2139                 :           4 :         subflow_specific.conn_request = subflow_v4_conn_request;
    2140                 :           4 :         subflow_specific.syn_recv_sock = subflow_syn_recv_sock;
    2141                 :           4 :         subflow_specific.sk_rx_dst_set = subflow_finish_connect;
    2142                 :           4 :         subflow_specific.rebuild_header = subflow_rebuild_header;
    2143                 :             : 
    2144                 :           4 :         tcp_prot_override = tcp_prot;
    2145                 :           4 :         tcp_prot_override.release_cb = tcp_release_cb_override;
    2146                 :           4 :         tcp_prot_override.diag_destroy = tcp_abort_override;
    2147                 :             : 
    2148                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    2149                 :             :         /* In struct mptcp_subflow_request_sock, we assume the TCP request sock
    2150                 :             :          * structures for v4 and v6 have the same size. It should not changed in
    2151                 :             :          * the future but better to make sure to be warned if it is no longer
    2152                 :             :          * the case.
    2153                 :             :          */
    2154                 :           4 :         BUILD_BUG_ON(sizeof(struct tcp_request_sock) != sizeof(struct tcp6_request_sock));
    2155                 :             : 
    2156                 :           4 :         mptcp_subflow_v6_request_sock_ops = tcp6_request_sock_ops;
    2157                 :           4 :         mptcp_subflow_v6_request_sock_ops.slab_name = "request_sock_subflow_v6";
    2158                 :           4 :         mptcp_subflow_v6_request_sock_ops.destructor = subflow_v6_req_destructor;
    2159                 :             : 
    2160         [ -  + ]:           4 :         if (subflow_ops_init(&mptcp_subflow_v6_request_sock_ops) != 0)
    2161                 :           0 :                 panic("MPTCP: failed to init subflow v6 request sock ops\n");
    2162                 :             : 
    2163                 :           4 :         subflow_request_sock_ipv6_ops = tcp_request_sock_ipv6_ops;
    2164                 :           4 :         subflow_request_sock_ipv6_ops.route_req = subflow_v6_route_req;
    2165                 :           4 :         subflow_request_sock_ipv6_ops.send_synack = subflow_v6_send_synack;
    2166                 :             : 
    2167                 :           4 :         subflow_v6_specific = ipv6_specific;
    2168                 :           4 :         subflow_v6_specific.conn_request = subflow_v6_conn_request;
    2169                 :           4 :         subflow_v6_specific.syn_recv_sock = subflow_syn_recv_sock;
    2170                 :           4 :         subflow_v6_specific.sk_rx_dst_set = subflow_finish_connect;
    2171                 :           4 :         subflow_v6_specific.rebuild_header = subflow_v6_rebuild_header;
    2172                 :             : 
    2173                 :           4 :         subflow_v6m_specific = subflow_v6_specific;
    2174                 :           4 :         subflow_v6m_specific.queue_xmit = ipv4_specific.queue_xmit;
    2175                 :           4 :         subflow_v6m_specific.send_check = ipv4_specific.send_check;
    2176                 :           4 :         subflow_v6m_specific.net_header_len = ipv4_specific.net_header_len;
    2177                 :           4 :         subflow_v6m_specific.mtu_reduced = ipv4_specific.mtu_reduced;
    2178                 :           4 :         subflow_v6m_specific.rebuild_header = subflow_rebuild_header;
    2179                 :             : 
    2180                 :           4 :         tcpv6_prot_override = tcpv6_prot;
    2181                 :           4 :         tcpv6_prot_override.release_cb = tcp_release_cb_override;
    2182                 :           4 :         tcpv6_prot_override.diag_destroy = tcp_abort_override;
    2183                 :             : #endif
    2184                 :             : 
    2185                 :           4 :         mptcp_diag_subflow_init(&subflow_ulp_ops);
    2186                 :             : 
    2187         [ -  + ]:           4 :         if (tcp_register_ulp(&subflow_ulp_ops) != 0)
    2188                 :           0 :                 panic("MPTCP: failed to register subflows to ULP\n");
    2189                 :           4 : }
        

Generated by: LCOV version 2.3.1-1