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 } 47cc7972eaSChristoph Paasch if (opsize != expected_opsize) 48eda7acddSPeter Krystad break; 49eda7acddSPeter Krystad 50cc7972eaSChristoph Paasch /* try to be gentle vs future versions on the initial syn */ 51eda7acddSPeter Krystad version = *ptr++ & MPTCP_VERSION_MASK; 52cc7972eaSChristoph Paasch if (opsize != TCPOLEN_MPTCP_MPC_SYN) { 53eda7acddSPeter Krystad if (version != MPTCP_SUPPORTED_VERSION) 54eda7acddSPeter Krystad break; 55cc7972eaSChristoph Paasch } else if (version < MPTCP_SUPPORTED_VERSION) { 56cc7972eaSChristoph Paasch break; 57cc7972eaSChristoph Paasch } 58eda7acddSPeter Krystad 59eda7acddSPeter Krystad flags = *ptr++; 6065492c5aSPaolo Abeni if (!mptcp_cap_flag_sha256(flags) || 61eda7acddSPeter Krystad (flags & MPTCP_CAP_EXTENSIBILITY)) 62eda7acddSPeter Krystad break; 63eda7acddSPeter Krystad 64eda7acddSPeter Krystad /* RFC 6824, Section 3.1: 65eda7acddSPeter Krystad * "For the Checksum Required bit (labeled "A"), if either 66eda7acddSPeter Krystad * host requires the use of checksums, checksums MUST be used. 67eda7acddSPeter Krystad * In other words, the only way for checksums not to be used 68eda7acddSPeter Krystad * is if both hosts in their SYNs set A=0." 69eda7acddSPeter Krystad * 70eda7acddSPeter Krystad * Section 3.3.0: 71eda7acddSPeter Krystad * "If a checksum is not present when its use has been 72eda7acddSPeter Krystad * negotiated, the receiver MUST close the subflow with a RST as 73eda7acddSPeter Krystad * it is considered broken." 74eda7acddSPeter Krystad * 75eda7acddSPeter Krystad * We don't implement DSS checksum - fall back to TCP. 76eda7acddSPeter Krystad */ 77eda7acddSPeter Krystad if (flags & MPTCP_CAP_CHECKSUM_REQD) 78eda7acddSPeter Krystad break; 79eda7acddSPeter Krystad 80eda7acddSPeter Krystad mp_opt->mp_capable = 1; 81cc7972eaSChristoph Paasch if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) { 82eda7acddSPeter Krystad mp_opt->sndr_key = get_unaligned_be64(ptr); 83eda7acddSPeter Krystad ptr += 8; 84cc7972eaSChristoph Paasch } 85cc7972eaSChristoph Paasch if (opsize >= TCPOLEN_MPTCP_MPC_ACK) { 86eda7acddSPeter Krystad mp_opt->rcvr_key = get_unaligned_be64(ptr); 87eda7acddSPeter Krystad ptr += 8; 88eda7acddSPeter Krystad } 89cc7972eaSChristoph Paasch if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) { 90cc7972eaSChristoph Paasch /* Section 3.1.: 91cc7972eaSChristoph Paasch * "the data parameters in a MP_CAPABLE are semantically 92cc7972eaSChristoph Paasch * equivalent to those in a DSS option and can be used 93cc7972eaSChristoph Paasch * interchangeably." 94cc7972eaSChristoph Paasch */ 95cc7972eaSChristoph Paasch mp_opt->dss = 1; 96cc7972eaSChristoph Paasch mp_opt->use_map = 1; 97cc7972eaSChristoph Paasch mp_opt->mpc_map = 1; 98cc7972eaSChristoph Paasch mp_opt->data_len = get_unaligned_be16(ptr); 99cc7972eaSChristoph Paasch ptr += 2; 100cc7972eaSChristoph Paasch } 101cc7972eaSChristoph Paasch pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d", 102cc7972eaSChristoph Paasch version, flags, opsize, mp_opt->sndr_key, 103cc7972eaSChristoph Paasch mp_opt->rcvr_key, mp_opt->data_len); 104eda7acddSPeter Krystad break; 105eda7acddSPeter Krystad 106f296234cSPeter Krystad case MPTCPOPT_MP_JOIN: 107f296234cSPeter Krystad mp_opt->mp_join = 1; 108f296234cSPeter Krystad if (opsize == TCPOLEN_MPTCP_MPJ_SYN) { 109f296234cSPeter Krystad mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP; 110f296234cSPeter Krystad mp_opt->join_id = *ptr++; 111f296234cSPeter Krystad mp_opt->token = get_unaligned_be32(ptr); 112f296234cSPeter Krystad ptr += 4; 113f296234cSPeter Krystad mp_opt->nonce = get_unaligned_be32(ptr); 114f296234cSPeter Krystad ptr += 4; 115f296234cSPeter Krystad pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u", 116f296234cSPeter Krystad mp_opt->backup, mp_opt->join_id, 117f296234cSPeter Krystad mp_opt->token, mp_opt->nonce); 118f296234cSPeter Krystad } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) { 119f296234cSPeter Krystad mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP; 120f296234cSPeter Krystad mp_opt->join_id = *ptr++; 121f296234cSPeter Krystad mp_opt->thmac = get_unaligned_be64(ptr); 122f296234cSPeter Krystad ptr += 8; 123f296234cSPeter Krystad mp_opt->nonce = get_unaligned_be32(ptr); 124f296234cSPeter Krystad ptr += 4; 125f296234cSPeter Krystad pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u", 126f296234cSPeter Krystad mp_opt->backup, mp_opt->join_id, 127f296234cSPeter Krystad mp_opt->thmac, mp_opt->nonce); 128f296234cSPeter Krystad } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) { 129f296234cSPeter Krystad ptr += 2; 130f296234cSPeter Krystad memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN); 131f296234cSPeter Krystad pr_debug("MP_JOIN hmac"); 132f296234cSPeter Krystad } else { 133f296234cSPeter Krystad mp_opt->mp_join = 0; 134f296234cSPeter Krystad } 135f296234cSPeter Krystad break; 136f296234cSPeter Krystad 137eda7acddSPeter Krystad case MPTCPOPT_DSS: 138eda7acddSPeter Krystad pr_debug("DSS"); 139648ef4b8SMat Martineau ptr++; 140648ef4b8SMat Martineau 141cc7972eaSChristoph Paasch /* we must clear 'mpc_map' be able to detect MP_CAPABLE 142cc7972eaSChristoph Paasch * map vs DSS map in mptcp_incoming_options(), and reconstruct 143cc7972eaSChristoph Paasch * map info accordingly 144cc7972eaSChristoph Paasch */ 145cc7972eaSChristoph Paasch mp_opt->mpc_map = 0; 146648ef4b8SMat Martineau flags = (*ptr++) & MPTCP_DSS_FLAG_MASK; 147648ef4b8SMat Martineau mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0; 148648ef4b8SMat Martineau mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0; 149648ef4b8SMat Martineau mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0; 150648ef4b8SMat Martineau mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0; 151648ef4b8SMat Martineau mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK); 152648ef4b8SMat Martineau 153648ef4b8SMat Martineau pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d", 154648ef4b8SMat Martineau mp_opt->data_fin, mp_opt->dsn64, 155648ef4b8SMat Martineau mp_opt->use_map, mp_opt->ack64, 156648ef4b8SMat Martineau mp_opt->use_ack); 157648ef4b8SMat Martineau 158648ef4b8SMat Martineau expected_opsize = TCPOLEN_MPTCP_DSS_BASE; 159648ef4b8SMat Martineau 160648ef4b8SMat Martineau if (mp_opt->use_ack) { 161648ef4b8SMat Martineau if (mp_opt->ack64) 162648ef4b8SMat Martineau expected_opsize += TCPOLEN_MPTCP_DSS_ACK64; 163648ef4b8SMat Martineau else 164648ef4b8SMat Martineau expected_opsize += TCPOLEN_MPTCP_DSS_ACK32; 165648ef4b8SMat Martineau } 166648ef4b8SMat Martineau 167648ef4b8SMat Martineau if (mp_opt->use_map) { 168648ef4b8SMat Martineau if (mp_opt->dsn64) 169648ef4b8SMat Martineau expected_opsize += TCPOLEN_MPTCP_DSS_MAP64; 170648ef4b8SMat Martineau else 171648ef4b8SMat Martineau expected_opsize += TCPOLEN_MPTCP_DSS_MAP32; 172648ef4b8SMat Martineau } 173648ef4b8SMat Martineau 174648ef4b8SMat Martineau /* RFC 6824, Section 3.3: 175648ef4b8SMat Martineau * If a checksum is present, but its use had 176648ef4b8SMat Martineau * not been negotiated in the MP_CAPABLE handshake, 177648ef4b8SMat Martineau * the checksum field MUST be ignored. 178648ef4b8SMat Martineau */ 179648ef4b8SMat Martineau if (opsize != expected_opsize && 180648ef4b8SMat Martineau opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) 181648ef4b8SMat Martineau break; 182648ef4b8SMat Martineau 183eda7acddSPeter Krystad mp_opt->dss = 1; 184648ef4b8SMat Martineau 185648ef4b8SMat Martineau if (mp_opt->use_ack) { 186648ef4b8SMat Martineau if (mp_opt->ack64) { 187648ef4b8SMat Martineau mp_opt->data_ack = get_unaligned_be64(ptr); 188648ef4b8SMat Martineau ptr += 8; 189648ef4b8SMat Martineau } else { 190648ef4b8SMat Martineau mp_opt->data_ack = get_unaligned_be32(ptr); 191648ef4b8SMat Martineau ptr += 4; 192648ef4b8SMat Martineau } 193648ef4b8SMat Martineau 194648ef4b8SMat Martineau pr_debug("data_ack=%llu", mp_opt->data_ack); 195648ef4b8SMat Martineau } 196648ef4b8SMat Martineau 197648ef4b8SMat Martineau if (mp_opt->use_map) { 198648ef4b8SMat Martineau if (mp_opt->dsn64) { 199648ef4b8SMat Martineau mp_opt->data_seq = get_unaligned_be64(ptr); 200648ef4b8SMat Martineau ptr += 8; 201648ef4b8SMat Martineau } else { 202648ef4b8SMat Martineau mp_opt->data_seq = get_unaligned_be32(ptr); 203648ef4b8SMat Martineau ptr += 4; 204648ef4b8SMat Martineau } 205648ef4b8SMat Martineau 206648ef4b8SMat Martineau mp_opt->subflow_seq = get_unaligned_be32(ptr); 207648ef4b8SMat Martineau ptr += 4; 208648ef4b8SMat Martineau 209648ef4b8SMat Martineau mp_opt->data_len = get_unaligned_be16(ptr); 210648ef4b8SMat Martineau ptr += 2; 211648ef4b8SMat Martineau 212648ef4b8SMat Martineau pr_debug("data_seq=%llu subflow_seq=%u data_len=%u", 213648ef4b8SMat Martineau mp_opt->data_seq, mp_opt->subflow_seq, 214648ef4b8SMat Martineau mp_opt->data_len); 215648ef4b8SMat Martineau } 216648ef4b8SMat Martineau 217eda7acddSPeter Krystad break; 218eda7acddSPeter Krystad 2193df523abSPeter Krystad case MPTCPOPT_ADD_ADDR: 2203df523abSPeter Krystad mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO; 2213df523abSPeter Krystad if (!mp_opt->echo) { 2223df523abSPeter Krystad if (opsize == TCPOLEN_MPTCP_ADD_ADDR || 2233df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT) 2241b1a6ef5SGeliang Tang mp_opt->addr.family = AF_INET; 2253df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2263df523abSPeter Krystad else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 || 2273df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT) 2281b1a6ef5SGeliang Tang mp_opt->addr.family = AF_INET6; 2293df523abSPeter Krystad #endif 2303df523abSPeter Krystad else 2313df523abSPeter Krystad break; 2323df523abSPeter Krystad } else { 2333df523abSPeter Krystad if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE || 2343df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) 2351b1a6ef5SGeliang Tang mp_opt->addr.family = AF_INET; 2363df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2373df523abSPeter Krystad else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE || 2383df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) 2391b1a6ef5SGeliang Tang mp_opt->addr.family = AF_INET6; 2403df523abSPeter Krystad #endif 2413df523abSPeter Krystad else 2423df523abSPeter Krystad break; 2433df523abSPeter Krystad } 2443df523abSPeter Krystad 2453df523abSPeter Krystad mp_opt->add_addr = 1; 246f7dafee1SGeliang Tang mp_opt->addr.id = *ptr++; 2471b1a6ef5SGeliang Tang if (mp_opt->addr.family == AF_INET) { 248f7dafee1SGeliang Tang memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4); 2493df523abSPeter Krystad ptr += 4; 2503df523abSPeter Krystad if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT || 2513df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) { 252f7dafee1SGeliang Tang mp_opt->addr.port = htons(get_unaligned_be16(ptr)); 2533df523abSPeter Krystad ptr += 2; 2543df523abSPeter Krystad } 2553df523abSPeter Krystad } 2563df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 2573df523abSPeter Krystad else { 258f7dafee1SGeliang Tang memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16); 2593df523abSPeter Krystad ptr += 16; 2603df523abSPeter Krystad if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT || 2613df523abSPeter Krystad opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) { 262f7dafee1SGeliang Tang mp_opt->addr.port = htons(get_unaligned_be16(ptr)); 2633df523abSPeter Krystad ptr += 2; 2643df523abSPeter Krystad } 2653df523abSPeter Krystad } 2663df523abSPeter Krystad #endif 2673df523abSPeter Krystad if (!mp_opt->echo) { 2683df523abSPeter Krystad mp_opt->ahmac = get_unaligned_be64(ptr); 2693df523abSPeter Krystad ptr += 8; 2703df523abSPeter Krystad } 27190a4aea8SGeliang Tang pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d", 2721b1a6ef5SGeliang Tang (mp_opt->addr.family == AF_INET6) ? "6" : "", 273f7dafee1SGeliang Tang mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port)); 2743df523abSPeter Krystad break; 2753df523abSPeter Krystad 2763df523abSPeter Krystad case MPTCPOPT_RM_ADDR: 2775c4a824dSGeliang Tang if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 || 2785c4a824dSGeliang Tang opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX) 2793df523abSPeter Krystad break; 2803df523abSPeter Krystad 2818e60eed6SGeliang Tang ptr++; 2828e60eed6SGeliang Tang 2833df523abSPeter Krystad mp_opt->rm_addr = 1; 2845c4a824dSGeliang Tang mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE; 2855c4a824dSGeliang Tang for (i = 0; i < mp_opt->rm_list.nr; i++) 2865c4a824dSGeliang Tang mp_opt->rm_list.ids[i] = *ptr++; 2875c4a824dSGeliang Tang pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr); 2883df523abSPeter Krystad break; 2893df523abSPeter Krystad 29040453a5cSGeliang Tang case MPTCPOPT_MP_PRIO: 29140453a5cSGeliang Tang if (opsize != TCPOLEN_MPTCP_PRIO) 29240453a5cSGeliang Tang break; 29340453a5cSGeliang Tang 29440453a5cSGeliang Tang mp_opt->mp_prio = 1; 29540453a5cSGeliang Tang mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP; 29640453a5cSGeliang Tang pr_debug("MP_PRIO: prio=%d", mp_opt->backup); 29740453a5cSGeliang Tang break; 29840453a5cSGeliang Tang 29950c504a2SFlorian Westphal case MPTCPOPT_MP_FASTCLOSE: 30050c504a2SFlorian Westphal if (opsize != TCPOLEN_MPTCP_FASTCLOSE) 30150c504a2SFlorian Westphal break; 30250c504a2SFlorian Westphal 30350c504a2SFlorian Westphal ptr += 2; 30450c504a2SFlorian Westphal mp_opt->rcvr_key = get_unaligned_be64(ptr); 30550c504a2SFlorian Westphal ptr += 8; 30650c504a2SFlorian Westphal mp_opt->fastclose = 1; 30750c504a2SFlorian Westphal break; 30850c504a2SFlorian Westphal 309dc87efdbSFlorian Westphal case MPTCPOPT_RST: 310dc87efdbSFlorian Westphal if (opsize != TCPOLEN_MPTCP_RST) 311dc87efdbSFlorian Westphal break; 312dc87efdbSFlorian Westphal 313dc87efdbSFlorian Westphal if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) 314dc87efdbSFlorian Westphal break; 315dc87efdbSFlorian Westphal mp_opt->reset = 1; 316dc87efdbSFlorian Westphal flags = *ptr++; 317dc87efdbSFlorian Westphal mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT; 318dc87efdbSFlorian Westphal mp_opt->reset_reason = *ptr; 319dc87efdbSFlorian Westphal break; 320dc87efdbSFlorian Westphal 321eda7acddSPeter Krystad default: 322eda7acddSPeter Krystad break; 323eda7acddSPeter Krystad } 324eda7acddSPeter Krystad } 325eda7acddSPeter Krystad 326cec37a6eSPeter Krystad void mptcp_get_options(const struct sk_buff *skb, 327cfde141eSPaolo Abeni struct mptcp_options_received *mp_opt) 328cec37a6eSPeter Krystad { 329cec37a6eSPeter Krystad const struct tcphdr *th = tcp_hdr(skb); 330cfde141eSPaolo Abeni const unsigned char *ptr; 331cfde141eSPaolo Abeni int length; 332cec37a6eSPeter Krystad 333cfde141eSPaolo Abeni /* initialize option status */ 334cfde141eSPaolo Abeni mp_opt->mp_capable = 0; 335cfde141eSPaolo Abeni mp_opt->mp_join = 0; 336cfde141eSPaolo Abeni mp_opt->add_addr = 0; 337fe2d9b1aSGeliang Tang mp_opt->ahmac = 0; 33850c504a2SFlorian Westphal mp_opt->fastclose = 0; 339f7dafee1SGeliang Tang mp_opt->addr.port = 0; 340cfde141eSPaolo Abeni mp_opt->rm_addr = 0; 341cfde141eSPaolo Abeni mp_opt->dss = 0; 34240453a5cSGeliang Tang mp_opt->mp_prio = 0; 343dc87efdbSFlorian Westphal mp_opt->reset = 0; 344cfde141eSPaolo Abeni 345cfde141eSPaolo Abeni length = (th->doff * 4) - sizeof(struct tcphdr); 346cec37a6eSPeter Krystad ptr = (const unsigned char *)(th + 1); 347cec37a6eSPeter Krystad 348cec37a6eSPeter Krystad while (length > 0) { 349cec37a6eSPeter Krystad int opcode = *ptr++; 350cec37a6eSPeter Krystad int opsize; 351cec37a6eSPeter Krystad 352cec37a6eSPeter Krystad switch (opcode) { 353cec37a6eSPeter Krystad case TCPOPT_EOL: 354cec37a6eSPeter Krystad return; 355cec37a6eSPeter Krystad case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ 356cec37a6eSPeter Krystad length--; 357cec37a6eSPeter Krystad continue; 358cec37a6eSPeter Krystad default: 359cec37a6eSPeter Krystad opsize = *ptr++; 360cec37a6eSPeter Krystad if (opsize < 2) /* "silly options" */ 361cec37a6eSPeter Krystad return; 362cec37a6eSPeter Krystad if (opsize > length) 363cec37a6eSPeter Krystad return; /* don't parse partial options */ 364cec37a6eSPeter Krystad if (opcode == TCPOPT_MPTCP) 365cfde141eSPaolo Abeni mptcp_parse_option(skb, ptr, opsize, mp_opt); 366cec37a6eSPeter Krystad ptr += opsize - 2; 367cec37a6eSPeter Krystad length -= opsize; 368cec37a6eSPeter Krystad } 369cec37a6eSPeter Krystad } 370cec37a6eSPeter Krystad } 371cec37a6eSPeter Krystad 372cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb, 373cc7972eaSChristoph Paasch unsigned int *size, struct mptcp_out_options *opts) 374cec37a6eSPeter Krystad { 375cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 376cec37a6eSPeter Krystad 377cc7972eaSChristoph Paasch /* we will use snd_isn to detect first pkt [re]transmission 378cc7972eaSChristoph Paasch * in mptcp_established_options_mp() 379cc7972eaSChristoph Paasch */ 380cc7972eaSChristoph Paasch subflow->snd_isn = TCP_SKB_CB(skb)->end_seq; 381cec37a6eSPeter Krystad if (subflow->request_mptcp) { 382cec37a6eSPeter Krystad opts->suboptions = OPTION_MPTCP_MPC_SYN; 383*06fe1719SGeliang Tang opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk)); 384cec37a6eSPeter Krystad *size = TCPOLEN_MPTCP_MPC_SYN; 385cec37a6eSPeter Krystad return true; 386ec3edaa7SPeter Krystad } else if (subflow->request_join) { 387ec3edaa7SPeter Krystad pr_debug("remote_token=%u, nonce=%u", subflow->remote_token, 388ec3edaa7SPeter Krystad subflow->local_nonce); 389ec3edaa7SPeter Krystad opts->suboptions = OPTION_MPTCP_MPJ_SYN; 390ec3edaa7SPeter Krystad opts->join_id = subflow->local_id; 391ec3edaa7SPeter Krystad opts->token = subflow->remote_token; 392ec3edaa7SPeter Krystad opts->nonce = subflow->local_nonce; 393ec3edaa7SPeter Krystad opts->backup = subflow->request_bkup; 394ec3edaa7SPeter Krystad *size = TCPOLEN_MPTCP_MPJ_SYN; 395ec3edaa7SPeter Krystad return true; 396cec37a6eSPeter Krystad } 397cec37a6eSPeter Krystad return false; 398cec37a6eSPeter Krystad } 399cec37a6eSPeter Krystad 400ec3edaa7SPeter Krystad /* MP_JOIN client subflow must wait for 4th ack before sending any data: 401ec3edaa7SPeter Krystad * TCP can't schedule delack timer before the subflow is fully established. 402ec3edaa7SPeter Krystad * MPTCP uses the delack timer to do 3rd ack retransmissions 403ec3edaa7SPeter Krystad */ 404ec3edaa7SPeter Krystad static void schedule_3rdack_retransmission(struct sock *sk) 405ec3edaa7SPeter Krystad { 406ec3edaa7SPeter Krystad struct inet_connection_sock *icsk = inet_csk(sk); 407ec3edaa7SPeter Krystad struct tcp_sock *tp = tcp_sk(sk); 408ec3edaa7SPeter Krystad unsigned long timeout; 409ec3edaa7SPeter Krystad 410ec3edaa7SPeter Krystad /* reschedule with a timeout above RTT, as we must look only for drop */ 411ec3edaa7SPeter Krystad if (tp->srtt_us) 412ec3edaa7SPeter Krystad timeout = tp->srtt_us << 1; 413ec3edaa7SPeter Krystad else 414ec3edaa7SPeter Krystad timeout = TCP_TIMEOUT_INIT; 415ec3edaa7SPeter Krystad 416ec3edaa7SPeter Krystad WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); 417ec3edaa7SPeter Krystad icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 418ec3edaa7SPeter Krystad icsk->icsk_ack.timeout = timeout; 419ec3edaa7SPeter Krystad sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); 420ec3edaa7SPeter Krystad } 421ec3edaa7SPeter Krystad 422ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk) 423ec3edaa7SPeter Krystad { 424ec3edaa7SPeter Krystad struct inet_connection_sock *icsk = inet_csk(sk); 425ec3edaa7SPeter Krystad 426ec3edaa7SPeter Krystad sk_stop_timer(sk, &icsk->icsk_delack_timer); 427ec3edaa7SPeter Krystad icsk->icsk_ack.timeout = 0; 428ec3edaa7SPeter Krystad icsk->icsk_ack.ato = 0; 429ec3edaa7SPeter Krystad icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER); 430ec3edaa7SPeter Krystad } 431ec3edaa7SPeter Krystad 432cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb, 433d87903b6SPaolo Abeni bool snd_data_fin_enable, 434cc7972eaSChristoph Paasch unsigned int *size, 4356d0060f6SMat Martineau unsigned int remaining, 436cec37a6eSPeter Krystad struct mptcp_out_options *opts) 437cec37a6eSPeter Krystad { 438cec37a6eSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 439*06fe1719SGeliang Tang struct mptcp_sock *msk = mptcp_sk(subflow->conn); 440cc7972eaSChristoph Paasch struct mptcp_ext *mpext; 441cc7972eaSChristoph Paasch unsigned int data_len; 442cec37a6eSPeter Krystad 443ec3edaa7SPeter Krystad /* When skb is not available, we better over-estimate the emitted 444ec3edaa7SPeter Krystad * options len. A full DSS option (28 bytes) is longer than 445ec3edaa7SPeter Krystad * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so 446ec3edaa7SPeter Krystad * tell the caller to defer the estimate to 447ec3edaa7SPeter Krystad * mptcp_established_options_dss(), which will reserve enough space. 448cc7972eaSChristoph Paasch */ 449ec3edaa7SPeter Krystad if (!skb) 450ec3edaa7SPeter Krystad return false; 451ec3edaa7SPeter Krystad 452d87903b6SPaolo Abeni /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */ 453d87903b6SPaolo Abeni if (subflow->fully_established || snd_data_fin_enable || 454d87903b6SPaolo Abeni subflow->snd_isn != TCP_SKB_CB(skb)->seq || 455d87903b6SPaolo Abeni sk->sk_state != TCP_ESTABLISHED) 456ec3edaa7SPeter Krystad return false; 457ec3edaa7SPeter Krystad 458ec3edaa7SPeter Krystad if (subflow->mp_capable) { 459cc7972eaSChristoph Paasch mpext = mptcp_get_ext(skb); 460cc7972eaSChristoph Paasch data_len = mpext ? mpext->data_len : 0; 461cc7972eaSChristoph Paasch 462cc7972eaSChristoph Paasch /* we will check ext_copy.data_len in mptcp_write_options() to 463cc7972eaSChristoph Paasch * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and 464cc7972eaSChristoph Paasch * TCPOLEN_MPTCP_MPC_ACK 465cc7972eaSChristoph Paasch */ 466cc7972eaSChristoph Paasch opts->ext_copy.data_len = data_len; 467cec37a6eSPeter Krystad opts->suboptions = OPTION_MPTCP_MPC_ACK; 468cec37a6eSPeter Krystad opts->sndr_key = subflow->local_key; 469cec37a6eSPeter Krystad opts->rcvr_key = subflow->remote_key; 470*06fe1719SGeliang Tang opts->csum_reqd = READ_ONCE(msk->csum_enabled); 471cc7972eaSChristoph Paasch 472cc7972eaSChristoph Paasch /* Section 3.1. 473cc7972eaSChristoph Paasch * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK 474cc7972eaSChristoph Paasch * packets that start the first subflow of an MPTCP connection, 475cc7972eaSChristoph Paasch * as well as the first packet that carries data 476cc7972eaSChristoph Paasch */ 477cc7972eaSChristoph Paasch if (data_len > 0) 478cc7972eaSChristoph Paasch *size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4); 479cc7972eaSChristoph Paasch else 480cec37a6eSPeter Krystad *size = TCPOLEN_MPTCP_MPC_ACK; 481cc7972eaSChristoph Paasch 482cc7972eaSChristoph Paasch pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d", 483cc7972eaSChristoph Paasch subflow, subflow->local_key, subflow->remote_key, 484cc7972eaSChristoph Paasch data_len); 485cc7972eaSChristoph Paasch 486cec37a6eSPeter Krystad return true; 487ec3edaa7SPeter Krystad } else if (subflow->mp_join) { 488ec3edaa7SPeter Krystad opts->suboptions = OPTION_MPTCP_MPJ_ACK; 489ec3edaa7SPeter Krystad memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN); 490ec3edaa7SPeter Krystad *size = TCPOLEN_MPTCP_MPJ_ACK; 491ec3edaa7SPeter Krystad pr_debug("subflow=%p", subflow); 492ec3edaa7SPeter Krystad 493ec3edaa7SPeter Krystad schedule_3rdack_retransmission(sk); 494ec3edaa7SPeter Krystad return true; 495cec37a6eSPeter Krystad } 496cec37a6eSPeter Krystad return false; 497cec37a6eSPeter Krystad } 498cec37a6eSPeter Krystad 4996d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow, 5009c29e361SPaolo Abeni struct sk_buff *skb, struct mptcp_ext *ext) 5016d0060f6SMat Martineau { 502017512a0SPaolo Abeni /* The write_seq value has already been incremented, so the actual 503017512a0SPaolo Abeni * sequence number for the DATA_FIN is one less. 504017512a0SPaolo Abeni */ 505017512a0SPaolo Abeni u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1; 5067279da61SMat Martineau 5079c29e361SPaolo Abeni if (!ext->use_map || !skb->len) { 5086d0060f6SMat Martineau /* RFC6824 requires a DSS mapping with specific values 5096d0060f6SMat Martineau * if DATA_FIN is set but no data payload is mapped 5106d0060f6SMat Martineau */ 5116d37a0b8SMat Martineau ext->data_fin = 1; 5126d0060f6SMat Martineau ext->use_map = 1; 5136d0060f6SMat Martineau ext->dsn64 = 1; 514017512a0SPaolo Abeni ext->data_seq = data_fin_tx_seq; 5156d0060f6SMat Martineau ext->subflow_seq = 0; 5166d0060f6SMat Martineau ext->data_len = 1; 5177279da61SMat Martineau } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) { 5186d37a0b8SMat Martineau /* If there's an existing DSS mapping and it is the 5196d37a0b8SMat Martineau * final mapping, DATA_FIN consumes 1 additional byte of 5206d37a0b8SMat Martineau * mapping space. 5216d0060f6SMat Martineau */ 5226d37a0b8SMat Martineau ext->data_fin = 1; 5236d0060f6SMat Martineau ext->data_len++; 5246d0060f6SMat Martineau } 5256d0060f6SMat Martineau } 5266d0060f6SMat Martineau 5276d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb, 528d87903b6SPaolo Abeni bool snd_data_fin_enable, 5296d0060f6SMat Martineau unsigned int *size, 5306d0060f6SMat Martineau unsigned int remaining, 5316d0060f6SMat Martineau struct mptcp_out_options *opts) 5326d0060f6SMat Martineau { 5336d0060f6SMat Martineau struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 5347279da61SMat Martineau struct mptcp_sock *msk = mptcp_sk(subflow->conn); 5356d0060f6SMat Martineau unsigned int dss_size = 0; 5366d0060f6SMat Martineau struct mptcp_ext *mpext; 5376d0060f6SMat Martineau unsigned int ack_size; 538d22f4988SChristoph Paasch bool ret = false; 539d87903b6SPaolo Abeni u64 ack_seq; 5406d0060f6SMat Martineau 5410bac966aSMat Martineau mpext = skb ? mptcp_get_ext(skb) : NULL; 5426d0060f6SMat Martineau 5437279da61SMat Martineau if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) { 5446d0060f6SMat Martineau unsigned int map_size; 5456d0060f6SMat Martineau 5466d0060f6SMat Martineau map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64; 5476d0060f6SMat Martineau 5486d0060f6SMat Martineau remaining -= map_size; 5496d0060f6SMat Martineau dss_size = map_size; 5506d0060f6SMat Martineau if (mpext) 5516d0060f6SMat Martineau opts->ext_copy = *mpext; 5526d0060f6SMat Martineau 5537279da61SMat Martineau if (skb && snd_data_fin_enable) 5549c29e361SPaolo Abeni mptcp_write_data_fin(subflow, skb, &opts->ext_copy); 555d22f4988SChristoph Paasch ret = true; 556d22f4988SChristoph Paasch } 557d22f4988SChristoph Paasch 5582398e399SPaolo Abeni /* passive sockets msk will set the 'can_ack' after accept(), even 5592398e399SPaolo Abeni * if the first subflow may have the already the remote key handy 5602398e399SPaolo Abeni */ 561d22f4988SChristoph Paasch opts->ext_copy.use_ack = 0; 562dc093db5SPaolo Abeni if (!READ_ONCE(msk->can_ack)) { 563d22f4988SChristoph Paasch *size = ALIGN(dss_size, 4); 564d22f4988SChristoph Paasch return ret; 5656d0060f6SMat Martineau } 5666d0060f6SMat Martineau 567e3859603SPaolo Abeni ack_seq = READ_ONCE(msk->ack_seq); 56837198e93SDavide Caratti if (READ_ONCE(msk->use_64bit_ack)) { 5696d0060f6SMat Martineau ack_size = TCPOLEN_MPTCP_DSS_ACK64; 570e3859603SPaolo Abeni opts->ext_copy.data_ack = ack_seq; 571a0c1d0eaSChristoph Paasch opts->ext_copy.ack64 = 1; 572a0c1d0eaSChristoph Paasch } else { 573a0c1d0eaSChristoph Paasch ack_size = TCPOLEN_MPTCP_DSS_ACK32; 574e3859603SPaolo Abeni opts->ext_copy.data_ack32 = (uint32_t)ack_seq; 575a0c1d0eaSChristoph Paasch opts->ext_copy.ack64 = 0; 576a0c1d0eaSChristoph Paasch } 577a0c1d0eaSChristoph Paasch opts->ext_copy.use_ack = 1; 578ea4ca586SPaolo Abeni WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk)); 5796d0060f6SMat Martineau 5806d0060f6SMat Martineau /* Add kind/length/subtype/flag overhead if mapping is not populated */ 5816d0060f6SMat Martineau if (dss_size == 0) 5826d0060f6SMat Martineau ack_size += TCPOLEN_MPTCP_DSS_BASE; 5836d0060f6SMat Martineau 5846d0060f6SMat Martineau dss_size += ack_size; 5856d0060f6SMat Martineau 5866d0060f6SMat Martineau *size = ALIGN(dss_size, 4); 5876d0060f6SMat Martineau return true; 5886d0060f6SMat Martineau } 5896d0060f6SMat Martineau 590761c124eSGeliang Tang static u64 add_addr_generate_hmac(u64 key1, u64 key2, 591761c124eSGeliang Tang struct mptcp_addr_info *addr) 5923df523abSPeter Krystad { 593761c124eSGeliang Tang u16 port = ntohs(addr->port); 594bd697222STodd Malsbary u8 hmac[SHA256_DIGEST_SIZE]; 5953df523abSPeter Krystad u8 msg[19]; 596761c124eSGeliang Tang int i = 0; 5973df523abSPeter Krystad 598761c124eSGeliang Tang msg[i++] = addr->id; 599761c124eSGeliang Tang if (addr->family == AF_INET) { 600761c124eSGeliang Tang memcpy(&msg[i], &addr->addr.s_addr, 4); 601761c124eSGeliang Tang i += 4; 602761c124eSGeliang Tang } 603761c124eSGeliang Tang #if IS_ENABLED(CONFIG_MPTCP_IPV6) 604761c124eSGeliang Tang else if (addr->family == AF_INET6) { 605761c124eSGeliang Tang memcpy(&msg[i], &addr->addr6.s6_addr, 16); 606761c124eSGeliang Tang i += 16; 607761c124eSGeliang Tang } 608761c124eSGeliang Tang #endif 609761c124eSGeliang Tang msg[i++] = port >> 8; 610761c124eSGeliang Tang msg[i++] = port & 0xFF; 6113df523abSPeter Krystad 612761c124eSGeliang Tang mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac); 6133df523abSPeter Krystad 614bd697222STodd Malsbary return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]); 6153df523abSPeter Krystad } 6163df523abSPeter Krystad 61784dfe367SGeliang Tang static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb, 6183df523abSPeter Krystad unsigned int *size, 6193df523abSPeter Krystad unsigned int remaining, 6203df523abSPeter Krystad struct mptcp_out_options *opts) 6213df523abSPeter Krystad { 6223df523abSPeter Krystad struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 6233df523abSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 62484dfe367SGeliang Tang bool drop_other_suboptions = false; 62584dfe367SGeliang Tang unsigned int opt_size = *size; 6266a6c05a8SGeliang Tang bool echo; 6274a2777a8SGeliang Tang bool port; 6281b1c7a0eSPeter Krystad int len; 6293df523abSPeter Krystad 630fbe0f87aSGeliang Tang if ((mptcp_pm_should_add_signal_ipv6(msk) || 63107f8252fSDavide Caratti mptcp_pm_should_add_signal_port(msk) || 63207f8252fSDavide Caratti mptcp_pm_should_add_signal_echo(msk)) && 63384dfe367SGeliang Tang skb && skb_is_tcp_pure_ack(skb)) { 63484dfe367SGeliang Tang pr_debug("drop other suboptions"); 63584dfe367SGeliang Tang opts->suboptions = 0; 6363ae32c07SGeliang Tang opts->ext_copy.use_ack = 0; 6373ae32c07SGeliang Tang opts->ext_copy.use_map = 0; 63884dfe367SGeliang Tang remaining += opt_size; 63984dfe367SGeliang Tang drop_other_suboptions = true; 64084dfe367SGeliang Tang } 64184dfe367SGeliang Tang 642f643b803SGeliang Tang if (!mptcp_pm_should_add_signal(msk) || 64330f60baeSGeliang Tang !(mptcp_pm_add_addr_signal(msk, remaining, &opts->addr, &echo, &port))) 6443df523abSPeter Krystad return false; 6451b1c7a0eSPeter Krystad 64630f60baeSGeliang Tang len = mptcp_add_addr_len(opts->addr.family, echo, port); 6471b1c7a0eSPeter Krystad if (remaining < len) 6481b1c7a0eSPeter Krystad return false; 6491b1c7a0eSPeter Krystad 6501b1c7a0eSPeter Krystad *size = len; 65184dfe367SGeliang Tang if (drop_other_suboptions) 65284dfe367SGeliang Tang *size -= opt_size; 6533df523abSPeter Krystad opts->suboptions |= OPTION_MPTCP_ADD_ADDR; 6546a6c05a8SGeliang Tang if (!echo) { 6553df523abSPeter Krystad opts->ahmac = add_addr_generate_hmac(msk->local_key, 6563df523abSPeter Krystad msk->remote_key, 657761c124eSGeliang Tang &opts->addr); 6583df523abSPeter Krystad } 6594a2777a8SGeliang Tang pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d", 66030f60baeSGeliang Tang opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port)); 6613df523abSPeter Krystad 6623df523abSPeter Krystad return true; 6633df523abSPeter Krystad } 6643df523abSPeter Krystad 6655cb104aeSGeliang Tang static bool mptcp_established_options_rm_addr(struct sock *sk, 6665cb104aeSGeliang Tang unsigned int *size, 6675cb104aeSGeliang Tang unsigned int remaining, 6685cb104aeSGeliang Tang struct mptcp_out_options *opts) 6695cb104aeSGeliang Tang { 6705cb104aeSGeliang Tang struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 6715cb104aeSGeliang Tang struct mptcp_sock *msk = mptcp_sk(subflow->conn); 6726445e17aSGeliang Tang struct mptcp_rm_list rm_list; 6736445e17aSGeliang Tang int i, len; 6745cb104aeSGeliang Tang 6755cb104aeSGeliang Tang if (!mptcp_pm_should_rm_signal(msk) || 6766445e17aSGeliang Tang !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list))) 6775cb104aeSGeliang Tang return false; 6785cb104aeSGeliang Tang 6796445e17aSGeliang Tang len = mptcp_rm_addr_len(&rm_list); 6806445e17aSGeliang Tang if (len < 0) 6816445e17aSGeliang Tang return false; 6826445e17aSGeliang Tang if (remaining < len) 6835cb104aeSGeliang Tang return false; 6845cb104aeSGeliang Tang 6856445e17aSGeliang Tang *size = len; 6865cb104aeSGeliang Tang opts->suboptions |= OPTION_MPTCP_RM_ADDR; 6876445e17aSGeliang Tang opts->rm_list = rm_list; 6885cb104aeSGeliang Tang 6896445e17aSGeliang Tang for (i = 0; i < opts->rm_list.nr; i++) 6906445e17aSGeliang Tang pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]); 6915cb104aeSGeliang Tang 6925cb104aeSGeliang Tang return true; 6935cb104aeSGeliang Tang } 6945cb104aeSGeliang Tang 69506706542SGeliang Tang static bool mptcp_established_options_mp_prio(struct sock *sk, 69606706542SGeliang Tang unsigned int *size, 69706706542SGeliang Tang unsigned int remaining, 69806706542SGeliang Tang struct mptcp_out_options *opts) 69906706542SGeliang Tang { 70006706542SGeliang Tang struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 70106706542SGeliang Tang 70206706542SGeliang Tang if (!subflow->send_mp_prio) 70306706542SGeliang Tang return false; 70406706542SGeliang Tang 705ec99a470SDavide Caratti /* account for the trailing 'nop' option */ 706ec99a470SDavide Caratti if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN) 70706706542SGeliang Tang return false; 70806706542SGeliang Tang 709ec99a470SDavide Caratti *size = TCPOLEN_MPTCP_PRIO_ALIGN; 71006706542SGeliang Tang opts->suboptions |= OPTION_MPTCP_PRIO; 71106706542SGeliang Tang opts->backup = subflow->request_bkup; 71206706542SGeliang Tang 71306706542SGeliang Tang pr_debug("prio=%d", opts->backup); 71406706542SGeliang Tang 71506706542SGeliang Tang return true; 71606706542SGeliang Tang } 71706706542SGeliang Tang 718dc87efdbSFlorian Westphal static noinline void mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb, 719dc87efdbSFlorian Westphal unsigned int *size, 720dc87efdbSFlorian Westphal unsigned int remaining, 721dc87efdbSFlorian Westphal struct mptcp_out_options *opts) 722dc87efdbSFlorian Westphal { 723dc87efdbSFlorian Westphal const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 724dc87efdbSFlorian Westphal 725dc87efdbSFlorian Westphal if (remaining < TCPOLEN_MPTCP_RST) 726dc87efdbSFlorian Westphal return; 727dc87efdbSFlorian Westphal 728dc87efdbSFlorian Westphal *size = TCPOLEN_MPTCP_RST; 729dc87efdbSFlorian Westphal opts->suboptions |= OPTION_MPTCP_RST; 730dc87efdbSFlorian Westphal opts->reset_transient = subflow->reset_transient; 731dc87efdbSFlorian Westphal opts->reset_reason = subflow->reset_reason; 732dc87efdbSFlorian Westphal } 733dc87efdbSFlorian Westphal 7346d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb, 7356d0060f6SMat Martineau unsigned int *size, unsigned int remaining, 7366d0060f6SMat Martineau struct mptcp_out_options *opts) 7376d0060f6SMat Martineau { 738d87903b6SPaolo Abeni struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 739d87903b6SPaolo Abeni struct mptcp_sock *msk = mptcp_sk(subflow->conn); 7406d0060f6SMat Martineau unsigned int opt_size = 0; 741d87903b6SPaolo Abeni bool snd_data_fin; 7426d0060f6SMat Martineau bool ret = false; 7436d0060f6SMat Martineau 7443df523abSPeter Krystad opts->suboptions = 0; 7453df523abSPeter Krystad 746d87903b6SPaolo Abeni if (unlikely(__mptcp_check_fallback(msk))) 747e1ff9e82SDavide Caratti return false; 748e1ff9e82SDavide Caratti 749dc87efdbSFlorian Westphal if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) { 750dc87efdbSFlorian Westphal mptcp_established_options_rst(sk, skb, size, remaining, opts); 751dc87efdbSFlorian Westphal return true; 752dc87efdbSFlorian Westphal } 753d5824847SPaolo Abeni 754d87903b6SPaolo Abeni snd_data_fin = mptcp_data_fin_enabled(msk); 755d87903b6SPaolo Abeni if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts)) 7566d0060f6SMat Martineau ret = true; 757d87903b6SPaolo Abeni else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) 7586d0060f6SMat Martineau ret = true; 7596d0060f6SMat Martineau 7606d0060f6SMat Martineau /* we reserved enough space for the above options, and exceeding the 7616d0060f6SMat Martineau * TCP option space would be fatal 7626d0060f6SMat Martineau */ 7636d0060f6SMat Martineau if (WARN_ON_ONCE(opt_size > remaining)) 7646d0060f6SMat Martineau return false; 7656d0060f6SMat Martineau 7666d0060f6SMat Martineau *size += opt_size; 7676d0060f6SMat Martineau remaining -= opt_size; 76884dfe367SGeliang Tang if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) { 7693df523abSPeter Krystad *size += opt_size; 7703df523abSPeter Krystad remaining -= opt_size; 7713df523abSPeter Krystad ret = true; 7725cb104aeSGeliang Tang } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) { 7735cb104aeSGeliang Tang *size += opt_size; 7745cb104aeSGeliang Tang remaining -= opt_size; 7755cb104aeSGeliang Tang ret = true; 7763df523abSPeter Krystad } 7776d0060f6SMat Martineau 77806706542SGeliang Tang if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) { 77906706542SGeliang Tang *size += opt_size; 78006706542SGeliang Tang remaining -= opt_size; 78106706542SGeliang Tang ret = true; 78206706542SGeliang Tang } 78306706542SGeliang Tang 7846d0060f6SMat Martineau return ret; 7856d0060f6SMat Martineau } 7866d0060f6SMat Martineau 787cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size, 788cec37a6eSPeter Krystad struct mptcp_out_options *opts) 789cec37a6eSPeter Krystad { 790cec37a6eSPeter Krystad struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); 791cec37a6eSPeter Krystad 792cec37a6eSPeter Krystad if (subflow_req->mp_capable) { 793cec37a6eSPeter Krystad opts->suboptions = OPTION_MPTCP_MPC_SYNACK; 794cec37a6eSPeter Krystad opts->sndr_key = subflow_req->local_key; 795*06fe1719SGeliang Tang opts->csum_reqd = subflow_req->csum_reqd; 796cec37a6eSPeter Krystad *size = TCPOLEN_MPTCP_MPC_SYNACK; 797cec37a6eSPeter Krystad pr_debug("subflow_req=%p, local_key=%llu", 798cec37a6eSPeter Krystad subflow_req, subflow_req->local_key); 799cec37a6eSPeter Krystad return true; 800f296234cSPeter Krystad } else if (subflow_req->mp_join) { 801f296234cSPeter Krystad opts->suboptions = OPTION_MPTCP_MPJ_SYNACK; 802f296234cSPeter Krystad opts->backup = subflow_req->backup; 803f296234cSPeter Krystad opts->join_id = subflow_req->local_id; 804f296234cSPeter Krystad opts->thmac = subflow_req->thmac; 805f296234cSPeter Krystad opts->nonce = subflow_req->local_nonce; 806f296234cSPeter Krystad pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u", 807f296234cSPeter Krystad subflow_req, opts->backup, opts->join_id, 808f296234cSPeter Krystad opts->thmac, opts->nonce); 809f296234cSPeter Krystad *size = TCPOLEN_MPTCP_MPJ_SYNACK; 810f296234cSPeter Krystad return true; 811cec37a6eSPeter Krystad } 812cec37a6eSPeter Krystad return false; 813cec37a6eSPeter Krystad } 814cec37a6eSPeter Krystad 815d5824847SPaolo Abeni static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk, 816f296234cSPeter Krystad struct mptcp_subflow_context *subflow, 817d22f4988SChristoph Paasch struct sk_buff *skb, 818d22f4988SChristoph Paasch struct mptcp_options_received *mp_opt) 819d22f4988SChristoph Paasch { 820d22f4988SChristoph Paasch /* here we can process OoO, in-window pkts, only in-sequence 4th ack 821f296234cSPeter Krystad * will make the subflow fully established 822d22f4988SChristoph Paasch */ 823f296234cSPeter Krystad if (likely(subflow->fully_established)) { 824f296234cSPeter Krystad /* on passive sockets, check for 3rd ack retransmission 825f296234cSPeter Krystad * note that msk is always set by subflow_syn_recv_sock() 826f296234cSPeter Krystad * for mp_join subflows 827f296234cSPeter Krystad */ 828f296234cSPeter Krystad if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 && 829f296234cSPeter Krystad TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq && 830f296234cSPeter Krystad subflow->mp_join && mp_opt->mp_join && 831f296234cSPeter Krystad READ_ONCE(msk->pm.server_side)) 832d5824847SPaolo Abeni tcp_send_ack(ssk); 833f296234cSPeter Krystad goto fully_established; 834f296234cSPeter Krystad } 835d22f4988SChristoph Paasch 836d5824847SPaolo Abeni /* we must process OoO packets before the first subflow is fully 837d5824847SPaolo Abeni * established. OoO packets are instead a protocol violation 838d5824847SPaolo Abeni * for MP_JOIN subflows as the peer must not send any data 839d5824847SPaolo Abeni * before receiving the forth ack - cfr. RFC 8684 section 3.2. 840f296234cSPeter Krystad */ 841d5824847SPaolo Abeni if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) { 842d5824847SPaolo Abeni if (subflow->mp_join) 843d5824847SPaolo Abeni goto reset; 844f296234cSPeter Krystad return subflow->mp_capable; 845d5824847SPaolo Abeni } 846f296234cSPeter Krystad 8475a91e32bSPaolo Abeni if (mp_opt->dss && mp_opt->use_ack) { 848f296234cSPeter Krystad /* subflows are fully established as soon as we get any 849f296234cSPeter Krystad * additional ack. 850f296234cSPeter Krystad */ 8510be534f5SPaolo Abeni subflow->fully_established = 1; 852b93df08cSPaolo Abeni WRITE_ONCE(msk->fully_established, true); 853f296234cSPeter Krystad goto fully_established; 854f296234cSPeter Krystad } 855d22f4988SChristoph Paasch 85684dfe367SGeliang Tang if (mp_opt->add_addr) { 85784dfe367SGeliang Tang WRITE_ONCE(msk->fully_established, true); 85884dfe367SGeliang Tang return true; 85984dfe367SGeliang Tang } 86084dfe367SGeliang Tang 861d22f4988SChristoph Paasch /* If the first established packet does not contain MP_CAPABLE + data 862d5824847SPaolo Abeni * then fallback to TCP. Fallback scenarios requires a reset for 863d5824847SPaolo Abeni * MP_JOIN subflows. 864d22f4988SChristoph Paasch */ 865d22f4988SChristoph Paasch if (!mp_opt->mp_capable) { 866d5824847SPaolo Abeni if (subflow->mp_join) 867d5824847SPaolo Abeni goto reset; 868d22f4988SChristoph Paasch subflow->mp_capable = 0; 869e1ff9e82SDavide Caratti pr_fallback(msk); 870e1ff9e82SDavide Caratti __mptcp_do_fallback(msk); 871d22f4988SChristoph Paasch return false; 872d22f4988SChristoph Paasch } 873f296234cSPeter Krystad 874d6085fe1SPaolo Abeni if (unlikely(!READ_ONCE(msk->pm.server_side))) 875d6085fe1SPaolo Abeni pr_warn_once("bogus mpc option on established client sk"); 876b93df08cSPaolo Abeni mptcp_subflow_fully_established(subflow, mp_opt); 877f296234cSPeter Krystad 878f296234cSPeter Krystad fully_established: 8795b950ff4SPaolo Abeni /* if the subflow is not already linked into the conn_list, we can't 8805b950ff4SPaolo Abeni * notify the PM: this subflow is still on the listener queue 8815b950ff4SPaolo Abeni * and the PM possibly acquiring the subflow lock could race with 8825b950ff4SPaolo Abeni * the listener close 8835b950ff4SPaolo Abeni */ 8845b950ff4SPaolo Abeni if (likely(subflow->pm_notified) || list_empty(&subflow->node)) 885f296234cSPeter Krystad return true; 886f296234cSPeter Krystad 887f296234cSPeter Krystad subflow->pm_notified = 1; 888ec3edaa7SPeter Krystad if (subflow->mp_join) { 889d5824847SPaolo Abeni clear_3rdack_retransmission(ssk); 89062535200SGeliang Tang mptcp_pm_subflow_established(msk); 891ec3edaa7SPeter Krystad } else { 8926c714f1bSFlorian Westphal mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC); 893ec3edaa7SPeter Krystad } 894d22f4988SChristoph Paasch return true; 895d5824847SPaolo Abeni 896d5824847SPaolo Abeni reset: 897d5824847SPaolo Abeni mptcp_subflow_reset(ssk); 898d5824847SPaolo Abeni return false; 899d22f4988SChristoph Paasch } 900d22f4988SChristoph Paasch 901cc9d2566SPaolo Abeni static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit) 902cc9d2566SPaolo Abeni { 903cc9d2566SPaolo Abeni u32 old_ack32, cur_ack32; 904cc9d2566SPaolo Abeni 905cc9d2566SPaolo Abeni if (use_64bit) 906cc9d2566SPaolo Abeni return cur_ack; 907cc9d2566SPaolo Abeni 908cc9d2566SPaolo Abeni old_ack32 = (u32)old_ack; 909cc9d2566SPaolo Abeni cur_ack32 = (u32)cur_ack; 910cc9d2566SPaolo Abeni cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32; 911cc9d2566SPaolo Abeni if (unlikely(before(cur_ack32, old_ack32))) 912cc9d2566SPaolo Abeni return cur_ack + (1LL << 32); 913cc9d2566SPaolo Abeni return cur_ack; 914cc9d2566SPaolo Abeni } 915cc9d2566SPaolo Abeni 9166f8a612aSFlorian Westphal static void ack_update_msk(struct mptcp_sock *msk, 9176e628cd3SPaolo Abeni struct sock *ssk, 918cc9d2566SPaolo Abeni struct mptcp_options_received *mp_opt) 919cc9d2566SPaolo Abeni { 9207439d687SPaolo Abeni u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt); 9216f8a612aSFlorian Westphal struct sock *sk = (struct sock *)msk; 9227439d687SPaolo Abeni u64 old_snd_una; 9237439d687SPaolo Abeni 9247439d687SPaolo Abeni mptcp_data_lock(sk); 925cc9d2566SPaolo Abeni 926cc9d2566SPaolo Abeni /* avoid ack expansion on update conflict, to reduce the risk of 927cc9d2566SPaolo Abeni * wrongly expanding to a future ack sequence number, which is way 928cc9d2566SPaolo Abeni * more dangerous than missing an ack 929cc9d2566SPaolo Abeni */ 9307439d687SPaolo Abeni old_snd_una = msk->snd_una; 931cc9d2566SPaolo Abeni new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64); 932cc9d2566SPaolo Abeni 933cc9d2566SPaolo Abeni /* ACK for data not even sent yet? Ignore. */ 934eaa2ffabSPaolo Abeni if (after64(new_snd_una, snd_nxt)) 935cc9d2566SPaolo Abeni new_snd_una = old_snd_una; 936cc9d2566SPaolo Abeni 9376f8a612aSFlorian Westphal new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd; 9386f8a612aSFlorian Westphal 939219d0499SPaolo Abeni if (after64(new_wnd_end, msk->wnd_end)) 9407439d687SPaolo Abeni msk->wnd_end = new_wnd_end; 941219d0499SPaolo Abeni 942219d0499SPaolo Abeni /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */ 943d09d818eSPaolo Abeni if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt))) 944219d0499SPaolo Abeni __mptcp_check_push(sk, ssk); 9456f8a612aSFlorian Westphal 9467439d687SPaolo Abeni if (after64(new_snd_una, old_snd_una)) { 9477439d687SPaolo Abeni msk->snd_una = new_snd_una; 9487439d687SPaolo Abeni __mptcp_data_acked(sk); 949cc9d2566SPaolo Abeni } 9507439d687SPaolo Abeni mptcp_data_unlock(sk); 951ed66bfb4SGeliang Tang 952ed66bfb4SGeliang Tang trace_ack_update_msk(mp_opt->data_ack, 953ed66bfb4SGeliang Tang old_snd_una, new_snd_una, 954ed66bfb4SGeliang Tang new_wnd_end, msk->wnd_end); 955b51f9b80SPaolo Abeni } 956cc9d2566SPaolo Abeni 9571a49b2c2SMat Martineau bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit) 9583721b9b6SMat Martineau { 9593721b9b6SMat Martineau /* Skip if DATA_FIN was already received. 9603721b9b6SMat Martineau * If updating simultaneously with the recvmsg loop, values 9613721b9b6SMat Martineau * should match. If they mismatch, the peer is misbehaving and 9623721b9b6SMat Martineau * we will prefer the most recent information. 9633721b9b6SMat Martineau */ 964781bf13dSPaolo Abeni if (READ_ONCE(msk->rcv_data_fin)) 9653721b9b6SMat Martineau return false; 9663721b9b6SMat Martineau 9671a49b2c2SMat Martineau WRITE_ONCE(msk->rcv_data_fin_seq, 9681a49b2c2SMat Martineau expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit)); 9693721b9b6SMat Martineau WRITE_ONCE(msk->rcv_data_fin, 1); 9703721b9b6SMat Martineau 9713721b9b6SMat Martineau return true; 9723721b9b6SMat Martineau } 9733721b9b6SMat Martineau 9741b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk, 9751b1c7a0eSPeter Krystad struct mptcp_options_received *mp_opt) 9761b1c7a0eSPeter Krystad { 9771b1c7a0eSPeter Krystad u64 hmac = 0; 9781b1c7a0eSPeter Krystad 9791b1c7a0eSPeter Krystad if (mp_opt->echo) 9801b1c7a0eSPeter Krystad return true; 9811b1c7a0eSPeter Krystad 9821b1c7a0eSPeter Krystad hmac = add_addr_generate_hmac(msk->remote_key, 9831b1c7a0eSPeter Krystad msk->local_key, 984761c124eSGeliang Tang &mp_opt->addr); 9851b1c7a0eSPeter Krystad 9861b1c7a0eSPeter Krystad pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n", 9871b1c7a0eSPeter Krystad msk, (unsigned long long)hmac, 9881b1c7a0eSPeter Krystad (unsigned long long)mp_opt->ahmac); 9891b1c7a0eSPeter Krystad 9901b1c7a0eSPeter Krystad return hmac == mp_opt->ahmac; 9911b1c7a0eSPeter Krystad } 9921b1c7a0eSPeter Krystad 99377d0cab9SFlorian Westphal void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb) 994648ef4b8SMat Martineau { 995d22f4988SChristoph Paasch struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 9961b1c7a0eSPeter Krystad struct mptcp_sock *msk = mptcp_sk(subflow->conn); 997cfde141eSPaolo Abeni struct mptcp_options_received mp_opt; 998648ef4b8SMat Martineau struct mptcp_ext *mpext; 999648ef4b8SMat Martineau 10006e628cd3SPaolo Abeni if (__mptcp_check_fallback(msk)) { 10016e628cd3SPaolo Abeni /* Keep it simple and unconditionally trigger send data cleanup and 10026e628cd3SPaolo Abeni * pending queue spooling. We will need to acquire the data lock 10036e628cd3SPaolo Abeni * for more accurate checks, and once the lock is acquired, such 10046e628cd3SPaolo Abeni * helpers are cheap. 10056e628cd3SPaolo Abeni */ 10066e628cd3SPaolo Abeni mptcp_data_lock(subflow->conn); 1007219d0499SPaolo Abeni if (sk_stream_memory_free(sk)) 1008219d0499SPaolo Abeni __mptcp_check_push(subflow->conn, sk); 10096e628cd3SPaolo Abeni __mptcp_data_acked(subflow->conn); 10106e628cd3SPaolo Abeni mptcp_data_unlock(subflow->conn); 1011e1ff9e82SDavide Caratti return; 10126e628cd3SPaolo Abeni } 1013e1ff9e82SDavide Caratti 1014cfde141eSPaolo Abeni mptcp_get_options(skb, &mp_opt); 1015cfde141eSPaolo Abeni if (!check_fully_established(msk, sk, subflow, skb, &mp_opt)) 1016d22f4988SChristoph Paasch return; 1017648ef4b8SMat Martineau 101850c504a2SFlorian Westphal if (mp_opt.fastclose && 101950c504a2SFlorian Westphal msk->local_key == mp_opt.rcvr_key) { 102050c504a2SFlorian Westphal WRITE_ONCE(msk->rcv_fastclose, true); 102150c504a2SFlorian Westphal mptcp_schedule_work((struct sock *)msk); 102250c504a2SFlorian Westphal } 102350c504a2SFlorian Westphal 1024cfde141eSPaolo Abeni if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) { 1025a877de06SGeliang Tang if (!mp_opt.echo) { 1026f7dafee1SGeliang Tang mptcp_pm_add_addr_received(msk, &mp_opt.addr); 1027a877de06SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR); 1028a877de06SGeliang Tang } else { 1029f7dafee1SGeliang Tang mptcp_pm_add_addr_echoed(msk, &mp_opt.addr); 1030d58300c3SDavide Caratti mptcp_pm_del_add_timer(msk, &mp_opt.addr, true); 1031a877de06SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD); 1032a877de06SGeliang Tang } 10332fbdd9eaSGeliang Tang 1034f7dafee1SGeliang Tang if (mp_opt.addr.port) 10352fbdd9eaSGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD); 10362fbdd9eaSGeliang Tang 1037cfde141eSPaolo Abeni mp_opt.add_addr = 0; 10381b1c7a0eSPeter Krystad } 10391b1c7a0eSPeter Krystad 1040d0876b22SGeliang Tang if (mp_opt.rm_addr) { 10415c4a824dSGeliang Tang mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list); 1042d0876b22SGeliang Tang mp_opt.rm_addr = 0; 1043d0876b22SGeliang Tang } 1044d0876b22SGeliang Tang 104540453a5cSGeliang Tang if (mp_opt.mp_prio) { 104640453a5cSGeliang Tang mptcp_pm_mp_prio_received(sk, mp_opt.backup); 10470be2ac28SGeliang Tang MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX); 104840453a5cSGeliang Tang mp_opt.mp_prio = 0; 104940453a5cSGeliang Tang } 105040453a5cSGeliang Tang 1051dc87efdbSFlorian Westphal if (mp_opt.reset) { 1052dc87efdbSFlorian Westphal subflow->reset_seen = 1; 1053dc87efdbSFlorian Westphal subflow->reset_reason = mp_opt.reset_reason; 1054dc87efdbSFlorian Westphal subflow->reset_transient = mp_opt.reset_transient; 1055dc87efdbSFlorian Westphal } 1056dc87efdbSFlorian Westphal 1057cfde141eSPaolo Abeni if (!mp_opt.dss) 1058648ef4b8SMat Martineau return; 1059648ef4b8SMat Martineau 1060cc9d2566SPaolo Abeni /* we can't wait for recvmsg() to update the ack_seq, otherwise 1061cc9d2566SPaolo Abeni * monodirectional flows will stuck 1062cc9d2566SPaolo Abeni */ 1063cfde141eSPaolo Abeni if (mp_opt.use_ack) 10646f8a612aSFlorian Westphal ack_update_msk(msk, sk, &mp_opt); 1065cc9d2566SPaolo Abeni 106606827b34SMat Martineau /* Zero-data-length packets are dropped by the caller and not 106706827b34SMat Martineau * propagated to the MPTCP layer, so the skb extension does not 106806827b34SMat Martineau * need to be allocated or populated. DATA_FIN information, if 106906827b34SMat Martineau * present, needs to be updated here before the skb is freed. 107043b54c6eSMat Martineau */ 107143b54c6eSMat Martineau if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) { 107243b54c6eSMat Martineau if (mp_opt.data_fin && mp_opt.data_len == 1 && 10731a49b2c2SMat Martineau mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) && 107443b54c6eSMat Martineau schedule_work(&msk->work)) 107543b54c6eSMat Martineau sock_hold(subflow->conn); 107606827b34SMat Martineau 107706827b34SMat Martineau return; 107843b54c6eSMat Martineau } 107943b54c6eSMat Martineau 1080648ef4b8SMat Martineau mpext = skb_ext_add(skb, SKB_EXT_MPTCP); 1081648ef4b8SMat Martineau if (!mpext) 1082648ef4b8SMat Martineau return; 1083648ef4b8SMat Martineau 1084648ef4b8SMat Martineau memset(mpext, 0, sizeof(*mpext)); 1085648ef4b8SMat Martineau 1086cfde141eSPaolo Abeni if (mp_opt.use_map) { 1087cfde141eSPaolo Abeni if (mp_opt.mpc_map) { 1088cc7972eaSChristoph Paasch /* this is an MP_CAPABLE carrying MPTCP data 1089cc7972eaSChristoph Paasch * we know this map the first chunk of data 1090cc7972eaSChristoph Paasch */ 1091cc7972eaSChristoph Paasch mptcp_crypto_key_sha(subflow->remote_key, NULL, 1092cc7972eaSChristoph Paasch &mpext->data_seq); 1093cc7972eaSChristoph Paasch mpext->data_seq++; 1094cc7972eaSChristoph Paasch mpext->subflow_seq = 1; 1095cc7972eaSChristoph Paasch mpext->dsn64 = 1; 1096cc7972eaSChristoph Paasch mpext->mpc_map = 1; 1097a77895dbSPaolo Abeni mpext->data_fin = 0; 1098cc7972eaSChristoph Paasch } else { 1099cfde141eSPaolo Abeni mpext->data_seq = mp_opt.data_seq; 1100cfde141eSPaolo Abeni mpext->subflow_seq = mp_opt.subflow_seq; 1101cfde141eSPaolo Abeni mpext->dsn64 = mp_opt.dsn64; 1102cfde141eSPaolo Abeni mpext->data_fin = mp_opt.data_fin; 1103cc7972eaSChristoph Paasch } 1104cfde141eSPaolo Abeni mpext->data_len = mp_opt.data_len; 1105648ef4b8SMat Martineau mpext->use_map = 1; 1106648ef4b8SMat Martineau } 1107648ef4b8SMat Martineau } 1108648ef4b8SMat Martineau 1109fa3fe2b1SFlorian Westphal static void mptcp_set_rwin(const struct tcp_sock *tp) 1110fa3fe2b1SFlorian Westphal { 1111fa3fe2b1SFlorian Westphal const struct sock *ssk = (const struct sock *)tp; 1112fa3fe2b1SFlorian Westphal const struct mptcp_subflow_context *subflow; 1113fa3fe2b1SFlorian Westphal struct mptcp_sock *msk; 1114fa3fe2b1SFlorian Westphal u64 ack_seq; 1115fa3fe2b1SFlorian Westphal 1116fa3fe2b1SFlorian Westphal subflow = mptcp_subflow_ctx(ssk); 1117fa3fe2b1SFlorian Westphal msk = mptcp_sk(subflow->conn); 1118fa3fe2b1SFlorian Westphal 1119fa3fe2b1SFlorian Westphal ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd; 1120fa3fe2b1SFlorian Westphal 1121fa3fe2b1SFlorian Westphal if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent))) 1122fa3fe2b1SFlorian Westphal WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); 1123fa3fe2b1SFlorian Westphal } 1124fa3fe2b1SFlorian Westphal 1125fa3fe2b1SFlorian Westphal void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp, 1126fa3fe2b1SFlorian Westphal struct mptcp_out_options *opts) 1127eda7acddSPeter Krystad { 1128cc7972eaSChristoph Paasch if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK | 1129eda7acddSPeter Krystad OPTION_MPTCP_MPC_ACK) & opts->suboptions) { 1130*06fe1719SGeliang Tang u8 len, flag = MPTCP_CAP_HMAC_SHA256; 1131eda7acddSPeter Krystad 1132eda7acddSPeter Krystad if (OPTION_MPTCP_MPC_SYN & opts->suboptions) 1133eda7acddSPeter Krystad len = TCPOLEN_MPTCP_MPC_SYN; 1134cec37a6eSPeter Krystad else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) 1135cec37a6eSPeter Krystad len = TCPOLEN_MPTCP_MPC_SYNACK; 1136cc7972eaSChristoph Paasch else if (opts->ext_copy.data_len) 1137cc7972eaSChristoph Paasch len = TCPOLEN_MPTCP_MPC_ACK_DATA; 1138eda7acddSPeter Krystad else 1139eda7acddSPeter Krystad len = TCPOLEN_MPTCP_MPC_ACK; 1140eda7acddSPeter Krystad 1141*06fe1719SGeliang Tang if (opts->csum_reqd) 1142*06fe1719SGeliang Tang flag |= MPTCP_CAP_CHECKSUM_REQD; 1143*06fe1719SGeliang Tang 11443df523abSPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len, 11453df523abSPeter Krystad MPTCP_SUPPORTED_VERSION, 1146*06fe1719SGeliang Tang flag); 1147cc7972eaSChristoph Paasch 1148cc7972eaSChristoph Paasch if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) & 1149cc7972eaSChristoph Paasch opts->suboptions)) 1150cc7972eaSChristoph Paasch goto mp_capable_done; 1151cc7972eaSChristoph Paasch 1152eda7acddSPeter Krystad put_unaligned_be64(opts->sndr_key, ptr); 1153eda7acddSPeter Krystad ptr += 2; 1154cc7972eaSChristoph Paasch if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions)) 1155cc7972eaSChristoph Paasch goto mp_capable_done; 1156cc7972eaSChristoph Paasch 1157eda7acddSPeter Krystad put_unaligned_be64(opts->rcvr_key, ptr); 1158eda7acddSPeter Krystad ptr += 2; 1159cc7972eaSChristoph Paasch if (!opts->ext_copy.data_len) 1160cc7972eaSChristoph Paasch goto mp_capable_done; 1161cc7972eaSChristoph Paasch 1162cc7972eaSChristoph Paasch put_unaligned_be32(opts->ext_copy.data_len << 16 | 1163cc7972eaSChristoph Paasch TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); 1164cc7972eaSChristoph Paasch ptr += 1; 1165eda7acddSPeter Krystad } 11666d0060f6SMat Martineau 1167cc7972eaSChristoph Paasch mp_capable_done: 1168fef6b7ecSGeliang Tang if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) { 11696eb3d1e3SGeliang Tang u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE; 11706eb3d1e3SGeliang Tang u8 echo = MPTCP_ADDR_ECHO; 11713df523abSPeter Krystad 11723df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1173fef6b7ecSGeliang Tang if (opts->addr.family == AF_INET6) 1174e1ef6832SGeliang Tang len = TCPOLEN_MPTCP_ADD_ADDR6_BASE; 1175e1ef6832SGeliang Tang #endif 1176e1ef6832SGeliang Tang 117730f60baeSGeliang Tang if (opts->addr.port) 117822fb85ffSGeliang Tang len += TCPOLEN_MPTCP_PORT_LEN; 117922fb85ffSGeliang Tang 11806eb3d1e3SGeliang Tang if (opts->ahmac) { 11816eb3d1e3SGeliang Tang len += sizeof(opts->ahmac); 11826eb3d1e3SGeliang Tang echo = 0; 11836eb3d1e3SGeliang Tang } 11846eb3d1e3SGeliang Tang 11853df523abSPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR, 118630f60baeSGeliang Tang len, echo, opts->addr.id); 1187fef6b7ecSGeliang Tang if (opts->addr.family == AF_INET) { 118830f60baeSGeliang Tang memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4); 11893df523abSPeter Krystad ptr += 1; 11903df523abSPeter Krystad } 11913df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6) 1192fef6b7ecSGeliang Tang else if (opts->addr.family == AF_INET6) { 119330f60baeSGeliang Tang memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16); 11943df523abSPeter Krystad ptr += 4; 1195e1ef6832SGeliang Tang } 1196e1ef6832SGeliang Tang #endif 119722fb85ffSGeliang Tang 119830f60baeSGeliang Tang if (!opts->addr.port) { 11993df523abSPeter Krystad if (opts->ahmac) { 12003df523abSPeter Krystad put_unaligned_be64(opts->ahmac, ptr); 12013df523abSPeter Krystad ptr += 2; 12023df523abSPeter Krystad } 120322fb85ffSGeliang Tang } else { 120430f60baeSGeliang Tang u16 port = ntohs(opts->addr.port); 120530f60baeSGeliang Tang 120622fb85ffSGeliang Tang if (opts->ahmac) { 120722fb85ffSGeliang Tang u8 *bptr = (u8 *)ptr; 120822fb85ffSGeliang Tang 120930f60baeSGeliang Tang put_unaligned_be16(port, bptr); 121022fb85ffSGeliang Tang bptr += 2; 121122fb85ffSGeliang Tang put_unaligned_be64(opts->ahmac, bptr); 121222fb85ffSGeliang Tang bptr += 8; 121322fb85ffSGeliang Tang put_unaligned_be16(TCPOPT_NOP << 8 | 121422fb85ffSGeliang Tang TCPOPT_NOP, bptr); 121522fb85ffSGeliang Tang 121622fb85ffSGeliang Tang ptr += 3; 121722fb85ffSGeliang Tang } else { 121830f60baeSGeliang Tang put_unaligned_be32(port << 16 | 121922fb85ffSGeliang Tang TCPOPT_NOP << 8 | 122022fb85ffSGeliang Tang TCPOPT_NOP, ptr); 122122fb85ffSGeliang Tang ptr += 1; 12223df523abSPeter Krystad } 122322fb85ffSGeliang Tang } 12243df523abSPeter Krystad } 12253df523abSPeter Krystad 12263df523abSPeter Krystad if (OPTION_MPTCP_RM_ADDR & opts->suboptions) { 12276445e17aSGeliang Tang u8 i = 1; 12286445e17aSGeliang Tang 12293df523abSPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR, 12306445e17aSGeliang Tang TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr, 12316445e17aSGeliang Tang 0, opts->rm_list.ids[0]); 12326445e17aSGeliang Tang 12336445e17aSGeliang Tang while (i < opts->rm_list.nr) { 12346445e17aSGeliang Tang u8 id1, id2, id3, id4; 12356445e17aSGeliang Tang 12366445e17aSGeliang Tang id1 = opts->rm_list.ids[i]; 12376445e17aSGeliang Tang id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP; 12386445e17aSGeliang Tang id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP; 12396445e17aSGeliang Tang id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP; 12406445e17aSGeliang Tang put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr); 12416445e17aSGeliang Tang ptr += 1; 12426445e17aSGeliang Tang i += 4; 12436445e17aSGeliang Tang } 12443df523abSPeter Krystad } 12453df523abSPeter Krystad 124606706542SGeliang Tang if (OPTION_MPTCP_PRIO & opts->suboptions) { 124706706542SGeliang Tang const struct sock *ssk = (const struct sock *)tp; 124806706542SGeliang Tang struct mptcp_subflow_context *subflow; 124906706542SGeliang Tang 125006706542SGeliang Tang subflow = mptcp_subflow_ctx(ssk); 125106706542SGeliang Tang subflow->send_mp_prio = 0; 125206706542SGeliang Tang 125306706542SGeliang Tang *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO, 125406706542SGeliang Tang TCPOLEN_MPTCP_PRIO, 125506706542SGeliang Tang opts->backup, TCPOPT_NOP); 125606706542SGeliang Tang } 125706706542SGeliang Tang 1258ec3edaa7SPeter Krystad if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) { 1259ec3edaa7SPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 1260ec3edaa7SPeter Krystad TCPOLEN_MPTCP_MPJ_SYN, 1261ec3edaa7SPeter Krystad opts->backup, opts->join_id); 1262ec3edaa7SPeter Krystad put_unaligned_be32(opts->token, ptr); 1263ec3edaa7SPeter Krystad ptr += 1; 1264ec3edaa7SPeter Krystad put_unaligned_be32(opts->nonce, ptr); 1265ec3edaa7SPeter Krystad ptr += 1; 1266ec3edaa7SPeter Krystad } 1267ec3edaa7SPeter Krystad 1268f296234cSPeter Krystad if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) { 1269f296234cSPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 1270f296234cSPeter Krystad TCPOLEN_MPTCP_MPJ_SYNACK, 1271f296234cSPeter Krystad opts->backup, opts->join_id); 1272f296234cSPeter Krystad put_unaligned_be64(opts->thmac, ptr); 1273f296234cSPeter Krystad ptr += 2; 1274f296234cSPeter Krystad put_unaligned_be32(opts->nonce, ptr); 1275f296234cSPeter Krystad ptr += 1; 1276f296234cSPeter Krystad } 1277f296234cSPeter Krystad 1278ec3edaa7SPeter Krystad if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) { 1279ec3edaa7SPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, 1280ec3edaa7SPeter Krystad TCPOLEN_MPTCP_MPJ_ACK, 0, 0); 1281ec3edaa7SPeter Krystad memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN); 1282ec3edaa7SPeter Krystad ptr += 5; 1283ec3edaa7SPeter Krystad } 1284ec3edaa7SPeter Krystad 1285dc87efdbSFlorian Westphal if (OPTION_MPTCP_RST & opts->suboptions) 1286dc87efdbSFlorian Westphal *ptr++ = mptcp_option(MPTCPOPT_RST, 1287dc87efdbSFlorian Westphal TCPOLEN_MPTCP_RST, 1288dc87efdbSFlorian Westphal opts->reset_transient, 1289dc87efdbSFlorian Westphal opts->reset_reason); 1290dc87efdbSFlorian Westphal 12916d0060f6SMat Martineau if (opts->ext_copy.use_ack || opts->ext_copy.use_map) { 12926d0060f6SMat Martineau struct mptcp_ext *mpext = &opts->ext_copy; 12936d0060f6SMat Martineau u8 len = TCPOLEN_MPTCP_DSS_BASE; 12946d0060f6SMat Martineau u8 flags = 0; 12956d0060f6SMat Martineau 12966d0060f6SMat Martineau if (mpext->use_ack) { 1297a0c1d0eaSChristoph Paasch flags = MPTCP_DSS_HAS_ACK; 1298a0c1d0eaSChristoph Paasch if (mpext->ack64) { 12996d0060f6SMat Martineau len += TCPOLEN_MPTCP_DSS_ACK64; 1300a0c1d0eaSChristoph Paasch flags |= MPTCP_DSS_ACK64; 1301a0c1d0eaSChristoph Paasch } else { 1302a0c1d0eaSChristoph Paasch len += TCPOLEN_MPTCP_DSS_ACK32; 1303a0c1d0eaSChristoph Paasch } 13046d0060f6SMat Martineau } 13056d0060f6SMat Martineau 13066d0060f6SMat Martineau if (mpext->use_map) { 13076d0060f6SMat Martineau len += TCPOLEN_MPTCP_DSS_MAP64; 13086d0060f6SMat Martineau 13096d0060f6SMat Martineau /* Use only 64-bit mapping flags for now, add 13106d0060f6SMat Martineau * support for optional 32-bit mappings later. 13116d0060f6SMat Martineau */ 13126d0060f6SMat Martineau flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64; 13136d0060f6SMat Martineau if (mpext->data_fin) 13146d0060f6SMat Martineau flags |= MPTCP_DSS_DATA_FIN; 13156d0060f6SMat Martineau } 13166d0060f6SMat Martineau 13173df523abSPeter Krystad *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags); 13186d0060f6SMat Martineau 13196d0060f6SMat Martineau if (mpext->use_ack) { 1320a0c1d0eaSChristoph Paasch if (mpext->ack64) { 13216d0060f6SMat Martineau put_unaligned_be64(mpext->data_ack, ptr); 13226d0060f6SMat Martineau ptr += 2; 1323a0c1d0eaSChristoph Paasch } else { 1324a0c1d0eaSChristoph Paasch put_unaligned_be32(mpext->data_ack32, ptr); 1325a0c1d0eaSChristoph Paasch ptr += 1; 1326a0c1d0eaSChristoph Paasch } 13276d0060f6SMat Martineau } 13286d0060f6SMat Martineau 13296d0060f6SMat Martineau if (mpext->use_map) { 13306d0060f6SMat Martineau put_unaligned_be64(mpext->data_seq, ptr); 13316d0060f6SMat Martineau ptr += 2; 13326d0060f6SMat Martineau put_unaligned_be32(mpext->subflow_seq, ptr); 13336d0060f6SMat Martineau ptr += 1; 13346d0060f6SMat Martineau put_unaligned_be32(mpext->data_len << 16 | 13356d0060f6SMat Martineau TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); 13366d0060f6SMat Martineau } 13376d0060f6SMat Martineau } 1338fa3fe2b1SFlorian Westphal 1339fa3fe2b1SFlorian Westphal if (tp) 1340fa3fe2b1SFlorian Westphal mptcp_set_rwin(tp); 1341eda7acddSPeter Krystad } 1342dc87efdbSFlorian Westphal 1343dc87efdbSFlorian Westphal __be32 mptcp_get_reset_option(const struct sk_buff *skb) 1344dc87efdbSFlorian Westphal { 1345dc87efdbSFlorian Westphal const struct mptcp_ext *ext = mptcp_get_ext(skb); 1346dc87efdbSFlorian Westphal u8 flags, reason; 1347dc87efdbSFlorian Westphal 1348dc87efdbSFlorian Westphal if (ext) { 1349dc87efdbSFlorian Westphal flags = ext->reset_transient; 1350dc87efdbSFlorian Westphal reason = ext->reset_reason; 1351dc87efdbSFlorian Westphal 1352dc87efdbSFlorian Westphal return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST, 1353dc87efdbSFlorian Westphal flags, reason); 1354dc87efdbSFlorian Westphal } 1355dc87efdbSFlorian Westphal 1356dc87efdbSFlorian Westphal return htonl(0u); 1357dc87efdbSFlorian Westphal } 1358dc87efdbSFlorian Westphal EXPORT_SYMBOL_GPL(mptcp_get_reset_option); 1359