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
;
112 mp_opt
->data_len
= get_unaligned_be16(ptr
);
115 if (opsize
== TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM
) {
116 mp_opt
->csum
= get_unaligned((__force __sum16
*)ptr
);
117 mp_opt
->suboptions
|= OPTION_MPTCP_CSUMREQD
;
120 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u\n",
121 version
, flags
, opsize
, mp_opt
->sndr_key
,
122 mp_opt
->rcvr_key
, mp_opt
->data_len
, mp_opt
->csum
);
125 case MPTCPOPT_MP_JOIN
:
126 if (opsize
== TCPOLEN_MPTCP_MPJ_SYN
) {
127 mp_opt
->suboptions
|= OPTION_MPTCP_MPJ_SYN
;
128 mp_opt
->backup
= *ptr
++ & MPTCPOPT_BACKUP
;
129 mp_opt
->join_id
= *ptr
++;
130 mp_opt
->token
= get_unaligned_be32(ptr
);
132 mp_opt
->nonce
= get_unaligned_be32(ptr
);
134 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u\n",
135 mp_opt
->backup
, mp_opt
->join_id
,
136 mp_opt
->token
, mp_opt
->nonce
);
137 } else if (opsize
== TCPOLEN_MPTCP_MPJ_SYNACK
) {
138 mp_opt
->suboptions
|= OPTION_MPTCP_MPJ_SYNACK
;
139 mp_opt
->backup
= *ptr
++ & MPTCPOPT_BACKUP
;
140 mp_opt
->join_id
= *ptr
++;
141 mp_opt
->thmac
= get_unaligned_be64(ptr
);
143 mp_opt
->nonce
= get_unaligned_be32(ptr
);
145 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
146 mp_opt
->backup
, mp_opt
->join_id
,
147 mp_opt
->thmac
, mp_opt
->nonce
);
148 } else if (opsize
== TCPOLEN_MPTCP_MPJ_ACK
) {
149 mp_opt
->suboptions
|= OPTION_MPTCP_MPJ_ACK
;
151 memcpy(mp_opt
->hmac
, ptr
, MPTCPOPT_HMAC_LEN
);
152 pr_debug("MP_JOIN hmac\n");
160 /* we must clear 'mpc_map' be able to detect MP_CAPABLE
161 * map vs DSS map in mptcp_incoming_options(), and reconstruct
162 * map info accordingly
165 flags
= (*ptr
++) & MPTCP_DSS_FLAG_MASK
;
166 mp_opt
->data_fin
= (flags
& MPTCP_DSS_DATA_FIN
) != 0;
167 mp_opt
->dsn64
= (flags
& MPTCP_DSS_DSN64
) != 0;
168 mp_opt
->use_map
= (flags
& MPTCP_DSS_HAS_MAP
) != 0;
169 mp_opt
->ack64
= (flags
& MPTCP_DSS_ACK64
) != 0;
170 mp_opt
->use_ack
= (flags
& MPTCP_DSS_HAS_ACK
);
172 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d\n",
173 mp_opt
->data_fin
, mp_opt
->dsn64
,
174 mp_opt
->use_map
, mp_opt
->ack64
,
177 expected_opsize
= TCPOLEN_MPTCP_DSS_BASE
;
179 if (mp_opt
->use_ack
) {
181 expected_opsize
+= TCPOLEN_MPTCP_DSS_ACK64
;
183 expected_opsize
+= TCPOLEN_MPTCP_DSS_ACK32
;
186 if (mp_opt
->use_map
) {
188 expected_opsize
+= TCPOLEN_MPTCP_DSS_MAP64
;
190 expected_opsize
+= TCPOLEN_MPTCP_DSS_MAP32
;
193 /* Always parse any csum presence combination, we will enforce
194 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
196 if (opsize
!= expected_opsize
&&
197 opsize
!= expected_opsize
+ TCPOLEN_MPTCP_DSS_CHECKSUM
)
200 mp_opt
->suboptions
|= OPTION_MPTCP_DSS
;
201 if (mp_opt
->use_ack
) {
203 mp_opt
->data_ack
= get_unaligned_be64(ptr
);
206 mp_opt
->data_ack
= get_unaligned_be32(ptr
);
210 pr_debug("data_ack=%llu\n", mp_opt
->data_ack
);
213 if (mp_opt
->use_map
) {
215 mp_opt
->data_seq
= get_unaligned_be64(ptr
);
218 mp_opt
->data_seq
= get_unaligned_be32(ptr
);
222 mp_opt
->subflow_seq
= get_unaligned_be32(ptr
);
225 mp_opt
->data_len
= get_unaligned_be16(ptr
);
228 if (opsize
== expected_opsize
+ TCPOLEN_MPTCP_DSS_CHECKSUM
) {
229 mp_opt
->suboptions
|= OPTION_MPTCP_CSUMREQD
;
230 mp_opt
->csum
= get_unaligned((__force __sum16
*)ptr
);
234 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
235 mp_opt
->data_seq
, mp_opt
->subflow_seq
,
236 mp_opt
->data_len
, !!(mp_opt
->suboptions
& OPTION_MPTCP_CSUMREQD
),
242 case MPTCPOPT_ADD_ADDR
:
243 mp_opt
->echo
= (*ptr
++) & MPTCP_ADDR_ECHO
;
245 if (opsize
== TCPOLEN_MPTCP_ADD_ADDR
||
246 opsize
== TCPOLEN_MPTCP_ADD_ADDR_PORT
)
247 mp_opt
->addr
.family
= AF_INET
;
248 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
249 else if (opsize
== TCPOLEN_MPTCP_ADD_ADDR6
||
250 opsize
== TCPOLEN_MPTCP_ADD_ADDR6_PORT
)
251 mp_opt
->addr
.family
= AF_INET6
;
256 if (opsize
== TCPOLEN_MPTCP_ADD_ADDR_BASE
||
257 opsize
== TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT
)
258 mp_opt
->addr
.family
= AF_INET
;
259 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
260 else if (opsize
== TCPOLEN_MPTCP_ADD_ADDR6_BASE
||
261 opsize
== TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT
)
262 mp_opt
->addr
.family
= AF_INET6
;
268 mp_opt
->suboptions
|= OPTION_MPTCP_ADD_ADDR
;
269 mp_opt
->addr
.id
= *ptr
++;
270 mp_opt
->addr
.port
= 0;
272 if (mp_opt
->addr
.family
== AF_INET
) {
273 memcpy((u8
*)&mp_opt
->addr
.addr
.s_addr
, (u8
*)ptr
, 4);
275 if (opsize
== TCPOLEN_MPTCP_ADD_ADDR_PORT
||
276 opsize
== TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT
) {
277 mp_opt
->addr
.port
= htons(get_unaligned_be16(ptr
));
281 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
283 memcpy(mp_opt
->addr
.addr6
.s6_addr
, (u8
*)ptr
, 16);
285 if (opsize
== TCPOLEN_MPTCP_ADD_ADDR6_PORT
||
286 opsize
== TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT
) {
287 mp_opt
->addr
.port
= htons(get_unaligned_be16(ptr
));
293 mp_opt
->ahmac
= get_unaligned_be64(ptr
);
296 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d\n",
297 (mp_opt
->addr
.family
== AF_INET6
) ? "6" : "",
298 mp_opt
->addr
.id
, mp_opt
->ahmac
, mp_opt
->echo
, ntohs(mp_opt
->addr
.port
));
301 case MPTCPOPT_RM_ADDR
:
302 if (opsize
< TCPOLEN_MPTCP_RM_ADDR_BASE
+ 1 ||
303 opsize
> TCPOLEN_MPTCP_RM_ADDR_BASE
+ MPTCP_RM_IDS_MAX
)
308 mp_opt
->suboptions
|= OPTION_MPTCP_RM_ADDR
;
309 mp_opt
->rm_list
.nr
= opsize
- TCPOLEN_MPTCP_RM_ADDR_BASE
;
310 for (i
= 0; i
< mp_opt
->rm_list
.nr
; i
++)
311 mp_opt
->rm_list
.ids
[i
] = *ptr
++;
312 pr_debug("RM_ADDR: rm_list_nr=%d\n", mp_opt
->rm_list
.nr
);
315 case MPTCPOPT_MP_PRIO
:
316 if (opsize
!= TCPOLEN_MPTCP_PRIO
)
319 mp_opt
->suboptions
|= OPTION_MPTCP_PRIO
;
320 mp_opt
->backup
= *ptr
++ & MPTCP_PRIO_BKUP
;
321 pr_debug("MP_PRIO: prio=%d\n", mp_opt
->backup
);
324 case MPTCPOPT_MP_FASTCLOSE
:
325 if (opsize
!= TCPOLEN_MPTCP_FASTCLOSE
)
329 mp_opt
->rcvr_key
= get_unaligned_be64(ptr
);
331 mp_opt
->suboptions
|= OPTION_MPTCP_FASTCLOSE
;
332 pr_debug("MP_FASTCLOSE: recv_key=%llu\n", mp_opt
->rcvr_key
);
336 if (opsize
!= TCPOLEN_MPTCP_RST
)
339 if (!(TCP_SKB_CB(skb
)->tcp_flags
& TCPHDR_RST
))
342 mp_opt
->suboptions
|= OPTION_MPTCP_RST
;
344 mp_opt
->reset_transient
= flags
& MPTCP_RST_TRANSIENT
;
345 mp_opt
->reset_reason
= *ptr
;
346 pr_debug("MP_RST: transient=%u reason=%u\n",
347 mp_opt
->reset_transient
, mp_opt
->reset_reason
);
350 case MPTCPOPT_MP_FAIL
:
351 if (opsize
!= TCPOLEN_MPTCP_FAIL
)
355 mp_opt
->suboptions
|= OPTION_MPTCP_FAIL
;
356 mp_opt
->fail_seq
= get_unaligned_be64(ptr
);
357 pr_debug("MP_FAIL: data_seq=%llu\n", mp_opt
->fail_seq
);
365 void mptcp_get_options(const struct sk_buff
*skb
,
366 struct mptcp_options_received
*mp_opt
)
368 const struct tcphdr
*th
= tcp_hdr(skb
);
369 const unsigned char *ptr
;
372 /* initialize option status */
373 mp_opt
->suboptions
= 0;
375 length
= (th
->doff
* 4) - sizeof(struct tcphdr
);
376 ptr
= (const unsigned char *)(th
+ 1);
385 case TCPOPT_NOP
: /* Ref: RFC 793 section 3.1 */
392 if (opsize
< 2) /* "silly options" */
395 return; /* don't parse partial options */
396 if (opcode
== TCPOPT_MPTCP
)
397 mptcp_parse_option(skb
, ptr
, opsize
, mp_opt
);
404 bool mptcp_syn_options(struct sock
*sk
, const struct sk_buff
*skb
,
405 unsigned int *size
, struct mptcp_out_options
*opts
)
407 struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
409 /* we will use snd_isn to detect first pkt [re]transmission
410 * in mptcp_established_options_mp()
412 subflow
->snd_isn
= TCP_SKB_CB(skb
)->end_seq
;
413 if (subflow
->request_mptcp
) {
414 opts
->suboptions
= OPTION_MPTCP_MPC_SYN
;
415 opts
->csum_reqd
= mptcp_is_checksum_enabled(sock_net(sk
));
416 opts
->allow_join_id0
= mptcp_allow_join_id0(sock_net(sk
));
417 *size
= TCPOLEN_MPTCP_MPC_SYN
;
419 } else if (subflow
->request_join
) {
420 pr_debug("remote_token=%u, nonce=%u\n", subflow
->remote_token
,
421 subflow
->local_nonce
);
422 opts
->suboptions
= OPTION_MPTCP_MPJ_SYN
;
423 opts
->join_id
= subflow
->local_id
;
424 opts
->token
= subflow
->remote_token
;
425 opts
->nonce
= subflow
->local_nonce
;
426 opts
->backup
= subflow
->request_bkup
;
427 *size
= TCPOLEN_MPTCP_MPJ_SYN
;
433 static void clear_3rdack_retransmission(struct sock
*sk
)
435 struct inet_connection_sock
*icsk
= inet_csk(sk
);
437 sk_stop_timer(sk
, &icsk
->icsk_delack_timer
);
438 icsk
->icsk_ack
.timeout
= 0;
439 icsk
->icsk_ack
.ato
= 0;
440 icsk
->icsk_ack
.pending
&= ~(ICSK_ACK_SCHED
| ICSK_ACK_TIMER
);
443 static bool mptcp_established_options_mp(struct sock
*sk
, struct sk_buff
*skb
,
444 bool snd_data_fin_enable
,
446 struct mptcp_out_options
*opts
)
448 struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
449 struct mptcp_sock
*msk
= mptcp_sk(subflow
->conn
);
450 struct mptcp_ext
*mpext
;
451 unsigned int data_len
;
454 /* When skb is not available, we better over-estimate the emitted
455 * options len. A full DSS option (28 bytes) is longer than
456 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
457 * tell the caller to defer the estimate to
458 * mptcp_established_options_dss(), which will reserve enough space.
463 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
464 if (READ_ONCE(subflow
->fully_established
) || snd_data_fin_enable
||
465 subflow
->snd_isn
!= TCP_SKB_CB(skb
)->seq
||
466 sk
->sk_state
!= TCP_ESTABLISHED
)
469 if (subflow
->mp_capable
) {
470 mpext
= mptcp_get_ext(skb
);
471 data_len
= mpext
? mpext
->data_len
: 0;
473 /* we will check ops->data_len in mptcp_write_options() to
474 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
475 * TCPOLEN_MPTCP_MPC_ACK
477 opts
->data_len
= data_len
;
478 opts
->suboptions
= OPTION_MPTCP_MPC_ACK
;
479 opts
->sndr_key
= subflow
->local_key
;
480 opts
->rcvr_key
= subflow
->remote_key
;
481 opts
->csum_reqd
= READ_ONCE(msk
->csum_enabled
);
482 opts
->allow_join_id0
= mptcp_allow_join_id0(sock_net(sk
));
485 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
486 * packets that start the first subflow of an MPTCP connection,
487 * as well as the first packet that carries data
490 len
= TCPOLEN_MPTCP_MPC_ACK_DATA
;
491 if (opts
->csum_reqd
) {
492 /* we need to propagate more info to csum the pseudo hdr */
493 opts
->data_seq
= mpext
->data_seq
;
494 opts
->subflow_seq
= mpext
->subflow_seq
;
495 opts
->csum
= mpext
->csum
;
496 len
+= TCPOLEN_MPTCP_DSS_CHECKSUM
;
498 *size
= ALIGN(len
, 4);
500 *size
= TCPOLEN_MPTCP_MPC_ACK
;
503 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d\n",
504 subflow
, subflow
->local_key
, subflow
->remote_key
,
508 } else if (subflow
->mp_join
) {
509 opts
->suboptions
= OPTION_MPTCP_MPJ_ACK
;
510 memcpy(opts
->hmac
, subflow
->hmac
, MPTCPOPT_HMAC_LEN
);
511 *size
= TCPOLEN_MPTCP_MPJ_ACK
;
512 pr_debug("subflow=%p\n", subflow
);
514 /* we can use the full delegate action helper only from BH context
515 * If we are in process context - sk is flushing the backlog at
516 * socket lock release time - just set the appropriate flag, will
517 * be handled by the release callback
519 if (sock_owned_by_user(sk
))
520 set_bit(MPTCP_DELEGATE_ACK
, &subflow
->delegated_status
);
522 mptcp_subflow_delegate(subflow
, MPTCP_DELEGATE_ACK
);
528 static void mptcp_write_data_fin(struct mptcp_subflow_context
*subflow
,
529 struct sk_buff
*skb
, struct mptcp_ext
*ext
)
531 /* The write_seq value has already been incremented, so the actual
532 * sequence number for the DATA_FIN is one less.
534 u64 data_fin_tx_seq
= READ_ONCE(mptcp_sk(subflow
->conn
)->write_seq
) - 1;
536 if (!ext
->use_map
|| !skb
->len
) {
537 /* RFC6824 requires a DSS mapping with specific values
538 * if DATA_FIN is set but no data payload is mapped
543 ext
->data_seq
= data_fin_tx_seq
;
544 ext
->subflow_seq
= 0;
546 } else if (ext
->data_seq
+ ext
->data_len
== data_fin_tx_seq
) {
547 /* If there's an existing DSS mapping and it is the
548 * final mapping, DATA_FIN consumes 1 additional byte of
556 static bool mptcp_established_options_dss(struct sock
*sk
, struct sk_buff
*skb
,
557 bool snd_data_fin_enable
,
559 struct mptcp_out_options
*opts
)
561 struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
562 struct mptcp_sock
*msk
= mptcp_sk(subflow
->conn
);
563 unsigned int dss_size
= 0;
564 struct mptcp_ext
*mpext
;
565 unsigned int ack_size
;
569 opts
->csum_reqd
= READ_ONCE(msk
->csum_enabled
);
570 mpext
= skb
? mptcp_get_ext(skb
) : NULL
;
572 if (!skb
|| (mpext
&& mpext
->use_map
) || snd_data_fin_enable
) {
573 unsigned int map_size
= TCPOLEN_MPTCP_DSS_BASE
+ TCPOLEN_MPTCP_DSS_MAP64
;
577 map_size
+= TCPOLEN_MPTCP_DSS_CHECKSUM
;
579 opts
->ext_copy
= *mpext
;
583 if (skb
&& snd_data_fin_enable
)
584 mptcp_write_data_fin(subflow
, skb
, &opts
->ext_copy
);
585 opts
->suboptions
= OPTION_MPTCP_DSS
;
589 /* passive sockets msk will set the 'can_ack' after accept(), even
590 * if the first subflow may have the already the remote key handy
592 opts
->ext_copy
.use_ack
= 0;
593 if (!READ_ONCE(msk
->can_ack
)) {
594 *size
= ALIGN(dss_size
, 4);
598 ack_seq
= READ_ONCE(msk
->ack_seq
);
599 if (READ_ONCE(msk
->use_64bit_ack
)) {
600 ack_size
= TCPOLEN_MPTCP_DSS_ACK64
;
601 opts
->ext_copy
.data_ack
= ack_seq
;
602 opts
->ext_copy
.ack64
= 1;
604 ack_size
= TCPOLEN_MPTCP_DSS_ACK32
;
605 opts
->ext_copy
.data_ack32
= (uint32_t)ack_seq
;
606 opts
->ext_copy
.ack64
= 0;
608 opts
->ext_copy
.use_ack
= 1;
609 opts
->suboptions
= OPTION_MPTCP_DSS
;
610 WRITE_ONCE(msk
->old_wspace
, __mptcp_space((struct sock
*)msk
));
612 /* Add kind/length/subtype/flag overhead if mapping is not populated */
614 ack_size
+= TCPOLEN_MPTCP_DSS_BASE
;
616 dss_size
+= ack_size
;
618 *size
= ALIGN(dss_size
, 4);
622 static u64
add_addr_generate_hmac(u64 key1
, u64 key2
,
623 struct mptcp_addr_info
*addr
)
625 u16 port
= ntohs(addr
->port
);
626 u8 hmac
[SHA256_DIGEST_SIZE
];
631 if (addr
->family
== AF_INET
) {
632 memcpy(&msg
[i
], &addr
->addr
.s_addr
, 4);
635 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
636 else if (addr
->family
== AF_INET6
) {
637 memcpy(&msg
[i
], &addr
->addr6
.s6_addr
, 16);
641 msg
[i
++] = port
>> 8;
642 msg
[i
++] = port
& 0xFF;
644 mptcp_crypto_hmac_sha(key1
, key2
, msg
, i
, hmac
);
646 return get_unaligned_be64(&hmac
[SHA256_DIGEST_SIZE
- sizeof(u64
)]);
649 static bool mptcp_established_options_add_addr(struct sock
*sk
, struct sk_buff
*skb
,
651 unsigned int remaining
,
652 struct mptcp_out_options
*opts
)
654 struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
655 struct mptcp_sock
*msk
= mptcp_sk(subflow
->conn
);
656 bool drop_other_suboptions
= false;
657 unsigned int opt_size
= *size
;
661 /* add addr will strip the existing options, be sure to avoid breaking
664 if (!mptcp_pm_should_add_signal(msk
) ||
665 (opts
->suboptions
& (OPTION_MPTCP_MPJ_ACK
| OPTION_MPTCP_MPC_ACK
)) ||
666 !mptcp_pm_add_addr_signal(msk
, skb
, opt_size
, remaining
, &opts
->addr
,
667 &echo
, &drop_other_suboptions
))
671 * Later on, mptcp_write_options() will enforce mutually exclusion with
672 * DSS, bail out if such option is set and we can't drop it.
674 if (drop_other_suboptions
)
675 remaining
+= opt_size
;
676 else if (opts
->suboptions
& OPTION_MPTCP_DSS
)
679 len
= mptcp_add_addr_len(opts
->addr
.family
, echo
, !!opts
->addr
.port
);
684 if (drop_other_suboptions
) {
685 pr_debug("drop other suboptions\n");
686 opts
->suboptions
= 0;
688 /* note that e.g. DSS could have written into the memory
689 * aliased by ahmac, we must reset the field here
690 * to avoid appending the hmac even for ADD_ADDR echo
696 opts
->suboptions
|= OPTION_MPTCP_ADD_ADDR
;
698 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_ADDADDRTX
);
699 opts
->ahmac
= add_addr_generate_hmac(READ_ONCE(msk
->local_key
),
700 READ_ONCE(msk
->remote_key
),
703 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_ECHOADDTX
);
705 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d\n",
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\n", i
, opts
->rm_list
.ids
[i
]);
737 MPTCP_ADD_STATS(sock_net(sk
), MPTCP_MIB_RMADDRTX
, opts
->rm_list
.nr
);
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
|| (opts
->suboptions
& OPTIONS_MPTCP_MPC
))
754 /* account for the trailing 'nop' option */
755 if (remaining
< TCPOLEN_MPTCP_PRIO_ALIGN
)
758 *size
= TCPOLEN_MPTCP_PRIO_ALIGN
;
759 opts
->suboptions
|= OPTION_MPTCP_PRIO
;
760 opts
->backup
= subflow
->request_bkup
;
762 pr_debug("prio=%d\n", opts
->backup
);
767 static noinline
bool mptcp_established_options_rst(struct sock
*sk
, struct sk_buff
*skb
,
769 unsigned int remaining
,
770 struct mptcp_out_options
*opts
)
772 const struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
774 if (remaining
< TCPOLEN_MPTCP_RST
)
777 *size
= TCPOLEN_MPTCP_RST
;
778 opts
->suboptions
|= OPTION_MPTCP_RST
;
779 opts
->reset_transient
= subflow
->reset_transient
;
780 opts
->reset_reason
= subflow
->reset_reason
;
781 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_MPRSTTX
);
786 static bool mptcp_established_options_fastclose(struct sock
*sk
,
788 unsigned int remaining
,
789 struct mptcp_out_options
*opts
)
791 struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
792 struct mptcp_sock
*msk
= mptcp_sk(subflow
->conn
);
794 if (likely(!subflow
->send_fastclose
))
797 if (remaining
< TCPOLEN_MPTCP_FASTCLOSE
)
800 *size
= TCPOLEN_MPTCP_FASTCLOSE
;
801 opts
->suboptions
|= OPTION_MPTCP_FASTCLOSE
;
802 opts
->rcvr_key
= READ_ONCE(msk
->remote_key
);
804 pr_debug("FASTCLOSE key=%llu\n", opts
->rcvr_key
);
805 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_MPFASTCLOSETX
);
809 static bool mptcp_established_options_mp_fail(struct sock
*sk
,
811 unsigned int remaining
,
812 struct mptcp_out_options
*opts
)
814 struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
816 if (likely(!subflow
->send_mp_fail
))
819 if (remaining
< TCPOLEN_MPTCP_FAIL
)
822 *size
= TCPOLEN_MPTCP_FAIL
;
823 opts
->suboptions
|= OPTION_MPTCP_FAIL
;
824 opts
->fail_seq
= subflow
->map_seq
;
826 pr_debug("MP_FAIL fail_seq=%llu\n", opts
->fail_seq
);
827 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_MPFAILTX
);
832 bool mptcp_established_options(struct sock
*sk
, struct sk_buff
*skb
,
833 unsigned int *size
, unsigned int remaining
,
834 struct mptcp_out_options
*opts
)
836 struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
837 struct mptcp_sock
*msk
= mptcp_sk(subflow
->conn
);
838 unsigned int opt_size
= 0;
842 opts
->suboptions
= 0;
844 if (unlikely(__mptcp_check_fallback(msk
) && !mptcp_check_infinite_map(skb
)))
847 if (unlikely(skb
&& TCP_SKB_CB(skb
)->tcp_flags
& TCPHDR_RST
)) {
848 if (mptcp_established_options_fastclose(sk
, &opt_size
, remaining
, opts
) ||
849 mptcp_established_options_mp_fail(sk
, &opt_size
, remaining
, opts
)) {
851 remaining
-= opt_size
;
853 /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
854 if (mptcp_established_options_rst(sk
, skb
, &opt_size
, remaining
, opts
)) {
856 remaining
-= opt_size
;
861 snd_data_fin
= mptcp_data_fin_enabled(msk
);
862 if (mptcp_established_options_mp(sk
, skb
, snd_data_fin
, &opt_size
, opts
))
864 else if (mptcp_established_options_dss(sk
, skb
, snd_data_fin
, &opt_size
, opts
)) {
865 unsigned int mp_fail_size
;
868 if (mptcp_established_options_mp_fail(sk
, &mp_fail_size
,
869 remaining
- opt_size
, opts
)) {
870 *size
+= opt_size
+ mp_fail_size
;
871 remaining
-= opt_size
- mp_fail_size
;
876 /* we reserved enough space for the above options, and exceeding the
877 * TCP option space would be fatal
879 if (WARN_ON_ONCE(opt_size
> remaining
))
883 remaining
-= opt_size
;
884 if (mptcp_established_options_add_addr(sk
, skb
, &opt_size
, remaining
, opts
)) {
886 remaining
-= opt_size
;
888 } else if (mptcp_established_options_rm_addr(sk
, &opt_size
, remaining
, opts
)) {
890 remaining
-= opt_size
;
894 if (mptcp_established_options_mp_prio(sk
, &opt_size
, remaining
, opts
)) {
896 remaining
-= opt_size
;
903 bool mptcp_synack_options(const struct request_sock
*req
, unsigned int *size
,
904 struct mptcp_out_options
*opts
)
906 struct mptcp_subflow_request_sock
*subflow_req
= mptcp_subflow_rsk(req
);
908 if (subflow_req
->mp_capable
) {
909 opts
->suboptions
= OPTION_MPTCP_MPC_SYNACK
;
910 opts
->sndr_key
= subflow_req
->local_key
;
911 opts
->csum_reqd
= subflow_req
->csum_reqd
;
912 opts
->allow_join_id0
= subflow_req
->allow_join_id0
;
913 *size
= TCPOLEN_MPTCP_MPC_SYNACK
;
914 pr_debug("subflow_req=%p, local_key=%llu\n",
915 subflow_req
, subflow_req
->local_key
);
917 } else if (subflow_req
->mp_join
) {
918 opts
->suboptions
= OPTION_MPTCP_MPJ_SYNACK
;
919 opts
->backup
= subflow_req
->request_bkup
;
920 opts
->join_id
= subflow_req
->local_id
;
921 opts
->thmac
= subflow_req
->thmac
;
922 opts
->nonce
= subflow_req
->local_nonce
;
923 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
924 subflow_req
, opts
->backup
, opts
->join_id
,
925 opts
->thmac
, opts
->nonce
);
926 *size
= TCPOLEN_MPTCP_MPJ_SYNACK
;
932 static bool check_fully_established(struct mptcp_sock
*msk
, struct sock
*ssk
,
933 struct mptcp_subflow_context
*subflow
,
935 struct mptcp_options_received
*mp_opt
)
937 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
938 * will make the subflow fully established
940 if (likely(READ_ONCE(subflow
->fully_established
))) {
941 /* on passive sockets, check for 3rd ack retransmission
942 * note that msk is always set by subflow_syn_recv_sock()
943 * for mp_join subflows
945 if (TCP_SKB_CB(skb
)->seq
== subflow
->ssn_offset
+ 1 &&
946 TCP_SKB_CB(skb
)->end_seq
== TCP_SKB_CB(skb
)->seq
&&
947 subflow
->mp_join
&& (mp_opt
->suboptions
& OPTIONS_MPTCP_MPJ
) &&
948 !subflow
->request_join
)
953 /* we must process OoO packets before the first subflow is fully
954 * established. OoO packets are instead a protocol violation
955 * for MP_JOIN subflows as the peer must not send any data
956 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
958 if (TCP_SKB_CB(skb
)->seq
!= subflow
->ssn_offset
+ 1) {
959 if (subflow
->mp_join
)
961 if (subflow
->is_mptfo
&& mp_opt
->suboptions
& OPTION_MPTCP_MPC_ACK
)
962 goto set_fully_established
;
963 return subflow
->mp_capable
;
966 if (subflow
->remote_key_valid
&&
967 (((mp_opt
->suboptions
& OPTION_MPTCP_DSS
) && mp_opt
->use_ack
) ||
968 ((mp_opt
->suboptions
& OPTION_MPTCP_ADD_ADDR
) &&
969 (!mp_opt
->echo
|| subflow
->mp_join
)))) {
970 /* subflows are fully established as soon as we get any
971 * additional ack, including ADD_ADDR.
973 goto set_fully_established
;
976 /* If the first established packet does not contain MP_CAPABLE + data
977 * then fallback to TCP. Fallback scenarios requires a reset for
980 if (!(mp_opt
->suboptions
& OPTIONS_MPTCP_MPC
)) {
981 if (subflow
->mp_join
)
983 subflow
->mp_capable
= 0;
985 mptcp_do_fallback(ssk
);
989 if (mp_opt
->deny_join_id0
)
990 WRITE_ONCE(msk
->pm
.remote_deny_join_id0
, true);
992 if (unlikely(!READ_ONCE(msk
->pm
.server_side
)))
993 pr_warn_once("bogus mpc option on established client sk");
995 set_fully_established
:
996 mptcp_data_lock((struct sock
*)msk
);
997 __mptcp_subflow_fully_established(msk
, subflow
, mp_opt
);
998 mptcp_data_unlock((struct sock
*)msk
);
1001 /* if the subflow is not already linked into the conn_list, we can't
1002 * notify the PM: this subflow is still on the listener queue
1003 * and the PM possibly acquiring the subflow lock could race with
1004 * the listener close
1006 if (likely(subflow
->pm_notified
) || list_empty(&subflow
->node
))
1009 subflow
->pm_notified
= 1;
1010 if (subflow
->mp_join
) {
1011 clear_3rdack_retransmission(ssk
);
1012 mptcp_pm_subflow_established(msk
);
1014 mptcp_pm_fully_established(msk
, ssk
);
1019 mptcp_subflow_reset(ssk
);
1023 u64
__mptcp_expand_seq(u64 old_seq
, u64 cur_seq
)
1025 u32 old_seq32
, cur_seq32
;
1027 old_seq32
= (u32
)old_seq
;
1028 cur_seq32
= (u32
)cur_seq
;
1029 cur_seq
= (old_seq
& GENMASK_ULL(63, 32)) + cur_seq32
;
1030 if (unlikely(cur_seq32
< old_seq32
&& before(old_seq32
, cur_seq32
)))
1031 return cur_seq
+ (1LL << 32);
1033 /* reverse wrap could happen, too */
1034 if (unlikely(cur_seq32
> old_seq32
&& after(old_seq32
, cur_seq32
)))
1035 return cur_seq
- (1LL << 32);
1039 static void __mptcp_snd_una_update(struct mptcp_sock
*msk
, u64 new_snd_una
)
1041 msk
->bytes_acked
+= new_snd_una
- msk
->snd_una
;
1042 WRITE_ONCE(msk
->snd_una
, new_snd_una
);
1045 static void ack_update_msk(struct mptcp_sock
*msk
,
1047 struct mptcp_options_received
*mp_opt
)
1049 u64 new_wnd_end
, new_snd_una
, snd_nxt
= READ_ONCE(msk
->snd_nxt
);
1050 struct sock
*sk
= (struct sock
*)msk
;
1053 mptcp_data_lock(sk
);
1055 /* avoid ack expansion on update conflict, to reduce the risk of
1056 * wrongly expanding to a future ack sequence number, which is way
1057 * more dangerous than missing an ack
1059 old_snd_una
= msk
->snd_una
;
1060 new_snd_una
= mptcp_expand_seq(old_snd_una
, mp_opt
->data_ack
, mp_opt
->ack64
);
1062 /* ACK for data not even sent yet? Ignore.*/
1063 if (unlikely(after64(new_snd_una
, snd_nxt
)))
1064 new_snd_una
= old_snd_una
;
1066 new_wnd_end
= new_snd_una
+ tcp_sk(ssk
)->snd_wnd
;
1068 if (after64(new_wnd_end
, msk
->wnd_end
))
1069 WRITE_ONCE(msk
->wnd_end
, new_wnd_end
);
1071 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1072 if (after64(msk
->wnd_end
, snd_nxt
))
1073 __mptcp_check_push(sk
, ssk
);
1075 if (after64(new_snd_una
, old_snd_una
)) {
1076 __mptcp_snd_una_update(msk
, new_snd_una
);
1077 __mptcp_data_acked(sk
);
1079 msk
->last_ack_recv
= tcp_jiffies32
;
1080 mptcp_data_unlock(sk
);
1082 trace_ack_update_msk(mp_opt
->data_ack
,
1083 old_snd_una
, new_snd_una
,
1084 new_wnd_end
, READ_ONCE(msk
->wnd_end
));
1087 bool mptcp_update_rcv_data_fin(struct mptcp_sock
*msk
, u64 data_fin_seq
, bool use_64bit
)
1089 /* Skip if DATA_FIN was already received.
1090 * If updating simultaneously with the recvmsg loop, values
1091 * should match. If they mismatch, the peer is misbehaving and
1092 * we will prefer the most recent information.
1094 if (READ_ONCE(msk
->rcv_data_fin
))
1097 WRITE_ONCE(msk
->rcv_data_fin_seq
,
1098 mptcp_expand_seq(READ_ONCE(msk
->ack_seq
), data_fin_seq
, use_64bit
));
1099 WRITE_ONCE(msk
->rcv_data_fin
, 1);
1104 static bool add_addr_hmac_valid(struct mptcp_sock
*msk
,
1105 struct mptcp_options_received
*mp_opt
)
1112 hmac
= add_addr_generate_hmac(READ_ONCE(msk
->remote_key
),
1113 READ_ONCE(msk
->local_key
),
1116 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1117 msk
, hmac
, mp_opt
->ahmac
);
1119 return hmac
== mp_opt
->ahmac
;
1122 /* Return false if a subflow has been reset, else return true */
1123 bool mptcp_incoming_options(struct sock
*sk
, struct sk_buff
*skb
)
1125 struct mptcp_subflow_context
*subflow
= mptcp_subflow_ctx(sk
);
1126 struct mptcp_sock
*msk
= mptcp_sk(subflow
->conn
);
1127 struct mptcp_options_received mp_opt
;
1128 struct mptcp_ext
*mpext
;
1130 if (__mptcp_check_fallback(msk
)) {
1131 /* Keep it simple and unconditionally trigger send data cleanup and
1132 * pending queue spooling. We will need to acquire the data lock
1133 * for more accurate checks, and once the lock is acquired, such
1134 * helpers are cheap.
1136 mptcp_data_lock(subflow
->conn
);
1137 if (sk_stream_memory_free(sk
))
1138 __mptcp_check_push(subflow
->conn
, sk
);
1140 /* on fallback we just need to ignore the msk-level snd_una, as
1141 * this is really plain TCP
1143 __mptcp_snd_una_update(msk
, READ_ONCE(msk
->snd_nxt
));
1145 __mptcp_data_acked(subflow
->conn
);
1146 mptcp_data_unlock(subflow
->conn
);
1150 mptcp_get_options(skb
, &mp_opt
);
1152 /* The subflow can be in close state only if check_fully_established()
1153 * just sent a reset. If so, tell the caller to ignore the current packet.
1155 if (!check_fully_established(msk
, sk
, subflow
, skb
, &mp_opt
))
1156 return sk
->sk_state
!= TCP_CLOSE
;
1158 if (unlikely(mp_opt
.suboptions
!= OPTION_MPTCP_DSS
)) {
1159 if ((mp_opt
.suboptions
& OPTION_MPTCP_FASTCLOSE
) &&
1160 READ_ONCE(msk
->local_key
) == mp_opt
.rcvr_key
) {
1161 WRITE_ONCE(msk
->rcv_fastclose
, true);
1162 mptcp_schedule_work((struct sock
*)msk
);
1163 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_MPFASTCLOSERX
);
1166 if ((mp_opt
.suboptions
& OPTION_MPTCP_ADD_ADDR
) &&
1167 add_addr_hmac_valid(msk
, &mp_opt
)) {
1169 mptcp_pm_add_addr_received(sk
, &mp_opt
.addr
);
1170 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_ADDADDR
);
1172 mptcp_pm_add_addr_echoed(msk
, &mp_opt
.addr
);
1173 mptcp_pm_del_add_timer(msk
, &mp_opt
.addr
, true);
1174 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_ECHOADD
);
1177 if (mp_opt
.addr
.port
)
1178 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_PORTADD
);
1181 if (mp_opt
.suboptions
& OPTION_MPTCP_RM_ADDR
)
1182 mptcp_pm_rm_addr_received(msk
, &mp_opt
.rm_list
);
1184 if (mp_opt
.suboptions
& OPTION_MPTCP_PRIO
) {
1185 mptcp_pm_mp_prio_received(sk
, mp_opt
.backup
);
1186 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_MPPRIORX
);
1189 if (mp_opt
.suboptions
& OPTION_MPTCP_FAIL
) {
1190 mptcp_pm_mp_fail_received(sk
, mp_opt
.fail_seq
);
1191 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_MPFAILRX
);
1194 if (mp_opt
.suboptions
& OPTION_MPTCP_RST
) {
1195 subflow
->reset_seen
= 1;
1196 subflow
->reset_reason
= mp_opt
.reset_reason
;
1197 subflow
->reset_transient
= mp_opt
.reset_transient
;
1198 MPTCP_INC_STATS(sock_net(sk
), MPTCP_MIB_MPRSTRX
);
1201 if (!(mp_opt
.suboptions
& OPTION_MPTCP_DSS
))
1205 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1206 * monodirectional flows will stuck
1209 ack_update_msk(msk
, sk
, &mp_opt
);
1211 /* Zero-data-length packets are dropped by the caller and not
1212 * propagated to the MPTCP layer, so the skb extension does not
1213 * need to be allocated or populated. DATA_FIN information, if
1214 * present, needs to be updated here before the skb is freed.
1216 if (TCP_SKB_CB(skb
)->seq
== TCP_SKB_CB(skb
)->end_seq
) {
1217 if (mp_opt
.data_fin
&& mp_opt
.data_len
== 1 &&
1218 mptcp_update_rcv_data_fin(msk
, mp_opt
.data_seq
, mp_opt
.dsn64
))
1219 mptcp_schedule_work((struct sock
*)msk
);
1224 mpext
= skb_ext_add(skb
, SKB_EXT_MPTCP
);
1228 memset(mpext
, 0, sizeof(*mpext
));
1230 if (likely(mp_opt
.use_map
)) {
1231 if (mp_opt
.mpc_map
) {
1232 /* this is an MP_CAPABLE carrying MPTCP data
1233 * we know this map the first chunk of data
1235 mptcp_crypto_key_sha(subflow
->remote_key
, NULL
,
1238 mpext
->subflow_seq
= 1;
1241 mpext
->data_fin
= 0;
1243 mpext
->data_seq
= mp_opt
.data_seq
;
1244 mpext
->subflow_seq
= mp_opt
.subflow_seq
;
1245 mpext
->dsn64
= mp_opt
.dsn64
;
1246 mpext
->data_fin
= mp_opt
.data_fin
;
1248 mpext
->data_len
= mp_opt
.data_len
;
1250 mpext
->csum_reqd
= !!(mp_opt
.suboptions
& OPTION_MPTCP_CSUMREQD
);
1252 if (mpext
->csum_reqd
)
1253 mpext
->csum
= mp_opt
.csum
;
1259 static void mptcp_set_rwin(struct tcp_sock
*tp
, struct tcphdr
*th
)
1261 const struct sock
*ssk
= (const struct sock
*)tp
;
1262 struct mptcp_subflow_context
*subflow
;
1263 u64 ack_seq
, rcv_wnd_old
, rcv_wnd_new
;
1264 struct mptcp_sock
*msk
;
1268 subflow
= mptcp_subflow_ctx(ssk
);
1269 msk
= mptcp_sk(subflow
->conn
);
1271 ack_seq
= READ_ONCE(msk
->ack_seq
);
1272 rcv_wnd_new
= ack_seq
+ tp
->rcv_wnd
;
1274 rcv_wnd_old
= atomic64_read(&msk
->rcv_wnd_sent
);
1275 if (after64(rcv_wnd_new
, rcv_wnd_old
)) {
1279 rcv_wnd
= atomic64_cmpxchg(&msk
->rcv_wnd_sent
, rcv_wnd_old
, rcv_wnd_new
);
1281 if (rcv_wnd
== rcv_wnd_old
)
1284 rcv_wnd_old
= rcv_wnd
;
1285 if (before64(rcv_wnd_new
, rcv_wnd_old
)) {
1286 MPTCP_INC_STATS(sock_net(ssk
), MPTCP_MIB_RCVWNDCONFLICTUPDATE
);
1289 MPTCP_INC_STATS(sock_net(ssk
), MPTCP_MIB_RCVWNDCONFLICT
);
1294 if (rcv_wnd_new
!= rcv_wnd_old
) {
1296 win
= rcv_wnd_old
- ack_seq
;
1297 tp
->rcv_wnd
= min_t(u64
, win
, U32_MAX
);
1298 new_win
= tp
->rcv_wnd
;
1300 /* Make sure we do not exceed the maximum possible
1303 if (unlikely(th
->syn
))
1304 new_win
= min(new_win
, 65535U) << tp
->rx_opt
.rcv_wscale
;
1305 if (!tp
->rx_opt
.rcv_wscale
&&
1306 READ_ONCE(sock_net(ssk
)->ipv4
.sysctl_tcp_workaround_signed_windows
))
1307 new_win
= min(new_win
, MAX_TCP_WINDOW
);
1309 new_win
= min(new_win
, (65535U << tp
->rx_opt
.rcv_wscale
));
1311 /* RFC1323 scaling applied */
1312 new_win
>>= tp
->rx_opt
.rcv_wscale
;
1313 th
->window
= htons(new_win
);
1314 MPTCP_INC_STATS(sock_net(ssk
), MPTCP_MIB_RCVWNDSHARED
);
1318 __sum16
__mptcp_make_csum(u64 data_seq
, u32 subflow_seq
, u16 data_len
, __wsum sum
)
1320 struct csum_pseudo_header header
;
1323 /* cfr RFC 8684 3.3.1.:
1324 * the data sequence number used in the pseudo-header is
1325 * always the 64-bit value, irrespective of what length is used in the
1326 * DSS option itself.
1328 header
.data_seq
= cpu_to_be64(data_seq
);
1329 header
.subflow_seq
= htonl(subflow_seq
);
1330 header
.data_len
= htons(data_len
);
1333 csum
= csum_partial(&header
, sizeof(header
), sum
);
1334 return csum_fold(csum
);
1337 static __sum16
mptcp_make_csum(const struct mptcp_ext
*mpext
)
1339 return __mptcp_make_csum(mpext
->data_seq
, mpext
->subflow_seq
, mpext
->data_len
,
1340 ~csum_unfold(mpext
->csum
));
1343 static void put_len_csum(u16 len
, __sum16 csum
, void *data
)
1345 __sum16
*sumptr
= data
+ 2;
1348 put_unaligned_be16(len
, ptr
);
1350 put_unaligned(csum
, sumptr
);
1353 void mptcp_write_options(struct tcphdr
*th
, __be32
*ptr
, struct tcp_sock
*tp
,
1354 struct mptcp_out_options
*opts
)
1356 const struct sock
*ssk
= (const struct sock
*)tp
;
1357 struct mptcp_subflow_context
*subflow
;
1359 /* Which options can be used together?
1361 * X: mutually exclusive
1362 * O: often used together
1363 * C: can be used together in some cases
1364 * P: could be used together but we prefer not to (optimisations)
1366 * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
1367 * ------|------|------|------|------|------|------|------|------|
1368 * MPC |------|------|------|------|------|------|------|------|
1369 * MPJ | X |------|------|------|------|------|------|------|
1370 * DSS | X | X |------|------|------|------|------|------|
1371 * ADD | X | X | P |------|------|------|------|------|
1372 * RM | C | C | C | P |------|------|------|------|
1373 * PRIO | X | C | C | C | C |------|------|------|
1374 * FAIL | X | X | C | X | X | X |------|------|
1375 * FC | X | X | X | X | X | X | X |------|
1376 * RST | X | X | X | X | X | X | O | O |
1377 * ------|------|------|------|------|------|------|------|------|
1379 * The same applies in mptcp_established_options() function.
1381 if (likely(OPTION_MPTCP_DSS
& opts
->suboptions
)) {
1382 struct mptcp_ext
*mpext
= &opts
->ext_copy
;
1383 u8 len
= TCPOLEN_MPTCP_DSS_BASE
;
1386 if (mpext
->use_ack
) {
1387 flags
= MPTCP_DSS_HAS_ACK
;
1389 len
+= TCPOLEN_MPTCP_DSS_ACK64
;
1390 flags
|= MPTCP_DSS_ACK64
;
1392 len
+= TCPOLEN_MPTCP_DSS_ACK32
;
1396 if (mpext
->use_map
) {
1397 len
+= TCPOLEN_MPTCP_DSS_MAP64
;
1399 /* Use only 64-bit mapping flags for now, add
1400 * support for optional 32-bit mappings later.
1402 flags
|= MPTCP_DSS_HAS_MAP
| MPTCP_DSS_DSN64
;
1403 if (mpext
->data_fin
)
1404 flags
|= MPTCP_DSS_DATA_FIN
;
1406 if (opts
->csum_reqd
)
1407 len
+= TCPOLEN_MPTCP_DSS_CHECKSUM
;
1410 *ptr
++ = mptcp_option(MPTCPOPT_DSS
, len
, 0, flags
);
1412 if (mpext
->use_ack
) {
1414 put_unaligned_be64(mpext
->data_ack
, ptr
);
1417 put_unaligned_be32(mpext
->data_ack32
, ptr
);
1422 if (mpext
->use_map
) {
1423 put_unaligned_be64(mpext
->data_seq
, ptr
);
1425 put_unaligned_be32(mpext
->subflow_seq
, ptr
);
1427 if (opts
->csum_reqd
) {
1428 /* data_len == 0 is reserved for the infinite mapping,
1429 * the checksum will also be set to 0.
1431 put_len_csum(mpext
->data_len
,
1432 (mpext
->data_len
? mptcp_make_csum(mpext
) : 0),
1435 put_unaligned_be32(mpext
->data_len
<< 16 |
1436 TCPOPT_NOP
<< 8 | TCPOPT_NOP
, ptr
);
1441 /* We might need to add MP_FAIL options in rare cases */
1442 if (unlikely(OPTION_MPTCP_FAIL
& opts
->suboptions
))
1444 } else if (OPTIONS_MPTCP_MPC
& opts
->suboptions
) {
1445 u8 len
, flag
= MPTCP_CAP_HMAC_SHA256
;
1447 if (OPTION_MPTCP_MPC_SYN
& opts
->suboptions
) {
1448 len
= TCPOLEN_MPTCP_MPC_SYN
;
1449 } else if (OPTION_MPTCP_MPC_SYNACK
& opts
->suboptions
) {
1450 len
= TCPOLEN_MPTCP_MPC_SYNACK
;
1451 } else if (opts
->data_len
) {
1452 len
= TCPOLEN_MPTCP_MPC_ACK_DATA
;
1453 if (opts
->csum_reqd
)
1454 len
+= TCPOLEN_MPTCP_DSS_CHECKSUM
;
1456 len
= TCPOLEN_MPTCP_MPC_ACK
;
1459 if (opts
->csum_reqd
)
1460 flag
|= MPTCP_CAP_CHECKSUM_REQD
;
1462 if (!opts
->allow_join_id0
)
1463 flag
|= MPTCP_CAP_DENY_JOIN_ID0
;
1465 *ptr
++ = mptcp_option(MPTCPOPT_MP_CAPABLE
, len
,
1466 MPTCP_SUPPORTED_VERSION
,
1469 if (!((OPTION_MPTCP_MPC_SYNACK
| OPTION_MPTCP_MPC_ACK
) &
1471 goto mp_capable_done
;
1473 put_unaligned_be64(opts
->sndr_key
, ptr
);
1475 if (!((OPTION_MPTCP_MPC_ACK
) & opts
->suboptions
))
1476 goto mp_capable_done
;
1478 put_unaligned_be64(opts
->rcvr_key
, ptr
);
1480 if (!opts
->data_len
)
1481 goto mp_capable_done
;
1483 if (opts
->csum_reqd
) {
1484 put_len_csum(opts
->data_len
,
1485 __mptcp_make_csum(opts
->data_seq
,
1488 ~csum_unfold(opts
->csum
)),
1491 put_unaligned_be32(opts
->data_len
<< 16 |
1492 TCPOPT_NOP
<< 8 | TCPOPT_NOP
, ptr
);
1496 /* MPC is additionally mutually exclusive with MP_PRIO */
1497 goto mp_capable_done
;
1498 } else if (OPTIONS_MPTCP_MPJ
& opts
->suboptions
) {
1499 if (OPTION_MPTCP_MPJ_SYN
& opts
->suboptions
) {
1500 *ptr
++ = mptcp_option(MPTCPOPT_MP_JOIN
,
1501 TCPOLEN_MPTCP_MPJ_SYN
,
1502 opts
->backup
, opts
->join_id
);
1503 put_unaligned_be32(opts
->token
, ptr
);
1505 put_unaligned_be32(opts
->nonce
, ptr
);
1507 } else if (OPTION_MPTCP_MPJ_SYNACK
& opts
->suboptions
) {
1508 *ptr
++ = mptcp_option(MPTCPOPT_MP_JOIN
,
1509 TCPOLEN_MPTCP_MPJ_SYNACK
,
1510 opts
->backup
, opts
->join_id
);
1511 put_unaligned_be64(opts
->thmac
, ptr
);
1513 put_unaligned_be32(opts
->nonce
, ptr
);
1516 *ptr
++ = mptcp_option(MPTCPOPT_MP_JOIN
,
1517 TCPOLEN_MPTCP_MPJ_ACK
, 0, 0);
1518 memcpy(ptr
, opts
->hmac
, MPTCPOPT_HMAC_LEN
);
1521 } else if (OPTION_MPTCP_ADD_ADDR
& opts
->suboptions
) {
1522 u8 len
= TCPOLEN_MPTCP_ADD_ADDR_BASE
;
1523 u8 echo
= MPTCP_ADDR_ECHO
;
1525 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1526 if (opts
->addr
.family
== AF_INET6
)
1527 len
= TCPOLEN_MPTCP_ADD_ADDR6_BASE
;
1530 if (opts
->addr
.port
)
1531 len
+= TCPOLEN_MPTCP_PORT_LEN
;
1534 len
+= sizeof(opts
->ahmac
);
1538 *ptr
++ = mptcp_option(MPTCPOPT_ADD_ADDR
,
1539 len
, echo
, opts
->addr
.id
);
1540 if (opts
->addr
.family
== AF_INET
) {
1541 memcpy((u8
*)ptr
, (u8
*)&opts
->addr
.addr
.s_addr
, 4);
1544 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1545 else if (opts
->addr
.family
== AF_INET6
) {
1546 memcpy((u8
*)ptr
, opts
->addr
.addr6
.s6_addr
, 16);
1551 if (!opts
->addr
.port
) {
1553 put_unaligned_be64(opts
->ahmac
, ptr
);
1557 u16 port
= ntohs(opts
->addr
.port
);
1560 u8
*bptr
= (u8
*)ptr
;
1562 put_unaligned_be16(port
, bptr
);
1564 put_unaligned_be64(opts
->ahmac
, bptr
);
1566 put_unaligned_be16(TCPOPT_NOP
<< 8 |
1571 put_unaligned_be32(port
<< 16 |
1577 } else if (unlikely(OPTION_MPTCP_FASTCLOSE
& opts
->suboptions
)) {
1578 /* FASTCLOSE is mutually exclusive with others except RST */
1579 *ptr
++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE
,
1580 TCPOLEN_MPTCP_FASTCLOSE
,
1582 put_unaligned_be64(opts
->rcvr_key
, ptr
);
1585 if (OPTION_MPTCP_RST
& opts
->suboptions
)
1588 } else if (unlikely(OPTION_MPTCP_FAIL
& opts
->suboptions
)) {
1590 /* MP_FAIL is mutually exclusive with others except RST */
1591 subflow
= mptcp_subflow_ctx(ssk
);
1592 subflow
->send_mp_fail
= 0;
1594 *ptr
++ = mptcp_option(MPTCPOPT_MP_FAIL
,
1597 put_unaligned_be64(opts
->fail_seq
, ptr
);
1600 if (OPTION_MPTCP_RST
& opts
->suboptions
)
1603 } else if (unlikely(OPTION_MPTCP_RST
& opts
->suboptions
)) {
1605 *ptr
++ = mptcp_option(MPTCPOPT_RST
,
1607 opts
->reset_transient
,
1608 opts
->reset_reason
);
1612 if (OPTION_MPTCP_PRIO
& opts
->suboptions
) {
1613 subflow
= mptcp_subflow_ctx(ssk
);
1614 subflow
->send_mp_prio
= 0;
1616 *ptr
++ = mptcp_option(MPTCPOPT_MP_PRIO
,
1618 opts
->backup
, TCPOPT_NOP
);
1620 MPTCP_INC_STATS(sock_net(ssk
), MPTCP_MIB_MPPRIOTX
);
1624 if (OPTION_MPTCP_RM_ADDR
& opts
->suboptions
) {
1627 *ptr
++ = mptcp_option(MPTCPOPT_RM_ADDR
,
1628 TCPOLEN_MPTCP_RM_ADDR_BASE
+ opts
->rm_list
.nr
,
1629 0, opts
->rm_list
.ids
[0]);
1631 while (i
< opts
->rm_list
.nr
) {
1632 u8 id1
, id2
, id3
, id4
;
1634 id1
= opts
->rm_list
.ids
[i
];
1635 id2
= i
+ 1 < opts
->rm_list
.nr
? opts
->rm_list
.ids
[i
+ 1] : TCPOPT_NOP
;
1636 id3
= i
+ 2 < opts
->rm_list
.nr
? opts
->rm_list
.ids
[i
+ 2] : TCPOPT_NOP
;
1637 id4
= i
+ 3 < opts
->rm_list
.nr
? opts
->rm_list
.ids
[i
+ 3] : TCPOPT_NOP
;
1638 put_unaligned_be32(id1
<< 24 | id2
<< 16 | id3
<< 8 | id4
, ptr
);
1645 mptcp_set_rwin(tp
, th
);
1648 __be32
mptcp_get_reset_option(const struct sk_buff
*skb
)
1650 const struct mptcp_ext
*ext
= mptcp_get_ext(skb
);
1654 flags
= ext
->reset_transient
;
1655 reason
= ext
->reset_reason
;
1657 return mptcp_option(MPTCPOPT_RST
, TCPOLEN_MPTCP_RST
,
1663 EXPORT_SYMBOL_GPL(mptcp_get_reset_option
);