LCOV - code coverage report
Current view: top level - mptcp/subflow.c (source / functions) Coverage Total Hit
Test: export Lines: 90.5 % 1146 1037
Test Date: 2026-09-15 21:27:41 Functions: 95.7 % 69 66
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 58.4 % 956 558

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

Generated by: LCOV version 2.3.1-1