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