net: fec: Fix PHY init after phy_reset_after_clk_enable()
[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>
bd697222 10#include <crypto/sha.h>
eda7acdd
PK
11#include <net/tcp.h>
12#include <net/mptcp.h>
13#include "protocol.h"
14
65492c5a
PA
15static bool mptcp_cap_flag_sha256(u8 flags)
16{
17 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
18}
19
cfde141e
PA
20static void mptcp_parse_option(const struct sk_buff *skb,
21 const unsigned char *ptr, int opsize,
22 struct mptcp_options_received *mp_opt)
eda7acdd 23{
eda7acdd 24 u8 subtype = *ptr >> 4;
648ef4b8 25 int expected_opsize;
eda7acdd
PK
26 u8 version;
27 u8 flags;
28
29 switch (subtype) {
30 case MPTCPOPT_MP_CAPABLE:
cc7972ea
CP
31 /* strict size checking */
32 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
33 if (skb->len > tcp_hdr(skb)->doff << 2)
34 expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
35 else
36 expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
37 } else {
38 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
39 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
40 else
41 expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
42 }
43 if (opsize != expected_opsize)
eda7acdd
PK
44 break;
45
cc7972ea 46 /* try to be gentle vs future versions on the initial syn */
eda7acdd 47 version = *ptr++ & MPTCP_VERSION_MASK;
cc7972ea
CP
48 if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
49 if (version != MPTCP_SUPPORTED_VERSION)
50 break;
51 } else if (version < MPTCP_SUPPORTED_VERSION) {
eda7acdd 52 break;
cc7972ea 53 }
eda7acdd
PK
54
55 flags = *ptr++;
65492c5a 56 if (!mptcp_cap_flag_sha256(flags) ||
eda7acdd
PK
57 (flags & MPTCP_CAP_EXTENSIBILITY))
58 break;
59
60 /* RFC 6824, Section 3.1:
61 * "For the Checksum Required bit (labeled "A"), if either
62 * host requires the use of checksums, checksums MUST be used.
63 * In other words, the only way for checksums not to be used
64 * is if both hosts in their SYNs set A=0."
65 *
66 * Section 3.3.0:
67 * "If a checksum is not present when its use has been
68 * negotiated, the receiver MUST close the subflow with a RST as
69 * it is considered broken."
70 *
71 * We don't implement DSS checksum - fall back to TCP.
72 */
73 if (flags & MPTCP_CAP_CHECKSUM_REQD)
74 break;
75
76 mp_opt->mp_capable = 1;
cc7972ea
CP
77 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
78 mp_opt->sndr_key = get_unaligned_be64(ptr);
79 ptr += 8;
80 }
81 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
eda7acdd
PK
82 mp_opt->rcvr_key = get_unaligned_be64(ptr);
83 ptr += 8;
eda7acdd 84 }
cc7972ea
CP
85 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
86 /* Section 3.1.:
87 * "the data parameters in a MP_CAPABLE are semantically
88 * equivalent to those in a DSS option and can be used
89 * interchangeably."
90 */
91 mp_opt->dss = 1;
92 mp_opt->use_map = 1;
93 mp_opt->mpc_map = 1;
94 mp_opt->data_len = get_unaligned_be16(ptr);
95 ptr += 2;
96 }
97 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
98 version, flags, opsize, mp_opt->sndr_key,
99 mp_opt->rcvr_key, mp_opt->data_len);
eda7acdd
PK
100 break;
101
f296234c
PK
102 case MPTCPOPT_MP_JOIN:
103 mp_opt->mp_join = 1;
104 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
105 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
106 mp_opt->join_id = *ptr++;
107 mp_opt->token = get_unaligned_be32(ptr);
108 ptr += 4;
109 mp_opt->nonce = get_unaligned_be32(ptr);
110 ptr += 4;
111 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
112 mp_opt->backup, mp_opt->join_id,
113 mp_opt->token, mp_opt->nonce);
114 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
115 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
116 mp_opt->join_id = *ptr++;
117 mp_opt->thmac = get_unaligned_be64(ptr);
118 ptr += 8;
119 mp_opt->nonce = get_unaligned_be32(ptr);
120 ptr += 4;
121 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
122 mp_opt->backup, mp_opt->join_id,
123 mp_opt->thmac, mp_opt->nonce);
124 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
125 ptr += 2;
126 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
127 pr_debug("MP_JOIN hmac");
128 } else {
129 pr_warn("MP_JOIN bad option size");
130 mp_opt->mp_join = 0;
131 }
132 break;
133
eda7acdd
PK
134 case MPTCPOPT_DSS:
135 pr_debug("DSS");
648ef4b8
MM
136 ptr++;
137
cc7972ea
CP
138 /* we must clear 'mpc_map' be able to detect MP_CAPABLE
139 * map vs DSS map in mptcp_incoming_options(), and reconstruct
140 * map info accordingly
141 */
142 mp_opt->mpc_map = 0;
648ef4b8
MM
143 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
144 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
145 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
146 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
147 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
148 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
149
150 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
151 mp_opt->data_fin, mp_opt->dsn64,
152 mp_opt->use_map, mp_opt->ack64,
153 mp_opt->use_ack);
154
155 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
156
157 if (mp_opt->use_ack) {
158 if (mp_opt->ack64)
159 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
160 else
161 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
162 }
163
164 if (mp_opt->use_map) {
165 if (mp_opt->dsn64)
166 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
167 else
168 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
169 }
170
171 /* RFC 6824, Section 3.3:
172 * If a checksum is present, but its use had
173 * not been negotiated in the MP_CAPABLE handshake,
174 * the checksum field MUST be ignored.
175 */
176 if (opsize != expected_opsize &&
177 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
178 break;
179
eda7acdd 180 mp_opt->dss = 1;
648ef4b8
MM
181
182 if (mp_opt->use_ack) {
183 if (mp_opt->ack64) {
184 mp_opt->data_ack = get_unaligned_be64(ptr);
185 ptr += 8;
186 } else {
187 mp_opt->data_ack = get_unaligned_be32(ptr);
188 ptr += 4;
189 }
190
191 pr_debug("data_ack=%llu", mp_opt->data_ack);
192 }
193
194 if (mp_opt->use_map) {
195 if (mp_opt->dsn64) {
196 mp_opt->data_seq = get_unaligned_be64(ptr);
197 ptr += 8;
198 } else {
199 mp_opt->data_seq = get_unaligned_be32(ptr);
200 ptr += 4;
201 }
202
203 mp_opt->subflow_seq = get_unaligned_be32(ptr);
204 ptr += 4;
205
206 mp_opt->data_len = get_unaligned_be16(ptr);
207 ptr += 2;
208
209 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
210 mp_opt->data_seq, mp_opt->subflow_seq,
211 mp_opt->data_len);
212 }
213
eda7acdd
PK
214 break;
215
3df523ab
PK
216 case MPTCPOPT_ADD_ADDR:
217 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
218 if (!mp_opt->echo) {
219 if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
220 opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
221 mp_opt->family = MPTCP_ADDR_IPVERSION_4;
222#if IS_ENABLED(CONFIG_MPTCP_IPV6)
223 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
224 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
225 mp_opt->family = MPTCP_ADDR_IPVERSION_6;
226#endif
227 else
228 break;
229 } else {
230 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
231 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
232 mp_opt->family = MPTCP_ADDR_IPVERSION_4;
233#if IS_ENABLED(CONFIG_MPTCP_IPV6)
234 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
235 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
236 mp_opt->family = MPTCP_ADDR_IPVERSION_6;
237#endif
238 else
239 break;
240 }
241
242 mp_opt->add_addr = 1;
243 mp_opt->port = 0;
244 mp_opt->addr_id = *ptr++;
245 pr_debug("ADD_ADDR: id=%d", mp_opt->addr_id);
246 if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
247 memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4);
248 ptr += 4;
249 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
250 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
251 mp_opt->port = get_unaligned_be16(ptr);
252 ptr += 2;
253 }
254 }
255#if IS_ENABLED(CONFIG_MPTCP_IPV6)
256 else {
257 memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16);
258 ptr += 16;
259 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
260 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
261 mp_opt->port = get_unaligned_be16(ptr);
262 ptr += 2;
263 }
264 }
265#endif
266 if (!mp_opt->echo) {
267 mp_opt->ahmac = get_unaligned_be64(ptr);
268 ptr += 8;
269 }
270 break;
271
272 case MPTCPOPT_RM_ADDR:
273 if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE)
274 break;
275
8e60eed6
GT
276 ptr++;
277
3df523ab
PK
278 mp_opt->rm_addr = 1;
279 mp_opt->rm_id = *ptr++;
280 pr_debug("RM_ADDR: id=%d", mp_opt->rm_id);
281 break;
282
eda7acdd
PK
283 default:
284 break;
285 }
286}
287
cec37a6e 288void mptcp_get_options(const struct sk_buff *skb,
cfde141e 289 struct mptcp_options_received *mp_opt)
cec37a6e 290{
cec37a6e 291 const struct tcphdr *th = tcp_hdr(skb);
cfde141e
PA
292 const unsigned char *ptr;
293 int length;
cec37a6e 294
cfde141e
PA
295 /* initialize option status */
296 mp_opt->mp_capable = 0;
297 mp_opt->mp_join = 0;
298 mp_opt->add_addr = 0;
299 mp_opt->rm_addr = 0;
300 mp_opt->dss = 0;
301
302 length = (th->doff * 4) - sizeof(struct tcphdr);
cec37a6e
PK
303 ptr = (const unsigned char *)(th + 1);
304
305 while (length > 0) {
306 int opcode = *ptr++;
307 int opsize;
308
309 switch (opcode) {
310 case TCPOPT_EOL:
311 return;
312 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
313 length--;
314 continue;
315 default:
316 opsize = *ptr++;
317 if (opsize < 2) /* "silly options" */
318 return;
319 if (opsize > length)
320 return; /* don't parse partial options */
321 if (opcode == TCPOPT_MPTCP)
cfde141e 322 mptcp_parse_option(skb, ptr, opsize, mp_opt);
cec37a6e
PK
323 ptr += opsize - 2;
324 length -= opsize;
325 }
326 }
327}
328
cc7972ea
CP
329bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
330 unsigned int *size, struct mptcp_out_options *opts)
cec37a6e
PK
331{
332 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
333
cc7972ea
CP
334 /* we will use snd_isn to detect first pkt [re]transmission
335 * in mptcp_established_options_mp()
336 */
337 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
cec37a6e 338 if (subflow->request_mptcp) {
cec37a6e 339 opts->suboptions = OPTION_MPTCP_MPC_SYN;
cec37a6e
PK
340 *size = TCPOLEN_MPTCP_MPC_SYN;
341 return true;
ec3edaa7
PK
342 } else if (subflow->request_join) {
343 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
344 subflow->local_nonce);
345 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
346 opts->join_id = subflow->local_id;
347 opts->token = subflow->remote_token;
348 opts->nonce = subflow->local_nonce;
349 opts->backup = subflow->request_bkup;
350 *size = TCPOLEN_MPTCP_MPJ_SYN;
351 return true;
cec37a6e
PK
352 }
353 return false;
354}
355
ec3edaa7
PK
356/* MP_JOIN client subflow must wait for 4th ack before sending any data:
357 * TCP can't schedule delack timer before the subflow is fully established.
358 * MPTCP uses the delack timer to do 3rd ack retransmissions
359 */
360static void schedule_3rdack_retransmission(struct sock *sk)
361{
362 struct inet_connection_sock *icsk = inet_csk(sk);
363 struct tcp_sock *tp = tcp_sk(sk);
364 unsigned long timeout;
365
366 /* reschedule with a timeout above RTT, as we must look only for drop */
367 if (tp->srtt_us)
368 timeout = tp->srtt_us << 1;
369 else
370 timeout = TCP_TIMEOUT_INIT;
371
372 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
373 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
374 icsk->icsk_ack.timeout = timeout;
375 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
376}
377
378static void clear_3rdack_retransmission(struct sock *sk)
379{
380 struct inet_connection_sock *icsk = inet_csk(sk);
381
382 sk_stop_timer(sk, &icsk->icsk_delack_timer);
383 icsk->icsk_ack.timeout = 0;
384 icsk->icsk_ack.ato = 0;
385 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
386}
387
cc7972ea
CP
388static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
389 unsigned int *size,
6d0060f6
MM
390 unsigned int remaining,
391 struct mptcp_out_options *opts)
cec37a6e
PK
392{
393 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
cc7972ea
CP
394 struct mptcp_ext *mpext;
395 unsigned int data_len;
396
ec3edaa7
PK
397 /* When skb is not available, we better over-estimate the emitted
398 * options len. A full DSS option (28 bytes) is longer than
399 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
400 * tell the caller to defer the estimate to
401 * mptcp_established_options_dss(), which will reserve enough space.
402 */
403 if (!skb)
404 return false;
cc7972ea 405
ec3edaa7
PK
406 /* MPC/MPJ needed only on 3rd ack packet */
407 if (subflow->fully_established ||
408 subflow->snd_isn != TCP_SKB_CB(skb)->seq)
409 return false;
410
411 if (subflow->mp_capable) {
cc7972ea
CP
412 mpext = mptcp_get_ext(skb);
413 data_len = mpext ? mpext->data_len : 0;
cec37a6e 414
cc7972ea
CP
415 /* we will check ext_copy.data_len in mptcp_write_options() to
416 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
417 * TCPOLEN_MPTCP_MPC_ACK
418 */
419 opts->ext_copy.data_len = data_len;
cec37a6e
PK
420 opts->suboptions = OPTION_MPTCP_MPC_ACK;
421 opts->sndr_key = subflow->local_key;
422 opts->rcvr_key = subflow->remote_key;
cc7972ea
CP
423
424 /* Section 3.1.
425 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
426 * packets that start the first subflow of an MPTCP connection,
427 * as well as the first packet that carries data
428 */
429 if (data_len > 0)
430 *size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
431 else
432 *size = TCPOLEN_MPTCP_MPC_ACK;
433
434 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
435 subflow, subflow->local_key, subflow->remote_key,
436 data_len);
437
cec37a6e 438 return true;
ec3edaa7
PK
439 } else if (subflow->mp_join) {
440 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
441 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
442 *size = TCPOLEN_MPTCP_MPJ_ACK;
443 pr_debug("subflow=%p", subflow);
444
445 schedule_3rdack_retransmission(sk);
446 return true;
cec37a6e
PK
447 }
448 return false;
449}
450
6d0060f6 451static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
9c29e361 452 struct sk_buff *skb, struct mptcp_ext *ext)
6d0060f6 453{
017512a0
PA
454 /* The write_seq value has already been incremented, so the actual
455 * sequence number for the DATA_FIN is one less.
456 */
457 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
7279da61 458
9c29e361 459 if (!ext->use_map || !skb->len) {
6d0060f6
MM
460 /* RFC6824 requires a DSS mapping with specific values
461 * if DATA_FIN is set but no data payload is mapped
462 */
6d37a0b8 463 ext->data_fin = 1;
6d0060f6
MM
464 ext->use_map = 1;
465 ext->dsn64 = 1;
017512a0 466 ext->data_seq = data_fin_tx_seq;
6d0060f6
MM
467 ext->subflow_seq = 0;
468 ext->data_len = 1;
7279da61 469 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
6d37a0b8
MM
470 /* If there's an existing DSS mapping and it is the
471 * final mapping, DATA_FIN consumes 1 additional byte of
472 * mapping space.
6d0060f6 473 */
6d37a0b8 474 ext->data_fin = 1;
6d0060f6
MM
475 ext->data_len++;
476 }
477}
478
479static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
480 unsigned int *size,
481 unsigned int remaining,
482 struct mptcp_out_options *opts)
483{
484 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
7279da61 485 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
6d0060f6 486 unsigned int dss_size = 0;
7279da61 487 u64 snd_data_fin_enable;
6d0060f6 488 struct mptcp_ext *mpext;
6d0060f6 489 unsigned int ack_size;
d22f4988 490 bool ret = false;
6d0060f6 491
0bac966a 492 mpext = skb ? mptcp_get_ext(skb) : NULL;
7279da61 493 snd_data_fin_enable = READ_ONCE(msk->snd_data_fin_enable);
6d0060f6 494
7279da61 495 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
6d0060f6
MM
496 unsigned int map_size;
497
498 map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
499
500 remaining -= map_size;
501 dss_size = map_size;
502 if (mpext)
503 opts->ext_copy = *mpext;
504
7279da61 505 if (skb && snd_data_fin_enable)
9c29e361 506 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
d22f4988
CP
507 ret = true;
508 }
509
2398e399
PA
510 /* passive sockets msk will set the 'can_ack' after accept(), even
511 * if the first subflow may have the already the remote key handy
512 */
d22f4988 513 opts->ext_copy.use_ack = 0;
dc093db5 514 if (!READ_ONCE(msk->can_ack)) {
d22f4988
CP
515 *size = ALIGN(dss_size, 4);
516 return ret;
6d0060f6
MM
517 }
518
a0c1d0ea
CP
519 if (subflow->use_64bit_ack) {
520 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
917944da 521 opts->ext_copy.data_ack = READ_ONCE(msk->ack_seq);
a0c1d0ea
CP
522 opts->ext_copy.ack64 = 1;
523 } else {
524 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
917944da 525 opts->ext_copy.data_ack32 = (uint32_t)READ_ONCE(msk->ack_seq);
a0c1d0ea
CP
526 opts->ext_copy.ack64 = 0;
527 }
528 opts->ext_copy.use_ack = 1;
6d0060f6
MM
529
530 /* Add kind/length/subtype/flag overhead if mapping is not populated */
531 if (dss_size == 0)
532 ack_size += TCPOLEN_MPTCP_DSS_BASE;
533
534 dss_size += ack_size;
535
6d0060f6
MM
536 *size = ALIGN(dss_size, 4);
537 return true;
538}
539
3df523ab
PK
540static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
541 struct in_addr *addr)
542{
bd697222 543 u8 hmac[SHA256_DIGEST_SIZE];
3df523ab
PK
544 u8 msg[7];
545
546 msg[0] = addr_id;
547 memcpy(&msg[1], &addr->s_addr, 4);
548 msg[5] = 0;
549 msg[6] = 0;
550
551 mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
552
bd697222 553 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
3df523ab
PK
554}
555
556#if IS_ENABLED(CONFIG_MPTCP_IPV6)
557static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
558 struct in6_addr *addr)
559{
bd697222 560 u8 hmac[SHA256_DIGEST_SIZE];
3df523ab
PK
561 u8 msg[19];
562
563 msg[0] = addr_id;
564 memcpy(&msg[1], &addr->s6_addr, 16);
565 msg[17] = 0;
566 msg[18] = 0;
567
568 mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
569
bd697222 570 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
3df523ab
PK
571}
572#endif
573
574static bool mptcp_established_options_addr(struct sock *sk,
575 unsigned int *size,
576 unsigned int remaining,
577 struct mptcp_out_options *opts)
578{
579 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
580 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1b1c7a0e
PK
581 struct mptcp_addr_info saddr;
582 int len;
3df523ab 583
1b1c7a0e
PK
584 if (!mptcp_pm_should_signal(msk) ||
585 !(mptcp_pm_addr_signal(msk, remaining, &saddr)))
586 return false;
587
588 len = mptcp_add_addr_len(saddr.family);
589 if (remaining < len)
590 return false;
3df523ab 591
1b1c7a0e
PK
592 *size = len;
593 opts->addr_id = saddr.id;
594 if (saddr.family == AF_INET) {
3df523ab 595 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
1b1c7a0e 596 opts->addr = saddr.addr;
3df523ab
PK
597 opts->ahmac = add_addr_generate_hmac(msk->local_key,
598 msk->remote_key,
599 opts->addr_id,
600 &opts->addr);
3df523ab
PK
601 }
602#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1b1c7a0e 603 else if (saddr.family == AF_INET6) {
3df523ab 604 opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
1b1c7a0e 605 opts->addr6 = saddr.addr6;
3df523ab
PK
606 opts->ahmac = add_addr6_generate_hmac(msk->local_key,
607 msk->remote_key,
608 opts->addr_id,
609 &opts->addr6);
3df523ab
PK
610 }
611#endif
612 pr_debug("addr_id=%d, ahmac=%llu", opts->addr_id, opts->ahmac);
613
614 return true;
615}
616
6d0060f6
MM
617bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
618 unsigned int *size, unsigned int remaining,
619 struct mptcp_out_options *opts)
620{
621 unsigned int opt_size = 0;
622 bool ret = false;
623
3df523ab
PK
624 opts->suboptions = 0;
625
e1ff9e82
DC
626 if (unlikely(mptcp_check_fallback(sk)))
627 return false;
628
cc7972ea 629 if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
6d0060f6
MM
630 ret = true;
631 else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
632 opts))
633 ret = true;
634
635 /* we reserved enough space for the above options, and exceeding the
636 * TCP option space would be fatal
637 */
638 if (WARN_ON_ONCE(opt_size > remaining))
639 return false;
640
641 *size += opt_size;
642 remaining -= opt_size;
3df523ab
PK
643 if (mptcp_established_options_addr(sk, &opt_size, remaining, opts)) {
644 *size += opt_size;
645 remaining -= opt_size;
646 ret = true;
647 }
6d0060f6
MM
648
649 return ret;
650}
651
cec37a6e
PK
652bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
653 struct mptcp_out_options *opts)
654{
655 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
656
657 if (subflow_req->mp_capable) {
658 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
659 opts->sndr_key = subflow_req->local_key;
660 *size = TCPOLEN_MPTCP_MPC_SYNACK;
661 pr_debug("subflow_req=%p, local_key=%llu",
662 subflow_req, subflow_req->local_key);
663 return true;
f296234c
PK
664 } else if (subflow_req->mp_join) {
665 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
666 opts->backup = subflow_req->backup;
667 opts->join_id = subflow_req->local_id;
668 opts->thmac = subflow_req->thmac;
669 opts->nonce = subflow_req->local_nonce;
670 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
671 subflow_req, opts->backup, opts->join_id,
672 opts->thmac, opts->nonce);
673 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
674 return true;
cec37a6e
PK
675 }
676 return false;
677}
678
f296234c
PK
679static bool check_fully_established(struct mptcp_sock *msk, struct sock *sk,
680 struct mptcp_subflow_context *subflow,
0be534f5
PA
681 struct sk_buff *skb,
682 struct mptcp_options_received *mp_opt)
d22f4988
CP
683{
684 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
f296234c 685 * will make the subflow fully established
d22f4988 686 */
f296234c
PK
687 if (likely(subflow->fully_established)) {
688 /* on passive sockets, check for 3rd ack retransmission
689 * note that msk is always set by subflow_syn_recv_sock()
690 * for mp_join subflows
691 */
692 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
693 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
694 subflow->mp_join && mp_opt->mp_join &&
695 READ_ONCE(msk->pm.server_side))
696 tcp_send_ack(sk);
697 goto fully_established;
698 }
699
700 /* we should process OoO packets before the first subflow is fully
701 * established, but not expected for MP_JOIN subflows
702 */
703 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1)
704 return subflow->mp_capable;
d22f4988 705
5a91e32b 706 if (mp_opt->dss && mp_opt->use_ack) {
f296234c
PK
707 /* subflows are fully established as soon as we get any
708 * additional ack.
709 */
0be534f5 710 subflow->fully_established = 1;
b93df08c 711 WRITE_ONCE(msk->fully_established, true);
f296234c
PK
712 goto fully_established;
713 }
d22f4988 714
d22f4988
CP
715 /* If the first established packet does not contain MP_CAPABLE + data
716 * then fallback to TCP
717 */
718 if (!mp_opt->mp_capable) {
719 subflow->mp_capable = 0;
e1ff9e82
DC
720 pr_fallback(msk);
721 __mptcp_do_fallback(msk);
d22f4988
CP
722 return false;
723 }
f296234c 724
d6085fe1
PA
725 if (unlikely(!READ_ONCE(msk->pm.server_side)))
726 pr_warn_once("bogus mpc option on established client sk");
b93df08c 727 mptcp_subflow_fully_established(subflow, mp_opt);
f296234c
PK
728
729fully_established:
730 if (likely(subflow->pm_notified))
731 return true;
732
733 subflow->pm_notified = 1;
ec3edaa7
PK
734 if (subflow->mp_join) {
735 clear_3rdack_retransmission(sk);
f296234c 736 mptcp_pm_subflow_established(msk, subflow);
ec3edaa7 737 } else {
f296234c 738 mptcp_pm_fully_established(msk);
ec3edaa7 739 }
d22f4988
CP
740 return true;
741}
742
cc9d2566
PA
743static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
744{
745 u32 old_ack32, cur_ack32;
746
747 if (use_64bit)
748 return cur_ack;
749
750 old_ack32 = (u32)old_ack;
751 cur_ack32 = (u32)cur_ack;
752 cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
753 if (unlikely(before(cur_ack32, old_ack32)))
754 return cur_ack + (1LL << 32);
755 return cur_ack;
756}
757
758static void update_una(struct mptcp_sock *msk,
759 struct mptcp_options_received *mp_opt)
760{
761 u64 new_snd_una, snd_una, old_snd_una = atomic64_read(&msk->snd_una);
762 u64 write_seq = READ_ONCE(msk->write_seq);
763
764 /* avoid ack expansion on update conflict, to reduce the risk of
765 * wrongly expanding to a future ack sequence number, which is way
766 * more dangerous than missing an ack
767 */
768 new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
769
770 /* ACK for data not even sent yet? Ignore. */
771 if (after64(new_snd_una, write_seq))
772 new_snd_una = old_snd_una;
773
774 while (after64(new_snd_una, old_snd_una)) {
775 snd_una = old_snd_una;
776 old_snd_una = atomic64_cmpxchg(&msk->snd_una, snd_una,
777 new_snd_una);
b51f9b80
PA
778 if (old_snd_una == snd_una) {
779 mptcp_data_acked((struct sock *)msk);
cc9d2566 780 break;
b51f9b80 781 }
cc9d2566
PA
782 }
783}
784
1a49b2c2 785bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
3721b9b6
MM
786{
787 /* Skip if DATA_FIN was already received.
788 * If updating simultaneously with the recvmsg loop, values
789 * should match. If they mismatch, the peer is misbehaving and
790 * we will prefer the most recent information.
791 */
792 if (READ_ONCE(msk->rcv_data_fin) || !READ_ONCE(msk->first))
793 return false;
794
1a49b2c2
MM
795 WRITE_ONCE(msk->rcv_data_fin_seq,
796 expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
3721b9b6
MM
797 WRITE_ONCE(msk->rcv_data_fin, 1);
798
799 return true;
800}
801
1b1c7a0e
PK
802static bool add_addr_hmac_valid(struct mptcp_sock *msk,
803 struct mptcp_options_received *mp_opt)
804{
805 u64 hmac = 0;
806
807 if (mp_opt->echo)
808 return true;
809
810 if (mp_opt->family == MPTCP_ADDR_IPVERSION_4)
811 hmac = add_addr_generate_hmac(msk->remote_key,
812 msk->local_key,
813 mp_opt->addr_id, &mp_opt->addr);
814#if IS_ENABLED(CONFIG_MPTCP_IPV6)
815 else
816 hmac = add_addr6_generate_hmac(msk->remote_key,
817 msk->local_key,
818 mp_opt->addr_id, &mp_opt->addr6);
819#endif
820
821 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
822 msk, (unsigned long long)hmac,
823 (unsigned long long)mp_opt->ahmac);
824
825 return hmac == mp_opt->ahmac;
826}
827
648ef4b8
MM
828void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
829 struct tcp_options_received *opt_rx)
830{
d22f4988 831 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1b1c7a0e 832 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
cfde141e 833 struct mptcp_options_received mp_opt;
648ef4b8
MM
834 struct mptcp_ext *mpext;
835
e1ff9e82
DC
836 if (__mptcp_check_fallback(msk))
837 return;
838
cfde141e
PA
839 mptcp_get_options(skb, &mp_opt);
840 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
d22f4988 841 return;
648ef4b8 842
cfde141e 843 if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
1b1c7a0e
PK
844 struct mptcp_addr_info addr;
845
cfde141e
PA
846 addr.port = htons(mp_opt.port);
847 addr.id = mp_opt.addr_id;
848 if (mp_opt.family == MPTCP_ADDR_IPVERSION_4) {
1b1c7a0e 849 addr.family = AF_INET;
cfde141e 850 addr.addr = mp_opt.addr;
1b1c7a0e
PK
851 }
852#if IS_ENABLED(CONFIG_MPTCP_IPV6)
cfde141e 853 else if (mp_opt.family == MPTCP_ADDR_IPVERSION_6) {
1b1c7a0e 854 addr.family = AF_INET6;
cfde141e 855 addr.addr6 = mp_opt.addr6;
1b1c7a0e
PK
856 }
857#endif
cfde141e 858 if (!mp_opt.echo)
1b1c7a0e 859 mptcp_pm_add_addr_received(msk, &addr);
cfde141e 860 mp_opt.add_addr = 0;
1b1c7a0e
PK
861 }
862
cfde141e 863 if (!mp_opt.dss)
648ef4b8
MM
864 return;
865
cc9d2566
PA
866 /* we can't wait for recvmsg() to update the ack_seq, otherwise
867 * monodirectional flows will stuck
868 */
cfde141e
PA
869 if (mp_opt.use_ack)
870 update_una(msk, &mp_opt);
cc9d2566 871
06827b34
MM
872 /* Zero-data-length packets are dropped by the caller and not
873 * propagated to the MPTCP layer, so the skb extension does not
874 * need to be allocated or populated. DATA_FIN information, if
875 * present, needs to be updated here before the skb is freed.
43b54c6e
MM
876 */
877 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
878 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1a49b2c2 879 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
43b54c6e
MM
880 schedule_work(&msk->work))
881 sock_hold(subflow->conn);
06827b34
MM
882
883 return;
43b54c6e
MM
884 }
885
648ef4b8
MM
886 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
887 if (!mpext)
888 return;
889
890 memset(mpext, 0, sizeof(*mpext));
891
cfde141e
PA
892 if (mp_opt.use_map) {
893 if (mp_opt.mpc_map) {
cc7972ea
CP
894 /* this is an MP_CAPABLE carrying MPTCP data
895 * we know this map the first chunk of data
896 */
897 mptcp_crypto_key_sha(subflow->remote_key, NULL,
898 &mpext->data_seq);
899 mpext->data_seq++;
900 mpext->subflow_seq = 1;
901 mpext->dsn64 = 1;
902 mpext->mpc_map = 1;
a77895db 903 mpext->data_fin = 0;
cc7972ea 904 } else {
cfde141e
PA
905 mpext->data_seq = mp_opt.data_seq;
906 mpext->subflow_seq = mp_opt.subflow_seq;
907 mpext->dsn64 = mp_opt.dsn64;
908 mpext->data_fin = mp_opt.data_fin;
cc7972ea 909 }
cfde141e 910 mpext->data_len = mp_opt.data_len;
648ef4b8 911 mpext->use_map = 1;
648ef4b8 912 }
648ef4b8
MM
913}
914
eda7acdd
PK
915void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts)
916{
cc7972ea 917 if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
eda7acdd
PK
918 OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
919 u8 len;
920
921 if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
922 len = TCPOLEN_MPTCP_MPC_SYN;
cec37a6e
PK
923 else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
924 len = TCPOLEN_MPTCP_MPC_SYNACK;
cc7972ea
CP
925 else if (opts->ext_copy.data_len)
926 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
eda7acdd
PK
927 else
928 len = TCPOLEN_MPTCP_MPC_ACK;
929
3df523ab
PK
930 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
931 MPTCP_SUPPORTED_VERSION,
932 MPTCP_CAP_HMAC_SHA256);
cc7972ea
CP
933
934 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
935 opts->suboptions))
936 goto mp_capable_done;
937
eda7acdd
PK
938 put_unaligned_be64(opts->sndr_key, ptr);
939 ptr += 2;
cc7972ea
CP
940 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
941 goto mp_capable_done;
942
943 put_unaligned_be64(opts->rcvr_key, ptr);
944 ptr += 2;
945 if (!opts->ext_copy.data_len)
946 goto mp_capable_done;
947
948 put_unaligned_be32(opts->ext_copy.data_len << 16 |
949 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
950 ptr += 1;
eda7acdd 951 }
6d0060f6 952
cc7972ea 953mp_capable_done:
3df523ab
PK
954 if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
955 if (opts->ahmac)
956 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
957 TCPOLEN_MPTCP_ADD_ADDR, 0,
958 opts->addr_id);
959 else
960 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
961 TCPOLEN_MPTCP_ADD_ADDR_BASE,
962 MPTCP_ADDR_ECHO,
963 opts->addr_id);
964 memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4);
965 ptr += 1;
966 if (opts->ahmac) {
967 put_unaligned_be64(opts->ahmac, ptr);
968 ptr += 2;
969 }
970 }
971
972#if IS_ENABLED(CONFIG_MPTCP_IPV6)
973 if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) {
974 if (opts->ahmac)
975 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
976 TCPOLEN_MPTCP_ADD_ADDR6, 0,
977 opts->addr_id);
978 else
979 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
980 TCPOLEN_MPTCP_ADD_ADDR6_BASE,
981 MPTCP_ADDR_ECHO,
982 opts->addr_id);
983 memcpy((u8 *)ptr, opts->addr6.s6_addr, 16);
984 ptr += 4;
985 if (opts->ahmac) {
986 put_unaligned_be64(opts->ahmac, ptr);
987 ptr += 2;
988 }
989 }
990#endif
991
992 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
993 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
994 TCPOLEN_MPTCP_RM_ADDR_BASE,
995 0, opts->rm_id);
996 }
997
ec3edaa7
PK
998 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
999 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1000 TCPOLEN_MPTCP_MPJ_SYN,
1001 opts->backup, opts->join_id);
1002 put_unaligned_be32(opts->token, ptr);
1003 ptr += 1;
1004 put_unaligned_be32(opts->nonce, ptr);
1005 ptr += 1;
1006 }
1007
f296234c
PK
1008 if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1009 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1010 TCPOLEN_MPTCP_MPJ_SYNACK,
1011 opts->backup, opts->join_id);
1012 put_unaligned_be64(opts->thmac, ptr);
1013 ptr += 2;
1014 put_unaligned_be32(opts->nonce, ptr);
1015 ptr += 1;
1016 }
1017
ec3edaa7
PK
1018 if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1019 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1020 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1021 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1022 ptr += 5;
1023 }
1024
6d0060f6
MM
1025 if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
1026 struct mptcp_ext *mpext = &opts->ext_copy;
1027 u8 len = TCPOLEN_MPTCP_DSS_BASE;
1028 u8 flags = 0;
1029
1030 if (mpext->use_ack) {
a0c1d0ea
CP
1031 flags = MPTCP_DSS_HAS_ACK;
1032 if (mpext->ack64) {
1033 len += TCPOLEN_MPTCP_DSS_ACK64;
1034 flags |= MPTCP_DSS_ACK64;
1035 } else {
1036 len += TCPOLEN_MPTCP_DSS_ACK32;
1037 }
6d0060f6
MM
1038 }
1039
1040 if (mpext->use_map) {
1041 len += TCPOLEN_MPTCP_DSS_MAP64;
1042
1043 /* Use only 64-bit mapping flags for now, add
1044 * support for optional 32-bit mappings later.
1045 */
1046 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1047 if (mpext->data_fin)
1048 flags |= MPTCP_DSS_DATA_FIN;
1049 }
1050
3df523ab 1051 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
6d0060f6
MM
1052
1053 if (mpext->use_ack) {
a0c1d0ea
CP
1054 if (mpext->ack64) {
1055 put_unaligned_be64(mpext->data_ack, ptr);
1056 ptr += 2;
1057 } else {
1058 put_unaligned_be32(mpext->data_ack32, ptr);
1059 ptr += 1;
1060 }
6d0060f6
MM
1061 }
1062
1063 if (mpext->use_map) {
1064 put_unaligned_be64(mpext->data_seq, ptr);
1065 ptr += 2;
1066 put_unaligned_be32(mpext->subflow_seq, ptr);
1067 ptr += 1;
1068 put_unaligned_be32(mpext->data_len << 16 |
1069 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1070 }
1071 }
eda7acdd 1072}