LCOV - code coverage report
Current view: top level - mptcp/options.c (source / functions) Coverage Total Hit
Test: export Lines: 98.4 % 872 858
Test Date: 2024-10-18 11:08:02 Functions: 100.0 % 27 27
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 77.9 % 687 535

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

Generated by: LCOV version 2.0-1