mptcp: fix opt size when sending DSS + MP_FAIL
[linux-2.6-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;
eda7acdd
PK
29 u8 version;
30 u8 flags;
5c4a824d 31 u8 i;
eda7acdd
PK
32
33 switch (subtype) {
34 case MPTCPOPT_MP_CAPABLE:
cc7972ea
CP
35 /* strict size checking */
36 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
37 if (skb->len > tcp_hdr(skb)->doff << 2)
38 expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
39 else
40 expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
41 } else {
42 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
43 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
44 else
45 expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
46 }
208e8f66
GT
47
48 /* Cfr RFC 8684 Section 3.3.0:
49 * If a checksum is present but its use had
50 * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
51 * close the subflow with a RST, as it is not behaving as negotiated.
52 * If a checksum is not present when its use has been negotiated, the
53 * receiver MUST close the subflow with a RST, as it is considered
54 * broken
55 * We parse even option with mismatching csum presence, so that
56 * later in subflow_data_ready we can trigger the reset.
57 */
58 if (opsize != expected_opsize &&
59 (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
60 opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
eda7acdd
PK
61 break;
62
cc7972ea 63 /* try to be gentle vs future versions on the initial syn */
eda7acdd 64 version = *ptr++ & MPTCP_VERSION_MASK;
cc7972ea
CP
65 if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
66 if (version != MPTCP_SUPPORTED_VERSION)
67 break;
68 } else if (version < MPTCP_SUPPORTED_VERSION) {
eda7acdd 69 break;
cc7972ea 70 }
eda7acdd
PK
71
72 flags = *ptr++;
65492c5a 73 if (!mptcp_cap_flag_sha256(flags) ||
eda7acdd
PK
74 (flags & MPTCP_CAP_EXTENSIBILITY))
75 break;
76
77 /* RFC 6824, Section 3.1:
78 * "For the Checksum Required bit (labeled "A"), if either
79 * host requires the use of checksums, checksums MUST be used.
80 * In other words, the only way for checksums not to be used
81 * is if both hosts in their SYNs set A=0."
eda7acdd
PK
82 */
83 if (flags & MPTCP_CAP_CHECKSUM_REQD)
74c7dfbe 84 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
eda7acdd 85
a086aeba 86 mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
df377be3 87
74c7dfbe 88 mp_opt->suboptions |= OPTIONS_MPTCP_MPC;
cc7972ea
CP
89 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
90 mp_opt->sndr_key = get_unaligned_be64(ptr);
91 ptr += 8;
92 }
93 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
eda7acdd
PK
94 mp_opt->rcvr_key = get_unaligned_be64(ptr);
95 ptr += 8;
eda7acdd 96 }
208e8f66 97 if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
cc7972ea
CP
98 /* Section 3.1.:
99 * "the data parameters in a MP_CAPABLE are semantically
100 * equivalent to those in a DSS option and can be used
101 * interchangeably."
102 */
74c7dfbe 103 mp_opt->suboptions |= OPTION_MPTCP_DSS;
cc7972ea
CP
104 mp_opt->use_map = 1;
105 mp_opt->mpc_map = 1;
106 mp_opt->data_len = get_unaligned_be16(ptr);
107 ptr += 2;
108 }
208e8f66
GT
109 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
110 mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
74c7dfbe 111 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
208e8f66
GT
112 ptr += 2;
113 }
114 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
cc7972ea 115 version, flags, opsize, mp_opt->sndr_key,
208e8f66 116 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
eda7acdd
PK
117 break;
118
f296234c 119 case MPTCPOPT_MP_JOIN:
74c7dfbe 120 mp_opt->suboptions |= OPTIONS_MPTCP_MPJ;
f296234c
PK
121 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
122 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
123 mp_opt->join_id = *ptr++;
124 mp_opt->token = get_unaligned_be32(ptr);
125 ptr += 4;
126 mp_opt->nonce = get_unaligned_be32(ptr);
127 ptr += 4;
128 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
129 mp_opt->backup, mp_opt->join_id,
130 mp_opt->token, mp_opt->nonce);
131 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
132 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
133 mp_opt->join_id = *ptr++;
134 mp_opt->thmac = get_unaligned_be64(ptr);
135 ptr += 8;
136 mp_opt->nonce = get_unaligned_be32(ptr);
137 ptr += 4;
138 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
139 mp_opt->backup, mp_opt->join_id,
140 mp_opt->thmac, mp_opt->nonce);
141 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
142 ptr += 2;
143 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
144 pr_debug("MP_JOIN hmac");
145 } else {
74c7dfbe 146 mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ;
f296234c
PK
147 }
148 break;
149
eda7acdd
PK
150 case MPTCPOPT_DSS:
151 pr_debug("DSS");
648ef4b8
MM
152 ptr++;
153
cc7972ea
CP
154 /* we must clear 'mpc_map' be able to detect MP_CAPABLE
155 * map vs DSS map in mptcp_incoming_options(), and reconstruct
156 * map info accordingly
157 */
158 mp_opt->mpc_map = 0;
648ef4b8
MM
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
166 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
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
204 pr_debug("data_ack=%llu", mp_opt->data_ack);
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;
390b95a5
GT
224 mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
225 ptr += 2;
226 }
227
228 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
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 }
90a4aea8 290 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
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++;
306 pr_debug("RM_ADDR: rm_list_nr=%d", 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
GT
314 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
315 pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
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;
50c504a2
FW
326 break;
327
dc87efdb
FW
328 case MPTCPOPT_RST:
329 if (opsize != TCPOLEN_MPTCP_RST)
330 break;
331
332 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
333 break;
74c7dfbe
PA
334
335 mp_opt->suboptions |= OPTION_MPTCP_RST;
dc87efdb
FW
336 flags = *ptr++;
337 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
338 mp_opt->reset_reason = *ptr;
339 break;
340
5580d41b
GT
341 case MPTCPOPT_MP_FAIL:
342 if (opsize != TCPOLEN_MPTCP_FAIL)
343 break;
344
345 ptr += 2;
74c7dfbe 346 mp_opt->suboptions |= OPTION_MPTCP_FAIL;
5580d41b
GT
347 mp_opt->fail_seq = get_unaligned_be64(ptr);
348 pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
349 break;
350
eda7acdd
PK
351 default:
352 break;
353 }
354}
355
c863225b
GT
356void mptcp_get_options(const struct sock *sk,
357 const struct sk_buff *skb,
cfde141e 358 struct mptcp_options_received *mp_opt)
cec37a6e 359{
cec37a6e 360 const struct tcphdr *th = tcp_hdr(skb);
cfde141e
PA
361 const unsigned char *ptr;
362 int length;
cec37a6e 363
cfde141e 364 /* initialize option status */
74c7dfbe 365 mp_opt->suboptions = 0;
cfde141e
PA
366
367 length = (th->doff * 4) - sizeof(struct tcphdr);
cec37a6e
PK
368 ptr = (const unsigned char *)(th + 1);
369
370 while (length > 0) {
371 int opcode = *ptr++;
372 int opsize;
373
374 switch (opcode) {
375 case TCPOPT_EOL:
376 return;
377 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
378 length--;
379 continue;
380 default:
07718be2
MM
381 if (length < 2)
382 return;
cec37a6e
PK
383 opsize = *ptr++;
384 if (opsize < 2) /* "silly options" */
385 return;
386 if (opsize > length)
387 return; /* don't parse partial options */
388 if (opcode == TCPOPT_MPTCP)
cfde141e 389 mptcp_parse_option(skb, ptr, opsize, mp_opt);
cec37a6e
PK
390 ptr += opsize - 2;
391 length -= opsize;
392 }
393 }
394}
395
cc7972ea
CP
396bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
397 unsigned int *size, struct mptcp_out_options *opts)
cec37a6e
PK
398{
399 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
400
cc7972ea
CP
401 /* we will use snd_isn to detect first pkt [re]transmission
402 * in mptcp_established_options_mp()
403 */
404 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
cec37a6e 405 if (subflow->request_mptcp) {
cec37a6e 406 opts->suboptions = OPTION_MPTCP_MPC_SYN;
06fe1719 407 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
bab6b88e 408 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
cec37a6e
PK
409 *size = TCPOLEN_MPTCP_MPC_SYN;
410 return true;
ec3edaa7
PK
411 } else if (subflow->request_join) {
412 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
413 subflow->local_nonce);
414 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
415 opts->join_id = subflow->local_id;
416 opts->token = subflow->remote_token;
417 opts->nonce = subflow->local_nonce;
418 opts->backup = subflow->request_bkup;
419 *size = TCPOLEN_MPTCP_MPJ_SYN;
420 return true;
cec37a6e
PK
421 }
422 return false;
423}
424
ec3edaa7
PK
425static void clear_3rdack_retransmission(struct sock *sk)
426{
427 struct inet_connection_sock *icsk = inet_csk(sk);
428
429 sk_stop_timer(sk, &icsk->icsk_delack_timer);
430 icsk->icsk_ack.timeout = 0;
431 icsk->icsk_ack.ato = 0;
432 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
433}
434
cc7972ea 435static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
d87903b6 436 bool snd_data_fin_enable,
cc7972ea 437 unsigned int *size,
6d0060f6
MM
438 unsigned int remaining,
439 struct mptcp_out_options *opts)
cec37a6e
PK
440{
441 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
06fe1719 442 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
cc7972ea
CP
443 struct mptcp_ext *mpext;
444 unsigned int data_len;
c94b1f96 445 u8 len;
cc7972ea 446
ec3edaa7
PK
447 /* When skb is not available, we better over-estimate the emitted
448 * options len. A full DSS option (28 bytes) is longer than
449 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
450 * tell the caller to defer the estimate to
451 * mptcp_established_options_dss(), which will reserve enough space.
452 */
453 if (!skb)
454 return false;
cc7972ea 455
d87903b6
PA
456 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
457 if (subflow->fully_established || snd_data_fin_enable ||
458 subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
459 sk->sk_state != TCP_ESTABLISHED)
ec3edaa7
PK
460 return false;
461
462 if (subflow->mp_capable) {
cc7972ea
CP
463 mpext = mptcp_get_ext(skb);
464 data_len = mpext ? mpext->data_len : 0;
cec37a6e 465
f7cc8890 466 /* we will check ops->data_len in mptcp_write_options() to
cc7972ea
CP
467 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
468 * TCPOLEN_MPTCP_MPC_ACK
469 */
f7cc8890 470 opts->data_len = data_len;
cec37a6e
PK
471 opts->suboptions = OPTION_MPTCP_MPC_ACK;
472 opts->sndr_key = subflow->local_key;
473 opts->rcvr_key = subflow->remote_key;
06fe1719 474 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
bab6b88e 475 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
cc7972ea
CP
476
477 /* Section 3.1.
478 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
479 * packets that start the first subflow of an MPTCP connection,
480 * as well as the first packet that carries data
481 */
c94b1f96
GT
482 if (data_len > 0) {
483 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
484 if (opts->csum_reqd) {
c5b39e26 485 /* we need to propagate more info to csum the pseudo hdr */
f7cc8890
DC
486 opts->data_seq = mpext->data_seq;
487 opts->subflow_seq = mpext->subflow_seq;
488 opts->csum = mpext->csum;
c94b1f96
GT
489 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
490 }
491 *size = ALIGN(len, 4);
492 } else {
cc7972ea 493 *size = TCPOLEN_MPTCP_MPC_ACK;
c94b1f96 494 }
cc7972ea
CP
495
496 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
497 subflow, subflow->local_key, subflow->remote_key,
498 data_len);
499
cec37a6e 500 return true;
ec3edaa7
PK
501 } else if (subflow->mp_join) {
502 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
503 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
504 *size = TCPOLEN_MPTCP_MPJ_ACK;
505 pr_debug("subflow=%p", subflow);
506
bcd97734
PA
507 /* we can use the full delegate action helper only from BH context
508 * If we are in process context - sk is flushing the backlog at
509 * socket lock release time - just set the appropriate flag, will
510 * be handled by the release callback
511 */
512 if (sock_owned_by_user(sk))
513 set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
514 else
515 mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
ec3edaa7 516 return true;
cec37a6e
PK
517 }
518 return false;
519}
520
6d0060f6 521static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
9c29e361 522 struct sk_buff *skb, struct mptcp_ext *ext)
6d0060f6 523{
017512a0
PA
524 /* The write_seq value has already been incremented, so the actual
525 * sequence number for the DATA_FIN is one less.
526 */
527 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
7279da61 528
9c29e361 529 if (!ext->use_map || !skb->len) {
6d0060f6
MM
530 /* RFC6824 requires a DSS mapping with specific values
531 * if DATA_FIN is set but no data payload is mapped
532 */
6d37a0b8 533 ext->data_fin = 1;
6d0060f6
MM
534 ext->use_map = 1;
535 ext->dsn64 = 1;
017512a0 536 ext->data_seq = data_fin_tx_seq;
6d0060f6
MM
537 ext->subflow_seq = 0;
538 ext->data_len = 1;
7279da61 539 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
6d37a0b8
MM
540 /* If there's an existing DSS mapping and it is the
541 * final mapping, DATA_FIN consumes 1 additional byte of
542 * mapping space.
6d0060f6 543 */
6d37a0b8 544 ext->data_fin = 1;
6d0060f6
MM
545 ext->data_len++;
546 }
547}
548
549static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
d87903b6 550 bool snd_data_fin_enable,
6d0060f6
MM
551 unsigned int *size,
552 unsigned int remaining,
553 struct mptcp_out_options *opts)
554{
555 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
7279da61 556 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
6d0060f6
MM
557 unsigned int dss_size = 0;
558 struct mptcp_ext *mpext;
6d0060f6 559 unsigned int ack_size;
d22f4988 560 bool ret = false;
d87903b6 561 u64 ack_seq;
6d0060f6 562
c5b39e26 563 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
0bac966a 564 mpext = skb ? mptcp_get_ext(skb) : NULL;
6d0060f6 565
7279da61 566 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
c5b39e26 567 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
6d0060f6 568
c5b39e26
GT
569 if (mpext) {
570 if (opts->csum_reqd)
571 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
6d0060f6 572
6d0060f6 573 opts->ext_copy = *mpext;
c5b39e26 574 }
6d0060f6 575
c5b39e26
GT
576 remaining -= map_size;
577 dss_size = map_size;
7279da61 578 if (skb && snd_data_fin_enable)
9c29e361 579 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
1bff1e43 580 opts->suboptions = OPTION_MPTCP_DSS;
d22f4988
CP
581 ret = true;
582 }
583
2398e399
PA
584 /* passive sockets msk will set the 'can_ack' after accept(), even
585 * if the first subflow may have the already the remote key handy
586 */
d22f4988 587 opts->ext_copy.use_ack = 0;
dc093db5 588 if (!READ_ONCE(msk->can_ack)) {
d22f4988
CP
589 *size = ALIGN(dss_size, 4);
590 return ret;
6d0060f6
MM
591 }
592
e3859603 593 ack_seq = READ_ONCE(msk->ack_seq);
37198e93 594 if (READ_ONCE(msk->use_64bit_ack)) {
a0c1d0ea 595 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
e3859603 596 opts->ext_copy.data_ack = ack_seq;
a0c1d0ea
CP
597 opts->ext_copy.ack64 = 1;
598 } else {
599 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
e3859603 600 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
a0c1d0ea
CP
601 opts->ext_copy.ack64 = 0;
602 }
603 opts->ext_copy.use_ack = 1;
1bff1e43 604 opts->suboptions = OPTION_MPTCP_DSS;
ea4ca586 605 WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
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;
6a6c05a8 653 bool echo;
4a2777a8 654 bool port;
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
YL
661 (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
662 !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
663 &echo, &port, &drop_other_suboptions))
1b1c7a0e
PK
664 return false;
665
1f5e9e2f
YL
666 if (drop_other_suboptions)
667 remaining += opt_size;
30f60bae 668 len = mptcp_add_addr_len(opts->addr.family, echo, port);
1b1c7a0e
PK
669 if (remaining < len)
670 return false;
3df523ab 671
1b1c7a0e 672 *size = len;
1f5e9e2f
YL
673 if (drop_other_suboptions) {
674 pr_debug("drop other suboptions");
675 opts->suboptions = 0;
1bff1e43
PA
676
677 /* note that e.g. DSS could have written into the memory
678 * aliased by ahmac, we must reset the field here
679 * to avoid appending the hmac even for ADD_ADDR echo
680 * options
681 */
682 opts->ahmac = 0;
84dfe367 683 *size -= opt_size;
1f5e9e2f 684 }
fef6b7ec 685 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
761c124e
GT
686 if (!echo) {
687 opts->ahmac = add_addr_generate_hmac(msk->local_key,
688 msk->remote_key,
689 &opts->addr);
3df523ab 690 }
4a2777a8 691 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
30f60bae 692 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
3df523ab
PK
693
694 return true;
695}
696
5cb104ae
GT
697static bool mptcp_established_options_rm_addr(struct sock *sk,
698 unsigned int *size,
699 unsigned int remaining,
700 struct mptcp_out_options *opts)
701{
702 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
703 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
6445e17a
GT
704 struct mptcp_rm_list rm_list;
705 int i, len;
5cb104ae
GT
706
707 if (!mptcp_pm_should_rm_signal(msk) ||
6445e17a 708 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
5cb104ae
GT
709 return false;
710
6445e17a
GT
711 len = mptcp_rm_addr_len(&rm_list);
712 if (len < 0)
713 return false;
714 if (remaining < len)
5cb104ae
GT
715 return false;
716
6445e17a 717 *size = len;
5cb104ae 718 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
6445e17a 719 opts->rm_list = rm_list;
5cb104ae 720
6445e17a
GT
721 for (i = 0; i < opts->rm_list.nr; i++)
722 pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
5cb104ae
GT
723
724 return true;
725}
726
06706542
GT
727static bool mptcp_established_options_mp_prio(struct sock *sk,
728 unsigned int *size,
729 unsigned int remaining,
730 struct mptcp_out_options *opts)
731{
732 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
733
1bff1e43
PA
734 /* can't send MP_PRIO with MPC, as they share the same option space:
735 * 'backup'. Also it makes no sense at all
736 */
13ac17a3 737 if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
06706542
GT
738 return false;
739
ec99a470
DC
740 /* account for the trailing 'nop' option */
741 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
06706542
GT
742 return false;
743
ec99a470 744 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
06706542
GT
745 opts->suboptions |= OPTION_MPTCP_PRIO;
746 opts->backup = subflow->request_bkup;
747
748 pr_debug("prio=%d", opts->backup);
749
750 return true;
751}
752
c25aeb4e 753static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
dc87efdb
FW
754 unsigned int *size,
755 unsigned int remaining,
756 struct mptcp_out_options *opts)
757{
758 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
759
760 if (remaining < TCPOLEN_MPTCP_RST)
c25aeb4e 761 return false;
dc87efdb
FW
762
763 *size = TCPOLEN_MPTCP_RST;
764 opts->suboptions |= OPTION_MPTCP_RST;
765 opts->reset_transient = subflow->reset_transient;
766 opts->reset_reason = subflow->reset_reason;
c25aeb4e
GT
767
768 return true;
769}
770
771static bool mptcp_established_options_mp_fail(struct sock *sk,
772 unsigned int *size,
773 unsigned int remaining,
774 struct mptcp_out_options *opts)
775{
776 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
777
778 if (likely(!subflow->send_mp_fail))
779 return false;
780
781 if (remaining < TCPOLEN_MPTCP_FAIL)
782 return false;
783
784 *size = TCPOLEN_MPTCP_FAIL;
785 opts->suboptions |= OPTION_MPTCP_FAIL;
786 opts->fail_seq = subflow->map_seq;
787
788 pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
789
790 return true;
dc87efdb
FW
791}
792
6d0060f6
MM
793bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
794 unsigned int *size, unsigned int remaining,
795 struct mptcp_out_options *opts)
796{
d87903b6
PA
797 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
798 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
6d0060f6 799 unsigned int opt_size = 0;
d87903b6 800 bool snd_data_fin;
6d0060f6
MM
801 bool ret = false;
802
3df523ab
PK
803 opts->suboptions = 0;
804
d87903b6 805 if (unlikely(__mptcp_check_fallback(msk)))
e1ff9e82
DC
806 return false;
807
dc87efdb 808 if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
c25aeb4e
GT
809 if (mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
810 *size += opt_size;
811 remaining -= opt_size;
812 }
813 if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
814 *size += opt_size;
815 remaining -= opt_size;
816 }
dc87efdb
FW
817 return true;
818 }
d5824847 819
d87903b6
PA
820 snd_data_fin = mptcp_data_fin_enabled(msk);
821 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
6d0060f6 822 ret = true;
c25aeb4e 823 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) {
04fac2ca
MB
824 unsigned int mp_fail_size;
825
6d0060f6 826 ret = true;
04fac2ca
MB
827 if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
828 remaining - opt_size, opts)) {
829 *size += opt_size + mp_fail_size;
830 remaining -= opt_size - mp_fail_size;
c25aeb4e
GT
831 return true;
832 }
833 }
6d0060f6
MM
834
835 /* we reserved enough space for the above options, and exceeding the
836 * TCP option space would be fatal
837 */
838 if (WARN_ON_ONCE(opt_size > remaining))
839 return false;
840
841 *size += opt_size;
842 remaining -= opt_size;
84dfe367 843 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
3df523ab
PK
844 *size += opt_size;
845 remaining -= opt_size;
846 ret = true;
5cb104ae
GT
847 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
848 *size += opt_size;
849 remaining -= opt_size;
850 ret = true;
3df523ab 851 }
6d0060f6 852
06706542
GT
853 if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
854 *size += opt_size;
855 remaining -= opt_size;
856 ret = true;
857 }
858
6d0060f6
MM
859 return ret;
860}
861
cec37a6e
PK
862bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
863 struct mptcp_out_options *opts)
864{
865 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
866
867 if (subflow_req->mp_capable) {
868 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
869 opts->sndr_key = subflow_req->local_key;
06fe1719 870 opts->csum_reqd = subflow_req->csum_reqd;
bab6b88e 871 opts->allow_join_id0 = subflow_req->allow_join_id0;
cec37a6e
PK
872 *size = TCPOLEN_MPTCP_MPC_SYNACK;
873 pr_debug("subflow_req=%p, local_key=%llu",
874 subflow_req, subflow_req->local_key);
875 return true;
f296234c
PK
876 } else if (subflow_req->mp_join) {
877 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
878 opts->backup = subflow_req->backup;
879 opts->join_id = subflow_req->local_id;
880 opts->thmac = subflow_req->thmac;
881 opts->nonce = subflow_req->local_nonce;
882 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
883 subflow_req, opts->backup, opts->join_id,
884 opts->thmac, opts->nonce);
885 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
886 return true;
cec37a6e
PK
887 }
888 return false;
889}
890
d5824847 891static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
f296234c 892 struct mptcp_subflow_context *subflow,
0be534f5
PA
893 struct sk_buff *skb,
894 struct mptcp_options_received *mp_opt)
d22f4988
CP
895{
896 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
f296234c 897 * will make the subflow fully established
d22f4988 898 */
f296234c
PK
899 if (likely(subflow->fully_established)) {
900 /* on passive sockets, check for 3rd ack retransmission
901 * note that msk is always set by subflow_syn_recv_sock()
902 * for mp_join subflows
903 */
904 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
905 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
74c7dfbe 906 subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
f296234c 907 READ_ONCE(msk->pm.server_side))
d5824847 908 tcp_send_ack(ssk);
f296234c
PK
909 goto fully_established;
910 }
911
d5824847
PA
912 /* we must process OoO packets before the first subflow is fully
913 * established. OoO packets are instead a protocol violation
914 * for MP_JOIN subflows as the peer must not send any data
915 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
f296234c 916 */
d5824847
PA
917 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
918 if (subflow->mp_join)
919 goto reset;
f296234c 920 return subflow->mp_capable;
d5824847 921 }
d22f4988 922
74c7dfbe
PA
923 if (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
924 ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo)) {
f296234c 925 /* subflows are fully established as soon as we get any
67b12f79 926 * additional ack, including ADD_ADDR.
f296234c 927 */
0be534f5 928 subflow->fully_established = 1;
b93df08c 929 WRITE_ONCE(msk->fully_established, true);
f296234c
PK
930 goto fully_established;
931 }
d22f4988 932
d22f4988 933 /* If the first established packet does not contain MP_CAPABLE + data
d5824847
PA
934 * then fallback to TCP. Fallback scenarios requires a reset for
935 * MP_JOIN subflows.
d22f4988 936 */
74c7dfbe 937 if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
d5824847
PA
938 if (subflow->mp_join)
939 goto reset;
d22f4988 940 subflow->mp_capable = 0;
e1ff9e82
DC
941 pr_fallback(msk);
942 __mptcp_do_fallback(msk);
d22f4988
CP
943 return false;
944 }
f296234c 945
df377be3
GT
946 if (mp_opt->deny_join_id0)
947 WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
948
d6085fe1
PA
949 if (unlikely(!READ_ONCE(msk->pm.server_side)))
950 pr_warn_once("bogus mpc option on established client sk");
b93df08c 951 mptcp_subflow_fully_established(subflow, mp_opt);
f296234c
PK
952
953fully_established:
5b950ff4
PA
954 /* if the subflow is not already linked into the conn_list, we can't
955 * notify the PM: this subflow is still on the listener queue
956 * and the PM possibly acquiring the subflow lock could race with
957 * the listener close
958 */
959 if (likely(subflow->pm_notified) || list_empty(&subflow->node))
f296234c
PK
960 return true;
961
962 subflow->pm_notified = 1;
ec3edaa7 963 if (subflow->mp_join) {
d5824847 964 clear_3rdack_retransmission(ssk);
62535200 965 mptcp_pm_subflow_established(msk);
ec3edaa7 966 } else {
6c714f1b 967 mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
ec3edaa7 968 }
d22f4988 969 return true;
d5824847
PA
970
971reset:
972 mptcp_subflow_reset(ssk);
973 return false;
d22f4988
CP
974}
975
1502328f 976u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
cc9d2566 977{
1502328f
PA
978 u32 old_seq32, cur_seq32;
979
980 old_seq32 = (u32)old_seq;
981 cur_seq32 = (u32)cur_seq;
982 cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
983 if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
984 return cur_seq + (1LL << 32);
985
986 /* reverse wrap could happen, too */
987 if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
988 return cur_seq - (1LL << 32);
989 return cur_seq;
cc9d2566
PA
990}
991
6f8a612a 992static void ack_update_msk(struct mptcp_sock *msk,
6e628cd3 993 struct sock *ssk,
6f8a612a 994 struct mptcp_options_received *mp_opt)
cc9d2566 995{
7439d687 996 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
6f8a612a 997 struct sock *sk = (struct sock *)msk;
7439d687
PA
998 u64 old_snd_una;
999
1000 mptcp_data_lock(sk);
cc9d2566
PA
1001
1002 /* avoid ack expansion on update conflict, to reduce the risk of
1003 * wrongly expanding to a future ack sequence number, which is way
1004 * more dangerous than missing an ack
1005 */
7439d687 1006 old_snd_una = msk->snd_una;
1502328f 1007 new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
cc9d2566 1008
0d199e43
FW
1009 /* ACK for data not even sent yet? Ignore.*/
1010 if (unlikely(after64(new_snd_una, snd_nxt)))
1011 new_snd_una = old_snd_una;
cc9d2566 1012
6f8a612a
FW
1013 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1014
219d0499 1015 if (after64(new_wnd_end, msk->wnd_end))
7439d687 1016 msk->wnd_end = new_wnd_end;
219d0499
PA
1017
1018 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
d09d818e 1019 if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
219d0499 1020 __mptcp_check_push(sk, ssk);
6f8a612a 1021
7439d687
PA
1022 if (after64(new_snd_una, old_snd_una)) {
1023 msk->snd_una = new_snd_una;
1024 __mptcp_data_acked(sk);
cc9d2566 1025 }
7439d687 1026 mptcp_data_unlock(sk);
ed66bfb4
GT
1027
1028 trace_ack_update_msk(mp_opt->data_ack,
1029 old_snd_una, new_snd_una,
1030 new_wnd_end, msk->wnd_end);
cc9d2566
PA
1031}
1032
1a49b2c2 1033bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
3721b9b6
MM
1034{
1035 /* Skip if DATA_FIN was already received.
1036 * If updating simultaneously with the recvmsg loop, values
1037 * should match. If they mismatch, the peer is misbehaving and
1038 * we will prefer the most recent information.
1039 */
781bf13d 1040 if (READ_ONCE(msk->rcv_data_fin))
3721b9b6
MM
1041 return false;
1042
1a49b2c2 1043 WRITE_ONCE(msk->rcv_data_fin_seq,
1502328f 1044 mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
3721b9b6
MM
1045 WRITE_ONCE(msk->rcv_data_fin, 1);
1046
1047 return true;
1048}
1049
1b1c7a0e
PK
1050static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1051 struct mptcp_options_received *mp_opt)
1052{
1053 u64 hmac = 0;
1054
1055 if (mp_opt->echo)
1056 return true;
1057
761c124e
GT
1058 hmac = add_addr_generate_hmac(msk->remote_key,
1059 msk->local_key,
1060 &mp_opt->addr);
1b1c7a0e
PK
1061
1062 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1063 msk, (unsigned long long)hmac,
1064 (unsigned long long)mp_opt->ahmac);
1065
1066 return hmac == mp_opt->ahmac;
1067}
1068
6787b7e3
JW
1069/* Return false if a subflow has been reset, else return true */
1070bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
648ef4b8 1071{
d22f4988 1072 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1b1c7a0e 1073 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
cfde141e 1074 struct mptcp_options_received mp_opt;
648ef4b8
MM
1075 struct mptcp_ext *mpext;
1076
6e628cd3
PA
1077 if (__mptcp_check_fallback(msk)) {
1078 /* Keep it simple and unconditionally trigger send data cleanup and
1079 * pending queue spooling. We will need to acquire the data lock
1080 * for more accurate checks, and once the lock is acquired, such
1081 * helpers are cheap.
1082 */
1083 mptcp_data_lock(subflow->conn);
219d0499
PA
1084 if (sk_stream_memory_free(sk))
1085 __mptcp_check_push(subflow->conn, sk);
6e628cd3
PA
1086 __mptcp_data_acked(subflow->conn);
1087 mptcp_data_unlock(subflow->conn);
6787b7e3 1088 return true;
6e628cd3 1089 }
e1ff9e82 1090
c863225b 1091 mptcp_get_options(sk, skb, &mp_opt);
6787b7e3
JW
1092
1093 /* The subflow can be in close state only if check_fully_established()
1094 * just sent a reset. If so, tell the caller to ignore the current packet.
1095 */
cfde141e 1096 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
6787b7e3 1097 return sk->sk_state != TCP_CLOSE;
648ef4b8 1098
f6c2ef59
PA
1099 if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1100 if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1101 msk->local_key == mp_opt.rcvr_key) {
1102 WRITE_ONCE(msk->rcv_fastclose, true);
1103 mptcp_schedule_work((struct sock *)msk);
1104 }
50c504a2 1105
f6c2ef59
PA
1106 if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1107 add_addr_hmac_valid(msk, &mp_opt)) {
1108 if (!mp_opt.echo) {
1109 mptcp_pm_add_addr_received(msk, &mp_opt.addr);
1110 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1111 } else {
1112 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1113 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1114 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1115 }
1116
1117 if (mp_opt.addr.port)
1118 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
a877de06 1119 }
2fbdd9ea 1120
f6c2ef59
PA
1121 if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1122 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1b1c7a0e 1123
f6c2ef59
PA
1124 if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1125 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1126 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1127 }
d0876b22 1128
f6c2ef59
PA
1129 if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1130 mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1131 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1132 }
40453a5c 1133
f6c2ef59
PA
1134 if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1135 subflow->reset_seen = 1;
1136 subflow->reset_reason = mp_opt.reset_reason;
1137 subflow->reset_transient = mp_opt.reset_transient;
1138 }
5580d41b 1139
f6c2ef59
PA
1140 if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1141 return true;
dc87efdb
FW
1142 }
1143
cc9d2566
PA
1144 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1145 * monodirectional flows will stuck
1146 */
cfde141e 1147 if (mp_opt.use_ack)
6f8a612a 1148 ack_update_msk(msk, sk, &mp_opt);
cc9d2566 1149
06827b34
MM
1150 /* Zero-data-length packets are dropped by the caller and not
1151 * propagated to the MPTCP layer, so the skb extension does not
1152 * need to be allocated or populated. DATA_FIN information, if
1153 * present, needs to be updated here before the skb is freed.
43b54c6e
MM
1154 */
1155 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1156 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1a49b2c2 1157 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
43b54c6e
MM
1158 schedule_work(&msk->work))
1159 sock_hold(subflow->conn);
06827b34 1160
6787b7e3 1161 return true;
43b54c6e
MM
1162 }
1163
648ef4b8
MM
1164 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1165 if (!mpext)
6787b7e3 1166 return true;
648ef4b8
MM
1167
1168 memset(mpext, 0, sizeof(*mpext));
1169
f6c2ef59 1170 if (likely(mp_opt.use_map)) {
cfde141e 1171 if (mp_opt.mpc_map) {
cc7972ea
CP
1172 /* this is an MP_CAPABLE carrying MPTCP data
1173 * we know this map the first chunk of data
1174 */
1175 mptcp_crypto_key_sha(subflow->remote_key, NULL,
1176 &mpext->data_seq);
1177 mpext->data_seq++;
1178 mpext->subflow_seq = 1;
1179 mpext->dsn64 = 1;
1180 mpext->mpc_map = 1;
a77895db 1181 mpext->data_fin = 0;
cc7972ea 1182 } else {
cfde141e
PA
1183 mpext->data_seq = mp_opt.data_seq;
1184 mpext->subflow_seq = mp_opt.subflow_seq;
1185 mpext->dsn64 = mp_opt.dsn64;
1186 mpext->data_fin = mp_opt.data_fin;
cc7972ea 1187 }
cfde141e 1188 mpext->data_len = mp_opt.data_len;
648ef4b8 1189 mpext->use_map = 1;
74c7dfbe 1190 mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
208e8f66
GT
1191
1192 if (mpext->csum_reqd)
1193 mpext->csum = mp_opt.csum;
648ef4b8 1194 }
6787b7e3
JW
1195
1196 return true;
648ef4b8
MM
1197}
1198
fa3fe2b1
FW
1199static void mptcp_set_rwin(const struct tcp_sock *tp)
1200{
1201 const struct sock *ssk = (const struct sock *)tp;
1202 const struct mptcp_subflow_context *subflow;
1203 struct mptcp_sock *msk;
1204 u64 ack_seq;
1205
1206 subflow = mptcp_subflow_ctx(ssk);
1207 msk = mptcp_sk(subflow->conn);
1208
1209 ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1210
1211 if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1212 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1213}
1214
f7cc8890 1215static u16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __sum16 sum)
c94b1f96
GT
1216{
1217 struct csum_pseudo_header header;
1218 __wsum csum;
1219
1220 /* cfr RFC 8684 3.3.1.:
1221 * the data sequence number used in the pseudo-header is
1222 * always the 64-bit value, irrespective of what length is used in the
1223 * DSS option itself.
1224 */
f7cc8890
DC
1225 header.data_seq = cpu_to_be64(data_seq);
1226 header.subflow_seq = htonl(subflow_seq);
1227 header.data_len = htons(data_len);
c94b1f96
GT
1228 header.csum = 0;
1229
f7cc8890 1230 csum = csum_partial(&header, sizeof(header), ~csum_unfold(sum));
c94b1f96
GT
1231 return (__force u16)csum_fold(csum);
1232}
1233
f7cc8890
DC
1234static u16 mptcp_make_csum(const struct mptcp_ext *mpext)
1235{
1236 return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1237 mpext->csum);
1238}
1239
fa3fe2b1
FW
1240void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1241 struct mptcp_out_options *opts)
eda7acdd 1242{
c25aeb4e
GT
1243 if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1244 const struct sock *ssk = (const struct sock *)tp;
1245 struct mptcp_subflow_context *subflow;
1246
1247 subflow = mptcp_subflow_ctx(ssk);
1248 subflow->send_mp_fail = 0;
1249
1250 *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1251 TCPOLEN_MPTCP_FAIL,
1252 0, 0);
1253 put_unaligned_be64(opts->fail_seq, ptr);
1254 ptr += 2;
1255 }
1256
1bff1e43
PA
1257 /* RST is mutually exclusive with everything else */
1258 if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1259 *ptr++ = mptcp_option(MPTCPOPT_RST,
1260 TCPOLEN_MPTCP_RST,
1261 opts->reset_transient,
1262 opts->reset_reason);
1263 return;
1264 }
1265
1266 /* DSS, MPC, MPJ and ADD_ADDR are mutually exclusive, see
1267 * mptcp_established_options*()
1268 */
1269 if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1270 struct mptcp_ext *mpext = &opts->ext_copy;
1271 u8 len = TCPOLEN_MPTCP_DSS_BASE;
1272 u8 flags = 0;
1273
1274 if (mpext->use_ack) {
1275 flags = MPTCP_DSS_HAS_ACK;
1276 if (mpext->ack64) {
1277 len += TCPOLEN_MPTCP_DSS_ACK64;
1278 flags |= MPTCP_DSS_ACK64;
1279 } else {
1280 len += TCPOLEN_MPTCP_DSS_ACK32;
1281 }
1282 }
1283
1284 if (mpext->use_map) {
1285 len += TCPOLEN_MPTCP_DSS_MAP64;
1286
1287 /* Use only 64-bit mapping flags for now, add
1288 * support for optional 32-bit mappings later.
1289 */
1290 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1291 if (mpext->data_fin)
1292 flags |= MPTCP_DSS_DATA_FIN;
1293
1294 if (opts->csum_reqd)
1295 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1296 }
1297
1298 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1299
1300 if (mpext->use_ack) {
1301 if (mpext->ack64) {
1302 put_unaligned_be64(mpext->data_ack, ptr);
1303 ptr += 2;
1304 } else {
1305 put_unaligned_be32(mpext->data_ack32, ptr);
1306 ptr += 1;
1307 }
1308 }
1309
1310 if (mpext->use_map) {
1311 put_unaligned_be64(mpext->data_seq, ptr);
1312 ptr += 2;
1313 put_unaligned_be32(mpext->subflow_seq, ptr);
1314 ptr += 1;
1315 if (opts->csum_reqd) {
1316 put_unaligned_be32(mpext->data_len << 16 |
1317 mptcp_make_csum(mpext), ptr);
1318 } else {
1319 put_unaligned_be32(mpext->data_len << 16 |
1320 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1321 }
1322 }
13ac17a3 1323 } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
06fe1719 1324 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
eda7acdd 1325
c94b1f96 1326 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
eda7acdd 1327 len = TCPOLEN_MPTCP_MPC_SYN;
c94b1f96 1328 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
cec37a6e 1329 len = TCPOLEN_MPTCP_MPC_SYNACK;
f7cc8890 1330 } else if (opts->data_len) {
cc7972ea 1331 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
c94b1f96
GT
1332 if (opts->csum_reqd)
1333 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1334 } else {
eda7acdd 1335 len = TCPOLEN_MPTCP_MPC_ACK;
c94b1f96 1336 }
eda7acdd 1337
06fe1719
GT
1338 if (opts->csum_reqd)
1339 flag |= MPTCP_CAP_CHECKSUM_REQD;
1340
bab6b88e
GT
1341 if (!opts->allow_join_id0)
1342 flag |= MPTCP_CAP_DENY_JOIN_ID0;
1343
3df523ab
PK
1344 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1345 MPTCP_SUPPORTED_VERSION,
06fe1719 1346 flag);
cc7972ea
CP
1347
1348 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1349 opts->suboptions))
1350 goto mp_capable_done;
1351
eda7acdd
PK
1352 put_unaligned_be64(opts->sndr_key, ptr);
1353 ptr += 2;
cc7972ea
CP
1354 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1355 goto mp_capable_done;
1356
1357 put_unaligned_be64(opts->rcvr_key, ptr);
1358 ptr += 2;
f7cc8890 1359 if (!opts->data_len)
cc7972ea
CP
1360 goto mp_capable_done;
1361
c94b1f96 1362 if (opts->csum_reqd) {
f7cc8890
DC
1363 put_unaligned_be32(opts->data_len << 16 |
1364 __mptcp_make_csum(opts->data_seq,
1365 opts->subflow_seq,
1366 opts->data_len,
1367 opts->csum), ptr);
c94b1f96 1368 } else {
f7cc8890 1369 put_unaligned_be32(opts->data_len << 16 |
c94b1f96
GT
1370 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1371 }
cc7972ea 1372 ptr += 1;
6d0060f6 1373
1bff1e43
PA
1374 /* MPC is additionally mutually exclusive with MP_PRIO */
1375 goto mp_capable_done;
1376 } else if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1377 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1378 TCPOLEN_MPTCP_MPJ_SYN,
1379 opts->backup, opts->join_id);
1380 put_unaligned_be32(opts->token, ptr);
1381 ptr += 1;
1382 put_unaligned_be32(opts->nonce, ptr);
1383 ptr += 1;
1384 } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1385 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1386 TCPOLEN_MPTCP_MPJ_SYNACK,
1387 opts->backup, opts->join_id);
1388 put_unaligned_be64(opts->thmac, ptr);
1389 ptr += 2;
1390 put_unaligned_be32(opts->nonce, ptr);
1391 ptr += 1;
1392 } else if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1393 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1394 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1395 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1396 ptr += 5;
1397 } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
6eb3d1e3
GT
1398 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1399 u8 echo = MPTCP_ADDR_ECHO;
1400
e1ef6832 1401#if IS_ENABLED(CONFIG_MPTCP_IPV6)
fef6b7ec 1402 if (opts->addr.family == AF_INET6)
e1ef6832
GT
1403 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1404#endif
1405
30f60bae 1406 if (opts->addr.port)
22fb85ff
GT
1407 len += TCPOLEN_MPTCP_PORT_LEN;
1408
3df523ab 1409 if (opts->ahmac) {
6eb3d1e3
GT
1410 len += sizeof(opts->ahmac);
1411 echo = 0;
3df523ab 1412 }
3df523ab 1413
6eb3d1e3 1414 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
30f60bae 1415 len, echo, opts->addr.id);
fef6b7ec 1416 if (opts->addr.family == AF_INET) {
30f60bae 1417 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
e1ef6832 1418 ptr += 1;
3df523ab 1419 }
3df523ab 1420#if IS_ENABLED(CONFIG_MPTCP_IPV6)
fef6b7ec 1421 else if (opts->addr.family == AF_INET6) {
30f60bae 1422 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
e1ef6832 1423 ptr += 4;
3df523ab 1424 }
3df523ab
PK
1425#endif
1426
30f60bae 1427 if (!opts->addr.port) {
22fb85ff
GT
1428 if (opts->ahmac) {
1429 put_unaligned_be64(opts->ahmac, ptr);
1430 ptr += 2;
1431 }
1432 } else {
30f60bae
GT
1433 u16 port = ntohs(opts->addr.port);
1434
22fb85ff
GT
1435 if (opts->ahmac) {
1436 u8 *bptr = (u8 *)ptr;
1437
30f60bae 1438 put_unaligned_be16(port, bptr);
22fb85ff
GT
1439 bptr += 2;
1440 put_unaligned_be64(opts->ahmac, bptr);
1441 bptr += 8;
1442 put_unaligned_be16(TCPOPT_NOP << 8 |
1443 TCPOPT_NOP, bptr);
1444
1445 ptr += 3;
1446 } else {
30f60bae 1447 put_unaligned_be32(port << 16 |
22fb85ff
GT
1448 TCPOPT_NOP << 8 |
1449 TCPOPT_NOP, ptr);
1450 ptr += 1;
1451 }
3df523ab
PK
1452 }
1453 }
3df523ab 1454
1bff1e43
PA
1455 if (OPTION_MPTCP_PRIO & opts->suboptions) {
1456 const struct sock *ssk = (const struct sock *)tp;
1457 struct mptcp_subflow_context *subflow;
1458
1459 subflow = mptcp_subflow_ctx(ssk);
1460 subflow->send_mp_prio = 0;
1461
1462 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1463 TCPOLEN_MPTCP_PRIO,
1464 opts->backup, TCPOPT_NOP);
1465 }
1466
1467mp_capable_done:
3df523ab 1468 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
6445e17a
GT
1469 u8 i = 1;
1470
3df523ab 1471 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
6445e17a
GT
1472 TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1473 0, opts->rm_list.ids[0]);
1474
1475 while (i < opts->rm_list.nr) {
1476 u8 id1, id2, id3, id4;
1477
1478 id1 = opts->rm_list.ids[i];
1479 id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1480 id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1481 id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1482 put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1483 ptr += 1;
1484 i += 4;
1485 }
3df523ab
PK
1486 }
1487
fa3fe2b1
FW
1488 if (tp)
1489 mptcp_set_rwin(tp);
eda7acdd 1490}
dc87efdb
FW
1491
1492__be32 mptcp_get_reset_option(const struct sk_buff *skb)
1493{
1494 const struct mptcp_ext *ext = mptcp_get_ext(skb);
1495 u8 flags, reason;
1496
1497 if (ext) {
1498 flags = ext->reset_transient;
1499 reason = ext->reset_reason;
1500
1501 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1502 flags, reason);
1503 }
1504
1505 return htonl(0u);
1506}
1507EXPORT_SYMBOL_GPL(mptcp_get_reset_option);