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;
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;
40 expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
42 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
43 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
45 expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
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
55 * We parse even option with mismatching csum presence, so that
56 * later in subflow_data_ready we can trigger the reset.
58 if (opsize != expected_opsize &&
59 (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
60 opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
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)
68 } else if (version < MPTCP_SUPPORTED_VERSION) {
73 if (!mptcp_cap_flag_sha256(flags) ||
74 (flags & MPTCP_CAP_EXTENSIBILITY))
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."
83 if (flags & MPTCP_CAP_CHECKSUM_REQD)
84 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
86 mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
88 mp_opt->suboptions |= OPTIONS_MPTCP_MPC;
89 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
90 mp_opt->sndr_key = get_unaligned_be64(ptr);
93 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
94 mp_opt->rcvr_key = get_unaligned_be64(ptr);
97 if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
99 * "the data parameters in a MP_CAPABLE are semantically
100 * equivalent to those in a DSS option and can be used
103 mp_opt->suboptions |= OPTION_MPTCP_DSS;
107 mp_opt->data_len = get_unaligned_be16(ptr);
110 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
111 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
112 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
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);
120 case MPTCPOPT_MP_JOIN:
121 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
122 mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYN;
123 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
124 mp_opt->join_id = *ptr++;
125 mp_opt->token = get_unaligned_be32(ptr);
127 mp_opt->nonce = get_unaligned_be32(ptr);
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->suboptions |= OPTION_MPTCP_MPJ_SYNACK;
134 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
135 mp_opt->join_id = *ptr++;
136 mp_opt->thmac = get_unaligned_be64(ptr);
138 mp_opt->nonce = get_unaligned_be32(ptr);
140 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
141 mp_opt->backup, mp_opt->join_id,
142 mp_opt->thmac, mp_opt->nonce);
143 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
144 mp_opt->suboptions |= OPTION_MPTCP_MPJ_ACK;
146 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
147 pr_debug("MP_JOIN hmac");
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
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);
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,
172 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
174 if (mp_opt->use_ack) {
176 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
178 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
181 if (mp_opt->use_map) {
183 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
185 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
188 /* Always parse any csum presence combination, we will enforce
189 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
191 if (opsize != expected_opsize &&
192 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
195 mp_opt->suboptions |= OPTION_MPTCP_DSS;
196 if (mp_opt->use_ack) {
198 mp_opt->data_ack = get_unaligned_be64(ptr);
201 mp_opt->data_ack = get_unaligned_be32(ptr);
205 pr_debug("data_ack=%llu", mp_opt->data_ack);
208 if (mp_opt->use_map) {
210 mp_opt->data_seq = get_unaligned_be64(ptr);
213 mp_opt->data_seq = get_unaligned_be32(ptr);
217 mp_opt->subflow_seq = get_unaligned_be32(ptr);
220 mp_opt->data_len = get_unaligned_be16(ptr);
223 if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
224 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
225 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
229 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
230 mp_opt->data_seq, mp_opt->subflow_seq,
231 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
237 case MPTCPOPT_ADD_ADDR:
238 mp_opt->echo = (*ptr++) & MPTCP_ADDR_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;
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;
263 mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
264 mp_opt->addr.id = *ptr++;
265 mp_opt->addr.port = 0;
267 if (mp_opt->addr.family == AF_INET) {
268 memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
270 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
271 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
272 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
276 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
278 memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
280 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
281 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
282 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
288 mp_opt->ahmac = get_unaligned_be64(ptr);
291 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
292 (mp_opt->addr.family == AF_INET6) ? "6" : "",
293 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
296 case MPTCPOPT_RM_ADDR:
297 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
298 opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
303 mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
304 mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
305 for (i = 0; i < mp_opt->rm_list.nr; i++)
306 mp_opt->rm_list.ids[i] = *ptr++;
307 pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
310 case MPTCPOPT_MP_PRIO:
311 if (opsize != TCPOLEN_MPTCP_PRIO)
314 mp_opt->suboptions |= OPTION_MPTCP_PRIO;
315 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
316 pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
319 case MPTCPOPT_MP_FASTCLOSE:
320 if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
324 mp_opt->rcvr_key = get_unaligned_be64(ptr);
326 mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
327 pr_debug("MP_FASTCLOSE: recv_key=%llu", mp_opt->rcvr_key);
331 if (opsize != TCPOLEN_MPTCP_RST)
334 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
337 mp_opt->suboptions |= OPTION_MPTCP_RST;
339 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
340 mp_opt->reset_reason = *ptr;
341 pr_debug("MP_RST: transient=%u reason=%u",
342 mp_opt->reset_transient, mp_opt->reset_reason);
345 case MPTCPOPT_MP_FAIL:
346 if (opsize != TCPOLEN_MPTCP_FAIL)
350 mp_opt->suboptions |= OPTION_MPTCP_FAIL;
351 mp_opt->fail_seq = get_unaligned_be64(ptr);
352 pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
360 void mptcp_get_options(const struct sk_buff *skb,
361 struct mptcp_options_received *mp_opt)
363 const struct tcphdr *th = tcp_hdr(skb);
364 const unsigned char *ptr;
367 /* initialize option status */
368 mp_opt->suboptions = 0;
370 length = (th->doff * 4) - sizeof(struct tcphdr);
371 ptr = (const unsigned char *)(th + 1);
380 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
387 if (opsize < 2) /* "silly options" */
390 return; /* don't parse partial options */
391 if (opcode == TCPOPT_MPTCP)
392 mptcp_parse_option(skb, ptr, opsize, mp_opt);
399 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
400 unsigned int *size, struct mptcp_out_options *opts)
402 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
404 /* we will use snd_isn to detect first pkt [re]transmission
405 * in mptcp_established_options_mp()
407 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
408 if (subflow->request_mptcp) {
409 opts->suboptions = OPTION_MPTCP_MPC_SYN;
410 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
411 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
412 *size = TCPOLEN_MPTCP_MPC_SYN;
414 } else if (subflow->request_join) {
415 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
416 subflow->local_nonce);
417 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
418 opts->join_id = subflow->local_id;
419 opts->token = subflow->remote_token;
420 opts->nonce = subflow->local_nonce;
421 opts->backup = subflow->request_bkup;
422 *size = TCPOLEN_MPTCP_MPJ_SYN;
428 static void clear_3rdack_retransmission(struct sock *sk)
430 struct inet_connection_sock *icsk = inet_csk(sk);
432 sk_stop_timer(sk, &icsk->icsk_delack_timer);
433 icsk->icsk_ack.timeout = 0;
434 icsk->icsk_ack.ato = 0;
435 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
438 static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
439 bool snd_data_fin_enable,
441 unsigned int remaining,
442 struct mptcp_out_options *opts)
444 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
445 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
446 struct mptcp_ext *mpext;
447 unsigned int data_len;
450 /* When skb is not available, we better over-estimate the emitted
451 * options len. A full DSS option (28 bytes) is longer than
452 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
453 * tell the caller to defer the estimate to
454 * mptcp_established_options_dss(), which will reserve enough space.
459 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
460 if (subflow->fully_established || snd_data_fin_enable ||
461 subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
462 sk->sk_state != TCP_ESTABLISHED)
465 if (subflow->mp_capable) {
466 mpext = mptcp_get_ext(skb);
467 data_len = mpext ? mpext->data_len : 0;
469 /* we will check ops->data_len in mptcp_write_options() to
470 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
471 * TCPOLEN_MPTCP_MPC_ACK
473 opts->data_len = data_len;
474 opts->suboptions = OPTION_MPTCP_MPC_ACK;
475 opts->sndr_key = subflow->local_key;
476 opts->rcvr_key = subflow->remote_key;
477 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
478 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
481 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
482 * packets that start the first subflow of an MPTCP connection,
483 * as well as the first packet that carries data
486 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
487 if (opts->csum_reqd) {
488 /* we need to propagate more info to csum the pseudo hdr */
489 opts->data_seq = mpext->data_seq;
490 opts->subflow_seq = mpext->subflow_seq;
491 opts->csum = mpext->csum;
492 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
494 *size = ALIGN(len, 4);
496 *size = TCPOLEN_MPTCP_MPC_ACK;
499 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
500 subflow, subflow->local_key, subflow->remote_key,
504 } else if (subflow->mp_join) {
505 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
506 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
507 *size = TCPOLEN_MPTCP_MPJ_ACK;
508 pr_debug("subflow=%p", subflow);
510 /* we can use the full delegate action helper only from BH context
511 * If we are in process context - sk is flushing the backlog at
512 * socket lock release time - just set the appropriate flag, will
513 * be handled by the release callback
515 if (sock_owned_by_user(sk))
516 set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
518 mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
524 static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
525 struct sk_buff *skb, struct mptcp_ext *ext)
527 /* The write_seq value has already been incremented, so the actual
528 * sequence number for the DATA_FIN is one less.
530 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
532 if (!ext->use_map || !skb->len) {
533 /* RFC6824 requires a DSS mapping with specific values
534 * if DATA_FIN is set but no data payload is mapped
539 ext->data_seq = data_fin_tx_seq;
540 ext->subflow_seq = 0;
542 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
543 /* If there's an existing DSS mapping and it is the
544 * final mapping, DATA_FIN consumes 1 additional byte of
552 static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
553 bool snd_data_fin_enable,
555 unsigned int remaining,
556 struct mptcp_out_options *opts)
558 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
559 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
560 unsigned int dss_size = 0;
561 struct mptcp_ext *mpext;
562 unsigned int ack_size;
566 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
567 mpext = skb ? mptcp_get_ext(skb) : NULL;
569 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
570 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
574 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
576 opts->ext_copy = *mpext;
579 remaining -= map_size;
581 if (skb && snd_data_fin_enable)
582 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
583 opts->suboptions = OPTION_MPTCP_DSS;
587 /* passive sockets msk will set the 'can_ack' after accept(), even
588 * if the first subflow may have the already the remote key handy
590 opts->ext_copy.use_ack = 0;
591 if (!READ_ONCE(msk->can_ack)) {
592 *size = ALIGN(dss_size, 4);
596 ack_seq = READ_ONCE(msk->ack_seq);
597 if (READ_ONCE(msk->use_64bit_ack)) {
598 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
599 opts->ext_copy.data_ack = ack_seq;
600 opts->ext_copy.ack64 = 1;
602 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
603 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
604 opts->ext_copy.ack64 = 0;
606 opts->ext_copy.use_ack = 1;
607 opts->suboptions = OPTION_MPTCP_DSS;
608 WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
610 /* Add kind/length/subtype/flag overhead if mapping is not populated */
612 ack_size += TCPOLEN_MPTCP_DSS_BASE;
614 dss_size += ack_size;
616 *size = ALIGN(dss_size, 4);
620 static u64 add_addr_generate_hmac(u64 key1, u64 key2,
621 struct mptcp_addr_info *addr)
623 u16 port = ntohs(addr->port);
624 u8 hmac[SHA256_DIGEST_SIZE];
629 if (addr->family == AF_INET) {
630 memcpy(&msg[i], &addr->addr.s_addr, 4);
633 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
634 else if (addr->family == AF_INET6) {
635 memcpy(&msg[i], &addr->addr6.s6_addr, 16);
639 msg[i++] = port >> 8;
640 msg[i++] = port & 0xFF;
642 mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
644 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
647 static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
649 unsigned int remaining,
650 struct mptcp_out_options *opts)
652 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
653 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
654 bool drop_other_suboptions = false;
655 unsigned int opt_size = *size;
659 /* add addr will strip the existing options, be sure to avoid breaking
662 if (!mptcp_pm_should_add_signal(msk) ||
663 (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
664 !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
665 &echo, &drop_other_suboptions))
668 if (drop_other_suboptions)
669 remaining += opt_size;
670 len = mptcp_add_addr_len(opts->addr.family, echo, !!opts->addr.port);
675 if (drop_other_suboptions) {
676 pr_debug("drop other suboptions");
677 opts->suboptions = 0;
679 /* note that e.g. DSS could have written into the memory
680 * aliased by ahmac, we must reset the field here
681 * to avoid appending the hmac even for ADD_ADDR echo
687 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
689 opts->ahmac = add_addr_generate_hmac(msk->local_key,
693 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
694 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
699 static bool mptcp_established_options_rm_addr(struct sock *sk,
701 unsigned int remaining,
702 struct mptcp_out_options *opts)
704 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
705 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
706 struct mptcp_rm_list rm_list;
709 if (!mptcp_pm_should_rm_signal(msk) ||
710 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
713 len = mptcp_rm_addr_len(&rm_list);
720 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
721 opts->rm_list = rm_list;
723 for (i = 0; i < opts->rm_list.nr; i++)
724 pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
729 static bool mptcp_established_options_mp_prio(struct sock *sk,
731 unsigned int remaining,
732 struct mptcp_out_options *opts)
734 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
736 /* can't send MP_PRIO with MPC, as they share the same option space:
737 * 'backup'. Also it makes no sense at all
739 if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
742 /* account for the trailing 'nop' option */
743 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
746 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
747 opts->suboptions |= OPTION_MPTCP_PRIO;
748 opts->backup = subflow->request_bkup;
750 pr_debug("prio=%d", opts->backup);
755 static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
757 unsigned int remaining,
758 struct mptcp_out_options *opts)
760 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
762 if (remaining < TCPOLEN_MPTCP_RST)
765 *size = TCPOLEN_MPTCP_RST;
766 opts->suboptions |= OPTION_MPTCP_RST;
767 opts->reset_transient = subflow->reset_transient;
768 opts->reset_reason = subflow->reset_reason;
769 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
774 static bool mptcp_established_options_fastclose(struct sock *sk,
776 unsigned int remaining,
777 struct mptcp_out_options *opts)
779 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
780 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
782 if (likely(!subflow->send_fastclose))
785 if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
788 *size = TCPOLEN_MPTCP_FASTCLOSE;
789 opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
790 opts->rcvr_key = msk->remote_key;
792 pr_debug("FASTCLOSE key=%llu", opts->rcvr_key);
793 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
797 static bool mptcp_established_options_mp_fail(struct sock *sk,
799 unsigned int remaining,
800 struct mptcp_out_options *opts)
802 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
804 if (likely(!subflow->send_mp_fail))
807 if (remaining < TCPOLEN_MPTCP_FAIL)
810 *size = TCPOLEN_MPTCP_FAIL;
811 opts->suboptions |= OPTION_MPTCP_FAIL;
812 opts->fail_seq = subflow->map_seq;
814 pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
815 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
820 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
821 unsigned int *size, unsigned int remaining,
822 struct mptcp_out_options *opts)
824 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
825 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
826 unsigned int opt_size = 0;
830 opts->suboptions = 0;
832 if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
835 if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
836 if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
837 mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
839 remaining -= opt_size;
841 /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
842 if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
844 remaining -= opt_size;
849 snd_data_fin = mptcp_data_fin_enabled(msk);
850 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
852 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) {
853 unsigned int mp_fail_size;
856 if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
857 remaining - opt_size, opts)) {
858 *size += opt_size + mp_fail_size;
859 remaining -= opt_size - mp_fail_size;
864 /* we reserved enough space for the above options, and exceeding the
865 * TCP option space would be fatal
867 if (WARN_ON_ONCE(opt_size > remaining))
871 remaining -= opt_size;
872 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
874 remaining -= opt_size;
876 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
878 remaining -= opt_size;
882 if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
884 remaining -= opt_size;
891 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
892 struct mptcp_out_options *opts)
894 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
896 if (subflow_req->mp_capable) {
897 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
898 opts->sndr_key = subflow_req->local_key;
899 opts->csum_reqd = subflow_req->csum_reqd;
900 opts->allow_join_id0 = subflow_req->allow_join_id0;
901 *size = TCPOLEN_MPTCP_MPC_SYNACK;
902 pr_debug("subflow_req=%p, local_key=%llu",
903 subflow_req, subflow_req->local_key);
905 } else if (subflow_req->mp_join) {
906 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
907 opts->backup = subflow_req->backup;
908 opts->join_id = subflow_req->local_id;
909 opts->thmac = subflow_req->thmac;
910 opts->nonce = subflow_req->local_nonce;
911 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
912 subflow_req, opts->backup, opts->join_id,
913 opts->thmac, opts->nonce);
914 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
920 static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
921 struct mptcp_subflow_context *subflow,
923 struct mptcp_options_received *mp_opt)
925 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
926 * will make the subflow fully established
928 if (likely(subflow->fully_established)) {
929 /* on passive sockets, check for 3rd ack retransmission
930 * note that msk is always set by subflow_syn_recv_sock()
931 * for mp_join subflows
933 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
934 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
935 subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
936 !subflow->request_join)
938 goto fully_established;
941 /* we must process OoO packets before the first subflow is fully
942 * established. OoO packets are instead a protocol violation
943 * for MP_JOIN subflows as the peer must not send any data
944 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
946 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
947 if (subflow->mp_join)
949 return subflow->mp_capable;
952 if (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
953 ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo)) {
954 /* subflows are fully established as soon as we get any
955 * additional ack, including ADD_ADDR.
957 subflow->fully_established = 1;
958 WRITE_ONCE(msk->fully_established, true);
959 goto fully_established;
962 /* If the first established packet does not contain MP_CAPABLE + data
963 * then fallback to TCP. Fallback scenarios requires a reset for
966 if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
967 if (subflow->mp_join)
969 subflow->mp_capable = 0;
971 mptcp_do_fallback(ssk);
975 if (mp_opt->deny_join_id0)
976 WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
978 if (unlikely(!READ_ONCE(msk->pm.server_side)))
979 pr_warn_once("bogus mpc option on established client sk");
980 mptcp_subflow_fully_established(subflow, mp_opt);
983 /* if the subflow is not already linked into the conn_list, we can't
984 * notify the PM: this subflow is still on the listener queue
985 * and the PM possibly acquiring the subflow lock could race with
988 if (likely(subflow->pm_notified) || list_empty(&subflow->node))
991 subflow->pm_notified = 1;
992 if (subflow->mp_join) {
993 clear_3rdack_retransmission(ssk);
994 mptcp_pm_subflow_established(msk);
996 mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
1001 mptcp_subflow_reset(ssk);
1005 u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1007 u32 old_seq32, cur_seq32;
1009 old_seq32 = (u32)old_seq;
1010 cur_seq32 = (u32)cur_seq;
1011 cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
1012 if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
1013 return cur_seq + (1LL << 32);
1015 /* reverse wrap could happen, too */
1016 if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
1017 return cur_seq - (1LL << 32);
1021 static void ack_update_msk(struct mptcp_sock *msk,
1023 struct mptcp_options_received *mp_opt)
1025 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1026 struct sock *sk = (struct sock *)msk;
1029 mptcp_data_lock(sk);
1031 /* avoid ack expansion on update conflict, to reduce the risk of
1032 * wrongly expanding to a future ack sequence number, which is way
1033 * more dangerous than missing an ack
1035 old_snd_una = msk->snd_una;
1036 new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1038 /* ACK for data not even sent yet? Ignore.*/
1039 if (unlikely(after64(new_snd_una, snd_nxt)))
1040 new_snd_una = old_snd_una;
1042 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1044 if (after64(new_wnd_end, msk->wnd_end))
1045 msk->wnd_end = new_wnd_end;
1047 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1048 if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1049 __mptcp_check_push(sk, ssk);
1051 if (after64(new_snd_una, old_snd_una)) {
1052 msk->snd_una = new_snd_una;
1053 __mptcp_data_acked(sk);
1055 mptcp_data_unlock(sk);
1057 trace_ack_update_msk(mp_opt->data_ack,
1058 old_snd_una, new_snd_una,
1059 new_wnd_end, msk->wnd_end);
1062 bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1064 /* Skip if DATA_FIN was already received.
1065 * If updating simultaneously with the recvmsg loop, values
1066 * should match. If they mismatch, the peer is misbehaving and
1067 * we will prefer the most recent information.
1069 if (READ_ONCE(msk->rcv_data_fin))
1072 WRITE_ONCE(msk->rcv_data_fin_seq,
1073 mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1074 WRITE_ONCE(msk->rcv_data_fin, 1);
1079 static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1080 struct mptcp_options_received *mp_opt)
1087 hmac = add_addr_generate_hmac(msk->remote_key,
1091 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1092 msk, hmac, mp_opt->ahmac);
1094 return hmac == mp_opt->ahmac;
1097 /* Return false if a subflow has been reset, else return true */
1098 bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1100 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1101 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1102 struct mptcp_options_received mp_opt;
1103 struct mptcp_ext *mpext;
1105 if (__mptcp_check_fallback(msk)) {
1106 /* Keep it simple and unconditionally trigger send data cleanup and
1107 * pending queue spooling. We will need to acquire the data lock
1108 * for more accurate checks, and once the lock is acquired, such
1109 * helpers are cheap.
1111 mptcp_data_lock(subflow->conn);
1112 if (sk_stream_memory_free(sk))
1113 __mptcp_check_push(subflow->conn, sk);
1114 __mptcp_data_acked(subflow->conn);
1115 mptcp_data_unlock(subflow->conn);
1119 mptcp_get_options(skb, &mp_opt);
1121 /* The subflow can be in close state only if check_fully_established()
1122 * just sent a reset. If so, tell the caller to ignore the current packet.
1124 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1125 return sk->sk_state != TCP_CLOSE;
1127 if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1128 if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1129 msk->local_key == mp_opt.rcvr_key) {
1130 WRITE_ONCE(msk->rcv_fastclose, true);
1131 mptcp_schedule_work((struct sock *)msk);
1132 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
1135 if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1136 add_addr_hmac_valid(msk, &mp_opt)) {
1138 mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1139 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1141 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1142 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1143 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1146 if (mp_opt.addr.port)
1147 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1150 if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1151 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1153 if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1154 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1155 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1158 if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1159 mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1160 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1163 if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1164 subflow->reset_seen = 1;
1165 subflow->reset_reason = mp_opt.reset_reason;
1166 subflow->reset_transient = mp_opt.reset_transient;
1167 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1170 if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1174 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1175 * monodirectional flows will stuck
1178 ack_update_msk(msk, sk, &mp_opt);
1180 /* Zero-data-length packets are dropped by the caller and not
1181 * propagated to the MPTCP layer, so the skb extension does not
1182 * need to be allocated or populated. DATA_FIN information, if
1183 * present, needs to be updated here before the skb is freed.
1185 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1186 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1187 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
1188 mptcp_schedule_work((struct sock *)msk);
1193 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1197 memset(mpext, 0, sizeof(*mpext));
1199 if (likely(mp_opt.use_map)) {
1200 if (mp_opt.mpc_map) {
1201 /* this is an MP_CAPABLE carrying MPTCP data
1202 * we know this map the first chunk of data
1204 mptcp_crypto_key_sha(subflow->remote_key, NULL,
1207 mpext->subflow_seq = 1;
1210 mpext->data_fin = 0;
1212 mpext->data_seq = mp_opt.data_seq;
1213 mpext->subflow_seq = mp_opt.subflow_seq;
1214 mpext->dsn64 = mp_opt.dsn64;
1215 mpext->data_fin = mp_opt.data_fin;
1217 mpext->data_len = mp_opt.data_len;
1219 mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1221 if (mpext->csum_reqd)
1222 mpext->csum = mp_opt.csum;
1228 static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
1230 const struct sock *ssk = (const struct sock *)tp;
1231 struct mptcp_subflow_context *subflow;
1232 u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
1233 struct mptcp_sock *msk;
1237 subflow = mptcp_subflow_ctx(ssk);
1238 msk = mptcp_sk(subflow->conn);
1240 ack_seq = READ_ONCE(msk->ack_seq);
1241 rcv_wnd_new = ack_seq + tp->rcv_wnd;
1243 rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1244 if (after64(rcv_wnd_new, rcv_wnd_old)) {
1248 rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1250 if (rcv_wnd == rcv_wnd_old)
1253 rcv_wnd_old = rcv_wnd;
1254 if (before64(rcv_wnd_new, rcv_wnd_old)) {
1255 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1258 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1263 if (rcv_wnd_new != rcv_wnd_old) {
1265 win = rcv_wnd_old - ack_seq;
1266 tp->rcv_wnd = min_t(u64, win, U32_MAX);
1267 new_win = tp->rcv_wnd;
1269 /* Make sure we do not exceed the maximum possible
1272 if (unlikely(th->syn))
1273 new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1274 if (!tp->rx_opt.rcv_wscale &&
1275 READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1276 new_win = min(new_win, MAX_TCP_WINDOW);
1278 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1280 /* RFC1323 scaling applied */
1281 new_win >>= tp->rx_opt.rcv_wscale;
1282 th->window = htons(new_win);
1283 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1287 __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1289 struct csum_pseudo_header header;
1292 /* cfr RFC 8684 3.3.1.:
1293 * the data sequence number used in the pseudo-header is
1294 * always the 64-bit value, irrespective of what length is used in the
1295 * DSS option itself.
1297 header.data_seq = cpu_to_be64(data_seq);
1298 header.subflow_seq = htonl(subflow_seq);
1299 header.data_len = htons(data_len);
1302 csum = csum_partial(&header, sizeof(header), sum);
1303 return csum_fold(csum);
1306 static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1308 return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1309 ~csum_unfold(mpext->csum));
1312 static void put_len_csum(u16 len, __sum16 csum, void *data)
1314 __sum16 *sumptr = data + 2;
1317 put_unaligned_be16(len, ptr);
1319 put_unaligned(csum, sumptr);
1322 void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1323 struct mptcp_out_options *opts)
1325 const struct sock *ssk = (const struct sock *)tp;
1326 struct mptcp_subflow_context *subflow;
1328 /* Which options can be used together?
1330 * X: mutually exclusive
1331 * O: often used together
1332 * C: can be used together in some cases
1333 * P: could be used together but we prefer not to (optimisations)
1335 * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
1336 * ------|------|------|------|------|------|------|------|------|
1337 * MPC |------|------|------|------|------|------|------|------|
1338 * MPJ | X |------|------|------|------|------|------|------|
1339 * DSS | X | X |------|------|------|------|------|------|
1340 * ADD | X | X | P |------|------|------|------|------|
1341 * RM | C | C | C | P |------|------|------|------|
1342 * PRIO | X | C | C | C | C |------|------|------|
1343 * FAIL | X | X | C | X | X | X |------|------|
1344 * FC | X | X | X | X | X | X | X |------|
1345 * RST | X | X | X | X | X | X | O | O |
1346 * ------|------|------|------|------|------|------|------|------|
1348 * The same applies in mptcp_established_options() function.
1350 if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1351 struct mptcp_ext *mpext = &opts->ext_copy;
1352 u8 len = TCPOLEN_MPTCP_DSS_BASE;
1355 if (mpext->use_ack) {
1356 flags = MPTCP_DSS_HAS_ACK;
1358 len += TCPOLEN_MPTCP_DSS_ACK64;
1359 flags |= MPTCP_DSS_ACK64;
1361 len += TCPOLEN_MPTCP_DSS_ACK32;
1365 if (mpext->use_map) {
1366 len += TCPOLEN_MPTCP_DSS_MAP64;
1368 /* Use only 64-bit mapping flags for now, add
1369 * support for optional 32-bit mappings later.
1371 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1372 if (mpext->data_fin)
1373 flags |= MPTCP_DSS_DATA_FIN;
1375 if (opts->csum_reqd)
1376 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1379 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1381 if (mpext->use_ack) {
1383 put_unaligned_be64(mpext->data_ack, ptr);
1386 put_unaligned_be32(mpext->data_ack32, ptr);
1391 if (mpext->use_map) {
1392 put_unaligned_be64(mpext->data_seq, ptr);
1394 put_unaligned_be32(mpext->subflow_seq, ptr);
1396 if (opts->csum_reqd) {
1397 /* data_len == 0 is reserved for the infinite mapping,
1398 * the checksum will also be set to 0.
1400 put_len_csum(mpext->data_len,
1401 (mpext->data_len ? mptcp_make_csum(mpext) : 0),
1404 put_unaligned_be32(mpext->data_len << 16 |
1405 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1410 /* We might need to add MP_FAIL options in rare cases */
1411 if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1413 } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1414 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1416 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1417 len = TCPOLEN_MPTCP_MPC_SYN;
1418 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1419 len = TCPOLEN_MPTCP_MPC_SYNACK;
1420 } else if (opts->data_len) {
1421 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1422 if (opts->csum_reqd)
1423 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1425 len = TCPOLEN_MPTCP_MPC_ACK;
1428 if (opts->csum_reqd)
1429 flag |= MPTCP_CAP_CHECKSUM_REQD;
1431 if (!opts->allow_join_id0)
1432 flag |= MPTCP_CAP_DENY_JOIN_ID0;
1434 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1435 MPTCP_SUPPORTED_VERSION,
1438 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1440 goto mp_capable_done;
1442 put_unaligned_be64(opts->sndr_key, ptr);
1444 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1445 goto mp_capable_done;
1447 put_unaligned_be64(opts->rcvr_key, ptr);
1449 if (!opts->data_len)
1450 goto mp_capable_done;
1452 if (opts->csum_reqd) {
1453 put_len_csum(opts->data_len,
1454 __mptcp_make_csum(opts->data_seq,
1457 ~csum_unfold(opts->csum)),
1460 put_unaligned_be32(opts->data_len << 16 |
1461 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1465 /* MPC is additionally mutually exclusive with MP_PRIO */
1466 goto mp_capable_done;
1467 } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
1468 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1469 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1470 TCPOLEN_MPTCP_MPJ_SYN,
1471 opts->backup, opts->join_id);
1472 put_unaligned_be32(opts->token, ptr);
1474 put_unaligned_be32(opts->nonce, ptr);
1476 } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1477 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1478 TCPOLEN_MPTCP_MPJ_SYNACK,
1479 opts->backup, opts->join_id);
1480 put_unaligned_be64(opts->thmac, ptr);
1482 put_unaligned_be32(opts->nonce, ptr);
1485 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1486 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1487 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1490 } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1491 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1492 u8 echo = MPTCP_ADDR_ECHO;
1494 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1495 if (opts->addr.family == AF_INET6)
1496 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1499 if (opts->addr.port)
1500 len += TCPOLEN_MPTCP_PORT_LEN;
1503 len += sizeof(opts->ahmac);
1507 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1508 len, echo, opts->addr.id);
1509 if (opts->addr.family == AF_INET) {
1510 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1513 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1514 else if (opts->addr.family == AF_INET6) {
1515 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1520 if (!opts->addr.port) {
1522 put_unaligned_be64(opts->ahmac, ptr);
1526 u16 port = ntohs(opts->addr.port);
1529 u8 *bptr = (u8 *)ptr;
1531 put_unaligned_be16(port, bptr);
1533 put_unaligned_be64(opts->ahmac, bptr);
1535 put_unaligned_be16(TCPOPT_NOP << 8 |
1540 put_unaligned_be32(port << 16 |
1546 } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1547 /* FASTCLOSE is mutually exclusive with others except RST */
1548 *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1549 TCPOLEN_MPTCP_FASTCLOSE,
1551 put_unaligned_be64(opts->rcvr_key, ptr);
1554 if (OPTION_MPTCP_RST & opts->suboptions)
1557 } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1559 /* MP_FAIL is mutually exclusive with others except RST */
1560 subflow = mptcp_subflow_ctx(ssk);
1561 subflow->send_mp_fail = 0;
1563 *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1566 put_unaligned_be64(opts->fail_seq, ptr);
1569 if (OPTION_MPTCP_RST & opts->suboptions)
1572 } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1574 *ptr++ = mptcp_option(MPTCPOPT_RST,
1576 opts->reset_transient,
1577 opts->reset_reason);
1581 if (OPTION_MPTCP_PRIO & opts->suboptions) {
1582 subflow = mptcp_subflow_ctx(ssk);
1583 subflow->send_mp_prio = 0;
1585 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1587 opts->backup, TCPOPT_NOP);
1589 MPTCP_INC_STATS(sock_net((const struct sock *)tp),
1590 MPTCP_MIB_MPPRIOTX);
1594 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1597 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1598 TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1599 0, opts->rm_list.ids[0]);
1601 while (i < opts->rm_list.nr) {
1602 u8 id1, id2, id3, id4;
1604 id1 = opts->rm_list.ids[i];
1605 id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1606 id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1607 id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1608 put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1615 mptcp_set_rwin(tp, th);
1618 __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1620 const struct mptcp_ext *ext = mptcp_get_ext(skb);
1624 flags = ext->reset_transient;
1625 reason = ext->reset_reason;
1627 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1633 EXPORT_SYMBOL_GPL(mptcp_get_reset_option);