Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
7#define pr_fmt(fmt) "MPTCP: " fmt
8
9#include <linux/kernel.h>
10#include <crypto/sha.h>
11#include <net/tcp.h>
12#include <net/mptcp.h>
13#include "protocol.h"
14
15static bool mptcp_cap_flag_sha256(u8 flags)
16{
17 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
18}
19
20static void mptcp_parse_option(const struct sk_buff *skb,
21 const unsigned char *ptr, int opsize,
22 struct mptcp_options_received *mp_opt)
23{
24 u8 subtype = *ptr >> 4;
25 int expected_opsize;
26 u8 version;
27 u8 flags;
28
29 switch (subtype) {
30 case MPTCPOPT_MP_CAPABLE:
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)
44 break;
45
46 /* try to be gentle vs future versions on the initial syn */
47 version = *ptr++ & MPTCP_VERSION_MASK;
48 if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
49 if (version != MPTCP_SUPPORTED_VERSION)
50 break;
51 } else if (version < MPTCP_SUPPORTED_VERSION) {
52 break;
53 }
54
55 flags = *ptr++;
56 if (!mptcp_cap_flag_sha256(flags) ||
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;
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) {
82 mp_opt->rcvr_key = get_unaligned_be64(ptr);
83 ptr += 8;
84 }
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);
100 break;
101
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
134 case MPTCPOPT_DSS:
135 pr_debug("DSS");
136 ptr++;
137
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;
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
180 mp_opt->dss = 1;
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
214 break;
215
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
276 ptr++;
277
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
283 default:
284 break;
285 }
286}
287
288void mptcp_get_options(const struct sk_buff *skb,
289 struct mptcp_options_received *mp_opt)
290{
291 const struct tcphdr *th = tcp_hdr(skb);
292 const unsigned char *ptr;
293 int length;
294
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);
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)
322 mptcp_parse_option(skb, ptr, opsize, mp_opt);
323 ptr += opsize - 2;
324 length -= opsize;
325 }
326 }
327}
328
329bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
330 unsigned int *size, struct mptcp_out_options *opts)
331{
332 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
333
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;
338 if (subflow->request_mptcp) {
339 opts->suboptions = OPTION_MPTCP_MPC_SYN;
340 *size = TCPOLEN_MPTCP_MPC_SYN;
341 return true;
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;
352 }
353 return false;
354}
355
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
388static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
389 unsigned int *size,
390 unsigned int remaining,
391 struct mptcp_out_options *opts)
392{
393 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
394 struct mptcp_ext *mpext;
395 unsigned int data_len;
396
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;
405
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) {
412 mpext = mptcp_get_ext(skb);
413 data_len = mpext ? mpext->data_len : 0;
414
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;
420 opts->suboptions = OPTION_MPTCP_MPC_ACK;
421 opts->sndr_key = subflow->local_key;
422 opts->rcvr_key = subflow->remote_key;
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
438 return true;
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;
447 }
448 return false;
449}
450
451static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
452 struct sk_buff *skb, struct mptcp_ext *ext)
453{
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;
458
459 if (!ext->use_map || !skb->len) {
460 /* RFC6824 requires a DSS mapping with specific values
461 * if DATA_FIN is set but no data payload is mapped
462 */
463 ext->data_fin = 1;
464 ext->use_map = 1;
465 ext->dsn64 = 1;
466 ext->data_seq = data_fin_tx_seq;
467 ext->subflow_seq = 0;
468 ext->data_len = 1;
469 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
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.
473 */
474 ext->data_fin = 1;
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);
485 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
486 unsigned int dss_size = 0;
487 u64 snd_data_fin_enable;
488 struct mptcp_ext *mpext;
489 unsigned int ack_size;
490 bool ret = false;
491
492 mpext = skb ? mptcp_get_ext(skb) : NULL;
493 snd_data_fin_enable = READ_ONCE(msk->snd_data_fin_enable);
494
495 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
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
505 if (skb && snd_data_fin_enable)
506 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
507 ret = true;
508 }
509
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 */
513 opts->ext_copy.use_ack = 0;
514 if (!READ_ONCE(msk->can_ack)) {
515 *size = ALIGN(dss_size, 4);
516 return ret;
517 }
518
519 if (subflow->use_64bit_ack) {
520 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
521 opts->ext_copy.data_ack = READ_ONCE(msk->ack_seq);
522 opts->ext_copy.ack64 = 1;
523 } else {
524 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
525 opts->ext_copy.data_ack32 = (uint32_t)READ_ONCE(msk->ack_seq);
526 opts->ext_copy.ack64 = 0;
527 }
528 opts->ext_copy.use_ack = 1;
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
536 *size = ALIGN(dss_size, 4);
537 return true;
538}
539
540static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
541 struct in_addr *addr)
542{
543 u8 hmac[SHA256_DIGEST_SIZE];
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
553 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
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{
560 u8 hmac[SHA256_DIGEST_SIZE];
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
570 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
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);
581 struct mptcp_addr_info saddr;
582 int len;
583
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;
591
592 *size = len;
593 opts->addr_id = saddr.id;
594 if (saddr.family == AF_INET) {
595 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
596 opts->addr = saddr.addr;
597 opts->ahmac = add_addr_generate_hmac(msk->local_key,
598 msk->remote_key,
599 opts->addr_id,
600 &opts->addr);
601 }
602#if IS_ENABLED(CONFIG_MPTCP_IPV6)
603 else if (saddr.family == AF_INET6) {
604 opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
605 opts->addr6 = saddr.addr6;
606 opts->ahmac = add_addr6_generate_hmac(msk->local_key,
607 msk->remote_key,
608 opts->addr_id,
609 &opts->addr6);
610 }
611#endif
612 pr_debug("addr_id=%d, ahmac=%llu", opts->addr_id, opts->ahmac);
613
614 return true;
615}
616
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
624 opts->suboptions = 0;
625
626 if (unlikely(mptcp_check_fallback(sk)))
627 return false;
628
629 if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
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;
643 if (mptcp_established_options_addr(sk, &opt_size, remaining, opts)) {
644 *size += opt_size;
645 remaining -= opt_size;
646 ret = true;
647 }
648
649 return ret;
650}
651
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;
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;
675 }
676 return false;
677}
678
679static bool check_fully_established(struct mptcp_sock *msk, struct sock *sk,
680 struct mptcp_subflow_context *subflow,
681 struct sk_buff *skb,
682 struct mptcp_options_received *mp_opt)
683{
684 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
685 * will make the subflow fully established
686 */
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;
705
706 if (mp_opt->dss && mp_opt->use_ack) {
707 /* subflows are fully established as soon as we get any
708 * additional ack.
709 */
710 subflow->fully_established = 1;
711 WRITE_ONCE(msk->fully_established, true);
712 goto fully_established;
713 }
714
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;
720 pr_fallback(msk);
721 __mptcp_do_fallback(msk);
722 return false;
723 }
724
725 if (unlikely(!READ_ONCE(msk->pm.server_side)))
726 pr_warn_once("bogus mpc option on established client sk");
727 mptcp_subflow_fully_established(subflow, mp_opt);
728
729fully_established:
730 if (likely(subflow->pm_notified))
731 return true;
732
733 subflow->pm_notified = 1;
734 if (subflow->mp_join) {
735 clear_3rdack_retransmission(sk);
736 mptcp_pm_subflow_established(msk, subflow);
737 } else {
738 mptcp_pm_fully_established(msk);
739 }
740 return true;
741}
742
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);
778 if (old_snd_una == snd_una) {
779 mptcp_data_acked((struct sock *)msk);
780 break;
781 }
782 }
783}
784
785bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
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
795 WRITE_ONCE(msk->rcv_data_fin_seq,
796 expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
797 WRITE_ONCE(msk->rcv_data_fin, 1);
798
799 return true;
800}
801
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
828void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
829 struct tcp_options_received *opt_rx)
830{
831 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
832 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
833 struct mptcp_options_received mp_opt;
834 struct mptcp_ext *mpext;
835
836 if (__mptcp_check_fallback(msk))
837 return;
838
839 mptcp_get_options(skb, &mp_opt);
840 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
841 return;
842
843 if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
844 struct mptcp_addr_info addr;
845
846 addr.port = htons(mp_opt.port);
847 addr.id = mp_opt.addr_id;
848 if (mp_opt.family == MPTCP_ADDR_IPVERSION_4) {
849 addr.family = AF_INET;
850 addr.addr = mp_opt.addr;
851 }
852#if IS_ENABLED(CONFIG_MPTCP_IPV6)
853 else if (mp_opt.family == MPTCP_ADDR_IPVERSION_6) {
854 addr.family = AF_INET6;
855 addr.addr6 = mp_opt.addr6;
856 }
857#endif
858 if (!mp_opt.echo)
859 mptcp_pm_add_addr_received(msk, &addr);
860 mp_opt.add_addr = 0;
861 }
862
863 if (!mp_opt.dss)
864 return;
865
866 /* we can't wait for recvmsg() to update the ack_seq, otherwise
867 * monodirectional flows will stuck
868 */
869 if (mp_opt.use_ack)
870 update_una(msk, &mp_opt);
871
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.
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 &&
879 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
880 schedule_work(&msk->work))
881 sock_hold(subflow->conn);
882
883 return;
884 }
885
886 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
887 if (!mpext)
888 return;
889
890 memset(mpext, 0, sizeof(*mpext));
891
892 if (mp_opt.use_map) {
893 if (mp_opt.mpc_map) {
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;
903 mpext->data_fin = 0;
904 } else {
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;
909 }
910 mpext->data_len = mp_opt.data_len;
911 mpext->use_map = 1;
912 }
913}
914
915void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts)
916{
917 if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
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;
923 else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
924 len = TCPOLEN_MPTCP_MPC_SYNACK;
925 else if (opts->ext_copy.data_len)
926 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
927 else
928 len = TCPOLEN_MPTCP_MPC_ACK;
929
930 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
931 MPTCP_SUPPORTED_VERSION,
932 MPTCP_CAP_HMAC_SHA256);
933
934 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
935 opts->suboptions))
936 goto mp_capable_done;
937
938 put_unaligned_be64(opts->sndr_key, ptr);
939 ptr += 2;
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;
951 }
952
953mp_capable_done:
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
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
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
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
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) {
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 }
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
1051 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1052
1053 if (mpext->use_ack) {
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 }
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 }
1072}
1// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
7#define pr_fmt(fmt) "MPTCP: " fmt
8
9#include <linux/kernel.h>
10#include <crypto/sha2.h>
11#include <net/tcp.h>
12#include <net/mptcp.h>
13#include "protocol.h"
14#include "mib.h"
15
16#include <trace/events/mptcp.h>
17
18static bool mptcp_cap_flag_sha256(u8 flags)
19{
20 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
21}
22
23static void mptcp_parse_option(const struct sk_buff *skb,
24 const unsigned char *ptr, int opsize,
25 struct mptcp_options_received *mp_opt)
26{
27 u8 subtype = *ptr >> 4;
28 int expected_opsize;
29 u8 version;
30 u8 flags;
31 u8 i;
32
33 switch (subtype) {
34 case MPTCPOPT_MP_CAPABLE:
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 }
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))
61 break;
62
63 /* try to be gentle vs future versions on the initial syn */
64 version = *ptr++ & MPTCP_VERSION_MASK;
65 if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
66 if (version != MPTCP_SUPPORTED_VERSION)
67 break;
68 } else if (version < MPTCP_SUPPORTED_VERSION) {
69 break;
70 }
71
72 flags = *ptr++;
73 if (!mptcp_cap_flag_sha256(flags) ||
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."
82 */
83 if (flags & MPTCP_CAP_CHECKSUM_REQD)
84 mp_opt->csum_reqd = 1;
85
86 if (flags & MPTCP_CAP_DENY_JOIN_ID0)
87 mp_opt->deny_join_id0 = 1;
88
89 mp_opt->mp_capable = 1;
90 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
91 mp_opt->sndr_key = get_unaligned_be64(ptr);
92 ptr += 8;
93 }
94 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
95 mp_opt->rcvr_key = get_unaligned_be64(ptr);
96 ptr += 8;
97 }
98 if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
99 /* Section 3.1.:
100 * "the data parameters in a MP_CAPABLE are semantically
101 * equivalent to those in a DSS option and can be used
102 * interchangeably."
103 */
104 mp_opt->dss = 1;
105 mp_opt->use_map = 1;
106 mp_opt->mpc_map = 1;
107 mp_opt->data_len = get_unaligned_be16(ptr);
108 ptr += 2;
109 }
110 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
111 mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
112 mp_opt->csum_reqd = 1;
113 ptr += 2;
114 }
115 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
116 version, flags, opsize, mp_opt->sndr_key,
117 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
118 break;
119
120 case MPTCPOPT_MP_JOIN:
121 mp_opt->mp_join = 1;
122 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
123 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
124 mp_opt->join_id = *ptr++;
125 mp_opt->token = get_unaligned_be32(ptr);
126 ptr += 4;
127 mp_opt->nonce = get_unaligned_be32(ptr);
128 ptr += 4;
129 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
130 mp_opt->backup, mp_opt->join_id,
131 mp_opt->token, mp_opt->nonce);
132 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
133 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
134 mp_opt->join_id = *ptr++;
135 mp_opt->thmac = get_unaligned_be64(ptr);
136 ptr += 8;
137 mp_opt->nonce = get_unaligned_be32(ptr);
138 ptr += 4;
139 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
140 mp_opt->backup, mp_opt->join_id,
141 mp_opt->thmac, mp_opt->nonce);
142 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
143 ptr += 2;
144 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
145 pr_debug("MP_JOIN hmac");
146 } else {
147 mp_opt->mp_join = 0;
148 }
149 break;
150
151 case MPTCPOPT_DSS:
152 pr_debug("DSS");
153 ptr++;
154
155 /* we must clear 'mpc_map' be able to detect MP_CAPABLE
156 * map vs DSS map in mptcp_incoming_options(), and reconstruct
157 * map info accordingly
158 */
159 mp_opt->mpc_map = 0;
160 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
161 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
162 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
163 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
164 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
165 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
166
167 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
168 mp_opt->data_fin, mp_opt->dsn64,
169 mp_opt->use_map, mp_opt->ack64,
170 mp_opt->use_ack);
171
172 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
173
174 if (mp_opt->use_ack) {
175 if (mp_opt->ack64)
176 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
177 else
178 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
179 }
180
181 if (mp_opt->use_map) {
182 if (mp_opt->dsn64)
183 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
184 else
185 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
186 }
187
188 /* Always parse any csum presence combination, we will enforce
189 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
190 */
191 if (opsize != expected_opsize &&
192 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
193 break;
194
195 mp_opt->dss = 1;
196
197 if (mp_opt->use_ack) {
198 if (mp_opt->ack64) {
199 mp_opt->data_ack = get_unaligned_be64(ptr);
200 ptr += 8;
201 } else {
202 mp_opt->data_ack = get_unaligned_be32(ptr);
203 ptr += 4;
204 }
205
206 pr_debug("data_ack=%llu", mp_opt->data_ack);
207 }
208
209 if (mp_opt->use_map) {
210 if (mp_opt->dsn64) {
211 mp_opt->data_seq = get_unaligned_be64(ptr);
212 ptr += 8;
213 } else {
214 mp_opt->data_seq = get_unaligned_be32(ptr);
215 ptr += 4;
216 }
217
218 mp_opt->subflow_seq = get_unaligned_be32(ptr);
219 ptr += 4;
220
221 mp_opt->data_len = get_unaligned_be16(ptr);
222 ptr += 2;
223
224 if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
225 mp_opt->csum_reqd = 1;
226 mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
227 ptr += 2;
228 }
229
230 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
231 mp_opt->data_seq, mp_opt->subflow_seq,
232 mp_opt->data_len, mp_opt->csum_reqd, mp_opt->csum);
233 }
234
235 break;
236
237 case MPTCPOPT_ADD_ADDR:
238 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
239 if (!mp_opt->echo) {
240 if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
241 opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
242 mp_opt->addr.family = AF_INET;
243#if IS_ENABLED(CONFIG_MPTCP_IPV6)
244 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
245 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
246 mp_opt->addr.family = AF_INET6;
247#endif
248 else
249 break;
250 } else {
251 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
252 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
253 mp_opt->addr.family = AF_INET;
254#if IS_ENABLED(CONFIG_MPTCP_IPV6)
255 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
256 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
257 mp_opt->addr.family = AF_INET6;
258#endif
259 else
260 break;
261 }
262
263 mp_opt->add_addr = 1;
264 mp_opt->addr.id = *ptr++;
265 if (mp_opt->addr.family == AF_INET) {
266 memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
267 ptr += 4;
268 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
269 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
270 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
271 ptr += 2;
272 }
273 }
274#if IS_ENABLED(CONFIG_MPTCP_IPV6)
275 else {
276 memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
277 ptr += 16;
278 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
279 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
280 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
281 ptr += 2;
282 }
283 }
284#endif
285 if (!mp_opt->echo) {
286 mp_opt->ahmac = get_unaligned_be64(ptr);
287 ptr += 8;
288 }
289 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
290 (mp_opt->addr.family == AF_INET6) ? "6" : "",
291 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
292 break;
293
294 case MPTCPOPT_RM_ADDR:
295 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
296 opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
297 break;
298
299 ptr++;
300
301 mp_opt->rm_addr = 1;
302 mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
303 for (i = 0; i < mp_opt->rm_list.nr; i++)
304 mp_opt->rm_list.ids[i] = *ptr++;
305 pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
306 break;
307
308 case MPTCPOPT_MP_PRIO:
309 if (opsize != TCPOLEN_MPTCP_PRIO)
310 break;
311
312 mp_opt->mp_prio = 1;
313 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
314 pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
315 break;
316
317 case MPTCPOPT_MP_FASTCLOSE:
318 if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
319 break;
320
321 ptr += 2;
322 mp_opt->rcvr_key = get_unaligned_be64(ptr);
323 ptr += 8;
324 mp_opt->fastclose = 1;
325 break;
326
327 case MPTCPOPT_RST:
328 if (opsize != TCPOLEN_MPTCP_RST)
329 break;
330
331 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
332 break;
333 mp_opt->reset = 1;
334 flags = *ptr++;
335 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
336 mp_opt->reset_reason = *ptr;
337 break;
338
339 default:
340 break;
341 }
342}
343
344void mptcp_get_options(const struct sock *sk,
345 const struct sk_buff *skb,
346 struct mptcp_options_received *mp_opt)
347{
348 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
349 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
350 const struct tcphdr *th = tcp_hdr(skb);
351 const unsigned char *ptr;
352 int length;
353
354 /* initialize option status */
355 mp_opt->mp_capable = 0;
356 mp_opt->mp_join = 0;
357 mp_opt->add_addr = 0;
358 mp_opt->ahmac = 0;
359 mp_opt->fastclose = 0;
360 mp_opt->addr.port = 0;
361 mp_opt->rm_addr = 0;
362 mp_opt->dss = 0;
363 mp_opt->mp_prio = 0;
364 mp_opt->reset = 0;
365 mp_opt->csum_reqd = READ_ONCE(msk->csum_enabled);
366 mp_opt->deny_join_id0 = 0;
367
368 length = (th->doff * 4) - sizeof(struct tcphdr);
369 ptr = (const unsigned char *)(th + 1);
370
371 while (length > 0) {
372 int opcode = *ptr++;
373 int opsize;
374
375 switch (opcode) {
376 case TCPOPT_EOL:
377 return;
378 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
379 length--;
380 continue;
381 default:
382 if (length < 2)
383 return;
384 opsize = *ptr++;
385 if (opsize < 2) /* "silly options" */
386 return;
387 if (opsize > length)
388 return; /* don't parse partial options */
389 if (opcode == TCPOPT_MPTCP)
390 mptcp_parse_option(skb, ptr, opsize, mp_opt);
391 ptr += opsize - 2;
392 length -= opsize;
393 }
394 }
395}
396
397bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
398 unsigned int *size, struct mptcp_out_options *opts)
399{
400 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
401
402 /* we will use snd_isn to detect first pkt [re]transmission
403 * in mptcp_established_options_mp()
404 */
405 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
406 if (subflow->request_mptcp) {
407 opts->suboptions = OPTION_MPTCP_MPC_SYN;
408 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
409 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
410 *size = TCPOLEN_MPTCP_MPC_SYN;
411 return true;
412 } else if (subflow->request_join) {
413 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
414 subflow->local_nonce);
415 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
416 opts->join_id = subflow->local_id;
417 opts->token = subflow->remote_token;
418 opts->nonce = subflow->local_nonce;
419 opts->backup = subflow->request_bkup;
420 *size = TCPOLEN_MPTCP_MPJ_SYN;
421 return true;
422 }
423 return false;
424}
425
426/* MP_JOIN client subflow must wait for 4th ack before sending any data:
427 * TCP can't schedule delack timer before the subflow is fully established.
428 * MPTCP uses the delack timer to do 3rd ack retransmissions
429 */
430static void schedule_3rdack_retransmission(struct sock *sk)
431{
432 struct inet_connection_sock *icsk = inet_csk(sk);
433 struct tcp_sock *tp = tcp_sk(sk);
434 unsigned long timeout;
435
436 /* reschedule with a timeout above RTT, as we must look only for drop */
437 if (tp->srtt_us)
438 timeout = tp->srtt_us << 1;
439 else
440 timeout = TCP_TIMEOUT_INIT;
441
442 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
443 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
444 icsk->icsk_ack.timeout = timeout;
445 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
446}
447
448static void clear_3rdack_retransmission(struct sock *sk)
449{
450 struct inet_connection_sock *icsk = inet_csk(sk);
451
452 sk_stop_timer(sk, &icsk->icsk_delack_timer);
453 icsk->icsk_ack.timeout = 0;
454 icsk->icsk_ack.ato = 0;
455 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
456}
457
458static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
459 bool snd_data_fin_enable,
460 unsigned int *size,
461 unsigned int remaining,
462 struct mptcp_out_options *opts)
463{
464 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
465 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
466 struct mptcp_ext *mpext;
467 unsigned int data_len;
468 u8 len;
469
470 /* When skb is not available, we better over-estimate the emitted
471 * options len. A full DSS option (28 bytes) is longer than
472 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
473 * tell the caller to defer the estimate to
474 * mptcp_established_options_dss(), which will reserve enough space.
475 */
476 if (!skb)
477 return false;
478
479 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
480 if (subflow->fully_established || snd_data_fin_enable ||
481 subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
482 sk->sk_state != TCP_ESTABLISHED)
483 return false;
484
485 if (subflow->mp_capable) {
486 mpext = mptcp_get_ext(skb);
487 data_len = mpext ? mpext->data_len : 0;
488
489 /* we will check ext_copy.data_len in mptcp_write_options() to
490 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
491 * TCPOLEN_MPTCP_MPC_ACK
492 */
493 opts->ext_copy.data_len = data_len;
494 opts->suboptions = OPTION_MPTCP_MPC_ACK;
495 opts->sndr_key = subflow->local_key;
496 opts->rcvr_key = subflow->remote_key;
497 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
498 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
499
500 /* Section 3.1.
501 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
502 * packets that start the first subflow of an MPTCP connection,
503 * as well as the first packet that carries data
504 */
505 if (data_len > 0) {
506 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
507 if (opts->csum_reqd) {
508 /* we need to propagate more info to csum the pseudo hdr */
509 opts->ext_copy.data_seq = mpext->data_seq;
510 opts->ext_copy.subflow_seq = mpext->subflow_seq;
511 opts->ext_copy.csum = mpext->csum;
512 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
513 }
514 *size = ALIGN(len, 4);
515 } else {
516 *size = TCPOLEN_MPTCP_MPC_ACK;
517 }
518
519 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
520 subflow, subflow->local_key, subflow->remote_key,
521 data_len);
522
523 return true;
524 } else if (subflow->mp_join) {
525 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
526 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
527 *size = TCPOLEN_MPTCP_MPJ_ACK;
528 pr_debug("subflow=%p", subflow);
529
530 schedule_3rdack_retransmission(sk);
531 return true;
532 }
533 return false;
534}
535
536static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
537 struct sk_buff *skb, struct mptcp_ext *ext)
538{
539 /* The write_seq value has already been incremented, so the actual
540 * sequence number for the DATA_FIN is one less.
541 */
542 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
543
544 if (!ext->use_map || !skb->len) {
545 /* RFC6824 requires a DSS mapping with specific values
546 * if DATA_FIN is set but no data payload is mapped
547 */
548 ext->data_fin = 1;
549 ext->use_map = 1;
550 ext->dsn64 = 1;
551 ext->data_seq = data_fin_tx_seq;
552 ext->subflow_seq = 0;
553 ext->data_len = 1;
554 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
555 /* If there's an existing DSS mapping and it is the
556 * final mapping, DATA_FIN consumes 1 additional byte of
557 * mapping space.
558 */
559 ext->data_fin = 1;
560 ext->data_len++;
561 }
562}
563
564static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
565 bool snd_data_fin_enable,
566 unsigned int *size,
567 unsigned int remaining,
568 struct mptcp_out_options *opts)
569{
570 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
571 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
572 unsigned int dss_size = 0;
573 struct mptcp_ext *mpext;
574 unsigned int ack_size;
575 bool ret = false;
576 u64 ack_seq;
577
578 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
579 mpext = skb ? mptcp_get_ext(skb) : NULL;
580
581 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
582 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
583
584 if (mpext) {
585 if (opts->csum_reqd)
586 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
587
588 opts->ext_copy = *mpext;
589 }
590
591 remaining -= map_size;
592 dss_size = map_size;
593 if (skb && snd_data_fin_enable)
594 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
595 ret = true;
596 }
597
598 /* passive sockets msk will set the 'can_ack' after accept(), even
599 * if the first subflow may have the already the remote key handy
600 */
601 opts->ext_copy.use_ack = 0;
602 if (!READ_ONCE(msk->can_ack)) {
603 *size = ALIGN(dss_size, 4);
604 return ret;
605 }
606
607 ack_seq = READ_ONCE(msk->ack_seq);
608 if (READ_ONCE(msk->use_64bit_ack)) {
609 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
610 opts->ext_copy.data_ack = ack_seq;
611 opts->ext_copy.ack64 = 1;
612 } else {
613 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
614 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
615 opts->ext_copy.ack64 = 0;
616 }
617 opts->ext_copy.use_ack = 1;
618 WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
619
620 /* Add kind/length/subtype/flag overhead if mapping is not populated */
621 if (dss_size == 0)
622 ack_size += TCPOLEN_MPTCP_DSS_BASE;
623
624 dss_size += ack_size;
625
626 *size = ALIGN(dss_size, 4);
627 return true;
628}
629
630static u64 add_addr_generate_hmac(u64 key1, u64 key2,
631 struct mptcp_addr_info *addr)
632{
633 u16 port = ntohs(addr->port);
634 u8 hmac[SHA256_DIGEST_SIZE];
635 u8 msg[19];
636 int i = 0;
637
638 msg[i++] = addr->id;
639 if (addr->family == AF_INET) {
640 memcpy(&msg[i], &addr->addr.s_addr, 4);
641 i += 4;
642 }
643#if IS_ENABLED(CONFIG_MPTCP_IPV6)
644 else if (addr->family == AF_INET6) {
645 memcpy(&msg[i], &addr->addr6.s6_addr, 16);
646 i += 16;
647 }
648#endif
649 msg[i++] = port >> 8;
650 msg[i++] = port & 0xFF;
651
652 mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
653
654 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
655}
656
657static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
658 unsigned int *size,
659 unsigned int remaining,
660 struct mptcp_out_options *opts)
661{
662 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
663 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
664 bool drop_other_suboptions = false;
665 unsigned int opt_size = *size;
666 bool echo;
667 bool port;
668 int len;
669
670 if ((mptcp_pm_should_add_signal_ipv6(msk) ||
671 mptcp_pm_should_add_signal_port(msk) ||
672 mptcp_pm_should_add_signal_echo(msk)) &&
673 skb && skb_is_tcp_pure_ack(skb)) {
674 pr_debug("drop other suboptions");
675 opts->suboptions = 0;
676 opts->ext_copy.use_ack = 0;
677 opts->ext_copy.use_map = 0;
678 remaining += opt_size;
679 drop_other_suboptions = true;
680 }
681
682 if (!mptcp_pm_should_add_signal(msk) ||
683 !(mptcp_pm_add_addr_signal(msk, remaining, &opts->addr, &echo, &port)))
684 return false;
685
686 len = mptcp_add_addr_len(opts->addr.family, echo, port);
687 if (remaining < len)
688 return false;
689
690 *size = len;
691 if (drop_other_suboptions)
692 *size -= opt_size;
693 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
694 if (!echo) {
695 opts->ahmac = add_addr_generate_hmac(msk->local_key,
696 msk->remote_key,
697 &opts->addr);
698 }
699 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
700 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
701
702 return true;
703}
704
705static bool mptcp_established_options_rm_addr(struct sock *sk,
706 unsigned int *size,
707 unsigned int remaining,
708 struct mptcp_out_options *opts)
709{
710 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
711 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
712 struct mptcp_rm_list rm_list;
713 int i, len;
714
715 if (!mptcp_pm_should_rm_signal(msk) ||
716 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
717 return false;
718
719 len = mptcp_rm_addr_len(&rm_list);
720 if (len < 0)
721 return false;
722 if (remaining < len)
723 return false;
724
725 *size = len;
726 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
727 opts->rm_list = rm_list;
728
729 for (i = 0; i < opts->rm_list.nr; i++)
730 pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
731
732 return true;
733}
734
735static bool mptcp_established_options_mp_prio(struct sock *sk,
736 unsigned int *size,
737 unsigned int remaining,
738 struct mptcp_out_options *opts)
739{
740 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
741
742 if (!subflow->send_mp_prio)
743 return false;
744
745 /* account for the trailing 'nop' option */
746 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
747 return false;
748
749 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
750 opts->suboptions |= OPTION_MPTCP_PRIO;
751 opts->backup = subflow->request_bkup;
752
753 pr_debug("prio=%d", opts->backup);
754
755 return true;
756}
757
758static noinline void mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
759 unsigned int *size,
760 unsigned int remaining,
761 struct mptcp_out_options *opts)
762{
763 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
764
765 if (remaining < TCPOLEN_MPTCP_RST)
766 return;
767
768 *size = TCPOLEN_MPTCP_RST;
769 opts->suboptions |= OPTION_MPTCP_RST;
770 opts->reset_transient = subflow->reset_transient;
771 opts->reset_reason = subflow->reset_reason;
772}
773
774bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
775 unsigned int *size, unsigned int remaining,
776 struct mptcp_out_options *opts)
777{
778 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
779 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
780 unsigned int opt_size = 0;
781 bool snd_data_fin;
782 bool ret = false;
783
784 opts->suboptions = 0;
785
786 if (unlikely(__mptcp_check_fallback(msk)))
787 return false;
788
789 if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
790 mptcp_established_options_rst(sk, skb, size, remaining, opts);
791 return true;
792 }
793
794 snd_data_fin = mptcp_data_fin_enabled(msk);
795 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
796 ret = true;
797 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts))
798 ret = true;
799
800 /* we reserved enough space for the above options, and exceeding the
801 * TCP option space would be fatal
802 */
803 if (WARN_ON_ONCE(opt_size > remaining))
804 return false;
805
806 *size += opt_size;
807 remaining -= opt_size;
808 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
809 *size += opt_size;
810 remaining -= opt_size;
811 ret = true;
812 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
813 *size += opt_size;
814 remaining -= opt_size;
815 ret = true;
816 }
817
818 if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
819 *size += opt_size;
820 remaining -= opt_size;
821 ret = true;
822 }
823
824 return ret;
825}
826
827bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
828 struct mptcp_out_options *opts)
829{
830 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
831
832 if (subflow_req->mp_capable) {
833 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
834 opts->sndr_key = subflow_req->local_key;
835 opts->csum_reqd = subflow_req->csum_reqd;
836 opts->allow_join_id0 = subflow_req->allow_join_id0;
837 *size = TCPOLEN_MPTCP_MPC_SYNACK;
838 pr_debug("subflow_req=%p, local_key=%llu",
839 subflow_req, subflow_req->local_key);
840 return true;
841 } else if (subflow_req->mp_join) {
842 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
843 opts->backup = subflow_req->backup;
844 opts->join_id = subflow_req->local_id;
845 opts->thmac = subflow_req->thmac;
846 opts->nonce = subflow_req->local_nonce;
847 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
848 subflow_req, opts->backup, opts->join_id,
849 opts->thmac, opts->nonce);
850 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
851 return true;
852 }
853 return false;
854}
855
856static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
857 struct mptcp_subflow_context *subflow,
858 struct sk_buff *skb,
859 struct mptcp_options_received *mp_opt)
860{
861 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
862 * will make the subflow fully established
863 */
864 if (likely(subflow->fully_established)) {
865 /* on passive sockets, check for 3rd ack retransmission
866 * note that msk is always set by subflow_syn_recv_sock()
867 * for mp_join subflows
868 */
869 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
870 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
871 subflow->mp_join && mp_opt->mp_join &&
872 READ_ONCE(msk->pm.server_side))
873 tcp_send_ack(ssk);
874 goto fully_established;
875 }
876
877 /* we must process OoO packets before the first subflow is fully
878 * established. OoO packets are instead a protocol violation
879 * for MP_JOIN subflows as the peer must not send any data
880 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
881 */
882 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
883 if (subflow->mp_join)
884 goto reset;
885 return subflow->mp_capable;
886 }
887
888 if ((mp_opt->dss && mp_opt->use_ack) ||
889 (mp_opt->add_addr && !mp_opt->echo)) {
890 /* subflows are fully established as soon as we get any
891 * additional ack, including ADD_ADDR.
892 */
893 subflow->fully_established = 1;
894 WRITE_ONCE(msk->fully_established, true);
895 goto fully_established;
896 }
897
898 /* If the first established packet does not contain MP_CAPABLE + data
899 * then fallback to TCP. Fallback scenarios requires a reset for
900 * MP_JOIN subflows.
901 */
902 if (!mp_opt->mp_capable) {
903 if (subflow->mp_join)
904 goto reset;
905 subflow->mp_capable = 0;
906 pr_fallback(msk);
907 __mptcp_do_fallback(msk);
908 return false;
909 }
910
911 if (mp_opt->deny_join_id0)
912 WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
913
914 if (unlikely(!READ_ONCE(msk->pm.server_side)))
915 pr_warn_once("bogus mpc option on established client sk");
916 mptcp_subflow_fully_established(subflow, mp_opt);
917
918fully_established:
919 /* if the subflow is not already linked into the conn_list, we can't
920 * notify the PM: this subflow is still on the listener queue
921 * and the PM possibly acquiring the subflow lock could race with
922 * the listener close
923 */
924 if (likely(subflow->pm_notified) || list_empty(&subflow->node))
925 return true;
926
927 subflow->pm_notified = 1;
928 if (subflow->mp_join) {
929 clear_3rdack_retransmission(ssk);
930 mptcp_pm_subflow_established(msk);
931 } else {
932 mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
933 }
934 return true;
935
936reset:
937 mptcp_subflow_reset(ssk);
938 return false;
939}
940
941u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
942{
943 u32 old_seq32, cur_seq32;
944
945 old_seq32 = (u32)old_seq;
946 cur_seq32 = (u32)cur_seq;
947 cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
948 if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
949 return cur_seq + (1LL << 32);
950
951 /* reverse wrap could happen, too */
952 if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
953 return cur_seq - (1LL << 32);
954 return cur_seq;
955}
956
957static void ack_update_msk(struct mptcp_sock *msk,
958 struct sock *ssk,
959 struct mptcp_options_received *mp_opt)
960{
961 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
962 struct sock *sk = (struct sock *)msk;
963 u64 old_snd_una;
964
965 mptcp_data_lock(sk);
966
967 /* avoid ack expansion on update conflict, to reduce the risk of
968 * wrongly expanding to a future ack sequence number, which is way
969 * more dangerous than missing an ack
970 */
971 old_snd_una = msk->snd_una;
972 new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
973
974 /* ACK for data not even sent yet? Ignore. */
975 if (after64(new_snd_una, snd_nxt))
976 new_snd_una = old_snd_una;
977
978 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
979
980 if (after64(new_wnd_end, msk->wnd_end))
981 msk->wnd_end = new_wnd_end;
982
983 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
984 if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
985 __mptcp_check_push(sk, ssk);
986
987 if (after64(new_snd_una, old_snd_una)) {
988 msk->snd_una = new_snd_una;
989 __mptcp_data_acked(sk);
990 }
991 mptcp_data_unlock(sk);
992
993 trace_ack_update_msk(mp_opt->data_ack,
994 old_snd_una, new_snd_una,
995 new_wnd_end, msk->wnd_end);
996}
997
998bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
999{
1000 /* Skip if DATA_FIN was already received.
1001 * If updating simultaneously with the recvmsg loop, values
1002 * should match. If they mismatch, the peer is misbehaving and
1003 * we will prefer the most recent information.
1004 */
1005 if (READ_ONCE(msk->rcv_data_fin))
1006 return false;
1007
1008 WRITE_ONCE(msk->rcv_data_fin_seq,
1009 mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1010 WRITE_ONCE(msk->rcv_data_fin, 1);
1011
1012 return true;
1013}
1014
1015static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1016 struct mptcp_options_received *mp_opt)
1017{
1018 u64 hmac = 0;
1019
1020 if (mp_opt->echo)
1021 return true;
1022
1023 hmac = add_addr_generate_hmac(msk->remote_key,
1024 msk->local_key,
1025 &mp_opt->addr);
1026
1027 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1028 msk, (unsigned long long)hmac,
1029 (unsigned long long)mp_opt->ahmac);
1030
1031 return hmac == mp_opt->ahmac;
1032}
1033
1034/* Return false if a subflow has been reset, else return true */
1035bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1036{
1037 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1038 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1039 struct mptcp_options_received mp_opt;
1040 struct mptcp_ext *mpext;
1041
1042 if (__mptcp_check_fallback(msk)) {
1043 /* Keep it simple and unconditionally trigger send data cleanup and
1044 * pending queue spooling. We will need to acquire the data lock
1045 * for more accurate checks, and once the lock is acquired, such
1046 * helpers are cheap.
1047 */
1048 mptcp_data_lock(subflow->conn);
1049 if (sk_stream_memory_free(sk))
1050 __mptcp_check_push(subflow->conn, sk);
1051 __mptcp_data_acked(subflow->conn);
1052 mptcp_data_unlock(subflow->conn);
1053 return true;
1054 }
1055
1056 mptcp_get_options(sk, skb, &mp_opt);
1057
1058 /* The subflow can be in close state only if check_fully_established()
1059 * just sent a reset. If so, tell the caller to ignore the current packet.
1060 */
1061 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1062 return sk->sk_state != TCP_CLOSE;
1063
1064 if (mp_opt.fastclose &&
1065 msk->local_key == mp_opt.rcvr_key) {
1066 WRITE_ONCE(msk->rcv_fastclose, true);
1067 mptcp_schedule_work((struct sock *)msk);
1068 }
1069
1070 if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
1071 if (!mp_opt.echo) {
1072 mptcp_pm_add_addr_received(msk, &mp_opt.addr);
1073 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1074 } else {
1075 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1076 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1077 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1078 }
1079
1080 if (mp_opt.addr.port)
1081 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1082
1083 mp_opt.add_addr = 0;
1084 }
1085
1086 if (mp_opt.rm_addr) {
1087 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1088 mp_opt.rm_addr = 0;
1089 }
1090
1091 if (mp_opt.mp_prio) {
1092 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1093 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1094 mp_opt.mp_prio = 0;
1095 }
1096
1097 if (mp_opt.reset) {
1098 subflow->reset_seen = 1;
1099 subflow->reset_reason = mp_opt.reset_reason;
1100 subflow->reset_transient = mp_opt.reset_transient;
1101 }
1102
1103 if (!mp_opt.dss)
1104 return true;
1105
1106 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1107 * monodirectional flows will stuck
1108 */
1109 if (mp_opt.use_ack)
1110 ack_update_msk(msk, sk, &mp_opt);
1111
1112 /* Zero-data-length packets are dropped by the caller and not
1113 * propagated to the MPTCP layer, so the skb extension does not
1114 * need to be allocated or populated. DATA_FIN information, if
1115 * present, needs to be updated here before the skb is freed.
1116 */
1117 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1118 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1119 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
1120 schedule_work(&msk->work))
1121 sock_hold(subflow->conn);
1122
1123 return true;
1124 }
1125
1126 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1127 if (!mpext)
1128 return true;
1129
1130 memset(mpext, 0, sizeof(*mpext));
1131
1132 if (mp_opt.use_map) {
1133 if (mp_opt.mpc_map) {
1134 /* this is an MP_CAPABLE carrying MPTCP data
1135 * we know this map the first chunk of data
1136 */
1137 mptcp_crypto_key_sha(subflow->remote_key, NULL,
1138 &mpext->data_seq);
1139 mpext->data_seq++;
1140 mpext->subflow_seq = 1;
1141 mpext->dsn64 = 1;
1142 mpext->mpc_map = 1;
1143 mpext->data_fin = 0;
1144 } else {
1145 mpext->data_seq = mp_opt.data_seq;
1146 mpext->subflow_seq = mp_opt.subflow_seq;
1147 mpext->dsn64 = mp_opt.dsn64;
1148 mpext->data_fin = mp_opt.data_fin;
1149 }
1150 mpext->data_len = mp_opt.data_len;
1151 mpext->use_map = 1;
1152 mpext->csum_reqd = mp_opt.csum_reqd;
1153
1154 if (mpext->csum_reqd)
1155 mpext->csum = mp_opt.csum;
1156 }
1157
1158 return true;
1159}
1160
1161static void mptcp_set_rwin(const struct tcp_sock *tp)
1162{
1163 const struct sock *ssk = (const struct sock *)tp;
1164 const struct mptcp_subflow_context *subflow;
1165 struct mptcp_sock *msk;
1166 u64 ack_seq;
1167
1168 subflow = mptcp_subflow_ctx(ssk);
1169 msk = mptcp_sk(subflow->conn);
1170
1171 ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1172
1173 if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1174 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1175}
1176
1177static u16 mptcp_make_csum(const struct mptcp_ext *mpext)
1178{
1179 struct csum_pseudo_header header;
1180 __wsum csum;
1181
1182 /* cfr RFC 8684 3.3.1.:
1183 * the data sequence number used in the pseudo-header is
1184 * always the 64-bit value, irrespective of what length is used in the
1185 * DSS option itself.
1186 */
1187 header.data_seq = cpu_to_be64(mpext->data_seq);
1188 header.subflow_seq = htonl(mpext->subflow_seq);
1189 header.data_len = htons(mpext->data_len);
1190 header.csum = 0;
1191
1192 csum = csum_partial(&header, sizeof(header), ~csum_unfold(mpext->csum));
1193 return (__force u16)csum_fold(csum);
1194}
1195
1196void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1197 struct mptcp_out_options *opts)
1198{
1199 if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
1200 OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
1201 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1202
1203 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1204 len = TCPOLEN_MPTCP_MPC_SYN;
1205 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1206 len = TCPOLEN_MPTCP_MPC_SYNACK;
1207 } else if (opts->ext_copy.data_len) {
1208 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1209 if (opts->csum_reqd)
1210 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1211 } else {
1212 len = TCPOLEN_MPTCP_MPC_ACK;
1213 }
1214
1215 if (opts->csum_reqd)
1216 flag |= MPTCP_CAP_CHECKSUM_REQD;
1217
1218 if (!opts->allow_join_id0)
1219 flag |= MPTCP_CAP_DENY_JOIN_ID0;
1220
1221 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1222 MPTCP_SUPPORTED_VERSION,
1223 flag);
1224
1225 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1226 opts->suboptions))
1227 goto mp_capable_done;
1228
1229 put_unaligned_be64(opts->sndr_key, ptr);
1230 ptr += 2;
1231 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1232 goto mp_capable_done;
1233
1234 put_unaligned_be64(opts->rcvr_key, ptr);
1235 ptr += 2;
1236 if (!opts->ext_copy.data_len)
1237 goto mp_capable_done;
1238
1239 if (opts->csum_reqd) {
1240 put_unaligned_be32(opts->ext_copy.data_len << 16 |
1241 mptcp_make_csum(&opts->ext_copy), ptr);
1242 } else {
1243 put_unaligned_be32(opts->ext_copy.data_len << 16 |
1244 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1245 }
1246 ptr += 1;
1247 }
1248
1249mp_capable_done:
1250 if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1251 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1252 u8 echo = MPTCP_ADDR_ECHO;
1253
1254#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1255 if (opts->addr.family == AF_INET6)
1256 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1257#endif
1258
1259 if (opts->addr.port)
1260 len += TCPOLEN_MPTCP_PORT_LEN;
1261
1262 if (opts->ahmac) {
1263 len += sizeof(opts->ahmac);
1264 echo = 0;
1265 }
1266
1267 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1268 len, echo, opts->addr.id);
1269 if (opts->addr.family == AF_INET) {
1270 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1271 ptr += 1;
1272 }
1273#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1274 else if (opts->addr.family == AF_INET6) {
1275 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1276 ptr += 4;
1277 }
1278#endif
1279
1280 if (!opts->addr.port) {
1281 if (opts->ahmac) {
1282 put_unaligned_be64(opts->ahmac, ptr);
1283 ptr += 2;
1284 }
1285 } else {
1286 u16 port = ntohs(opts->addr.port);
1287
1288 if (opts->ahmac) {
1289 u8 *bptr = (u8 *)ptr;
1290
1291 put_unaligned_be16(port, bptr);
1292 bptr += 2;
1293 put_unaligned_be64(opts->ahmac, bptr);
1294 bptr += 8;
1295 put_unaligned_be16(TCPOPT_NOP << 8 |
1296 TCPOPT_NOP, bptr);
1297
1298 ptr += 3;
1299 } else {
1300 put_unaligned_be32(port << 16 |
1301 TCPOPT_NOP << 8 |
1302 TCPOPT_NOP, ptr);
1303 ptr += 1;
1304 }
1305 }
1306 }
1307
1308 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1309 u8 i = 1;
1310
1311 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1312 TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1313 0, opts->rm_list.ids[0]);
1314
1315 while (i < opts->rm_list.nr) {
1316 u8 id1, id2, id3, id4;
1317
1318 id1 = opts->rm_list.ids[i];
1319 id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1320 id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1321 id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1322 put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1323 ptr += 1;
1324 i += 4;
1325 }
1326 }
1327
1328 if (OPTION_MPTCP_PRIO & opts->suboptions) {
1329 const struct sock *ssk = (const struct sock *)tp;
1330 struct mptcp_subflow_context *subflow;
1331
1332 subflow = mptcp_subflow_ctx(ssk);
1333 subflow->send_mp_prio = 0;
1334
1335 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1336 TCPOLEN_MPTCP_PRIO,
1337 opts->backup, TCPOPT_NOP);
1338 }
1339
1340 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1341 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1342 TCPOLEN_MPTCP_MPJ_SYN,
1343 opts->backup, opts->join_id);
1344 put_unaligned_be32(opts->token, ptr);
1345 ptr += 1;
1346 put_unaligned_be32(opts->nonce, ptr);
1347 ptr += 1;
1348 }
1349
1350 if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1351 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1352 TCPOLEN_MPTCP_MPJ_SYNACK,
1353 opts->backup, opts->join_id);
1354 put_unaligned_be64(opts->thmac, ptr);
1355 ptr += 2;
1356 put_unaligned_be32(opts->nonce, ptr);
1357 ptr += 1;
1358 }
1359
1360 if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1361 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1362 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1363 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1364 ptr += 5;
1365 }
1366
1367 if (OPTION_MPTCP_RST & opts->suboptions)
1368 *ptr++ = mptcp_option(MPTCPOPT_RST,
1369 TCPOLEN_MPTCP_RST,
1370 opts->reset_transient,
1371 opts->reset_reason);
1372
1373 if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
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) {
1420 put_unaligned_be32(mpext->data_len << 16 |
1421 mptcp_make_csum(mpext), ptr);
1422 } else {
1423 put_unaligned_be32(mpext->data_len << 16 |
1424 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1425 }
1426 }
1427 }
1428
1429 if (tp)
1430 mptcp_set_rwin(tp);
1431}
1432
1433__be32 mptcp_get_reset_option(const struct sk_buff *skb)
1434{
1435 const struct mptcp_ext *ext = mptcp_get_ext(skb);
1436 u8 flags, reason;
1437
1438 if (ext) {
1439 flags = ext->reset_transient;
1440 reason = ext->reset_reason;
1441
1442 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1443 flags, reason);
1444 }
1445
1446 return htonl(0u);
1447}
1448EXPORT_SYMBOL_GPL(mptcp_get_reset_option);