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