1eda7acddSPeter Krystad // SPDX-License-Identifier: GPL-2.0 2eda7acddSPeter Krystad /* Multipath TCP 3eda7acddSPeter Krystad * 4eda7acddSPeter Krystad * Copyright (c) 2017 - 2019, Intel Corporation. 5eda7acddSPeter Krystad */ 6eda7acddSPeter Krystad 7c85adcedSGeliang Tang #define pr_fmt(fmt) "MPTCP: " fmt 8c85adcedSGeliang Tang 9eda7acddSPeter Krystad #include <linux/kernel.h> 10a24d22b2SEric Biggers #include <crypto/sha2.h> 11eda7acddSPeter Krystad #include <net/tcp.h> 12eda7acddSPeter Krystad #include <net/mptcp.h> 13eda7acddSPeter Krystad #include "protocol.h" 14a877de06SGeliang Tang #include "mib.h" 15eda7acddSPeter Krystad 16ed66bfb4SGeliang Tang #include <trace/events/mptcp.h> 17ed66bfb4SGeliang Tang 1865492c5aSPaolo Abeni static bool mptcp_cap_flag_sha256(u8 flags) 1965492c5aSPaolo Abeni { 2065492c5aSPaolo Abeni return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256; 2165492c5aSPaolo Abeni } 2265492c5aSPaolo Abeni 23cfde141eSPaolo Abeni static void mptcp_parse_option(const struct sk_buff *skb, 24cfde141eSPaolo Abeni const unsigned char *ptr, int opsize, 25cfde141eSPaolo Abeni struct mptcp_options_received *mp_opt) 26eda7acddSPeter Krystad { 27eda7acddSPeter Krystad u8 subtype = *ptr >> 4; 28648ef4b8SMat Martineau int expected_opsize; 29eda7acddSPeter Krystad u8 version; 30eda7acddSPeter Krystad u8 flags; 315c4a824dSGeliang Tang u8 i; 32eda7acddSPeter Krystad 33eda7acddSPeter Krystad switch (subtype) { 34eda7acddSPeter Krystad case MPTCPOPT_MP_CAPABLE: 35cc7972eaSChristoph Paasch /* strict size checking */ 36cc7972eaSChristoph Paasch if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 37cc7972eaSChristoph Paasch if (skb->len > tcp_hdr(skb)->doff << 2) 38cc7972eaSChristoph Paasch expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA; 39cc7972eaSChristoph Paasch else 40cc7972eaSChristoph Paasch expected_opsize = TCPOLEN_MPTCP_MPC_ACK; 41cc7972eaSChristoph Paasch } else { 42cc7972eaSChristoph Paasch if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) 43cc7972eaSChristoph Paasch expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK; 44cc7972eaSChristoph Paasch else 45cc7972eaSChristoph Paasch expected_opsize = TCPOLEN_MPTCP_MPC_SYN; 46cc7972eaSChristoph Paasch } 47208e8f66SGeliang Tang 48208e8f66SGeliang Tang /* Cfr RFC 8684 Section 3.3.0: 49208e8f66SGeliang Tang * If a checksum is present but its use had 50208e8f66SGeliang Tang * not been negotiated in the MP_CAPABLE handshake, the receiver MUST 51208e8f66SGeliang Tang * close the subflow with a RST, as it is not behaving as negotiated. 52208e8f66SGeliang Tang * If a checksum is not present when its use has been negotiated, the 53208e8f66SGeliang Tang * receiver MUST close the subflow with a RST, as it is considered 54208e8f66SGeliang Tang * broken 55208e8f66SGeliang Tang * We parse even option with mismatching csum presence, so that 56208e8f66SGeliang Tang * later in subflow_data_ready we can trigger the reset. 57208e8f66SGeliang Tang */ 58208e8f66SGeliang Tang if (opsize != expected_opsize && 59208e8f66SGeliang Tang (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA || 60208e8f66SGeliang Tang opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM)) 61eda7acddSPeter Krystad break; 62eda7acddSPeter Krystad 63cc7972eaSChristoph Paasch /* try to be gentle vs future versions on the initial syn */ 64eda7acddSPeter Krystad version = *ptr++ & MPTCP_VERSION_MASK; 65cc7972eaSChristoph Paasch if (opsize != TCPOLEN_MPTCP_MPC_SYN) { 66eda7acddSPeter Krystad if (version != MPTCP_SUPPORTED_VERSION) 67eda7acddSPeter Krystad break; 68cc7972eaSChristoph Paasch } else if (version < MPTCP_SUPPORTED_VERSION) { 69cc7972eaSChristoph Paasch break; 70cc7972eaSChristoph Paasch } 71eda7acddSPeter Krystad 72eda7acddSPeter Krystad flags = *ptr++; 7365492c5aSPaolo Abeni if (!mptcp_cap_flag_sha256(flags) || 74eda7acddSPeter Krystad (flags & MPTCP_CAP_EXTENSIBILITY)) 75eda7acddSPeter Krystad break; 76eda7acddSPeter Krystad 77eda7acddSPeter Krystad /* RFC 6824, Section 3.1: 78eda7acddSPeter Krystad * "For the Checksum Required bit (labeled "A"), if either 79eda7acddSPeter Krystad * host requires the use of checksums, checksums MUST be used. 80eda7acddSPeter Krystad * In other words, the only way for checksums not to be used 81eda7acddSPeter Krystad * is if both hosts in their SYNs set A=0." 82eda7acddSPeter Krystad */ 83eda7acddSPeter Krystad if (flags & MPTCP_CAP_CHECKSUM_REQD) 8474c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD; 85eda7acddSPeter Krystad 86a086aebaSPaolo Abeni mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0); 87df377be3SGeliang Tang 8874c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTIONS_MPTCP_MPC; 89cc7972eaSChristoph Paasch if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) { 90eda7acddSPeter Krystad mp_opt->sndr_key = get_unaligned_be64(ptr); 91eda7acddSPeter Krystad ptr += 8; 92cc7972eaSChristoph Paasch } 93cc7972eaSChristoph Paasch if (opsize >= TCPOLEN_MPTCP_MPC_ACK) { 94eda7acddSPeter Krystad mp_opt->rcvr_key = get_unaligned_be64(ptr); 95eda7acddSPeter Krystad ptr += 8; 96eda7acddSPeter Krystad } 97208e8f66SGeliang Tang if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) { 98cc7972eaSChristoph Paasch /* Section 3.1.: 99cc7972eaSChristoph Paasch * "the data parameters in a MP_CAPABLE are semantically 100cc7972eaSChristoph Paasch * equivalent to those in a DSS option and can be used 101cc7972eaSChristoph Paasch * interchangeably." 102cc7972eaSChristoph Paasch */ 10374c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_DSS; 104cc7972eaSChristoph Paasch mp_opt->use_map = 1; 105cc7972eaSChristoph Paasch mp_opt->mpc_map = 1; 106cc7972eaSChristoph Paasch mp_opt->data_len = get_unaligned_be16(ptr); 107cc7972eaSChristoph Paasch ptr += 2; 108cc7972eaSChristoph Paasch } 109208e8f66SGeliang Tang if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) { 110208e8f66SGeliang Tang mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr); 11174c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD; 112208e8f66SGeliang Tang ptr += 2; 113208e8f66SGeliang Tang } 114208e8f66SGeliang Tang pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u", 115cc7972eaSChristoph Paasch version, flags, opsize, mp_opt->sndr_key, 116208e8f66SGeliang Tang mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum); 117eda7acddSPeter Krystad break; 118eda7acddSPeter Krystad 119f296234cSPeter Krystad case MPTCPOPT_MP_JOIN: 12074c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTIONS_MPTCP_MPJ; 121f296234cSPeter Krystad if (opsize == TCPOLEN_MPTCP_MPJ_SYN) { 122f296234cSPeter Krystad mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP; 123f296234cSPeter Krystad mp_opt->join_id = *ptr++; 124f296234cSPeter Krystad mp_opt->token = get_unaligned_be32(ptr); 125f296234cSPeter Krystad ptr += 4; 126f296234cSPeter Krystad mp_opt->nonce = get_unaligned_be32(ptr); 127f296234cSPeter Krystad ptr += 4; 128f296234cSPeter Krystad pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u", 129f296234cSPeter Krystad mp_opt->backup, mp_opt->join_id, 130f296234cSPeter Krystad mp_opt->token, mp_opt->nonce); 131f296234cSPeter Krystad } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) { 132f296234cSPeter Krystad mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP; 133f296234cSPeter Krystad mp_opt->join_id = *ptr++; 134f296234cSPeter Krystad mp_opt->thmac = get_unaligned_be64(ptr); 135f296234cSPeter Krystad ptr += 8; 136f296234cSPeter Krystad mp_opt->nonce = get_unaligned_be32(ptr); 137f296234cSPeter Krystad ptr += 4; 138f296234cSPeter Krystad pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u", 139f296234cSPeter Krystad mp_opt->backup, mp_opt->join_id, 140f296234cSPeter Krystad mp_opt->thmac, mp_opt->nonce); 141f296234cSPeter Krystad } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) { 142f296234cSPeter Krystad ptr += 2; 143f296234cSPeter Krystad memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN); 144f296234cSPeter Krystad pr_debug("MP_JOIN hmac"); 145f296234cSPeter Krystad } else { 14674c7dfbeSPaolo Abeni mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ; 147f296234cSPeter Krystad } 148f296234cSPeter Krystad break; 149f296234cSPeter Krystad 150eda7acddSPeter Krystad case MPTCPOPT_DSS: 151eda7acddSPeter Krystad pr_debug("DSS"); 152648ef4b8SMat Martineau ptr++; 153648ef4b8SMat Martineau 154cc7972eaSChristoph Paasch /* we must clear 'mpc_map' be able to detect MP_CAPABLE 155cc7972eaSChristoph Paasch * map vs DSS map in mptcp_incoming_options(), and reconstruct 156cc7972eaSChristoph Paasch * map info accordingly 157cc7972eaSChristoph Paasch */ 158cc7972eaSChristoph Paasch mp_opt->mpc_map = 0; 159648ef4b8SMat Martineau flags = (*ptr++) & MPTCP_DSS_FLAG_MASK; 160648ef4b8SMat Martineau mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0; 161648ef4b8SMat Martineau mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0; 162648ef4b8SMat Martineau mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0; 163648ef4b8SMat Martineau mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0; 164648ef4b8SMat Martineau mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK); 165648ef4b8SMat Martineau 166648ef4b8SMat Martineau pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d", 167648ef4b8SMat Martineau mp_opt->data_fin, mp_opt->dsn64, 168648ef4b8SMat Martineau mp_opt->use_map, mp_opt->ack64, 169648ef4b8SMat Martineau mp_opt->use_ack); 170648ef4b8SMat Martineau 171648ef4b8SMat Martineau expected_opsize = TCPOLEN_MPTCP_DSS_BASE; 172648ef4b8SMat Martineau 173648ef4b8SMat Martineau if (mp_opt->use_ack) { 174648ef4b8SMat Martineau if (mp_opt->ack64) 175648ef4b8SMat Martineau expected_opsize += TCPOLEN_MPTCP_DSS_ACK64; 176648ef4b8SMat Martineau else 177648ef4b8SMat Martineau expected_opsize += TCPOLEN_MPTCP_DSS_ACK32; 178648ef4b8SMat Martineau } 179648ef4b8SMat Martineau 180648ef4b8SMat Martineau if (mp_opt->use_map) { 181648ef4b8SMat Martineau if (mp_opt->dsn64) 182648ef4b8SMat Martineau expected_opsize += TCPOLEN_MPTCP_DSS_MAP64; 183648ef4b8SMat Martineau else 184648ef4b8SMat Martineau expected_opsize += TCPOLEN_MPTCP_DSS_MAP32; 185648ef4b8SMat Martineau } 186648ef4b8SMat Martineau 187390b95a5SGeliang Tang /* Always parse any csum presence combination, we will enforce 188390b95a5SGeliang Tang * RFC 8684 Section 3.3.0 checks later in subflow_data_ready 189648ef4b8SMat Martineau */ 190648ef4b8SMat Martineau if (opsize != expected_opsize && 191648ef4b8SMat Martineau opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) 192648ef4b8SMat Martineau break; 193648ef4b8SMat Martineau 19474c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_DSS; 195648ef4b8SMat Martineau if (mp_opt->use_ack) { 196648ef4b8SMat Martineau if (mp_opt->ack64) { 197648ef4b8SMat Martineau mp_opt->data_ack = get_unaligned_be64(ptr); 198648ef4b8SMat Martineau ptr += 8; 199648ef4b8SMat Martineau } else { 200648ef4b8SMat Martineau mp_opt->data_ack = get_unaligned_be32(ptr); 201648ef4b8SMat Martineau ptr += 4; 202648ef4b8SMat Martineau } 203648ef4b8SMat Martineau 204648ef4b8SMat Martineau pr_debug("data_ack=%llu", mp_opt->data_ack); 205648ef4b8SMat Martineau } 206648ef4b8SMat Martineau 207648ef4b8SMat Martineau if (mp_opt->use_map) { 208648ef4b8SMat Martineau if (mp_opt->dsn64) { 209648ef4b8SMat Martineau mp_opt->data_seq = get_unaligned_be64(ptr); 210648ef4b8SMat Martineau ptr += 8; 211648ef4b8SMat Martineau } else { 212648ef4b8SMat Martineau mp_opt->data_seq = get_unaligned_be32(ptr); 213648ef4b8SMat Martineau ptr += 4; 214648ef4b8SMat Martineau } 215648ef4b8SMat Martineau 216648ef4b8SMat Martineau mp_opt->subflow_seq = get_unaligned_be32(ptr); 217648ef4b8SMat Martineau ptr += 4; 218648ef4b8SMat Martineau 219648ef4b8SMat Martineau mp_opt->data_len = get_unaligned_be16(ptr); 220648ef4b8SMat Martineau ptr += 2; 221648ef4b8SMat Martineau 222390b95a5SGeliang Tang if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) { 22374c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD; 224390b95a5SGeliang Tang mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr); 225390b95a5SGeliang Tang ptr += 2; 226390b95a5SGeliang Tang } 227390b95a5SGeliang Tang 228390b95a5SGeliang Tang pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u", 229648ef4b8SMat Martineau mp_opt->data_seq, mp_opt->subflow_seq, 23074c7dfbeSPaolo Abeni mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD), 23174c7dfbeSPaolo Abeni mp_opt->csum); 232648ef4b8SMat Martineau } 233648ef4b8SMat Martineau 234eda7acddSPeter Krystad break; 235eda7acddSPeter Krystad 2363df523abSPeter Krystad case MPTCPOPT_ADD_ADDR: 2373df523abSPeter Krystad mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO; 2383df523abSPeter Krystad if (!mp_opt->echo) { 2393df523abSPeter Krystad if (opsize == TCPOLEN_MPTCP_ADD_ADDR || 2403df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT) 2411b1a6ef5SGeliang Tang mp_opt->addr.family = AF_INET; 2423df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2433df523abSPeter Krystad else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 || 2443df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT) 2451b1a6ef5SGeliang Tang mp_opt->addr.family = AF_INET6; 2463df523abSPeter Krystad #endif 2473df523abSPeter Krystad else 2483df523abSPeter Krystad break; 2493df523abSPeter Krystad } else { 2503df523abSPeter Krystad if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE || 2513df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) 2521b1a6ef5SGeliang Tang mp_opt->addr.family = AF_INET; 2533df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2543df523abSPeter Krystad else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE || 2553df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) 2561b1a6ef5SGeliang Tang mp_opt->addr.family = AF_INET6; 2573df523abSPeter Krystad #endif 2583df523abSPeter Krystad else 2593df523abSPeter Krystad break; 2603df523abSPeter Krystad } 2613df523abSPeter Krystad 26274c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR; 263f7dafee1SGeliang Tang mp_opt->addr.id = *ptr++; 264a086aebaSPaolo Abeni mp_opt->addr.port = 0; 265a086aebaSPaolo Abeni mp_opt->ahmac = 0; 2661b1a6ef5SGeliang Tang if (mp_opt->addr.family == AF_INET) { 267f7dafee1SGeliang Tang memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4); 2683df523abSPeter Krystad ptr += 4; 2693df523abSPeter Krystad if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT || 2703df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) { 271f7dafee1SGeliang Tang mp_opt->addr.port = htons(get_unaligned_be16(ptr)); 2723df523abSPeter Krystad ptr += 2; 2733df523abSPeter Krystad } 2743df523abSPeter Krystad } 2753df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2763df523abSPeter Krystad else { 277f7dafee1SGeliang Tang memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16); 2783df523abSPeter Krystad ptr += 16; 2793df523abSPeter Krystad if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT || 2803df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) { 281f7dafee1SGeliang Tang mp_opt->addr.port = htons(get_unaligned_be16(ptr)); 2823df523abSPeter Krystad ptr += 2; 2833df523abSPeter Krystad } 2843df523abSPeter Krystad } 2853df523abSPeter Krystad #endif 2863df523abSPeter Krystad if (!mp_opt->echo) { 2873df523abSPeter Krystad mp_opt->ahmac = get_unaligned_be64(ptr); 2883df523abSPeter Krystad ptr += 8; 2893df523abSPeter Krystad } 29090a4aea8SGeliang Tang pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d", 2911b1a6ef5SGeliang Tang (mp_opt->addr.family == AF_INET6) ? "6" : "", 292f7dafee1SGeliang Tang mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port)); 2933df523abSPeter Krystad break; 2943df523abSPeter Krystad 2953df523abSPeter Krystad case MPTCPOPT_RM_ADDR: 2965c4a824dSGeliang Tang if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 || 2975c4a824dSGeliang Tang opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX) 2983df523abSPeter Krystad break; 2993df523abSPeter Krystad 3008e60eed6SGeliang Tang ptr++; 3018e60eed6SGeliang Tang 30274c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR; 3035c4a824dSGeliang Tang mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE; 3045c4a824dSGeliang Tang for (i = 0; i < mp_opt->rm_list.nr; i++) 3055c4a824dSGeliang Tang mp_opt->rm_list.ids[i] = *ptr++; 3065c4a824dSGeliang Tang pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr); 3073df523abSPeter Krystad break; 3083df523abSPeter Krystad 30940453a5cSGeliang Tang case MPTCPOPT_MP_PRIO: 31040453a5cSGeliang Tang if (opsize != TCPOLEN_MPTCP_PRIO) 31140453a5cSGeliang Tang break; 31240453a5cSGeliang Tang 31374c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_PRIO; 31440453a5cSGeliang Tang mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP; 31540453a5cSGeliang Tang pr_debug("MP_PRIO: prio=%d", mp_opt->backup); 31640453a5cSGeliang Tang break; 31740453a5cSGeliang Tang 31850c504a2SFlorian Westphal case MPTCPOPT_MP_FASTCLOSE: 31950c504a2SFlorian Westphal if (opsize != TCPOLEN_MPTCP_FASTCLOSE) 32050c504a2SFlorian Westphal break; 32150c504a2SFlorian Westphal 32250c504a2SFlorian Westphal ptr += 2; 32350c504a2SFlorian Westphal mp_opt->rcvr_key = get_unaligned_be64(ptr); 32450c504a2SFlorian Westphal ptr += 8; 32574c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE; 32650c504a2SFlorian Westphal break; 32750c504a2SFlorian Westphal 328dc87efdbSFlorian Westphal case MPTCPOPT_RST: 329dc87efdbSFlorian Westphal if (opsize != TCPOLEN_MPTCP_RST) 330dc87efdbSFlorian Westphal break; 331dc87efdbSFlorian Westphal 332dc87efdbSFlorian Westphal if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) 333dc87efdbSFlorian Westphal break; 33474c7dfbeSPaolo Abeni 33574c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_RST; 336dc87efdbSFlorian Westphal flags = *ptr++; 337dc87efdbSFlorian Westphal mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT; 338dc87efdbSFlorian Westphal mp_opt->reset_reason = *ptr; 339dc87efdbSFlorian Westphal break; 340dc87efdbSFlorian Westphal 3415580d41bSGeliang Tang case MPTCPOPT_MP_FAIL: 3425580d41bSGeliang Tang if (opsize != TCPOLEN_MPTCP_FAIL) 3435580d41bSGeliang Tang break; 3445580d41bSGeliang Tang 3455580d41bSGeliang Tang ptr += 2; 34674c7dfbeSPaolo Abeni mp_opt->suboptions |= OPTION_MPTCP_FAIL; 3475580d41bSGeliang Tang mp_opt->fail_seq = get_unaligned_be64(ptr); 3485580d41bSGeliang Tang pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq); 3495580d41bSGeliang Tang break; 3505580d41bSGeliang Tang 351eda7acddSPeter Krystad default: 352eda7acddSPeter Krystad break; 353eda7acddSPeter Krystad } 354eda7acddSPeter Krystad } 355eda7acddSPeter Krystad 356c863225bSGeliang Tang void mptcp_get_options(const struct sock *sk, 357c863225bSGeliang Tang const struct sk_buff *skb, 358cfde141eSPaolo Abeni struct mptcp_options_received *mp_opt) 359cec37a6eSPeter Krystad { 360cec37a6eSPeter Krystad const struct tcphdr *th = tcp_hdr(skb); 361cfde141eSPaolo Abeni const unsigned char *ptr; 362cfde141eSPaolo Abeni int length; 363cec37a6eSPeter Krystad 364cfde141eSPaolo Abeni /* initialize option status */ 36574c7dfbeSPaolo Abeni mp_opt->suboptions = 0; 366cfde141eSPaolo Abeni 367cfde141eSPaolo Abeni length = (th->doff * 4) - sizeof(struct tcphdr); 368cec37a6eSPeter Krystad ptr = (const unsigned char *)(th + 1); 369cec37a6eSPeter Krystad 370cec37a6eSPeter Krystad while (length > 0) { 371cec37a6eSPeter Krystad int opcode = *ptr++; 372cec37a6eSPeter Krystad int opsize; 373cec37a6eSPeter Krystad 374cec37a6eSPeter Krystad switch (opcode) { 375cec37a6eSPeter Krystad case TCPOPT_EOL: 376cec37a6eSPeter Krystad return; 377cec37a6eSPeter Krystad case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ 378cec37a6eSPeter Krystad length--; 379cec37a6eSPeter Krystad continue; 380cec37a6eSPeter Krystad default: 38107718be2SMaxim Mikityanskiy if (length < 2) 38207718be2SMaxim Mikityanskiy return; 383cec37a6eSPeter Krystad opsize = *ptr++; 384cec37a6eSPeter Krystad if (opsize < 2) /* "silly options" */ 385cec37a6eSPeter Krystad return; 386cec37a6eSPeter Krystad if (opsize > length) 387cec37a6eSPeter Krystad return; /* don't parse partial options */ 388cec37a6eSPeter Krystad if (opcode == TCPOPT_MPTCP) 389cfde141eSPaolo Abeni mptcp_parse_option(skb, ptr, opsize, mp_opt); 390cec37a6eSPeter Krystad ptr += opsize - 2; 391cec37a6eSPeter Krystad length -= opsize; 392cec37a6eSPeter Krystad } 393cec37a6eSPeter Krystad } 394cec37a6eSPeter Krystad } 395cec37a6eSPeter Krystad 396cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb, 397cc7972eaSChristoph Paasch unsigned int *size, struct mptcp_out_options *opts) 398cec37a6eSPeter Krystad { 399cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 400cec37a6eSPeter Krystad 401cc7972eaSChristoph Paasch /* we will use snd_isn to detect first pkt [re]transmission 402cc7972eaSChristoph Paasch * in mptcp_established_options_mp() 403cc7972eaSChristoph Paasch */ 404cc7972eaSChristoph Paasch subflow->snd_isn = TCP_SKB_CB(skb)->end_seq; 405cec37a6eSPeter Krystad if (subflow->request_mptcp) { 406cec37a6eSPeter Krystad opts->suboptions = OPTION_MPTCP_MPC_SYN; 40706fe1719SGeliang Tang opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk)); 408bab6b88eSGeliang Tang opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk)); 409cec37a6eSPeter Krystad *size = TCPOLEN_MPTCP_MPC_SYN; 410cec37a6eSPeter Krystad return true; 411ec3edaa7SPeter Krystad } else if (subflow->request_join) { 412ec3edaa7SPeter Krystad pr_debug("remote_token=%u, nonce=%u", subflow->remote_token, 413ec3edaa7SPeter Krystad subflow->local_nonce); 414ec3edaa7SPeter Krystad opts->suboptions = OPTION_MPTCP_MPJ_SYN; 415ec3edaa7SPeter Krystad opts->join_id = subflow->local_id; 416ec3edaa7SPeter Krystad opts->token = subflow->remote_token; 417ec3edaa7SPeter Krystad opts->nonce = subflow->local_nonce; 418ec3edaa7SPeter Krystad opts->backup = subflow->request_bkup; 419ec3edaa7SPeter Krystad *size = TCPOLEN_MPTCP_MPJ_SYN; 420ec3edaa7SPeter Krystad return true; 421cec37a6eSPeter Krystad } 422cec37a6eSPeter Krystad return false; 423cec37a6eSPeter Krystad } 424cec37a6eSPeter Krystad 425ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk) 426ec3edaa7SPeter Krystad { 427ec3edaa7SPeter Krystad struct inet_connection_sock *icsk = inet_csk(sk); 428ec3edaa7SPeter Krystad 429ec3edaa7SPeter Krystad sk_stop_timer(sk, &icsk->icsk_delack_timer); 430ec3edaa7SPeter Krystad icsk->icsk_ack.timeout = 0; 431ec3edaa7SPeter Krystad icsk->icsk_ack.ato = 0; 432ec3edaa7SPeter Krystad icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER); 433ec3edaa7SPeter Krystad } 434ec3edaa7SPeter Krystad 435cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb, 436d87903b6SPaolo Abeni bool snd_data_fin_enable, 437cc7972eaSChristoph Paasch unsigned int *size, 4386d0060f6SMat Martineau unsigned int remaining, 439cec37a6eSPeter Krystad struct mptcp_out_options *opts) 440cec37a6eSPeter Krystad { 441cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 44206fe1719SGeliang Tang struct mptcp_sock *msk = mptcp_sk(subflow->conn); 443cc7972eaSChristoph Paasch struct mptcp_ext *mpext; 444cc7972eaSChristoph Paasch unsigned int data_len; 445c94b1f96SGeliang Tang u8 len; 446cec37a6eSPeter Krystad 447ec3edaa7SPeter Krystad /* When skb is not available, we better over-estimate the emitted 448ec3edaa7SPeter Krystad * options len. A full DSS option (28 bytes) is longer than 449ec3edaa7SPeter Krystad * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so 450ec3edaa7SPeter Krystad * tell the caller to defer the estimate to 451ec3edaa7SPeter Krystad * mptcp_established_options_dss(), which will reserve enough space. 452cc7972eaSChristoph Paasch */ 453ec3edaa7SPeter Krystad if (!skb) 454ec3edaa7SPeter Krystad return false; 455ec3edaa7SPeter Krystad 456d87903b6SPaolo Abeni /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */ 457d87903b6SPaolo Abeni if (subflow->fully_established || snd_data_fin_enable || 458d87903b6SPaolo Abeni subflow->snd_isn != TCP_SKB_CB(skb)->seq || 459d87903b6SPaolo Abeni sk->sk_state != TCP_ESTABLISHED) 460ec3edaa7SPeter Krystad return false; 461ec3edaa7SPeter Krystad 462ec3edaa7SPeter Krystad if (subflow->mp_capable) { 463cc7972eaSChristoph Paasch mpext = mptcp_get_ext(skb); 464cc7972eaSChristoph Paasch data_len = mpext ? mpext->data_len : 0; 465cc7972eaSChristoph Paasch 466f7cc8890SDavide Caratti /* we will check ops->data_len in mptcp_write_options() to 467cc7972eaSChristoph Paasch * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and 468cc7972eaSChristoph Paasch * TCPOLEN_MPTCP_MPC_ACK 469cc7972eaSChristoph Paasch */ 470f7cc8890SDavide Caratti opts->data_len = data_len; 471cec37a6eSPeter Krystad opts->suboptions = OPTION_MPTCP_MPC_ACK; 472cec37a6eSPeter Krystad opts->sndr_key = subflow->local_key; 473cec37a6eSPeter Krystad opts->rcvr_key = subflow->remote_key; 47406fe1719SGeliang Tang opts->csum_reqd = READ_ONCE(msk->csum_enabled); 475bab6b88eSGeliang Tang opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk)); 476cc7972eaSChristoph Paasch 477cc7972eaSChristoph Paasch /* Section 3.1. 478cc7972eaSChristoph Paasch * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK 479cc7972eaSChristoph Paasch * packets that start the first subflow of an MPTCP connection, 480cc7972eaSChristoph Paasch * as well as the first packet that carries data 481cc7972eaSChristoph Paasch */ 482c94b1f96SGeliang Tang if (data_len > 0) { 483c94b1f96SGeliang Tang len = TCPOLEN_MPTCP_MPC_ACK_DATA; 484c94b1f96SGeliang Tang if (opts->csum_reqd) { 485c5b39e26SGeliang Tang /* we need to propagate more info to csum the pseudo hdr */ 486f7cc8890SDavide Caratti opts->data_seq = mpext->data_seq; 487f7cc8890SDavide Caratti opts->subflow_seq = mpext->subflow_seq; 488f7cc8890SDavide Caratti opts->csum = mpext->csum; 489c94b1f96SGeliang Tang len += TCPOLEN_MPTCP_DSS_CHECKSUM; 490c94b1f96SGeliang Tang } 491c94b1f96SGeliang Tang *size = ALIGN(len, 4); 492c94b1f96SGeliang Tang } else { 493cec37a6eSPeter Krystad *size = TCPOLEN_MPTCP_MPC_ACK; 494c94b1f96SGeliang Tang } 495cc7972eaSChristoph Paasch 496cc7972eaSChristoph Paasch pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d", 497cc7972eaSChristoph Paasch subflow, subflow->local_key, subflow->remote_key, 498cc7972eaSChristoph Paasch data_len); 499cc7972eaSChristoph Paasch 500cec37a6eSPeter Krystad return true; 501ec3edaa7SPeter Krystad } else if (subflow->mp_join) { 502ec3edaa7SPeter Krystad opts->suboptions = OPTION_MPTCP_MPJ_ACK; 503ec3edaa7SPeter Krystad memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN); 504ec3edaa7SPeter Krystad *size = TCPOLEN_MPTCP_MPJ_ACK; 505ec3edaa7SPeter Krystad pr_debug("subflow=%p", subflow); 506ec3edaa7SPeter Krystad 507bcd97734SPaolo Abeni /* we can use the full delegate action helper only from BH context 508bcd97734SPaolo Abeni * If we are in process context - sk is flushing the backlog at 509bcd97734SPaolo Abeni * socket lock release time - just set the appropriate flag, will 510bcd97734SPaolo Abeni * be handled by the release callback 511bcd97734SPaolo Abeni */ 512bcd97734SPaolo Abeni if (sock_owned_by_user(sk)) 513bcd97734SPaolo Abeni set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status); 514bcd97734SPaolo Abeni else 515bcd97734SPaolo Abeni mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK); 516ec3edaa7SPeter Krystad return true; 517cec37a6eSPeter Krystad } 518cec37a6eSPeter Krystad return false; 519cec37a6eSPeter Krystad } 520cec37a6eSPeter Krystad 5216d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow, 5229c29e361SPaolo Abeni struct sk_buff *skb, struct mptcp_ext *ext) 5236d0060f6SMat Martineau { 524017512a0SPaolo Abeni /* The write_seq value has already been incremented, so the actual 525017512a0SPaolo Abeni * sequence number for the DATA_FIN is one less. 526017512a0SPaolo Abeni */ 527017512a0SPaolo Abeni u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1; 5287279da61SMat Martineau 5299c29e361SPaolo Abeni if (!ext->use_map || !skb->len) { 5306d0060f6SMat Martineau /* RFC6824 requires a DSS mapping with specific values 5316d0060f6SMat Martineau * if DATA_FIN is set but no data payload is mapped 5326d0060f6SMat Martineau */ 5336d37a0b8SMat Martineau ext->data_fin = 1; 5346d0060f6SMat Martineau ext->use_map = 1; 5356d0060f6SMat Martineau ext->dsn64 = 1; 536017512a0SPaolo Abeni ext->data_seq = data_fin_tx_seq; 5376d0060f6SMat Martineau ext->subflow_seq = 0; 5386d0060f6SMat Martineau ext->data_len = 1; 5397279da61SMat Martineau } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) { 5406d37a0b8SMat Martineau /* If there's an existing DSS mapping and it is the 5416d37a0b8SMat Martineau * final mapping, DATA_FIN consumes 1 additional byte of 5426d37a0b8SMat Martineau * mapping space. 5436d0060f6SMat Martineau */ 5446d37a0b8SMat Martineau ext->data_fin = 1; 5456d0060f6SMat Martineau ext->data_len++; 5466d0060f6SMat Martineau } 5476d0060f6SMat Martineau } 5486d0060f6SMat Martineau 5496d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb, 550d87903b6SPaolo Abeni bool snd_data_fin_enable, 5516d0060f6SMat Martineau unsigned int *size, 5526d0060f6SMat Martineau unsigned int remaining, 5536d0060f6SMat Martineau struct mptcp_out_options *opts) 5546d0060f6SMat Martineau { 5556d0060f6SMat Martineau struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 5567279da61SMat Martineau struct mptcp_sock *msk = mptcp_sk(subflow->conn); 5576d0060f6SMat Martineau unsigned int dss_size = 0; 5586d0060f6SMat Martineau struct mptcp_ext *mpext; 5596d0060f6SMat Martineau unsigned int ack_size; 560d22f4988SChristoph Paasch bool ret = false; 561d87903b6SPaolo Abeni u64 ack_seq; 5626d0060f6SMat Martineau 563c5b39e26SGeliang Tang opts->csum_reqd = READ_ONCE(msk->csum_enabled); 5640bac966aSMat Martineau mpext = skb ? mptcp_get_ext(skb) : NULL; 5656d0060f6SMat Martineau 5667279da61SMat Martineau if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) { 567c5b39e26SGeliang Tang unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64; 5686d0060f6SMat Martineau 569c5b39e26SGeliang Tang if (mpext) { 570c5b39e26SGeliang Tang if (opts->csum_reqd) 571c5b39e26SGeliang Tang map_size += TCPOLEN_MPTCP_DSS_CHECKSUM; 572c5b39e26SGeliang Tang 573c5b39e26SGeliang Tang opts->ext_copy = *mpext; 574c5b39e26SGeliang Tang } 5756d0060f6SMat Martineau 5766d0060f6SMat Martineau remaining -= map_size; 5776d0060f6SMat Martineau dss_size = map_size; 5787279da61SMat Martineau if (skb && snd_data_fin_enable) 5799c29e361SPaolo Abeni mptcp_write_data_fin(subflow, skb, &opts->ext_copy); 5801bff1e43SPaolo Abeni opts->suboptions = OPTION_MPTCP_DSS; 581d22f4988SChristoph Paasch ret = true; 582d22f4988SChristoph Paasch } 583d22f4988SChristoph Paasch 5842398e399SPaolo Abeni /* passive sockets msk will set the 'can_ack' after accept(), even 5852398e399SPaolo Abeni * if the first subflow may have the already the remote key handy 5862398e399SPaolo Abeni */ 587d22f4988SChristoph Paasch opts->ext_copy.use_ack = 0; 588dc093db5SPaolo Abeni if (!READ_ONCE(msk->can_ack)) { 589d22f4988SChristoph Paasch *size = ALIGN(dss_size, 4); 590d22f4988SChristoph Paasch return ret; 5916d0060f6SMat Martineau } 5926d0060f6SMat Martineau 593e3859603SPaolo Abeni ack_seq = READ_ONCE(msk->ack_seq); 59437198e93SDavide Caratti if (READ_ONCE(msk->use_64bit_ack)) { 5956d0060f6SMat Martineau ack_size = TCPOLEN_MPTCP_DSS_ACK64; 596e3859603SPaolo Abeni opts->ext_copy.data_ack = ack_seq; 597a0c1d0eaSChristoph Paasch opts->ext_copy.ack64 = 1; 598a0c1d0eaSChristoph Paasch } else { 599a0c1d0eaSChristoph Paasch ack_size = TCPOLEN_MPTCP_DSS_ACK32; 600e3859603SPaolo Abeni opts->ext_copy.data_ack32 = (uint32_t)ack_seq; 601a0c1d0eaSChristoph Paasch opts->ext_copy.ack64 = 0; 602a0c1d0eaSChristoph Paasch } 603a0c1d0eaSChristoph Paasch opts->ext_copy.use_ack = 1; 6041bff1e43SPaolo Abeni opts->suboptions = OPTION_MPTCP_DSS; 605ea4ca586SPaolo Abeni WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk)); 6066d0060f6SMat Martineau 6076d0060f6SMat Martineau /* Add kind/length/subtype/flag overhead if mapping is not populated */ 6086d0060f6SMat Martineau if (dss_size == 0) 6096d0060f6SMat Martineau ack_size += TCPOLEN_MPTCP_DSS_BASE; 6106d0060f6SMat Martineau 6116d0060f6SMat Martineau dss_size += ack_size; 6126d0060f6SMat Martineau 6136d0060f6SMat Martineau *size = ALIGN(dss_size, 4); 6146d0060f6SMat Martineau return true; 6156d0060f6SMat Martineau } 6166d0060f6SMat Martineau 617761c124eSGeliang Tang static u64 add_addr_generate_hmac(u64 key1, u64 key2, 618761c124eSGeliang Tang struct mptcp_addr_info *addr) 6193df523abSPeter Krystad { 620761c124eSGeliang Tang u16 port = ntohs(addr->port); 621bd697222STodd Malsbary u8 hmac[SHA256_DIGEST_SIZE]; 6223df523abSPeter Krystad u8 msg[19]; 623761c124eSGeliang Tang int i = 0; 6243df523abSPeter Krystad 625761c124eSGeliang Tang msg[i++] = addr->id; 626761c124eSGeliang Tang if (addr->family == AF_INET) { 627761c124eSGeliang Tang memcpy(&msg[i], &addr->addr.s_addr, 4); 628761c124eSGeliang Tang i += 4; 629761c124eSGeliang Tang } 630761c124eSGeliang Tang #if IS_ENABLED(CONFIG_MPTCP_IPV6) 631761c124eSGeliang Tang else if (addr->family == AF_INET6) { 632761c124eSGeliang Tang memcpy(&msg[i], &addr->addr6.s6_addr, 16); 633761c124eSGeliang Tang i += 16; 634761c124eSGeliang Tang } 635761c124eSGeliang Tang #endif 636761c124eSGeliang Tang msg[i++] = port >> 8; 637761c124eSGeliang Tang msg[i++] = port & 0xFF; 6383df523abSPeter Krystad 639761c124eSGeliang Tang mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac); 6403df523abSPeter Krystad 641bd697222STodd Malsbary return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]); 6423df523abSPeter Krystad } 6433df523abSPeter Krystad 64484dfe367SGeliang Tang static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb, 6453df523abSPeter Krystad unsigned int *size, 6463df523abSPeter Krystad unsigned int remaining, 6473df523abSPeter Krystad struct mptcp_out_options *opts) 6483df523abSPeter Krystad { 6493df523abSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 6503df523abSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 65184dfe367SGeliang Tang bool drop_other_suboptions = false; 65284dfe367SGeliang Tang unsigned int opt_size = *size; 6536a6c05a8SGeliang Tang bool echo; 6544a2777a8SGeliang Tang bool port; 6551b1c7a0eSPeter Krystad int len; 6563df523abSPeter Krystad 6571f5e9e2fSYonglong Li /* add addr will strip the existing options, be sure to avoid breaking 6581f5e9e2fSYonglong Li * MPC/MPJ handshakes 6591f5e9e2fSYonglong Li */ 660f643b803SGeliang Tang if (!mptcp_pm_should_add_signal(msk) || 6611f5e9e2fSYonglong Li (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) || 6621f5e9e2fSYonglong Li !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr, 6631f5e9e2fSYonglong Li &echo, &port, &drop_other_suboptions)) 6643df523abSPeter Krystad return false; 6651b1c7a0eSPeter Krystad 6661f5e9e2fSYonglong Li if (drop_other_suboptions) 6671f5e9e2fSYonglong Li remaining += opt_size; 66830f60baeSGeliang Tang len = mptcp_add_addr_len(opts->addr.family, echo, port); 6691b1c7a0eSPeter Krystad if (remaining < len) 6701b1c7a0eSPeter Krystad return false; 6711b1c7a0eSPeter Krystad 6721b1c7a0eSPeter Krystad *size = len; 6731f5e9e2fSYonglong Li if (drop_other_suboptions) { 6741f5e9e2fSYonglong Li pr_debug("drop other suboptions"); 6751f5e9e2fSYonglong Li opts->suboptions = 0; 6761bff1e43SPaolo Abeni 6771bff1e43SPaolo Abeni /* note that e.g. DSS could have written into the memory 6781bff1e43SPaolo Abeni * aliased by ahmac, we must reset the field here 6791bff1e43SPaolo Abeni * to avoid appending the hmac even for ADD_ADDR echo 6801bff1e43SPaolo Abeni * options 6811bff1e43SPaolo Abeni */ 6821bff1e43SPaolo Abeni opts->ahmac = 0; 68384dfe367SGeliang Tang *size -= opt_size; 6841f5e9e2fSYonglong Li } 6853df523abSPeter Krystad opts->suboptions |= OPTION_MPTCP_ADD_ADDR; 6866a6c05a8SGeliang Tang if (!echo) { 6873df523abSPeter Krystad opts->ahmac = add_addr_generate_hmac(msk->local_key, 6883df523abSPeter Krystad msk->remote_key, 689761c124eSGeliang Tang &opts->addr); 6903df523abSPeter Krystad } 6914a2777a8SGeliang Tang pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d", 69230f60baeSGeliang Tang opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port)); 6933df523abSPeter Krystad 6943df523abSPeter Krystad return true; 6953df523abSPeter Krystad } 6963df523abSPeter Krystad 6975cb104aeSGeliang Tang static bool mptcp_established_options_rm_addr(struct sock *sk, 6985cb104aeSGeliang Tang unsigned int *size, 6995cb104aeSGeliang Tang unsigned int remaining, 7005cb104aeSGeliang Tang struct mptcp_out_options *opts) 7015cb104aeSGeliang Tang { 7025cb104aeSGeliang Tang struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 7035cb104aeSGeliang Tang struct mptcp_sock *msk = mptcp_sk(subflow->conn); 7046445e17aSGeliang Tang struct mptcp_rm_list rm_list; 7056445e17aSGeliang Tang int i, len; 7065cb104aeSGeliang Tang 7075cb104aeSGeliang Tang if (!mptcp_pm_should_rm_signal(msk) || 7086445e17aSGeliang Tang !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list))) 7095cb104aeSGeliang Tang return false; 7105cb104aeSGeliang Tang 7116445e17aSGeliang Tang len = mptcp_rm_addr_len(&rm_list); 7126445e17aSGeliang Tang if (len < 0) 7136445e17aSGeliang Tang return false; 7146445e17aSGeliang Tang if (remaining < len) 7155cb104aeSGeliang Tang return false; 7165cb104aeSGeliang Tang 7176445e17aSGeliang Tang *size = len; 7185cb104aeSGeliang Tang opts->suboptions |= OPTION_MPTCP_RM_ADDR; 7196445e17aSGeliang Tang opts->rm_list = rm_list; 7205cb104aeSGeliang Tang 7216445e17aSGeliang Tang for (i = 0; i < opts->rm_list.nr; i++) 7226445e17aSGeliang Tang pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]); 7235cb104aeSGeliang Tang 7245cb104aeSGeliang Tang return true; 7255cb104aeSGeliang Tang } 7265cb104aeSGeliang Tang 72706706542SGeliang Tang static bool mptcp_established_options_mp_prio(struct sock *sk, 72806706542SGeliang Tang unsigned int *size, 72906706542SGeliang Tang unsigned int remaining, 73006706542SGeliang Tang struct mptcp_out_options *opts) 73106706542SGeliang Tang { 73206706542SGeliang Tang struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 73306706542SGeliang Tang 7341bff1e43SPaolo Abeni /* can't send MP_PRIO with MPC, as they share the same option space: 7351bff1e43SPaolo Abeni * 'backup'. Also it makes no sense at all 7361bff1e43SPaolo Abeni */ 73713ac17a3SGeliang Tang if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC)) 73806706542SGeliang Tang return false; 73906706542SGeliang Tang 740ec99a470SDavide Caratti /* account for the trailing 'nop' option */ 741ec99a470SDavide Caratti if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN) 74206706542SGeliang Tang return false; 74306706542SGeliang Tang 744ec99a470SDavide Caratti *size = TCPOLEN_MPTCP_PRIO_ALIGN; 74506706542SGeliang Tang opts->suboptions |= OPTION_MPTCP_PRIO; 74606706542SGeliang Tang opts->backup = subflow->request_bkup; 74706706542SGeliang Tang 74806706542SGeliang Tang pr_debug("prio=%d", opts->backup); 74906706542SGeliang Tang 75006706542SGeliang Tang return true; 75106706542SGeliang Tang } 75206706542SGeliang Tang 753c25aeb4eSGeliang Tang static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb, 754dc87efdbSFlorian Westphal unsigned int *size, 755dc87efdbSFlorian Westphal unsigned int remaining, 756dc87efdbSFlorian Westphal struct mptcp_out_options *opts) 757dc87efdbSFlorian Westphal { 758dc87efdbSFlorian Westphal const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 759dc87efdbSFlorian Westphal 760dc87efdbSFlorian Westphal if (remaining < TCPOLEN_MPTCP_RST) 761c25aeb4eSGeliang Tang return false; 762dc87efdbSFlorian Westphal 763dc87efdbSFlorian Westphal *size = TCPOLEN_MPTCP_RST; 764dc87efdbSFlorian Westphal opts->suboptions |= OPTION_MPTCP_RST; 765dc87efdbSFlorian Westphal opts->reset_transient = subflow->reset_transient; 766dc87efdbSFlorian Westphal opts->reset_reason = subflow->reset_reason; 767c25aeb4eSGeliang Tang 768c25aeb4eSGeliang Tang return true; 769c25aeb4eSGeliang Tang } 770c25aeb4eSGeliang Tang 771f284c0c7SPaolo Abeni static bool mptcp_established_options_fastclose(struct sock *sk, 772f284c0c7SPaolo Abeni unsigned int *size, 773f284c0c7SPaolo Abeni unsigned int remaining, 774f284c0c7SPaolo Abeni struct mptcp_out_options *opts) 775f284c0c7SPaolo Abeni { 776f284c0c7SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 777f284c0c7SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(subflow->conn); 778f284c0c7SPaolo Abeni 779f284c0c7SPaolo Abeni if (likely(!subflow->send_fastclose)) 780f284c0c7SPaolo Abeni return false; 781f284c0c7SPaolo Abeni 782f284c0c7SPaolo Abeni if (remaining < TCPOLEN_MPTCP_FASTCLOSE) 783f284c0c7SPaolo Abeni return false; 784f284c0c7SPaolo Abeni 785f284c0c7SPaolo Abeni *size = TCPOLEN_MPTCP_FASTCLOSE; 786f284c0c7SPaolo Abeni opts->suboptions |= OPTION_MPTCP_FASTCLOSE; 787f284c0c7SPaolo Abeni opts->rcvr_key = msk->remote_key; 788f284c0c7SPaolo Abeni 789f284c0c7SPaolo Abeni pr_debug("FASTCLOSE key=%llu", opts->rcvr_key); 790f284c0c7SPaolo Abeni return true; 791f284c0c7SPaolo Abeni } 792f284c0c7SPaolo Abeni 793c25aeb4eSGeliang Tang static bool mptcp_established_options_mp_fail(struct sock *sk, 794c25aeb4eSGeliang Tang unsigned int *size, 795c25aeb4eSGeliang Tang unsigned int remaining, 796c25aeb4eSGeliang Tang struct mptcp_out_options *opts) 797c25aeb4eSGeliang Tang { 798c25aeb4eSGeliang Tang struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 799c25aeb4eSGeliang Tang 800c25aeb4eSGeliang Tang if (likely(!subflow->send_mp_fail)) 801c25aeb4eSGeliang Tang return false; 802c25aeb4eSGeliang Tang 803c25aeb4eSGeliang Tang if (remaining < TCPOLEN_MPTCP_FAIL) 804c25aeb4eSGeliang Tang return false; 805c25aeb4eSGeliang Tang 806c25aeb4eSGeliang Tang *size = TCPOLEN_MPTCP_FAIL; 807c25aeb4eSGeliang Tang opts->suboptions |= OPTION_MPTCP_FAIL; 808c25aeb4eSGeliang Tang opts->fail_seq = subflow->map_seq; 809c25aeb4eSGeliang Tang 810c25aeb4eSGeliang Tang pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq); 811c25aeb4eSGeliang Tang 812c25aeb4eSGeliang Tang return true; 813dc87efdbSFlorian Westphal } 814dc87efdbSFlorian Westphal 8156d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb, 8166d0060f6SMat Martineau unsigned int *size, unsigned int remaining, 8176d0060f6SMat Martineau struct mptcp_out_options *opts) 8186d0060f6SMat Martineau { 819d87903b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 820d87903b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(subflow->conn); 8216d0060f6SMat Martineau unsigned int opt_size = 0; 822d87903b6SPaolo Abeni bool snd_data_fin; 8236d0060f6SMat Martineau bool ret = false; 8246d0060f6SMat Martineau 8253df523abSPeter Krystad opts->suboptions = 0; 8263df523abSPeter Krystad 827d87903b6SPaolo Abeni if (unlikely(__mptcp_check_fallback(msk))) 828e1ff9e82SDavide Caratti return false; 829e1ff9e82SDavide Caratti 830dc87efdbSFlorian Westphal if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) { 831f284c0c7SPaolo Abeni if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) || 832f284c0c7SPaolo Abeni mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) { 833c25aeb4eSGeliang Tang *size += opt_size; 834c25aeb4eSGeliang Tang remaining -= opt_size; 835c25aeb4eSGeliang Tang } 836f284c0c7SPaolo Abeni /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */ 837c25aeb4eSGeliang Tang if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) { 838c25aeb4eSGeliang Tang *size += opt_size; 839c25aeb4eSGeliang Tang remaining -= opt_size; 840c25aeb4eSGeliang Tang } 841dc87efdbSFlorian Westphal return true; 842dc87efdbSFlorian Westphal } 843d5824847SPaolo Abeni 844d87903b6SPaolo Abeni snd_data_fin = mptcp_data_fin_enabled(msk); 845d87903b6SPaolo Abeni if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts)) 8466d0060f6SMat Martineau ret = true; 847c25aeb4eSGeliang Tang else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) { 84804fac2caSMatthieu Baerts unsigned int mp_fail_size; 84904fac2caSMatthieu Baerts 8506d0060f6SMat Martineau ret = true; 85104fac2caSMatthieu Baerts if (mptcp_established_options_mp_fail(sk, &mp_fail_size, 85204fac2caSMatthieu Baerts remaining - opt_size, opts)) { 85304fac2caSMatthieu Baerts *size += opt_size + mp_fail_size; 85404fac2caSMatthieu Baerts remaining -= opt_size - mp_fail_size; 855c25aeb4eSGeliang Tang return true; 856c25aeb4eSGeliang Tang } 857c25aeb4eSGeliang Tang } 8586d0060f6SMat Martineau 8596d0060f6SMat Martineau /* we reserved enough space for the above options, and exceeding the 8606d0060f6SMat Martineau * TCP option space would be fatal 8616d0060f6SMat Martineau */ 8626d0060f6SMat Martineau if (WARN_ON_ONCE(opt_size > remaining)) 8636d0060f6SMat Martineau return false; 8646d0060f6SMat Martineau 8656d0060f6SMat Martineau *size += opt_size; 8666d0060f6SMat Martineau remaining -= opt_size; 86784dfe367SGeliang Tang if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) { 8683df523abSPeter Krystad *size += opt_size; 8693df523abSPeter Krystad remaining -= opt_size; 8703df523abSPeter Krystad ret = true; 8715cb104aeSGeliang Tang } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) { 8725cb104aeSGeliang Tang *size += opt_size; 8735cb104aeSGeliang Tang remaining -= opt_size; 8745cb104aeSGeliang Tang ret = true; 8753df523abSPeter Krystad } 8766d0060f6SMat Martineau 87706706542SGeliang Tang if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) { 87806706542SGeliang Tang *size += opt_size; 87906706542SGeliang Tang remaining -= opt_size; 88006706542SGeliang Tang ret = true; 88106706542SGeliang Tang } 88206706542SGeliang Tang 8836d0060f6SMat Martineau return ret; 8846d0060f6SMat Martineau } 8856d0060f6SMat Martineau 886cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size, 887cec37a6eSPeter Krystad struct mptcp_out_options *opts) 888cec37a6eSPeter Krystad { 889cec37a6eSPeter Krystad struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 890cec37a6eSPeter Krystad 891cec37a6eSPeter Krystad if (subflow_req->mp_capable) { 892cec37a6eSPeter Krystad opts->suboptions = OPTION_MPTCP_MPC_SYNACK; 893cec37a6eSPeter Krystad opts->sndr_key = subflow_req->local_key; 89406fe1719SGeliang Tang opts->csum_reqd = subflow_req->csum_reqd; 895bab6b88eSGeliang Tang opts->allow_join_id0 = subflow_req->allow_join_id0; 896cec37a6eSPeter Krystad *size = TCPOLEN_MPTCP_MPC_SYNACK; 897cec37a6eSPeter Krystad pr_debug("subflow_req=%p, local_key=%llu", 898cec37a6eSPeter Krystad subflow_req, subflow_req->local_key); 899cec37a6eSPeter Krystad return true; 900f296234cSPeter Krystad } else if (subflow_req->mp_join) { 901f296234cSPeter Krystad opts->suboptions = OPTION_MPTCP_MPJ_SYNACK; 902f296234cSPeter Krystad opts->backup = subflow_req->backup; 903f296234cSPeter Krystad opts->join_id = subflow_req->local_id; 904f296234cSPeter Krystad opts->thmac = subflow_req->thmac; 905f296234cSPeter Krystad opts->nonce = subflow_req->local_nonce; 906f296234cSPeter Krystad pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u", 907f296234cSPeter Krystad subflow_req, opts->backup, opts->join_id, 908f296234cSPeter Krystad opts->thmac, opts->nonce); 909f296234cSPeter Krystad *size = TCPOLEN_MPTCP_MPJ_SYNACK; 910f296234cSPeter Krystad return true; 911cec37a6eSPeter Krystad } 912cec37a6eSPeter Krystad return false; 913cec37a6eSPeter Krystad } 914cec37a6eSPeter Krystad 915d5824847SPaolo Abeni static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk, 916f296234cSPeter Krystad struct mptcp_subflow_context *subflow, 917d22f4988SChristoph Paasch struct sk_buff *skb, 918d22f4988SChristoph Paasch struct mptcp_options_received *mp_opt) 919d22f4988SChristoph Paasch { 920d22f4988SChristoph Paasch /* here we can process OoO, in-window pkts, only in-sequence 4th ack 921f296234cSPeter Krystad * will make the subflow fully established 922d22f4988SChristoph Paasch */ 923f296234cSPeter Krystad if (likely(subflow->fully_established)) { 924f296234cSPeter Krystad /* on passive sockets, check for 3rd ack retransmission 925f296234cSPeter Krystad * note that msk is always set by subflow_syn_recv_sock() 926f296234cSPeter Krystad * for mp_join subflows 927f296234cSPeter Krystad */ 928f296234cSPeter Krystad if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 && 929f296234cSPeter Krystad TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq && 93074c7dfbeSPaolo Abeni subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) && 931f296234cSPeter Krystad READ_ONCE(msk->pm.server_side)) 932d5824847SPaolo Abeni tcp_send_ack(ssk); 933f296234cSPeter Krystad goto fully_established; 934f296234cSPeter Krystad } 935d22f4988SChristoph Paasch 936d5824847SPaolo Abeni /* we must process OoO packets before the first subflow is fully 937d5824847SPaolo Abeni * established. OoO packets are instead a protocol violation 938d5824847SPaolo Abeni * for MP_JOIN subflows as the peer must not send any data 939d5824847SPaolo Abeni * before receiving the forth ack - cfr. RFC 8684 section 3.2. 940f296234cSPeter Krystad */ 941d5824847SPaolo Abeni if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) { 942d5824847SPaolo Abeni if (subflow->mp_join) 943d5824847SPaolo Abeni goto reset; 944f296234cSPeter Krystad return subflow->mp_capable; 945d5824847SPaolo Abeni } 946f296234cSPeter Krystad 94774c7dfbeSPaolo Abeni if (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) || 94874c7dfbeSPaolo Abeni ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo)) { 949f296234cSPeter Krystad /* subflows are fully established as soon as we get any 95067b12f79SMatthieu Baerts * additional ack, including ADD_ADDR. 951f296234cSPeter Krystad */ 9520be534f5SPaolo Abeni subflow->fully_established = 1; 953b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, true); 954f296234cSPeter Krystad goto fully_established; 955f296234cSPeter Krystad } 956d22f4988SChristoph Paasch 957d22f4988SChristoph Paasch /* If the first established packet does not contain MP_CAPABLE + data 958d5824847SPaolo Abeni * then fallback to TCP. Fallback scenarios requires a reset for 959d5824847SPaolo Abeni * MP_JOIN subflows. 960d22f4988SChristoph Paasch */ 96174c7dfbeSPaolo Abeni if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) { 962d5824847SPaolo Abeni if (subflow->mp_join) 963d5824847SPaolo Abeni goto reset; 964d22f4988SChristoph Paasch subflow->mp_capable = 0; 965e1ff9e82SDavide Caratti pr_fallback(msk); 966e1ff9e82SDavide Caratti __mptcp_do_fallback(msk); 967d22f4988SChristoph Paasch return false; 968d22f4988SChristoph Paasch } 969f296234cSPeter Krystad 970df377be3SGeliang Tang if (mp_opt->deny_join_id0) 971df377be3SGeliang Tang WRITE_ONCE(msk->pm.remote_deny_join_id0, true); 972df377be3SGeliang Tang 973d6085fe1SPaolo Abeni if (unlikely(!READ_ONCE(msk->pm.server_side))) 974d6085fe1SPaolo Abeni pr_warn_once("bogus mpc option on established client sk"); 975b93df08cSPaolo Abeni mptcp_subflow_fully_established(subflow, mp_opt); 976f296234cSPeter Krystad 977f296234cSPeter Krystad fully_established: 9785b950ff4SPaolo Abeni /* if the subflow is not already linked into the conn_list, we can't 9795b950ff4SPaolo Abeni * notify the PM: this subflow is still on the listener queue 9805b950ff4SPaolo Abeni * and the PM possibly acquiring the subflow lock could race with 9815b950ff4SPaolo Abeni * the listener close 9825b950ff4SPaolo Abeni */ 9835b950ff4SPaolo Abeni if (likely(subflow->pm_notified) || list_empty(&subflow->node)) 984f296234cSPeter Krystad return true; 985f296234cSPeter Krystad 986f296234cSPeter Krystad subflow->pm_notified = 1; 987ec3edaa7SPeter Krystad if (subflow->mp_join) { 988d5824847SPaolo Abeni clear_3rdack_retransmission(ssk); 98962535200SGeliang Tang mptcp_pm_subflow_established(msk); 990ec3edaa7SPeter Krystad } else { 9916c714f1bSFlorian Westphal mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC); 992ec3edaa7SPeter Krystad } 993d22f4988SChristoph Paasch return true; 994d5824847SPaolo Abeni 995d5824847SPaolo Abeni reset: 996d5824847SPaolo Abeni mptcp_subflow_reset(ssk); 997d5824847SPaolo Abeni return false; 998d22f4988SChristoph Paasch } 999d22f4988SChristoph Paasch 10001502328fSPaolo Abeni u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq) 1001cc9d2566SPaolo Abeni { 10021502328fSPaolo Abeni u32 old_seq32, cur_seq32; 1003cc9d2566SPaolo Abeni 10041502328fSPaolo Abeni old_seq32 = (u32)old_seq; 10051502328fSPaolo Abeni cur_seq32 = (u32)cur_seq; 10061502328fSPaolo Abeni cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32; 10071502328fSPaolo Abeni if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32))) 10081502328fSPaolo Abeni return cur_seq + (1LL << 32); 1009cc9d2566SPaolo Abeni 10101502328fSPaolo Abeni /* reverse wrap could happen, too */ 10111502328fSPaolo Abeni if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32))) 10121502328fSPaolo Abeni return cur_seq - (1LL << 32); 10131502328fSPaolo Abeni return cur_seq; 1014cc9d2566SPaolo Abeni } 1015cc9d2566SPaolo Abeni 10166f8a612aSFlorian Westphal static void ack_update_msk(struct mptcp_sock *msk, 10176e628cd3SPaolo Abeni struct sock *ssk, 1018cc9d2566SPaolo Abeni struct mptcp_options_received *mp_opt) 1019cc9d2566SPaolo Abeni { 10207439d687SPaolo Abeni u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt); 10216f8a612aSFlorian Westphal struct sock *sk = (struct sock *)msk; 10227439d687SPaolo Abeni u64 old_snd_una; 10237439d687SPaolo Abeni 10247439d687SPaolo Abeni mptcp_data_lock(sk); 1025cc9d2566SPaolo Abeni 1026cc9d2566SPaolo Abeni /* avoid ack expansion on update conflict, to reduce the risk of 1027cc9d2566SPaolo Abeni * wrongly expanding to a future ack sequence number, which is way 1028cc9d2566SPaolo Abeni * more dangerous than missing an ack 1029cc9d2566SPaolo Abeni */ 10307439d687SPaolo Abeni old_snd_una = msk->snd_una; 10311502328fSPaolo Abeni new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64); 1032cc9d2566SPaolo Abeni 10330d199e43SFlorian Westphal /* ACK for data not even sent yet? Ignore.*/ 10340d199e43SFlorian Westphal if (unlikely(after64(new_snd_una, snd_nxt))) 1035cc9d2566SPaolo Abeni new_snd_una = old_snd_una; 1036cc9d2566SPaolo Abeni 10376f8a612aSFlorian Westphal new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd; 10386f8a612aSFlorian Westphal 1039219d0499SPaolo Abeni if (after64(new_wnd_end, msk->wnd_end)) 10407439d687SPaolo Abeni msk->wnd_end = new_wnd_end; 1041219d0499SPaolo Abeni 1042219d0499SPaolo Abeni /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */ 1043d09d818eSPaolo Abeni if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt))) 1044219d0499SPaolo Abeni __mptcp_check_push(sk, ssk); 10456f8a612aSFlorian Westphal 10467439d687SPaolo Abeni if (after64(new_snd_una, old_snd_una)) { 10477439d687SPaolo Abeni msk->snd_una = new_snd_una; 10487439d687SPaolo Abeni __mptcp_data_acked(sk); 1049cc9d2566SPaolo Abeni } 10507439d687SPaolo Abeni mptcp_data_unlock(sk); 1051ed66bfb4SGeliang Tang 1052ed66bfb4SGeliang Tang trace_ack_update_msk(mp_opt->data_ack, 1053ed66bfb4SGeliang Tang old_snd_una, new_snd_una, 1054ed66bfb4SGeliang Tang new_wnd_end, msk->wnd_end); 1055b51f9b80SPaolo Abeni } 1056cc9d2566SPaolo Abeni 10571a49b2c2SMat Martineau bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit) 10583721b9b6SMat Martineau { 10593721b9b6SMat Martineau /* Skip if DATA_FIN was already received. 10603721b9b6SMat Martineau * If updating simultaneously with the recvmsg loop, values 10613721b9b6SMat Martineau * should match. If they mismatch, the peer is misbehaving and 10623721b9b6SMat Martineau * we will prefer the most recent information. 10633721b9b6SMat Martineau */ 1064781bf13dSPaolo Abeni if (READ_ONCE(msk->rcv_data_fin)) 10653721b9b6SMat Martineau return false; 10663721b9b6SMat Martineau 10671a49b2c2SMat Martineau WRITE_ONCE(msk->rcv_data_fin_seq, 10681502328fSPaolo Abeni mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit)); 10693721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 1); 10703721b9b6SMat Martineau 10713721b9b6SMat Martineau return true; 10723721b9b6SMat Martineau } 10733721b9b6SMat Martineau 10741b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk, 10751b1c7a0eSPeter Krystad struct mptcp_options_received *mp_opt) 10761b1c7a0eSPeter Krystad { 10771b1c7a0eSPeter Krystad u64 hmac = 0; 10781b1c7a0eSPeter Krystad 10791b1c7a0eSPeter Krystad if (mp_opt->echo) 10801b1c7a0eSPeter Krystad return true; 10811b1c7a0eSPeter Krystad 10821b1c7a0eSPeter Krystad hmac = add_addr_generate_hmac(msk->remote_key, 10831b1c7a0eSPeter Krystad msk->local_key, 1084761c124eSGeliang Tang &mp_opt->addr); 10851b1c7a0eSPeter Krystad 10861b1c7a0eSPeter Krystad pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n", 10871b1c7a0eSPeter Krystad msk, (unsigned long long)hmac, 10881b1c7a0eSPeter Krystad (unsigned long long)mp_opt->ahmac); 10891b1c7a0eSPeter Krystad 10901b1c7a0eSPeter Krystad return hmac == mp_opt->ahmac; 10911b1c7a0eSPeter Krystad } 10921b1c7a0eSPeter Krystad 10936787b7e3SJianguo Wu /* Return false if a subflow has been reset, else return true */ 10946787b7e3SJianguo Wu bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb) 1095648ef4b8SMat Martineau { 1096d22f4988SChristoph Paasch struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 10971b1c7a0eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 1098cfde141eSPaolo Abeni struct mptcp_options_received mp_opt; 1099648ef4b8SMat Martineau struct mptcp_ext *mpext; 1100648ef4b8SMat Martineau 11016e628cd3SPaolo Abeni if (__mptcp_check_fallback(msk)) { 11026e628cd3SPaolo Abeni /* Keep it simple and unconditionally trigger send data cleanup and 11036e628cd3SPaolo Abeni * pending queue spooling. We will need to acquire the data lock 11046e628cd3SPaolo Abeni * for more accurate checks, and once the lock is acquired, such 11056e628cd3SPaolo Abeni * helpers are cheap. 11066e628cd3SPaolo Abeni */ 11076e628cd3SPaolo Abeni mptcp_data_lock(subflow->conn); 1108219d0499SPaolo Abeni if (sk_stream_memory_free(sk)) 1109219d0499SPaolo Abeni __mptcp_check_push(subflow->conn, sk); 11106e628cd3SPaolo Abeni __mptcp_data_acked(subflow->conn); 11116e628cd3SPaolo Abeni mptcp_data_unlock(subflow->conn); 11126787b7e3SJianguo Wu return true; 11136e628cd3SPaolo Abeni } 1114e1ff9e82SDavide Caratti 1115c863225bSGeliang Tang mptcp_get_options(sk, skb, &mp_opt); 11166787b7e3SJianguo Wu 11176787b7e3SJianguo Wu /* The subflow can be in close state only if check_fully_established() 11186787b7e3SJianguo Wu * just sent a reset. If so, tell the caller to ignore the current packet. 11196787b7e3SJianguo Wu */ 1120cfde141eSPaolo Abeni if (!check_fully_established(msk, sk, subflow, skb, &mp_opt)) 11216787b7e3SJianguo Wu return sk->sk_state != TCP_CLOSE; 1122648ef4b8SMat Martineau 1123f6c2ef59SPaolo Abeni if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) { 112474c7dfbeSPaolo Abeni if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) && 112550c504a2SFlorian Westphal msk->local_key == mp_opt.rcvr_key) { 112650c504a2SFlorian Westphal WRITE_ONCE(msk->rcv_fastclose, true); 112750c504a2SFlorian Westphal mptcp_schedule_work((struct sock *)msk); 112850c504a2SFlorian Westphal } 112950c504a2SFlorian Westphal 1130f6c2ef59SPaolo Abeni if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) && 1131f6c2ef59SPaolo Abeni add_addr_hmac_valid(msk, &mp_opt)) { 1132a877de06SGeliang Tang if (!mp_opt.echo) { 1133f7dafee1SGeliang Tang mptcp_pm_add_addr_received(msk, &mp_opt.addr); 1134a877de06SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR); 1135a877de06SGeliang Tang } else { 1136f7dafee1SGeliang Tang mptcp_pm_add_addr_echoed(msk, &mp_opt.addr); 1137d58300c3SDavide Caratti mptcp_pm_del_add_timer(msk, &mp_opt.addr, true); 1138a877de06SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD); 1139a877de06SGeliang Tang } 11402fbdd9eaSGeliang Tang 1141f7dafee1SGeliang Tang if (mp_opt.addr.port) 11422fbdd9eaSGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD); 11431b1c7a0eSPeter Krystad } 11441b1c7a0eSPeter Krystad 114574c7dfbeSPaolo Abeni if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR) 11465c4a824dSGeliang Tang mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list); 1147d0876b22SGeliang Tang 114874c7dfbeSPaolo Abeni if (mp_opt.suboptions & OPTION_MPTCP_PRIO) { 114940453a5cSGeliang Tang mptcp_pm_mp_prio_received(sk, mp_opt.backup); 11500be2ac28SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX); 115140453a5cSGeliang Tang } 115240453a5cSGeliang Tang 115374c7dfbeSPaolo Abeni if (mp_opt.suboptions & OPTION_MPTCP_FAIL) { 11545580d41bSGeliang Tang mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq); 1155eb7f3365SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX); 11565580d41bSGeliang Tang } 11575580d41bSGeliang Tang 115874c7dfbeSPaolo Abeni if (mp_opt.suboptions & OPTION_MPTCP_RST) { 1159dc87efdbSFlorian Westphal subflow->reset_seen = 1; 1160dc87efdbSFlorian Westphal subflow->reset_reason = mp_opt.reset_reason; 1161dc87efdbSFlorian Westphal subflow->reset_transient = mp_opt.reset_transient; 1162dc87efdbSFlorian Westphal } 1163dc87efdbSFlorian Westphal 116474c7dfbeSPaolo Abeni if (!(mp_opt.suboptions & OPTION_MPTCP_DSS)) 11656787b7e3SJianguo Wu return true; 1166f6c2ef59SPaolo Abeni } 1167648ef4b8SMat Martineau 1168cc9d2566SPaolo Abeni /* we can't wait for recvmsg() to update the ack_seq, otherwise 1169cc9d2566SPaolo Abeni * monodirectional flows will stuck 1170cc9d2566SPaolo Abeni */ 1171cfde141eSPaolo Abeni if (mp_opt.use_ack) 11726f8a612aSFlorian Westphal ack_update_msk(msk, sk, &mp_opt); 1173cc9d2566SPaolo Abeni 117406827b34SMat Martineau /* Zero-data-length packets are dropped by the caller and not 117506827b34SMat Martineau * propagated to the MPTCP layer, so the skb extension does not 117606827b34SMat Martineau * need to be allocated or populated. DATA_FIN information, if 117706827b34SMat Martineau * present, needs to be updated here before the skb is freed. 117843b54c6eSMat Martineau */ 117943b54c6eSMat Martineau if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) { 118043b54c6eSMat Martineau if (mp_opt.data_fin && mp_opt.data_len == 1 && 11811a49b2c2SMat Martineau mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) && 118243b54c6eSMat Martineau schedule_work(&msk->work)) 118343b54c6eSMat Martineau sock_hold(subflow->conn); 118406827b34SMat Martineau 11856787b7e3SJianguo Wu return true; 118643b54c6eSMat Martineau } 118743b54c6eSMat Martineau 1188648ef4b8SMat Martineau mpext = skb_ext_add(skb, SKB_EXT_MPTCP); 1189648ef4b8SMat Martineau if (!mpext) 11906787b7e3SJianguo Wu return true; 1191648ef4b8SMat Martineau 1192648ef4b8SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1193648ef4b8SMat Martineau 1194f6c2ef59SPaolo Abeni if (likely(mp_opt.use_map)) { 1195cfde141eSPaolo Abeni if (mp_opt.mpc_map) { 1196cc7972eaSChristoph Paasch /* this is an MP_CAPABLE carrying MPTCP data 1197cc7972eaSChristoph Paasch * we know this map the first chunk of data 1198cc7972eaSChristoph Paasch */ 1199cc7972eaSChristoph Paasch mptcp_crypto_key_sha(subflow->remote_key, NULL, 1200cc7972eaSChristoph Paasch &mpext->data_seq); 1201cc7972eaSChristoph Paasch mpext->data_seq++; 1202cc7972eaSChristoph Paasch mpext->subflow_seq = 1; 1203cc7972eaSChristoph Paasch mpext->dsn64 = 1; 1204cc7972eaSChristoph Paasch mpext->mpc_map = 1; 1205a77895dbSPaolo Abeni mpext->data_fin = 0; 1206cc7972eaSChristoph Paasch } else { 1207cfde141eSPaolo Abeni mpext->data_seq = mp_opt.data_seq; 1208cfde141eSPaolo Abeni mpext->subflow_seq = mp_opt.subflow_seq; 1209cfde141eSPaolo Abeni mpext->dsn64 = mp_opt.dsn64; 1210cfde141eSPaolo Abeni mpext->data_fin = mp_opt.data_fin; 1211cc7972eaSChristoph Paasch } 1212cfde141eSPaolo Abeni mpext->data_len = mp_opt.data_len; 1213648ef4b8SMat Martineau mpext->use_map = 1; 121474c7dfbeSPaolo Abeni mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD); 1215208e8f66SGeliang Tang 1216208e8f66SGeliang Tang if (mpext->csum_reqd) 1217208e8f66SGeliang Tang mpext->csum = mp_opt.csum; 1218648ef4b8SMat Martineau } 12196787b7e3SJianguo Wu 12206787b7e3SJianguo Wu return true; 1221648ef4b8SMat Martineau } 1222648ef4b8SMat Martineau 1223fa3fe2b1SFlorian Westphal static void mptcp_set_rwin(const struct tcp_sock *tp) 1224fa3fe2b1SFlorian Westphal { 1225fa3fe2b1SFlorian Westphal const struct sock *ssk = (const struct sock *)tp; 1226fa3fe2b1SFlorian Westphal const struct mptcp_subflow_context *subflow; 1227fa3fe2b1SFlorian Westphal struct mptcp_sock *msk; 1228fa3fe2b1SFlorian Westphal u64 ack_seq; 1229fa3fe2b1SFlorian Westphal 1230fa3fe2b1SFlorian Westphal subflow = mptcp_subflow_ctx(ssk); 1231fa3fe2b1SFlorian Westphal msk = mptcp_sk(subflow->conn); 1232fa3fe2b1SFlorian Westphal 1233fa3fe2b1SFlorian Westphal ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd; 1234fa3fe2b1SFlorian Westphal 1235fa3fe2b1SFlorian Westphal if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent))) 1236fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 1237fa3fe2b1SFlorian Westphal } 1238fa3fe2b1SFlorian Westphal 1239c312ee21SGeliang Tang u16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum) 1240c94b1f96SGeliang Tang { 1241c94b1f96SGeliang Tang struct csum_pseudo_header header; 1242c94b1f96SGeliang Tang __wsum csum; 1243c94b1f96SGeliang Tang 1244c94b1f96SGeliang Tang /* cfr RFC 8684 3.3.1.: 1245c94b1f96SGeliang Tang * the data sequence number used in the pseudo-header is 1246c94b1f96SGeliang Tang * always the 64-bit value, irrespective of what length is used in the 1247c94b1f96SGeliang Tang * DSS option itself. 1248c94b1f96SGeliang Tang */ 1249f7cc8890SDavide Caratti header.data_seq = cpu_to_be64(data_seq); 1250f7cc8890SDavide Caratti header.subflow_seq = htonl(subflow_seq); 1251f7cc8890SDavide Caratti header.data_len = htons(data_len); 1252c94b1f96SGeliang Tang header.csum = 0; 1253c94b1f96SGeliang Tang 1254c312ee21SGeliang Tang csum = csum_partial(&header, sizeof(header), sum); 1255c94b1f96SGeliang Tang return (__force u16)csum_fold(csum); 1256c94b1f96SGeliang Tang } 1257c94b1f96SGeliang Tang 1258f7cc8890SDavide Caratti static u16 mptcp_make_csum(const struct mptcp_ext *mpext) 1259f7cc8890SDavide Caratti { 1260f7cc8890SDavide Caratti return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len, 1261c312ee21SGeliang Tang ~csum_unfold(mpext->csum)); 1262f7cc8890SDavide Caratti } 1263f7cc8890SDavide Caratti 1264fa3fe2b1SFlorian Westphal void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp, 1265fa3fe2b1SFlorian Westphal struct mptcp_out_options *opts) 1266eda7acddSPeter Krystad { 1267c25aeb4eSGeliang Tang const struct sock *ssk = (const struct sock *)tp; 1268c25aeb4eSGeliang Tang struct mptcp_subflow_context *subflow; 1269c25aeb4eSGeliang Tang 1270f284c0c7SPaolo Abeni /* DSS, MPC, MPJ, ADD_ADDR, FASTCLOSE and RST are mutually exclusive, 1271f284c0c7SPaolo Abeni * see mptcp_established_options*() 12721bff1e43SPaolo Abeni */ 12731bff1e43SPaolo Abeni if (likely(OPTION_MPTCP_DSS & opts->suboptions)) { 12741bff1e43SPaolo Abeni struct mptcp_ext *mpext = &opts->ext_copy; 12751bff1e43SPaolo Abeni u8 len = TCPOLEN_MPTCP_DSS_BASE; 12761bff1e43SPaolo Abeni u8 flags = 0; 12771bff1e43SPaolo Abeni 12781bff1e43SPaolo Abeni if (mpext->use_ack) { 12791bff1e43SPaolo Abeni flags = MPTCP_DSS_HAS_ACK; 12801bff1e43SPaolo Abeni if (mpext->ack64) { 12811bff1e43SPaolo Abeni len += TCPOLEN_MPTCP_DSS_ACK64; 12821bff1e43SPaolo Abeni flags |= MPTCP_DSS_ACK64; 12831bff1e43SPaolo Abeni } else { 12841bff1e43SPaolo Abeni len += TCPOLEN_MPTCP_DSS_ACK32; 12851bff1e43SPaolo Abeni } 12861bff1e43SPaolo Abeni } 12871bff1e43SPaolo Abeni 12881bff1e43SPaolo Abeni if (mpext->use_map) { 12891bff1e43SPaolo Abeni len += TCPOLEN_MPTCP_DSS_MAP64; 12901bff1e43SPaolo Abeni 12911bff1e43SPaolo Abeni /* Use only 64-bit mapping flags for now, add 12921bff1e43SPaolo Abeni * support for optional 32-bit mappings later. 12931bff1e43SPaolo Abeni */ 12941bff1e43SPaolo Abeni flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64; 12951bff1e43SPaolo Abeni if (mpext->data_fin) 12961bff1e43SPaolo Abeni flags |= MPTCP_DSS_DATA_FIN; 12971bff1e43SPaolo Abeni 12981bff1e43SPaolo Abeni if (opts->csum_reqd) 12991bff1e43SPaolo Abeni len += TCPOLEN_MPTCP_DSS_CHECKSUM; 13001bff1e43SPaolo Abeni } 13011bff1e43SPaolo Abeni 13021bff1e43SPaolo Abeni *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags); 13031bff1e43SPaolo Abeni 13041bff1e43SPaolo Abeni if (mpext->use_ack) { 13051bff1e43SPaolo Abeni if (mpext->ack64) { 13061bff1e43SPaolo Abeni put_unaligned_be64(mpext->data_ack, ptr); 13071bff1e43SPaolo Abeni ptr += 2; 13081bff1e43SPaolo Abeni } else { 13091bff1e43SPaolo Abeni put_unaligned_be32(mpext->data_ack32, ptr); 13101bff1e43SPaolo Abeni ptr += 1; 13111bff1e43SPaolo Abeni } 13121bff1e43SPaolo Abeni } 13131bff1e43SPaolo Abeni 13141bff1e43SPaolo Abeni if (mpext->use_map) { 13151bff1e43SPaolo Abeni put_unaligned_be64(mpext->data_seq, ptr); 13161bff1e43SPaolo Abeni ptr += 2; 13171bff1e43SPaolo Abeni put_unaligned_be32(mpext->subflow_seq, ptr); 13181bff1e43SPaolo Abeni ptr += 1; 13191bff1e43SPaolo Abeni if (opts->csum_reqd) { 13201bff1e43SPaolo Abeni put_unaligned_be32(mpext->data_len << 16 | 13211bff1e43SPaolo Abeni mptcp_make_csum(mpext), ptr); 13221bff1e43SPaolo Abeni } else { 13231bff1e43SPaolo Abeni put_unaligned_be32(mpext->data_len << 16 | 13241bff1e43SPaolo Abeni TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); 13251bff1e43SPaolo Abeni } 1326110b6d1fSGeliang Tang ptr += 1; 13271bff1e43SPaolo Abeni } 1328*902c8f86SMatthieu Baerts 1329*902c8f86SMatthieu Baerts /* We might need to add MP_FAIL options in rare cases */ 1330*902c8f86SMatthieu Baerts if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) 1331*902c8f86SMatthieu Baerts goto mp_fail; 133213ac17a3SGeliang Tang } else if (OPTIONS_MPTCP_MPC & opts->suboptions) { 133306fe1719SGeliang Tang u8 len, flag = MPTCP_CAP_HMAC_SHA256; 1334eda7acddSPeter Krystad 1335c94b1f96SGeliang Tang if (OPTION_MPTCP_MPC_SYN & opts->suboptions) { 1336eda7acddSPeter Krystad len = TCPOLEN_MPTCP_MPC_SYN; 1337c94b1f96SGeliang Tang } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) { 1338cec37a6eSPeter Krystad len = TCPOLEN_MPTCP_MPC_SYNACK; 1339f7cc8890SDavide Caratti } else if (opts->data_len) { 1340cc7972eaSChristoph Paasch len = TCPOLEN_MPTCP_MPC_ACK_DATA; 1341c94b1f96SGeliang Tang if (opts->csum_reqd) 1342c94b1f96SGeliang Tang len += TCPOLEN_MPTCP_DSS_CHECKSUM; 1343c94b1f96SGeliang Tang } else { 1344eda7acddSPeter Krystad len = TCPOLEN_MPTCP_MPC_ACK; 1345c94b1f96SGeliang Tang } 1346eda7acddSPeter Krystad 134706fe1719SGeliang Tang if (opts->csum_reqd) 134806fe1719SGeliang Tang flag |= MPTCP_CAP_CHECKSUM_REQD; 134906fe1719SGeliang Tang 1350bab6b88eSGeliang Tang if (!opts->allow_join_id0) 1351bab6b88eSGeliang Tang flag |= MPTCP_CAP_DENY_JOIN_ID0; 1352bab6b88eSGeliang Tang 13533df523abSPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len, 13543df523abSPeter Krystad MPTCP_SUPPORTED_VERSION, 135506fe1719SGeliang Tang flag); 1356cc7972eaSChristoph Paasch 1357cc7972eaSChristoph Paasch if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) & 1358cc7972eaSChristoph Paasch opts->suboptions)) 1359cc7972eaSChristoph Paasch goto mp_capable_done; 1360cc7972eaSChristoph Paasch 1361eda7acddSPeter Krystad put_unaligned_be64(opts->sndr_key, ptr); 1362eda7acddSPeter Krystad ptr += 2; 1363cc7972eaSChristoph Paasch if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions)) 1364cc7972eaSChristoph Paasch goto mp_capable_done; 1365cc7972eaSChristoph Paasch 1366eda7acddSPeter Krystad put_unaligned_be64(opts->rcvr_key, ptr); 1367eda7acddSPeter Krystad ptr += 2; 1368f7cc8890SDavide Caratti if (!opts->data_len) 1369cc7972eaSChristoph Paasch goto mp_capable_done; 1370cc7972eaSChristoph Paasch 1371c94b1f96SGeliang Tang if (opts->csum_reqd) { 1372f7cc8890SDavide Caratti put_unaligned_be32(opts->data_len << 16 | 1373f7cc8890SDavide Caratti __mptcp_make_csum(opts->data_seq, 1374f7cc8890SDavide Caratti opts->subflow_seq, 1375f7cc8890SDavide Caratti opts->data_len, 1376c312ee21SGeliang Tang ~csum_unfold(opts->csum)), ptr); 1377c94b1f96SGeliang Tang } else { 1378f7cc8890SDavide Caratti put_unaligned_be32(opts->data_len << 16 | 1379cc7972eaSChristoph Paasch TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); 1380c94b1f96SGeliang Tang } 1381cc7972eaSChristoph Paasch ptr += 1; 13826d0060f6SMat Martineau 13831bff1e43SPaolo Abeni /* MPC is additionally mutually exclusive with MP_PRIO */ 13841bff1e43SPaolo Abeni goto mp_capable_done; 138571b077e4SPaolo Abeni } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) { 138671b077e4SPaolo Abeni if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) { 13871bff1e43SPaolo Abeni *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 13881bff1e43SPaolo Abeni TCPOLEN_MPTCP_MPJ_SYN, 13891bff1e43SPaolo Abeni opts->backup, opts->join_id); 13901bff1e43SPaolo Abeni put_unaligned_be32(opts->token, ptr); 13911bff1e43SPaolo Abeni ptr += 1; 13921bff1e43SPaolo Abeni put_unaligned_be32(opts->nonce, ptr); 13931bff1e43SPaolo Abeni ptr += 1; 13941bff1e43SPaolo Abeni } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) { 13951bff1e43SPaolo Abeni *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 13961bff1e43SPaolo Abeni TCPOLEN_MPTCP_MPJ_SYNACK, 13971bff1e43SPaolo Abeni opts->backup, opts->join_id); 13981bff1e43SPaolo Abeni put_unaligned_be64(opts->thmac, ptr); 13991bff1e43SPaolo Abeni ptr += 2; 14001bff1e43SPaolo Abeni put_unaligned_be32(opts->nonce, ptr); 14011bff1e43SPaolo Abeni ptr += 1; 140271b077e4SPaolo Abeni } else { 14031bff1e43SPaolo Abeni *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 14041bff1e43SPaolo Abeni TCPOLEN_MPTCP_MPJ_ACK, 0, 0); 14051bff1e43SPaolo Abeni memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN); 14061bff1e43SPaolo Abeni ptr += 5; 140771b077e4SPaolo Abeni } 14081bff1e43SPaolo Abeni } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) { 14096eb3d1e3SGeliang Tang u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE; 14106eb3d1e3SGeliang Tang u8 echo = MPTCP_ADDR_ECHO; 14113df523abSPeter Krystad 14123df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1413fef6b7ecSGeliang Tang if (opts->addr.family == AF_INET6) 1414e1ef6832SGeliang Tang len = TCPOLEN_MPTCP_ADD_ADDR6_BASE; 1415e1ef6832SGeliang Tang #endif 1416e1ef6832SGeliang Tang 141730f60baeSGeliang Tang if (opts->addr.port) 141822fb85ffSGeliang Tang len += TCPOLEN_MPTCP_PORT_LEN; 141922fb85ffSGeliang Tang 14206eb3d1e3SGeliang Tang if (opts->ahmac) { 14216eb3d1e3SGeliang Tang len += sizeof(opts->ahmac); 14226eb3d1e3SGeliang Tang echo = 0; 14236eb3d1e3SGeliang Tang } 14246eb3d1e3SGeliang Tang 14253df523abSPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR, 142630f60baeSGeliang Tang len, echo, opts->addr.id); 1427fef6b7ecSGeliang Tang if (opts->addr.family == AF_INET) { 142830f60baeSGeliang Tang memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4); 14293df523abSPeter Krystad ptr += 1; 14303df523abSPeter Krystad } 14313df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1432fef6b7ecSGeliang Tang else if (opts->addr.family == AF_INET6) { 143330f60baeSGeliang Tang memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16); 14343df523abSPeter Krystad ptr += 4; 1435e1ef6832SGeliang Tang } 1436e1ef6832SGeliang Tang #endif 143722fb85ffSGeliang Tang 143830f60baeSGeliang Tang if (!opts->addr.port) { 14393df523abSPeter Krystad if (opts->ahmac) { 14403df523abSPeter Krystad put_unaligned_be64(opts->ahmac, ptr); 14413df523abSPeter Krystad ptr += 2; 14423df523abSPeter Krystad } 144322fb85ffSGeliang Tang } else { 144430f60baeSGeliang Tang u16 port = ntohs(opts->addr.port); 144530f60baeSGeliang Tang 144622fb85ffSGeliang Tang if (opts->ahmac) { 144722fb85ffSGeliang Tang u8 *bptr = (u8 *)ptr; 144822fb85ffSGeliang Tang 144930f60baeSGeliang Tang put_unaligned_be16(port, bptr); 145022fb85ffSGeliang Tang bptr += 2; 145122fb85ffSGeliang Tang put_unaligned_be64(opts->ahmac, bptr); 145222fb85ffSGeliang Tang bptr += 8; 145322fb85ffSGeliang Tang put_unaligned_be16(TCPOPT_NOP << 8 | 145422fb85ffSGeliang Tang TCPOPT_NOP, bptr); 145522fb85ffSGeliang Tang 145622fb85ffSGeliang Tang ptr += 3; 145722fb85ffSGeliang Tang } else { 145830f60baeSGeliang Tang put_unaligned_be32(port << 16 | 145922fb85ffSGeliang Tang TCPOPT_NOP << 8 | 146022fb85ffSGeliang Tang TCPOPT_NOP, ptr); 146122fb85ffSGeliang Tang ptr += 1; 14623df523abSPeter Krystad } 146322fb85ffSGeliang Tang } 1464f284c0c7SPaolo Abeni } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) { 1465f284c0c7SPaolo Abeni /* FASTCLOSE is mutually exclusive with others except RST */ 1466f284c0c7SPaolo Abeni *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE, 1467f284c0c7SPaolo Abeni TCPOLEN_MPTCP_FASTCLOSE, 1468f284c0c7SPaolo Abeni 0, 0); 1469f284c0c7SPaolo Abeni put_unaligned_be64(opts->rcvr_key, ptr); 1470f284c0c7SPaolo Abeni ptr += 2; 1471f284c0c7SPaolo Abeni 1472f284c0c7SPaolo Abeni if (OPTION_MPTCP_RST & opts->suboptions) 1473f284c0c7SPaolo Abeni goto mp_rst; 1474f284c0c7SPaolo Abeni return; 1475*902c8f86SMatthieu Baerts } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) { 1476*902c8f86SMatthieu Baerts mp_fail: 1477*902c8f86SMatthieu Baerts /* MP_FAIL is mutually exclusive with others except RST */ 1478*902c8f86SMatthieu Baerts subflow = mptcp_subflow_ctx(ssk); 1479*902c8f86SMatthieu Baerts subflow->send_mp_fail = 0; 1480*902c8f86SMatthieu Baerts 1481*902c8f86SMatthieu Baerts *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL, 1482*902c8f86SMatthieu Baerts TCPOLEN_MPTCP_FAIL, 1483*902c8f86SMatthieu Baerts 0, 0); 1484*902c8f86SMatthieu Baerts put_unaligned_be64(opts->fail_seq, ptr); 1485*902c8f86SMatthieu Baerts ptr += 2; 1486*902c8f86SMatthieu Baerts 1487*902c8f86SMatthieu Baerts if (OPTION_MPTCP_RST & opts->suboptions) 1488*902c8f86SMatthieu Baerts goto mp_rst; 1489*902c8f86SMatthieu Baerts return; 1490f284c0c7SPaolo Abeni } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) { 1491f284c0c7SPaolo Abeni mp_rst: 1492f284c0c7SPaolo Abeni *ptr++ = mptcp_option(MPTCPOPT_RST, 1493f284c0c7SPaolo Abeni TCPOLEN_MPTCP_RST, 1494f284c0c7SPaolo Abeni opts->reset_transient, 1495f284c0c7SPaolo Abeni opts->reset_reason); 1496f284c0c7SPaolo Abeni return; 14973df523abSPeter Krystad } 14983df523abSPeter Krystad 14991bff1e43SPaolo Abeni if (OPTION_MPTCP_PRIO & opts->suboptions) { 15001bff1e43SPaolo Abeni subflow = mptcp_subflow_ctx(ssk); 15011bff1e43SPaolo Abeni subflow->send_mp_prio = 0; 15021bff1e43SPaolo Abeni 15031bff1e43SPaolo Abeni *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO, 15041bff1e43SPaolo Abeni TCPOLEN_MPTCP_PRIO, 15051bff1e43SPaolo Abeni opts->backup, TCPOPT_NOP); 15061bff1e43SPaolo Abeni } 15071bff1e43SPaolo Abeni 15081bff1e43SPaolo Abeni mp_capable_done: 15093df523abSPeter Krystad if (OPTION_MPTCP_RM_ADDR & opts->suboptions) { 15106445e17aSGeliang Tang u8 i = 1; 15116445e17aSGeliang Tang 15123df523abSPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR, 15136445e17aSGeliang Tang TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr, 15146445e17aSGeliang Tang 0, opts->rm_list.ids[0]); 15156445e17aSGeliang Tang 15166445e17aSGeliang Tang while (i < opts->rm_list.nr) { 15176445e17aSGeliang Tang u8 id1, id2, id3, id4; 15186445e17aSGeliang Tang 15196445e17aSGeliang Tang id1 = opts->rm_list.ids[i]; 15206445e17aSGeliang Tang id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP; 15216445e17aSGeliang Tang id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP; 15226445e17aSGeliang Tang id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP; 15236445e17aSGeliang Tang put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr); 15246445e17aSGeliang Tang ptr += 1; 15256445e17aSGeliang Tang i += 4; 15266445e17aSGeliang Tang } 15273df523abSPeter Krystad } 15283df523abSPeter Krystad 1529fa3fe2b1SFlorian Westphal if (tp) 1530fa3fe2b1SFlorian Westphal mptcp_set_rwin(tp); 1531eda7acddSPeter Krystad } 1532dc87efdbSFlorian Westphal 1533dc87efdbSFlorian Westphal __be32 mptcp_get_reset_option(const struct sk_buff *skb) 1534dc87efdbSFlorian Westphal { 1535dc87efdbSFlorian Westphal const struct mptcp_ext *ext = mptcp_get_ext(skb); 1536dc87efdbSFlorian Westphal u8 flags, reason; 1537dc87efdbSFlorian Westphal 1538dc87efdbSFlorian Westphal if (ext) { 1539dc87efdbSFlorian Westphal flags = ext->reset_transient; 1540dc87efdbSFlorian Westphal reason = ext->reset_reason; 1541dc87efdbSFlorian Westphal 1542dc87efdbSFlorian Westphal return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST, 1543dc87efdbSFlorian Westphal flags, reason); 1544dc87efdbSFlorian Westphal } 1545dc87efdbSFlorian Westphal 1546dc87efdbSFlorian Westphal return htonl(0u); 1547dc87efdbSFlorian Westphal } 1548dc87efdbSFlorian Westphal EXPORT_SYMBOL_GPL(mptcp_get_reset_option); 1549