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