LCOV - code coverage report
Current view: top level - mptcp/options.c (source / functions) Coverage Total Hit
Test: export-net Lines: 98.3 % 900 885
Test Date: 2025-11-14 12:52:17 Functions: 100.0 % 29 29
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 76.0 % 739 562

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

Generated by: LCOV version 2.3.1-1