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