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: |
Maxim Mikityanskiy | 07718be | 2021-06-10 19:40:30 +0300 | [diff] [blame] | 359 | if (length < 2) |
| 360 | return; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 361 | opsize = *ptr++; |
| 362 | if (opsize < 2) /* "silly options" */ |
| 363 | return; |
| 364 | if (opsize > length) |
| 365 | return; /* don't parse partial options */ |
| 366 | if (opcode == TCPOPT_MPTCP) |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 367 | mptcp_parse_option(skb, ptr, opsize, mp_opt); |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 368 | ptr += opsize - 2; |
| 369 | length -= opsize; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 374 | bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb, |
| 375 | unsigned int *size, struct mptcp_out_options *opts) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 376 | { |
| 377 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 378 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 379 | /* we will use snd_isn to detect first pkt [re]transmission |
| 380 | * in mptcp_established_options_mp() |
| 381 | */ |
| 382 | subflow->snd_isn = TCP_SKB_CB(skb)->end_seq; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 383 | if (subflow->request_mptcp) { |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 384 | opts->suboptions = OPTION_MPTCP_MPC_SYN; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 385 | *size = TCPOLEN_MPTCP_MPC_SYN; |
| 386 | return true; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 387 | } else if (subflow->request_join) { |
| 388 | pr_debug("remote_token=%u, nonce=%u", subflow->remote_token, |
| 389 | subflow->local_nonce); |
| 390 | opts->suboptions = OPTION_MPTCP_MPJ_SYN; |
| 391 | opts->join_id = subflow->local_id; |
| 392 | opts->token = subflow->remote_token; |
| 393 | opts->nonce = subflow->local_nonce; |
| 394 | opts->backup = subflow->request_bkup; |
| 395 | *size = TCPOLEN_MPTCP_MPJ_SYN; |
| 396 | return true; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 397 | } |
| 398 | return false; |
| 399 | } |
| 400 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 401 | /* MP_JOIN client subflow must wait for 4th ack before sending any data: |
| 402 | * TCP can't schedule delack timer before the subflow is fully established. |
| 403 | * MPTCP uses the delack timer to do 3rd ack retransmissions |
| 404 | */ |
| 405 | static void schedule_3rdack_retransmission(struct sock *sk) |
| 406 | { |
| 407 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 408 | struct tcp_sock *tp = tcp_sk(sk); |
| 409 | unsigned long timeout; |
| 410 | |
| 411 | /* reschedule with a timeout above RTT, as we must look only for drop */ |
| 412 | if (tp->srtt_us) |
| 413 | timeout = tp->srtt_us << 1; |
| 414 | else |
| 415 | timeout = TCP_TIMEOUT_INIT; |
| 416 | |
| 417 | WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); |
| 418 | icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; |
| 419 | icsk->icsk_ack.timeout = timeout; |
| 420 | sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); |
| 421 | } |
| 422 | |
| 423 | static void clear_3rdack_retransmission(struct sock *sk) |
| 424 | { |
| 425 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 426 | |
| 427 | sk_stop_timer(sk, &icsk->icsk_delack_timer); |
| 428 | icsk->icsk_ack.timeout = 0; |
| 429 | icsk->icsk_ack.ato = 0; |
| 430 | icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER); |
| 431 | } |
| 432 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 433 | 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] | 434 | bool snd_data_fin_enable, |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 435 | unsigned int *size, |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 436 | unsigned int remaining, |
| 437 | struct mptcp_out_options *opts) |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 438 | { |
| 439 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 440 | struct mptcp_ext *mpext; |
| 441 | unsigned int data_len; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 442 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 443 | /* When skb is not available, we better over-estimate the emitted |
| 444 | * options len. A full DSS option (28 bytes) is longer than |
| 445 | * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so |
| 446 | * tell the caller to defer the estimate to |
| 447 | * mptcp_established_options_dss(), which will reserve enough space. |
| 448 | */ |
| 449 | if (!skb) |
| 450 | return false; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 451 | |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 452 | /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */ |
| 453 | if (subflow->fully_established || snd_data_fin_enable || |
| 454 | subflow->snd_isn != TCP_SKB_CB(skb)->seq || |
| 455 | sk->sk_state != TCP_ESTABLISHED) |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 456 | return false; |
| 457 | |
| 458 | if (subflow->mp_capable) { |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 459 | mpext = mptcp_get_ext(skb); |
| 460 | data_len = mpext ? mpext->data_len : 0; |
| 461 | |
| 462 | /* we will check ext_copy.data_len in mptcp_write_options() to |
| 463 | * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and |
| 464 | * TCPOLEN_MPTCP_MPC_ACK |
| 465 | */ |
| 466 | opts->ext_copy.data_len = data_len; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 467 | opts->suboptions = OPTION_MPTCP_MPC_ACK; |
| 468 | opts->sndr_key = subflow->local_key; |
| 469 | opts->rcvr_key = subflow->remote_key; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 470 | |
| 471 | /* Section 3.1. |
| 472 | * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK |
| 473 | * packets that start the first subflow of an MPTCP connection, |
| 474 | * as well as the first packet that carries data |
| 475 | */ |
| 476 | if (data_len > 0) |
| 477 | *size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4); |
| 478 | else |
| 479 | *size = TCPOLEN_MPTCP_MPC_ACK; |
| 480 | |
| 481 | pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d", |
| 482 | subflow, subflow->local_key, subflow->remote_key, |
| 483 | data_len); |
| 484 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 485 | return true; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 486 | } else if (subflow->mp_join) { |
| 487 | opts->suboptions = OPTION_MPTCP_MPJ_ACK; |
| 488 | memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN); |
| 489 | *size = TCPOLEN_MPTCP_MPJ_ACK; |
| 490 | pr_debug("subflow=%p", subflow); |
| 491 | |
| 492 | schedule_3rdack_retransmission(sk); |
| 493 | return true; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 494 | } |
| 495 | return false; |
| 496 | } |
| 497 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 498 | static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow, |
Paolo Abeni | 9c29e36 | 2020-07-03 18:06:04 +0200 | [diff] [blame] | 499 | struct sk_buff *skb, struct mptcp_ext *ext) |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 500 | { |
Paolo Abeni | 017512a | 2020-10-05 12:01:06 +0200 | [diff] [blame] | 501 | /* The write_seq value has already been incremented, so the actual |
| 502 | * sequence number for the DATA_FIN is one less. |
| 503 | */ |
| 504 | 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] | 505 | |
Paolo Abeni | 9c29e36 | 2020-07-03 18:06:04 +0200 | [diff] [blame] | 506 | if (!ext->use_map || !skb->len) { |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 507 | /* RFC6824 requires a DSS mapping with specific values |
| 508 | * if DATA_FIN is set but no data payload is mapped |
| 509 | */ |
Mat Martineau | 6d37a0b | 2020-02-28 15:47:41 -0800 | [diff] [blame] | 510 | ext->data_fin = 1; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 511 | ext->use_map = 1; |
| 512 | ext->dsn64 = 1; |
Paolo Abeni | 017512a | 2020-10-05 12:01:06 +0200 | [diff] [blame] | 513 | ext->data_seq = data_fin_tx_seq; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 514 | ext->subflow_seq = 0; |
| 515 | ext->data_len = 1; |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 516 | } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) { |
Mat Martineau | 6d37a0b | 2020-02-28 15:47:41 -0800 | [diff] [blame] | 517 | /* If there's an existing DSS mapping and it is the |
| 518 | * final mapping, DATA_FIN consumes 1 additional byte of |
| 519 | * mapping space. |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 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->data_len++; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | 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] | 527 | bool snd_data_fin_enable, |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 528 | unsigned int *size, |
| 529 | unsigned int remaining, |
| 530 | struct mptcp_out_options *opts) |
| 531 | { |
| 532 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 533 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 534 | unsigned int dss_size = 0; |
| 535 | struct mptcp_ext *mpext; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 536 | unsigned int ack_size; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 537 | bool ret = false; |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 538 | u64 ack_seq; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 539 | |
Mat Martineau | 0bac966 | 2020-07-28 15:11:59 -0700 | [diff] [blame] | 540 | mpext = skb ? mptcp_get_ext(skb) : NULL; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 541 | |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 542 | if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) { |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 543 | unsigned int map_size; |
| 544 | |
| 545 | map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64; |
| 546 | |
| 547 | remaining -= map_size; |
| 548 | dss_size = map_size; |
| 549 | if (mpext) |
| 550 | opts->ext_copy = *mpext; |
| 551 | |
Mat Martineau | 7279da6 | 2020-07-28 15:12:02 -0700 | [diff] [blame] | 552 | if (skb && snd_data_fin_enable) |
Paolo Abeni | 9c29e36 | 2020-07-03 18:06:04 +0200 | [diff] [blame] | 553 | mptcp_write_data_fin(subflow, skb, &opts->ext_copy); |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 554 | ret = true; |
| 555 | } |
| 556 | |
Paolo Abeni | 2398e39 | 2020-03-04 16:51:07 +0100 | [diff] [blame] | 557 | /* passive sockets msk will set the 'can_ack' after accept(), even |
| 558 | * if the first subflow may have the already the remote key handy |
| 559 | */ |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 560 | opts->ext_copy.use_ack = 0; |
Paolo Abeni | dc093db | 2020-03-13 16:52:42 +0100 | [diff] [blame] | 561 | if (!READ_ONCE(msk->can_ack)) { |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 562 | *size = ALIGN(dss_size, 4); |
| 563 | return ret; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 566 | ack_seq = READ_ONCE(msk->ack_seq); |
Davide Caratti | 37198e9 | 2020-10-06 18:26:17 +0200 | [diff] [blame] | 567 | if (READ_ONCE(msk->use_64bit_ack)) { |
Christoph Paasch | a0c1d0e | 2020-05-14 08:53:03 -0700 | [diff] [blame] | 568 | ack_size = TCPOLEN_MPTCP_DSS_ACK64; |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 569 | opts->ext_copy.data_ack = ack_seq; |
Christoph Paasch | a0c1d0e | 2020-05-14 08:53:03 -0700 | [diff] [blame] | 570 | opts->ext_copy.ack64 = 1; |
| 571 | } else { |
| 572 | ack_size = TCPOLEN_MPTCP_DSS_ACK32; |
Paolo Abeni | e385960 | 2021-02-11 15:30:41 -0800 | [diff] [blame] | 573 | opts->ext_copy.data_ack32 = (uint32_t)ack_seq; |
Christoph Paasch | a0c1d0e | 2020-05-14 08:53:03 -0700 | [diff] [blame] | 574 | opts->ext_copy.ack64 = 0; |
| 575 | } |
| 576 | opts->ext_copy.use_ack = 1; |
Paolo Abeni | ea4ca58 | 2020-11-19 11:46:03 -0800 | [diff] [blame] | 577 | WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk)); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 578 | |
| 579 | /* Add kind/length/subtype/flag overhead if mapping is not populated */ |
| 580 | if (dss_size == 0) |
| 581 | ack_size += TCPOLEN_MPTCP_DSS_BASE; |
| 582 | |
| 583 | dss_size += ack_size; |
| 584 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 585 | *size = ALIGN(dss_size, 4); |
| 586 | return true; |
| 587 | } |
| 588 | |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 589 | static u64 add_addr_generate_hmac(u64 key1, u64 key2, |
| 590 | struct mptcp_addr_info *addr) |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 591 | { |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 592 | u16 port = ntohs(addr->port); |
Todd Malsbary | bd69722 | 2020-05-21 19:10:49 -0700 | [diff] [blame] | 593 | u8 hmac[SHA256_DIGEST_SIZE]; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 594 | u8 msg[19]; |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 595 | int i = 0; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 596 | |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 597 | msg[i++] = addr->id; |
| 598 | if (addr->family == AF_INET) { |
| 599 | memcpy(&msg[i], &addr->addr.s_addr, 4); |
| 600 | i += 4; |
| 601 | } |
| 602 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
| 603 | else if (addr->family == AF_INET6) { |
| 604 | memcpy(&msg[i], &addr->addr6.s6_addr, 16); |
| 605 | i += 16; |
| 606 | } |
| 607 | #endif |
| 608 | msg[i++] = port >> 8; |
| 609 | msg[i++] = port & 0xFF; |
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 | mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 612 | |
Todd Malsbary | bd69722 | 2020-05-21 19:10:49 -0700 | [diff] [blame] | 613 | return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 614 | } |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 615 | |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 616 | 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] | 617 | unsigned int *size, |
| 618 | unsigned int remaining, |
| 619 | struct mptcp_out_options *opts) |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 620 | { |
| 621 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 622 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 623 | bool drop_other_suboptions = false; |
| 624 | unsigned int opt_size = *size; |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 625 | bool echo; |
Geliang Tang | 4a2777a8 | 2020-12-09 15:51:22 -0800 | [diff] [blame] | 626 | bool port; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 627 | int len; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 628 | |
Geliang Tang | fbe0f87 | 2020-12-09 15:51:23 -0800 | [diff] [blame] | 629 | if ((mptcp_pm_should_add_signal_ipv6(msk) || |
Davide Caratti | 07f8252 | 2021-04-06 17:16:04 -0700 | [diff] [blame] | 630 | mptcp_pm_should_add_signal_port(msk) || |
| 631 | mptcp_pm_should_add_signal_echo(msk)) && |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 632 | skb && skb_is_tcp_pure_ack(skb)) { |
| 633 | pr_debug("drop other suboptions"); |
| 634 | opts->suboptions = 0; |
Geliang Tang | 3ae32c0 | 2020-12-15 17:56:51 +0800 | [diff] [blame] | 635 | opts->ext_copy.use_ack = 0; |
| 636 | opts->ext_copy.use_map = 0; |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 637 | remaining += opt_size; |
| 638 | drop_other_suboptions = true; |
| 639 | } |
| 640 | |
Geliang Tang | f643b80 | 2020-09-24 08:29:47 +0800 | [diff] [blame] | 641 | if (!mptcp_pm_should_add_signal(msk) || |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 642 | !(mptcp_pm_add_addr_signal(msk, remaining, &opts->addr, &echo, &port))) |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 643 | return false; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 644 | |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 645 | len = mptcp_add_addr_len(opts->addr.family, echo, port); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 646 | if (remaining < len) |
| 647 | return false; |
| 648 | |
| 649 | *size = len; |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 650 | if (drop_other_suboptions) |
| 651 | *size -= opt_size; |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 652 | opts->suboptions |= OPTION_MPTCP_ADD_ADDR; |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 653 | if (!echo) { |
| 654 | opts->ahmac = add_addr_generate_hmac(msk->local_key, |
| 655 | msk->remote_key, |
| 656 | &opts->addr); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 657 | } |
Geliang Tang | 4a2777a8 | 2020-12-09 15:51:22 -0800 | [diff] [blame] | 658 | pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d", |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 659 | opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port)); |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 660 | |
| 661 | return true; |
| 662 | } |
| 663 | |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 664 | static bool mptcp_established_options_rm_addr(struct sock *sk, |
| 665 | unsigned int *size, |
| 666 | unsigned int remaining, |
| 667 | struct mptcp_out_options *opts) |
| 668 | { |
| 669 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 670 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 671 | struct mptcp_rm_list rm_list; |
| 672 | int i, len; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 673 | |
| 674 | if (!mptcp_pm_should_rm_signal(msk) || |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 675 | !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list))) |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 676 | return false; |
| 677 | |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 678 | len = mptcp_rm_addr_len(&rm_list); |
| 679 | if (len < 0) |
| 680 | return false; |
| 681 | if (remaining < len) |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 682 | return false; |
| 683 | |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 684 | *size = len; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 685 | opts->suboptions |= OPTION_MPTCP_RM_ADDR; |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 686 | opts->rm_list = rm_list; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 687 | |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 688 | for (i = 0; i < opts->rm_list.nr; i++) |
| 689 | 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] | 690 | |
| 691 | return true; |
| 692 | } |
| 693 | |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 694 | static bool mptcp_established_options_mp_prio(struct sock *sk, |
| 695 | unsigned int *size, |
| 696 | unsigned int remaining, |
| 697 | struct mptcp_out_options *opts) |
| 698 | { |
| 699 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 700 | |
| 701 | if (!subflow->send_mp_prio) |
| 702 | return false; |
| 703 | |
Davide Caratti | ec99a47 | 2021-02-01 14:05:26 +0100 | [diff] [blame] | 704 | /* account for the trailing 'nop' option */ |
| 705 | if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN) |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 706 | return false; |
| 707 | |
Davide Caratti | ec99a47 | 2021-02-01 14:05:26 +0100 | [diff] [blame] | 708 | *size = TCPOLEN_MPTCP_PRIO_ALIGN; |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 709 | opts->suboptions |= OPTION_MPTCP_PRIO; |
| 710 | opts->backup = subflow->request_bkup; |
| 711 | |
| 712 | pr_debug("prio=%d", opts->backup); |
| 713 | |
| 714 | return true; |
| 715 | } |
| 716 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 717 | static noinline void mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb, |
| 718 | unsigned int *size, |
| 719 | unsigned int remaining, |
| 720 | struct mptcp_out_options *opts) |
| 721 | { |
| 722 | const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 723 | |
| 724 | if (remaining < TCPOLEN_MPTCP_RST) |
| 725 | return; |
| 726 | |
| 727 | *size = TCPOLEN_MPTCP_RST; |
| 728 | opts->suboptions |= OPTION_MPTCP_RST; |
| 729 | opts->reset_transient = subflow->reset_transient; |
| 730 | opts->reset_reason = subflow->reset_reason; |
| 731 | } |
| 732 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 733 | bool mptcp_established_options(struct sock *sk, struct sk_buff *skb, |
| 734 | unsigned int *size, unsigned int remaining, |
| 735 | struct mptcp_out_options *opts) |
| 736 | { |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 737 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 738 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 739 | unsigned int opt_size = 0; |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 740 | bool snd_data_fin; |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 741 | bool ret = false; |
| 742 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 743 | opts->suboptions = 0; |
| 744 | |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 745 | if (unlikely(__mptcp_check_fallback(msk))) |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 746 | return false; |
| 747 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 748 | if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) { |
| 749 | mptcp_established_options_rst(sk, skb, size, remaining, opts); |
| 750 | return true; |
| 751 | } |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 752 | |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 753 | snd_data_fin = mptcp_data_fin_enabled(msk); |
| 754 | 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] | 755 | ret = true; |
Paolo Abeni | d87903b | 2021-02-19 18:35:38 +0100 | [diff] [blame] | 756 | 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] | 757 | ret = true; |
| 758 | |
| 759 | /* we reserved enough space for the above options, and exceeding the |
| 760 | * TCP option space would be fatal |
| 761 | */ |
| 762 | if (WARN_ON_ONCE(opt_size > remaining)) |
| 763 | return false; |
| 764 | |
| 765 | *size += opt_size; |
| 766 | remaining -= opt_size; |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 767 | if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) { |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 768 | *size += opt_size; |
| 769 | remaining -= opt_size; |
| 770 | ret = true; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 771 | } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) { |
| 772 | *size += opt_size; |
| 773 | remaining -= opt_size; |
| 774 | ret = true; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 775 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 776 | |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 777 | if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) { |
| 778 | *size += opt_size; |
| 779 | remaining -= opt_size; |
| 780 | ret = true; |
| 781 | } |
| 782 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 783 | return ret; |
| 784 | } |
| 785 | |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 786 | bool mptcp_synack_options(const struct request_sock *req, unsigned int *size, |
| 787 | struct mptcp_out_options *opts) |
| 788 | { |
| 789 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
| 790 | |
| 791 | if (subflow_req->mp_capable) { |
| 792 | opts->suboptions = OPTION_MPTCP_MPC_SYNACK; |
| 793 | opts->sndr_key = subflow_req->local_key; |
| 794 | *size = TCPOLEN_MPTCP_MPC_SYNACK; |
| 795 | pr_debug("subflow_req=%p, local_key=%llu", |
| 796 | subflow_req, subflow_req->local_key); |
| 797 | return true; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 798 | } else if (subflow_req->mp_join) { |
| 799 | opts->suboptions = OPTION_MPTCP_MPJ_SYNACK; |
| 800 | opts->backup = subflow_req->backup; |
| 801 | opts->join_id = subflow_req->local_id; |
| 802 | opts->thmac = subflow_req->thmac; |
| 803 | opts->nonce = subflow_req->local_nonce; |
| 804 | pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u", |
| 805 | subflow_req, opts->backup, opts->join_id, |
| 806 | opts->thmac, opts->nonce); |
| 807 | *size = TCPOLEN_MPTCP_MPJ_SYNACK; |
| 808 | return true; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 809 | } |
| 810 | return false; |
| 811 | } |
| 812 | |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 813 | static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk, |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 814 | struct mptcp_subflow_context *subflow, |
Paolo Abeni | 0be534f | 2020-03-19 11:06:30 +0100 | [diff] [blame] | 815 | struct sk_buff *skb, |
| 816 | struct mptcp_options_received *mp_opt) |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 817 | { |
| 818 | /* 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] | 819 | * will make the subflow fully established |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 820 | */ |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 821 | if (likely(subflow->fully_established)) { |
| 822 | /* on passive sockets, check for 3rd ack retransmission |
| 823 | * note that msk is always set by subflow_syn_recv_sock() |
| 824 | * for mp_join subflows |
| 825 | */ |
| 826 | if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 && |
| 827 | TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq && |
| 828 | subflow->mp_join && mp_opt->mp_join && |
| 829 | READ_ONCE(msk->pm.server_side)) |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 830 | tcp_send_ack(ssk); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 831 | goto fully_established; |
| 832 | } |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 833 | |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 834 | /* we must process OoO packets before the first subflow is fully |
| 835 | * established. OoO packets are instead a protocol violation |
| 836 | * for MP_JOIN subflows as the peer must not send any data |
| 837 | * before receiving the forth ack - cfr. RFC 8684 section 3.2. |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 838 | */ |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 839 | if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) { |
| 840 | if (subflow->mp_join) |
| 841 | goto reset; |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 842 | return subflow->mp_capable; |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 843 | } |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 844 | |
Paolo Abeni | 5a91e32 | 2020-04-30 15:01:54 +0200 | [diff] [blame] | 845 | if (mp_opt->dss && mp_opt->use_ack) { |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 846 | /* subflows are fully established as soon as we get any |
| 847 | * additional ack. |
| 848 | */ |
Paolo Abeni | 0be534f | 2020-03-19 11:06:30 +0100 | [diff] [blame] | 849 | subflow->fully_established = 1; |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 850 | WRITE_ONCE(msk->fully_established, true); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 851 | goto fully_established; |
| 852 | } |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 853 | |
Geliang Tang | 84dfe36 | 2020-11-19 11:46:00 -0800 | [diff] [blame] | 854 | if (mp_opt->add_addr) { |
| 855 | WRITE_ONCE(msk->fully_established, true); |
| 856 | return true; |
| 857 | } |
| 858 | |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 859 | /* If the first established packet does not contain MP_CAPABLE + data |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 860 | * then fallback to TCP. Fallback scenarios requires a reset for |
| 861 | * MP_JOIN subflows. |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 862 | */ |
| 863 | if (!mp_opt->mp_capable) { |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 864 | if (subflow->mp_join) |
| 865 | goto reset; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 866 | subflow->mp_capable = 0; |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 867 | pr_fallback(msk); |
| 868 | __mptcp_do_fallback(msk); |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 869 | return false; |
| 870 | } |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 871 | |
Paolo Abeni | d6085fe | 2020-04-30 15:01:53 +0200 | [diff] [blame] | 872 | if (unlikely(!READ_ONCE(msk->pm.server_side))) |
| 873 | pr_warn_once("bogus mpc option on established client sk"); |
Paolo Abeni | b93df08 | 2020-07-23 13:02:32 +0200 | [diff] [blame] | 874 | mptcp_subflow_fully_established(subflow, mp_opt); |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 875 | |
| 876 | fully_established: |
Paolo Abeni | 5b950ff | 2020-12-09 12:03:29 +0100 | [diff] [blame] | 877 | /* if the subflow is not already linked into the conn_list, we can't |
| 878 | * notify the PM: this subflow is still on the listener queue |
| 879 | * and the PM possibly acquiring the subflow lock could race with |
| 880 | * the listener close |
| 881 | */ |
| 882 | if (likely(subflow->pm_notified) || list_empty(&subflow->node)) |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 883 | return true; |
| 884 | |
| 885 | subflow->pm_notified = 1; |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 886 | if (subflow->mp_join) { |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 887 | clear_3rdack_retransmission(ssk); |
Geliang Tang | 62535200 | 2021-03-26 11:26:33 -0700 | [diff] [blame] | 888 | mptcp_pm_subflow_established(msk); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 889 | } else { |
Florian Westphal | 6c714f1 | 2021-02-12 15:59:58 -0800 | [diff] [blame] | 890 | mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC); |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 891 | } |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 892 | return true; |
Paolo Abeni | d582484 | 2020-10-09 19:00:00 +0200 | [diff] [blame] | 893 | |
| 894 | reset: |
| 895 | mptcp_subflow_reset(ssk); |
| 896 | return false; |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame^] | 899 | u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq) |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 900 | { |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame^] | 901 | u32 old_seq32, cur_seq32; |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 902 | |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame^] | 903 | old_seq32 = (u32)old_seq; |
| 904 | cur_seq32 = (u32)cur_seq; |
| 905 | cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32; |
| 906 | if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32))) |
| 907 | return cur_seq + (1LL << 32); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 908 | |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame^] | 909 | /* reverse wrap could happen, too */ |
| 910 | if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32))) |
| 911 | return cur_seq - (1LL << 32); |
| 912 | return cur_seq; |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 915 | static void ack_update_msk(struct mptcp_sock *msk, |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 916 | struct sock *ssk, |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 917 | struct mptcp_options_received *mp_opt) |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 918 | { |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 919 | 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] | 920 | struct sock *sk = (struct sock *)msk; |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 921 | u64 old_snd_una; |
| 922 | |
| 923 | mptcp_data_lock(sk); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 924 | |
| 925 | /* avoid ack expansion on update conflict, to reduce the risk of |
| 926 | * wrongly expanding to a future ack sequence number, which is way |
| 927 | * more dangerous than missing an ack |
| 928 | */ |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 929 | old_snd_una = msk->snd_una; |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame^] | 930 | 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] | 931 | |
| 932 | /* ACK for data not even sent yet? Ignore. */ |
Paolo Abeni | eaa2ffa | 2020-11-16 10:48:08 +0100 | [diff] [blame] | 933 | if (after64(new_snd_una, snd_nxt)) |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 934 | new_snd_una = old_snd_una; |
| 935 | |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 936 | new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd; |
| 937 | |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 938 | if (after64(new_wnd_end, msk->wnd_end)) |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 939 | msk->wnd_end = new_wnd_end; |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 940 | |
| 941 | /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */ |
Paolo Abeni | d09d818 | 2021-02-11 15:30:42 -0800 | [diff] [blame] | 942 | if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt))) |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 943 | __mptcp_check_push(sk, ssk); |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 944 | |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 945 | if (after64(new_snd_una, old_snd_una)) { |
| 946 | msk->snd_una = new_snd_una; |
| 947 | __mptcp_data_acked(sk); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 948 | } |
Paolo Abeni | 7439d68 | 2020-11-27 11:10:26 +0100 | [diff] [blame] | 949 | mptcp_data_unlock(sk); |
Geliang Tang | ed66bfb | 2021-04-16 15:38:06 -0700 | [diff] [blame] | 950 | |
| 951 | trace_ack_update_msk(mp_opt->data_ack, |
| 952 | old_snd_una, new_snd_una, |
| 953 | new_wnd_end, msk->wnd_end); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Mat Martineau | 1a49b2c | 2020-09-29 15:08:20 -0700 | [diff] [blame] | 956 | 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] | 957 | { |
| 958 | /* Skip if DATA_FIN was already received. |
| 959 | * If updating simultaneously with the recvmsg loop, values |
| 960 | * should match. If they mismatch, the peer is misbehaving and |
| 961 | * we will prefer the most recent information. |
| 962 | */ |
Paolo Abeni | 781bf13 | 2021-04-01 16:19:43 -0700 | [diff] [blame] | 963 | if (READ_ONCE(msk->rcv_data_fin)) |
Mat Martineau | 3721b9b | 2020-07-28 15:12:03 -0700 | [diff] [blame] | 964 | return false; |
| 965 | |
Mat Martineau | 1a49b2c | 2020-09-29 15:08:20 -0700 | [diff] [blame] | 966 | WRITE_ONCE(msk->rcv_data_fin_seq, |
Paolo Abeni | 1502328 | 2021-06-18 15:02:20 -0700 | [diff] [blame^] | 967 | 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] | 968 | WRITE_ONCE(msk->rcv_data_fin, 1); |
| 969 | |
| 970 | return true; |
| 971 | } |
| 972 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 973 | static bool add_addr_hmac_valid(struct mptcp_sock *msk, |
| 974 | struct mptcp_options_received *mp_opt) |
| 975 | { |
| 976 | u64 hmac = 0; |
| 977 | |
| 978 | if (mp_opt->echo) |
| 979 | return true; |
| 980 | |
Geliang Tang | 761c124 | 2021-04-06 17:16:02 -0700 | [diff] [blame] | 981 | hmac = add_addr_generate_hmac(msk->remote_key, |
| 982 | msk->local_key, |
| 983 | &mp_opt->addr); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 984 | |
| 985 | pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n", |
| 986 | msk, (unsigned long long)hmac, |
| 987 | (unsigned long long)mp_opt->ahmac); |
| 988 | |
| 989 | return hmac == mp_opt->ahmac; |
| 990 | } |
| 991 | |
Florian Westphal | 77d0cab | 2020-09-25 01:23:02 +0200 | [diff] [blame] | 992 | void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb) |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 993 | { |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 994 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 995 | struct mptcp_sock *msk = mptcp_sk(subflow->conn); |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 996 | struct mptcp_options_received mp_opt; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 997 | struct mptcp_ext *mpext; |
| 998 | |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 999 | if (__mptcp_check_fallback(msk)) { |
| 1000 | /* Keep it simple and unconditionally trigger send data cleanup and |
| 1001 | * pending queue spooling. We will need to acquire the data lock |
| 1002 | * for more accurate checks, and once the lock is acquired, such |
| 1003 | * helpers are cheap. |
| 1004 | */ |
| 1005 | mptcp_data_lock(subflow->conn); |
Paolo Abeni | 219d049 | 2020-12-16 12:48:34 +0100 | [diff] [blame] | 1006 | if (sk_stream_memory_free(sk)) |
| 1007 | __mptcp_check_push(subflow->conn, sk); |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1008 | __mptcp_data_acked(subflow->conn); |
| 1009 | mptcp_data_unlock(subflow->conn); |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 1010 | return; |
Paolo Abeni | 6e628cd | 2020-11-27 11:10:27 +0100 | [diff] [blame] | 1011 | } |
Davide Caratti | e1ff9e8 | 2020-06-29 22:26:20 +0200 | [diff] [blame] | 1012 | |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1013 | mptcp_get_options(skb, &mp_opt); |
| 1014 | if (!check_fully_established(msk, sk, subflow, skb, &mp_opt)) |
Christoph Paasch | d22f498 | 2020-01-21 16:56:32 -0800 | [diff] [blame] | 1015 | return; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1016 | |
Florian Westphal | 50c504a | 2020-12-10 14:25:04 -0800 | [diff] [blame] | 1017 | if (mp_opt.fastclose && |
| 1018 | msk->local_key == mp_opt.rcvr_key) { |
| 1019 | WRITE_ONCE(msk->rcv_fastclose, true); |
| 1020 | mptcp_schedule_work((struct sock *)msk); |
| 1021 | } |
| 1022 | |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1023 | if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) { |
Geliang Tang | a877de06 | 2020-09-24 08:29:51 +0800 | [diff] [blame] | 1024 | if (!mp_opt.echo) { |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 1025 | mptcp_pm_add_addr_received(msk, &mp_opt.addr); |
Geliang Tang | a877de06 | 2020-09-24 08:29:51 +0800 | [diff] [blame] | 1026 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR); |
| 1027 | } else { |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 1028 | mptcp_pm_add_addr_echoed(msk, &mp_opt.addr); |
Davide Caratti | d58300c | 2021-05-25 14:23:13 -0700 | [diff] [blame] | 1029 | mptcp_pm_del_add_timer(msk, &mp_opt.addr, true); |
Geliang Tang | a877de06 | 2020-09-24 08:29:51 +0800 | [diff] [blame] | 1030 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD); |
| 1031 | } |
Geliang Tang | 2fbdd9e | 2021-02-01 15:09:19 -0800 | [diff] [blame] | 1032 | |
Geliang Tang | f7dafee | 2021-04-06 17:16:00 -0700 | [diff] [blame] | 1033 | if (mp_opt.addr.port) |
Geliang Tang | 2fbdd9e | 2021-02-01 15:09:19 -0800 | [diff] [blame] | 1034 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD); |
| 1035 | |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1036 | mp_opt.add_addr = 0; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 1039 | if (mp_opt.rm_addr) { |
Geliang Tang | 5c4a824 | 2021-03-12 17:16:13 -0800 | [diff] [blame] | 1040 | mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list); |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 1041 | mp_opt.rm_addr = 0; |
| 1042 | } |
| 1043 | |
Geliang Tang | 40453a5 | 2021-01-08 16:47:58 -0800 | [diff] [blame] | 1044 | if (mp_opt.mp_prio) { |
| 1045 | mptcp_pm_mp_prio_received(sk, mp_opt.backup); |
Geliang Tang | 0be2ac2 | 2021-01-08 16:48:01 -0800 | [diff] [blame] | 1046 | MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX); |
Geliang Tang | 40453a5 | 2021-01-08 16:47:58 -0800 | [diff] [blame] | 1047 | mp_opt.mp_prio = 0; |
| 1048 | } |
| 1049 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 1050 | if (mp_opt.reset) { |
| 1051 | subflow->reset_seen = 1; |
| 1052 | subflow->reset_reason = mp_opt.reset_reason; |
| 1053 | subflow->reset_transient = mp_opt.reset_transient; |
| 1054 | } |
| 1055 | |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1056 | if (!mp_opt.dss) |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1057 | return; |
| 1058 | |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1059 | /* we can't wait for recvmsg() to update the ack_seq, otherwise |
| 1060 | * monodirectional flows will stuck |
| 1061 | */ |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1062 | if (mp_opt.use_ack) |
Florian Westphal | 6f8a612 | 2020-11-16 10:48:13 +0100 | [diff] [blame] | 1063 | ack_update_msk(msk, sk, &mp_opt); |
Paolo Abeni | cc9d256 | 2020-03-27 14:48:42 -0700 | [diff] [blame] | 1064 | |
Mat Martineau | 06827b3 | 2020-07-28 15:12:08 -0700 | [diff] [blame] | 1065 | /* Zero-data-length packets are dropped by the caller and not |
| 1066 | * propagated to the MPTCP layer, so the skb extension does not |
| 1067 | * need to be allocated or populated. DATA_FIN information, if |
| 1068 | * present, needs to be updated here before the skb is freed. |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 1069 | */ |
| 1070 | if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) { |
| 1071 | if (mp_opt.data_fin && mp_opt.data_len == 1 && |
Mat Martineau | 1a49b2c | 2020-09-29 15:08:20 -0700 | [diff] [blame] | 1072 | 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] | 1073 | schedule_work(&msk->work)) |
| 1074 | sock_hold(subflow->conn); |
Mat Martineau | 06827b3 | 2020-07-28 15:12:08 -0700 | [diff] [blame] | 1075 | |
| 1076 | return; |
Mat Martineau | 43b54c6 | 2020-07-28 15:12:06 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1079 | mpext = skb_ext_add(skb, SKB_EXT_MPTCP); |
| 1080 | if (!mpext) |
| 1081 | return; |
| 1082 | |
| 1083 | memset(mpext, 0, sizeof(*mpext)); |
| 1084 | |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1085 | if (mp_opt.use_map) { |
| 1086 | if (mp_opt.mpc_map) { |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1087 | /* this is an MP_CAPABLE carrying MPTCP data |
| 1088 | * we know this map the first chunk of data |
| 1089 | */ |
| 1090 | mptcp_crypto_key_sha(subflow->remote_key, NULL, |
| 1091 | &mpext->data_seq); |
| 1092 | mpext->data_seq++; |
| 1093 | mpext->subflow_seq = 1; |
| 1094 | mpext->dsn64 = 1; |
| 1095 | mpext->mpc_map = 1; |
Paolo Abeni | a77895d | 2020-04-30 15:01:55 +0200 | [diff] [blame] | 1096 | mpext->data_fin = 0; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1097 | } else { |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1098 | mpext->data_seq = mp_opt.data_seq; |
| 1099 | mpext->subflow_seq = mp_opt.subflow_seq; |
| 1100 | mpext->dsn64 = mp_opt.dsn64; |
| 1101 | mpext->data_fin = mp_opt.data_fin; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1102 | } |
Paolo Abeni | cfde141 | 2020-04-30 15:01:52 +0200 | [diff] [blame] | 1103 | mpext->data_len = mp_opt.data_len; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1104 | mpext->use_map = 1; |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1105 | } |
Mat Martineau | 648ef4b | 2020-01-21 16:56:24 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 1108 | static void mptcp_set_rwin(const struct tcp_sock *tp) |
| 1109 | { |
| 1110 | const struct sock *ssk = (const struct sock *)tp; |
| 1111 | const struct mptcp_subflow_context *subflow; |
| 1112 | struct mptcp_sock *msk; |
| 1113 | u64 ack_seq; |
| 1114 | |
| 1115 | subflow = mptcp_subflow_ctx(ssk); |
| 1116 | msk = mptcp_sk(subflow->conn); |
| 1117 | |
| 1118 | ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd; |
| 1119 | |
| 1120 | if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent))) |
| 1121 | WRITE_ONCE(msk->rcv_wnd_sent, ack_seq); |
| 1122 | } |
| 1123 | |
| 1124 | void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp, |
| 1125 | struct mptcp_out_options *opts) |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1126 | { |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1127 | if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK | |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1128 | OPTION_MPTCP_MPC_ACK) & opts->suboptions) { |
| 1129 | u8 len; |
| 1130 | |
| 1131 | if (OPTION_MPTCP_MPC_SYN & opts->suboptions) |
| 1132 | len = TCPOLEN_MPTCP_MPC_SYN; |
Peter Krystad | cec37a6 | 2020-01-21 16:56:18 -0800 | [diff] [blame] | 1133 | else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) |
| 1134 | len = TCPOLEN_MPTCP_MPC_SYNACK; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1135 | else if (opts->ext_copy.data_len) |
| 1136 | len = TCPOLEN_MPTCP_MPC_ACK_DATA; |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1137 | else |
| 1138 | len = TCPOLEN_MPTCP_MPC_ACK; |
| 1139 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1140 | *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len, |
| 1141 | MPTCP_SUPPORTED_VERSION, |
| 1142 | MPTCP_CAP_HMAC_SHA256); |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1143 | |
| 1144 | if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) & |
| 1145 | opts->suboptions)) |
| 1146 | goto mp_capable_done; |
| 1147 | |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1148 | put_unaligned_be64(opts->sndr_key, ptr); |
| 1149 | ptr += 2; |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1150 | if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions)) |
| 1151 | goto mp_capable_done; |
| 1152 | |
| 1153 | put_unaligned_be64(opts->rcvr_key, ptr); |
| 1154 | ptr += 2; |
| 1155 | if (!opts->ext_copy.data_len) |
| 1156 | goto mp_capable_done; |
| 1157 | |
| 1158 | put_unaligned_be32(opts->ext_copy.data_len << 16 | |
| 1159 | TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); |
| 1160 | ptr += 1; |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1161 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1162 | |
Christoph Paasch | cc7972e | 2020-01-21 16:56:31 -0800 | [diff] [blame] | 1163 | mp_capable_done: |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 1164 | if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) { |
Geliang Tang | 6eb3d1e | 2020-12-09 15:51:18 -0800 | [diff] [blame] | 1165 | u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE; |
| 1166 | u8 echo = MPTCP_ADDR_ECHO; |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1167 | |
| 1168 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 1169 | if (opts->addr.family == AF_INET6) |
Geliang Tang | e1ef683 | 2020-12-09 15:51:19 -0800 | [diff] [blame] | 1170 | len = TCPOLEN_MPTCP_ADD_ADDR6_BASE; |
| 1171 | #endif |
Geliang Tang | 6eb3d1e | 2020-12-09 15:51:18 -0800 | [diff] [blame] | 1172 | |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1173 | if (opts->addr.port) |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1174 | len += TCPOLEN_MPTCP_PORT_LEN; |
| 1175 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1176 | if (opts->ahmac) { |
Geliang Tang | 6eb3d1e | 2020-12-09 15:51:18 -0800 | [diff] [blame] | 1177 | len += sizeof(opts->ahmac); |
| 1178 | echo = 0; |
| 1179 | } |
Geliang Tang | e1ef683 | 2020-12-09 15:51:19 -0800 | [diff] [blame] | 1180 | |
Geliang Tang | 6eb3d1e | 2020-12-09 15:51:18 -0800 | [diff] [blame] | 1181 | *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR, |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1182 | len, echo, opts->addr.id); |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 1183 | if (opts->addr.family == AF_INET) { |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1184 | memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4); |
Geliang Tang | e1ef683 | 2020-12-09 15:51:19 -0800 | [diff] [blame] | 1185 | ptr += 1; |
| 1186 | } |
| 1187 | #if IS_ENABLED(CONFIG_MPTCP_IPV6) |
Geliang Tang | fef6b7e | 2021-04-06 17:15:59 -0700 | [diff] [blame] | 1188 | else if (opts->addr.family == AF_INET6) { |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1189 | memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16); |
Geliang Tang | e1ef683 | 2020-12-09 15:51:19 -0800 | [diff] [blame] | 1190 | ptr += 4; |
| 1191 | } |
| 1192 | #endif |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1193 | |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1194 | if (!opts->addr.port) { |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1195 | if (opts->ahmac) { |
| 1196 | put_unaligned_be64(opts->ahmac, ptr); |
| 1197 | ptr += 2; |
| 1198 | } |
| 1199 | } else { |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1200 | u16 port = ntohs(opts->addr.port); |
| 1201 | |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1202 | if (opts->ahmac) { |
| 1203 | u8 *bptr = (u8 *)ptr; |
| 1204 | |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1205 | put_unaligned_be16(port, bptr); |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1206 | bptr += 2; |
| 1207 | put_unaligned_be64(opts->ahmac, bptr); |
| 1208 | bptr += 8; |
| 1209 | put_unaligned_be16(TCPOPT_NOP << 8 | |
| 1210 | TCPOPT_NOP, bptr); |
| 1211 | |
| 1212 | ptr += 3; |
| 1213 | } else { |
Geliang Tang | 30f60ba | 2021-04-06 17:15:58 -0700 | [diff] [blame] | 1214 | put_unaligned_be32(port << 16 | |
Geliang Tang | 22fb85f | 2020-12-09 15:51:20 -0800 | [diff] [blame] | 1215 | TCPOPT_NOP << 8 | |
| 1216 | TCPOPT_NOP, ptr); |
| 1217 | ptr += 1; |
| 1218 | } |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1219 | } |
| 1220 | } |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1221 | |
| 1222 | if (OPTION_MPTCP_RM_ADDR & opts->suboptions) { |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 1223 | u8 i = 1; |
| 1224 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1225 | *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR, |
Geliang Tang | 6445e17 | 2021-03-12 17:16:11 -0800 | [diff] [blame] | 1226 | TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr, |
| 1227 | 0, opts->rm_list.ids[0]); |
| 1228 | |
| 1229 | while (i < opts->rm_list.nr) { |
| 1230 | u8 id1, id2, id3, id4; |
| 1231 | |
| 1232 | id1 = opts->rm_list.ids[i]; |
| 1233 | id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP; |
| 1234 | id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP; |
| 1235 | id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP; |
| 1236 | put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr); |
| 1237 | ptr += 1; |
| 1238 | i += 4; |
| 1239 | } |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Geliang Tang | 0670654 | 2021-01-08 16:47:57 -0800 | [diff] [blame] | 1242 | if (OPTION_MPTCP_PRIO & opts->suboptions) { |
| 1243 | const struct sock *ssk = (const struct sock *)tp; |
| 1244 | struct mptcp_subflow_context *subflow; |
| 1245 | |
| 1246 | subflow = mptcp_subflow_ctx(ssk); |
| 1247 | subflow->send_mp_prio = 0; |
| 1248 | |
| 1249 | *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO, |
| 1250 | TCPOLEN_MPTCP_PRIO, |
| 1251 | opts->backup, TCPOPT_NOP); |
| 1252 | } |
| 1253 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1254 | if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) { |
| 1255 | *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, |
| 1256 | TCPOLEN_MPTCP_MPJ_SYN, |
| 1257 | opts->backup, opts->join_id); |
| 1258 | put_unaligned_be32(opts->token, ptr); |
| 1259 | ptr += 1; |
| 1260 | put_unaligned_be32(opts->nonce, ptr); |
| 1261 | ptr += 1; |
| 1262 | } |
| 1263 | |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame] | 1264 | if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) { |
| 1265 | *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, |
| 1266 | TCPOLEN_MPTCP_MPJ_SYNACK, |
| 1267 | opts->backup, opts->join_id); |
| 1268 | put_unaligned_be64(opts->thmac, ptr); |
| 1269 | ptr += 2; |
| 1270 | put_unaligned_be32(opts->nonce, ptr); |
| 1271 | ptr += 1; |
| 1272 | } |
| 1273 | |
Peter Krystad | ec3edaa | 2020-03-27 14:48:40 -0700 | [diff] [blame] | 1274 | if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) { |
| 1275 | *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN, |
| 1276 | TCPOLEN_MPTCP_MPJ_ACK, 0, 0); |
| 1277 | memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN); |
| 1278 | ptr += 5; |
| 1279 | } |
| 1280 | |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 1281 | if (OPTION_MPTCP_RST & opts->suboptions) |
| 1282 | *ptr++ = mptcp_option(MPTCPOPT_RST, |
| 1283 | TCPOLEN_MPTCP_RST, |
| 1284 | opts->reset_transient, |
| 1285 | opts->reset_reason); |
| 1286 | |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1287 | if (opts->ext_copy.use_ack || opts->ext_copy.use_map) { |
| 1288 | struct mptcp_ext *mpext = &opts->ext_copy; |
| 1289 | u8 len = TCPOLEN_MPTCP_DSS_BASE; |
| 1290 | u8 flags = 0; |
| 1291 | |
| 1292 | if (mpext->use_ack) { |
Christoph Paasch | a0c1d0e | 2020-05-14 08:53:03 -0700 | [diff] [blame] | 1293 | flags = MPTCP_DSS_HAS_ACK; |
| 1294 | if (mpext->ack64) { |
| 1295 | len += TCPOLEN_MPTCP_DSS_ACK64; |
| 1296 | flags |= MPTCP_DSS_ACK64; |
| 1297 | } else { |
| 1298 | len += TCPOLEN_MPTCP_DSS_ACK32; |
| 1299 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | if (mpext->use_map) { |
| 1303 | len += TCPOLEN_MPTCP_DSS_MAP64; |
| 1304 | |
| 1305 | /* Use only 64-bit mapping flags for now, add |
| 1306 | * support for optional 32-bit mappings later. |
| 1307 | */ |
| 1308 | flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64; |
| 1309 | if (mpext->data_fin) |
| 1310 | flags |= MPTCP_DSS_DATA_FIN; |
| 1311 | } |
| 1312 | |
Peter Krystad | 3df523a | 2020-03-27 14:48:37 -0700 | [diff] [blame] | 1313 | *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags); |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 1314 | |
| 1315 | if (mpext->use_ack) { |
Christoph Paasch | a0c1d0e | 2020-05-14 08:53:03 -0700 | [diff] [blame] | 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 | } |
Mat Martineau | 6d0060f | 2020-01-21 16:56:23 -0800 | [diff] [blame] | 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 | put_unaligned_be32(mpext->data_len << 16 | |
| 1331 | TCPOPT_NOP << 8 | TCPOPT_NOP, ptr); |
| 1332 | } |
| 1333 | } |
Florian Westphal | fa3fe2b | 2020-11-19 11:46:02 -0800 | [diff] [blame] | 1334 | |
| 1335 | if (tp) |
| 1336 | mptcp_set_rwin(tp); |
Peter Krystad | eda7acd | 2020-01-21 16:56:16 -0800 | [diff] [blame] | 1337 | } |
Florian Westphal | dc87efd | 2021-04-01 16:19:44 -0700 | [diff] [blame] | 1338 | |
| 1339 | __be32 mptcp_get_reset_option(const struct sk_buff *skb) |
| 1340 | { |
| 1341 | const struct mptcp_ext *ext = mptcp_get_ext(skb); |
| 1342 | u8 flags, reason; |
| 1343 | |
| 1344 | if (ext) { |
| 1345 | flags = ext->reset_transient; |
| 1346 | reason = ext->reset_reason; |
| 1347 | |
| 1348 | return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST, |
| 1349 | flags, reason); |
| 1350 | } |
| 1351 | |
| 1352 | return htonl(0u); |
| 1353 | } |
| 1354 | EXPORT_SYMBOL_GPL(mptcp_get_reset_option); |