1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2017 - 2019, Intel Corporation.
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/kernel.h>
10 #include <crypto/sha2.h>
12 #include <net/mptcp.h>
16 #include <trace/events/mptcp.h>
18 static bool mptcp_cap_flag_sha256(u8 flags)
20 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
23 static void mptcp_parse_option(const struct sk_buff *skb,
24 const unsigned char *ptr, int opsize,
25 struct mptcp_options_received *mp_opt)
27 u8 subtype = *ptr >> 4;
35 case MPTCPOPT_MP_CAPABLE:
36 /* strict size checking */
37 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
38 if (skb->len > tcp_hdr(skb)->doff << 2)
39 expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
41 expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
42 subopt = OPTION_MPTCP_MPC_ACK;
44 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) {
45 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
46 subopt = OPTION_MPTCP_MPC_SYNACK;
48 expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
49 subopt = OPTION_MPTCP_MPC_SYN;
53 /* Cfr RFC 8684 Section 3.3.0:
54 * If a checksum is present but its use had
55 * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
56 * close the subflow with a RST, as it is not behaving as negotiated.
57 * If a checksum is not present when its use has been negotiated, the
58 * receiver MUST close the subflow with a RST, as it is considered
60 * We parse even option with mismatching csum presence, so that
61 * later in subflow_data_ready we can trigger the reset.
63 if (opsize != expected_opsize &&
64 (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
65 opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
68 /* try to be gentle vs future versions on the initial syn */
69 version = *ptr++ & MPTCP_VERSION_MASK;
70 if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
71 if (version != MPTCP_SUPPORTED_VERSION)
73 } else if (version < MPTCP_SUPPORTED_VERSION) {
78 if (!mptcp_cap_flag_sha256(flags) ||
79 (flags & MPTCP_CAP_EXTENSIBILITY))
82 /* RFC 6824, Section 3.1:
83 * "For the Checksum Required bit (labeled "A"), if either
84 * host requires the use of checksums, checksums MUST be used.
85 * In other words, the only way for checksums not to be used
86 * is if both hosts in their SYNs set A=0."
88 if (flags & MPTCP_CAP_CHECKSUM_REQD)
89 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
91 mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
93 mp_opt->suboptions |= subopt;
94 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
95 mp_opt->sndr_key = get_unaligned_be64(ptr);
98 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
99 mp_opt->rcvr_key = get_unaligned_be64(ptr);
102 if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
104 * "the data parameters in a MP_CAPABLE are semantically
105 * equivalent to those in a DSS option and can be used
108 mp_opt->suboptions |= OPTION_MPTCP_DSS;
111 mp_opt->data_len = get_unaligned_be16(ptr);
114 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
115 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
116 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
119 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
120 version, flags, opsize, mp_opt->sndr_key,
121 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
124 case MPTCPOPT_MP_JOIN:
125 mp_opt->suboptions |= OPTIONS_MPTCP_MPJ;
126 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
127 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
128 mp_opt->join_id = *ptr++;
129 mp_opt->token = get_unaligned_be32(ptr);
131 mp_opt->nonce = get_unaligned_be32(ptr);
133 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
134 mp_opt->backup, mp_opt->join_id,
135 mp_opt->token, mp_opt->nonce);
136 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
137 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
138 mp_opt->join_id = *ptr++;
139 mp_opt->thmac = get_unaligned_be64(ptr);
141 mp_opt->nonce = get_unaligned_be32(ptr);
143 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
144 mp_opt->backup, mp_opt->join_id,
145 mp_opt->thmac, mp_opt->nonce);
146 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
148 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
149 pr_debug("MP_JOIN hmac");
151 mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ;
159 /* we must clear 'mpc_map' be able to detect MP_CAPABLE
160 * map vs DSS map in mptcp_incoming_options(), and reconstruct
161 * map info accordingly
164 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
165 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
166 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
167 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
168 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
169 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
171 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
172 mp_opt->data_fin, mp_opt->dsn64,
173 mp_opt->use_map, mp_opt->ack64,
176 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
178 if (mp_opt->use_ack) {
180 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
182 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
185 if (mp_opt->use_map) {
187 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
189 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
192 /* Always parse any csum presence combination, we will enforce
193 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
195 if (opsize != expected_opsize &&
196 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
199 mp_opt->suboptions |= OPTION_MPTCP_DSS;
200 if (mp_opt->use_ack) {
202 mp_opt->data_ack = get_unaligned_be64(ptr);
205 mp_opt->data_ack = get_unaligned_be32(ptr);
209 pr_debug("data_ack=%llu", mp_opt->data_ack);
212 if (mp_opt->use_map) {
214 mp_opt->data_seq = get_unaligned_be64(ptr);
217 mp_opt->data_seq = get_unaligned_be32(ptr);
221 mp_opt->subflow_seq = get_unaligned_be32(ptr);
224 mp_opt->data_len = get_unaligned_be16(ptr);
227 if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
228 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
229 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
233 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
234 mp_opt->data_seq, mp_opt->subflow_seq,
235 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
241 case MPTCPOPT_ADD_ADDR:
242 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
244 if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
245 opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
246 mp_opt->addr.family = AF_INET;
247 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
248 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
249 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
250 mp_opt->addr.family = AF_INET6;
255 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
256 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
257 mp_opt->addr.family = AF_INET;
258 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
259 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
260 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
261 mp_opt->addr.family = AF_INET6;
267 mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
268 mp_opt->addr.id = *ptr++;
269 mp_opt->addr.port = 0;
271 if (mp_opt->addr.family == AF_INET) {
272 memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
274 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
275 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
276 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
280 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
282 memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
284 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
285 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
286 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
292 mp_opt->ahmac = get_unaligned_be64(ptr);
295 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
296 (mp_opt->addr.family == AF_INET6) ? "6" : "",
297 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
300 case MPTCPOPT_RM_ADDR:
301 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
302 opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
307 mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
308 mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
309 for (i = 0; i < mp_opt->rm_list.nr; i++)
310 mp_opt->rm_list.ids[i] = *ptr++;
311 pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
314 case MPTCPOPT_MP_PRIO:
315 if (opsize != TCPOLEN_MPTCP_PRIO)
318 mp_opt->suboptions |= OPTION_MPTCP_PRIO;
319 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
320 pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
323 case MPTCPOPT_MP_FASTCLOSE:
324 if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
328 mp_opt->rcvr_key = get_unaligned_be64(ptr);
330 mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
331 pr_debug("MP_FASTCLOSE: recv_key=%llu", mp_opt->rcvr_key);
335 if (opsize != TCPOLEN_MPTCP_RST)
338 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
341 mp_opt->suboptions |= OPTION_MPTCP_RST;
343 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
344 mp_opt->reset_reason = *ptr;
345 pr_debug("MP_RST: transient=%u reason=%u",
346 mp_opt->reset_transient, mp_opt->reset_reason);
349 case MPTCPOPT_MP_FAIL:
350 if (opsize != TCPOLEN_MPTCP_FAIL)
354 mp_opt->suboptions |= OPTION_MPTCP_FAIL;
355 mp_opt->fail_seq = get_unaligned_be64(ptr);
356 pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
364 void mptcp_get_options(const struct sk_buff *skb,
365 struct mptcp_options_received *mp_opt)
367 const struct tcphdr *th = tcp_hdr(skb);
368 const unsigned char *ptr;
371 /* initialize option status */
372 mp_opt->suboptions = 0;
374 length = (th->doff * 4) - sizeof(struct tcphdr);
375 ptr = (const unsigned char *)(th + 1);
384 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
391 if (opsize < 2) /* "silly options" */
394 return; /* don't parse partial options */
395 if (opcode == TCPOPT_MPTCP)
396 mptcp_parse_option(skb, ptr, opsize, mp_opt);
403 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
404 unsigned int *size, struct mptcp_out_options *opts)
406 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
408 /* we will use snd_isn to detect first pkt [re]transmission
409 * in mptcp_established_options_mp()
411 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
412 if (subflow->request_mptcp) {
413 opts->suboptions = OPTION_MPTCP_MPC_SYN;
414 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
415 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
416 *size = TCPOLEN_MPTCP_MPC_SYN;
418 } else if (subflow->request_join) {
419 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
420 subflow->local_nonce);
421 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
422 opts->join_id = subflow->local_id;
423 opts->token = subflow->remote_token;
424 opts->nonce = subflow->local_nonce;
425 opts->backup = subflow->request_bkup;
426 *size = TCPOLEN_MPTCP_MPJ_SYN;
432 static void clear_3rdack_retransmission(struct sock *sk)
434 struct inet_connection_sock *icsk = inet_csk(sk);
436 sk_stop_timer(sk, &icsk->icsk_delack_timer);
437 icsk->icsk_ack.timeout = 0;
438 icsk->icsk_ack.ato = 0;
439 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
442 static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
443 bool snd_data_fin_enable,
445 struct mptcp_out_options *opts)
447 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
448 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
449 struct mptcp_ext *mpext;
450 unsigned int data_len;
453 /* When skb is not available, we better over-estimate the emitted
454 * options len. A full DSS option (28 bytes) is longer than
455 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
456 * tell the caller to defer the estimate to
457 * mptcp_established_options_dss(), which will reserve enough space.
462 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
463 if (subflow->fully_established || snd_data_fin_enable ||
464 subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
465 sk->sk_state != TCP_ESTABLISHED)
468 if (subflow->mp_capable) {
469 mpext = mptcp_get_ext(skb);
470 data_len = mpext ? mpext->data_len : 0;
472 /* we will check ops->data_len in mptcp_write_options() to
473 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
474 * TCPOLEN_MPTCP_MPC_ACK
476 opts->data_len = data_len;
477 opts->suboptions = OPTION_MPTCP_MPC_ACK;
478 opts->sndr_key = subflow->local_key;
479 opts->rcvr_key = subflow->remote_key;
480 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
481 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
484 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
485 * packets that start the first subflow of an MPTCP connection,
486 * as well as the first packet that carries data
489 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
490 if (opts->csum_reqd) {
491 /* we need to propagate more info to csum the pseudo hdr */
492 opts->data_seq = mpext->data_seq;
493 opts->subflow_seq = mpext->subflow_seq;
494 opts->csum = mpext->csum;
495 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
497 *size = ALIGN(len, 4);
499 *size = TCPOLEN_MPTCP_MPC_ACK;
502 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
503 subflow, subflow->local_key, subflow->remote_key,
507 } else if (subflow->mp_join) {
508 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
509 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
510 *size = TCPOLEN_MPTCP_MPJ_ACK;
511 pr_debug("subflow=%p", subflow);
513 /* we can use the full delegate action helper only from BH context
514 * If we are in process context - sk is flushing the backlog at
515 * socket lock release time - just set the appropriate flag, will
516 * be handled by the release callback
518 if (sock_owned_by_user(sk))
519 set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
521 mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
527 static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
528 struct sk_buff *skb, struct mptcp_ext *ext)
530 /* The write_seq value has already been incremented, so the actual
531 * sequence number for the DATA_FIN is one less.
533 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
535 if (!ext->use_map || !skb->len) {
536 /* RFC6824 requires a DSS mapping with specific values
537 * if DATA_FIN is set but no data payload is mapped
542 ext->data_seq = data_fin_tx_seq;
543 ext->subflow_seq = 0;
545 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
546 /* If there's an existing DSS mapping and it is the
547 * final mapping, DATA_FIN consumes 1 additional byte of
555 static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
556 bool snd_data_fin_enable,
558 struct mptcp_out_options *opts)
560 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
561 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
562 unsigned int dss_size = 0;
563 struct mptcp_ext *mpext;
564 unsigned int ack_size;
568 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
569 mpext = skb ? mptcp_get_ext(skb) : NULL;
571 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
572 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
576 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
578 opts->ext_copy = *mpext;
582 if (skb && snd_data_fin_enable)
583 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
584 opts->suboptions = OPTION_MPTCP_DSS;
588 /* passive sockets msk will set the 'can_ack' after accept(), even
589 * if the first subflow may have the already the remote key handy
591 opts->ext_copy.use_ack = 0;
592 if (!READ_ONCE(msk->can_ack)) {
593 *size = ALIGN(dss_size, 4);
597 ack_seq = READ_ONCE(msk->ack_seq);
598 if (READ_ONCE(msk->use_64bit_ack)) {
599 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
600 opts->ext_copy.data_ack = ack_seq;
601 opts->ext_copy.ack64 = 1;
603 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
604 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
605 opts->ext_copy.ack64 = 0;
607 opts->ext_copy.use_ack = 1;
608 opts->suboptions = OPTION_MPTCP_DSS;
609 WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
611 /* Add kind/length/subtype/flag overhead if mapping is not populated */
613 ack_size += TCPOLEN_MPTCP_DSS_BASE;
615 dss_size += ack_size;
617 *size = ALIGN(dss_size, 4);
621 static u64 add_addr_generate_hmac(u64 key1, u64 key2,
622 struct mptcp_addr_info *addr)
624 u16 port = ntohs(addr->port);
625 u8 hmac[SHA256_DIGEST_SIZE];
630 if (addr->family == AF_INET) {
631 memcpy(&msg[i], &addr->addr.s_addr, 4);
634 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
635 else if (addr->family == AF_INET6) {
636 memcpy(&msg[i], &addr->addr6.s6_addr, 16);
640 msg[i++] = port >> 8;
641 msg[i++] = port & 0xFF;
643 mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
645 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
648 static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
650 unsigned int remaining,
651 struct mptcp_out_options *opts)
653 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
654 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
655 bool drop_other_suboptions = false;
656 unsigned int opt_size = *size;
660 /* add addr will strip the existing options, be sure to avoid breaking
663 if (!mptcp_pm_should_add_signal(msk) ||
664 (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
665 !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
666 &echo, &drop_other_suboptions))
669 if (drop_other_suboptions)
670 remaining += opt_size;
671 len = mptcp_add_addr_len(opts->addr.family, echo, !!opts->addr.port);
676 if (drop_other_suboptions) {
677 pr_debug("drop other suboptions");
678 opts->suboptions = 0;
680 /* note that e.g. DSS could have written into the memory
681 * aliased by ahmac, we must reset the field here
682 * to avoid appending the hmac even for ADD_ADDR echo
688 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
690 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
691 opts->ahmac = add_addr_generate_hmac(msk->local_key,
695 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
697 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
698 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
703 static bool mptcp_established_options_rm_addr(struct sock *sk,
705 unsigned int remaining,
706 struct mptcp_out_options *opts)
708 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
709 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
710 struct mptcp_rm_list rm_list;
713 if (!mptcp_pm_should_rm_signal(msk) ||
714 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
717 len = mptcp_rm_addr_len(&rm_list);
724 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
725 opts->rm_list = rm_list;
727 for (i = 0; i < opts->rm_list.nr; i++)
728 pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
729 MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
733 static bool mptcp_established_options_mp_prio(struct sock *sk,
735 unsigned int remaining,
736 struct mptcp_out_options *opts)
738 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
740 /* can't send MP_PRIO with MPC, as they share the same option space:
741 * 'backup'. Also it makes no sense at all
743 if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
746 /* account for the trailing 'nop' option */
747 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
750 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
751 opts->suboptions |= OPTION_MPTCP_PRIO;
752 opts->backup = subflow->request_bkup;
754 pr_debug("prio=%d", opts->backup);
759 static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
761 unsigned int remaining,
762 struct mptcp_out_options *opts)
764 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
766 if (remaining < TCPOLEN_MPTCP_RST)
769 *size = TCPOLEN_MPTCP_RST;
770 opts->suboptions |= OPTION_MPTCP_RST;
771 opts->reset_transient = subflow->reset_transient;
772 opts->reset_reason = subflow->reset_reason;
773 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
778 static bool mptcp_established_options_fastclose(struct sock *sk,
780 unsigned int remaining,
781 struct mptcp_out_options *opts)
783 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
784 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
786 if (likely(!subflow->send_fastclose))
789 if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
792 *size = TCPOLEN_MPTCP_FASTCLOSE;
793 opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
794 opts->rcvr_key = msk->remote_key;
796 pr_debug("FASTCLOSE key=%llu", opts->rcvr_key);
797 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
801 static bool mptcp_established_options_mp_fail(struct sock *sk,
803 unsigned int remaining,
804 struct mptcp_out_options *opts)
806 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
808 if (likely(!subflow->send_mp_fail))
811 if (remaining < TCPOLEN_MPTCP_FAIL)
814 *size = TCPOLEN_MPTCP_FAIL;
815 opts->suboptions |= OPTION_MPTCP_FAIL;
816 opts->fail_seq = subflow->map_seq;
818 pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
819 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
824 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
825 unsigned int *size, unsigned int remaining,
826 struct mptcp_out_options *opts)
828 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
829 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
830 unsigned int opt_size = 0;
834 opts->suboptions = 0;
836 if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
839 if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
840 if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
841 mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
843 remaining -= opt_size;
845 /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
846 if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
848 remaining -= opt_size;
853 snd_data_fin = mptcp_data_fin_enabled(msk);
854 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
856 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
857 unsigned int mp_fail_size;
860 if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
861 remaining - opt_size, opts)) {
862 *size += opt_size + mp_fail_size;
863 remaining -= opt_size - mp_fail_size;
868 /* we reserved enough space for the above options, and exceeding the
869 * TCP option space would be fatal
871 if (WARN_ON_ONCE(opt_size > remaining))
875 remaining -= opt_size;
876 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
878 remaining -= opt_size;
880 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
882 remaining -= opt_size;
886 if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
888 remaining -= opt_size;
895 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
896 struct mptcp_out_options *opts)
898 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
900 if (subflow_req->mp_capable) {
901 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
902 opts->sndr_key = subflow_req->local_key;
903 opts->csum_reqd = subflow_req->csum_reqd;
904 opts->allow_join_id0 = subflow_req->allow_join_id0;
905 *size = TCPOLEN_MPTCP_MPC_SYNACK;
906 pr_debug("subflow_req=%p, local_key=%llu",
907 subflow_req, subflow_req->local_key);
909 } else if (subflow_req->mp_join) {
910 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
911 opts->backup = subflow_req->backup;
912 opts->join_id = subflow_req->local_id;
913 opts->thmac = subflow_req->thmac;
914 opts->nonce = subflow_req->local_nonce;
915 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
916 subflow_req, opts->backup, opts->join_id,
917 opts->thmac, opts->nonce);
918 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
924 static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
925 struct mptcp_subflow_context *subflow,
927 struct mptcp_options_received *mp_opt)
929 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
930 * will make the subflow fully established
932 if (likely(subflow->fully_established)) {
933 /* on passive sockets, check for 3rd ack retransmission
934 * note that msk is always set by subflow_syn_recv_sock()
935 * for mp_join subflows
937 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
938 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
939 subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
940 !subflow->request_join)
945 /* we must process OoO packets before the first subflow is fully
946 * established. OoO packets are instead a protocol violation
947 * for MP_JOIN subflows as the peer must not send any data
948 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
950 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
951 if (subflow->mp_join)
953 if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
954 goto set_fully_established;
955 return subflow->mp_capable;
958 if (subflow->remote_key_valid &&
959 (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
960 ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo))) {
961 /* subflows are fully established as soon as we get any
962 * additional ack, including ADD_ADDR.
964 subflow->fully_established = 1;
965 WRITE_ONCE(msk->fully_established, true);
969 /* If the first established packet does not contain MP_CAPABLE + data
970 * then fallback to TCP. Fallback scenarios requires a reset for
973 if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
974 if (subflow->mp_join)
976 subflow->mp_capable = 0;
978 mptcp_do_fallback(ssk);
982 if (mp_opt->deny_join_id0)
983 WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
985 set_fully_established:
986 if (unlikely(!READ_ONCE(msk->pm.server_side)))
987 pr_warn_once("bogus mpc option on established client sk");
988 mptcp_subflow_fully_established(subflow, mp_opt);
991 /* if the subflow is not already linked into the conn_list, we can't
992 * notify the PM: this subflow is still on the listener queue
993 * and the PM possibly acquiring the subflow lock could race with
996 if (likely(subflow->pm_notified) || list_empty(&subflow->node))
999 subflow->pm_notified = 1;
1000 if (subflow->mp_join) {
1001 clear_3rdack_retransmission(ssk);
1002 mptcp_pm_subflow_established(msk);
1004 mptcp_pm_fully_established(msk, ssk);
1009 mptcp_subflow_reset(ssk);
1013 u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1015 u32 old_seq32, cur_seq32;
1017 old_seq32 = (u32)old_seq;
1018 cur_seq32 = (u32)cur_seq;
1019 cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
1020 if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
1021 return cur_seq + (1LL << 32);
1023 /* reverse wrap could happen, too */
1024 if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
1025 return cur_seq - (1LL << 32);
1029 static void ack_update_msk(struct mptcp_sock *msk,
1031 struct mptcp_options_received *mp_opt)
1033 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1034 struct sock *sk = (struct sock *)msk;
1037 mptcp_data_lock(sk);
1039 /* avoid ack expansion on update conflict, to reduce the risk of
1040 * wrongly expanding to a future ack sequence number, which is way
1041 * more dangerous than missing an ack
1043 old_snd_una = msk->snd_una;
1044 new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1046 /* ACK for data not even sent yet? Ignore.*/
1047 if (unlikely(after64(new_snd_una, snd_nxt)))
1048 new_snd_una = old_snd_una;
1050 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1052 if (after64(new_wnd_end, msk->wnd_end))
1053 msk->wnd_end = new_wnd_end;
1055 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1056 if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1057 __mptcp_check_push(sk, ssk);
1059 if (after64(new_snd_una, old_snd_una)) {
1060 msk->snd_una = new_snd_una;
1061 __mptcp_data_acked(sk);
1063 mptcp_data_unlock(sk);
1065 trace_ack_update_msk(mp_opt->data_ack,
1066 old_snd_una, new_snd_una,
1067 new_wnd_end, msk->wnd_end);
1070 bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1072 /* Skip if DATA_FIN was already received.
1073 * If updating simultaneously with the recvmsg loop, values
1074 * should match. If they mismatch, the peer is misbehaving and
1075 * we will prefer the most recent information.
1077 if (READ_ONCE(msk->rcv_data_fin))
1080 WRITE_ONCE(msk->rcv_data_fin_seq,
1081 mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1082 WRITE_ONCE(msk->rcv_data_fin, 1);
1087 static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1088 struct mptcp_options_received *mp_opt)
1095 hmac = add_addr_generate_hmac(msk->remote_key,
1099 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1100 msk, hmac, mp_opt->ahmac);
1102 return hmac == mp_opt->ahmac;
1105 /* Return false if a subflow has been reset, else return true */
1106 bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1108 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1109 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1110 struct mptcp_options_received mp_opt;
1111 struct mptcp_ext *mpext;
1113 if (__mptcp_check_fallback(msk)) {
1114 /* Keep it simple and unconditionally trigger send data cleanup and
1115 * pending queue spooling. We will need to acquire the data lock
1116 * for more accurate checks, and once the lock is acquired, such
1117 * helpers are cheap.
1119 mptcp_data_lock(subflow->conn);
1120 if (sk_stream_memory_free(sk))
1121 __mptcp_check_push(subflow->conn, sk);
1122 __mptcp_data_acked(subflow->conn);
1123 mptcp_data_unlock(subflow->conn);
1127 mptcp_get_options(skb, &mp_opt);
1129 /* The subflow can be in close state only if check_fully_established()
1130 * just sent a reset. If so, tell the caller to ignore the current packet.
1132 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1133 return sk->sk_state != TCP_CLOSE;
1135 if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1136 if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1137 msk->local_key == mp_opt.rcvr_key) {
1138 WRITE_ONCE(msk->rcv_fastclose, true);
1139 mptcp_schedule_work((struct sock *)msk);
1140 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
1143 if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1144 add_addr_hmac_valid(msk, &mp_opt)) {
1146 mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1147 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1149 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1150 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1151 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1154 if (mp_opt.addr.port)
1155 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1158 if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1159 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1161 if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1162 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1163 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1166 if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1167 mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1168 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1171 if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1172 subflow->reset_seen = 1;
1173 subflow->reset_reason = mp_opt.reset_reason;
1174 subflow->reset_transient = mp_opt.reset_transient;
1175 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1178 if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1182 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1183 * monodirectional flows will stuck
1186 ack_update_msk(msk, sk, &mp_opt);
1188 /* Zero-data-length packets are dropped by the caller and not
1189 * propagated to the MPTCP layer, so the skb extension does not
1190 * need to be allocated or populated. DATA_FIN information, if
1191 * present, needs to be updated here before the skb is freed.
1193 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1194 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1195 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
1196 mptcp_schedule_work((struct sock *)msk);
1201 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1205 memset(mpext, 0, sizeof(*mpext));
1207 if (likely(mp_opt.use_map)) {
1208 if (mp_opt.mpc_map) {
1209 /* this is an MP_CAPABLE carrying MPTCP data
1210 * we know this map the first chunk of data
1212 mptcp_crypto_key_sha(subflow->remote_key, NULL,
1215 mpext->subflow_seq = 1;
1218 mpext->data_fin = 0;
1220 mpext->data_seq = mp_opt.data_seq;
1221 mpext->subflow_seq = mp_opt.subflow_seq;
1222 mpext->dsn64 = mp_opt.dsn64;
1223 mpext->data_fin = mp_opt.data_fin;
1225 mpext->data_len = mp_opt.data_len;
1227 mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1229 if (mpext->csum_reqd)
1230 mpext->csum = mp_opt.csum;
1236 static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
1238 const struct sock *ssk = (const struct sock *)tp;
1239 struct mptcp_subflow_context *subflow;
1240 u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
1241 struct mptcp_sock *msk;
1245 subflow = mptcp_subflow_ctx(ssk);
1246 msk = mptcp_sk(subflow->conn);
1248 ack_seq = READ_ONCE(msk->ack_seq);
1249 rcv_wnd_new = ack_seq + tp->rcv_wnd;
1251 rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1252 if (after64(rcv_wnd_new, rcv_wnd_old)) {
1256 rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1258 if (rcv_wnd == rcv_wnd_old)
1260 if (before64(rcv_wnd_new, rcv_wnd)) {
1261 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1264 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1265 rcv_wnd_old = rcv_wnd;
1270 if (rcv_wnd_new != rcv_wnd_old) {
1272 win = rcv_wnd_old - ack_seq;
1273 tp->rcv_wnd = min_t(u64, win, U32_MAX);
1274 new_win = tp->rcv_wnd;
1276 /* Make sure we do not exceed the maximum possible
1279 if (unlikely(th->syn))
1280 new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1281 if (!tp->rx_opt.rcv_wscale &&
1282 READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1283 new_win = min(new_win, MAX_TCP_WINDOW);
1285 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1287 /* RFC1323 scaling applied */
1288 new_win >>= tp->rx_opt.rcv_wscale;
1289 th->window = htons(new_win);
1290 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1294 __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1296 struct csum_pseudo_header header;
1299 /* cfr RFC 8684 3.3.1.:
1300 * the data sequence number used in the pseudo-header is
1301 * always the 64-bit value, irrespective of what length is used in the
1302 * DSS option itself.
1304 header.data_seq = cpu_to_be64(data_seq);
1305 header.subflow_seq = htonl(subflow_seq);
1306 header.data_len = htons(data_len);
1309 csum = csum_partial(&header, sizeof(header), sum);
1310 return csum_fold(csum);
1313 static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1315 return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1316 ~csum_unfold(mpext->csum));
1319 static void put_len_csum(u16 len, __sum16 csum, void *data)
1321 __sum16 *sumptr = data + 2;
1324 put_unaligned_be16(len, ptr);
1326 put_unaligned(csum, sumptr);
1329 void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1330 struct mptcp_out_options *opts)
1332 const struct sock *ssk = (const struct sock *)tp;
1333 struct mptcp_subflow_context *subflow;
1335 /* Which options can be used together?
1337 * X: mutually exclusive
1338 * O: often used together
1339 * C: can be used together in some cases
1340 * P: could be used together but we prefer not to (optimisations)
1342 * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
1343 * ------|------|------|------|------|------|------|------|------|
1344 * MPC |------|------|------|------|------|------|------|------|
1345 * MPJ | X |------|------|------|------|------|------|------|
1346 * DSS | X | X |------|------|------|------|------|------|
1347 * ADD | X | X | P |------|------|------|------|------|
1348 * RM | C | C | C | P |------|------|------|------|
1349 * PRIO | X | C | C | C | C |------|------|------|
1350 * FAIL | X | X | C | X | X | X |------|------|
1351 * FC | X | X | X | X | X | X | X |------|
1352 * RST | X | X | X | X | X | X | O | O |
1353 * ------|------|------|------|------|------|------|------|------|
1355 * The same applies in mptcp_established_options() function.
1357 if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1358 struct mptcp_ext *mpext = &opts->ext_copy;
1359 u8 len = TCPOLEN_MPTCP_DSS_BASE;
1362 if (mpext->use_ack) {
1363 flags = MPTCP_DSS_HAS_ACK;
1365 len += TCPOLEN_MPTCP_DSS_ACK64;
1366 flags |= MPTCP_DSS_ACK64;
1368 len += TCPOLEN_MPTCP_DSS_ACK32;
1372 if (mpext->use_map) {
1373 len += TCPOLEN_MPTCP_DSS_MAP64;
1375 /* Use only 64-bit mapping flags for now, add
1376 * support for optional 32-bit mappings later.
1378 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1379 if (mpext->data_fin)
1380 flags |= MPTCP_DSS_DATA_FIN;
1382 if (opts->csum_reqd)
1383 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1386 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1388 if (mpext->use_ack) {
1390 put_unaligned_be64(mpext->data_ack, ptr);
1393 put_unaligned_be32(mpext->data_ack32, ptr);
1398 if (mpext->use_map) {
1399 put_unaligned_be64(mpext->data_seq, ptr);
1401 put_unaligned_be32(mpext->subflow_seq, ptr);
1403 if (opts->csum_reqd) {
1404 /* data_len == 0 is reserved for the infinite mapping,
1405 * the checksum will also be set to 0.
1407 put_len_csum(mpext->data_len,
1408 (mpext->data_len ? mptcp_make_csum(mpext) : 0),
1411 put_unaligned_be32(mpext->data_len << 16 |
1412 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1417 /* We might need to add MP_FAIL options in rare cases */
1418 if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1420 } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1421 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1423 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1424 len = TCPOLEN_MPTCP_MPC_SYN;
1425 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1426 len = TCPOLEN_MPTCP_MPC_SYNACK;
1427 } else if (opts->data_len) {
1428 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1429 if (opts->csum_reqd)
1430 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1432 len = TCPOLEN_MPTCP_MPC_ACK;
1435 if (opts->csum_reqd)
1436 flag |= MPTCP_CAP_CHECKSUM_REQD;
1438 if (!opts->allow_join_id0)
1439 flag |= MPTCP_CAP_DENY_JOIN_ID0;
1441 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1442 MPTCP_SUPPORTED_VERSION,
1445 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1447 goto mp_capable_done;
1449 put_unaligned_be64(opts->sndr_key, ptr);
1451 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1452 goto mp_capable_done;
1454 put_unaligned_be64(opts->rcvr_key, ptr);
1456 if (!opts->data_len)
1457 goto mp_capable_done;
1459 if (opts->csum_reqd) {
1460 put_len_csum(opts->data_len,
1461 __mptcp_make_csum(opts->data_seq,
1464 ~csum_unfold(opts->csum)),
1467 put_unaligned_be32(opts->data_len << 16 |
1468 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1472 /* MPC is additionally mutually exclusive with MP_PRIO */
1473 goto mp_capable_done;
1474 } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
1475 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1476 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1477 TCPOLEN_MPTCP_MPJ_SYN,
1478 opts->backup, opts->join_id);
1479 put_unaligned_be32(opts->token, ptr);
1481 put_unaligned_be32(opts->nonce, ptr);
1483 } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1484 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1485 TCPOLEN_MPTCP_MPJ_SYNACK,
1486 opts->backup, opts->join_id);
1487 put_unaligned_be64(opts->thmac, ptr);
1489 put_unaligned_be32(opts->nonce, ptr);
1492 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1493 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1494 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1497 } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1498 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1499 u8 echo = MPTCP_ADDR_ECHO;
1501 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1502 if (opts->addr.family == AF_INET6)
1503 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1506 if (opts->addr.port)
1507 len += TCPOLEN_MPTCP_PORT_LEN;
1510 len += sizeof(opts->ahmac);
1514 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1515 len, echo, opts->addr.id);
1516 if (opts->addr.family == AF_INET) {
1517 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1520 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1521 else if (opts->addr.family == AF_INET6) {
1522 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1527 if (!opts->addr.port) {
1529 put_unaligned_be64(opts->ahmac, ptr);
1533 u16 port = ntohs(opts->addr.port);
1536 u8 *bptr = (u8 *)ptr;
1538 put_unaligned_be16(port, bptr);
1540 put_unaligned_be64(opts->ahmac, bptr);
1542 put_unaligned_be16(TCPOPT_NOP << 8 |
1547 put_unaligned_be32(port << 16 |
1553 } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1554 /* FASTCLOSE is mutually exclusive with others except RST */
1555 *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1556 TCPOLEN_MPTCP_FASTCLOSE,
1558 put_unaligned_be64(opts->rcvr_key, ptr);
1561 if (OPTION_MPTCP_RST & opts->suboptions)
1564 } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1566 /* MP_FAIL is mutually exclusive with others except RST */
1567 subflow = mptcp_subflow_ctx(ssk);
1568 subflow->send_mp_fail = 0;
1570 *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1573 put_unaligned_be64(opts->fail_seq, ptr);
1576 if (OPTION_MPTCP_RST & opts->suboptions)
1579 } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1581 *ptr++ = mptcp_option(MPTCPOPT_RST,
1583 opts->reset_transient,
1584 opts->reset_reason);
1588 if (OPTION_MPTCP_PRIO & opts->suboptions) {
1589 subflow = mptcp_subflow_ctx(ssk);
1590 subflow->send_mp_prio = 0;
1592 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1594 opts->backup, TCPOPT_NOP);
1596 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
1600 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1603 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1604 TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1605 0, opts->rm_list.ids[0]);
1607 while (i < opts->rm_list.nr) {
1608 u8 id1, id2, id3, id4;
1610 id1 = opts->rm_list.ids[i];
1611 id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1612 id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1613 id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1614 put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1621 mptcp_set_rwin(tp, th);
1624 __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1626 const struct mptcp_ext *ext = mptcp_get_ext(skb);
1630 flags = ext->reset_transient;
1631 reason = ext->reset_reason;
1633 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1639 EXPORT_SYMBOL_GPL(mptcp_get_reset_option);