blob: b87e46f515fb8bac86e38d09d8a1f3efa1a43c83 [file] [log] [blame]
Peter Krystadeda7acd2020-01-21 16:56:16 -08001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
Geliang Tangc85adce2020-04-03 17:14:08 +08007#define pr_fmt(fmt) "MPTCP: " fmt
8
Peter Krystadeda7acd2020-01-21 16:56:16 -08009#include <linux/kernel.h>
Eric Biggersa24d22b2020-11-12 21:20:21 -080010#include <crypto/sha2.h>
Peter Krystadeda7acd2020-01-21 16:56:16 -080011#include <net/tcp.h>
12#include <net/mptcp.h>
13#include "protocol.h"
Geliang Tanga877de062020-09-24 08:29:51 +080014#include "mib.h"
Peter Krystadeda7acd2020-01-21 16:56:16 -080015
Geliang Tanged66bfb2021-04-16 15:38:06 -070016#include <trace/events/mptcp.h>
17
Paolo Abeni65492c52020-01-21 16:56:30 -080018static bool mptcp_cap_flag_sha256(u8 flags)
19{
20 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
21}
22
Paolo Abenicfde1412020-04-30 15:01:52 +020023static void mptcp_parse_option(const struct sk_buff *skb,
24 const unsigned char *ptr, int opsize,
25 struct mptcp_options_received *mp_opt)
Peter Krystadeda7acd2020-01-21 16:56:16 -080026{
Peter Krystadeda7acd2020-01-21 16:56:16 -080027 u8 subtype = *ptr >> 4;
Mat Martineau648ef4b2020-01-21 16:56:24 -080028 int expected_opsize;
Peter Krystadeda7acd2020-01-21 16:56:16 -080029 u8 version;
30 u8 flags;
Geliang Tang5c4a8242021-03-12 17:16:13 -080031 u8 i;
Peter Krystadeda7acd2020-01-21 16:56:16 -080032
33 switch (subtype) {
34 case MPTCPOPT_MP_CAPABLE:
Christoph Paaschcc7972e2020-01-21 16:56:31 -080035 /* 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 Krystadeda7acd2020-01-21 16:56:16 -080048 break;
49
Christoph Paaschcc7972e2020-01-21 16:56:31 -080050 /* try to be gentle vs future versions on the initial syn */
Peter Krystadeda7acd2020-01-21 16:56:16 -080051 version = *ptr++ & MPTCP_VERSION_MASK;
Christoph Paaschcc7972e2020-01-21 16:56:31 -080052 if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
53 if (version != MPTCP_SUPPORTED_VERSION)
54 break;
55 } else if (version < MPTCP_SUPPORTED_VERSION) {
Peter Krystadeda7acd2020-01-21 16:56:16 -080056 break;
Christoph Paaschcc7972e2020-01-21 16:56:31 -080057 }
Peter Krystadeda7acd2020-01-21 16:56:16 -080058
59 flags = *ptr++;
Paolo Abeni65492c52020-01-21 16:56:30 -080060 if (!mptcp_cap_flag_sha256(flags) ||
Peter Krystadeda7acd2020-01-21 16:56:16 -080061 (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 Paaschcc7972e2020-01-21 16:56:31 -080081 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 Krystadeda7acd2020-01-21 16:56:16 -080086 mp_opt->rcvr_key = get_unaligned_be64(ptr);
87 ptr += 8;
Peter Krystadeda7acd2020-01-21 16:56:16 -080088 }
Christoph Paaschcc7972e2020-01-21 16:56:31 -080089 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 Krystadeda7acd2020-01-21 16:56:16 -0800104 break;
105
Peter Krystadf2962342020-03-27 14:48:39 -0700106 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 Krystadf2962342020-03-27 14:48:39 -0700133 mp_opt->mp_join = 0;
134 }
135 break;
136
Peter Krystadeda7acd2020-01-21 16:56:16 -0800137 case MPTCPOPT_DSS:
138 pr_debug("DSS");
Mat Martineau648ef4b2020-01-21 16:56:24 -0800139 ptr++;
140
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800141 /* 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 Martineau648ef4b2020-01-21 16:56:24 -0800146 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 Krystadeda7acd2020-01-21 16:56:16 -0800183 mp_opt->dss = 1;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800184
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 Krystadeda7acd2020-01-21 16:56:16 -0800217 break;
218
Peter Krystad3df523a2020-03-27 14:48:37 -0700219 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 Tang1b1a6ef2021-04-06 17:16:01 -0700224 mp_opt->addr.family = AF_INET;
Peter Krystad3df523a2020-03-27 14:48:37 -0700225#if IS_ENABLED(CONFIG_MPTCP_IPV6)
226 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
227 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700228 mp_opt->addr.family = AF_INET6;
Peter Krystad3df523a2020-03-27 14:48:37 -0700229#endif
230 else
231 break;
232 } else {
233 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
234 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700235 mp_opt->addr.family = AF_INET;
Peter Krystad3df523a2020-03-27 14:48:37 -0700236#if IS_ENABLED(CONFIG_MPTCP_IPV6)
237 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
238 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700239 mp_opt->addr.family = AF_INET6;
Peter Krystad3df523a2020-03-27 14:48:37 -0700240#endif
241 else
242 break;
243 }
244
245 mp_opt->add_addr = 1;
Geliang Tangf7dafee2021-04-06 17:16:00 -0700246 mp_opt->addr.id = *ptr++;
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700247 if (mp_opt->addr.family == AF_INET) {
Geliang Tangf7dafee2021-04-06 17:16:00 -0700248 memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
Peter Krystad3df523a2020-03-27 14:48:37 -0700249 ptr += 4;
250 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
251 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
Geliang Tangf7dafee2021-04-06 17:16:00 -0700252 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
Peter Krystad3df523a2020-03-27 14:48:37 -0700253 ptr += 2;
254 }
255 }
256#if IS_ENABLED(CONFIG_MPTCP_IPV6)
257 else {
Geliang Tangf7dafee2021-04-06 17:16:00 -0700258 memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
Peter Krystad3df523a2020-03-27 14:48:37 -0700259 ptr += 16;
260 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
261 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
Geliang Tangf7dafee2021-04-06 17:16:00 -0700262 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
Peter Krystad3df523a2020-03-27 14:48:37 -0700263 ptr += 2;
264 }
265 }
266#endif
267 if (!mp_opt->echo) {
268 mp_opt->ahmac = get_unaligned_be64(ptr);
269 ptr += 8;
270 }
Geliang Tang90a4aea2020-12-09 15:51:25 -0800271 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700272 (mp_opt->addr.family == AF_INET6) ? "6" : "",
Geliang Tangf7dafee2021-04-06 17:16:00 -0700273 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
Peter Krystad3df523a2020-03-27 14:48:37 -0700274 break;
275
276 case MPTCPOPT_RM_ADDR:
Geliang Tang5c4a8242021-03-12 17:16:13 -0800277 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
278 opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
Peter Krystad3df523a2020-03-27 14:48:37 -0700279 break;
280
Geliang Tang8e60eed2020-06-08 18:47:54 +0800281 ptr++;
282
Peter Krystad3df523a2020-03-27 14:48:37 -0700283 mp_opt->rm_addr = 1;
Geliang Tang5c4a8242021-03-12 17:16:13 -0800284 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 Krystad3df523a2020-03-27 14:48:37 -0700288 break;
289
Geliang Tang40453a52021-01-08 16:47:58 -0800290 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 Westphal50c504a2020-12-10 14:25:04 -0800299 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 Westphaldc87efd2021-04-01 16:19:44 -0700309 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 Krystadeda7acd2020-01-21 16:56:16 -0800321 default:
322 break;
323 }
324}
325
Peter Krystadcec37a62020-01-21 16:56:18 -0800326void mptcp_get_options(const struct sk_buff *skb,
Paolo Abenicfde1412020-04-30 15:01:52 +0200327 struct mptcp_options_received *mp_opt)
Peter Krystadcec37a62020-01-21 16:56:18 -0800328{
Peter Krystadcec37a62020-01-21 16:56:18 -0800329 const struct tcphdr *th = tcp_hdr(skb);
Paolo Abenicfde1412020-04-30 15:01:52 +0200330 const unsigned char *ptr;
331 int length;
Peter Krystadcec37a62020-01-21 16:56:18 -0800332
Paolo Abenicfde1412020-04-30 15:01:52 +0200333 /* initialize option status */
334 mp_opt->mp_capable = 0;
335 mp_opt->mp_join = 0;
336 mp_opt->add_addr = 0;
Geliang Tangfe2d9b12020-10-19 18:23:15 +0800337 mp_opt->ahmac = 0;
Florian Westphal50c504a2020-12-10 14:25:04 -0800338 mp_opt->fastclose = 0;
Geliang Tangf7dafee2021-04-06 17:16:00 -0700339 mp_opt->addr.port = 0;
Paolo Abenicfde1412020-04-30 15:01:52 +0200340 mp_opt->rm_addr = 0;
341 mp_opt->dss = 0;
Geliang Tang40453a52021-01-08 16:47:58 -0800342 mp_opt->mp_prio = 0;
Florian Westphaldc87efd2021-04-01 16:19:44 -0700343 mp_opt->reset = 0;
Paolo Abenicfde1412020-04-30 15:01:52 +0200344
345 length = (th->doff * 4) - sizeof(struct tcphdr);
Peter Krystadcec37a62020-01-21 16:56:18 -0800346 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 Mikityanskiy07718be2021-06-10 19:40:30 +0300359 if (length < 2)
360 return;
Peter Krystadcec37a62020-01-21 16:56:18 -0800361 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 Abenicfde1412020-04-30 15:01:52 +0200367 mptcp_parse_option(skb, ptr, opsize, mp_opt);
Peter Krystadcec37a62020-01-21 16:56:18 -0800368 ptr += opsize - 2;
369 length -= opsize;
370 }
371 }
372}
373
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800374bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
375 unsigned int *size, struct mptcp_out_options *opts)
Peter Krystadcec37a62020-01-21 16:56:18 -0800376{
377 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
378
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800379 /* 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 Krystadcec37a62020-01-21 16:56:18 -0800383 if (subflow->request_mptcp) {
Peter Krystadcec37a62020-01-21 16:56:18 -0800384 opts->suboptions = OPTION_MPTCP_MPC_SYN;
Peter Krystadcec37a62020-01-21 16:56:18 -0800385 *size = TCPOLEN_MPTCP_MPC_SYN;
386 return true;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700387 } 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 Krystadcec37a62020-01-21 16:56:18 -0800397 }
398 return false;
399}
400
Peter Krystadec3edaa2020-03-27 14:48:40 -0700401/* 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 */
405static 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
423static 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 Paaschcc7972e2020-01-21 16:56:31 -0800433static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
Paolo Abenid87903b2021-02-19 18:35:38 +0100434 bool snd_data_fin_enable,
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800435 unsigned int *size,
Mat Martineau6d0060f2020-01-21 16:56:23 -0800436 unsigned int remaining,
437 struct mptcp_out_options *opts)
Peter Krystadcec37a62020-01-21 16:56:18 -0800438{
439 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800440 struct mptcp_ext *mpext;
441 unsigned int data_len;
Peter Krystadcec37a62020-01-21 16:56:18 -0800442
Peter Krystadec3edaa2020-03-27 14:48:40 -0700443 /* 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 Paaschcc7972e2020-01-21 16:56:31 -0800451
Paolo Abenid87903b2021-02-19 18:35:38 +0100452 /* 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 Krystadec3edaa2020-03-27 14:48:40 -0700456 return false;
457
458 if (subflow->mp_capable) {
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800459 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 Krystadcec37a62020-01-21 16:56:18 -0800467 opts->suboptions = OPTION_MPTCP_MPC_ACK;
468 opts->sndr_key = subflow->local_key;
469 opts->rcvr_key = subflow->remote_key;
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800470
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 Krystadcec37a62020-01-21 16:56:18 -0800485 return true;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700486 } 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 Krystadcec37a62020-01-21 16:56:18 -0800494 }
495 return false;
496}
497
Mat Martineau6d0060f2020-01-21 16:56:23 -0800498static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
Paolo Abeni9c29e362020-07-03 18:06:04 +0200499 struct sk_buff *skb, struct mptcp_ext *ext)
Mat Martineau6d0060f2020-01-21 16:56:23 -0800500{
Paolo Abeni017512a2020-10-05 12:01:06 +0200501 /* 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 Martineau7279da62020-07-28 15:12:02 -0700505
Paolo Abeni9c29e362020-07-03 18:06:04 +0200506 if (!ext->use_map || !skb->len) {
Mat Martineau6d0060f2020-01-21 16:56:23 -0800507 /* RFC6824 requires a DSS mapping with specific values
508 * if DATA_FIN is set but no data payload is mapped
509 */
Mat Martineau6d37a0b2020-02-28 15:47:41 -0800510 ext->data_fin = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800511 ext->use_map = 1;
512 ext->dsn64 = 1;
Paolo Abeni017512a2020-10-05 12:01:06 +0200513 ext->data_seq = data_fin_tx_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800514 ext->subflow_seq = 0;
515 ext->data_len = 1;
Mat Martineau7279da62020-07-28 15:12:02 -0700516 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
Mat Martineau6d37a0b2020-02-28 15:47:41 -0800517 /* 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 Martineau6d0060f2020-01-21 16:56:23 -0800520 */
Mat Martineau6d37a0b2020-02-28 15:47:41 -0800521 ext->data_fin = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800522 ext->data_len++;
523 }
524}
525
526static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
Paolo Abenid87903b2021-02-19 18:35:38 +0100527 bool snd_data_fin_enable,
Mat Martineau6d0060f2020-01-21 16:56:23 -0800528 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 Martineau7279da62020-07-28 15:12:02 -0700533 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800534 unsigned int dss_size = 0;
535 struct mptcp_ext *mpext;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800536 unsigned int ack_size;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800537 bool ret = false;
Paolo Abenid87903b2021-02-19 18:35:38 +0100538 u64 ack_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800539
Mat Martineau0bac9662020-07-28 15:11:59 -0700540 mpext = skb ? mptcp_get_ext(skb) : NULL;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800541
Mat Martineau7279da62020-07-28 15:12:02 -0700542 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
Mat Martineau6d0060f2020-01-21 16:56:23 -0800543 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 Martineau7279da62020-07-28 15:12:02 -0700552 if (skb && snd_data_fin_enable)
Paolo Abeni9c29e362020-07-03 18:06:04 +0200553 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
Christoph Paaschd22f4982020-01-21 16:56:32 -0800554 ret = true;
555 }
556
Paolo Abeni2398e392020-03-04 16:51:07 +0100557 /* 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 Paaschd22f4982020-01-21 16:56:32 -0800560 opts->ext_copy.use_ack = 0;
Paolo Abenidc093db2020-03-13 16:52:42 +0100561 if (!READ_ONCE(msk->can_ack)) {
Christoph Paaschd22f4982020-01-21 16:56:32 -0800562 *size = ALIGN(dss_size, 4);
563 return ret;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800564 }
565
Paolo Abenie3859602021-02-11 15:30:41 -0800566 ack_seq = READ_ONCE(msk->ack_seq);
Davide Caratti37198e92020-10-06 18:26:17 +0200567 if (READ_ONCE(msk->use_64bit_ack)) {
Christoph Paascha0c1d0e2020-05-14 08:53:03 -0700568 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
Paolo Abenie3859602021-02-11 15:30:41 -0800569 opts->ext_copy.data_ack = ack_seq;
Christoph Paascha0c1d0e2020-05-14 08:53:03 -0700570 opts->ext_copy.ack64 = 1;
571 } else {
572 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
Paolo Abenie3859602021-02-11 15:30:41 -0800573 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
Christoph Paascha0c1d0e2020-05-14 08:53:03 -0700574 opts->ext_copy.ack64 = 0;
575 }
576 opts->ext_copy.use_ack = 1;
Paolo Abeniea4ca582020-11-19 11:46:03 -0800577 WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
Mat Martineau6d0060f2020-01-21 16:56:23 -0800578
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 Martineau6d0060f2020-01-21 16:56:23 -0800585 *size = ALIGN(dss_size, 4);
586 return true;
587}
588
Geliang Tang761c1242021-04-06 17:16:02 -0700589static u64 add_addr_generate_hmac(u64 key1, u64 key2,
590 struct mptcp_addr_info *addr)
Peter Krystad3df523a2020-03-27 14:48:37 -0700591{
Geliang Tang761c1242021-04-06 17:16:02 -0700592 u16 port = ntohs(addr->port);
Todd Malsbarybd697222020-05-21 19:10:49 -0700593 u8 hmac[SHA256_DIGEST_SIZE];
Peter Krystad3df523a2020-03-27 14:48:37 -0700594 u8 msg[19];
Geliang Tang761c1242021-04-06 17:16:02 -0700595 int i = 0;
Peter Krystad3df523a2020-03-27 14:48:37 -0700596
Geliang Tang761c1242021-04-06 17:16:02 -0700597 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 Krystad3df523a2020-03-27 14:48:37 -0700610
Geliang Tang761c1242021-04-06 17:16:02 -0700611 mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
Peter Krystad3df523a2020-03-27 14:48:37 -0700612
Todd Malsbarybd697222020-05-21 19:10:49 -0700613 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
Peter Krystad3df523a2020-03-27 14:48:37 -0700614}
Peter Krystad3df523a2020-03-27 14:48:37 -0700615
Geliang Tang84dfe362020-11-19 11:46:00 -0800616static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
Geliang Tangf643b802020-09-24 08:29:47 +0800617 unsigned int *size,
618 unsigned int remaining,
619 struct mptcp_out_options *opts)
Peter Krystad3df523a2020-03-27 14:48:37 -0700620{
621 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
622 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Geliang Tang84dfe362020-11-19 11:46:00 -0800623 bool drop_other_suboptions = false;
624 unsigned int opt_size = *size;
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800625 bool echo;
Geliang Tang4a2777a82020-12-09 15:51:22 -0800626 bool port;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700627 int len;
Peter Krystad3df523a2020-03-27 14:48:37 -0700628
Geliang Tangfbe0f872020-12-09 15:51:23 -0800629 if ((mptcp_pm_should_add_signal_ipv6(msk) ||
Davide Caratti07f82522021-04-06 17:16:04 -0700630 mptcp_pm_should_add_signal_port(msk) ||
631 mptcp_pm_should_add_signal_echo(msk)) &&
Geliang Tang84dfe362020-11-19 11:46:00 -0800632 skb && skb_is_tcp_pure_ack(skb)) {
633 pr_debug("drop other suboptions");
634 opts->suboptions = 0;
Geliang Tang3ae32c02020-12-15 17:56:51 +0800635 opts->ext_copy.use_ack = 0;
636 opts->ext_copy.use_map = 0;
Geliang Tang84dfe362020-11-19 11:46:00 -0800637 remaining += opt_size;
638 drop_other_suboptions = true;
639 }
640
Geliang Tangf643b802020-09-24 08:29:47 +0800641 if (!mptcp_pm_should_add_signal(msk) ||
Geliang Tang30f60ba2021-04-06 17:15:58 -0700642 !(mptcp_pm_add_addr_signal(msk, remaining, &opts->addr, &echo, &port)))
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700643 return false;
Peter Krystad3df523a2020-03-27 14:48:37 -0700644
Geliang Tang30f60ba2021-04-06 17:15:58 -0700645 len = mptcp_add_addr_len(opts->addr.family, echo, port);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700646 if (remaining < len)
647 return false;
648
649 *size = len;
Geliang Tang84dfe362020-11-19 11:46:00 -0800650 if (drop_other_suboptions)
651 *size -= opt_size;
Geliang Tangfef6b7e2021-04-06 17:15:59 -0700652 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
Geliang Tang761c1242021-04-06 17:16:02 -0700653 if (!echo) {
654 opts->ahmac = add_addr_generate_hmac(msk->local_key,
655 msk->remote_key,
656 &opts->addr);
Peter Krystad3df523a2020-03-27 14:48:37 -0700657 }
Geliang Tang4a2777a82020-12-09 15:51:22 -0800658 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
Geliang Tang30f60ba2021-04-06 17:15:58 -0700659 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
Peter Krystad3df523a2020-03-27 14:48:37 -0700660
661 return true;
662}
663
Geliang Tang5cb104a2020-09-24 08:29:48 +0800664static 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 Tang6445e172021-03-12 17:16:11 -0800671 struct mptcp_rm_list rm_list;
672 int i, len;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800673
674 if (!mptcp_pm_should_rm_signal(msk) ||
Geliang Tang6445e172021-03-12 17:16:11 -0800675 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
Geliang Tang5cb104a2020-09-24 08:29:48 +0800676 return false;
677
Geliang Tang6445e172021-03-12 17:16:11 -0800678 len = mptcp_rm_addr_len(&rm_list);
679 if (len < 0)
680 return false;
681 if (remaining < len)
Geliang Tang5cb104a2020-09-24 08:29:48 +0800682 return false;
683
Geliang Tang6445e172021-03-12 17:16:11 -0800684 *size = len;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800685 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
Geliang Tang6445e172021-03-12 17:16:11 -0800686 opts->rm_list = rm_list;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800687
Geliang Tang6445e172021-03-12 17:16:11 -0800688 for (i = 0; i < opts->rm_list.nr; i++)
689 pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
Geliang Tang5cb104a2020-09-24 08:29:48 +0800690
691 return true;
692}
693
Geliang Tang06706542021-01-08 16:47:57 -0800694static 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 Carattiec99a472021-02-01 14:05:26 +0100704 /* account for the trailing 'nop' option */
705 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
Geliang Tang06706542021-01-08 16:47:57 -0800706 return false;
707
Davide Carattiec99a472021-02-01 14:05:26 +0100708 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
Geliang Tang06706542021-01-08 16:47:57 -0800709 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 Westphaldc87efd2021-04-01 16:19:44 -0700717static 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 Martineau6d0060f2020-01-21 16:56:23 -0800733bool 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 Abenid87903b2021-02-19 18:35:38 +0100737 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
738 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800739 unsigned int opt_size = 0;
Paolo Abenid87903b2021-02-19 18:35:38 +0100740 bool snd_data_fin;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800741 bool ret = false;
742
Peter Krystad3df523a2020-03-27 14:48:37 -0700743 opts->suboptions = 0;
744
Paolo Abenid87903b2021-02-19 18:35:38 +0100745 if (unlikely(__mptcp_check_fallback(msk)))
Davide Carattie1ff9e82020-06-29 22:26:20 +0200746 return false;
747
Florian Westphaldc87efd2021-04-01 16:19:44 -0700748 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 Abenid5824842020-10-09 19:00:00 +0200752
Paolo Abenid87903b2021-02-19 18:35:38 +0100753 snd_data_fin = mptcp_data_fin_enabled(msk);
754 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
Mat Martineau6d0060f2020-01-21 16:56:23 -0800755 ret = true;
Paolo Abenid87903b2021-02-19 18:35:38 +0100756 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts))
Mat Martineau6d0060f2020-01-21 16:56:23 -0800757 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 Tang84dfe362020-11-19 11:46:00 -0800767 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
Peter Krystad3df523a2020-03-27 14:48:37 -0700768 *size += opt_size;
769 remaining -= opt_size;
770 ret = true;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800771 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
772 *size += opt_size;
773 remaining -= opt_size;
774 ret = true;
Peter Krystad3df523a2020-03-27 14:48:37 -0700775 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800776
Geliang Tang06706542021-01-08 16:47:57 -0800777 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 Martineau6d0060f2020-01-21 16:56:23 -0800783 return ret;
784}
785
Peter Krystadcec37a62020-01-21 16:56:18 -0800786bool 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 Krystadf2962342020-03-27 14:48:39 -0700798 } 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 Krystadcec37a62020-01-21 16:56:18 -0800809 }
810 return false;
811}
812
Paolo Abenid5824842020-10-09 19:00:00 +0200813static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
Peter Krystadf2962342020-03-27 14:48:39 -0700814 struct mptcp_subflow_context *subflow,
Paolo Abeni0be534f2020-03-19 11:06:30 +0100815 struct sk_buff *skb,
816 struct mptcp_options_received *mp_opt)
Christoph Paaschd22f4982020-01-21 16:56:32 -0800817{
818 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
Peter Krystadf2962342020-03-27 14:48:39 -0700819 * will make the subflow fully established
Christoph Paaschd22f4982020-01-21 16:56:32 -0800820 */
Peter Krystadf2962342020-03-27 14:48:39 -0700821 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 Abenid5824842020-10-09 19:00:00 +0200830 tcp_send_ack(ssk);
Peter Krystadf2962342020-03-27 14:48:39 -0700831 goto fully_established;
832 }
Christoph Paaschd22f4982020-01-21 16:56:32 -0800833
Paolo Abenid5824842020-10-09 19:00:00 +0200834 /* 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 Krystadf2962342020-03-27 14:48:39 -0700838 */
Paolo Abenid5824842020-10-09 19:00:00 +0200839 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
840 if (subflow->mp_join)
841 goto reset;
Peter Krystadf2962342020-03-27 14:48:39 -0700842 return subflow->mp_capable;
Paolo Abenid5824842020-10-09 19:00:00 +0200843 }
Peter Krystadf2962342020-03-27 14:48:39 -0700844
Paolo Abeni5a91e322020-04-30 15:01:54 +0200845 if (mp_opt->dss && mp_opt->use_ack) {
Peter Krystadf2962342020-03-27 14:48:39 -0700846 /* subflows are fully established as soon as we get any
847 * additional ack.
848 */
Paolo Abeni0be534f2020-03-19 11:06:30 +0100849 subflow->fully_established = 1;
Paolo Abenib93df082020-07-23 13:02:32 +0200850 WRITE_ONCE(msk->fully_established, true);
Peter Krystadf2962342020-03-27 14:48:39 -0700851 goto fully_established;
852 }
Christoph Paaschd22f4982020-01-21 16:56:32 -0800853
Geliang Tang84dfe362020-11-19 11:46:00 -0800854 if (mp_opt->add_addr) {
855 WRITE_ONCE(msk->fully_established, true);
856 return true;
857 }
858
Christoph Paaschd22f4982020-01-21 16:56:32 -0800859 /* If the first established packet does not contain MP_CAPABLE + data
Paolo Abenid5824842020-10-09 19:00:00 +0200860 * then fallback to TCP. Fallback scenarios requires a reset for
861 * MP_JOIN subflows.
Christoph Paaschd22f4982020-01-21 16:56:32 -0800862 */
863 if (!mp_opt->mp_capable) {
Paolo Abenid5824842020-10-09 19:00:00 +0200864 if (subflow->mp_join)
865 goto reset;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800866 subflow->mp_capable = 0;
Davide Carattie1ff9e82020-06-29 22:26:20 +0200867 pr_fallback(msk);
868 __mptcp_do_fallback(msk);
Christoph Paaschd22f4982020-01-21 16:56:32 -0800869 return false;
870 }
Peter Krystadf2962342020-03-27 14:48:39 -0700871
Paolo Abenid6085fe2020-04-30 15:01:53 +0200872 if (unlikely(!READ_ONCE(msk->pm.server_side)))
873 pr_warn_once("bogus mpc option on established client sk");
Paolo Abenib93df082020-07-23 13:02:32 +0200874 mptcp_subflow_fully_established(subflow, mp_opt);
Peter Krystadf2962342020-03-27 14:48:39 -0700875
876fully_established:
Paolo Abeni5b950ff2020-12-09 12:03:29 +0100877 /* 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 Krystadf2962342020-03-27 14:48:39 -0700883 return true;
884
885 subflow->pm_notified = 1;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700886 if (subflow->mp_join) {
Paolo Abenid5824842020-10-09 19:00:00 +0200887 clear_3rdack_retransmission(ssk);
Geliang Tang625352002021-03-26 11:26:33 -0700888 mptcp_pm_subflow_established(msk);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700889 } else {
Florian Westphal6c714f12021-02-12 15:59:58 -0800890 mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700891 }
Christoph Paaschd22f4982020-01-21 16:56:32 -0800892 return true;
Paolo Abenid5824842020-10-09 19:00:00 +0200893
894reset:
895 mptcp_subflow_reset(ssk);
896 return false;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800897}
898
Paolo Abeni15023282021-06-18 15:02:20 -0700899u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
Paolo Abenicc9d2562020-03-27 14:48:42 -0700900{
Paolo Abeni15023282021-06-18 15:02:20 -0700901 u32 old_seq32, cur_seq32;
Paolo Abenicc9d2562020-03-27 14:48:42 -0700902
Paolo Abeni15023282021-06-18 15:02:20 -0700903 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 Abenicc9d2562020-03-27 14:48:42 -0700908
Paolo Abeni15023282021-06-18 15:02:20 -0700909 /* 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 Abenicc9d2562020-03-27 14:48:42 -0700913}
914
Florian Westphal6f8a6122020-11-16 10:48:13 +0100915static void ack_update_msk(struct mptcp_sock *msk,
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100916 struct sock *ssk,
Florian Westphal6f8a6122020-11-16 10:48:13 +0100917 struct mptcp_options_received *mp_opt)
Paolo Abenicc9d2562020-03-27 14:48:42 -0700918{
Paolo Abeni7439d682020-11-27 11:10:26 +0100919 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
Florian Westphal6f8a6122020-11-16 10:48:13 +0100920 struct sock *sk = (struct sock *)msk;
Paolo Abeni7439d682020-11-27 11:10:26 +0100921 u64 old_snd_una;
922
923 mptcp_data_lock(sk);
Paolo Abenicc9d2562020-03-27 14:48:42 -0700924
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 Abeni7439d682020-11-27 11:10:26 +0100929 old_snd_una = msk->snd_una;
Paolo Abeni15023282021-06-18 15:02:20 -0700930 new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
Paolo Abenicc9d2562020-03-27 14:48:42 -0700931
932 /* ACK for data not even sent yet? Ignore. */
Paolo Abenieaa2ffa2020-11-16 10:48:08 +0100933 if (after64(new_snd_una, snd_nxt))
Paolo Abenicc9d2562020-03-27 14:48:42 -0700934 new_snd_una = old_snd_una;
935
Florian Westphal6f8a6122020-11-16 10:48:13 +0100936 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
937
Paolo Abeni219d0492020-12-16 12:48:34 +0100938 if (after64(new_wnd_end, msk->wnd_end))
Paolo Abeni7439d682020-11-27 11:10:26 +0100939 msk->wnd_end = new_wnd_end;
Paolo Abeni219d0492020-12-16 12:48:34 +0100940
941 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
Paolo Abenid09d8182021-02-11 15:30:42 -0800942 if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
Paolo Abeni219d0492020-12-16 12:48:34 +0100943 __mptcp_check_push(sk, ssk);
Florian Westphal6f8a6122020-11-16 10:48:13 +0100944
Paolo Abeni7439d682020-11-27 11:10:26 +0100945 if (after64(new_snd_una, old_snd_una)) {
946 msk->snd_una = new_snd_una;
947 __mptcp_data_acked(sk);
Paolo Abenicc9d2562020-03-27 14:48:42 -0700948 }
Paolo Abeni7439d682020-11-27 11:10:26 +0100949 mptcp_data_unlock(sk);
Geliang Tanged66bfb2021-04-16 15:38:06 -0700950
951 trace_ack_update_msk(mp_opt->data_ack,
952 old_snd_una, new_snd_una,
953 new_wnd_end, msk->wnd_end);
Paolo Abenicc9d2562020-03-27 14:48:42 -0700954}
955
Mat Martineau1a49b2c2020-09-29 15:08:20 -0700956bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
Mat Martineau3721b9b2020-07-28 15:12:03 -0700957{
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 Abeni781bf132021-04-01 16:19:43 -0700963 if (READ_ONCE(msk->rcv_data_fin))
Mat Martineau3721b9b2020-07-28 15:12:03 -0700964 return false;
965
Mat Martineau1a49b2c2020-09-29 15:08:20 -0700966 WRITE_ONCE(msk->rcv_data_fin_seq,
Paolo Abeni15023282021-06-18 15:02:20 -0700967 mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
Mat Martineau3721b9b2020-07-28 15:12:03 -0700968 WRITE_ONCE(msk->rcv_data_fin, 1);
969
970 return true;
971}
972
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700973static 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 Tang761c1242021-04-06 17:16:02 -0700981 hmac = add_addr_generate_hmac(msk->remote_key,
982 msk->local_key,
983 &mp_opt->addr);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700984
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 Westphal77d0cab2020-09-25 01:23:02 +0200992void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
Mat Martineau648ef4b2020-01-21 16:56:24 -0800993{
Christoph Paaschd22f4982020-01-21 16:56:32 -0800994 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700995 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Paolo Abenicfde1412020-04-30 15:01:52 +0200996 struct mptcp_options_received mp_opt;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800997 struct mptcp_ext *mpext;
998
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100999 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 Abeni219d0492020-12-16 12:48:34 +01001006 if (sk_stream_memory_free(sk))
1007 __mptcp_check_push(subflow->conn, sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001008 __mptcp_data_acked(subflow->conn);
1009 mptcp_data_unlock(subflow->conn);
Davide Carattie1ff9e82020-06-29 22:26:20 +02001010 return;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001011 }
Davide Carattie1ff9e82020-06-29 22:26:20 +02001012
Paolo Abenicfde1412020-04-30 15:01:52 +02001013 mptcp_get_options(skb, &mp_opt);
1014 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
Christoph Paaschd22f4982020-01-21 16:56:32 -08001015 return;
Mat Martineau648ef4b2020-01-21 16:56:24 -08001016
Florian Westphal50c504a2020-12-10 14:25:04 -08001017 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 Abenicfde1412020-04-30 15:01:52 +02001023 if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
Geliang Tanga877de062020-09-24 08:29:51 +08001024 if (!mp_opt.echo) {
Geliang Tangf7dafee2021-04-06 17:16:00 -07001025 mptcp_pm_add_addr_received(msk, &mp_opt.addr);
Geliang Tanga877de062020-09-24 08:29:51 +08001026 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1027 } else {
Geliang Tangf7dafee2021-04-06 17:16:00 -07001028 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
Davide Carattid58300c2021-05-25 14:23:13 -07001029 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
Geliang Tanga877de062020-09-24 08:29:51 +08001030 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1031 }
Geliang Tang2fbdd9e2021-02-01 15:09:19 -08001032
Geliang Tangf7dafee2021-04-06 17:16:00 -07001033 if (mp_opt.addr.port)
Geliang Tang2fbdd9e2021-02-01 15:09:19 -08001034 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1035
Paolo Abenicfde1412020-04-30 15:01:52 +02001036 mp_opt.add_addr = 0;
Peter Krystad1b1c7a02020-03-27 14:48:38 -07001037 }
1038
Geliang Tangd0876b22020-09-24 08:29:49 +08001039 if (mp_opt.rm_addr) {
Geliang Tang5c4a8242021-03-12 17:16:13 -08001040 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
Geliang Tangd0876b22020-09-24 08:29:49 +08001041 mp_opt.rm_addr = 0;
1042 }
1043
Geliang Tang40453a52021-01-08 16:47:58 -08001044 if (mp_opt.mp_prio) {
1045 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
Geliang Tang0be2ac22021-01-08 16:48:01 -08001046 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
Geliang Tang40453a52021-01-08 16:47:58 -08001047 mp_opt.mp_prio = 0;
1048 }
1049
Florian Westphaldc87efd2021-04-01 16:19:44 -07001050 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 Abenicfde1412020-04-30 15:01:52 +02001056 if (!mp_opt.dss)
Mat Martineau648ef4b2020-01-21 16:56:24 -08001057 return;
1058
Paolo Abenicc9d2562020-03-27 14:48:42 -07001059 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1060 * monodirectional flows will stuck
1061 */
Paolo Abenicfde1412020-04-30 15:01:52 +02001062 if (mp_opt.use_ack)
Florian Westphal6f8a6122020-11-16 10:48:13 +01001063 ack_update_msk(msk, sk, &mp_opt);
Paolo Abenicc9d2562020-03-27 14:48:42 -07001064
Mat Martineau06827b32020-07-28 15:12:08 -07001065 /* 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 Martineau43b54c62020-07-28 15:12:06 -07001069 */
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 Martineau1a49b2c2020-09-29 15:08:20 -07001072 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
Mat Martineau43b54c62020-07-28 15:12:06 -07001073 schedule_work(&msk->work))
1074 sock_hold(subflow->conn);
Mat Martineau06827b32020-07-28 15:12:08 -07001075
1076 return;
Mat Martineau43b54c62020-07-28 15:12:06 -07001077 }
1078
Mat Martineau648ef4b2020-01-21 16:56:24 -08001079 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1080 if (!mpext)
1081 return;
1082
1083 memset(mpext, 0, sizeof(*mpext));
1084
Paolo Abenicfde1412020-04-30 15:01:52 +02001085 if (mp_opt.use_map) {
1086 if (mp_opt.mpc_map) {
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001087 /* 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 Abenia77895d2020-04-30 15:01:55 +02001096 mpext->data_fin = 0;
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001097 } else {
Paolo Abenicfde1412020-04-30 15:01:52 +02001098 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 Paaschcc7972e2020-01-21 16:56:31 -08001102 }
Paolo Abenicfde1412020-04-30 15:01:52 +02001103 mpext->data_len = mp_opt.data_len;
Mat Martineau648ef4b2020-01-21 16:56:24 -08001104 mpext->use_map = 1;
Mat Martineau648ef4b2020-01-21 16:56:24 -08001105 }
Mat Martineau648ef4b2020-01-21 16:56:24 -08001106}
1107
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08001108static 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
1124void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1125 struct mptcp_out_options *opts)
Peter Krystadeda7acd2020-01-21 16:56:16 -08001126{
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001127 if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
Peter Krystadeda7acd2020-01-21 16:56:16 -08001128 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 Krystadcec37a62020-01-21 16:56:18 -08001133 else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
1134 len = TCPOLEN_MPTCP_MPC_SYNACK;
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001135 else if (opts->ext_copy.data_len)
1136 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
Peter Krystadeda7acd2020-01-21 16:56:16 -08001137 else
1138 len = TCPOLEN_MPTCP_MPC_ACK;
1139
Peter Krystad3df523a2020-03-27 14:48:37 -07001140 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1141 MPTCP_SUPPORTED_VERSION,
1142 MPTCP_CAP_HMAC_SHA256);
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001143
1144 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1145 opts->suboptions))
1146 goto mp_capable_done;
1147
Peter Krystadeda7acd2020-01-21 16:56:16 -08001148 put_unaligned_be64(opts->sndr_key, ptr);
1149 ptr += 2;
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001150 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 Krystadeda7acd2020-01-21 16:56:16 -08001161 }
Mat Martineau6d0060f2020-01-21 16:56:23 -08001162
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001163mp_capable_done:
Geliang Tangfef6b7e2021-04-06 17:15:59 -07001164 if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
Geliang Tang6eb3d1e2020-12-09 15:51:18 -08001165 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1166 u8 echo = MPTCP_ADDR_ECHO;
Peter Krystad3df523a2020-03-27 14:48:37 -07001167
1168#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Geliang Tangfef6b7e2021-04-06 17:15:59 -07001169 if (opts->addr.family == AF_INET6)
Geliang Tange1ef6832020-12-09 15:51:19 -08001170 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1171#endif
Geliang Tang6eb3d1e2020-12-09 15:51:18 -08001172
Geliang Tang30f60ba2021-04-06 17:15:58 -07001173 if (opts->addr.port)
Geliang Tang22fb85f2020-12-09 15:51:20 -08001174 len += TCPOLEN_MPTCP_PORT_LEN;
1175
Peter Krystad3df523a2020-03-27 14:48:37 -07001176 if (opts->ahmac) {
Geliang Tang6eb3d1e2020-12-09 15:51:18 -08001177 len += sizeof(opts->ahmac);
1178 echo = 0;
1179 }
Geliang Tange1ef6832020-12-09 15:51:19 -08001180
Geliang Tang6eb3d1e2020-12-09 15:51:18 -08001181 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
Geliang Tang30f60ba2021-04-06 17:15:58 -07001182 len, echo, opts->addr.id);
Geliang Tangfef6b7e2021-04-06 17:15:59 -07001183 if (opts->addr.family == AF_INET) {
Geliang Tang30f60ba2021-04-06 17:15:58 -07001184 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
Geliang Tange1ef6832020-12-09 15:51:19 -08001185 ptr += 1;
1186 }
1187#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Geliang Tangfef6b7e2021-04-06 17:15:59 -07001188 else if (opts->addr.family == AF_INET6) {
Geliang Tang30f60ba2021-04-06 17:15:58 -07001189 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
Geliang Tange1ef6832020-12-09 15:51:19 -08001190 ptr += 4;
1191 }
1192#endif
Geliang Tang22fb85f2020-12-09 15:51:20 -08001193
Geliang Tang30f60ba2021-04-06 17:15:58 -07001194 if (!opts->addr.port) {
Geliang Tang22fb85f2020-12-09 15:51:20 -08001195 if (opts->ahmac) {
1196 put_unaligned_be64(opts->ahmac, ptr);
1197 ptr += 2;
1198 }
1199 } else {
Geliang Tang30f60ba2021-04-06 17:15:58 -07001200 u16 port = ntohs(opts->addr.port);
1201
Geliang Tang22fb85f2020-12-09 15:51:20 -08001202 if (opts->ahmac) {
1203 u8 *bptr = (u8 *)ptr;
1204
Geliang Tang30f60ba2021-04-06 17:15:58 -07001205 put_unaligned_be16(port, bptr);
Geliang Tang22fb85f2020-12-09 15:51:20 -08001206 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 Tang30f60ba2021-04-06 17:15:58 -07001214 put_unaligned_be32(port << 16 |
Geliang Tang22fb85f2020-12-09 15:51:20 -08001215 TCPOPT_NOP << 8 |
1216 TCPOPT_NOP, ptr);
1217 ptr += 1;
1218 }
Peter Krystad3df523a2020-03-27 14:48:37 -07001219 }
1220 }
Peter Krystad3df523a2020-03-27 14:48:37 -07001221
1222 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
Geliang Tang6445e172021-03-12 17:16:11 -08001223 u8 i = 1;
1224
Peter Krystad3df523a2020-03-27 14:48:37 -07001225 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
Geliang Tang6445e172021-03-12 17:16:11 -08001226 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 Krystad3df523a2020-03-27 14:48:37 -07001240 }
1241
Geliang Tang06706542021-01-08 16:47:57 -08001242 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 Krystadec3edaa2020-03-27 14:48:40 -07001254 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 Krystadf2962342020-03-27 14:48:39 -07001264 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 Krystadec3edaa2020-03-27 14:48:40 -07001274 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 Westphaldc87efd2021-04-01 16:19:44 -07001281 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 Martineau6d0060f2020-01-21 16:56:23 -08001287 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 Paascha0c1d0e2020-05-14 08:53:03 -07001293 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 Martineau6d0060f2020-01-21 16:56:23 -08001300 }
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 Krystad3df523a2020-03-27 14:48:37 -07001313 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
Mat Martineau6d0060f2020-01-21 16:56:23 -08001314
1315 if (mpext->use_ack) {
Christoph Paascha0c1d0e2020-05-14 08:53:03 -07001316 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 Martineau6d0060f2020-01-21 16:56:23 -08001323 }
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 Westphalfa3fe2b2020-11-19 11:46:02 -08001334
1335 if (tp)
1336 mptcp_set_rwin(tp);
Peter Krystadeda7acd2020-01-21 16:56:16 -08001337}
Florian Westphaldc87efd2021-04-01 16:19:44 -07001338
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}
1354EXPORT_SYMBOL_GPL(mptcp_get_reset_option);