LCOV - code coverage report
Current view: top level - mptcp/options.c (source / functions) Coverage Total Hit
Test: export-net Lines: 97.9 % 905 886
Test Date: 2026-05-31 08:04:47 Functions: 100.0 % 29 29
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 71.0 % 797 566

             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 <crypto/sha2.h>
      11                 :             : #include <net/tcp.h>
      12                 :             : #include <net/mptcp.h>
      13                 :             : #include "protocol.h"
      14                 :             : #include "mib.h"
      15                 :             : 
      16                 :             : #include <trace/events/mptcp.h>
      17                 :             : 
      18                 :             : static bool mptcp_cap_flag_sha256(u8 flags)
      19                 :             : {
      20                 :             :         return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
      21                 :             : }
      22                 :             : 
      23                 :     1402359 : static void mptcp_parse_option(const struct sk_buff *skb,
      24                 :             :                                const unsigned char *ptr, int opsize,
      25                 :             :                                struct mptcp_options_received *mp_opt)
      26                 :             : {
      27                 :     1402359 :         u8 subtype = *ptr >> 4;
      28                 :     1402359 :         int expected_opsize;
      29                 :     1402359 :         u16 subopt;
      30                 :     1402359 :         u8 version;
      31                 :     1402359 :         u8 flags;
      32                 :     1402359 :         u8 i;
      33                 :             : 
      34   [ +  +  +  +  :     1402359 :         switch (subtype) {
          +  +  +  +  +  
                      - ]
      35                 :        7389 :         case MPTCPOPT_MP_CAPABLE:
      36                 :             :                 /* strict size checking */
      37         [ +  + ]:        7389 :                 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
      38         [ +  + ]:        4211 :                         if (skb->len > tcp_hdr(skb)->doff << 2)
      39                 :             :                                 expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
      40                 :             :                         else
      41                 :        3196 :                                 expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
      42                 :             :                         subopt = OPTION_MPTCP_MPC_ACK;
      43                 :             :                 } else {
      44         [ +  + ]:        3178 :                         if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) {
      45                 :             :                                 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
      46                 :             :                                 subopt = OPTION_MPTCP_MPC_SYNACK;
      47                 :             :                         } else {
      48                 :        1694 :                                 expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
      49                 :        1694 :                                 subopt = OPTION_MPTCP_MPC_SYN;
      50                 :             :                         }
      51                 :             :                 }
      52                 :             : 
      53                 :             :                 /* Cfr RFC 8684 Section 3.3.0:
      54                 :             :                  * If a checksum is present but its use had
      55                 :             :                  * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
      56                 :             :                  * close the subflow with a RST, as it is not behaving as negotiated.
      57                 :             :                  * If a checksum is not present when its use has been negotiated, the
      58                 :             :                  * receiver MUST close the subflow with a RST, as it is considered
      59                 :             :                  * broken
      60                 :             :                  * We parse even option with mismatching csum presence, so that
      61                 :             :                  * later in subflow_data_ready we can trigger the reset.
      62                 :             :                  */
      63         [ +  + ]:        7389 :                 if (opsize != expected_opsize &&
      64                 :          92 :                     (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
      65         [ +  + ]:          92 :                      opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
      66                 :             :                         break;
      67                 :             : 
      68                 :             :                 /* try to be gentle vs future versions on the initial syn */
      69                 :        7377 :                 version = *ptr++ & MPTCP_VERSION_MASK;
      70         [ +  + ]:        7377 :                 if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
      71         [ +  + ]:        5689 :                         if (version != MPTCP_SUPPORTED_VERSION)
      72                 :             :                                 break;
      73         [ +  - ]:        1688 :                 } else if (version < MPTCP_SUPPORTED_VERSION) {
      74                 :             :                         break;
      75                 :             :                 }
      76                 :             : 
      77                 :        7371 :                 flags = *ptr++;
      78   [ +  +  +  + ]:        7371 :                 if (!mptcp_cap_flag_sha256(flags) ||
      79                 :             :                     (flags & MPTCP_CAP_EXTENSIBILITY))
      80                 :             :                         break;
      81                 :             : 
      82                 :             :                 /* RFC 6824, Section 3.1:
      83                 :             :                  * "For the Checksum Required bit (labeled "A"), if either
      84                 :             :                  * host requires the use of checksums, checksums MUST be used.
      85                 :             :                  * In other words, the only way for checksums not to be used
      86                 :             :                  * is if both hosts in their SYNs set A=0."
      87                 :             :                  */
      88         [ +  + ]:        7335 :                 if (flags & MPTCP_CAP_CHECKSUM_REQD)
      89                 :         556 :                         mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
      90                 :             : 
      91                 :        7335 :                 mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
      92                 :             : 
      93                 :        7335 :                 mp_opt->suboptions |= subopt;
      94         [ +  + ]:        7335 :                 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
      95         [ +  + ]:        5659 :                         mp_opt->sndr_key = get_unaligned_be64(ptr);
      96                 :        5659 :                         ptr += 8;
      97                 :             :                 }
      98         [ +  + ]:        5659 :                 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
      99                 :        4193 :                         mp_opt->rcvr_key = get_unaligned_be64(ptr);
     100                 :        4193 :                         ptr += 8;
     101                 :             :                 }
     102         [ +  + ]:        5869 :                 if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
     103                 :             :                         /* Section 3.1.:
     104                 :             :                          * "the data parameters in a MP_CAPABLE are semantically
     105                 :             :                          * equivalent to those in a DSS option and can be used
     106                 :             :                          * interchangeably."
     107                 :             :                          */
     108                 :        1015 :                         mp_opt->suboptions |= OPTION_MPTCP_DSS;
     109                 :        1015 :                         mp_opt->use_map = 1;
     110                 :        1015 :                         mp_opt->mpc_map = 1;
     111         [ +  + ]:        1015 :                         mp_opt->data_len = get_unaligned_be16(ptr);
     112                 :        1015 :                         ptr += 2;
     113                 :             :                 }
     114         [ +  + ]:        1015 :                 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
     115                 :          80 :                         mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
     116                 :          80 :                         mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
     117                 :          80 :                         ptr += 2;
     118                 :             :                 }
     119   [ -  +  -  - ]:        7335 :                 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u\n",
     120                 :             :                          version, flags, opsize, mp_opt->sndr_key,
     121                 :             :                          mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
     122                 :             :                 break;
     123                 :             : 
     124                 :        1958 :         case MPTCPOPT_MP_JOIN:
     125         [ +  + ]:        1958 :                 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
     126                 :         500 :                         mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYN;
     127                 :         500 :                         mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
     128                 :         500 :                         mp_opt->join_id = *ptr++;
     129         [ -  + ]:         500 :                         mp_opt->token = get_unaligned_be32(ptr);
     130                 :         500 :                         ptr += 4;
     131                 :         500 :                         mp_opt->nonce = get_unaligned_be32(ptr);
     132                 :         500 :                         ptr += 4;
     133   [ -  +  -  - ]:         500 :                         pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u\n",
     134                 :             :                                  mp_opt->backup, mp_opt->join_id,
     135                 :             :                                  mp_opt->token, mp_opt->nonce);
     136         [ +  + ]:        1458 :                 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
     137                 :         460 :                         mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYNACK;
     138                 :         460 :                         mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
     139                 :         460 :                         mp_opt->join_id = *ptr++;
     140         [ -  + ]:         460 :                         mp_opt->thmac = get_unaligned_be64(ptr);
     141                 :         460 :                         ptr += 8;
     142         [ -  + ]:         460 :                         mp_opt->nonce = get_unaligned_be32(ptr);
     143                 :         460 :                         ptr += 4;
     144   [ -  +  -  - ]:         460 :                         pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
     145                 :             :                                  mp_opt->backup, mp_opt->join_id,
     146                 :             :                                  mp_opt->thmac, mp_opt->nonce);
     147         [ +  - ]:         998 :                 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
     148                 :         998 :                         mp_opt->suboptions |= OPTION_MPTCP_MPJ_ACK;
     149                 :         998 :                         ptr += 2;
     150                 :         998 :                         memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
     151   [ -  +  -  - ]:         998 :                         pr_debug("MP_JOIN hmac\n");
     152                 :             :                 }
     153                 :             :                 break;
     154                 :             : 
     155                 :             :         case MPTCPOPT_DSS:
     156   [ -  +  -  - ]:     1391520 :                 pr_debug("DSS\n");
     157                 :     1391520 :                 ptr++;
     158                 :             : 
     159                 :     1391520 :                 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
     160                 :     1391520 :                 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
     161                 :     1391520 :                 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
     162                 :     1391520 :                 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
     163                 :     1391520 :                 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
     164                 :     1391520 :                 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
     165                 :             : 
     166   [ -  +  -  - ]:     1391520 :                 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d\n",
     167                 :             :                          mp_opt->data_fin, mp_opt->dsn64,
     168                 :             :                          mp_opt->use_map, mp_opt->ack64,
     169                 :             :                          mp_opt->use_ack);
     170                 :             : 
     171                 :     1391520 :                 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
     172                 :             : 
     173         [ +  - ]:     1391520 :                 if (mp_opt->use_ack) {
     174         [ +  + ]:     1391520 :                         if (mp_opt->ack64)
     175                 :             :                                 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
     176                 :             :                         else
     177                 :      131013 :                                 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
     178                 :             :                 }
     179                 :             : 
     180         [ +  + ]:     1391520 :                 if (mp_opt->use_map) {
     181         [ +  + ]:      843278 :                         if (mp_opt->dsn64)
     182                 :      843236 :                                 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
     183                 :             :                         else
     184                 :          42 :                                 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
     185                 :             :                 }
     186                 :             : 
     187                 :             :                 /* Always parse any csum presence combination, we will enforce
     188                 :             :                  * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
     189                 :             :                  */
     190         [ +  + ]:     1391520 :                 if (opsize != expected_opsize &&
     191         [ +  - ]:       76342 :                     opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
     192                 :             :                         break;
     193                 :             : 
     194                 :     1391520 :                 mp_opt->suboptions |= OPTION_MPTCP_DSS;
     195         [ +  - ]:     1391520 :                 if (mp_opt->use_ack) {
     196         [ +  + ]:     1391520 :                         if (mp_opt->ack64) {
     197                 :     1260507 :                                 mp_opt->data_ack = get_unaligned_be64(ptr);
     198                 :     1260507 :                                 ptr += 8;
     199                 :             :                         } else {
     200                 :      131013 :                                 mp_opt->data_ack = get_unaligned_be32(ptr);
     201                 :      131013 :                                 ptr += 4;
     202                 :             :                         }
     203                 :             : 
     204   [ -  +  -  - ]:     1391520 :                         pr_debug("data_ack=%llu\n", mp_opt->data_ack);
     205                 :             :                 }
     206                 :             : 
     207         [ +  + ]:     1391520 :                 if (mp_opt->use_map) {
     208         [ +  + ]:      843278 :                         if (mp_opt->dsn64) {
     209                 :      843236 :                                 mp_opt->data_seq = get_unaligned_be64(ptr);
     210                 :      843236 :                                 ptr += 8;
     211                 :             :                         } else {
     212                 :          42 :                                 mp_opt->data_seq = get_unaligned_be32(ptr);
     213                 :          42 :                                 ptr += 4;
     214                 :             :                         }
     215                 :             : 
     216         [ +  + ]:      843278 :                         mp_opt->subflow_seq = get_unaligned_be32(ptr);
     217                 :      843278 :                         ptr += 4;
     218                 :             : 
     219         [ +  + ]:      843278 :                         mp_opt->data_len = get_unaligned_be16(ptr);
     220                 :      843278 :                         ptr += 2;
     221                 :             : 
     222         [ +  + ]:      843278 :                         if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
     223                 :       76342 :                                 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
     224                 :       76342 :                                 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
     225                 :       76342 :                                 ptr += 2;
     226                 :             :                         }
     227                 :             : 
     228   [ -  +  -  - ]:      843278 :                         pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
     229                 :             :                                  mp_opt->data_seq, mp_opt->subflow_seq,
     230                 :             :                                  mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
     231                 :             :                                  mp_opt->csum);
     232                 :             :                 }
     233                 :             : 
     234                 :             :                 break;
     235                 :             : 
     236                 :         795 :         case MPTCPOPT_ADD_ADDR:
     237                 :         795 :                 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
     238         [ +  + ]:         795 :                 if (!mp_opt->echo) {
     239                 :         403 :                         if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
     240         [ +  + ]:         403 :                             opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
     241                 :         309 :                                 mp_opt->addr.family = AF_INET;
     242                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
     243                 :          94 :                         else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
     244         [ +  - ]:          94 :                                  opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
     245                 :          94 :                                 mp_opt->addr.family = AF_INET6;
     246                 :             : #endif
     247                 :             :                         else
     248                 :             :                                 break;
     249                 :             :                 } else {
     250                 :         392 :                         if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
     251         [ +  + ]:         392 :                             opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
     252                 :         302 :                                 mp_opt->addr.family = AF_INET;
     253                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
     254                 :          90 :                         else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
     255         [ +  - ]:          90 :                                  opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
     256                 :          90 :                                 mp_opt->addr.family = AF_INET6;
     257                 :             : #endif
     258                 :             :                         else
     259                 :             :                                 break;
     260                 :             :                 }
     261                 :             : 
     262                 :         795 :                 mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
     263                 :         795 :                 mp_opt->addr.id = *ptr++;
     264                 :         795 :                 mp_opt->addr.port = 0;
     265                 :         795 :                 mp_opt->ahmac = 0;
     266         [ +  + ]:         795 :                 if (mp_opt->addr.family == AF_INET) {
     267                 :         611 :                         memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
     268                 :         611 :                         ptr += 4;
     269                 :         611 :                         if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
     270         [ +  + ]:         611 :                             opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
     271                 :          74 :                                 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
     272                 :          74 :                                 ptr += 2;
     273                 :             :                         }
     274                 :             :                 }
     275                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
     276                 :             :                 else {
     277                 :         184 :                         memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
     278                 :         184 :                         ptr += 16;
     279                 :         184 :                         if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
     280         [ +  + ]:         184 :                             opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
     281                 :          12 :                                 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
     282                 :          12 :                                 ptr += 2;
     283                 :             :                         }
     284                 :             :                 }
     285                 :             : #endif
     286         [ +  + ]:         795 :                 if (!mp_opt->echo) {
     287                 :         403 :                         mp_opt->ahmac = get_unaligned_be64(ptr);
     288                 :         403 :                         ptr += 8;
     289                 :             :                 }
     290   [ -  +  -  -  :         795 :                 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d\n",
                   -  - ]
     291                 :             :                          (mp_opt->addr.family == AF_INET6) ? "6" : "",
     292                 :             :                          mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
     293                 :             :                 break;
     294                 :             : 
     295                 :         108 :         case MPTCPOPT_RM_ADDR:
     296         [ +  - ]:         108 :                 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
     297                 :             :                     opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
     298                 :             :                         break;
     299                 :             : 
     300                 :         108 :                 ptr++;
     301                 :             : 
     302                 :         108 :                 mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
     303                 :         108 :                 mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
     304         [ +  + ]:         224 :                 for (i = 0; i < mp_opt->rm_list.nr; i++)
     305                 :         116 :                         mp_opt->rm_list.ids[i] = *ptr++;
     306   [ -  +  -  - ]:         108 :                 pr_debug("RM_ADDR: rm_list_nr=%d\n", mp_opt->rm_list.nr);
     307                 :             :                 break;
     308                 :             : 
     309                 :          46 :         case MPTCPOPT_MP_PRIO:
     310         [ +  + ]:          46 :                 if (opsize != TCPOLEN_MPTCP_PRIO)
     311                 :             :                         break;
     312                 :             : 
     313                 :          40 :                 mp_opt->suboptions |= OPTION_MPTCP_PRIO;
     314                 :          40 :                 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
     315   [ -  +  -  - ]:          40 :                 pr_debug("MP_PRIO: prio=%d\n", mp_opt->backup);
     316                 :             :                 break;
     317                 :             : 
     318                 :         263 :         case MPTCPOPT_MP_FASTCLOSE:
     319         [ +  - ]:         263 :                 if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
     320                 :             :                         break;
     321                 :             : 
     322                 :         263 :                 ptr += 2;
     323         [ -  + ]:         263 :                 mp_opt->rcvr_key = get_unaligned_be64(ptr);
     324                 :         263 :                 ptr += 8;
     325                 :         263 :                 mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
     326   [ -  +  -  - ]:         263 :                 pr_debug("MP_FASTCLOSE: recv_key=%llu\n", mp_opt->rcvr_key);
     327                 :             :                 break;
     328                 :             : 
     329                 :         274 :         case MPTCPOPT_RST:
     330         [ +  - ]:         274 :                 if (opsize != TCPOLEN_MPTCP_RST)
     331                 :             :                         break;
     332                 :             : 
     333         [ +  + ]:         274 :                 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
     334                 :             :                         break;
     335                 :             : 
     336                 :         262 :                 mp_opt->suboptions |= OPTION_MPTCP_RST;
     337                 :         262 :                 flags = *ptr++;
     338                 :         262 :                 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
     339                 :         262 :                 mp_opt->reset_reason = *ptr;
     340   [ -  +  -  - ]:         262 :                 pr_debug("MP_RST: transient=%u reason=%u\n",
     341                 :             :                          mp_opt->reset_transient, mp_opt->reset_reason);
     342                 :             :                 break;
     343                 :             : 
     344                 :           6 :         case MPTCPOPT_MP_FAIL:
     345         [ +  - ]:           6 :                 if (opsize != TCPOLEN_MPTCP_FAIL)
     346                 :             :                         break;
     347                 :             : 
     348                 :           6 :                 ptr += 2;
     349                 :           6 :                 mp_opt->suboptions |= OPTION_MPTCP_FAIL;
     350         [ -  + ]:           6 :                 mp_opt->fail_seq = get_unaligned_be64(ptr);
     351   [ -  +  -  - ]:           6 :                 pr_debug("MP_FAIL: data_seq=%llu\n", mp_opt->fail_seq);
     352                 :             :                 break;
     353                 :             : 
     354                 :             :         default:
     355                 :             :                 break;
     356                 :             :         }
     357                 :     1402359 : }
     358                 :             : 
     359                 :     1402526 : void mptcp_get_options(const struct sk_buff *skb,
     360                 :             :                        struct mptcp_options_received *mp_opt)
     361                 :             : {
     362                 :     1402526 :         const struct tcphdr *th = tcp_hdr(skb);
     363                 :     1402526 :         const unsigned char *ptr;
     364                 :     1402526 :         int length;
     365                 :             : 
     366                 :             :         /* Ensure that casting the whole status to u32 is efficient and safe */
     367                 :     1402526 :         BUILD_BUG_ON(sizeof_field(struct mptcp_options_received, status) != sizeof(u32));
     368                 :     1402526 :         BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_options_received, status),
     369                 :             :                                  sizeof(u32)));
     370                 :     1402526 :         *(u32 *)&mp_opt->status = 0;
     371                 :             : 
     372                 :     1402526 :         length = (th->doff * 4) - sizeof(struct tcphdr);
     373                 :     1402526 :         ptr = (const unsigned char *)(th + 1);
     374                 :             : 
     375         [ +  + ]:     8602814 :         while (length > 0) {
     376                 :     7200288 :                 int opcode = *ptr++;
     377                 :     7200288 :                 int opsize;
     378                 :             : 
     379      [ +  +  - ]:     7200288 :                 switch (opcode) {
     380                 :             :                 case TCPOPT_EOL:
     381                 :             :                         return;
     382                 :     4367336 :                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
     383                 :     4367336 :                         length--;
     384                 :     4367336 :                         continue;
     385                 :     2832952 :                 default:
     386         [ +  - ]:     2832952 :                         if (length < 2)
     387                 :             :                                 return;
     388                 :     2832952 :                         opsize = *ptr++;
     389         [ +  - ]:     2832952 :                         if (opsize < 2) /* "silly options" */
     390                 :             :                                 return;
     391         [ +  - ]:     2832952 :                         if (opsize > length)
     392                 :             :                                 return; /* don't parse partial options */
     393         [ +  + ]:     2832952 :                         if (opcode == TCPOPT_MPTCP)
     394                 :     1402359 :                                 mptcp_parse_option(skb, ptr, opsize, mp_opt);
     395                 :     2832952 :                         ptr += opsize - 2;
     396                 :     2832952 :                         length -= opsize;
     397                 :             :                 }
     398                 :             :         }
     399                 :             : }
     400                 :             : 
     401                 :        2336 : bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
     402                 :             :                        unsigned int *size, struct mptcp_out_options *opts)
     403                 :             : {
     404         [ +  + ]:        2336 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     405                 :             : 
     406                 :             :         /* we will use snd_isn to detect first pkt [re]transmission
     407                 :             :          * in mptcp_established_options_mp()
     408                 :             :          */
     409                 :        2336 :         subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
     410         [ +  + ]:        2336 :         if (subflow->request_mptcp) {
     411         [ +  + ]:        1738 :                 if (unlikely(subflow_simultaneous_connect(sk))) {
     412         [ -  + ]:           6 :                         WARN_ON_ONCE(!mptcp_try_fallback(sk, MPTCP_MIB_SIMULTCONNFALLBACK));
     413                 :             : 
     414                 :             :                         /* Ensure mptcp_finish_connect() will not process the
     415                 :             :                          * MPC handshake.
     416                 :             :                          */
     417                 :           6 :                         subflow->request_mptcp = 0;
     418                 :           6 :                         return false;
     419                 :             :                 }
     420                 :             : 
     421                 :        1732 :                 opts->suboptions = OPTION_MPTCP_MPC_SYN;
     422                 :        1732 :                 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
     423                 :        1732 :                 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
     424                 :        1732 :                 *size = TCPOLEN_MPTCP_MPC_SYN;
     425                 :        1732 :                 return true;
     426         [ +  + ]:         598 :         } else if (subflow->request_join) {
     427   [ -  +  -  - ]:         568 :                 pr_debug("remote_token=%u, nonce=%u\n", subflow->remote_token,
     428                 :             :                          subflow->local_nonce);
     429                 :         568 :                 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
     430                 :         568 :                 opts->join_id = subflow->local_id;
     431                 :         568 :                 opts->token = subflow->remote_token;
     432                 :         568 :                 opts->nonce = subflow->local_nonce;
     433                 :         568 :                 opts->backup = subflow->request_bkup;
     434                 :         568 :                 *size = TCPOLEN_MPTCP_MPJ_SYN;
     435                 :         568 :                 return true;
     436                 :             :         }
     437                 :             :         return false;
     438                 :             : }
     439                 :             : 
     440                 :         936 : static void clear_3rdack_retransmission(struct sock *sk)
     441                 :             : {
     442                 :         936 :         struct inet_connection_sock *icsk = inet_csk(sk);
     443                 :             : 
     444                 :         936 :         sk_stop_timer(sk, &icsk->icsk_delack_timer);
     445                 :         936 :         icsk->icsk_ack.ato = 0;
     446                 :         936 :         icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
     447                 :         936 : }
     448                 :             : 
     449                 :     2644476 : static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
     450                 :             :                                          bool snd_data_fin_enable,
     451                 :             :                                          unsigned int *size,
     452                 :             :                                          struct mptcp_out_options *opts)
     453                 :             : {
     454         [ -  + ]:     2644476 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     455         [ -  + ]:     2644476 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
     456                 :     2644476 :         struct mptcp_ext *mpext;
     457                 :     2644476 :         unsigned int data_len;
     458                 :     2644476 :         u8 len;
     459                 :             : 
     460                 :             :         /* When skb is not available, we better over-estimate the emitted
     461                 :             :          * options len. A full DSS option (28 bytes) is longer than
     462                 :             :          * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
     463                 :             :          * tell the caller to defer the estimate to
     464                 :             :          * mptcp_established_options_dss(), which will reserve enough space.
     465                 :             :          */
     466         [ +  + ]:     2644476 :         if (!skb)
     467                 :             :                 return false;
     468                 :             : 
     469                 :             :         /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
     470   [ -  +  +  +  :     1233493 :         if (READ_ONCE(subflow->fully_established) || snd_data_fin_enable ||
                   +  + ]
           [ +  +  +  + ]
     471         [ +  + ]:        4037 :             subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
     472         [ +  + ]:        2816 :             sk->sk_state != TCP_ESTABLISHED)
     473                 :             :                 return false;
     474                 :             : 
     475         [ +  + ]:        2816 :         if (subflow->mp_capable) {
     476         [ +  + ]:        2342 :                 mpext = mptcp_get_ext(skb);
     477         [ +  + ]:        1609 :                 data_len = mpext ? mpext->data_len : 0;
     478                 :             : 
     479                 :             :                 /* we will check ops->data_len in mptcp_write_options() to
     480                 :             :                  * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
     481                 :             :                  * TCPOLEN_MPTCP_MPC_ACK
     482                 :             :                  */
     483                 :        2342 :                 opts->data_len = data_len;
     484                 :        2342 :                 opts->suboptions = OPTION_MPTCP_MPC_ACK;
     485                 :        2342 :                 opts->sndr_key = subflow->local_key;
     486                 :        2342 :                 opts->rcvr_key = subflow->remote_key;
     487         [ -  + ]:        2342 :                 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
     488                 :        2342 :                 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
     489                 :             : 
     490                 :             :                 /* Section 3.1.
     491                 :             :                  * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
     492                 :             :                  * packets that start the first subflow of an MPTCP connection,
     493                 :             :                  * as well as the first packet that carries data
     494                 :             :                  */
     495         [ +  + ]:        2342 :                 if (data_len > 0) {
     496                 :         876 :                         len = TCPOLEN_MPTCP_MPC_ACK_DATA;
     497         [ +  + ]:         876 :                         if (opts->csum_reqd) {
     498                 :             :                                 /* we need to propagate more info to csum the pseudo hdr */
     499                 :          80 :                                 opts->data_seq = mpext->data_seq;
     500                 :          80 :                                 opts->subflow_seq = mpext->subflow_seq;
     501                 :          80 :                                 opts->csum = mpext->csum;
     502                 :          80 :                                 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
     503                 :             :                         }
     504                 :         876 :                         *size = ALIGN(len, 4);
     505                 :             :                 } else {
     506                 :        1466 :                         *size = TCPOLEN_MPTCP_MPC_ACK;
     507                 :             :                 }
     508                 :             : 
     509   [ -  +  -  - ]:        2342 :                 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d\n",
     510                 :             :                          subflow, subflow->local_key, subflow->remote_key,
     511                 :             :                          data_len);
     512                 :             : 
     513                 :        2342 :                 return true;
     514         [ +  + ]:         474 :         } else if (subflow->mp_join) {
     515                 :         474 :                 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
     516                 :         474 :                 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
     517                 :         474 :                 *size = TCPOLEN_MPTCP_MPJ_ACK;
     518   [ -  +  -  - ]:         474 :                 pr_debug("subflow=%p\n", subflow);
     519                 :             : 
     520                 :             :                 /* we can use the full delegate action helper only from BH context
     521                 :             :                  * If we are in process context - sk is flushing the backlog at
     522                 :             :                  * socket lock release time - just set the appropriate flag, will
     523                 :             :                  * be handled by the release callback
     524                 :             :                  */
     525         [ +  + ]:         474 :                 if (sock_owned_by_user(sk))
     526                 :         381 :                         set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
     527                 :             :                 else
     528                 :          93 :                         mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
     529                 :         474 :                 return true;
     530                 :             :         }
     531                 :             :         return false;
     532                 :             : }
     533                 :             : 
     534                 :       35236 : static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
     535                 :             :                                  struct sk_buff *skb, struct mptcp_ext *ext)
     536                 :             : {
     537                 :             :         /* The write_seq value has already been incremented, so the actual
     538                 :             :          * sequence number for the DATA_FIN is one less.
     539                 :             :          */
     540         [ -  + ]:       35236 :         u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
     541                 :             : 
     542   [ +  +  -  + ]:       35236 :         if (!ext->use_map || !skb->len) {
     543                 :             :                 /* RFC6824 requires a DSS mapping with specific values
     544                 :             :                  * if DATA_FIN is set but no data payload is mapped
     545                 :             :                  */
     546                 :       14611 :                 ext->data_fin = 1;
     547                 :       14611 :                 ext->use_map = 1;
     548                 :       14611 :                 ext->dsn64 = 1;
     549                 :       14611 :                 ext->data_seq = data_fin_tx_seq;
     550                 :       14611 :                 ext->subflow_seq = 0;
     551                 :       14611 :                 ext->data_len = 1;
     552         [ +  + ]:       20625 :         } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
     553                 :             :                 /* If there's an existing DSS mapping and it is the
     554                 :             :                  * final mapping, DATA_FIN consumes 1 additional byte of
     555                 :             :                  * mapping space.
     556                 :             :                  */
     557                 :        2049 :                 ext->data_fin = 1;
     558                 :        2049 :                 ext->data_len++;
     559                 :             :         }
     560                 :       35236 : }
     561                 :             : 
     562                 :     2641660 : static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
     563                 :             :                                           bool snd_data_fin_enable,
     564                 :             :                                           unsigned int *size,
     565                 :             :                                           struct mptcp_out_options *opts)
     566                 :             : {
     567         [ -  + ]:     2641660 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     568         [ -  + ]:     2641660 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
     569         [ -  + ]:     2641660 :         struct tcp_sock *tp = tcp_sk(sk);
     570                 :     2641660 :         unsigned int dss_size = 0;
     571                 :     2641660 :         struct mptcp_ext *mpext;
     572                 :     2641660 :         unsigned int ack_size;
     573                 :     2641660 :         bool ret = false;
     574                 :             : 
     575                 :             :         /* Zero `use_ack` and `use_map` flags with one shot. */
     576                 :     2641660 :         BUILD_BUG_ON(sizeof_field(struct mptcp_ext, flags) != sizeof(u16));
     577                 :     2641660 :         BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_ext, flags),
     578                 :             :                                  sizeof(u16)));
     579                 :     2641660 :         *(u16 *)&opts->ext_copy.flags = 0;
     580         [ -  + ]:     2641660 :         opts->csum_reqd = READ_ONCE(msk->csum_enabled);
     581         [ +  + ]:     2641660 :         mpext = skb ? mptcp_get_ext(skb) : NULL;
     582                 :             : 
     583   [ +  +  -  +  :     1230677 :         if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
                   +  + ]
     584                 :     2072822 :                 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
     585                 :             : 
     586         [ +  + ]:     2072822 :                 if (mpext) {
     587         [ +  + ]:      647228 :                         if (opts->csum_reqd)
     588                 :       74236 :                                 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
     589                 :             : 
     590                 :      647228 :                         opts->ext_copy = *mpext;
     591                 :             :                 }
     592                 :             : 
     593                 :     2072822 :                 dss_size = map_size;
     594         [ +  + ]:     2072822 :                 if (skb && snd_data_fin_enable)
     595                 :       35236 :                         mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
     596                 :     2072822 :                 opts->suboptions = OPTION_MPTCP_DSS;
     597                 :     2072822 :                 ret = true;
     598                 :             :         }
     599                 :             : 
     600                 :             :         /* passive sockets msk will set the 'can_ack' after accept(), even
     601                 :             :          * if the first subflow may have the already the remote key handy
     602                 :             :          */
     603   [ -  +  +  + ]:     2641660 :         if (!READ_ONCE(msk->can_ack)) {
                 [ +  + ]
     604                 :          30 :                 *size = ALIGN(dss_size, 4);
     605                 :          30 :                 return ret;
     606                 :             :         }
     607                 :             : 
     608   [ -  +  +  + ]:     2641630 :         if (READ_ONCE(msk->use_64bit_ack)) {
                 [ +  + ]
     609                 :     2431204 :                 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
     610                 :     2431204 :                 opts->ext_copy.ack64 = 1;
     611                 :             :         } else {
     612                 :      210426 :                 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
     613                 :      210426 :                 opts->ext_copy.ack64 = 0;
     614                 :             :         }
     615                 :     2641630 :         opts->ext_copy.use_ack = 1;
     616                 :     2641630 :         opts->suboptions = OPTION_MPTCP_DSS;
     617                 :             : 
     618                 :             :         /* Add kind/length/subtype/flag overhead if mapping is not populated */
     619         [ +  + ]:     2641630 :         if (dss_size == 0)
     620                 :      568838 :                 ack_size += TCPOLEN_MPTCP_DSS_BASE;
     621                 :             : 
     622                 :             :         /* The caller is __tcp_transmit_skb(), and will compute the new rcv
     623                 :             :          * wnd soon: ensure that the window can shrink.
     624                 :             :          */
     625         [ +  + ]:     2641630 :         if (skb)
     626                 :     1230677 :                 tp->rcv_wnd = tp->rcv_nxt - tp->rcv_wup;
     627                 :             : 
     628                 :     2641630 :         dss_size += ack_size;
     629                 :             : 
     630                 :     2641630 :         *size = ALIGN(dss_size, 4);
     631                 :     2641630 :         return true;
     632                 :             : }
     633                 :             : 
     634                 :         862 : static u64 add_addr_generate_hmac(u64 key1, u64 key2,
     635                 :             :                                   struct mptcp_addr_info *addr)
     636                 :             : {
     637                 :         862 :         u16 port = ntohs(addr->port);
     638                 :         862 :         u8 hmac[SHA256_DIGEST_SIZE];
     639                 :         862 :         u8 msg[19];
     640                 :         862 :         int i = 0;
     641                 :             : 
     642                 :         862 :         msg[i++] = addr->id;
     643         [ +  + ]:         862 :         if (addr->family == AF_INET) {
     644                 :         662 :                 memcpy(&msg[i], &addr->addr.s_addr, 4);
     645                 :         662 :                 i += 4;
     646                 :             :         }
     647                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
     648         [ +  - ]:         200 :         else if (addr->family == AF_INET6) {
     649                 :         200 :                 memcpy(&msg[i], &addr->addr6.s6_addr, 16);
     650                 :         200 :                 i += 16;
     651                 :             :         }
     652                 :             : #endif
     653                 :         862 :         msg[i++] = port >> 8;
     654                 :         862 :         msg[i++] = port & 0xFF;
     655                 :             : 
     656                 :         862 :         mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
     657                 :             : 
     658                 :         862 :         return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
     659                 :             : }
     660                 :             : 
     661                 :     2644472 : static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
     662                 :             :                                                unsigned int *size,
     663                 :             :                                                unsigned int remaining,
     664                 :             :                                                struct mptcp_out_options *opts)
     665                 :             : {
     666         [ -  + ]:     2644472 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     667         [ -  + ]:     2644472 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
     668                 :     2644472 :         bool drop_other_suboptions = false;
     669                 :     2644472 :         unsigned int opt_size = *size;
     670                 :     2644472 :         struct mptcp_addr_info addr;
     671                 :     2644472 :         bool echo;
     672                 :     2644472 :         int len;
     673                 :             : 
     674                 :             :         /* add addr will strip the existing options, be sure to avoid breaking
     675                 :             :          * MPC/MPJ handshakes
     676                 :             :          */
     677         [ +  + ]:     2644472 :         if (!mptcp_pm_should_add_signal(msk) ||
     678   [ +  -  +  + ]:        1726 :             (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
     679                 :         863 :             !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &addr,
     680                 :             :                     &echo, &drop_other_suboptions))
     681                 :     2643619 :                 return false;
     682                 :             : 
     683                 :             :         /*
     684                 :             :          * Later on, mptcp_write_options() will enforce mutually exclusion with
     685                 :             :          * DSS, bail out if such option is set and we can't drop it.
     686                 :             :          */
     687   [ -  +  +  - ]:         853 :         if (drop_other_suboptions)
                 [ +  - ]
     688                 :         853 :                 remaining += opt_size;
     689         [ #  # ]:           0 :         else if (opts->suboptions & OPTION_MPTCP_DSS)
     690                 :             :                 return false;
     691                 :             : 
     692   [ -  +  +  + ]:         853 :         len = mptcp_add_addr_len(addr.family, echo, !!addr.port);
                 [ +  + ]
     693         [ -  + ]:         853 :         if (remaining < len)
     694                 :             :                 return false;
     695                 :             : 
     696                 :         853 :         *size = len;
     697   [ -  +  +  - ]:         853 :         if (drop_other_suboptions) {
                 [ +  - ]
     698   [ -  +  -  - ]:         853 :                 pr_debug("drop other suboptions\n");
     699                 :         853 :                 opts->suboptions = 0;
     700                 :             : 
     701                 :             :                 /* note that e.g. DSS could have written into the memory
     702                 :             :                  * aliased by ahmac, we must reset the field here
     703                 :             :                  * to avoid appending the hmac even for ADD_ADDR echo
     704                 :             :                  * options
     705                 :             :                  */
     706                 :         853 :                 opts->ahmac = 0;
     707                 :         853 :                 *size -= opt_size;
     708                 :             :         }
     709                 :         853 :         opts->addr = addr;
     710                 :         853 :         opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
     711   [ -  +  +  + ]:         853 :         if (!echo) {
                 [ +  + ]
     712         [ +  - ]:         459 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
     713                 :         459 :                 opts->ahmac = add_addr_generate_hmac(READ_ONCE(msk->local_key),
     714                 :         459 :                                                      READ_ONCE(msk->remote_key),
     715                 :             :                                                      &opts->addr);
     716                 :             :         } else {
     717         [ +  - ]:         394 :                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
     718                 :             :         }
     719   [ -  +  -  -  :         853 :         pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d\n",
                   -  - ]
           [ -  +  -  - ]
     720                 :             :                  opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
     721                 :             : 
     722                 :             :         return true;
     723                 :             : }
     724                 :             : 
     725                 :     2643619 : static bool mptcp_established_options_rm_addr(struct sock *sk,
     726                 :             :                                               unsigned int *size,
     727                 :             :                                               unsigned int remaining,
     728                 :             :                                               struct mptcp_out_options *opts)
     729                 :             : {
     730         [ -  + ]:     2643619 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     731         [ -  + ]:     2643619 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
     732                 :     2643619 :         struct mptcp_rm_list rm_list;
     733                 :     2643619 :         int i, len;
     734                 :             : 
     735   [ +  +  -  + ]:     2643727 :         if (!mptcp_pm_should_rm_signal(msk) ||
     736                 :         108 :             !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
     737                 :     2643511 :                 return false;
     738                 :             : 
     739         [ -  + ]:         293 :         len = mptcp_rm_addr_len(&rm_list);
     740                 :         108 :         if (len < 0)
     741                 :             :                 return false;
     742         [ -  + ]:         108 :         if (remaining < len)
     743                 :             :                 return false;
     744                 :             : 
     745                 :         108 :         *size = len;
     746                 :         108 :         opts->suboptions |= OPTION_MPTCP_RM_ADDR;
     747                 :         108 :         opts->rm_list = rm_list;
     748                 :             : 
     749         [ +  + ]:         224 :         for (i = 0; i < opts->rm_list.nr; i++)
     750   [ -  +  -  - ]:         116 :                 pr_debug("rm_list_ids[%d]=%d\n", i, opts->rm_list.ids[i]);
     751                 :         108 :         MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
     752                 :         108 :         return true;
     753                 :             : }
     754                 :             : 
     755                 :     2644472 : static bool mptcp_established_options_mp_prio(struct sock *sk,
     756                 :             :                                               unsigned int *size,
     757                 :             :                                               unsigned int remaining,
     758                 :             :                                               struct mptcp_out_options *opts)
     759                 :             : {
     760         [ +  + ]:     2644472 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     761                 :             : 
     762                 :             :         /* can't send MP_PRIO with MPC, as they share the same option space:
     763                 :             :          * 'backup'. Also it makes no sense at all
     764                 :             :          */
     765   [ +  +  +  - ]:     2644472 :         if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
     766                 :             :                 return false;
     767                 :             : 
     768                 :             :         /* account for the trailing 'nop' option */
     769         [ +  - ]:          28 :         if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
     770                 :             :                 return false;
     771                 :             : 
     772                 :          28 :         *size = TCPOLEN_MPTCP_PRIO_ALIGN;
     773                 :          28 :         opts->suboptions |= OPTION_MPTCP_PRIO;
     774                 :          28 :         opts->backup = subflow->request_bkup;
     775                 :             : 
     776   [ -  +  -  - ]:          28 :         pr_debug("prio=%d\n", opts->backup);
     777                 :             : 
     778                 :             :         return true;
     779                 :             : }
     780                 :             : 
     781                 :         503 : static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
     782                 :             :                                                    unsigned int *size,
     783                 :             :                                                    unsigned int remaining,
     784                 :             :                                                    struct mptcp_out_options *opts)
     785                 :             : {
     786         [ +  - ]:         503 :         const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     787                 :             : 
     788         [ +  - ]:         503 :         if (remaining < TCPOLEN_MPTCP_RST)
     789                 :             :                 return false;
     790                 :             : 
     791                 :         503 :         *size = TCPOLEN_MPTCP_RST;
     792                 :         503 :         opts->suboptions |= OPTION_MPTCP_RST;
     793                 :         503 :         opts->reset_transient = subflow->reset_transient;
     794                 :         503 :         opts->reset_reason = subflow->reset_reason;
     795         [ +  - ]:         503 :         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
     796                 :             : 
     797                 :             :         return true;
     798                 :             : }
     799                 :             : 
     800                 :         503 : static bool mptcp_established_options_fastclose(struct sock *sk,
     801                 :             :                                                 unsigned int *size,
     802                 :             :                                                 unsigned int remaining,
     803                 :             :                                                 struct mptcp_out_options *opts)
     804                 :             : {
     805         [ -  + ]:         503 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     806         [ -  + ]:         503 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
     807                 :             : 
     808         [ +  + ]:         503 :         if (likely(!subflow->send_fastclose))
     809                 :             :                 return false;
     810                 :             : 
     811         [ +  - ]:         444 :         if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
     812                 :             :                 return false;
     813                 :             : 
     814                 :         444 :         *size = TCPOLEN_MPTCP_FASTCLOSE;
     815                 :         444 :         opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
     816                 :         444 :         opts->rcvr_key = READ_ONCE(msk->remote_key);
     817                 :             : 
     818   [ -  +  -  - ]:         444 :         pr_debug("FASTCLOSE key=%llu\n", opts->rcvr_key);
     819         [ +  - ]:         444 :         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
     820                 :             :         return true;
     821                 :             : }
     822                 :             : 
     823                 :     2641719 : static bool mptcp_established_options_mp_fail(struct sock *sk,
     824                 :             :                                               unsigned int *size,
     825                 :             :                                               unsigned int remaining,
     826                 :             :                                               struct mptcp_out_options *opts)
     827                 :             : {
     828         [ +  + ]:     2641719 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     829                 :             : 
     830         [ +  + ]:     2641719 :         if (likely(!subflow->send_mp_fail))
     831                 :             :                 return false;
     832                 :             : 
     833         [ +  - ]:           6 :         if (remaining < TCPOLEN_MPTCP_FAIL)
     834                 :             :                 return false;
     835                 :             : 
     836                 :           6 :         *size = TCPOLEN_MPTCP_FAIL;
     837                 :           6 :         opts->suboptions |= OPTION_MPTCP_FAIL;
     838                 :           6 :         opts->fail_seq = subflow->map_seq;
     839                 :             : 
     840   [ -  +  -  - ]:           6 :         pr_debug("MP_FAIL fail_seq=%llu\n", opts->fail_seq);
     841         [ +  - ]:           6 :         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
     842                 :             : 
     843                 :             :         return true;
     844                 :             : }
     845                 :             : 
     846                 :     2682799 : bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
     847                 :             :                                unsigned int *size, unsigned int remaining,
     848                 :             :                                struct mptcp_out_options *opts)
     849                 :             : {
     850         [ -  + ]:     2682799 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
     851         [ -  + ]:     2682799 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
     852                 :     2682799 :         unsigned int opt_size = 0;
     853                 :     2682799 :         bool snd_data_fin;
     854                 :     2682799 :         bool ret = false;
     855                 :             : 
     856                 :     2682799 :         opts->suboptions = 0;
     857                 :             : 
     858                 :             :         /* Force later mptcp_write_options(), but do not use any actual
     859                 :             :          * option space.
     860                 :             :          */
     861   [ +  +  +  + ]:     2682799 :         if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
     862                 :             :                 return true;
     863                 :             : 
     864   [ +  +  +  + ]:     2644979 :         if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
     865   [ +  +  +  + ]:         562 :                 if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
     866                 :          59 :                     mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
     867                 :         446 :                         *size += opt_size;
     868                 :         446 :                         remaining -= opt_size;
     869                 :             :                 }
     870                 :             :                 /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
     871         [ +  - ]:         503 :                 if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
     872                 :         503 :                         *size += opt_size;
     873                 :         503 :                         remaining -= opt_size;
     874                 :             :                 }
     875                 :         503 :                 return true;
     876                 :             :         }
     877                 :             : 
     878         [ +  + ]:     2644476 :         snd_data_fin = mptcp_data_fin_enabled(msk);
     879         [ +  + ]:     2644476 :         if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
     880                 :             :                 ret = true;
     881         [ +  - ]:     2641660 :         else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
     882                 :     2641660 :                 unsigned int mp_fail_size;
     883                 :             : 
     884                 :     2641660 :                 ret = true;
     885         [ +  + ]:     2641660 :                 if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
     886                 :             :                                                       remaining - opt_size, opts)) {
     887                 :           4 :                         *size += opt_size + mp_fail_size;
     888                 :           4 :                         remaining -= opt_size - mp_fail_size;
     889                 :           4 :                         return true;
     890                 :             :                 }
     891                 :             :         }
     892                 :             : 
     893                 :             :         /* we reserved enough space for the above options, and exceeding the
     894                 :             :          * TCP option space would be fatal
     895                 :             :          */
     896   [ -  +  +  - ]:     2644472 :         if (WARN_ON_ONCE(opt_size > remaining))
     897                 :             :                 return false;
     898                 :             : 
     899                 :     2644472 :         *size += opt_size;
     900                 :     2644472 :         remaining -= opt_size;
     901         [ +  + ]:     2644472 :         if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
     902                 :         853 :                 *size += opt_size;
     903                 :         853 :                 remaining -= opt_size;
     904                 :         853 :                 ret = true;
     905         [ +  + ]:     2643619 :         } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
     906                 :         108 :                 *size += opt_size;
     907                 :         108 :                 remaining -= opt_size;
     908                 :         108 :                 ret = true;
     909                 :             :         }
     910                 :             : 
     911         [ +  + ]:     2644472 :         if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
     912                 :          28 :                 *size += opt_size;
     913                 :          28 :                 remaining -= opt_size;
     914                 :          28 :                 ret = true;
     915                 :             :         }
     916                 :             : 
     917                 :             :         return ret;
     918                 :             : }
     919                 :             : 
     920                 :        2280 : bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
     921                 :             :                           struct mptcp_out_options *opts)
     922                 :             : {
     923                 :        2280 :         struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
     924                 :             : 
     925         [ +  + ]:        2280 :         if (subflow_req->mp_capable) {
     926                 :        1624 :                 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
     927                 :        1624 :                 opts->sndr_key = subflow_req->local_key;
     928                 :        1624 :                 opts->csum_reqd = subflow_req->csum_reqd;
     929                 :        1624 :                 opts->allow_join_id0 = subflow_req->allow_join_id0;
     930                 :        1624 :                 *size = TCPOLEN_MPTCP_MPC_SYNACK;
     931   [ -  +  -  - ]:        1624 :                 pr_debug("subflow_req=%p, local_key=%llu\n",
     932                 :             :                          subflow_req, subflow_req->local_key);
     933                 :        1624 :                 return true;
     934         [ +  + ]:         656 :         } else if (subflow_req->mp_join) {
     935                 :         496 :                 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
     936                 :         496 :                 opts->backup = subflow_req->request_bkup;
     937                 :         496 :                 opts->join_id = subflow_req->local_id;
     938                 :         496 :                 opts->thmac = subflow_req->thmac;
     939                 :         496 :                 opts->nonce = subflow_req->local_nonce;
     940   [ -  +  -  - ]:         496 :                 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
     941                 :             :                          subflow_req, opts->backup, opts->join_id,
     942                 :             :                          opts->thmac, opts->nonce);
     943                 :         496 :                 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
     944                 :         496 :                 return true;
     945                 :             :         }
     946                 :             :         return false;
     947                 :             : }
     948                 :             : 
     949                 :     1395958 : static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
     950                 :             :                                     struct mptcp_subflow_context *subflow,
     951                 :             :                                     struct sk_buff *skb,
     952                 :             :                                     struct mptcp_options_received *mp_opt)
     953                 :             : {
     954                 :             :         /* here we can process OoO, in-window pkts, only in-sequence 4th ack
     955                 :             :          * will make the subflow fully established
     956                 :             :          */
     957   [ -  +  +  + ]:     1395958 :         if (likely(READ_ONCE(subflow->fully_established))) {
                 [ +  + ]
     958                 :             :                 /* on passive sockets, check for 3rd ack retransmission
     959                 :             :                  * note that msk is always set by subflow_syn_recv_sock()
     960                 :             :                  * for mp_join subflows
     961                 :             :                  */
     962         [ +  + ]:     1393993 :                 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
     963   [ +  +  +  + ]:      146785 :                     TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
     964         [ +  + ]:       52362 :                     subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
     965         [ +  - ]:         486 :                     !subflow->request_join)
     966                 :         486 :                         tcp_send_ack(ssk);
     967                 :     1393993 :                 goto check_notify;
     968                 :             :         }
     969                 :             : 
     970                 :             :         /* we must process OoO packets before the first subflow is fully
     971                 :             :          * established. OoO packets are instead a protocol violation
     972                 :             :          * for MP_JOIN subflows as the peer must not send any data
     973                 :             :          * before receiving the forth ack - cfr. RFC 8684 section 3.2.
     974                 :             :          */
     975         [ +  + ]:        1965 :         if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
     976         [ -  + ]:          61 :                 if (subflow->mp_join)
     977                 :           0 :                         goto reset;
     978   [ -  +  -  - ]:          61 :                 if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
     979                 :           0 :                         goto set_fully_established;
     980                 :          61 :                 return subflow->mp_capable;
     981                 :             :         }
     982                 :             : 
     983         [ +  + ]:        1904 :         if (subflow->remote_key_valid &&
     984   [ +  +  -  +  :        1866 :             (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
                   +  + ]
     985                 :         145 :              ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) &&
     986   [ +  +  +  - ]:         145 :               (!mp_opt->echo || subflow->mp_join)))) {
     987                 :             :                 /* subflows are fully established as soon as we get any
     988                 :             :                  * additional ack, including ADD_ADDR.
     989                 :             :                  */
     990                 :        1852 :                 goto set_fully_established;
     991                 :             :         }
     992                 :             : 
     993                 :             :         /* If the first established packet does not contain MP_CAPABLE + data
     994                 :             :          * then fallback to TCP. Fallback scenarios requires a reset for
     995                 :             :          * MP_JOIN subflows.
     996                 :             :          */
     997         [ +  + ]:          52 :         if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
     998         [ +  + ]:          14 :                 if (subflow->mp_join)
     999                 :           8 :                         goto reset;
    1000                 :           6 :                 subflow->mp_capable = 0;
    1001         [ -  + ]:           6 :                 if (!mptcp_try_fallback(ssk, MPTCP_MIB_MPCAPABLEDATAFALLBACK)) {
    1002         [ #  # ]:           0 :                         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_FALLBACKFAILED);
    1003                 :           0 :                         goto reset;
    1004                 :             :                 }
    1005                 :             :                 return false;
    1006                 :             :         }
    1007                 :             : 
    1008   [ -  +  +  - ]:          38 :         if (unlikely(!READ_ONCE(msk->pm.server_side)))
                 [ +  - ]
    1009                 :             :                 /* DO-NOT-MERGE: use WARN i/o pr_warn: only for MPTCP export */
    1010                 :           0 :                 WARN_ONCE(1, "bogus mpc option on established client sk");
    1011                 :             : 
    1012                 :          38 : set_fully_established:
    1013         [ -  + ]:        1890 :         if (mp_opt->deny_join_id0)
    1014                 :           0 :                 WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
    1015                 :             : 
    1016                 :        1890 :         mptcp_data_lock((struct sock *)msk);
    1017                 :        1890 :         __mptcp_subflow_fully_established(msk, subflow, mp_opt);
    1018                 :        1890 :         mptcp_data_unlock((struct sock *)msk);
    1019                 :             : 
    1020                 :     1395883 : check_notify:
    1021                 :             :         /* if the subflow is not already linked into the conn_list, we can't
    1022                 :             :          * notify the PM: this subflow is still on the listener queue
    1023                 :             :          * and the PM possibly acquiring the subflow lock could race with
    1024                 :             :          * the listener close
    1025                 :             :          */
    1026   [ +  +  +  - ]:     1395883 :         if (likely(subflow->pm_notified) || list_empty(&subflow->node))
    1027                 :             :                 return true;
    1028                 :             : 
    1029                 :        2374 :         subflow->pm_notified = 1;
    1030         [ +  + ]:        2374 :         if (subflow->mp_join) {
    1031                 :         936 :                 clear_3rdack_retransmission(ssk);
    1032                 :         936 :                 mptcp_pm_subflow_established(msk);
    1033                 :             :         } else {
    1034                 :        1438 :                 mptcp_pm_fully_established(msk, ssk);
    1035                 :             :         }
    1036                 :             :         return true;
    1037                 :             : 
    1038                 :           8 : reset:
    1039                 :           8 :         mptcp_subflow_reset(ssk);
    1040                 :           8 :         return false;
    1041                 :             : }
    1042                 :             : 
    1043                 :      138081 : u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
    1044                 :             : {
    1045                 :      138081 :         u32 old_seq32, cur_seq32;
    1046                 :             : 
    1047                 :      138081 :         old_seq32 = (u32)old_seq;
    1048                 :      138081 :         cur_seq32 = (u32)cur_seq;
    1049                 :      138081 :         cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
    1050   [ +  +  -  + ]:      138081 :         if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
    1051                 :           0 :                 return cur_seq + (1LL << 32);
    1052                 :             : 
    1053                 :             :         /* reverse wrap could happen, too */
    1054   [ +  +  -  + ]:      138081 :         if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
    1055                 :           0 :                 return cur_seq - (1LL << 32);
    1056                 :             :         return cur_seq;
    1057                 :             : }
    1058                 :             : 
    1059                 :           0 : static void __mptcp_snd_una_update(struct mptcp_sock *msk, u64 new_snd_una)
    1060                 :             : {
    1061                 :      398440 :         msk->bytes_acked += new_snd_una - msk->snd_una;
    1062                 :      398440 :         WRITE_ONCE(msk->snd_una, new_snd_una);
    1063                 :             : }
    1064                 :             : 
    1065                 :     1392529 : static void rwin_update(struct mptcp_sock *msk, struct sock *ssk,
    1066                 :             :                         struct sk_buff *skb)
    1067                 :             : {
    1068         [ -  + ]:     1392529 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
    1069         [ -  + ]:     1392529 :         struct tcp_sock *tp = tcp_sk(ssk);
    1070                 :     1392529 :         u64 mptcp_rcv_wnd;
    1071                 :             : 
    1072                 :             :         /* Avoid touching extra cachelines if TCP is going to accept this
    1073                 :             :          * skb without filling the TCP-level window even with a possibly
    1074                 :             :          * outdated mptcp-level rwin.
    1075                 :             :          */
    1076   [ +  -  +  + ]:     1392529 :         if (!skb->len || skb->len < tcp_receive_window(tp))
    1077                 :             :                 return;
    1078                 :             : 
    1079         [ -  + ]:        3091 :         mptcp_rcv_wnd = atomic64_read(&msk->rcv_wnd_sent);
    1080         [ -  + ]:        3091 :         if (!after64(mptcp_rcv_wnd, subflow->rcv_wnd_sent))
    1081                 :             :                 return;
    1082                 :             : 
    1083                 :             :         /* Some other subflow grew the mptcp-level rwin since rcv_wup,
    1084                 :             :          * resync.
    1085                 :             :          */
    1086                 :           0 :         tp->rcv_wnd += mptcp_rcv_wnd - subflow->rcv_wnd_sent;
    1087         [ #  # ]:           0 :         tcp_update_max_rcv_wnd_seq(tp);
    1088                 :           0 :         subflow->rcv_wnd_sent = mptcp_rcv_wnd;
    1089                 :             : }
    1090                 :             : 
    1091                 :     1391520 : static void ack_update_msk(struct mptcp_sock *msk,
    1092                 :             :                            struct sock *ssk,
    1093                 :             :                            struct mptcp_options_received *mp_opt)
    1094                 :             : {
    1095                 :     1391520 :         u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
    1096                 :     1391520 :         struct sock *sk = (struct sock *)msk;
    1097                 :     1391520 :         u64 old_snd_una;
    1098                 :             : 
    1099                 :     1391520 :         mptcp_data_lock(sk);
    1100                 :             : 
    1101                 :             :         /* avoid ack expansion on update conflict, to reduce the risk of
    1102                 :             :          * wrongly expanding to a future ack sequence number, which is way
    1103                 :             :          * more dangerous than missing an ack
    1104                 :             :          */
    1105                 :     1391520 :         old_snd_una = msk->snd_una;
    1106         [ +  + ]:     1391520 :         new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
    1107                 :             : 
    1108                 :             :         /* ACK for data not even sent yet? Ignore.*/
    1109         [ +  + ]:     1391520 :         if (unlikely(after64(new_snd_una, snd_nxt)))
    1110                 :          97 :                 new_snd_una = old_snd_una;
    1111                 :             : 
    1112         [ -  + ]:     1391520 :         new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
    1113                 :             : 
    1114         [ +  + ]:     1391520 :         if (after64(new_wnd_end, msk->wnd_end))
    1115                 :      385725 :                 WRITE_ONCE(msk->wnd_end, new_wnd_end);
    1116                 :             : 
    1117                 :             :         /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
    1118         [ +  + ]:     1391520 :         if (after64(msk->wnd_end, snd_nxt))
    1119                 :     1335374 :                 __mptcp_check_push(sk, ssk);
    1120                 :             : 
    1121         [ +  + ]:     1391520 :         if (after64(new_snd_una, old_snd_una)) {
    1122                 :      387691 :                 __mptcp_snd_una_update(msk, new_snd_una);
    1123                 :      387691 :                 __mptcp_data_acked(sk);
    1124                 :             :         }
    1125                 :     1391520 :         msk->last_ack_recv = tcp_jiffies32;
    1126                 :     1391520 :         mptcp_data_unlock(sk);
    1127                 :             : 
    1128                 :     1391520 :         trace_ack_update_msk(mp_opt->data_ack,
    1129                 :             :                              old_snd_una, new_snd_una,
    1130                 :     1391520 :                              new_wnd_end, READ_ONCE(msk->wnd_end));
    1131                 :     1391520 : }
    1132                 :             : 
    1133                 :       15755 : bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
    1134                 :             : {
    1135                 :             :         /* Skip if DATA_FIN was already received.
    1136                 :             :          * If updating simultaneously with the recvmsg loop, values
    1137                 :             :          * should match. If they mismatch, the peer is misbehaving and
    1138                 :             :          * we will prefer the most recent information.
    1139                 :             :          */
    1140   [ -  +  +  + ]:       15755 :         if (READ_ONCE(msk->rcv_data_fin))
                 [ +  + ]
    1141                 :             :                 return false;
    1142                 :             : 
    1143         [ +  + ]:        3196 :         WRITE_ONCE(msk->rcv_data_fin_seq,
    1144                 :             :                    mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
    1145                 :        3196 :         WRITE_ONCE(msk->rcv_data_fin, 1);
    1146                 :             : 
    1147                 :        3196 :         return true;
    1148                 :             : }
    1149                 :             : 
    1150                 :         795 : static bool add_addr_hmac_valid(struct mptcp_sock *msk,
    1151                 :             :                                 struct mptcp_options_received *mp_opt)
    1152                 :             : {
    1153                 :         795 :         u64 hmac = 0;
    1154                 :             : 
    1155         [ +  + ]:         795 :         if (mp_opt->echo)
    1156                 :             :                 return true;
    1157                 :             : 
    1158                 :         403 :         hmac = add_addr_generate_hmac(READ_ONCE(msk->remote_key),
    1159                 :         403 :                                       READ_ONCE(msk->local_key),
    1160                 :             :                                       &mp_opt->addr);
    1161                 :             : 
    1162   [ -  +  -  - ]:         403 :         pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
    1163                 :             :                  msk, hmac, mp_opt->ahmac);
    1164                 :             : 
    1165                 :         403 :         return hmac == mp_opt->ahmac;
    1166                 :             : }
    1167                 :             : 
    1168                 :             : /* Return false in case of error (or subflow has been reset),
    1169                 :             :  * else return true.
    1170                 :             :  */
    1171                 :     1406747 : bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
    1172                 :             : {
    1173         [ -  + ]:     1406747 :         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
    1174         [ -  + ]:     1406747 :         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
    1175                 :     1406747 :         struct mptcp_options_received mp_opt;
    1176                 :     1406747 :         struct mptcp_ext *mpext;
    1177                 :             : 
    1178         [ +  + ]:     1406747 :         if (__mptcp_check_fallback(msk)) {
    1179                 :             :                 /* Keep it simple and unconditionally trigger send data cleanup and
    1180                 :             :                  * pending queue spooling. We will need to acquire the data lock
    1181                 :             :                  * for more accurate checks, and once the lock is acquired, such
    1182                 :             :                  * helpers are cheap.
    1183                 :             :                  */
    1184                 :       10789 :                 mptcp_data_lock(subflow->conn);
    1185         [ +  - ]:       10789 :                 if (sk_stream_memory_free(sk))
    1186                 :       10789 :                         __mptcp_check_push(subflow->conn, sk);
    1187                 :             : 
    1188                 :             :                 /* on fallback we just need to ignore the msk-level snd_una, as
    1189                 :             :                  * this is really plain TCP
    1190                 :             :                  */
    1191                 :       10789 :                 __mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
    1192                 :             : 
    1193                 :       10789 :                 __mptcp_data_acked(subflow->conn);
    1194                 :       10789 :                 mptcp_data_unlock(subflow->conn);
    1195                 :       10789 :                 return true;
    1196                 :             :         }
    1197                 :             : 
    1198                 :     1395958 :         mptcp_get_options(skb, &mp_opt);
    1199                 :             : 
    1200                 :             :         /* The subflow can be in close state only if check_fully_established()
    1201                 :             :          * just sent a reset. If so, tell the caller to ignore the current packet.
    1202                 :             :          */
    1203         [ +  + ]:     1395958 :         if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
    1204                 :          60 :                 return sk->sk_state != TCP_CLOSE;
    1205                 :             : 
    1206         [ +  + ]:     1395898 :         if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
    1207         [ +  + ]:       80894 :                 if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
    1208         [ +  + ]:         263 :                     READ_ONCE(msk->local_key) == mp_opt.rcvr_key) {
    1209                 :         253 :                         WRITE_ONCE(msk->rcv_fastclose, true);
    1210                 :         253 :                         mptcp_schedule_work((struct sock *)msk);
    1211         [ +  - ]:         253 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
    1212                 :             :                 }
    1213                 :             : 
    1214   [ +  +  +  - ]:       81689 :                 if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
    1215                 :         795 :                     add_addr_hmac_valid(msk, &mp_opt)) {
    1216         [ +  + ]:         795 :                         if (!mp_opt.echo) {
    1217                 :         403 :                                 mptcp_pm_add_addr_received(sk, &mp_opt.addr);
    1218         [ +  - ]:         403 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
    1219                 :             :                         } else {
    1220                 :         392 :                                 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
    1221                 :         392 :                                 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
    1222         [ +  - ]:         392 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
    1223                 :             :                         }
    1224                 :             : 
    1225         [ +  + ]:         795 :                         if (mp_opt.addr.port)
    1226         [ +  - ]:          86 :                                 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
    1227                 :             :                 }
    1228                 :             : 
    1229         [ +  + ]:       80894 :                 if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
    1230                 :         108 :                         mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
    1231                 :             : 
    1232         [ +  + ]:       80894 :                 if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
    1233                 :          40 :                         mptcp_pm_mp_prio_received(sk, mp_opt.backup);
    1234         [ +  - ]:          40 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
    1235                 :             :                 }
    1236                 :             : 
    1237         [ +  + ]:       80894 :                 if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
    1238                 :           6 :                         mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
    1239         [ +  - ]:           6 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
    1240                 :             :                 }
    1241                 :             : 
    1242         [ +  + ]:       80894 :                 if (mp_opt.suboptions & OPTION_MPTCP_RST) {
    1243                 :         252 :                         subflow->reset_seen = 1;
    1244                 :         252 :                         subflow->reset_reason = mp_opt.reset_reason;
    1245                 :         252 :                         subflow->reset_transient = mp_opt.reset_transient;
    1246         [ +  - ]:         252 :                         MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
    1247                 :             :                 }
    1248                 :             : 
    1249         [ +  + ]:       80894 :                 if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
    1250                 :             :                         return true;
    1251                 :             :         }
    1252                 :             : 
    1253                 :             :         /* we can't wait for recvmsg() to update the ack_seq, otherwise
    1254                 :             :          * monodirectional flows will stuck
    1255                 :             :          */
    1256         [ +  + ]:     1392529 :         if (mp_opt.use_ack)
    1257                 :     1391520 :                 ack_update_msk(msk, sk, &mp_opt);
    1258                 :     1392529 :         rwin_update(msk, sk, skb);
    1259                 :             : 
    1260                 :             :         /* Zero-data-length packets are dropped by the caller and not
    1261                 :             :          * propagated to the MPTCP layer, so the skb extension does not
    1262                 :             :          * need to be allocated or populated. DATA_FIN information, if
    1263                 :             :          * present, needs to be updated here before the skb is freed.
    1264                 :             :          */
    1265         [ +  + ]:     1392529 :         if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
    1266   [ +  +  +  -  :      570588 :                 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
                   +  + ]
    1267                 :       12535 :                     mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
    1268                 :        2979 :                         mptcp_schedule_work((struct sock *)msk);
    1269                 :             : 
    1270                 :      558053 :                 return true;
    1271                 :             :         }
    1272                 :             : 
    1273                 :      834476 :         mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
    1274         [ +  - ]:      834476 :         if (!mpext)
    1275                 :             :                 return false;
    1276                 :             : 
    1277                 :      834476 :         memset(mpext, 0, sizeof(*mpext));
    1278                 :             : 
    1279         [ +  + ]:      834476 :         if (likely(mp_opt.use_map)) {
    1280         [ +  + ]:      831716 :                 if (mp_opt.mpc_map) {
    1281                 :             :                         /* this is an MP_CAPABLE carrying MPTCP data
    1282                 :             :                          * we know this map the first chunk of data
    1283                 :             :                          */
    1284                 :        1009 :                         mptcp_crypto_key_sha(subflow->remote_key, NULL,
    1285                 :             :                                              &mpext->data_seq);
    1286                 :        1009 :                         mpext->data_seq++;
    1287                 :        1009 :                         mpext->subflow_seq = 1;
    1288                 :        1009 :                         mpext->dsn64 = 1;
    1289                 :        1009 :                         mpext->mpc_map = 1;
    1290                 :        1009 :                         mpext->data_fin = 0;
    1291                 :             :                 } else {
    1292                 :      830707 :                         mpext->data_seq = mp_opt.data_seq;
    1293                 :      830707 :                         mpext->subflow_seq = mp_opt.subflow_seq;
    1294                 :      830707 :                         mpext->dsn64 = mp_opt.dsn64;
    1295                 :      830707 :                         mpext->data_fin = mp_opt.data_fin;
    1296                 :             :                 }
    1297                 :      831716 :                 mpext->data_len = mp_opt.data_len;
    1298                 :      831716 :                 mpext->use_map = 1;
    1299                 :      831716 :                 mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
    1300                 :             : 
    1301         [ +  + ]:      831716 :                 if (mpext->csum_reqd)
    1302                 :       73291 :                         mpext->csum = mp_opt.csum;
    1303                 :             :         }
    1304                 :             : 
    1305                 :             :         return true;
    1306                 :             : }
    1307                 :             : 
    1308                 :     1229824 : static u64 mptcp_set_rwin(struct mptcp_sock *msk, struct tcp_sock *tp,
    1309                 :             :                           struct tcphdr *th, u64 ack_seq)
    1310                 :             : {
    1311                 :     1229824 :         const struct sock *ssk = (const struct sock *)tp;
    1312                 :     1229824 :         u64 rcv_wnd_old, rcv_wnd_new;
    1313                 :     1229824 :         u32 new_win;
    1314                 :     1229824 :         u64 win;
    1315                 :             : 
    1316                 :     1229824 :         rcv_wnd_new = ack_seq + tp->rcv_wnd;
    1317                 :             : 
    1318         [ +  + ]:     1229824 :         rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
    1319         [ +  + ]:     1229824 :         if (after64(rcv_wnd_new, rcv_wnd_old)) {
    1320                 :          70 :                 u64 rcv_wnd;
    1321                 :             : 
    1322                 :      427576 :                 for (;;) {
    1323                 :      427646 :                         rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
    1324                 :             : 
    1325         [ +  + ]:      427576 :                         if (rcv_wnd == rcv_wnd_old)
    1326                 :             :                                 break;
    1327                 :             : 
    1328                 :           2 :                         rcv_wnd_old = rcv_wnd;
    1329         [ -  + ]:           2 :                         if (before64(rcv_wnd_new, rcv_wnd_old)) {
    1330         [ #  # ]:           0 :                                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
    1331                 :           0 :                                 goto raise_win;
    1332                 :             :                         }
    1333         [ -  + ]:          72 :                         MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
    1334                 :             :                 }
    1335                 :      427574 :                 goto update_wspace;
    1336                 :             :         }
    1337                 :             : 
    1338         [ +  + ]:      802250 :         if (rcv_wnd_new != rcv_wnd_old) {
    1339                 :      272864 : raise_win:
    1340                 :             :                 /* The msk-level rcv wnd is after the tcp level one,
    1341                 :             :                  * sync the latter.
    1342                 :             :                  */
    1343                 :      272864 :                 rcv_wnd_new = rcv_wnd_old;
    1344                 :      272864 :                 win = rcv_wnd_old - ack_seq;
    1345                 :      272864 :                 new_win = min_t(u64, win, U32_MAX);
    1346                 :      272864 :                 tp->rcv_wnd = new_win;
    1347         [ +  + ]:      272864 :                 tcp_update_max_rcv_wnd_seq(tp);
    1348                 :             : 
    1349                 :             :                 /* Make sure we do not exceed the maximum possible
    1350                 :             :                  * scaled window.
    1351                 :             :                  */
    1352         [ -  + ]:      272864 :                 if (unlikely(th->syn))
    1353                 :           0 :                         new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
    1354         [ -  + ]:      272864 :                 if (!tp->rx_opt.rcv_wscale &&
           [ -  +  -  - ]
    1355         [ #  # ]:           0 :                     READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
    1356                 :           0 :                         new_win = min(new_win, MAX_TCP_WINDOW);
    1357                 :             :                 else
    1358                 :      272864 :                         new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
    1359                 :             : 
    1360                 :             :                 /* RFC1323 scaling applied */
    1361                 :      272864 :                 new_win >>= tp->rx_opt.rcv_wscale;
    1362                 :      272864 :                 th->window = htons(new_win);
    1363         [ +  - ]:      272864 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
    1364                 :             :         }
    1365                 :             : 
    1366                 :      529386 : update_wspace:
    1367                 :     1229824 :         WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
    1368                 :     1229824 :         return rcv_wnd_new;
    1369                 :             : }
    1370                 :             : 
    1371                 :       12276 : static void mptcp_track_rwin(struct tcp_sock *tp)
    1372                 :             : {
    1373                 :       12276 :         const struct sock *ssk = (const struct sock *)tp;
    1374                 :       12276 :         struct mptcp_subflow_context *subflow;
    1375                 :       12276 :         struct mptcp_sock *msk;
    1376                 :             : 
    1377         [ +  - ]:       12276 :         if (!ssk)
    1378                 :             :                 return;
    1379                 :             : 
    1380         [ -  + ]:       12276 :         subflow = mptcp_subflow_ctx(ssk);
    1381         [ -  + ]:       12276 :         msk = mptcp_sk(subflow->conn);
    1382                 :       12276 :         WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
    1383                 :             : }
    1384                 :             : 
    1385                 :      107928 : __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
    1386                 :             : {
    1387                 :      107928 :         struct csum_pseudo_header header;
    1388                 :      107928 :         __wsum csum;
    1389                 :             : 
    1390                 :             :         /* cfr RFC 8684 3.3.1.:
    1391                 :             :          * the data sequence number used in the pseudo-header is
    1392                 :             :          * always the 64-bit value, irrespective of what length is used in the
    1393                 :             :          * DSS option itself.
    1394                 :             :          */
    1395                 :      107928 :         header.data_seq = cpu_to_be64(data_seq);
    1396                 :      107928 :         header.subflow_seq = htonl(subflow_seq);
    1397                 :      107928 :         header.data_len = htons(data_len);
    1398                 :      107928 :         header.csum = 0;
    1399                 :             : 
    1400                 :      107928 :         csum = csum_partial(&header, sizeof(header), sum);
    1401                 :      107928 :         return csum_fold(csum);
    1402                 :             : }
    1403                 :             : 
    1404                 :       77548 : static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
    1405                 :             : {
    1406                 :      155096 :         return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
    1407                 :       77548 :                                  ~csum_unfold(mpext->csum));
    1408                 :             : }
    1409                 :             : 
    1410                 :           0 : static void put_len_csum(u16 len, __sum16 csum, void *data)
    1411                 :             : {
    1412                 :       77630 :         __sum16 *sumptr = data + 2;
    1413                 :       77630 :         __be16 *ptr = data;
    1414                 :             : 
    1415                 :       77630 :         put_unaligned_be16(len, ptr);
    1416                 :             : 
    1417                 :       77630 :         put_unaligned(csum, sumptr);
    1418                 :       77630 : }
    1419                 :             : 
    1420                 :     1250692 : void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
    1421                 :             :                          struct mptcp_out_options *opts)
    1422                 :             : {
    1423                 :     1250692 :         const struct sock *ssk = (const struct sock *)tp;
    1424                 :     1250692 :         struct mptcp_subflow_context *subflow;
    1425                 :             : 
    1426                 :             :         /* Which options can be used together?
    1427                 :             :          *
    1428                 :             :          * X: mutually exclusive
    1429                 :             :          * O: often used together
    1430                 :             :          * C: can be used together in some cases
    1431                 :             :          * P: could be used together but we prefer not to (optimisations)
    1432                 :             :          *
    1433                 :             :          *  Opt: | MPC  | MPJ  | DSS  | ADD  |  RM  | PRIO | FAIL |  FC  |
    1434                 :             :          * ------|------|------|------|------|------|------|------|------|
    1435                 :             :          *  MPC  |------|------|------|------|------|------|------|------|
    1436                 :             :          *  MPJ  |  X   |------|------|------|------|------|------|------|
    1437                 :             :          *  DSS  |  X   |  X   |------|------|------|------|------|------|
    1438                 :             :          *  ADD  |  X   |  X   |  P   |------|------|------|------|------|
    1439                 :             :          *  RM   |  C   |  C   |  C   |  P   |------|------|------|------|
    1440                 :             :          *  PRIO |  X   |  C   |  C   |  C   |  C   |------|------|------|
    1441                 :             :          *  FAIL |  X   |  X   |  C   |  X   |  X   |  X   |------|------|
    1442                 :             :          *  FC   |  X   |  X   |  X   |  X   |  X   |  X   |  X   |------|
    1443                 :             :          *  RST  |  X   |  X   |  X   |  X   |  X   |  X   |  O   |  O   |
    1444                 :             :          * ------|------|------|------|------|------|------|------|------|
    1445                 :             :          *
    1446                 :             :          * The same applies in mptcp_established_options() function.
    1447                 :             :          */
    1448         [ +  + ]:     1250692 :         if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
    1449                 :     1229824 :                 struct mptcp_ext *mpext = &opts->ext_copy;
    1450                 :     1229824 :                 u8 len = TCPOLEN_MPTCP_DSS_BASE;
    1451                 :     1229824 :                 u8 flags = 0;
    1452                 :             : 
    1453         [ +  - ]:     1229824 :                 if (mpext->use_ack) {
    1454                 :     1229824 :                         flags = MPTCP_DSS_HAS_ACK;
    1455         [ +  + ]:     1229824 :                         if (mpext->ack64) {
    1456                 :             :                                 len += TCPOLEN_MPTCP_DSS_ACK64;
    1457                 :             :                                 flags |= MPTCP_DSS_ACK64;
    1458                 :             :                         } else {
    1459                 :       58102 :                                 len += TCPOLEN_MPTCP_DSS_ACK32;
    1460                 :             :                         }
    1461                 :             :                 }
    1462                 :             : 
    1463         [ +  + ]:     1229824 :                 if (mpext->use_map) {
    1464                 :      661839 :                         len += TCPOLEN_MPTCP_DSS_MAP64;
    1465                 :             : 
    1466                 :             :                         /* Use only 64-bit mapping flags for now, add
    1467                 :             :                          * support for optional 32-bit mappings later.
    1468                 :             :                          */
    1469                 :      661839 :                         flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
    1470         [ +  + ]:      661839 :                         if (mpext->data_fin)
    1471                 :       16660 :                                 flags |= MPTCP_DSS_DATA_FIN;
    1472                 :             : 
    1473         [ +  + ]:      661839 :                         if (opts->csum_reqd)
    1474                 :       77550 :                                 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
    1475                 :             :                 }
    1476                 :             : 
    1477         [ +  - ]:     1229824 :                 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
    1478                 :             : 
    1479         [ +  - ]:     1229824 :                 if (mpext->use_ack) {
    1480                 :     1229824 :                         struct mptcp_sock *msk;
    1481                 :     1229824 :                         u64 ack_seq;
    1482                 :             : 
    1483                 :             :                         /* DSS option is set only by mptcp_established_option,
    1484                 :             :                          * the caller is __tcp_transmit_skb() and ssk is always
    1485                 :             :                          * not NULL.
    1486                 :             :                          */
    1487         [ -  + ]:     1229824 :                         subflow = mptcp_subflow_ctx(ssk);
    1488         [ -  + ]:     1229824 :                         msk = mptcp_sk(subflow->conn);
    1489                 :     1229824 :                         ack_seq = READ_ONCE(msk->ack_seq);
    1490         [ +  + ]:     1229824 :                         if (mpext->ack64) {
    1491                 :     1171722 :                                 put_unaligned_be64(ack_seq, ptr);
    1492                 :     1171722 :                                 ptr += 2;
    1493                 :             :                         } else {
    1494                 :       58102 :                                 put_unaligned_be32(ack_seq, ptr);
    1495                 :       58102 :                                 ptr += 1;
    1496                 :             :                         }
    1497                 :     1229824 :                         subflow->rcv_wnd_sent = mptcp_set_rwin(msk, tp, th,
    1498                 :             :                                                                ack_seq);
    1499                 :             :                 }
    1500                 :             : 
    1501         [ +  + ]:     1229824 :                 if (mpext->use_map) {
    1502         [ +  + ]:      661839 :                         put_unaligned_be64(mpext->data_seq, ptr);
    1503                 :      661839 :                         ptr += 2;
    1504         [ +  + ]:      661839 :                         put_unaligned_be32(mpext->subflow_seq, ptr);
    1505                 :      661839 :                         ptr += 1;
    1506         [ +  + ]:      661839 :                         if (opts->csum_reqd) {
    1507                 :             :                                 /* data_len == 0 is reserved for the infinite mapping,
    1508                 :             :                                  * the checksum will also be set to 0.
    1509                 :             :                                  */
    1510                 :       77550 :                                 put_len_csum(mpext->data_len,
    1511         [ +  + ]:       77550 :                                              (mpext->data_len ? mptcp_make_csum(mpext) : 0),
    1512                 :             :                                              ptr);
    1513                 :             :                         } else {
    1514                 :      584289 :                                 put_unaligned_be32(mpext->data_len << 16 |
    1515                 :      584289 :                                                    TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
    1516                 :             :                         }
    1517                 :      661839 :                         ptr += 1;
    1518                 :             :                 }
    1519                 :             : 
    1520                 :             :                 /* We might need to add MP_FAIL options in rare cases */
    1521         [ +  + ]:     1229824 :                 if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
    1522                 :           4 :                         goto mp_fail;
    1523         [ +  + ]:       20868 :         } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
    1524                 :        5698 :                 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
    1525                 :             : 
    1526         [ +  + ]:        5698 :                 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
    1527                 :             :                         len = TCPOLEN_MPTCP_MPC_SYN;
    1528         [ +  + ]:        3966 :                 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
    1529                 :             :                         len = TCPOLEN_MPTCP_MPC_SYNACK;
    1530         [ +  + ]:        2342 :                 } else if (opts->data_len) {
    1531                 :         876 :                         len = TCPOLEN_MPTCP_MPC_ACK_DATA;
    1532         [ +  + ]:         876 :                         if (opts->csum_reqd)
    1533                 :          80 :                                 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
    1534                 :             :                 } else {
    1535                 :             :                         len = TCPOLEN_MPTCP_MPC_ACK;
    1536                 :             :                 }
    1537                 :             : 
    1538         [ +  + ]:        5698 :                 if (opts->csum_reqd)
    1539                 :         454 :                         flag |= MPTCP_CAP_CHECKSUM_REQD;
    1540                 :             : 
    1541         [ +  + ]:        5698 :                 if (!opts->allow_join_id0)
    1542                 :          34 :                         flag |= MPTCP_CAP_DENY_JOIN_ID0;
    1543                 :             : 
    1544         [ +  + ]:        5698 :                 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
    1545                 :             :                                       MPTCP_SUPPORTED_VERSION,
    1546                 :             :                                       flag);
    1547                 :             : 
    1548                 :        5698 :                 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
    1549         [ +  + ]:        5698 :                     opts->suboptions))
    1550                 :        1732 :                         goto mp_capable_done;
    1551                 :             : 
    1552         [ +  + ]:        3966 :                 put_unaligned_be64(opts->sndr_key, ptr);
    1553                 :        3966 :                 ptr += 2;
    1554         [ +  + ]:        3966 :                 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
    1555                 :        1624 :                         goto mp_capable_done;
    1556                 :             : 
    1557         [ +  + ]:        2342 :                 put_unaligned_be64(opts->rcvr_key, ptr);
    1558                 :        2342 :                 ptr += 2;
    1559         [ +  + ]:        2342 :                 if (!opts->data_len)
    1560                 :        1466 :                         goto mp_capable_done;
    1561                 :             : 
    1562         [ +  + ]:         876 :                 if (opts->csum_reqd) {
    1563                 :          80 :                         put_len_csum(opts->data_len,
    1564                 :          80 :                                      __mptcp_make_csum(opts->data_seq,
    1565                 :             :                                                        opts->subflow_seq,
    1566                 :             :                                                        opts->data_len,
    1567                 :          80 :                                                        ~csum_unfold(opts->csum)),
    1568                 :             :                                      ptr);
    1569                 :             :                 } else {
    1570                 :         796 :                         put_unaligned_be32(opts->data_len << 16 |
    1571                 :         796 :                                            TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
    1572                 :             :                 }
    1573                 :         876 :                 ptr += 1;
    1574                 :             : 
    1575                 :             :                 /* MPC is additionally mutually exclusive with MP_PRIO */
    1576                 :         876 :                 goto mp_capable_done;
    1577         [ +  + ]:       15170 :         } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
    1578         [ +  + ]:        1538 :                 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
    1579                 :         568 :                         *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
    1580                 :             :                                               TCPOLEN_MPTCP_MPJ_SYN,
    1581                 :         568 :                                               opts->backup, opts->join_id);
    1582                 :         568 :                         put_unaligned_be32(opts->token, ptr);
    1583                 :         568 :                         ptr += 1;
    1584                 :         568 :                         put_unaligned_be32(opts->nonce, ptr);
    1585                 :         568 :                         ptr += 1;
    1586         [ +  + ]:         970 :                 } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
    1587                 :         496 :                         *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
    1588                 :             :                                               TCPOLEN_MPTCP_MPJ_SYNACK,
    1589                 :         496 :                                               opts->backup, opts->join_id);
    1590                 :         496 :                         put_unaligned_be64(opts->thmac, ptr);
    1591                 :         496 :                         ptr += 2;
    1592                 :         496 :                         put_unaligned_be32(opts->nonce, ptr);
    1593                 :         496 :                         ptr += 1;
    1594                 :             :                 } else {
    1595                 :         474 :                         *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
    1596                 :             :                                               TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
    1597                 :         474 :                         memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
    1598                 :         474 :                         ptr += 5;
    1599                 :             :                 }
    1600         [ +  + ]:       13632 :         } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
    1601                 :         853 :                 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
    1602                 :         853 :                 u8 echo = MPTCP_ADDR_ECHO;
    1603                 :             : 
    1604                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1605         [ +  + ]:         853 :                 if (opts->addr.family == AF_INET6)
    1606                 :         198 :                         len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
    1607                 :             : #endif
    1608                 :             : 
    1609         [ +  + ]:         853 :                 if (opts->addr.port)
    1610                 :          86 :                         len += TCPOLEN_MPTCP_PORT_LEN;
    1611                 :             : 
    1612         [ +  + ]:         853 :                 if (opts->ahmac) {
    1613                 :         459 :                         len += sizeof(opts->ahmac);
    1614                 :         459 :                         echo = 0;
    1615                 :             :                 }
    1616                 :             : 
    1617                 :         853 :                 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
    1618         [ +  + ]:         853 :                                       len, echo, opts->addr.id);
    1619         [ +  + ]:         853 :                 if (opts->addr.family == AF_INET) {
    1620                 :         655 :                         memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
    1621                 :         655 :                         ptr += 1;
    1622                 :             :                 }
    1623                 :             : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
    1624         [ +  - ]:         198 :                 else if (opts->addr.family == AF_INET6) {
    1625                 :         198 :                         memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
    1626                 :         198 :                         ptr += 4;
    1627                 :             :                 }
    1628                 :             : #endif
    1629                 :             : 
    1630         [ +  + ]:         853 :                 if (!opts->addr.port) {
    1631         [ +  + ]:         767 :                         if (opts->ahmac) {
    1632                 :         413 :                                 put_unaligned_be64(opts->ahmac, ptr);
    1633                 :         413 :                                 ptr += 2;
    1634                 :             :                         }
    1635                 :             :                 } else {
    1636                 :          86 :                         u16 port = ntohs(opts->addr.port);
    1637                 :             : 
    1638         [ +  + ]:          86 :                         if (opts->ahmac) {
    1639                 :          46 :                                 u8 *bptr = (u8 *)ptr;
    1640                 :             : 
    1641                 :          46 :                                 put_unaligned_be16(port, bptr);
    1642                 :          46 :                                 bptr += 2;
    1643                 :          46 :                                 put_unaligned_be64(opts->ahmac, bptr);
    1644                 :          46 :                                 bptr += 8;
    1645                 :          46 :                                 put_unaligned_be16(TCPOPT_NOP << 8 |
    1646                 :             :                                                    TCPOPT_NOP, bptr);
    1647                 :             : 
    1648                 :          46 :                                 ptr += 3;
    1649                 :             :                         } else {
    1650                 :          40 :                                 put_unaligned_be32(port << 16 |
    1651                 :          40 :                                                    TCPOPT_NOP << 8 |
    1652                 :             :                                                    TCPOPT_NOP, ptr);
    1653                 :          40 :                                 ptr += 1;
    1654                 :             :                         }
    1655                 :             :                 }
    1656         [ +  + ]:       12779 :         } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
    1657                 :             :                 /* FASTCLOSE is mutually exclusive with others except RST */
    1658                 :         444 :                 *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
    1659                 :             :                                       TCPOLEN_MPTCP_FASTCLOSE,
    1660                 :             :                                       0, 0);
    1661         [ +  - ]:         444 :                 put_unaligned_be64(opts->rcvr_key, ptr);
    1662                 :         444 :                 ptr += 2;
    1663                 :             : 
    1664         [ +  - ]:         444 :                 if (OPTION_MPTCP_RST & opts->suboptions)
    1665                 :         444 :                         goto mp_rst;
    1666                 :             :                 return;
    1667         [ +  + ]:       12335 :         } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
    1668                 :           2 : mp_fail:
    1669                 :             :                 /* MP_FAIL is mutually exclusive with others except RST */
    1670         [ +  + ]:           6 :                 subflow = mptcp_subflow_ctx(ssk);
    1671                 :           6 :                 subflow->send_mp_fail = 0;
    1672                 :             : 
    1673                 :           6 :                 *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
    1674                 :             :                                       TCPOLEN_MPTCP_FAIL,
    1675                 :             :                                       0, 0);
    1676         [ +  + ]:           6 :                 put_unaligned_be64(opts->fail_seq, ptr);
    1677                 :           6 :                 ptr += 2;
    1678                 :             : 
    1679         [ +  + ]:           6 :                 if (OPTION_MPTCP_RST & opts->suboptions)
    1680                 :           2 :                         goto mp_rst;
    1681                 :             :                 return;
    1682         [ +  + ]:       12333 :         } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
    1683                 :          57 : mp_rst:
    1684                 :         503 :                 *ptr++ = mptcp_option(MPTCPOPT_RST,
    1685                 :             :                                       TCPOLEN_MPTCP_RST,
    1686                 :         503 :                                       opts->reset_transient,
    1687                 :         503 :                                       opts->reset_reason);
    1688                 :         503 :                 return;
    1689         [ +  - ]:       12276 :         } else if (unlikely(!opts->suboptions)) {
    1690                 :             :                 /* Fallback to TCP */
    1691                 :       12276 :                 mptcp_track_rwin(tp);
    1692                 :       12276 :                 return;
    1693                 :             :         }
    1694                 :             : 
    1695         [ +  + ]:     1232211 :         if (OPTION_MPTCP_PRIO & opts->suboptions) {
    1696         [ +  - ]:          28 :                 subflow = mptcp_subflow_ctx(ssk);
    1697                 :          28 :                 subflow->send_mp_prio = 0;
    1698                 :             : 
    1699                 :          28 :                 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
    1700                 :             :                                       TCPOLEN_MPTCP_PRIO,
    1701         [ +  - ]:          28 :                                       opts->backup, TCPOPT_NOP);
    1702                 :             : 
    1703         [ +  - ]:          28 :                 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
    1704                 :             :         }
    1705                 :             : 
    1706                 :     1232183 : mp_capable_done:
    1707         [ +  + ]:     1237909 :         if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
    1708                 :         108 :                 u8 i = 1;
    1709                 :             : 
    1710                 :         108 :                 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
    1711                 :         108 :                                       TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
    1712                 :         108 :                                       0, opts->rm_list.ids[0]);
    1713                 :             : 
    1714         [ +  + ]:         112 :                 while (i < opts->rm_list.nr) {
    1715                 :           4 :                         u8 id1, id2, id3, id4;
    1716                 :             : 
    1717                 :           4 :                         id1 = opts->rm_list.ids[i];
    1718         [ +  - ]:           4 :                         id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
    1719         [ -  + ]:           4 :                         id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
    1720         [ -  + ]:           4 :                         id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
    1721                 :           4 :                         put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
    1722                 :           4 :                         ptr += 1;
    1723                 :           4 :                         i += 4;
    1724                 :             :                 }
    1725                 :             :         }
    1726                 :             : }
    1727                 :             : 
    1728                 :          16 : __be32 mptcp_get_reset_option(const struct sk_buff *skb)
    1729                 :             : {
    1730         [ +  - ]:          16 :         const struct mptcp_ext *ext = mptcp_get_ext(skb);
    1731                 :          16 :         u8 flags, reason;
    1732                 :             : 
    1733         [ +  - ]:          16 :         if (ext) {
    1734                 :          16 :                 flags = ext->reset_transient;
    1735                 :          16 :                 reason = ext->reset_reason;
    1736                 :             : 
    1737                 :          16 :                 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
    1738                 :             :                                     flags, reason);
    1739                 :             :         }
    1740                 :             : 
    1741                 :             :         return htonl(0u);
    1742                 :             : }
    1743                 :             : EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
        

Generated by: LCOV version 2.3.1-1