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;
106 mp_opt->data_len = get_unaligned_be16(ptr);
109 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
110 mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
111 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
114 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
115 version, flags, opsize, mp_opt->sndr_key,
116 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
119 case MPTCPOPT_MP_JOIN:
120 mp_opt->suboptions |= OPTIONS_MPTCP_MPJ;
121 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
122 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
123 mp_opt->join_id = *ptr++;
124 mp_opt->token = get_unaligned_be32(ptr);
126 mp_opt->nonce = get_unaligned_be32(ptr);
128 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
129 mp_opt->backup, mp_opt->join_id,
130 mp_opt->token, mp_opt->nonce);
131 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
132 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
133 mp_opt->join_id = *ptr++;
134 mp_opt->thmac = get_unaligned_be64(ptr);
136 mp_opt->nonce = get_unaligned_be32(ptr);
138 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
139 mp_opt->backup, mp_opt->join_id,
140 mp_opt->thmac, mp_opt->nonce);
141 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
143 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
144 pr_debug("MP_JOIN hmac");
146 mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ;
154 /* we must clear 'mpc_map' be able to detect MP_CAPABLE
155 * map vs DSS map in mptcp_incoming_options(), and reconstruct
156 * map info accordingly
159 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
160 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
161 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
162 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
163 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
164 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
166 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
167 mp_opt->data_fin, mp_opt->dsn64,
168 mp_opt->use_map, mp_opt->ack64,
171 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
173 if (mp_opt->use_ack) {
175 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
177 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
180 if (mp_opt->use_map) {
182 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
184 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
187 /* Always parse any csum presence combination, we will enforce
188 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
190 if (opsize != expected_opsize &&
191 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
194 mp_opt->suboptions |= OPTION_MPTCP_DSS;
195 if (mp_opt->use_ack) {
197 mp_opt->data_ack = get_unaligned_be64(ptr);
200 mp_opt->data_ack = get_unaligned_be32(ptr);
204 pr_debug("data_ack=%llu", mp_opt->data_ack);
207 if (mp_opt->use_map) {
209 mp_opt->data_seq = get_unaligned_be64(ptr);
212 mp_opt->data_seq = get_unaligned_be32(ptr);
216 mp_opt->subflow_seq = get_unaligned_be32(ptr);
219 mp_opt->data_len = get_unaligned_be16(ptr);
222 if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
223 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
224 mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
228 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
229 mp_opt->data_seq, mp_opt->subflow_seq,
230 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
236 case MPTCPOPT_ADD_ADDR:
237 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
239 if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
240 opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
241 mp_opt->addr.family = AF_INET;
242 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
243 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
244 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
245 mp_opt->addr.family = AF_INET6;
250 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
251 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
252 mp_opt->addr.family = AF_INET;
253 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
254 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
255 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
256 mp_opt->addr.family = AF_INET6;
262 mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
263 mp_opt->addr.id = *ptr++;
264 mp_opt->addr.port = 0;
266 if (mp_opt->addr.family == AF_INET) {
267 memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
269 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
270 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
271 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
275 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
277 memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
279 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
280 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
281 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
287 mp_opt->ahmac = get_unaligned_be64(ptr);
290 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
291 (mp_opt->addr.family == AF_INET6) ? "6" : "",
292 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
295 case MPTCPOPT_RM_ADDR:
296 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
297 opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
302 mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
303 mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
304 for (i = 0; i < mp_opt->rm_list.nr; i++)
305 mp_opt->rm_list.ids[i] = *ptr++;
306 pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
309 case MPTCPOPT_MP_PRIO:
310 if (opsize != TCPOLEN_MPTCP_PRIO)
313 mp_opt->suboptions |= OPTION_MPTCP_PRIO;
314 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
315 pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
318 case MPTCPOPT_MP_FASTCLOSE:
319 if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
323 mp_opt->rcvr_key = get_unaligned_be64(ptr);
325 mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
329 if (opsize != TCPOLEN_MPTCP_RST)
332 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
335 mp_opt->suboptions |= OPTION_MPTCP_RST;
337 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
338 mp_opt->reset_reason = *ptr;
341 case MPTCPOPT_MP_FAIL:
342 if (opsize != TCPOLEN_MPTCP_FAIL)
346 mp_opt->suboptions |= OPTION_MPTCP_FAIL;
347 mp_opt->fail_seq = get_unaligned_be64(ptr);
348 pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
356 void mptcp_get_options(const struct sock *sk,
357 const struct sk_buff *skb,
358 struct mptcp_options_received *mp_opt)
360 const struct tcphdr *th = tcp_hdr(skb);
361 const unsigned char *ptr;
364 /* initialize option status */
365 mp_opt->suboptions = 0;
367 length = (th->doff * 4) - sizeof(struct tcphdr);
368 ptr = (const unsigned char *)(th + 1);
377 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
384 if (opsize < 2) /* "silly options" */
387 return; /* don't parse partial options */
388 if (opcode == TCPOPT_MPTCP)
389 mptcp_parse_option(skb, ptr, opsize, mp_opt);
396 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
397 unsigned int *size, struct mptcp_out_options *opts)
399 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
401 /* we will use snd_isn to detect first pkt [re]transmission
402 * in mptcp_established_options_mp()
404 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
405 if (subflow->request_mptcp) {
406 opts->suboptions = OPTION_MPTCP_MPC_SYN;
407 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
408 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
409 *size = TCPOLEN_MPTCP_MPC_SYN;
411 } else if (subflow->request_join) {
412 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
413 subflow->local_nonce);
414 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
415 opts->join_id = subflow->local_id;
416 opts->token = subflow->remote_token;
417 opts->nonce = subflow->local_nonce;
418 opts->backup = subflow->request_bkup;
419 *size = TCPOLEN_MPTCP_MPJ_SYN;
425 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
426 * TCP can't schedule delack timer before the subflow is fully established.
427 * MPTCP uses the delack timer to do 3rd ack retransmissions
429 static void schedule_3rdack_retransmission(struct sock *sk)
431 struct inet_connection_sock *icsk = inet_csk(sk);
432 struct tcp_sock *tp = tcp_sk(sk);
433 unsigned long timeout;
435 /* reschedule with a timeout above RTT, as we must look only for drop */
437 timeout = tp->srtt_us << 1;
439 timeout = TCP_TIMEOUT_INIT;
441 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
442 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
443 icsk->icsk_ack.timeout = timeout;
444 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
447 static void clear_3rdack_retransmission(struct sock *sk)
449 struct inet_connection_sock *icsk = inet_csk(sk);
451 sk_stop_timer(sk, &icsk->icsk_delack_timer);
452 icsk->icsk_ack.timeout = 0;
453 icsk->icsk_ack.ato = 0;
454 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
457 static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
458 bool snd_data_fin_enable,
460 unsigned int remaining,
461 struct mptcp_out_options *opts)
463 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
464 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
465 struct mptcp_ext *mpext;
466 unsigned int data_len;
469 /* When skb is not available, we better over-estimate the emitted
470 * options len. A full DSS option (28 bytes) is longer than
471 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
472 * tell the caller to defer the estimate to
473 * mptcp_established_options_dss(), which will reserve enough space.
478 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
479 if (subflow->fully_established || snd_data_fin_enable ||
480 subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
481 sk->sk_state != TCP_ESTABLISHED)
484 if (subflow->mp_capable) {
485 mpext = mptcp_get_ext(skb);
486 data_len = mpext ? mpext->data_len : 0;
488 /* we will check ext_copy.data_len in mptcp_write_options() to
489 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
490 * TCPOLEN_MPTCP_MPC_ACK
492 opts->ext_copy.data_len = data_len;
493 opts->suboptions = OPTION_MPTCP_MPC_ACK;
494 opts->sndr_key = subflow->local_key;
495 opts->rcvr_key = subflow->remote_key;
496 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
497 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
500 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
501 * packets that start the first subflow of an MPTCP connection,
502 * as well as the first packet that carries data
505 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
506 if (opts->csum_reqd) {
507 /* we need to propagate more info to csum the pseudo hdr */
508 opts->ext_copy.data_seq = mpext->data_seq;
509 opts->ext_copy.subflow_seq = mpext->subflow_seq;
510 opts->ext_copy.csum = mpext->csum;
511 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
513 *size = ALIGN(len, 4);
515 *size = TCPOLEN_MPTCP_MPC_ACK;
518 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
519 subflow, subflow->local_key, subflow->remote_key,
523 } else if (subflow->mp_join) {
524 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
525 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
526 *size = TCPOLEN_MPTCP_MPJ_ACK;
527 pr_debug("subflow=%p", subflow);
529 schedule_3rdack_retransmission(sk);
535 static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
536 struct sk_buff *skb, struct mptcp_ext *ext)
538 /* The write_seq value has already been incremented, so the actual
539 * sequence number for the DATA_FIN is one less.
541 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
543 if (!ext->use_map || !skb->len) {
544 /* RFC6824 requires a DSS mapping with specific values
545 * if DATA_FIN is set but no data payload is mapped
550 ext->data_seq = data_fin_tx_seq;
551 ext->subflow_seq = 0;
553 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
554 /* If there's an existing DSS mapping and it is the
555 * final mapping, DATA_FIN consumes 1 additional byte of
563 static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
564 bool snd_data_fin_enable,
566 unsigned int remaining,
567 struct mptcp_out_options *opts)
569 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
570 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
571 unsigned int dss_size = 0;
572 struct mptcp_ext *mpext;
573 unsigned int ack_size;
577 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
578 mpext = skb ? mptcp_get_ext(skb) : NULL;
580 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
581 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
585 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
587 opts->ext_copy = *mpext;
590 remaining -= map_size;
592 if (skb && snd_data_fin_enable)
593 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
594 opts->suboptions = OPTION_MPTCP_DSS;
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
601 opts->ext_copy.use_ack = 0;
602 if (!READ_ONCE(msk->can_ack)) {
603 *size = ALIGN(dss_size, 4);
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;
613 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
614 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
615 opts->ext_copy.ack64 = 0;
617 opts->ext_copy.use_ack = 1;
618 opts->suboptions = OPTION_MPTCP_DSS;
619 WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
621 /* Add kind/length/subtype/flag overhead if mapping is not populated */
623 ack_size += TCPOLEN_MPTCP_DSS_BASE;
625 dss_size += ack_size;
627 *size = ALIGN(dss_size, 4);
631 static u64 add_addr_generate_hmac(u64 key1, u64 key2,
632 struct mptcp_addr_info *addr)
634 u16 port = ntohs(addr->port);
635 u8 hmac[SHA256_DIGEST_SIZE];
640 if (addr->family == AF_INET) {
641 memcpy(&msg[i], &addr->addr.s_addr, 4);
644 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
645 else if (addr->family == AF_INET6) {
646 memcpy(&msg[i], &addr->addr6.s6_addr, 16);
650 msg[i++] = port >> 8;
651 msg[i++] = port & 0xFF;
653 mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
655 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
658 static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
660 unsigned int remaining,
661 struct mptcp_out_options *opts)
663 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
664 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
665 bool drop_other_suboptions = false;
666 unsigned int opt_size = *size;
671 /* add addr will strip the existing options, be sure to avoid breaking
674 if (!mptcp_pm_should_add_signal(msk) ||
675 (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
676 !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
677 &echo, &port, &drop_other_suboptions))
680 if (drop_other_suboptions)
681 remaining += opt_size;
682 len = mptcp_add_addr_len(opts->addr.family, echo, port);
687 if (drop_other_suboptions) {
688 pr_debug("drop other suboptions");
689 opts->suboptions = 0;
691 /* note that e.g. DSS could have written into the memory
692 * aliased by ahmac, we must reset the field here
693 * to avoid appending the hmac even for ADD_ADDR echo
699 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
701 opts->ahmac = add_addr_generate_hmac(msk->local_key,
705 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
706 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
711 static bool mptcp_established_options_rm_addr(struct sock *sk,
713 unsigned int remaining,
714 struct mptcp_out_options *opts)
716 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
717 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
718 struct mptcp_rm_list rm_list;
721 if (!mptcp_pm_should_rm_signal(msk) ||
722 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
725 len = mptcp_rm_addr_len(&rm_list);
732 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
733 opts->rm_list = rm_list;
735 for (i = 0; i < opts->rm_list.nr; i++)
736 pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
741 static bool mptcp_established_options_mp_prio(struct sock *sk,
743 unsigned int remaining,
744 struct mptcp_out_options *opts)
746 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
748 /* can't send MP_PRIO with MPC, as they share the same option space:
749 * 'backup'. Also it makes no sense at all
751 if (!subflow->send_mp_prio ||
752 ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
753 OPTION_MPTCP_MPC_ACK) & opts->suboptions))
756 /* account for the trailing 'nop' option */
757 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
760 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
761 opts->suboptions |= OPTION_MPTCP_PRIO;
762 opts->backup = subflow->request_bkup;
764 pr_debug("prio=%d", opts->backup);
769 static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
771 unsigned int remaining,
772 struct mptcp_out_options *opts)
774 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
776 if (remaining < TCPOLEN_MPTCP_RST)
779 *size = TCPOLEN_MPTCP_RST;
780 opts->suboptions |= OPTION_MPTCP_RST;
781 opts->reset_transient = subflow->reset_transient;
782 opts->reset_reason = subflow->reset_reason;
787 static bool mptcp_established_options_mp_fail(struct sock *sk,
789 unsigned int remaining,
790 struct mptcp_out_options *opts)
792 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
794 if (likely(!subflow->send_mp_fail))
797 if (remaining < TCPOLEN_MPTCP_FAIL)
800 *size = TCPOLEN_MPTCP_FAIL;
801 opts->suboptions |= OPTION_MPTCP_FAIL;
802 opts->fail_seq = subflow->map_seq;
804 pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
809 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
810 unsigned int *size, unsigned int remaining,
811 struct mptcp_out_options *opts)
813 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
814 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
815 unsigned int opt_size = 0;
819 opts->suboptions = 0;
821 if (unlikely(__mptcp_check_fallback(msk)))
824 if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
825 if (mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
827 remaining -= opt_size;
829 if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
831 remaining -= opt_size;
836 snd_data_fin = mptcp_data_fin_enabled(msk);
837 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
839 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) {
841 if (mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
843 remaining -= opt_size;
848 /* we reserved enough space for the above options, and exceeding the
849 * TCP option space would be fatal
851 if (WARN_ON_ONCE(opt_size > remaining))
855 remaining -= opt_size;
856 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
858 remaining -= opt_size;
860 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
862 remaining -= opt_size;
866 if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
868 remaining -= opt_size;
875 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
876 struct mptcp_out_options *opts)
878 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
880 if (subflow_req->mp_capable) {
881 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
882 opts->sndr_key = subflow_req->local_key;
883 opts->csum_reqd = subflow_req->csum_reqd;
884 opts->allow_join_id0 = subflow_req->allow_join_id0;
885 *size = TCPOLEN_MPTCP_MPC_SYNACK;
886 pr_debug("subflow_req=%p, local_key=%llu",
887 subflow_req, subflow_req->local_key);
889 } else if (subflow_req->mp_join) {
890 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
891 opts->backup = subflow_req->backup;
892 opts->join_id = subflow_req->local_id;
893 opts->thmac = subflow_req->thmac;
894 opts->nonce = subflow_req->local_nonce;
895 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
896 subflow_req, opts->backup, opts->join_id,
897 opts->thmac, opts->nonce);
898 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
904 static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
905 struct mptcp_subflow_context *subflow,
907 struct mptcp_options_received *mp_opt)
909 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
910 * will make the subflow fully established
912 if (likely(subflow->fully_established)) {
913 /* on passive sockets, check for 3rd ack retransmission
914 * note that msk is always set by subflow_syn_recv_sock()
915 * for mp_join subflows
917 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
918 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
919 subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
920 READ_ONCE(msk->pm.server_side))
922 goto fully_established;
925 /* we must process OoO packets before the first subflow is fully
926 * established. OoO packets are instead a protocol violation
927 * for MP_JOIN subflows as the peer must not send any data
928 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
930 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
931 if (subflow->mp_join)
933 return subflow->mp_capable;
936 if (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
937 ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo)) {
938 /* subflows are fully established as soon as we get any
939 * additional ack, including ADD_ADDR.
941 subflow->fully_established = 1;
942 WRITE_ONCE(msk->fully_established, true);
943 goto fully_established;
946 /* If the first established packet does not contain MP_CAPABLE + data
947 * then fallback to TCP. Fallback scenarios requires a reset for
950 if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
951 if (subflow->mp_join)
953 subflow->mp_capable = 0;
955 __mptcp_do_fallback(msk);
959 if (mp_opt->deny_join_id0)
960 WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
962 if (unlikely(!READ_ONCE(msk->pm.server_side)))
963 pr_warn_once("bogus mpc option on established client sk");
964 mptcp_subflow_fully_established(subflow, mp_opt);
967 /* if the subflow is not already linked into the conn_list, we can't
968 * notify the PM: this subflow is still on the listener queue
969 * and the PM possibly acquiring the subflow lock could race with
972 if (likely(subflow->pm_notified) || list_empty(&subflow->node))
975 subflow->pm_notified = 1;
976 if (subflow->mp_join) {
977 clear_3rdack_retransmission(ssk);
978 mptcp_pm_subflow_established(msk);
980 mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
985 mptcp_subflow_reset(ssk);
989 u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
991 u32 old_seq32, cur_seq32;
993 old_seq32 = (u32)old_seq;
994 cur_seq32 = (u32)cur_seq;
995 cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
996 if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
997 return cur_seq + (1LL << 32);
999 /* reverse wrap could happen, too */
1000 if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
1001 return cur_seq - (1LL << 32);
1005 static void ack_update_msk(struct mptcp_sock *msk,
1007 struct mptcp_options_received *mp_opt)
1009 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1010 struct sock *sk = (struct sock *)msk;
1013 mptcp_data_lock(sk);
1015 /* avoid ack expansion on update conflict, to reduce the risk of
1016 * wrongly expanding to a future ack sequence number, which is way
1017 * more dangerous than missing an ack
1019 old_snd_una = msk->snd_una;
1020 new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1022 /* ACK for data not even sent yet and even above recovery bound? Ignore.*/
1023 if (unlikely(after64(new_snd_una, snd_nxt))) {
1024 if (!msk->recovery || after64(new_snd_una, msk->recovery_snd_nxt))
1025 new_snd_una = old_snd_una;
1028 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1030 if (after64(new_wnd_end, msk->wnd_end))
1031 msk->wnd_end = new_wnd_end;
1033 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1034 if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1035 __mptcp_check_push(sk, ssk);
1037 if (after64(new_snd_una, old_snd_una)) {
1038 msk->snd_una = new_snd_una;
1039 __mptcp_data_acked(sk);
1041 mptcp_data_unlock(sk);
1043 trace_ack_update_msk(mp_opt->data_ack,
1044 old_snd_una, new_snd_una,
1045 new_wnd_end, msk->wnd_end);
1048 bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1050 /* Skip if DATA_FIN was already received.
1051 * If updating simultaneously with the recvmsg loop, values
1052 * should match. If they mismatch, the peer is misbehaving and
1053 * we will prefer the most recent information.
1055 if (READ_ONCE(msk->rcv_data_fin))
1058 WRITE_ONCE(msk->rcv_data_fin_seq,
1059 mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1060 WRITE_ONCE(msk->rcv_data_fin, 1);
1065 static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1066 struct mptcp_options_received *mp_opt)
1073 hmac = add_addr_generate_hmac(msk->remote_key,
1077 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1078 msk, (unsigned long long)hmac,
1079 (unsigned long long)mp_opt->ahmac);
1081 return hmac == mp_opt->ahmac;
1084 /* Return false if a subflow has been reset, else return true */
1085 bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1087 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1088 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1089 struct mptcp_options_received mp_opt;
1090 struct mptcp_ext *mpext;
1092 if (__mptcp_check_fallback(msk)) {
1093 /* Keep it simple and unconditionally trigger send data cleanup and
1094 * pending queue spooling. We will need to acquire the data lock
1095 * for more accurate checks, and once the lock is acquired, such
1096 * helpers are cheap.
1098 mptcp_data_lock(subflow->conn);
1099 if (sk_stream_memory_free(sk))
1100 __mptcp_check_push(subflow->conn, sk);
1101 __mptcp_data_acked(subflow->conn);
1102 mptcp_data_unlock(subflow->conn);
1106 mptcp_get_options(sk, skb, &mp_opt);
1108 /* The subflow can be in close state only if check_fully_established()
1109 * just sent a reset. If so, tell the caller to ignore the current packet.
1111 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1112 return sk->sk_state != TCP_CLOSE;
1114 if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1115 if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1116 msk->local_key == mp_opt.rcvr_key) {
1117 WRITE_ONCE(msk->rcv_fastclose, true);
1118 mptcp_schedule_work((struct sock *)msk);
1121 if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1122 add_addr_hmac_valid(msk, &mp_opt)) {
1124 mptcp_pm_add_addr_received(msk, &mp_opt.addr);
1125 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1127 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1128 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1129 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1132 if (mp_opt.addr.port)
1133 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1136 if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1137 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1139 if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1140 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1141 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1144 if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1145 mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1146 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1149 if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1150 subflow->reset_seen = 1;
1151 subflow->reset_reason = mp_opt.reset_reason;
1152 subflow->reset_transient = mp_opt.reset_transient;
1155 if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1159 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1160 * monodirectional flows will stuck
1163 ack_update_msk(msk, sk, &mp_opt);
1165 /* Zero-data-length packets are dropped by the caller and not
1166 * propagated to the MPTCP layer, so the skb extension does not
1167 * need to be allocated or populated. DATA_FIN information, if
1168 * present, needs to be updated here before the skb is freed.
1170 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1171 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1172 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
1173 schedule_work(&msk->work))
1174 sock_hold(subflow->conn);
1179 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1183 memset(mpext, 0, sizeof(*mpext));
1185 if (likely(mp_opt.use_map)) {
1186 if (mp_opt.mpc_map) {
1187 /* this is an MP_CAPABLE carrying MPTCP data
1188 * we know this map the first chunk of data
1190 mptcp_crypto_key_sha(subflow->remote_key, NULL,
1193 mpext->subflow_seq = 1;
1196 mpext->data_fin = 0;
1198 mpext->data_seq = mp_opt.data_seq;
1199 mpext->subflow_seq = mp_opt.subflow_seq;
1200 mpext->dsn64 = mp_opt.dsn64;
1201 mpext->data_fin = mp_opt.data_fin;
1203 mpext->data_len = mp_opt.data_len;
1205 mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1207 if (mpext->csum_reqd)
1208 mpext->csum = mp_opt.csum;
1214 static void mptcp_set_rwin(const struct tcp_sock *tp)
1216 const struct sock *ssk = (const struct sock *)tp;
1217 const struct mptcp_subflow_context *subflow;
1218 struct mptcp_sock *msk;
1221 subflow = mptcp_subflow_ctx(ssk);
1222 msk = mptcp_sk(subflow->conn);
1224 ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1226 if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1227 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1230 static u16 mptcp_make_csum(const struct mptcp_ext *mpext)
1232 struct csum_pseudo_header header;
1235 /* cfr RFC 8684 3.3.1.:
1236 * the data sequence number used in the pseudo-header is
1237 * always the 64-bit value, irrespective of what length is used in the
1238 * DSS option itself.
1240 header.data_seq = cpu_to_be64(mpext->data_seq);
1241 header.subflow_seq = htonl(mpext->subflow_seq);
1242 header.data_len = htons(mpext->data_len);
1245 csum = csum_partial(&header, sizeof(header), ~csum_unfold(mpext->csum));
1246 return (__force u16)csum_fold(csum);
1249 void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1250 struct mptcp_out_options *opts)
1252 if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1253 const struct sock *ssk = (const struct sock *)tp;
1254 struct mptcp_subflow_context *subflow;
1256 subflow = mptcp_subflow_ctx(ssk);
1257 subflow->send_mp_fail = 0;
1259 *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1262 put_unaligned_be64(opts->fail_seq, ptr);
1266 /* RST is mutually exclusive with everything else */
1267 if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1268 *ptr++ = mptcp_option(MPTCPOPT_RST,
1270 opts->reset_transient,
1271 opts->reset_reason);
1275 /* DSS, MPC, MPJ and ADD_ADDR are mutually exclusive, see
1276 * mptcp_established_options*()
1278 if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1279 struct mptcp_ext *mpext = &opts->ext_copy;
1280 u8 len = TCPOLEN_MPTCP_DSS_BASE;
1283 if (mpext->use_ack) {
1284 flags = MPTCP_DSS_HAS_ACK;
1286 len += TCPOLEN_MPTCP_DSS_ACK64;
1287 flags |= MPTCP_DSS_ACK64;
1289 len += TCPOLEN_MPTCP_DSS_ACK32;
1293 if (mpext->use_map) {
1294 len += TCPOLEN_MPTCP_DSS_MAP64;
1296 /* Use only 64-bit mapping flags for now, add
1297 * support for optional 32-bit mappings later.
1299 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1300 if (mpext->data_fin)
1301 flags |= MPTCP_DSS_DATA_FIN;
1303 if (opts->csum_reqd)
1304 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1307 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1309 if (mpext->use_ack) {
1311 put_unaligned_be64(mpext->data_ack, ptr);
1314 put_unaligned_be32(mpext->data_ack32, ptr);
1319 if (mpext->use_map) {
1320 put_unaligned_be64(mpext->data_seq, ptr);
1322 put_unaligned_be32(mpext->subflow_seq, ptr);
1324 if (opts->csum_reqd) {
1325 put_unaligned_be32(mpext->data_len << 16 |
1326 mptcp_make_csum(mpext), ptr);
1328 put_unaligned_be32(mpext->data_len << 16 |
1329 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1332 } else if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
1333 OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
1334 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1336 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1337 len = TCPOLEN_MPTCP_MPC_SYN;
1338 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1339 len = TCPOLEN_MPTCP_MPC_SYNACK;
1340 } else if (opts->ext_copy.data_len) {
1341 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1342 if (opts->csum_reqd)
1343 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1345 len = TCPOLEN_MPTCP_MPC_ACK;
1348 if (opts->csum_reqd)
1349 flag |= MPTCP_CAP_CHECKSUM_REQD;
1351 if (!opts->allow_join_id0)
1352 flag |= MPTCP_CAP_DENY_JOIN_ID0;
1354 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1355 MPTCP_SUPPORTED_VERSION,
1358 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1360 goto mp_capable_done;
1362 put_unaligned_be64(opts->sndr_key, ptr);
1364 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1365 goto mp_capable_done;
1367 put_unaligned_be64(opts->rcvr_key, ptr);
1369 if (!opts->ext_copy.data_len)
1370 goto mp_capable_done;
1372 if (opts->csum_reqd) {
1373 put_unaligned_be32(opts->ext_copy.data_len << 16 |
1374 mptcp_make_csum(&opts->ext_copy), ptr);
1376 put_unaligned_be32(opts->ext_copy.data_len << 16 |
1377 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1381 /* MPC is additionally mutually exclusive with MP_PRIO */
1382 goto mp_capable_done;
1383 } else if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1384 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1385 TCPOLEN_MPTCP_MPJ_SYN,
1386 opts->backup, opts->join_id);
1387 put_unaligned_be32(opts->token, ptr);
1389 put_unaligned_be32(opts->nonce, ptr);
1391 } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1392 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1393 TCPOLEN_MPTCP_MPJ_SYNACK,
1394 opts->backup, opts->join_id);
1395 put_unaligned_be64(opts->thmac, ptr);
1397 put_unaligned_be32(opts->nonce, ptr);
1399 } else if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1400 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1401 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1402 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1404 } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1405 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1406 u8 echo = MPTCP_ADDR_ECHO;
1408 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1409 if (opts->addr.family == AF_INET6)
1410 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1413 if (opts->addr.port)
1414 len += TCPOLEN_MPTCP_PORT_LEN;
1417 len += sizeof(opts->ahmac);
1421 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1422 len, echo, opts->addr.id);
1423 if (opts->addr.family == AF_INET) {
1424 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1427 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1428 else if (opts->addr.family == AF_INET6) {
1429 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1434 if (!opts->addr.port) {
1436 put_unaligned_be64(opts->ahmac, ptr);
1440 u16 port = ntohs(opts->addr.port);
1443 u8 *bptr = (u8 *)ptr;
1445 put_unaligned_be16(port, bptr);
1447 put_unaligned_be64(opts->ahmac, bptr);
1449 put_unaligned_be16(TCPOPT_NOP << 8 |
1454 put_unaligned_be32(port << 16 |
1462 if (OPTION_MPTCP_PRIO & opts->suboptions) {
1463 const struct sock *ssk = (const struct sock *)tp;
1464 struct mptcp_subflow_context *subflow;
1466 subflow = mptcp_subflow_ctx(ssk);
1467 subflow->send_mp_prio = 0;
1469 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1471 opts->backup, TCPOPT_NOP);
1475 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1478 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1479 TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1480 0, opts->rm_list.ids[0]);
1482 while (i < opts->rm_list.nr) {
1483 u8 id1, id2, id3, id4;
1485 id1 = opts->rm_list.ids[i];
1486 id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1487 id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1488 id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1489 put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1499 __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1501 const struct mptcp_ext *ext = mptcp_get_ext(skb);
1505 flags = ext->reset_transient;
1506 reason = ext->reset_reason;
1508 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1514 EXPORT_SYMBOL_GPL(mptcp_get_reset_option);