Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Multipath TCP |
| 3 | * |
| 4 | * Copyright (c) 2017 - 2019, Intel Corporation. |
| 5 | */ |
| 6 | |
Geliang Tang | c85adce | 2020-04-03 17:14:08 +0800 | [diff] [blame] | 7 | #define pr_fmt(fmt) "MPTCP: " fmt |
| 8 | |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 9 | #include <linux/kernel.h> |
Eric Biggers | a24d22b | 2020-11-12 21:20:21 -0800 | [diff] [blame] | 10 | #include <crypto/sha2.h> |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 11 | #include <net/tcp.h> |
| 12 | #include <net/mptcp.h> |
| 13 | #include "protocol.h" |
Geliang Tang | a877de06 | 2020-09-24 08:29:51 +0800 | [diff] [blame] | 14 | #include "mib.h" |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 15 | |
Geliang Tang | ed66bfb | 2021-04-16 15:38:06 -0700 | [diff] [blame] | 16 | #include <trace/events/mptcp.h> |
| 17 | |
Paolo Abeni | 65492c5 | 2020-01-21 16:56:30 -0800 | [diff] [blame] | 18 | static bool mptcp_cap_flag_sha256(u8 flags) |
| 19 | { |
| 20 | return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256; |
| 21 | } |
| 22 | |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 23 | static void mptcp_parse_option(const struct sk_buff *skb, |
| 24 | const unsigned char *ptr, int opsize, |
| 25 | struct mptcp_options_received *mp_opt) |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 26 | { |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 27 | u8 subtype = *ptr >> 4; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 28 | int expected_opsize; |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 29 | u8 version; |
| 30 | u8 flags; |
Geliang Tang | 5c4a824 | 2021-03-12 17:16:13 -0800 | [diff] [blame] | 31 | u8 i; |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 32 | |
| 33 | switch (subtype) { |
| 34 | case MPTCPOPT_MP_CAPABLE: |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 35 | /* strict size checking */ |
| 36 | if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { |
| 37 | if (skb->len > tcp_hdr(skb)->doff << 2) |
| 38 | expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA; |
| 39 | else |
| 40 | expected_opsize = TCPOLEN_MPTCP_MPC_ACK; |
| 41 | } else { |
| 42 | if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) |
| 43 | expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK; |
| 44 | else |
| 45 | expected_opsize = TCPOLEN_MPTCP_MPC_SYN; |
| 46 | } |
Geliang Tang | 208e8f6 | 2021-06-17 16:46:14 -0700 | [diff] [blame] | 47 | |
| 48 | /* Cfr RFC 8684 Section 3.3.0: |
| 49 | * If a checksum is present but its use had |
| 50 | * not been negotiated in the MP_CAPABLE handshake, the receiver MUST |
| 51 | * close the subflow with a RST, as it is not behaving as negotiated. |
| 52 | * If a checksum is not present when its use has been negotiated, the |
| 53 | * receiver MUST close the subflow with a RST, as it is considered |
| 54 | * broken |
| 55 | * We parse even option with mismatching csum presence, so that |
| 56 | * later in subflow_data_ready we can trigger the reset. |
| 57 | */ |
| 58 | if (opsize != expected_opsize && |
| 59 | (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA || |
| 60 | opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM)) |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 61 | break; |
| 62 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 63 | /* try to be gentle vs future versions on the initial syn */ |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 64 | version = *ptr++ & MPTCP_VERSION_MASK; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 65 | if (opsize != TCPOLEN_MPTCP_MPC_SYN) { |
| 66 | if (version != MPTCP_SUPPORTED_VERSION) |
| 67 | break; |
| 68 | } else if (version < MPTCP_SUPPORTED_VERSION) { |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 69 | break; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 70 | } |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 71 | |
| 72 | flags = *ptr++; |
Paolo Abeni | 65492c5 | 2020-01-21 16:56:30 -0800 | [diff] [blame] | 73 | if (!mptcp_cap_flag_sha256(flags) || |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 74 | (flags & MPTCP_CAP_EXTENSIBILITY)) |
| 75 | break; |
| 76 | |
| 77 | /* RFC 6824, Section 3.1: |
| 78 | * "For the Checksum Required bit (labeled "A"), if either |
| 79 | * host requires the use of checksums, checksums MUST be used. |
| 80 | * In other words, the only way for checksums not to be used |
| 81 | * is if both hosts in their SYNs set A=0." |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 82 | */ |
| 83 | if (flags & MPTCP_CAP_CHECKSUM_REQD) |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 84 | mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD; |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 85 | |
Paolo Abeni | a086aeb | 2021-08-26 17:44:51 -0700 | [diff] [blame] | 86 | mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0); |
Geliang Tang | df377be | 2021-06-22 12:25:20 -0700 | [diff] [blame] | 87 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 88 | mp_opt->suboptions |= OPTIONS_MPTCP_MPC; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 89 | if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) { |
| 90 | mp_opt->sndr_key = get_unaligned_be64(ptr); |
| 91 | ptr += 8; |
| 92 | } |
| 93 | if (opsize >= TCPOLEN_MPTCP_MPC_ACK) { |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 94 | mp_opt->rcvr_key = get_unaligned_be64(ptr); |
| 95 | ptr += 8; |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 96 | } |
Geliang Tang | 208e8f6 | 2021-06-17 16:46:14 -0700 | [diff] [blame] | 97 | if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) { |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 98 | /* Section 3.1.: |
| 99 | * "the data parameters in a MP_CAPABLE are semantically |
| 100 | * equivalent to those in a DSS option and can be used |
| 101 | * interchangeably." |
| 102 | */ |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 103 | mp_opt->suboptions |= OPTION_MPTCP_DSS; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 104 | mp_opt->use_map = 1; |
| 105 | mp_opt->mpc_map = 1; |
| 106 | mp_opt->data_len = get_unaligned_be16(ptr); |
| 107 | ptr += 2; |
| 108 | } |
Geliang Tang | 208e8f6 | 2021-06-17 16:46:14 -0700 | [diff] [blame] | 109 | if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) { |
| 110 | mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr); |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 111 | mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD; |
Geliang Tang | 208e8f6 | 2021-06-17 16:46:14 -0700 | [diff] [blame] | 112 | ptr += 2; |
| 113 | } |
| 114 | pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u", |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 115 | version, flags, opsize, mp_opt->sndr_key, |
Geliang Tang | 208e8f6 | 2021-06-17 16:46:14 -0700 | [diff] [blame] | 116 | mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum); |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 117 | break; |
| 118 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 119 | case MPTCPOPT_MP_JOIN: |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 120 | mp_opt->suboptions |= OPTIONS_MPTCP_MPJ; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 121 | if (opsize == TCPOLEN_MPTCP_MPJ_SYN) { |
| 122 | mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP; |
| 123 | mp_opt->join_id = *ptr++; |
| 124 | mp_opt->token = get_unaligned_be32(ptr); |
| 125 | ptr += 4; |
| 126 | mp_opt->nonce = get_unaligned_be32(ptr); |
| 127 | ptr += 4; |
| 128 | pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u", |
| 129 | mp_opt->backup, mp_opt->join_id, |
| 130 | mp_opt->token, mp_opt->nonce); |
| 131 | } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) { |
| 132 | mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP; |
| 133 | mp_opt->join_id = *ptr++; |
| 134 | mp_opt->thmac = get_unaligned_be64(ptr); |
| 135 | ptr += 8; |
| 136 | mp_opt->nonce = get_unaligned_be32(ptr); |
| 137 | ptr += 4; |
| 138 | pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u", |
| 139 | mp_opt->backup, mp_opt->join_id, |
| 140 | mp_opt->thmac, mp_opt->nonce); |
| 141 | } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) { |
| 142 | ptr += 2; |
| 143 | memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN); |
| 144 | pr_debug("MP_JOIN hmac"); |
| 145 | } else { |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 146 | mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 147 | } |
| 148 | break; |
| 149 | |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 150 | case MPTCPOPT_DSS: |
| 151 | pr_debug("DSS"); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 152 | ptr++; |
| 153 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 154 | /* we must clear 'mpc_map' be able to detect MP_CAPABLE |
| 155 | * map vs DSS map in mptcp_incoming_options(), and reconstruct |
| 156 | * map info accordingly |
| 157 | */ |
| 158 | mp_opt->mpc_map = 0; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 159 | flags = (*ptr++) & MPTCP_DSS_FLAG_MASK; |
| 160 | mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0; |
| 161 | mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0; |
| 162 | mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0; |
| 163 | mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0; |
| 164 | mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK); |
| 165 | |
| 166 | pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d", |
| 167 | mp_opt->data_fin, mp_opt->dsn64, |
| 168 | mp_opt->use_map, mp_opt->ack64, |
| 169 | mp_opt->use_ack); |
| 170 | |
| 171 | expected_opsize = TCPOLEN_MPTCP_DSS_BASE; |
| 172 | |
| 173 | if (mp_opt->use_ack) { |
| 174 | if (mp_opt->ack64) |
| 175 | expected_opsize += TCPOLEN_MPTCP_DSS_ACK64; |
| 176 | else |
| 177 | expected_opsize += TCPOLEN_MPTCP_DSS_ACK32; |
| 178 | } |
| 179 | |
| 180 | if (mp_opt->use_map) { |
| 181 | if (mp_opt->dsn64) |
| 182 | expected_opsize += TCPOLEN_MPTCP_DSS_MAP64; |
| 183 | else |
| 184 | expected_opsize += TCPOLEN_MPTCP_DSS_MAP32; |
| 185 | } |
| 186 | |
Geliang Tang | 390b95a | 2021-06-17 16:46:15 -0700 | [diff] [blame] | 187 | /* Always parse any csum presence combination, we will enforce |
| 188 | * RFC 8684 Section 3.3.0 checks later in subflow_data_ready |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 189 | */ |
| 190 | if (opsize != expected_opsize && |
| 191 | opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) |
| 192 | break; |
| 193 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 194 | mp_opt->suboptions |= OPTION_MPTCP_DSS; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 195 | if (mp_opt->use_ack) { |
| 196 | if (mp_opt->ack64) { |
| 197 | mp_opt->data_ack = get_unaligned_be64(ptr); |
| 198 | ptr += 8; |
| 199 | } else { |
| 200 | mp_opt->data_ack = get_unaligned_be32(ptr); |
| 201 | ptr += 4; |
| 202 | } |
| 203 | |
| 204 | pr_debug("data_ack=%llu", mp_opt->data_ack); |
| 205 | } |
| 206 | |
| 207 | if (mp_opt->use_map) { |
| 208 | if (mp_opt->dsn64) { |
| 209 | mp_opt->data_seq = get_unaligned_be64(ptr); |
| 210 | ptr += 8; |
| 211 | } else { |
| 212 | mp_opt->data_seq = get_unaligned_be32(ptr); |
| 213 | ptr += 4; |
| 214 | } |
| 215 | |
| 216 | mp_opt->subflow_seq = get_unaligned_be32(ptr); |
| 217 | ptr += 4; |
| 218 | |
| 219 | mp_opt->data_len = get_unaligned_be16(ptr); |
| 220 | ptr += 2; |
| 221 | |
Geliang Tang | 390b95a | 2021-06-17 16:46:15 -0700 | [diff] [blame] | 222 | if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) { |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 223 | mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD; |
Geliang Tang | 390b95a | 2021-06-17 16:46:15 -0700 | [diff] [blame] | 224 | mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr); |
| 225 | ptr += 2; |
| 226 | } |
| 227 | |
| 228 | pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u", |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 229 | mp_opt->data_seq, mp_opt->subflow_seq, |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 230 | mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD), |
| 231 | mp_opt->csum); |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 234 | break; |
| 235 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 236 | case MPTCPOPT_ADD_ADDR: |
| 237 | mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO; |
| 238 | if (!mp_opt->echo) { |
| 239 | if (opsize == TCPOLEN_MPTCP_ADD_ADDR || |
| 240 | opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT) |
Geliang Tang | 1b1a6ef | 2021-04-06 17:16:01 -0700 | [diff] [blame] | 241 | mp_opt->addr.family = AF_INET; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 242 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 243 | else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 || |
| 244 | opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT) |
Geliang Tang | 1b1a6ef | 2021-04-06 17:16:01 -0700 | [diff] [blame] | 245 | mp_opt->addr.family = AF_INET6; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 246 | #endif |
| 247 | else |
| 248 | break; |
| 249 | } else { |
| 250 | if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE || |
| 251 | opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) |
Geliang Tang | 1b1a6ef | 2021-04-06 17:16:01 -0700 | [diff] [blame] | 252 | mp_opt->addr.family = AF_INET; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 253 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 254 | else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE || |
| 255 | opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) |
Geliang Tang | 1b1a6ef | 2021-04-06 17:16:01 -0700 | [diff] [blame] | 256 | mp_opt->addr.family = AF_INET6; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 257 | #endif |
| 258 | else |
| 259 | break; |
| 260 | } |
| 261 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 262 | mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR; |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 263 | mp_opt->addr.id = *ptr++; |
Paolo Abeni | a086aeb | 2021-08-26 17:44:51 -0700 | [diff] [blame] | 264 | mp_opt->addr.port = 0; |
| 265 | mp_opt->ahmac = 0; |
Geliang Tang | 1b1a6ef | 2021-04-06 17:16:01 -0700 | [diff] [blame] | 266 | if (mp_opt->addr.family == AF_INET) { |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 267 | memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 268 | ptr += 4; |
| 269 | if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT || |
| 270 | opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) { |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 271 | mp_opt->addr.port = htons(get_unaligned_be16(ptr)); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 272 | ptr += 2; |
| 273 | } |
| 274 | } |
| 275 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 276 | else { |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 277 | memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 278 | ptr += 16; |
| 279 | if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT || |
| 280 | opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) { |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 281 | mp_opt->addr.port = htons(get_unaligned_be16(ptr)); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 282 | ptr += 2; |
| 283 | } |
| 284 | } |
| 285 | #endif |
| 286 | if (!mp_opt->echo) { |
| 287 | mp_opt->ahmac = get_unaligned_be64(ptr); |
| 288 | ptr += 8; |
| 289 | } |
Geliang Tang | 90a4aea | 2020-12-09 15:51:25 -0800 | [diff] [blame] | 290 | pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d", |
Geliang Tang | 1b1a6ef | 2021-04-06 17:16:01 -0700 | [diff] [blame] | 291 | (mp_opt->addr.family == AF_INET6) ? "6" : "", |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 292 | mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port)); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | case MPTCPOPT_RM_ADDR: |
Geliang Tang | 5c4a824 | 2021-03-12 17:16:13 -0800 | [diff] [blame] | 296 | if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 || |
| 297 | opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX) |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 298 | break; |
| 299 | |
Geliang Tang | 8e60eed | 2020-06-08 18:47:54 +0800 | [diff] [blame] | 300 | ptr++; |
| 301 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 302 | mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR; |
Geliang Tang | 5c4a824 | 2021-03-12 17:16:13 -0800 | [diff] [blame] | 303 | mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE; |
| 304 | for (i = 0; i < mp_opt->rm_list.nr; i++) |
| 305 | mp_opt->rm_list.ids[i] = *ptr++; |
| 306 | pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 307 | break; |
| 308 | |
Geliang Tang | 40453a5 | 2021-01-08 16:47:58 -0800 | [diff] [blame] | 309 | case MPTCPOPT_MP_PRIO: |
| 310 | if (opsize != TCPOLEN_MPTCP_PRIO) |
| 311 | break; |
| 312 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 313 | mp_opt->suboptions |= OPTION_MPTCP_PRIO; |
Geliang Tang | 40453a5 | 2021-01-08 16:47:58 -0800 | [diff] [blame] | 314 | mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP; |
| 315 | pr_debug("MP_PRIO: prio=%d", mp_opt->backup); |
| 316 | break; |
| 317 | |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 318 | case MPTCPOPT_MP_FASTCLOSE: |
| 319 | if (opsize != TCPOLEN_MPTCP_FASTCLOSE) |
| 320 | break; |
| 321 | |
| 322 | ptr += 2; |
| 323 | mp_opt->rcvr_key = get_unaligned_be64(ptr); |
| 324 | ptr += 8; |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 325 | mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE; |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 326 | break; |
| 327 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 328 | case MPTCPOPT_RST: |
| 329 | if (opsize != TCPOLEN_MPTCP_RST) |
| 330 | break; |
| 331 | |
| 332 | if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) |
| 333 | break; |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 334 | |
| 335 | mp_opt->suboptions |= OPTION_MPTCP_RST; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 336 | flags = *ptr++; |
| 337 | mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT; |
| 338 | mp_opt->reset_reason = *ptr; |
| 339 | break; |
| 340 | |
Geliang Tang | 5580d41 | 2021-08-24 16:26:16 -0700 | [diff] [blame] | 341 | case MPTCPOPT_MP_FAIL: |
| 342 | if (opsize != TCPOLEN_MPTCP_FAIL) |
| 343 | break; |
| 344 | |
| 345 | ptr += 2; |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 346 | mp_opt->suboptions |= OPTION_MPTCP_FAIL; |
Geliang Tang | 5580d41 | 2021-08-24 16:26:16 -0700 | [diff] [blame] | 347 | mp_opt->fail_seq = get_unaligned_be64(ptr); |
| 348 | pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq); |
| 349 | break; |
| 350 | |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 351 | default: |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
Geliang Tang | c863225 | 2021-06-17 16:46:12 -0700 | [diff] [blame] | 356 | void mptcp_get_options(const struct sock *sk, |
| 357 | const struct sk_buff *skb, |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 358 | struct mptcp_options_received *mp_opt) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 359 | { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 360 | const struct tcphdr *th = tcp_hdr(skb); |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 361 | const unsigned char *ptr; |
| 362 | int length; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 363 | |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 364 | /* initialize option status */ |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 365 | mp_opt->suboptions = 0; |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 366 | |
| 367 | length = (th->doff * 4) - sizeof(struct tcphdr); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 368 | ptr = (const unsigned char *)(th + 1); |
| 369 | |
| 370 | while (length > 0) { |
| 371 | int opcode = *ptr++; |
| 372 | int opsize; |
| 373 | |
| 374 | switch (opcode) { |
| 375 | case TCPOPT_EOL: |
| 376 | return; |
| 377 | case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ |
| 378 | length--; |
| 379 | continue; |
| 380 | default: |
Maxim Mikityanskiy | 07718be | 2021-06-10 19:40:30 +0300 | [diff] [blame] | 381 | if (length < 2) |
| 382 | return; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 383 | opsize = *ptr++; |
| 384 | if (opsize < 2) /* "silly options" */ |
| 385 | return; |
| 386 | if (opsize > length) |
| 387 | return; /* don't parse partial options */ |
| 388 | if (opcode == TCPOPT_MPTCP) |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 389 | mptcp_parse_option(skb, ptr, opsize, mp_opt); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 390 | ptr += opsize - 2; |
| 391 | length -= opsize; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 396 | bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb, |
| 397 | unsigned int *size, struct mptcp_out_options *opts) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 398 | { |
| 399 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 400 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 401 | /* we will use snd_isn to detect first pkt [re]transmission |
| 402 | * in mptcp_established_options_mp() |
| 403 | */ |
| 404 | subflow->snd_isn = TCP_SKB_CB(skb)->end_seq; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 405 | if (subflow->request_mptcp) { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 406 | opts->suboptions = OPTION_MPTCP_MPC_SYN; |
Geliang Tang | 06fe171 | 2021-06-17 16:46:09 -0700 | [diff] [blame] | 407 | opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk)); |
Geliang Tang | bab6b88 | 2021-06-22 12:25:19 -0700 | [diff] [blame] | 408 | opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk)); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 409 | *size = TCPOLEN_MPTCP_MPC_SYN; |
| 410 | return true; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 411 | } else if (subflow->request_join) { |
| 412 | pr_debug("remote_token=%u, nonce=%u", subflow->remote_token, |
| 413 | subflow->local_nonce); |
| 414 | opts->suboptions = OPTION_MPTCP_MPJ_SYN; |
| 415 | opts->join_id = subflow->local_id; |
| 416 | opts->token = subflow->remote_token; |
| 417 | opts->nonce = subflow->local_nonce; |
| 418 | opts->backup = subflow->request_bkup; |
| 419 | *size = TCPOLEN_MPTCP_MPJ_SYN; |
| 420 | return true; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 421 | } |
| 422 | return false; |
| 423 | } |
| 424 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 425 | static void clear_3rdack_retransmission(struct sock *sk) |
| 426 | { |
| 427 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 428 | |
| 429 | sk_stop_timer(sk, &icsk->icsk_delack_timer); |
| 430 | icsk->icsk_ack.timeout = 0; |
| 431 | icsk->icsk_ack.ato = 0; |
| 432 | icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER); |
| 433 | } |
| 434 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 435 | static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb, |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 436 | bool snd_data_fin_enable, |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 437 | unsigned int *size, |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 438 | unsigned int remaining, |
| 439 | struct mptcp_out_options *opts) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 440 | { |
| 441 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Geliang Tang | 06fe171 | 2021-06-17 16:46:09 -0700 | [diff] [blame] | 442 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 443 | struct mptcp_ext *mpext; |
| 444 | unsigned int data_len; |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 445 | u8 len; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 446 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 447 | /* When skb is not available, we better over-estimate the emitted |
| 448 | * options len. A full DSS option (28 bytes) is longer than |
| 449 | * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so |
| 450 | * tell the caller to defer the estimate to |
| 451 | * mptcp_established_options_dss(), which will reserve enough space. |
| 452 | */ |
| 453 | if (!skb) |
| 454 | return false; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 455 | |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 456 | /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */ |
| 457 | if (subflow->fully_established || snd_data_fin_enable || |
| 458 | subflow->snd_isn != TCP_SKB_CB(skb)->seq || |
| 459 | sk->sk_state != TCP_ESTABLISHED) |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 460 | return false; |
| 461 | |
| 462 | if (subflow->mp_capable) { |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 463 | mpext = mptcp_get_ext(skb); |
| 464 | data_len = mpext ? mpext->data_len : 0; |
| 465 | |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 466 | /* we will check ops->data_len in mptcp_write_options() to |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 467 | * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and |
| 468 | * TCPOLEN_MPTCP_MPC_ACK |
| 469 | */ |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 470 | opts->data_len = data_len; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 471 | opts->suboptions = OPTION_MPTCP_MPC_ACK; |
| 472 | opts->sndr_key = subflow->local_key; |
| 473 | opts->rcvr_key = subflow->remote_key; |
Geliang Tang | 06fe171 | 2021-06-17 16:46:09 -0700 | [diff] [blame] | 474 | opts->csum_reqd = READ_ONCE(msk->csum_enabled); |
Geliang Tang | bab6b88 | 2021-06-22 12:25:19 -0700 | [diff] [blame] | 475 | opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk)); |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 476 | |
| 477 | /* Section 3.1. |
| 478 | * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK |
| 479 | * packets that start the first subflow of an MPTCP connection, |
| 480 | * as well as the first packet that carries data |
| 481 | */ |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 482 | if (data_len > 0) { |
| 483 | len = TCPOLEN_MPTCP_MPC_ACK_DATA; |
| 484 | if (opts->csum_reqd) { |
Geliang Tang | c5b39e2 | 2021-06-17 16:46:11 -0700 | [diff] [blame] | 485 | /* we need to propagate more info to csum the pseudo hdr */ |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 486 | opts->data_seq = mpext->data_seq; |
| 487 | opts->subflow_seq = mpext->subflow_seq; |
| 488 | opts->csum = mpext->csum; |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 489 | len += TCPOLEN_MPTCP_DSS_CHECKSUM; |
| 490 | } |
| 491 | *size = ALIGN(len, 4); |
| 492 | } else { |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 493 | *size = TCPOLEN_MPTCP_MPC_ACK; |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 494 | } |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 495 | |
| 496 | pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d", |
| 497 | subflow, subflow->local_key, subflow->remote_key, |
| 498 | data_len); |
| 499 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 500 | return true; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 501 | } else if (subflow->mp_join) { |
| 502 | opts->suboptions = OPTION_MPTCP_MPJ_ACK; |
| 503 | memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN); |
| 504 | *size = TCPOLEN_MPTCP_MPJ_ACK; |
| 505 | pr_debug("subflow=%p", subflow); |
| 506 | |
Paolo Abeni | bcd9773 | 2021-11-19 15:27:55 +0100 | [diff] [blame] | 507 | /* we can use the full delegate action helper only from BH context |
| 508 | * If we are in process context - sk is flushing the backlog at |
| 509 | * socket lock release time - just set the appropriate flag, will |
| 510 | * be handled by the release callback |
| 511 | */ |
| 512 | if (sock_owned_by_user(sk)) |
| 513 | set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status); |
| 514 | else |
| 515 | mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 516 | return true; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 517 | } |
| 518 | return false; |
| 519 | } |
| 520 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 521 | static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow, |
Paolo Abeni | 9c29e36 | 2020-07-03 18:06:04 +0200 | [diff] [blame] | 522 | struct sk_buff *skb, struct mptcp_ext *ext) |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 523 | { |
Paolo Abeni | 017512a | 2020-10-05 12:01:06 +0200 | [diff] [blame] | 524 | /* The write_seq value has already been incremented, so the actual |
| 525 | * sequence number for the DATA_FIN is one less. |
| 526 | */ |
| 527 | u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1; |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 528 | |
Paolo Abeni | 9c29e36 | 2020-07-03 18:06:04 +0200 | [diff] [blame] | 529 | if (!ext->use_map || !skb->len) { |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 530 | /* RFC6824 requires a DSS mapping with specific values |
| 531 | * if DATA_FIN is set but no data payload is mapped |
| 532 | */ |
Mat Martineau | 6d37a0b | 2020-02-28 15:47:41 -0800 | [diff] [blame] | 533 | ext->data_fin = 1; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 534 | ext->use_map = 1; |
| 535 | ext->dsn64 = 1; |
Paolo Abeni | 017512a | 2020-10-05 12:01:06 +0200 | [diff] [blame] | 536 | ext->data_seq = data_fin_tx_seq; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 537 | ext->subflow_seq = 0; |
| 538 | ext->data_len = 1; |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 539 | } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) { |
Mat Martineau | 6d37a0b | 2020-02-28 15:47:41 -0800 | [diff] [blame] | 540 | /* If there's an existing DSS mapping and it is the |
| 541 | * final mapping, DATA_FIN consumes 1 additional byte of |
| 542 | * mapping space. |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 543 | */ |
Mat Martineau | 6d37a0b | 2020-02-28 15:47:41 -0800 | [diff] [blame] | 544 | ext->data_fin = 1; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 545 | ext->data_len++; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb, |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 550 | bool snd_data_fin_enable, |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 551 | unsigned int *size, |
| 552 | unsigned int remaining, |
| 553 | struct mptcp_out_options *opts) |
| 554 | { |
| 555 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 556 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 557 | unsigned int dss_size = 0; |
| 558 | struct mptcp_ext *mpext; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 559 | unsigned int ack_size; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 560 | bool ret = false; |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 561 | u64 ack_seq; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 562 | |
Geliang Tang | c5b39e2 | 2021-06-17 16:46:11 -0700 | [diff] [blame] | 563 | opts->csum_reqd = READ_ONCE(msk->csum_enabled); |
Mat Martineau | 0bac966 | 2020-07-28 15:11:59 -0700 | [diff] [blame] | 564 | mpext = skb ? mptcp_get_ext(skb) : NULL; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 565 | |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 566 | if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) { |
Geliang Tang | c5b39e2 | 2021-06-17 16:46:11 -0700 | [diff] [blame] | 567 | unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 568 | |
Geliang Tang | c5b39e2 | 2021-06-17 16:46:11 -0700 | [diff] [blame] | 569 | if (mpext) { |
| 570 | if (opts->csum_reqd) |
| 571 | map_size += TCPOLEN_MPTCP_DSS_CHECKSUM; |
| 572 | |
| 573 | opts->ext_copy = *mpext; |
| 574 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 575 | |
| 576 | remaining -= map_size; |
| 577 | dss_size = map_size; |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 578 | if (skb && snd_data_fin_enable) |
Paolo Abeni | 9c29e36 | 2020-07-03 18:06:04 +0200 | [diff] [blame] | 579 | mptcp_write_data_fin(subflow, skb, &opts->ext_copy); |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 580 | opts->suboptions = OPTION_MPTCP_DSS; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 581 | ret = true; |
| 582 | } |
| 583 | |
Paolo Abeni | 2398e39 | 2020-03-04 16:51:07 +0100 | [diff] [blame] | 584 | /* passive sockets msk will set the 'can_ack' after accept(), even |
| 585 | * if the first subflow may have the already the remote key handy |
| 586 | */ |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 587 | opts->ext_copy.use_ack = 0; |
Paolo Abeni | dc093db | 2020-03-13 16:52:42 +0100 | [diff] [blame] | 588 | if (!READ_ONCE(msk->can_ack)) { |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 589 | *size = ALIGN(dss_size, 4); |
| 590 | return ret; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 593 | ack_seq = READ_ONCE(msk->ack_seq); |
Davide Caratti | 37198e9 | 2020-10-06 18:26:17 +0200 | [diff] [blame] | 594 | if (READ_ONCE(msk->use_64bit_ack)) { |
Christoph Paasch | a0c1d0e | 2020-05-14 08:53:03 -0700 | [diff] [blame] | 595 | ack_size = TCPOLEN_MPTCP_DSS_ACK64; |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 596 | opts->ext_copy.data_ack = ack_seq; |
Christoph Paasch | a0c1d0e | 2020-05-14 08:53:03 -0700 | [diff] [blame] | 597 | opts->ext_copy.ack64 = 1; |
| 598 | } else { |
| 599 | ack_size = TCPOLEN_MPTCP_DSS_ACK32; |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 600 | opts->ext_copy.data_ack32 = (uint32_t)ack_seq; |
Christoph Paasch | a0c1d0e | 2020-05-14 08:53:03 -0700 | [diff] [blame] | 601 | opts->ext_copy.ack64 = 0; |
| 602 | } |
| 603 | opts->ext_copy.use_ack = 1; |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 604 | opts->suboptions = OPTION_MPTCP_DSS; |
Paolo Abeni | ea4ca58 | 2020-11-19 11:46:03 -0800 | [diff] [blame] | 605 | WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk)); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 606 | |
| 607 | /* Add kind/length/subtype/flag overhead if mapping is not populated */ |
| 608 | if (dss_size == 0) |
| 609 | ack_size += TCPOLEN_MPTCP_DSS_BASE; |
| 610 | |
| 611 | dss_size += ack_size; |
| 612 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 613 | *size = ALIGN(dss_size, 4); |
| 614 | return true; |
| 615 | } |
| 616 | |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 617 | static u64 add_addr_generate_hmac(u64 key1, u64 key2, |
| 618 | struct mptcp_addr_info *addr) |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 619 | { |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 620 | u16 port = ntohs(addr->port); |
Todd Malsbary | bd69722 | 2020-05-21 19:10:49 -0700 | [diff] [blame] | 621 | u8 hmac[SHA256_DIGEST_SIZE]; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 622 | u8 msg[19]; |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 623 | int i = 0; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 624 | |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 625 | msg[i++] = addr->id; |
| 626 | if (addr->family == AF_INET) { |
| 627 | memcpy(&msg[i], &addr->addr.s_addr, 4); |
| 628 | i += 4; |
| 629 | } |
| 630 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 631 | else if (addr->family == AF_INET6) { |
| 632 | memcpy(&msg[i], &addr->addr6.s6_addr, 16); |
| 633 | i += 16; |
| 634 | } |
| 635 | #endif |
| 636 | msg[i++] = port >> 8; |
| 637 | msg[i++] = port & 0xFF; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 638 | |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 639 | mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 640 | |
Todd Malsbary | bd69722 | 2020-05-21 19:10:49 -0700 | [diff] [blame] | 641 | return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 642 | } |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 643 | |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 644 | static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb, |
Geliang Tang | f643b80 | 2020-09-24 08:29:47 +0800 | [diff] [blame] | 645 | unsigned int *size, |
| 646 | unsigned int remaining, |
| 647 | struct mptcp_out_options *opts) |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 648 | { |
| 649 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 650 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 651 | bool drop_other_suboptions = false; |
| 652 | unsigned int opt_size = *size; |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 653 | bool echo; |
Geliang Tang | 4a2777a8 | 2020-12-09 15:51:22 -0800 | [diff] [blame] | 654 | bool port; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 655 | int len; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 656 | |
Yonglong Li | 1f5e9e2 | 2021-08-23 18:05:39 -0700 | [diff] [blame] | 657 | /* add addr will strip the existing options, be sure to avoid breaking |
| 658 | * MPC/MPJ handshakes |
| 659 | */ |
Geliang Tang | f643b80 | 2020-09-24 08:29:47 +0800 | [diff] [blame] | 660 | if (!mptcp_pm_should_add_signal(msk) || |
Yonglong Li | 1f5e9e2 | 2021-08-23 18:05:39 -0700 | [diff] [blame] | 661 | (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) || |
| 662 | !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr, |
| 663 | &echo, &port, &drop_other_suboptions)) |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 664 | return false; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 665 | |
Yonglong Li | 1f5e9e2 | 2021-08-23 18:05:39 -0700 | [diff] [blame] | 666 | if (drop_other_suboptions) |
| 667 | remaining += opt_size; |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 668 | len = mptcp_add_addr_len(opts->addr.family, echo, port); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 669 | if (remaining < len) |
| 670 | return false; |
| 671 | |
| 672 | *size = len; |
Yonglong Li | 1f5e9e2 | 2021-08-23 18:05:39 -0700 | [diff] [blame] | 673 | if (drop_other_suboptions) { |
| 674 | pr_debug("drop other suboptions"); |
| 675 | opts->suboptions = 0; |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 676 | |
| 677 | /* note that e.g. DSS could have written into the memory |
| 678 | * aliased by ahmac, we must reset the field here |
| 679 | * to avoid appending the hmac even for ADD_ADDR echo |
| 680 | * options |
| 681 | */ |
| 682 | opts->ahmac = 0; |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 683 | *size -= opt_size; |
Yonglong Li | 1f5e9e2 | 2021-08-23 18:05:39 -0700 | [diff] [blame] | 684 | } |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 685 | opts->suboptions |= OPTION_MPTCP_ADD_ADDR; |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 686 | if (!echo) { |
| 687 | opts->ahmac = add_addr_generate_hmac(msk->local_key, |
| 688 | msk->remote_key, |
| 689 | &opts->addr); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 690 | } |
Geliang Tang | 4a2777a8 | 2020-12-09 15:51:22 -0800 | [diff] [blame] | 691 | pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d", |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 692 | opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port)); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 693 | |
| 694 | return true; |
| 695 | } |
| 696 | |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 697 | static bool mptcp_established_options_rm_addr(struct sock *sk, |
| 698 | unsigned int *size, |
| 699 | unsigned int remaining, |
| 700 | struct mptcp_out_options *opts) |
| 701 | { |
| 702 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 703 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 704 | struct mptcp_rm_list rm_list; |
| 705 | int i, len; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 706 | |
| 707 | if (!mptcp_pm_should_rm_signal(msk) || |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 708 | !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list))) |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 709 | return false; |
| 710 | |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 711 | len = mptcp_rm_addr_len(&rm_list); |
| 712 | if (len < 0) |
| 713 | return false; |
| 714 | if (remaining < len) |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 715 | return false; |
| 716 | |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 717 | *size = len; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 718 | opts->suboptions |= OPTION_MPTCP_RM_ADDR; |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 719 | opts->rm_list = rm_list; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 720 | |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 721 | for (i = 0; i < opts->rm_list.nr; i++) |
| 722 | pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]); |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 723 | |
| 724 | return true; |
| 725 | } |
| 726 | |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 727 | static bool mptcp_established_options_mp_prio(struct sock *sk, |
| 728 | unsigned int *size, |
| 729 | unsigned int remaining, |
| 730 | struct mptcp_out_options *opts) |
| 731 | { |
| 732 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 733 | |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 734 | /* can't send MP_PRIO with MPC, as they share the same option space: |
| 735 | * 'backup'. Also it makes no sense at all |
| 736 | */ |
Geliang Tang | 13ac17a | 2021-09-24 14:12:35 -0700 | [diff] [blame] | 737 | if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC)) |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 738 | return false; |
| 739 | |
Davide Caratti | ec99a47 | 2021-02-01 14:05:26 +0100 | [diff] [blame] | 740 | /* account for the trailing 'nop' option */ |
| 741 | if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN) |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 742 | return false; |
| 743 | |
Davide Caratti | ec99a47 | 2021-02-01 14:05:26 +0100 | [diff] [blame] | 744 | *size = TCPOLEN_MPTCP_PRIO_ALIGN; |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 745 | opts->suboptions |= OPTION_MPTCP_PRIO; |
| 746 | opts->backup = subflow->request_bkup; |
| 747 | |
| 748 | pr_debug("prio=%d", opts->backup); |
| 749 | |
| 750 | return true; |
| 751 | } |
| 752 | |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 753 | static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb, |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 754 | unsigned int *size, |
| 755 | unsigned int remaining, |
| 756 | struct mptcp_out_options *opts) |
| 757 | { |
| 758 | const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 759 | |
| 760 | if (remaining < TCPOLEN_MPTCP_RST) |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 761 | return false; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 762 | |
| 763 | *size = TCPOLEN_MPTCP_RST; |
| 764 | opts->suboptions |= OPTION_MPTCP_RST; |
| 765 | opts->reset_transient = subflow->reset_transient; |
| 766 | opts->reset_reason = subflow->reset_reason; |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 767 | |
| 768 | return true; |
| 769 | } |
| 770 | |
Paolo Abeni | f284c0c | 2022-01-06 16:20:15 -0800 | [diff] [blame] | 771 | static bool mptcp_established_options_fastclose(struct sock *sk, |
| 772 | unsigned int *size, |
| 773 | unsigned int remaining, |
| 774 | struct mptcp_out_options *opts) |
| 775 | { |
| 776 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 777 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
| 778 | |
| 779 | if (likely(!subflow->send_fastclose)) |
| 780 | return false; |
| 781 | |
| 782 | if (remaining < TCPOLEN_MPTCP_FASTCLOSE) |
| 783 | return false; |
| 784 | |
| 785 | *size = TCPOLEN_MPTCP_FASTCLOSE; |
| 786 | opts->suboptions |= OPTION_MPTCP_FASTCLOSE; |
| 787 | opts->rcvr_key = msk->remote_key; |
| 788 | |
| 789 | pr_debug("FASTCLOSE key=%llu", opts->rcvr_key); |
| 790 | return true; |
| 791 | } |
| 792 | |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 793 | static bool mptcp_established_options_mp_fail(struct sock *sk, |
| 794 | unsigned int *size, |
| 795 | unsigned int remaining, |
| 796 | struct mptcp_out_options *opts) |
| 797 | { |
| 798 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 799 | |
| 800 | if (likely(!subflow->send_mp_fail)) |
| 801 | return false; |
| 802 | |
| 803 | if (remaining < TCPOLEN_MPTCP_FAIL) |
| 804 | return false; |
| 805 | |
| 806 | *size = TCPOLEN_MPTCP_FAIL; |
| 807 | opts->suboptions |= OPTION_MPTCP_FAIL; |
| 808 | opts->fail_seq = subflow->map_seq; |
| 809 | |
| 810 | pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq); |
| 811 | |
| 812 | return true; |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 815 | bool mptcp_established_options(struct sock *sk, struct sk_buff *skb, |
| 816 | unsigned int *size, unsigned int remaining, |
| 817 | struct mptcp_out_options *opts) |
| 818 | { |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 819 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 820 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 821 | unsigned int opt_size = 0; |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 822 | bool snd_data_fin; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 823 | bool ret = false; |
| 824 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 825 | opts->suboptions = 0; |
| 826 | |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 827 | if (unlikely(__mptcp_check_fallback(msk))) |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 828 | return false; |
| 829 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 830 | if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) { |
Paolo Abeni | f284c0c | 2022-01-06 16:20:15 -0800 | [diff] [blame] | 831 | if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) || |
| 832 | mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) { |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 833 | *size += opt_size; |
| 834 | remaining -= opt_size; |
| 835 | } |
Paolo Abeni | f284c0c | 2022-01-06 16:20:15 -0800 | [diff] [blame] | 836 | /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */ |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 837 | if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) { |
| 838 | *size += opt_size; |
| 839 | remaining -= opt_size; |
| 840 | } |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 841 | return true; |
| 842 | } |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 843 | |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 844 | snd_data_fin = mptcp_data_fin_enabled(msk); |
| 845 | if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts)) |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 846 | ret = true; |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 847 | else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) { |
Matthieu Baerts | 04fac2c | 2022-01-06 14:06:36 -0800 | [diff] [blame] | 848 | unsigned int mp_fail_size; |
| 849 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 850 | ret = true; |
Matthieu Baerts | 04fac2c | 2022-01-06 14:06:36 -0800 | [diff] [blame] | 851 | if (mptcp_established_options_mp_fail(sk, &mp_fail_size, |
| 852 | remaining - opt_size, opts)) { |
| 853 | *size += opt_size + mp_fail_size; |
| 854 | remaining -= opt_size - mp_fail_size; |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 855 | return true; |
| 856 | } |
| 857 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 858 | |
| 859 | /* we reserved enough space for the above options, and exceeding the |
| 860 | * TCP option space would be fatal |
| 861 | */ |
| 862 | if (WARN_ON_ONCE(opt_size > remaining)) |
| 863 | return false; |
| 864 | |
| 865 | *size += opt_size; |
| 866 | remaining -= opt_size; |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 867 | if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) { |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 868 | *size += opt_size; |
| 869 | remaining -= opt_size; |
| 870 | ret = true; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 871 | } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) { |
| 872 | *size += opt_size; |
| 873 | remaining -= opt_size; |
| 874 | ret = true; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 875 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 876 | |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 877 | if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) { |
| 878 | *size += opt_size; |
| 879 | remaining -= opt_size; |
| 880 | ret = true; |
| 881 | } |
| 882 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 883 | return ret; |
| 884 | } |
| 885 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 886 | bool mptcp_synack_options(const struct request_sock *req, unsigned int *size, |
| 887 | struct mptcp_out_options *opts) |
| 888 | { |
| 889 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
| 890 | |
| 891 | if (subflow_req->mp_capable) { |
| 892 | opts->suboptions = OPTION_MPTCP_MPC_SYNACK; |
| 893 | opts->sndr_key = subflow_req->local_key; |
Geliang Tang | 06fe171 | 2021-06-17 16:46:09 -0700 | [diff] [blame] | 894 | opts->csum_reqd = subflow_req->csum_reqd; |
Geliang Tang | bab6b88 | 2021-06-22 12:25:19 -0700 | [diff] [blame] | 895 | opts->allow_join_id0 = subflow_req->allow_join_id0; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 896 | *size = TCPOLEN_MPTCP_MPC_SYNACK; |
| 897 | pr_debug("subflow_req=%p, local_key=%llu", |
| 898 | subflow_req, subflow_req->local_key); |
| 899 | return true; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 900 | } else if (subflow_req->mp_join) { |
| 901 | opts->suboptions = OPTION_MPTCP_MPJ_SYNACK; |
| 902 | opts->backup = subflow_req->backup; |
| 903 | opts->join_id = subflow_req->local_id; |
| 904 | opts->thmac = subflow_req->thmac; |
| 905 | opts->nonce = subflow_req->local_nonce; |
| 906 | pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u", |
| 907 | subflow_req, opts->backup, opts->join_id, |
| 908 | opts->thmac, opts->nonce); |
| 909 | *size = TCPOLEN_MPTCP_MPJ_SYNACK; |
| 910 | return true; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 911 | } |
| 912 | return false; |
| 913 | } |
| 914 | |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 915 | static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk, |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 916 | struct mptcp_subflow_context *subflow, |
Paolo Abeni | 0be534f | 2020-03-19 11:06:30 +0100 | [diff] [blame] | 917 | struct sk_buff *skb, |
| 918 | struct mptcp_options_received *mp_opt) |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 919 | { |
| 920 | /* here we can process OoO, in-window pkts, only in-sequence 4th ack |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 921 | * will make the subflow fully established |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 922 | */ |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 923 | if (likely(subflow->fully_established)) { |
| 924 | /* on passive sockets, check for 3rd ack retransmission |
| 925 | * note that msk is always set by subflow_syn_recv_sock() |
| 926 | * for mp_join subflows |
| 927 | */ |
| 928 | if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 && |
| 929 | TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq && |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 930 | subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) && |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 931 | READ_ONCE(msk->pm.server_side)) |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 932 | tcp_send_ack(ssk); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 933 | goto fully_established; |
| 934 | } |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 935 | |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 936 | /* we must process OoO packets before the first subflow is fully |
| 937 | * established. OoO packets are instead a protocol violation |
| 938 | * for MP_JOIN subflows as the peer must not send any data |
| 939 | * before receiving the forth ack - cfr. RFC 8684 section 3.2. |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 940 | */ |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 941 | if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) { |
| 942 | if (subflow->mp_join) |
| 943 | goto reset; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 944 | return subflow->mp_capable; |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 945 | } |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 946 | |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 947 | if (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) || |
| 948 | ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo)) { |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 949 | /* subflows are fully established as soon as we get any |
Matthieu Baerts | 67b12f7 | 2021-08-18 16:42:37 -0700 | [diff] [blame] | 950 | * additional ack, including ADD_ADDR. |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 951 | */ |
Paolo Abeni | 0be534f | 2020-03-19 11:06:30 +0100 | [diff] [blame] | 952 | subflow->fully_established = 1; |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 953 | WRITE_ONCE(msk->fully_established, true); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 954 | goto fully_established; |
| 955 | } |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 956 | |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 957 | /* If the first established packet does not contain MP_CAPABLE + data |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 958 | * then fallback to TCP. Fallback scenarios requires a reset for |
| 959 | * MP_JOIN subflows. |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 960 | */ |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 961 | if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) { |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 962 | if (subflow->mp_join) |
| 963 | goto reset; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 964 | subflow->mp_capable = 0; |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 965 | pr_fallback(msk); |
| 966 | __mptcp_do_fallback(msk); |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 967 | return false; |
| 968 | } |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 969 | |
Geliang Tang | df377be | 2021-06-22 12:25:20 -0700 | [diff] [blame] | 970 | if (mp_opt->deny_join_id0) |
| 971 | WRITE_ONCE(msk->pm.remote_deny_join_id0, true); |
| 972 | |
Paolo Abeni | d6085fe | 2020-04-30 15:01:53 +0200 | [diff] [blame] | 973 | if (unlikely(!READ_ONCE(msk->pm.server_side))) |
| 974 | pr_warn_once("bogus mpc option on established client sk"); |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 975 | mptcp_subflow_fully_established(subflow, mp_opt); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 976 | |
| 977 | fully_established: |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 978 | /* if the subflow is not already linked into the conn_list, we can't |
| 979 | * notify the PM: this subflow is still on the listener queue |
| 980 | * and the PM possibly acquiring the subflow lock could race with |
| 981 | * the listener close |
| 982 | */ |
| 983 | if (likely(subflow->pm_notified) || list_empty(&subflow->node)) |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 984 | return true; |
| 985 | |
| 986 | subflow->pm_notified = 1; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 987 | if (subflow->mp_join) { |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 988 | clear_3rdack_retransmission(ssk); |
Geliang Tang | 62535200 | 2021-03-26 11:26:33 -0700 | [diff] [blame] | 989 | mptcp_pm_subflow_established(msk); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 990 | } else { |
Florian Westphal | 6c714f1 | 2021-02-12 15:59:58 -0800 | [diff] [blame] | 991 | mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 992 | } |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 993 | return true; |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 994 | |
| 995 | reset: |
| 996 | mptcp_subflow_reset(ssk); |
| 997 | return false; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame] | 1000 | u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq) |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1001 | { |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame] | 1002 | u32 old_seq32, cur_seq32; |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1003 | |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame] | 1004 | old_seq32 = (u32)old_seq; |
| 1005 | cur_seq32 = (u32)cur_seq; |
| 1006 | cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32; |
| 1007 | if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32))) |
| 1008 | return cur_seq + (1LL << 32); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1009 | |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame] | 1010 | /* reverse wrap could happen, too */ |
| 1011 | if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32))) |
| 1012 | return cur_seq - (1LL << 32); |
| 1013 | return cur_seq; |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1016 | static void ack_update_msk(struct mptcp_sock *msk, |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1017 | struct sock *ssk, |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1018 | struct mptcp_options_received *mp_opt) |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1019 | { |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1020 | u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt); |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1021 | struct sock *sk = (struct sock *)msk; |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1022 | u64 old_snd_una; |
| 1023 | |
| 1024 | mptcp_data_lock(sk); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1025 | |
| 1026 | /* avoid ack expansion on update conflict, to reduce the risk of |
| 1027 | * wrongly expanding to a future ack sequence number, which is way |
| 1028 | * more dangerous than missing an ack |
| 1029 | */ |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1030 | old_snd_una = msk->snd_una; |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame] | 1031 | new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1032 | |
Florian Westphal | 0d199e4 | 2021-09-24 14:12:34 -0700 | [diff] [blame] | 1033 | /* ACK for data not even sent yet? Ignore.*/ |
| 1034 | if (unlikely(after64(new_snd_una, snd_nxt))) |
| 1035 | new_snd_una = old_snd_una; |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1036 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1037 | new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd; |
| 1038 | |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 1039 | if (after64(new_wnd_end, msk->wnd_end)) |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1040 | msk->wnd_end = new_wnd_end; |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 1041 | |
| 1042 | /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */ |
Paolo Abeni | d09d818 | 2021-02-11 15:30:42 -0800 | [diff] [blame] | 1043 | if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt))) |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 1044 | __mptcp_check_push(sk, ssk); |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1045 | |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1046 | if (after64(new_snd_una, old_snd_una)) { |
| 1047 | msk->snd_una = new_snd_una; |
| 1048 | __mptcp_data_acked(sk); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1049 | } |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 1050 | mptcp_data_unlock(sk); |
Geliang Tang | ed66bfb | 2021-04-16 15:38:06 -0700 | [diff] [blame] | 1051 | |
| 1052 | trace_ack_update_msk(mp_opt->data_ack, |
| 1053 | old_snd_una, new_snd_una, |
| 1054 | new_wnd_end, msk->wnd_end); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Mat Martineau | 1a49b2c | 2020-09-29 15:08:20 -0700 | [diff] [blame] | 1057 | bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit) |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 1058 | { |
| 1059 | /* Skip if DATA_FIN was already received. |
| 1060 | * If updating simultaneously with the recvmsg loop, values |
| 1061 | * should match. If they mismatch, the peer is misbehaving and |
| 1062 | * we will prefer the most recent information. |
| 1063 | */ |
Paolo Abeni | 781bf13 | 2021-04-01 16:19:43 -0700 | [diff] [blame] | 1064 | if (READ_ONCE(msk->rcv_data_fin)) |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 1065 | return false; |
| 1066 | |
Mat Martineau | 1a49b2c | 2020-09-29 15:08:20 -0700 | [diff] [blame] | 1067 | WRITE_ONCE(msk->rcv_data_fin_seq, |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame] | 1068 | mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit)); |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 1069 | WRITE_ONCE(msk->rcv_data_fin, 1); |
| 1070 | |
| 1071 | return true; |
| 1072 | } |
| 1073 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 1074 | static bool add_addr_hmac_valid(struct mptcp_sock *msk, |
| 1075 | struct mptcp_options_received *mp_opt) |
| 1076 | { |
| 1077 | u64 hmac = 0; |
| 1078 | |
| 1079 | if (mp_opt->echo) |
| 1080 | return true; |
| 1081 | |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 1082 | hmac = add_addr_generate_hmac(msk->remote_key, |
| 1083 | msk->local_key, |
| 1084 | &mp_opt->addr); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 1085 | |
| 1086 | pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n", |
| 1087 | msk, (unsigned long long)hmac, |
| 1088 | (unsigned long long)mp_opt->ahmac); |
| 1089 | |
| 1090 | return hmac == mp_opt->ahmac; |
| 1091 | } |
| 1092 | |
Jianguo Wu | 6787b7e | 2021-07-09 17:20:49 -0700 | [diff] [blame] | 1093 | /* Return false if a subflow has been reset, else return true */ |
| 1094 | bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb) |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1095 | { |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 1096 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 1097 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1098 | struct mptcp_options_received mp_opt; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1099 | struct mptcp_ext *mpext; |
| 1100 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1101 | if (__mptcp_check_fallback(msk)) { |
| 1102 | /* Keep it simple and unconditionally trigger send data cleanup and |
| 1103 | * pending queue spooling. We will need to acquire the data lock |
| 1104 | * for more accurate checks, and once the lock is acquired, such |
| 1105 | * helpers are cheap. |
| 1106 | */ |
| 1107 | mptcp_data_lock(subflow->conn); |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 1108 | if (sk_stream_memory_free(sk)) |
| 1109 | __mptcp_check_push(subflow->conn, sk); |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1110 | __mptcp_data_acked(subflow->conn); |
| 1111 | mptcp_data_unlock(subflow->conn); |
Jianguo Wu | 6787b7e | 2021-07-09 17:20:49 -0700 | [diff] [blame] | 1112 | return true; |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1113 | } |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 1114 | |
Geliang Tang | c863225 | 2021-06-17 16:46:12 -0700 | [diff] [blame] | 1115 | mptcp_get_options(sk, skb, &mp_opt); |
Jianguo Wu | 6787b7e | 2021-07-09 17:20:49 -0700 | [diff] [blame] | 1116 | |
| 1117 | /* The subflow can be in close state only if check_fully_established() |
| 1118 | * just sent a reset. If so, tell the caller to ignore the current packet. |
| 1119 | */ |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1120 | if (!check_fully_established(msk, sk, subflow, skb, &mp_opt)) |
Jianguo Wu | 6787b7e | 2021-07-09 17:20:49 -0700 | [diff] [blame] | 1121 | return sk->sk_state != TCP_CLOSE; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1122 | |
Paolo Abeni | f6c2ef5 | 2021-08-26 17:44:53 -0700 | [diff] [blame] | 1123 | if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) { |
| 1124 | if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) && |
| 1125 | msk->local_key == mp_opt.rcvr_key) { |
| 1126 | WRITE_ONCE(msk->rcv_fastclose, true); |
| 1127 | mptcp_schedule_work((struct sock *)msk); |
Geliang Tang | a877de06 | 2020-09-24 08:29:51 +0800 | [diff] [blame] | 1128 | } |
Geliang Tang | 2fbdd9e | 2021-02-01 15:09:19 -0800 | [diff] [blame] | 1129 | |
Paolo Abeni | f6c2ef5 | 2021-08-26 17:44:53 -0700 | [diff] [blame] | 1130 | if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) && |
| 1131 | add_addr_hmac_valid(msk, &mp_opt)) { |
| 1132 | if (!mp_opt.echo) { |
| 1133 | mptcp_pm_add_addr_received(msk, &mp_opt.addr); |
| 1134 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR); |
| 1135 | } else { |
| 1136 | mptcp_pm_add_addr_echoed(msk, &mp_opt.addr); |
| 1137 | mptcp_pm_del_add_timer(msk, &mp_opt.addr, true); |
| 1138 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD); |
| 1139 | } |
| 1140 | |
| 1141 | if (mp_opt.addr.port) |
| 1142 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD); |
| 1143 | } |
| 1144 | |
| 1145 | if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR) |
| 1146 | mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list); |
| 1147 | |
| 1148 | if (mp_opt.suboptions & OPTION_MPTCP_PRIO) { |
| 1149 | mptcp_pm_mp_prio_received(sk, mp_opt.backup); |
| 1150 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX); |
| 1151 | } |
| 1152 | |
| 1153 | if (mp_opt.suboptions & OPTION_MPTCP_FAIL) { |
| 1154 | mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq); |
| 1155 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX); |
| 1156 | } |
| 1157 | |
| 1158 | if (mp_opt.suboptions & OPTION_MPTCP_RST) { |
| 1159 | subflow->reset_seen = 1; |
| 1160 | subflow->reset_reason = mp_opt.reset_reason; |
| 1161 | subflow->reset_transient = mp_opt.reset_transient; |
| 1162 | } |
| 1163 | |
| 1164 | if (!(mp_opt.suboptions & OPTION_MPTCP_DSS)) |
| 1165 | return true; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1168 | /* we can't wait for recvmsg() to update the ack_seq, otherwise |
| 1169 | * monodirectional flows will stuck |
| 1170 | */ |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1171 | if (mp_opt.use_ack) |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1172 | ack_update_msk(msk, sk, &mp_opt); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1173 | |
Mat Martineau | 06827b3 | 2020-07-28 15:12:08 -0700 | [diff] [blame] | 1174 | /* Zero-data-length packets are dropped by the caller and not |
| 1175 | * propagated to the MPTCP layer, so the skb extension does not |
| 1176 | * need to be allocated or populated. DATA_FIN information, if |
| 1177 | * present, needs to be updated here before the skb is freed. |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 1178 | */ |
| 1179 | if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) { |
| 1180 | if (mp_opt.data_fin && mp_opt.data_len == 1 && |
Mat Martineau | 1a49b2c | 2020-09-29 15:08:20 -0700 | [diff] [blame] | 1181 | mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) && |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 1182 | schedule_work(&msk->work)) |
| 1183 | sock_hold(subflow->conn); |
Mat Martineau | 06827b3 | 2020-07-28 15:12:08 -0700 | [diff] [blame] | 1184 | |
Jianguo Wu | 6787b7e | 2021-07-09 17:20:49 -0700 | [diff] [blame] | 1185 | return true; |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1188 | mpext = skb_ext_add(skb, SKB_EXT_MPTCP); |
| 1189 | if (!mpext) |
Jianguo Wu | 6787b7e | 2021-07-09 17:20:49 -0700 | [diff] [blame] | 1190 | return true; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1191 | |
| 1192 | memset(mpext, 0, sizeof(*mpext)); |
| 1193 | |
Paolo Abeni | f6c2ef5 | 2021-08-26 17:44:53 -0700 | [diff] [blame] | 1194 | if (likely(mp_opt.use_map)) { |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1195 | if (mp_opt.mpc_map) { |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1196 | /* this is an MP_CAPABLE carrying MPTCP data |
| 1197 | * we know this map the first chunk of data |
| 1198 | */ |
| 1199 | mptcp_crypto_key_sha(subflow->remote_key, NULL, |
| 1200 | &mpext->data_seq); |
| 1201 | mpext->data_seq++; |
| 1202 | mpext->subflow_seq = 1; |
| 1203 | mpext->dsn64 = 1; |
| 1204 | mpext->mpc_map = 1; |
Paolo Abeni | a77895d | 2020-04-30 15:01:55 +0200 | [diff] [blame] | 1205 | mpext->data_fin = 0; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1206 | } else { |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1207 | mpext->data_seq = mp_opt.data_seq; |
| 1208 | mpext->subflow_seq = mp_opt.subflow_seq; |
| 1209 | mpext->dsn64 = mp_opt.dsn64; |
| 1210 | mpext->data_fin = mp_opt.data_fin; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1211 | } |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1212 | mpext->data_len = mp_opt.data_len; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1213 | mpext->use_map = 1; |
Paolo Abeni | 74c7dfb | 2021-08-26 17:44:52 -0700 | [diff] [blame] | 1214 | mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD); |
Geliang Tang | 208e8f6 | 2021-06-17 16:46:14 -0700 | [diff] [blame] | 1215 | |
| 1216 | if (mpext->csum_reqd) |
| 1217 | mpext->csum = mp_opt.csum; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1218 | } |
Jianguo Wu | 6787b7e | 2021-07-09 17:20:49 -0700 | [diff] [blame] | 1219 | |
| 1220 | return true; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 1223 | static void mptcp_set_rwin(const struct tcp_sock *tp) |
| 1224 | { |
| 1225 | const struct sock *ssk = (const struct sock *)tp; |
| 1226 | const struct mptcp_subflow_context *subflow; |
| 1227 | struct mptcp_sock *msk; |
| 1228 | u64 ack_seq; |
| 1229 | |
| 1230 | subflow = mptcp_subflow_ctx(ssk); |
| 1231 | msk = mptcp_sk(subflow->conn); |
| 1232 | |
| 1233 | ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd; |
| 1234 | |
| 1235 | if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent))) |
| 1236 | WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); |
| 1237 | } |
| 1238 | |
Geliang Tang | c312ee2 | 2022-01-07 11:25:23 -0800 | [diff] [blame] | 1239 | u16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum) |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1240 | { |
| 1241 | struct csum_pseudo_header header; |
| 1242 | __wsum csum; |
| 1243 | |
| 1244 | /* cfr RFC 8684 3.3.1.: |
| 1245 | * the data sequence number used in the pseudo-header is |
| 1246 | * always the 64-bit value, irrespective of what length is used in the |
| 1247 | * DSS option itself. |
| 1248 | */ |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 1249 | header.data_seq = cpu_to_be64(data_seq); |
| 1250 | header.subflow_seq = htonl(subflow_seq); |
| 1251 | header.data_len = htons(data_len); |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1252 | header.csum = 0; |
| 1253 | |
Geliang Tang | c312ee2 | 2022-01-07 11:25:23 -0800 | [diff] [blame] | 1254 | csum = csum_partial(&header, sizeof(header), sum); |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1255 | return (__force u16)csum_fold(csum); |
| 1256 | } |
| 1257 | |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 1258 | static u16 mptcp_make_csum(const struct mptcp_ext *mpext) |
| 1259 | { |
| 1260 | return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len, |
Geliang Tang | c312ee2 | 2022-01-07 11:25:23 -0800 | [diff] [blame] | 1261 | ~csum_unfold(mpext->csum)); |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 1264 | void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp, |
| 1265 | struct mptcp_out_options *opts) |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1266 | { |
Geliang Tang | c25aeb4 | 2021-08-24 16:26:15 -0700 | [diff] [blame] | 1267 | if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) { |
| 1268 | const struct sock *ssk = (const struct sock *)tp; |
| 1269 | struct mptcp_subflow_context *subflow; |
| 1270 | |
| 1271 | subflow = mptcp_subflow_ctx(ssk); |
| 1272 | subflow->send_mp_fail = 0; |
| 1273 | |
| 1274 | *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL, |
| 1275 | TCPOLEN_MPTCP_FAIL, |
| 1276 | 0, 0); |
| 1277 | put_unaligned_be64(opts->fail_seq, ptr); |
| 1278 | ptr += 2; |
| 1279 | } |
| 1280 | |
Paolo Abeni | f284c0c | 2022-01-06 16:20:15 -0800 | [diff] [blame] | 1281 | /* DSS, MPC, MPJ, ADD_ADDR, FASTCLOSE and RST are mutually exclusive, |
| 1282 | * see mptcp_established_options*() |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 1283 | */ |
| 1284 | if (likely(OPTION_MPTCP_DSS & opts->suboptions)) { |
| 1285 | struct mptcp_ext *mpext = &opts->ext_copy; |
| 1286 | u8 len = TCPOLEN_MPTCP_DSS_BASE; |
| 1287 | u8 flags = 0; |
| 1288 | |
| 1289 | if (mpext->use_ack) { |
| 1290 | flags = MPTCP_DSS_HAS_ACK; |
| 1291 | if (mpext->ack64) { |
| 1292 | len += TCPOLEN_MPTCP_DSS_ACK64; |
| 1293 | flags |= MPTCP_DSS_ACK64; |
| 1294 | } else { |
| 1295 | len += TCPOLEN_MPTCP_DSS_ACK32; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | if (mpext->use_map) { |
| 1300 | len += TCPOLEN_MPTCP_DSS_MAP64; |
| 1301 | |
| 1302 | /* Use only 64-bit mapping flags for now, add |
| 1303 | * support for optional 32-bit mappings later. |
| 1304 | */ |
| 1305 | flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64; |
| 1306 | if (mpext->data_fin) |
| 1307 | flags |= MPTCP_DSS_DATA_FIN; |
| 1308 | |
| 1309 | if (opts->csum_reqd) |
| 1310 | len += TCPOLEN_MPTCP_DSS_CHECKSUM; |
| 1311 | } |
| 1312 | |
| 1313 | *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags); |
| 1314 | |
| 1315 | if (mpext->use_ack) { |
| 1316 | if (mpext->ack64) { |
| 1317 | put_unaligned_be64(mpext->data_ack, ptr); |
| 1318 | ptr += 2; |
| 1319 | } else { |
| 1320 | put_unaligned_be32(mpext->data_ack32, ptr); |
| 1321 | ptr += 1; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | if (mpext->use_map) { |
| 1326 | put_unaligned_be64(mpext->data_seq, ptr); |
| 1327 | ptr += 2; |
| 1328 | put_unaligned_be32(mpext->subflow_seq, ptr); |
| 1329 | ptr += 1; |
| 1330 | if (opts->csum_reqd) { |
| 1331 | put_unaligned_be32(mpext->data_len << 16 | |
| 1332 | mptcp_make_csum(mpext), ptr); |
| 1333 | } else { |
| 1334 | put_unaligned_be32(mpext->data_len << 16 | |
| 1335 | TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); |
| 1336 | } |
Geliang Tang | 110b6d1 | 2022-01-06 14:06:37 -0800 | [diff] [blame] | 1337 | ptr += 1; |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 1338 | } |
Geliang Tang | 13ac17a | 2021-09-24 14:12:35 -0700 | [diff] [blame] | 1339 | } else if (OPTIONS_MPTCP_MPC & opts->suboptions) { |
Geliang Tang | 06fe171 | 2021-06-17 16:46:09 -0700 | [diff] [blame] | 1340 | u8 len, flag = MPTCP_CAP_HMAC_SHA256; |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1341 | |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1342 | if (OPTION_MPTCP_MPC_SYN & opts->suboptions) { |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1343 | len = TCPOLEN_MPTCP_MPC_SYN; |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1344 | } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1345 | len = TCPOLEN_MPTCP_MPC_SYNACK; |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 1346 | } else if (opts->data_len) { |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1347 | len = TCPOLEN_MPTCP_MPC_ACK_DATA; |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1348 | if (opts->csum_reqd) |
| 1349 | len += TCPOLEN_MPTCP_DSS_CHECKSUM; |
| 1350 | } else { |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1351 | len = TCPOLEN_MPTCP_MPC_ACK; |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1352 | } |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1353 | |
Geliang Tang | 06fe171 | 2021-06-17 16:46:09 -0700 | [diff] [blame] | 1354 | if (opts->csum_reqd) |
| 1355 | flag |= MPTCP_CAP_CHECKSUM_REQD; |
| 1356 | |
Geliang Tang | bab6b88 | 2021-06-22 12:25:19 -0700 | [diff] [blame] | 1357 | if (!opts->allow_join_id0) |
| 1358 | flag |= MPTCP_CAP_DENY_JOIN_ID0; |
| 1359 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1360 | *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len, |
| 1361 | MPTCP_SUPPORTED_VERSION, |
Geliang Tang | 06fe171 | 2021-06-17 16:46:09 -0700 | [diff] [blame] | 1362 | flag); |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1363 | |
| 1364 | if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) & |
| 1365 | opts->suboptions)) |
| 1366 | goto mp_capable_done; |
| 1367 | |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1368 | put_unaligned_be64(opts->sndr_key, ptr); |
| 1369 | ptr += 2; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1370 | if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions)) |
| 1371 | goto mp_capable_done; |
| 1372 | |
| 1373 | put_unaligned_be64(opts->rcvr_key, ptr); |
| 1374 | ptr += 2; |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 1375 | if (!opts->data_len) |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1376 | goto mp_capable_done; |
| 1377 | |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1378 | if (opts->csum_reqd) { |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 1379 | put_unaligned_be32(opts->data_len << 16 | |
| 1380 | __mptcp_make_csum(opts->data_seq, |
| 1381 | opts->subflow_seq, |
| 1382 | opts->data_len, |
Geliang Tang | c312ee2 | 2022-01-07 11:25:23 -0800 | [diff] [blame] | 1383 | ~csum_unfold(opts->csum)), ptr); |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1384 | } else { |
Davide Caratti | f7cc889 | 2021-10-27 13:38:55 -0700 | [diff] [blame] | 1385 | put_unaligned_be32(opts->data_len << 16 | |
Geliang Tang | c94b1f9 | 2021-06-17 16:46:10 -0700 | [diff] [blame] | 1386 | TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); |
| 1387 | } |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1388 | ptr += 1; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1389 | |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 1390 | /* MPC is additionally mutually exclusive with MP_PRIO */ |
| 1391 | goto mp_capable_done; |
Paolo Abeni | 71b077e | 2022-01-06 16:20:21 -0800 | [diff] [blame] | 1392 | } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) { |
| 1393 | if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) { |
| 1394 | *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, |
| 1395 | TCPOLEN_MPTCP_MPJ_SYN, |
| 1396 | opts->backup, opts->join_id); |
| 1397 | put_unaligned_be32(opts->token, ptr); |
| 1398 | ptr += 1; |
| 1399 | put_unaligned_be32(opts->nonce, ptr); |
| 1400 | ptr += 1; |
| 1401 | } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) { |
| 1402 | *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, |
| 1403 | TCPOLEN_MPTCP_MPJ_SYNACK, |
| 1404 | opts->backup, opts->join_id); |
| 1405 | put_unaligned_be64(opts->thmac, ptr); |
| 1406 | ptr += 2; |
| 1407 | put_unaligned_be32(opts->nonce, ptr); |
| 1408 | ptr += 1; |
| 1409 | } else { |
| 1410 | *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, |
| 1411 | TCPOLEN_MPTCP_MPJ_ACK, 0, 0); |
| 1412 | memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN); |
| 1413 | ptr += 5; |
| 1414 | } |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 1415 | } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) { |
Geliang Tang | 6eb3d1e | 2020-12-09 15:51:18 -0800 | [diff] [blame] | 1416 | u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE; |
| 1417 | u8 echo = MPTCP_ADDR_ECHO; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1418 | |
| 1419 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 1420 | if (opts->addr.family == AF_INET6) |
Geliang Tang | e1ef683 | 2020-12-09 15:51:19 -0800 | [diff] [blame] | 1421 | len = TCPOLEN_MPTCP_ADD_ADDR6_BASE; |
| 1422 | #endif |
Geliang Tang | 6eb3d1e | 2020-12-09 15:51:18 -0800 | [diff] [blame] | 1423 | |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1424 | if (opts->addr.port) |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1425 | len += TCPOLEN_MPTCP_PORT_LEN; |
| 1426 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1427 | if (opts->ahmac) { |
Geliang Tang | 6eb3d1e | 2020-12-09 15:51:18 -0800 | [diff] [blame] | 1428 | len += sizeof(opts->ahmac); |
| 1429 | echo = 0; |
| 1430 | } |
Geliang Tang | e1ef683 | 2020-12-09 15:51:19 -0800 | [diff] [blame] | 1431 | |
Geliang Tang | 6eb3d1e | 2020-12-09 15:51:18 -0800 | [diff] [blame] | 1432 | *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR, |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1433 | len, echo, opts->addr.id); |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 1434 | if (opts->addr.family == AF_INET) { |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1435 | memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4); |
Geliang Tang | e1ef683 | 2020-12-09 15:51:19 -0800 | [diff] [blame] | 1436 | ptr += 1; |
| 1437 | } |
| 1438 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 1439 | else if (opts->addr.family == AF_INET6) { |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1440 | memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16); |
Geliang Tang | e1ef683 | 2020-12-09 15:51:19 -0800 | [diff] [blame] | 1441 | ptr += 4; |
| 1442 | } |
| 1443 | #endif |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1444 | |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1445 | if (!opts->addr.port) { |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1446 | if (opts->ahmac) { |
| 1447 | put_unaligned_be64(opts->ahmac, ptr); |
| 1448 | ptr += 2; |
| 1449 | } |
| 1450 | } else { |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1451 | u16 port = ntohs(opts->addr.port); |
| 1452 | |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1453 | if (opts->ahmac) { |
| 1454 | u8 *bptr = (u8 *)ptr; |
| 1455 | |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1456 | put_unaligned_be16(port, bptr); |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1457 | bptr += 2; |
| 1458 | put_unaligned_be64(opts->ahmac, bptr); |
| 1459 | bptr += 8; |
| 1460 | put_unaligned_be16(TCPOPT_NOP << 8 | |
| 1461 | TCPOPT_NOP, bptr); |
| 1462 | |
| 1463 | ptr += 3; |
| 1464 | } else { |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1465 | put_unaligned_be32(port << 16 | |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1466 | TCPOPT_NOP << 8 | |
| 1467 | TCPOPT_NOP, ptr); |
| 1468 | ptr += 1; |
| 1469 | } |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1470 | } |
Paolo Abeni | f284c0c | 2022-01-06 16:20:15 -0800 | [diff] [blame] | 1471 | } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) { |
| 1472 | /* FASTCLOSE is mutually exclusive with others except RST */ |
| 1473 | *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE, |
| 1474 | TCPOLEN_MPTCP_FASTCLOSE, |
| 1475 | 0, 0); |
| 1476 | put_unaligned_be64(opts->rcvr_key, ptr); |
| 1477 | ptr += 2; |
| 1478 | |
| 1479 | if (OPTION_MPTCP_RST & opts->suboptions) |
| 1480 | goto mp_rst; |
| 1481 | return; |
| 1482 | } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) { |
| 1483 | mp_rst: |
| 1484 | *ptr++ = mptcp_option(MPTCPOPT_RST, |
| 1485 | TCPOLEN_MPTCP_RST, |
| 1486 | opts->reset_transient, |
| 1487 | opts->reset_reason); |
| 1488 | return; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1489 | } |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1490 | |
Paolo Abeni | 1bff1e4 | 2021-08-24 16:26:13 -0700 | [diff] [blame] | 1491 | if (OPTION_MPTCP_PRIO & opts->suboptions) { |
| 1492 | const struct sock *ssk = (const struct sock *)tp; |
| 1493 | struct mptcp_subflow_context *subflow; |
| 1494 | |
| 1495 | subflow = mptcp_subflow_ctx(ssk); |
| 1496 | subflow->send_mp_prio = 0; |
| 1497 | |
| 1498 | *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO, |
| 1499 | TCPOLEN_MPTCP_PRIO, |
| 1500 | opts->backup, TCPOPT_NOP); |
| 1501 | } |
| 1502 | |
| 1503 | mp_capable_done: |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1504 | if (OPTION_MPTCP_RM_ADDR & opts->suboptions) { |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 1505 | u8 i = 1; |
| 1506 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1507 | *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR, |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 1508 | TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr, |
| 1509 | 0, opts->rm_list.ids[0]); |
| 1510 | |
| 1511 | while (i < opts->rm_list.nr) { |
| 1512 | u8 id1, id2, id3, id4; |
| 1513 | |
| 1514 | id1 = opts->rm_list.ids[i]; |
| 1515 | id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP; |
| 1516 | id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP; |
| 1517 | id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP; |
| 1518 | put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr); |
| 1519 | ptr += 1; |
| 1520 | i += 4; |
| 1521 | } |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 1524 | if (tp) |
| 1525 | mptcp_set_rwin(tp); |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1526 | } |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 1527 | |
| 1528 | __be32 mptcp_get_reset_option(const struct sk_buff *skb) |
| 1529 | { |
| 1530 | const struct mptcp_ext *ext = mptcp_get_ext(skb); |
| 1531 | u8 flags, reason; |
| 1532 | |
| 1533 | if (ext) { |
| 1534 | flags = ext->reset_transient; |
| 1535 | reason = ext->reset_reason; |
| 1536 | |
| 1537 | return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST, |
| 1538 | flags, reason); |
| 1539 | } |
| 1540 | |
| 1541 | return htonl(0u); |
| 1542 | } |
| 1543 | EXPORT_SYMBOL_GPL(mptcp_get_reset_option); |