blob: 1468774f1f87156c47c44e52ebed317a28499a22 [file] [log] [blame]
Peter Krystadeda7acd2020-01-21 16:56:16 -08001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
Geliang Tangc85adce2020-04-03 17:14:08 +08007#define pr_fmt(fmt) "MPTCP: " fmt
8
Peter Krystadeda7acd2020-01-21 16:56:16 -08009#include <linux/kernel.h>
Eric Biggersa24d22b2020-11-12 21:20:21 -080010#include <crypto/sha2.h>
Peter Krystadeda7acd2020-01-21 16:56:16 -080011#include <net/tcp.h>
12#include <net/mptcp.h>
13#include "protocol.h"
Geliang Tanga877de062020-09-24 08:29:51 +080014#include "mib.h"
Peter Krystadeda7acd2020-01-21 16:56:16 -080015
Geliang Tanged66bfb2021-04-16 15:38:06 -070016#include <trace/events/mptcp.h>
17
Paolo Abeni65492c52020-01-21 16:56:30 -080018static bool mptcp_cap_flag_sha256(u8 flags)
19{
20 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
21}
22
Paolo Abenicfde1412020-04-30 15:01:52 +020023static void mptcp_parse_option(const struct sk_buff *skb,
24 const unsigned char *ptr, int opsize,
25 struct mptcp_options_received *mp_opt)
Peter Krystadeda7acd2020-01-21 16:56:16 -080026{
Peter Krystadeda7acd2020-01-21 16:56:16 -080027 u8 subtype = *ptr >> 4;
Mat Martineau648ef4b2020-01-21 16:56:24 -080028 int expected_opsize;
Peter Krystadeda7acd2020-01-21 16:56:16 -080029 u8 version;
30 u8 flags;
Geliang Tang5c4a8242021-03-12 17:16:13 -080031 u8 i;
Peter Krystadeda7acd2020-01-21 16:56:16 -080032
33 switch (subtype) {
34 case MPTCPOPT_MP_CAPABLE:
Christoph Paaschcc7972e2020-01-21 16:56:31 -080035 /* strict size checking */
36 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
37 if (skb->len > tcp_hdr(skb)->doff << 2)
38 expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
39 else
40 expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
41 } else {
42 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
43 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
44 else
45 expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
46 }
47 if (opsize != expected_opsize)
Peter Krystadeda7acd2020-01-21 16:56:16 -080048 break;
49
Christoph Paaschcc7972e2020-01-21 16:56:31 -080050 /* try to be gentle vs future versions on the initial syn */
Peter Krystadeda7acd2020-01-21 16:56:16 -080051 version = *ptr++ & MPTCP_VERSION_MASK;
Christoph Paaschcc7972e2020-01-21 16:56:31 -080052 if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
53 if (version != MPTCP_SUPPORTED_VERSION)
54 break;
55 } else if (version < MPTCP_SUPPORTED_VERSION) {
Peter Krystadeda7acd2020-01-21 16:56:16 -080056 break;
Christoph Paaschcc7972e2020-01-21 16:56:31 -080057 }
Peter Krystadeda7acd2020-01-21 16:56:16 -080058
59 flags = *ptr++;
Paolo Abeni65492c52020-01-21 16:56:30 -080060 if (!mptcp_cap_flag_sha256(flags) ||
Peter Krystadeda7acd2020-01-21 16:56:16 -080061 (flags & MPTCP_CAP_EXTENSIBILITY))
62 break;
63
64 /* RFC 6824, Section 3.1:
65 * "For the Checksum Required bit (labeled "A"), if either
66 * host requires the use of checksums, checksums MUST be used.
67 * In other words, the only way for checksums not to be used
68 * is if both hosts in their SYNs set A=0."
69 *
70 * Section 3.3.0:
71 * "If a checksum is not present when its use has been
72 * negotiated, the receiver MUST close the subflow with a RST as
73 * it is considered broken."
74 *
75 * We don't implement DSS checksum - fall back to TCP.
76 */
77 if (flags & MPTCP_CAP_CHECKSUM_REQD)
78 break;
79
80 mp_opt->mp_capable = 1;
Christoph Paaschcc7972e2020-01-21 16:56:31 -080081 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
82 mp_opt->sndr_key = get_unaligned_be64(ptr);
83 ptr += 8;
84 }
85 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
Peter Krystadeda7acd2020-01-21 16:56:16 -080086 mp_opt->rcvr_key = get_unaligned_be64(ptr);
87 ptr += 8;
Peter Krystadeda7acd2020-01-21 16:56:16 -080088 }
Christoph Paaschcc7972e2020-01-21 16:56:31 -080089 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
90 /* Section 3.1.:
91 * "the data parameters in a MP_CAPABLE are semantically
92 * equivalent to those in a DSS option and can be used
93 * interchangeably."
94 */
95 mp_opt->dss = 1;
96 mp_opt->use_map = 1;
97 mp_opt->mpc_map = 1;
98 mp_opt->data_len = get_unaligned_be16(ptr);
99 ptr += 2;
100 }
101 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
102 version, flags, opsize, mp_opt->sndr_key,
103 mp_opt->rcvr_key, mp_opt->data_len);
Peter Krystadeda7acd2020-01-21 16:56:16 -0800104 break;
105
Peter Krystadf2962342020-03-27 14:48:39 -0700106 case MPTCPOPT_MP_JOIN:
107 mp_opt->mp_join = 1;
108 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
109 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
110 mp_opt->join_id = *ptr++;
111 mp_opt->token = get_unaligned_be32(ptr);
112 ptr += 4;
113 mp_opt->nonce = get_unaligned_be32(ptr);
114 ptr += 4;
115 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
116 mp_opt->backup, mp_opt->join_id,
117 mp_opt->token, mp_opt->nonce);
118 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
119 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
120 mp_opt->join_id = *ptr++;
121 mp_opt->thmac = get_unaligned_be64(ptr);
122 ptr += 8;
123 mp_opt->nonce = get_unaligned_be32(ptr);
124 ptr += 4;
125 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
126 mp_opt->backup, mp_opt->join_id,
127 mp_opt->thmac, mp_opt->nonce);
128 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
129 ptr += 2;
130 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
131 pr_debug("MP_JOIN hmac");
132 } else {
Peter Krystadf2962342020-03-27 14:48:39 -0700133 mp_opt->mp_join = 0;
134 }
135 break;
136
Peter Krystadeda7acd2020-01-21 16:56:16 -0800137 case MPTCPOPT_DSS:
138 pr_debug("DSS");
Mat Martineau648ef4b2020-01-21 16:56:24 -0800139 ptr++;
140
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800141 /* we must clear 'mpc_map' be able to detect MP_CAPABLE
142 * map vs DSS map in mptcp_incoming_options(), and reconstruct
143 * map info accordingly
144 */
145 mp_opt->mpc_map = 0;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800146 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
147 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
148 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
149 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
150 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
151 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
152
153 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
154 mp_opt->data_fin, mp_opt->dsn64,
155 mp_opt->use_map, mp_opt->ack64,
156 mp_opt->use_ack);
157
158 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
159
160 if (mp_opt->use_ack) {
161 if (mp_opt->ack64)
162 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
163 else
164 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
165 }
166
167 if (mp_opt->use_map) {
168 if (mp_opt->dsn64)
169 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
170 else
171 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
172 }
173
174 /* RFC 6824, Section 3.3:
175 * If a checksum is present, but its use had
176 * not been negotiated in the MP_CAPABLE handshake,
177 * the checksum field MUST be ignored.
178 */
179 if (opsize != expected_opsize &&
180 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
181 break;
182
Peter Krystadeda7acd2020-01-21 16:56:16 -0800183 mp_opt->dss = 1;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800184
185 if (mp_opt->use_ack) {
186 if (mp_opt->ack64) {
187 mp_opt->data_ack = get_unaligned_be64(ptr);
188 ptr += 8;
189 } else {
190 mp_opt->data_ack = get_unaligned_be32(ptr);
191 ptr += 4;
192 }
193
194 pr_debug("data_ack=%llu", mp_opt->data_ack);
195 }
196
197 if (mp_opt->use_map) {
198 if (mp_opt->dsn64) {
199 mp_opt->data_seq = get_unaligned_be64(ptr);
200 ptr += 8;
201 } else {
202 mp_opt->data_seq = get_unaligned_be32(ptr);
203 ptr += 4;
204 }
205
206 mp_opt->subflow_seq = get_unaligned_be32(ptr);
207 ptr += 4;
208
209 mp_opt->data_len = get_unaligned_be16(ptr);
210 ptr += 2;
211
212 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
213 mp_opt->data_seq, mp_opt->subflow_seq,
214 mp_opt->data_len);
215 }
216
Peter Krystadeda7acd2020-01-21 16:56:16 -0800217 break;
218
Peter Krystad3df523a2020-03-27 14:48:37 -0700219 case MPTCPOPT_ADD_ADDR:
220 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
221 if (!mp_opt->echo) {
222 if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
223 opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700224 mp_opt->addr.family = AF_INET;
Peter Krystad3df523a2020-03-27 14:48:37 -0700225#if IS_ENABLED(CONFIG_MPTCP_IPV6)
226 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
227 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700228 mp_opt->addr.family = AF_INET6;
Peter Krystad3df523a2020-03-27 14:48:37 -0700229#endif
230 else
231 break;
232 } else {
233 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
234 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700235 mp_opt->addr.family = AF_INET;
Peter Krystad3df523a2020-03-27 14:48:37 -0700236#if IS_ENABLED(CONFIG_MPTCP_IPV6)
237 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
238 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700239 mp_opt->addr.family = AF_INET6;
Peter Krystad3df523a2020-03-27 14:48:37 -0700240#endif
241 else
242 break;
243 }
244
245 mp_opt->add_addr = 1;
Geliang Tangf7dafee2021-04-06 17:16:00 -0700246 mp_opt->addr.id = *ptr++;
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700247 if (mp_opt->addr.family == AF_INET) {
Geliang Tangf7dafee2021-04-06 17:16:00 -0700248 memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
Peter Krystad3df523a2020-03-27 14:48:37 -0700249 ptr += 4;
250 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
251 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
Geliang Tangf7dafee2021-04-06 17:16:00 -0700252 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
Peter Krystad3df523a2020-03-27 14:48:37 -0700253 ptr += 2;
254 }
255 }
256#if IS_ENABLED(CONFIG_MPTCP_IPV6)
257 else {
Geliang Tangf7dafee2021-04-06 17:16:00 -0700258 memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
Peter Krystad3df523a2020-03-27 14:48:37 -0700259 ptr += 16;
260 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
261 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
Geliang Tangf7dafee2021-04-06 17:16:00 -0700262 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
Peter Krystad3df523a2020-03-27 14:48:37 -0700263 ptr += 2;
264 }
265 }
266#endif
267 if (!mp_opt->echo) {
268 mp_opt->ahmac = get_unaligned_be64(ptr);
269 ptr += 8;
270 }
Geliang Tang90a4aea2020-12-09 15:51:25 -0800271 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
Geliang Tang1b1a6ef2021-04-06 17:16:01 -0700272 (mp_opt->addr.family == AF_INET6) ? "6" : "",
Geliang Tangf7dafee2021-04-06 17:16:00 -0700273 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
Peter Krystad3df523a2020-03-27 14:48:37 -0700274 break;
275
276 case MPTCPOPT_RM_ADDR:
Geliang Tang5c4a8242021-03-12 17:16:13 -0800277 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
278 opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
Peter Krystad3df523a2020-03-27 14:48:37 -0700279 break;
280
Geliang Tang8e60eed2020-06-08 18:47:54 +0800281 ptr++;
282
Peter Krystad3df523a2020-03-27 14:48:37 -0700283 mp_opt->rm_addr = 1;
Geliang Tang5c4a8242021-03-12 17:16:13 -0800284 mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
285 for (i = 0; i < mp_opt->rm_list.nr; i++)
286 mp_opt->rm_list.ids[i] = *ptr++;
287 pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
Peter Krystad3df523a2020-03-27 14:48:37 -0700288 break;
289
Geliang Tang40453a52021-01-08 16:47:58 -0800290 case MPTCPOPT_MP_PRIO:
291 if (opsize != TCPOLEN_MPTCP_PRIO)
292 break;
293
294 mp_opt->mp_prio = 1;
295 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
296 pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
297 break;
298
Florian Westphal50c504a2020-12-10 14:25:04 -0800299 case MPTCPOPT_MP_FASTCLOSE:
300 if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
301 break;
302
303 ptr += 2;
304 mp_opt->rcvr_key = get_unaligned_be64(ptr);
305 ptr += 8;
306 mp_opt->fastclose = 1;
307 break;
308
Florian Westphaldc87efd2021-04-01 16:19:44 -0700309 case MPTCPOPT_RST:
310 if (opsize != TCPOLEN_MPTCP_RST)
311 break;
312
313 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
314 break;
315 mp_opt->reset = 1;
316 flags = *ptr++;
317 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
318 mp_opt->reset_reason = *ptr;
319 break;
320
Peter Krystadeda7acd2020-01-21 16:56:16 -0800321 default:
322 break;
323 }
324}
325
Peter Krystadcec37a62020-01-21 16:56:18 -0800326void mptcp_get_options(const struct sk_buff *skb,
Paolo Abenicfde1412020-04-30 15:01:52 +0200327 struct mptcp_options_received *mp_opt)
Peter Krystadcec37a62020-01-21 16:56:18 -0800328{
Peter Krystadcec37a62020-01-21 16:56:18 -0800329 const struct tcphdr *th = tcp_hdr(skb);
Paolo Abenicfde1412020-04-30 15:01:52 +0200330 const unsigned char *ptr;
331 int length;
Peter Krystadcec37a62020-01-21 16:56:18 -0800332
Paolo Abenicfde1412020-04-30 15:01:52 +0200333 /* initialize option status */
334 mp_opt->mp_capable = 0;
335 mp_opt->mp_join = 0;
336 mp_opt->add_addr = 0;
Geliang Tangfe2d9b12020-10-19 18:23:15 +0800337 mp_opt->ahmac = 0;
Florian Westphal50c504a2020-12-10 14:25:04 -0800338 mp_opt->fastclose = 0;
Geliang Tangf7dafee2021-04-06 17:16:00 -0700339 mp_opt->addr.port = 0;
Paolo Abenicfde1412020-04-30 15:01:52 +0200340 mp_opt->rm_addr = 0;
341 mp_opt->dss = 0;
Geliang Tang40453a52021-01-08 16:47:58 -0800342 mp_opt->mp_prio = 0;
Florian Westphaldc87efd2021-04-01 16:19:44 -0700343 mp_opt->reset = 0;
Paolo Abenicfde1412020-04-30 15:01:52 +0200344
345 length = (th->doff * 4) - sizeof(struct tcphdr);
Peter Krystadcec37a62020-01-21 16:56:18 -0800346 ptr = (const unsigned char *)(th + 1);
347
348 while (length > 0) {
349 int opcode = *ptr++;
350 int opsize;
351
352 switch (opcode) {
353 case TCPOPT_EOL:
354 return;
355 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
356 length--;
357 continue;
358 default:
359 opsize = *ptr++;
360 if (opsize < 2) /* "silly options" */
361 return;
362 if (opsize > length)
363 return; /* don't parse partial options */
364 if (opcode == TCPOPT_MPTCP)
Paolo Abenicfde1412020-04-30 15:01:52 +0200365 mptcp_parse_option(skb, ptr, opsize, mp_opt);
Peter Krystadcec37a62020-01-21 16:56:18 -0800366 ptr += opsize - 2;
367 length -= opsize;
368 }
369 }
370}
371
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800372bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
373 unsigned int *size, struct mptcp_out_options *opts)
Peter Krystadcec37a62020-01-21 16:56:18 -0800374{
375 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
376
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800377 /* we will use snd_isn to detect first pkt [re]transmission
378 * in mptcp_established_options_mp()
379 */
380 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
Peter Krystadcec37a62020-01-21 16:56:18 -0800381 if (subflow->request_mptcp) {
Peter Krystadcec37a62020-01-21 16:56:18 -0800382 opts->suboptions = OPTION_MPTCP_MPC_SYN;
Geliang Tang06fe1712021-06-17 16:46:09 -0700383 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
Peter Krystadcec37a62020-01-21 16:56:18 -0800384 *size = TCPOLEN_MPTCP_MPC_SYN;
385 return true;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700386 } else if (subflow->request_join) {
387 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
388 subflow->local_nonce);
389 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
390 opts->join_id = subflow->local_id;
391 opts->token = subflow->remote_token;
392 opts->nonce = subflow->local_nonce;
393 opts->backup = subflow->request_bkup;
394 *size = TCPOLEN_MPTCP_MPJ_SYN;
395 return true;
Peter Krystadcec37a62020-01-21 16:56:18 -0800396 }
397 return false;
398}
399
Peter Krystadec3edaa2020-03-27 14:48:40 -0700400/* MP_JOIN client subflow must wait for 4th ack before sending any data:
401 * TCP can't schedule delack timer before the subflow is fully established.
402 * MPTCP uses the delack timer to do 3rd ack retransmissions
403 */
404static void schedule_3rdack_retransmission(struct sock *sk)
405{
406 struct inet_connection_sock *icsk = inet_csk(sk);
407 struct tcp_sock *tp = tcp_sk(sk);
408 unsigned long timeout;
409
410 /* reschedule with a timeout above RTT, as we must look only for drop */
411 if (tp->srtt_us)
412 timeout = tp->srtt_us << 1;
413 else
414 timeout = TCP_TIMEOUT_INIT;
415
416 WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
417 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
418 icsk->icsk_ack.timeout = timeout;
419 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
420}
421
422static void clear_3rdack_retransmission(struct sock *sk)
423{
424 struct inet_connection_sock *icsk = inet_csk(sk);
425
426 sk_stop_timer(sk, &icsk->icsk_delack_timer);
427 icsk->icsk_ack.timeout = 0;
428 icsk->icsk_ack.ato = 0;
429 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
430}
431
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800432static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
Paolo Abenid87903b2021-02-19 18:35:38 +0100433 bool snd_data_fin_enable,
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800434 unsigned int *size,
Mat Martineau6d0060f2020-01-21 16:56:23 -0800435 unsigned int remaining,
436 struct mptcp_out_options *opts)
Peter Krystadcec37a62020-01-21 16:56:18 -0800437{
438 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
Geliang Tang06fe1712021-06-17 16:46:09 -0700439 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800440 struct mptcp_ext *mpext;
441 unsigned int data_len;
Geliang Tangc94b1f92021-06-17 16:46:10 -0700442 u8 len;
Peter Krystadcec37a62020-01-21 16:56:18 -0800443
Peter Krystadec3edaa2020-03-27 14:48:40 -0700444 /* When skb is not available, we better over-estimate the emitted
445 * options len. A full DSS option (28 bytes) is longer than
446 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
447 * tell the caller to defer the estimate to
448 * mptcp_established_options_dss(), which will reserve enough space.
449 */
450 if (!skb)
451 return false;
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800452
Paolo Abenid87903b2021-02-19 18:35:38 +0100453 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
454 if (subflow->fully_established || snd_data_fin_enable ||
455 subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
456 sk->sk_state != TCP_ESTABLISHED)
Peter Krystadec3edaa2020-03-27 14:48:40 -0700457 return false;
458
459 if (subflow->mp_capable) {
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800460 mpext = mptcp_get_ext(skb);
461 data_len = mpext ? mpext->data_len : 0;
462
463 /* we will check ext_copy.data_len in mptcp_write_options() to
464 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
465 * TCPOLEN_MPTCP_MPC_ACK
466 */
467 opts->ext_copy.data_len = data_len;
Peter Krystadcec37a62020-01-21 16:56:18 -0800468 opts->suboptions = OPTION_MPTCP_MPC_ACK;
469 opts->sndr_key = subflow->local_key;
470 opts->rcvr_key = subflow->remote_key;
Geliang Tang06fe1712021-06-17 16:46:09 -0700471 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800472
473 /* Section 3.1.
474 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
475 * packets that start the first subflow of an MPTCP connection,
476 * as well as the first packet that carries data
477 */
Geliang Tangc94b1f92021-06-17 16:46:10 -0700478 if (data_len > 0) {
479 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
480 if (opts->csum_reqd) {
Geliang Tangc5b39e22021-06-17 16:46:11 -0700481 /* we need to propagate more info to csum the pseudo hdr */
482 opts->ext_copy.data_seq = mpext->data_seq;
483 opts->ext_copy.subflow_seq = mpext->subflow_seq;
Geliang Tangc94b1f92021-06-17 16:46:10 -0700484 opts->ext_copy.csum = mpext->csum;
485 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
486 }
487 *size = ALIGN(len, 4);
488 } else {
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800489 *size = TCPOLEN_MPTCP_MPC_ACK;
Geliang Tangc94b1f92021-06-17 16:46:10 -0700490 }
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800491
492 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
493 subflow, subflow->local_key, subflow->remote_key,
494 data_len);
495
Peter Krystadcec37a62020-01-21 16:56:18 -0800496 return true;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700497 } else if (subflow->mp_join) {
498 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
499 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
500 *size = TCPOLEN_MPTCP_MPJ_ACK;
501 pr_debug("subflow=%p", subflow);
502
503 schedule_3rdack_retransmission(sk);
504 return true;
Peter Krystadcec37a62020-01-21 16:56:18 -0800505 }
506 return false;
507}
508
Mat Martineau6d0060f2020-01-21 16:56:23 -0800509static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
Paolo Abeni9c29e362020-07-03 18:06:04 +0200510 struct sk_buff *skb, struct mptcp_ext *ext)
Mat Martineau6d0060f2020-01-21 16:56:23 -0800511{
Paolo Abeni017512a2020-10-05 12:01:06 +0200512 /* The write_seq value has already been incremented, so the actual
513 * sequence number for the DATA_FIN is one less.
514 */
515 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
Mat Martineau7279da62020-07-28 15:12:02 -0700516
Paolo Abeni9c29e362020-07-03 18:06:04 +0200517 if (!ext->use_map || !skb->len) {
Mat Martineau6d0060f2020-01-21 16:56:23 -0800518 /* RFC6824 requires a DSS mapping with specific values
519 * if DATA_FIN is set but no data payload is mapped
520 */
Mat Martineau6d37a0b2020-02-28 15:47:41 -0800521 ext->data_fin = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800522 ext->use_map = 1;
523 ext->dsn64 = 1;
Paolo Abeni017512a2020-10-05 12:01:06 +0200524 ext->data_seq = data_fin_tx_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800525 ext->subflow_seq = 0;
526 ext->data_len = 1;
Mat Martineau7279da62020-07-28 15:12:02 -0700527 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
Mat Martineau6d37a0b2020-02-28 15:47:41 -0800528 /* If there's an existing DSS mapping and it is the
529 * final mapping, DATA_FIN consumes 1 additional byte of
530 * mapping space.
Mat Martineau6d0060f2020-01-21 16:56:23 -0800531 */
Mat Martineau6d37a0b2020-02-28 15:47:41 -0800532 ext->data_fin = 1;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800533 ext->data_len++;
534 }
535}
536
537static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
Paolo Abenid87903b2021-02-19 18:35:38 +0100538 bool snd_data_fin_enable,
Mat Martineau6d0060f2020-01-21 16:56:23 -0800539 unsigned int *size,
540 unsigned int remaining,
541 struct mptcp_out_options *opts)
542{
543 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
Mat Martineau7279da62020-07-28 15:12:02 -0700544 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800545 unsigned int dss_size = 0;
546 struct mptcp_ext *mpext;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800547 unsigned int ack_size;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800548 bool ret = false;
Paolo Abenid87903b2021-02-19 18:35:38 +0100549 u64 ack_seq;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800550
Geliang Tangc5b39e22021-06-17 16:46:11 -0700551 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
Mat Martineau0bac9662020-07-28 15:11:59 -0700552 mpext = skb ? mptcp_get_ext(skb) : NULL;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800553
Mat Martineau7279da62020-07-28 15:12:02 -0700554 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
Geliang Tangc5b39e22021-06-17 16:46:11 -0700555 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800556
Geliang Tangc5b39e22021-06-17 16:46:11 -0700557 if (mpext) {
558 if (opts->csum_reqd)
559 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
560
561 opts->ext_copy = *mpext;
562 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800563
564 remaining -= map_size;
565 dss_size = map_size;
Mat Martineau7279da62020-07-28 15:12:02 -0700566 if (skb && snd_data_fin_enable)
Paolo Abeni9c29e362020-07-03 18:06:04 +0200567 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
Christoph Paaschd22f4982020-01-21 16:56:32 -0800568 ret = true;
569 }
570
Paolo Abeni2398e392020-03-04 16:51:07 +0100571 /* passive sockets msk will set the 'can_ack' after accept(), even
572 * if the first subflow may have the already the remote key handy
573 */
Christoph Paaschd22f4982020-01-21 16:56:32 -0800574 opts->ext_copy.use_ack = 0;
Paolo Abenidc093db2020-03-13 16:52:42 +0100575 if (!READ_ONCE(msk->can_ack)) {
Christoph Paaschd22f4982020-01-21 16:56:32 -0800576 *size = ALIGN(dss_size, 4);
577 return ret;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800578 }
579
Paolo Abenie3859602021-02-11 15:30:41 -0800580 ack_seq = READ_ONCE(msk->ack_seq);
Davide Caratti37198e92020-10-06 18:26:17 +0200581 if (READ_ONCE(msk->use_64bit_ack)) {
Christoph Paascha0c1d0e2020-05-14 08:53:03 -0700582 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
Paolo Abenie3859602021-02-11 15:30:41 -0800583 opts->ext_copy.data_ack = ack_seq;
Christoph Paascha0c1d0e2020-05-14 08:53:03 -0700584 opts->ext_copy.ack64 = 1;
585 } else {
586 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
Paolo Abenie3859602021-02-11 15:30:41 -0800587 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
Christoph Paascha0c1d0e2020-05-14 08:53:03 -0700588 opts->ext_copy.ack64 = 0;
589 }
590 opts->ext_copy.use_ack = 1;
Paolo Abeniea4ca582020-11-19 11:46:03 -0800591 WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
Mat Martineau6d0060f2020-01-21 16:56:23 -0800592
593 /* Add kind/length/subtype/flag overhead if mapping is not populated */
594 if (dss_size == 0)
595 ack_size += TCPOLEN_MPTCP_DSS_BASE;
596
597 dss_size += ack_size;
598
Mat Martineau6d0060f2020-01-21 16:56:23 -0800599 *size = ALIGN(dss_size, 4);
600 return true;
601}
602
Geliang Tang761c1242021-04-06 17:16:02 -0700603static u64 add_addr_generate_hmac(u64 key1, u64 key2,
604 struct mptcp_addr_info *addr)
Peter Krystad3df523a2020-03-27 14:48:37 -0700605{
Geliang Tang761c1242021-04-06 17:16:02 -0700606 u16 port = ntohs(addr->port);
Todd Malsbarybd697222020-05-21 19:10:49 -0700607 u8 hmac[SHA256_DIGEST_SIZE];
Peter Krystad3df523a2020-03-27 14:48:37 -0700608 u8 msg[19];
Geliang Tang761c1242021-04-06 17:16:02 -0700609 int i = 0;
Peter Krystad3df523a2020-03-27 14:48:37 -0700610
Geliang Tang761c1242021-04-06 17:16:02 -0700611 msg[i++] = addr->id;
612 if (addr->family == AF_INET) {
613 memcpy(&msg[i], &addr->addr.s_addr, 4);
614 i += 4;
615 }
616#if IS_ENABLED(CONFIG_MPTCP_IPV6)
617 else if (addr->family == AF_INET6) {
618 memcpy(&msg[i], &addr->addr6.s6_addr, 16);
619 i += 16;
620 }
621#endif
622 msg[i++] = port >> 8;
623 msg[i++] = port & 0xFF;
Peter Krystad3df523a2020-03-27 14:48:37 -0700624
Geliang Tang761c1242021-04-06 17:16:02 -0700625 mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
Peter Krystad3df523a2020-03-27 14:48:37 -0700626
Todd Malsbarybd697222020-05-21 19:10:49 -0700627 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
Peter Krystad3df523a2020-03-27 14:48:37 -0700628}
Peter Krystad3df523a2020-03-27 14:48:37 -0700629
Geliang Tang84dfe362020-11-19 11:46:00 -0800630static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
Geliang Tangf643b802020-09-24 08:29:47 +0800631 unsigned int *size,
632 unsigned int remaining,
633 struct mptcp_out_options *opts)
Peter Krystad3df523a2020-03-27 14:48:37 -0700634{
635 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
636 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Geliang Tang84dfe362020-11-19 11:46:00 -0800637 bool drop_other_suboptions = false;
638 unsigned int opt_size = *size;
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800639 bool echo;
Geliang Tang4a2777a82020-12-09 15:51:22 -0800640 bool port;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700641 int len;
Peter Krystad3df523a2020-03-27 14:48:37 -0700642
Geliang Tangfbe0f872020-12-09 15:51:23 -0800643 if ((mptcp_pm_should_add_signal_ipv6(msk) ||
Davide Caratti07f82522021-04-06 17:16:04 -0700644 mptcp_pm_should_add_signal_port(msk) ||
645 mptcp_pm_should_add_signal_echo(msk)) &&
Geliang Tang84dfe362020-11-19 11:46:00 -0800646 skb && skb_is_tcp_pure_ack(skb)) {
647 pr_debug("drop other suboptions");
648 opts->suboptions = 0;
Geliang Tang3ae32c02020-12-15 17:56:51 +0800649 opts->ext_copy.use_ack = 0;
650 opts->ext_copy.use_map = 0;
Geliang Tang84dfe362020-11-19 11:46:00 -0800651 remaining += opt_size;
652 drop_other_suboptions = true;
653 }
654
Geliang Tangf643b802020-09-24 08:29:47 +0800655 if (!mptcp_pm_should_add_signal(msk) ||
Geliang Tang30f60ba2021-04-06 17:15:58 -0700656 !(mptcp_pm_add_addr_signal(msk, remaining, &opts->addr, &echo, &port)))
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700657 return false;
Peter Krystad3df523a2020-03-27 14:48:37 -0700658
Geliang Tang30f60ba2021-04-06 17:15:58 -0700659 len = mptcp_add_addr_len(opts->addr.family, echo, port);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700660 if (remaining < len)
661 return false;
662
663 *size = len;
Geliang Tang84dfe362020-11-19 11:46:00 -0800664 if (drop_other_suboptions)
665 *size -= opt_size;
Geliang Tangfef6b7e2021-04-06 17:15:59 -0700666 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
Geliang Tang761c1242021-04-06 17:16:02 -0700667 if (!echo) {
668 opts->ahmac = add_addr_generate_hmac(msk->local_key,
669 msk->remote_key,
670 &opts->addr);
Peter Krystad3df523a2020-03-27 14:48:37 -0700671 }
Geliang Tang4a2777a82020-12-09 15:51:22 -0800672 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
Geliang Tang30f60ba2021-04-06 17:15:58 -0700673 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
Peter Krystad3df523a2020-03-27 14:48:37 -0700674
675 return true;
676}
677
Geliang Tang5cb104a2020-09-24 08:29:48 +0800678static bool mptcp_established_options_rm_addr(struct sock *sk,
679 unsigned int *size,
680 unsigned int remaining,
681 struct mptcp_out_options *opts)
682{
683 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
684 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Geliang Tang6445e172021-03-12 17:16:11 -0800685 struct mptcp_rm_list rm_list;
686 int i, len;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800687
688 if (!mptcp_pm_should_rm_signal(msk) ||
Geliang Tang6445e172021-03-12 17:16:11 -0800689 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
Geliang Tang5cb104a2020-09-24 08:29:48 +0800690 return false;
691
Geliang Tang6445e172021-03-12 17:16:11 -0800692 len = mptcp_rm_addr_len(&rm_list);
693 if (len < 0)
694 return false;
695 if (remaining < len)
Geliang Tang5cb104a2020-09-24 08:29:48 +0800696 return false;
697
Geliang Tang6445e172021-03-12 17:16:11 -0800698 *size = len;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800699 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
Geliang Tang6445e172021-03-12 17:16:11 -0800700 opts->rm_list = rm_list;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800701
Geliang Tang6445e172021-03-12 17:16:11 -0800702 for (i = 0; i < opts->rm_list.nr; i++)
703 pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
Geliang Tang5cb104a2020-09-24 08:29:48 +0800704
705 return true;
706}
707
Geliang Tang06706542021-01-08 16:47:57 -0800708static bool mptcp_established_options_mp_prio(struct sock *sk,
709 unsigned int *size,
710 unsigned int remaining,
711 struct mptcp_out_options *opts)
712{
713 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
714
715 if (!subflow->send_mp_prio)
716 return false;
717
Davide Carattiec99a472021-02-01 14:05:26 +0100718 /* account for the trailing 'nop' option */
719 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
Geliang Tang06706542021-01-08 16:47:57 -0800720 return false;
721
Davide Carattiec99a472021-02-01 14:05:26 +0100722 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
Geliang Tang06706542021-01-08 16:47:57 -0800723 opts->suboptions |= OPTION_MPTCP_PRIO;
724 opts->backup = subflow->request_bkup;
725
726 pr_debug("prio=%d", opts->backup);
727
728 return true;
729}
730
Florian Westphaldc87efd2021-04-01 16:19:44 -0700731static noinline void mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
732 unsigned int *size,
733 unsigned int remaining,
734 struct mptcp_out_options *opts)
735{
736 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
737
738 if (remaining < TCPOLEN_MPTCP_RST)
739 return;
740
741 *size = TCPOLEN_MPTCP_RST;
742 opts->suboptions |= OPTION_MPTCP_RST;
743 opts->reset_transient = subflow->reset_transient;
744 opts->reset_reason = subflow->reset_reason;
745}
746
Mat Martineau6d0060f2020-01-21 16:56:23 -0800747bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
748 unsigned int *size, unsigned int remaining,
749 struct mptcp_out_options *opts)
750{
Paolo Abenid87903b2021-02-19 18:35:38 +0100751 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
752 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Mat Martineau6d0060f2020-01-21 16:56:23 -0800753 unsigned int opt_size = 0;
Paolo Abenid87903b2021-02-19 18:35:38 +0100754 bool snd_data_fin;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800755 bool ret = false;
756
Peter Krystad3df523a2020-03-27 14:48:37 -0700757 opts->suboptions = 0;
758
Paolo Abenid87903b2021-02-19 18:35:38 +0100759 if (unlikely(__mptcp_check_fallback(msk)))
Davide Carattie1ff9e82020-06-29 22:26:20 +0200760 return false;
761
Florian Westphaldc87efd2021-04-01 16:19:44 -0700762 if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
763 mptcp_established_options_rst(sk, skb, size, remaining, opts);
764 return true;
765 }
Paolo Abenid5824842020-10-09 19:00:00 +0200766
Paolo Abenid87903b2021-02-19 18:35:38 +0100767 snd_data_fin = mptcp_data_fin_enabled(msk);
768 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
Mat Martineau6d0060f2020-01-21 16:56:23 -0800769 ret = true;
Paolo Abenid87903b2021-02-19 18:35:38 +0100770 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts))
Mat Martineau6d0060f2020-01-21 16:56:23 -0800771 ret = true;
772
773 /* we reserved enough space for the above options, and exceeding the
774 * TCP option space would be fatal
775 */
776 if (WARN_ON_ONCE(opt_size > remaining))
777 return false;
778
779 *size += opt_size;
780 remaining -= opt_size;
Geliang Tang84dfe362020-11-19 11:46:00 -0800781 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
Peter Krystad3df523a2020-03-27 14:48:37 -0700782 *size += opt_size;
783 remaining -= opt_size;
784 ret = true;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800785 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
786 *size += opt_size;
787 remaining -= opt_size;
788 ret = true;
Peter Krystad3df523a2020-03-27 14:48:37 -0700789 }
Mat Martineau6d0060f2020-01-21 16:56:23 -0800790
Geliang Tang06706542021-01-08 16:47:57 -0800791 if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
792 *size += opt_size;
793 remaining -= opt_size;
794 ret = true;
795 }
796
Mat Martineau6d0060f2020-01-21 16:56:23 -0800797 return ret;
798}
799
Peter Krystadcec37a62020-01-21 16:56:18 -0800800bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
801 struct mptcp_out_options *opts)
802{
803 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
804
805 if (subflow_req->mp_capable) {
806 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
807 opts->sndr_key = subflow_req->local_key;
Geliang Tang06fe1712021-06-17 16:46:09 -0700808 opts->csum_reqd = subflow_req->csum_reqd;
Peter Krystadcec37a62020-01-21 16:56:18 -0800809 *size = TCPOLEN_MPTCP_MPC_SYNACK;
810 pr_debug("subflow_req=%p, local_key=%llu",
811 subflow_req, subflow_req->local_key);
812 return true;
Peter Krystadf2962342020-03-27 14:48:39 -0700813 } else if (subflow_req->mp_join) {
814 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
815 opts->backup = subflow_req->backup;
816 opts->join_id = subflow_req->local_id;
817 opts->thmac = subflow_req->thmac;
818 opts->nonce = subflow_req->local_nonce;
819 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
820 subflow_req, opts->backup, opts->join_id,
821 opts->thmac, opts->nonce);
822 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
823 return true;
Peter Krystadcec37a62020-01-21 16:56:18 -0800824 }
825 return false;
826}
827
Paolo Abenid5824842020-10-09 19:00:00 +0200828static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
Peter Krystadf2962342020-03-27 14:48:39 -0700829 struct mptcp_subflow_context *subflow,
Paolo Abeni0be534f2020-03-19 11:06:30 +0100830 struct sk_buff *skb,
831 struct mptcp_options_received *mp_opt)
Christoph Paaschd22f4982020-01-21 16:56:32 -0800832{
833 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
Peter Krystadf2962342020-03-27 14:48:39 -0700834 * will make the subflow fully established
Christoph Paaschd22f4982020-01-21 16:56:32 -0800835 */
Peter Krystadf2962342020-03-27 14:48:39 -0700836 if (likely(subflow->fully_established)) {
837 /* on passive sockets, check for 3rd ack retransmission
838 * note that msk is always set by subflow_syn_recv_sock()
839 * for mp_join subflows
840 */
841 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
842 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
843 subflow->mp_join && mp_opt->mp_join &&
844 READ_ONCE(msk->pm.server_side))
Paolo Abenid5824842020-10-09 19:00:00 +0200845 tcp_send_ack(ssk);
Peter Krystadf2962342020-03-27 14:48:39 -0700846 goto fully_established;
847 }
Christoph Paaschd22f4982020-01-21 16:56:32 -0800848
Paolo Abenid5824842020-10-09 19:00:00 +0200849 /* we must process OoO packets before the first subflow is fully
850 * established. OoO packets are instead a protocol violation
851 * for MP_JOIN subflows as the peer must not send any data
852 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
Peter Krystadf2962342020-03-27 14:48:39 -0700853 */
Paolo Abenid5824842020-10-09 19:00:00 +0200854 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
855 if (subflow->mp_join)
856 goto reset;
Peter Krystadf2962342020-03-27 14:48:39 -0700857 return subflow->mp_capable;
Paolo Abenid5824842020-10-09 19:00:00 +0200858 }
Peter Krystadf2962342020-03-27 14:48:39 -0700859
Paolo Abeni5a91e322020-04-30 15:01:54 +0200860 if (mp_opt->dss && mp_opt->use_ack) {
Peter Krystadf2962342020-03-27 14:48:39 -0700861 /* subflows are fully established as soon as we get any
862 * additional ack.
863 */
Paolo Abeni0be534f2020-03-19 11:06:30 +0100864 subflow->fully_established = 1;
Paolo Abenib93df082020-07-23 13:02:32 +0200865 WRITE_ONCE(msk->fully_established, true);
Peter Krystadf2962342020-03-27 14:48:39 -0700866 goto fully_established;
867 }
Christoph Paaschd22f4982020-01-21 16:56:32 -0800868
Geliang Tang84dfe362020-11-19 11:46:00 -0800869 if (mp_opt->add_addr) {
870 WRITE_ONCE(msk->fully_established, true);
871 return true;
872 }
873
Christoph Paaschd22f4982020-01-21 16:56:32 -0800874 /* If the first established packet does not contain MP_CAPABLE + data
Paolo Abenid5824842020-10-09 19:00:00 +0200875 * then fallback to TCP. Fallback scenarios requires a reset for
876 * MP_JOIN subflows.
Christoph Paaschd22f4982020-01-21 16:56:32 -0800877 */
878 if (!mp_opt->mp_capable) {
Paolo Abenid5824842020-10-09 19:00:00 +0200879 if (subflow->mp_join)
880 goto reset;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800881 subflow->mp_capable = 0;
Davide Carattie1ff9e82020-06-29 22:26:20 +0200882 pr_fallback(msk);
883 __mptcp_do_fallback(msk);
Christoph Paaschd22f4982020-01-21 16:56:32 -0800884 return false;
885 }
Peter Krystadf2962342020-03-27 14:48:39 -0700886
Paolo Abenid6085fe2020-04-30 15:01:53 +0200887 if (unlikely(!READ_ONCE(msk->pm.server_side)))
888 pr_warn_once("bogus mpc option on established client sk");
Paolo Abenib93df082020-07-23 13:02:32 +0200889 mptcp_subflow_fully_established(subflow, mp_opt);
Peter Krystadf2962342020-03-27 14:48:39 -0700890
891fully_established:
Paolo Abeni5b950ff2020-12-09 12:03:29 +0100892 /* if the subflow is not already linked into the conn_list, we can't
893 * notify the PM: this subflow is still on the listener queue
894 * and the PM possibly acquiring the subflow lock could race with
895 * the listener close
896 */
897 if (likely(subflow->pm_notified) || list_empty(&subflow->node))
Peter Krystadf2962342020-03-27 14:48:39 -0700898 return true;
899
900 subflow->pm_notified = 1;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700901 if (subflow->mp_join) {
Paolo Abenid5824842020-10-09 19:00:00 +0200902 clear_3rdack_retransmission(ssk);
Geliang Tang625352002021-03-26 11:26:33 -0700903 mptcp_pm_subflow_established(msk);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700904 } else {
Florian Westphal6c714f12021-02-12 15:59:58 -0800905 mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700906 }
Christoph Paaschd22f4982020-01-21 16:56:32 -0800907 return true;
Paolo Abenid5824842020-10-09 19:00:00 +0200908
909reset:
910 mptcp_subflow_reset(ssk);
911 return false;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800912}
913
Paolo Abenicc9d2562020-03-27 14:48:42 -0700914static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
915{
916 u32 old_ack32, cur_ack32;
917
918 if (use_64bit)
919 return cur_ack;
920
921 old_ack32 = (u32)old_ack;
922 cur_ack32 = (u32)cur_ack;
923 cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
924 if (unlikely(before(cur_ack32, old_ack32)))
925 return cur_ack + (1LL << 32);
926 return cur_ack;
927}
928
Florian Westphal6f8a6122020-11-16 10:48:13 +0100929static void ack_update_msk(struct mptcp_sock *msk,
Paolo Abeni6e628cd2020-11-27 11:10:27 +0100930 struct sock *ssk,
Florian Westphal6f8a6122020-11-16 10:48:13 +0100931 struct mptcp_options_received *mp_opt)
Paolo Abenicc9d2562020-03-27 14:48:42 -0700932{
Paolo Abeni7439d682020-11-27 11:10:26 +0100933 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
Florian Westphal6f8a6122020-11-16 10:48:13 +0100934 struct sock *sk = (struct sock *)msk;
Paolo Abeni7439d682020-11-27 11:10:26 +0100935 u64 old_snd_una;
936
937 mptcp_data_lock(sk);
Paolo Abenicc9d2562020-03-27 14:48:42 -0700938
939 /* avoid ack expansion on update conflict, to reduce the risk of
940 * wrongly expanding to a future ack sequence number, which is way
941 * more dangerous than missing an ack
942 */
Paolo Abeni7439d682020-11-27 11:10:26 +0100943 old_snd_una = msk->snd_una;
Paolo Abenicc9d2562020-03-27 14:48:42 -0700944 new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
945
946 /* ACK for data not even sent yet? Ignore. */
Paolo Abenieaa2ffa2020-11-16 10:48:08 +0100947 if (after64(new_snd_una, snd_nxt))
Paolo Abenicc9d2562020-03-27 14:48:42 -0700948 new_snd_una = old_snd_una;
949
Florian Westphal6f8a6122020-11-16 10:48:13 +0100950 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
951
Paolo Abeni219d0492020-12-16 12:48:34 +0100952 if (after64(new_wnd_end, msk->wnd_end))
Paolo Abeni7439d682020-11-27 11:10:26 +0100953 msk->wnd_end = new_wnd_end;
Paolo Abeni219d0492020-12-16 12:48:34 +0100954
955 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
Paolo Abenid09d8182021-02-11 15:30:42 -0800956 if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
Paolo Abeni219d0492020-12-16 12:48:34 +0100957 __mptcp_check_push(sk, ssk);
Florian Westphal6f8a6122020-11-16 10:48:13 +0100958
Paolo Abeni7439d682020-11-27 11:10:26 +0100959 if (after64(new_snd_una, old_snd_una)) {
960 msk->snd_una = new_snd_una;
961 __mptcp_data_acked(sk);
Paolo Abenicc9d2562020-03-27 14:48:42 -0700962 }
Paolo Abeni7439d682020-11-27 11:10:26 +0100963 mptcp_data_unlock(sk);
Geliang Tanged66bfb2021-04-16 15:38:06 -0700964
965 trace_ack_update_msk(mp_opt->data_ack,
966 old_snd_una, new_snd_una,
967 new_wnd_end, msk->wnd_end);
Paolo Abenicc9d2562020-03-27 14:48:42 -0700968}
969
Mat Martineau1a49b2c2020-09-29 15:08:20 -0700970bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
Mat Martineau3721b9b2020-07-28 15:12:03 -0700971{
972 /* Skip if DATA_FIN was already received.
973 * If updating simultaneously with the recvmsg loop, values
974 * should match. If they mismatch, the peer is misbehaving and
975 * we will prefer the most recent information.
976 */
Paolo Abeni781bf132021-04-01 16:19:43 -0700977 if (READ_ONCE(msk->rcv_data_fin))
Mat Martineau3721b9b2020-07-28 15:12:03 -0700978 return false;
979
Mat Martineau1a49b2c2020-09-29 15:08:20 -0700980 WRITE_ONCE(msk->rcv_data_fin_seq,
981 expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
Mat Martineau3721b9b2020-07-28 15:12:03 -0700982 WRITE_ONCE(msk->rcv_data_fin, 1);
983
984 return true;
985}
986
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700987static bool add_addr_hmac_valid(struct mptcp_sock *msk,
988 struct mptcp_options_received *mp_opt)
989{
990 u64 hmac = 0;
991
992 if (mp_opt->echo)
993 return true;
994
Geliang Tang761c1242021-04-06 17:16:02 -0700995 hmac = add_addr_generate_hmac(msk->remote_key,
996 msk->local_key,
997 &mp_opt->addr);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700998
999 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1000 msk, (unsigned long long)hmac,
1001 (unsigned long long)mp_opt->ahmac);
1002
1003 return hmac == mp_opt->ahmac;
1004}
1005
Florian Westphal77d0cab2020-09-25 01:23:02 +02001006void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
Mat Martineau648ef4b2020-01-21 16:56:24 -08001007{
Christoph Paaschd22f4982020-01-21 16:56:32 -08001008 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
Peter Krystad1b1c7a02020-03-27 14:48:38 -07001009 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
Paolo Abenicfde1412020-04-30 15:01:52 +02001010 struct mptcp_options_received mp_opt;
Mat Martineau648ef4b2020-01-21 16:56:24 -08001011 struct mptcp_ext *mpext;
1012
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001013 if (__mptcp_check_fallback(msk)) {
1014 /* Keep it simple and unconditionally trigger send data cleanup and
1015 * pending queue spooling. We will need to acquire the data lock
1016 * for more accurate checks, and once the lock is acquired, such
1017 * helpers are cheap.
1018 */
1019 mptcp_data_lock(subflow->conn);
Paolo Abeni219d0492020-12-16 12:48:34 +01001020 if (sk_stream_memory_free(sk))
1021 __mptcp_check_push(subflow->conn, sk);
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001022 __mptcp_data_acked(subflow->conn);
1023 mptcp_data_unlock(subflow->conn);
Davide Carattie1ff9e82020-06-29 22:26:20 +02001024 return;
Paolo Abeni6e628cd2020-11-27 11:10:27 +01001025 }
Davide Carattie1ff9e82020-06-29 22:26:20 +02001026
Paolo Abenicfde1412020-04-30 15:01:52 +02001027 mptcp_get_options(skb, &mp_opt);
1028 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
Christoph Paaschd22f4982020-01-21 16:56:32 -08001029 return;
Mat Martineau648ef4b2020-01-21 16:56:24 -08001030
Florian Westphal50c504a2020-12-10 14:25:04 -08001031 if (mp_opt.fastclose &&
1032 msk->local_key == mp_opt.rcvr_key) {
1033 WRITE_ONCE(msk->rcv_fastclose, true);
1034 mptcp_schedule_work((struct sock *)msk);
1035 }
1036
Paolo Abenicfde1412020-04-30 15:01:52 +02001037 if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
Geliang Tanga877de062020-09-24 08:29:51 +08001038 if (!mp_opt.echo) {
Geliang Tangf7dafee2021-04-06 17:16:00 -07001039 mptcp_pm_add_addr_received(msk, &mp_opt.addr);
Geliang Tanga877de062020-09-24 08:29:51 +08001040 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1041 } else {
Geliang Tangf7dafee2021-04-06 17:16:00 -07001042 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
Davide Carattid58300c2021-05-25 14:23:13 -07001043 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
Geliang Tanga877de062020-09-24 08:29:51 +08001044 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1045 }
Geliang Tang2fbdd9e2021-02-01 15:09:19 -08001046
Geliang Tangf7dafee2021-04-06 17:16:00 -07001047 if (mp_opt.addr.port)
Geliang Tang2fbdd9e2021-02-01 15:09:19 -08001048 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1049
Paolo Abenicfde1412020-04-30 15:01:52 +02001050 mp_opt.add_addr = 0;
Peter Krystad1b1c7a02020-03-27 14:48:38 -07001051 }
1052
Geliang Tangd0876b22020-09-24 08:29:49 +08001053 if (mp_opt.rm_addr) {
Geliang Tang5c4a8242021-03-12 17:16:13 -08001054 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
Geliang Tangd0876b22020-09-24 08:29:49 +08001055 mp_opt.rm_addr = 0;
1056 }
1057
Geliang Tang40453a52021-01-08 16:47:58 -08001058 if (mp_opt.mp_prio) {
1059 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
Geliang Tang0be2ac22021-01-08 16:48:01 -08001060 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
Geliang Tang40453a52021-01-08 16:47:58 -08001061 mp_opt.mp_prio = 0;
1062 }
1063
Florian Westphaldc87efd2021-04-01 16:19:44 -07001064 if (mp_opt.reset) {
1065 subflow->reset_seen = 1;
1066 subflow->reset_reason = mp_opt.reset_reason;
1067 subflow->reset_transient = mp_opt.reset_transient;
1068 }
1069
Paolo Abenicfde1412020-04-30 15:01:52 +02001070 if (!mp_opt.dss)
Mat Martineau648ef4b2020-01-21 16:56:24 -08001071 return;
1072
Paolo Abenicc9d2562020-03-27 14:48:42 -07001073 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1074 * monodirectional flows will stuck
1075 */
Paolo Abenicfde1412020-04-30 15:01:52 +02001076 if (mp_opt.use_ack)
Florian Westphal6f8a6122020-11-16 10:48:13 +01001077 ack_update_msk(msk, sk, &mp_opt);
Paolo Abenicc9d2562020-03-27 14:48:42 -07001078
Mat Martineau06827b32020-07-28 15:12:08 -07001079 /* Zero-data-length packets are dropped by the caller and not
1080 * propagated to the MPTCP layer, so the skb extension does not
1081 * need to be allocated or populated. DATA_FIN information, if
1082 * present, needs to be updated here before the skb is freed.
Mat Martineau43b54c62020-07-28 15:12:06 -07001083 */
1084 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1085 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
Mat Martineau1a49b2c2020-09-29 15:08:20 -07001086 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
Mat Martineau43b54c62020-07-28 15:12:06 -07001087 schedule_work(&msk->work))
1088 sock_hold(subflow->conn);
Mat Martineau06827b32020-07-28 15:12:08 -07001089
1090 return;
Mat Martineau43b54c62020-07-28 15:12:06 -07001091 }
1092
Mat Martineau648ef4b2020-01-21 16:56:24 -08001093 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1094 if (!mpext)
1095 return;
1096
1097 memset(mpext, 0, sizeof(*mpext));
1098
Paolo Abenicfde1412020-04-30 15:01:52 +02001099 if (mp_opt.use_map) {
1100 if (mp_opt.mpc_map) {
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001101 /* this is an MP_CAPABLE carrying MPTCP data
1102 * we know this map the first chunk of data
1103 */
1104 mptcp_crypto_key_sha(subflow->remote_key, NULL,
1105 &mpext->data_seq);
1106 mpext->data_seq++;
1107 mpext->subflow_seq = 1;
1108 mpext->dsn64 = 1;
1109 mpext->mpc_map = 1;
Paolo Abenia77895d2020-04-30 15:01:55 +02001110 mpext->data_fin = 0;
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001111 } else {
Paolo Abenicfde1412020-04-30 15:01:52 +02001112 mpext->data_seq = mp_opt.data_seq;
1113 mpext->subflow_seq = mp_opt.subflow_seq;
1114 mpext->dsn64 = mp_opt.dsn64;
1115 mpext->data_fin = mp_opt.data_fin;
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001116 }
Paolo Abenicfde1412020-04-30 15:01:52 +02001117 mpext->data_len = mp_opt.data_len;
Mat Martineau648ef4b2020-01-21 16:56:24 -08001118 mpext->use_map = 1;
Mat Martineau648ef4b2020-01-21 16:56:24 -08001119 }
Mat Martineau648ef4b2020-01-21 16:56:24 -08001120}
1121
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08001122static void mptcp_set_rwin(const struct tcp_sock *tp)
1123{
1124 const struct sock *ssk = (const struct sock *)tp;
1125 const struct mptcp_subflow_context *subflow;
1126 struct mptcp_sock *msk;
1127 u64 ack_seq;
1128
1129 subflow = mptcp_subflow_ctx(ssk);
1130 msk = mptcp_sk(subflow->conn);
1131
1132 ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1133
1134 if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1135 WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1136}
1137
Geliang Tangc94b1f92021-06-17 16:46:10 -07001138static u16 mptcp_make_csum(const struct mptcp_ext *mpext)
1139{
1140 struct csum_pseudo_header header;
1141 __wsum csum;
1142
1143 /* cfr RFC 8684 3.3.1.:
1144 * the data sequence number used in the pseudo-header is
1145 * always the 64-bit value, irrespective of what length is used in the
1146 * DSS option itself.
1147 */
1148 header.data_seq = cpu_to_be64(mpext->data_seq);
1149 header.subflow_seq = htonl(mpext->subflow_seq);
1150 header.data_len = htons(mpext->data_len);
1151 header.csum = 0;
1152
1153 csum = csum_partial(&header, sizeof(header), ~csum_unfold(mpext->csum));
1154 return (__force u16)csum_fold(csum);
1155}
1156
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08001157void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1158 struct mptcp_out_options *opts)
Peter Krystadeda7acd2020-01-21 16:56:16 -08001159{
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001160 if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
Peter Krystadeda7acd2020-01-21 16:56:16 -08001161 OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
Geliang Tang06fe1712021-06-17 16:46:09 -07001162 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
Peter Krystadeda7acd2020-01-21 16:56:16 -08001163
Geliang Tangc94b1f92021-06-17 16:46:10 -07001164 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
Peter Krystadeda7acd2020-01-21 16:56:16 -08001165 len = TCPOLEN_MPTCP_MPC_SYN;
Geliang Tangc94b1f92021-06-17 16:46:10 -07001166 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
Peter Krystadcec37a62020-01-21 16:56:18 -08001167 len = TCPOLEN_MPTCP_MPC_SYNACK;
Geliang Tangc94b1f92021-06-17 16:46:10 -07001168 } else if (opts->ext_copy.data_len) {
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001169 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
Geliang Tangc94b1f92021-06-17 16:46:10 -07001170 if (opts->csum_reqd)
1171 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1172 } else {
Peter Krystadeda7acd2020-01-21 16:56:16 -08001173 len = TCPOLEN_MPTCP_MPC_ACK;
Geliang Tangc94b1f92021-06-17 16:46:10 -07001174 }
Peter Krystadeda7acd2020-01-21 16:56:16 -08001175
Geliang Tang06fe1712021-06-17 16:46:09 -07001176 if (opts->csum_reqd)
1177 flag |= MPTCP_CAP_CHECKSUM_REQD;
1178
Peter Krystad3df523a2020-03-27 14:48:37 -07001179 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1180 MPTCP_SUPPORTED_VERSION,
Geliang Tang06fe1712021-06-17 16:46:09 -07001181 flag);
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001182
1183 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1184 opts->suboptions))
1185 goto mp_capable_done;
1186
Peter Krystadeda7acd2020-01-21 16:56:16 -08001187 put_unaligned_be64(opts->sndr_key, ptr);
1188 ptr += 2;
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001189 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1190 goto mp_capable_done;
1191
1192 put_unaligned_be64(opts->rcvr_key, ptr);
1193 ptr += 2;
1194 if (!opts->ext_copy.data_len)
1195 goto mp_capable_done;
1196
Geliang Tangc94b1f92021-06-17 16:46:10 -07001197 if (opts->csum_reqd) {
1198 put_unaligned_be32(opts->ext_copy.data_len << 16 |
1199 mptcp_make_csum(&opts->ext_copy), ptr);
1200 } else {
1201 put_unaligned_be32(opts->ext_copy.data_len << 16 |
1202 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1203 }
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001204 ptr += 1;
Peter Krystadeda7acd2020-01-21 16:56:16 -08001205 }
Mat Martineau6d0060f2020-01-21 16:56:23 -08001206
Christoph Paaschcc7972e2020-01-21 16:56:31 -08001207mp_capable_done:
Geliang Tangfef6b7e2021-04-06 17:15:59 -07001208 if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
Geliang Tang6eb3d1e2020-12-09 15:51:18 -08001209 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1210 u8 echo = MPTCP_ADDR_ECHO;
Peter Krystad3df523a2020-03-27 14:48:37 -07001211
1212#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Geliang Tangfef6b7e2021-04-06 17:15:59 -07001213 if (opts->addr.family == AF_INET6)
Geliang Tange1ef6832020-12-09 15:51:19 -08001214 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1215#endif
Geliang Tang6eb3d1e2020-12-09 15:51:18 -08001216
Geliang Tang30f60ba2021-04-06 17:15:58 -07001217 if (opts->addr.port)
Geliang Tang22fb85f2020-12-09 15:51:20 -08001218 len += TCPOLEN_MPTCP_PORT_LEN;
1219
Peter Krystad3df523a2020-03-27 14:48:37 -07001220 if (opts->ahmac) {
Geliang Tang6eb3d1e2020-12-09 15:51:18 -08001221 len += sizeof(opts->ahmac);
1222 echo = 0;
1223 }
Geliang Tange1ef6832020-12-09 15:51:19 -08001224
Geliang Tang6eb3d1e2020-12-09 15:51:18 -08001225 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
Geliang Tang30f60ba2021-04-06 17:15:58 -07001226 len, echo, opts->addr.id);
Geliang Tangfef6b7e2021-04-06 17:15:59 -07001227 if (opts->addr.family == AF_INET) {
Geliang Tang30f60ba2021-04-06 17:15:58 -07001228 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
Geliang Tange1ef6832020-12-09 15:51:19 -08001229 ptr += 1;
1230 }
1231#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Geliang Tangfef6b7e2021-04-06 17:15:59 -07001232 else if (opts->addr.family == AF_INET6) {
Geliang Tang30f60ba2021-04-06 17:15:58 -07001233 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
Geliang Tange1ef6832020-12-09 15:51:19 -08001234 ptr += 4;
1235 }
1236#endif
Geliang Tang22fb85f2020-12-09 15:51:20 -08001237
Geliang Tang30f60ba2021-04-06 17:15:58 -07001238 if (!opts->addr.port) {
Geliang Tang22fb85f2020-12-09 15:51:20 -08001239 if (opts->ahmac) {
1240 put_unaligned_be64(opts->ahmac, ptr);
1241 ptr += 2;
1242 }
1243 } else {
Geliang Tang30f60ba2021-04-06 17:15:58 -07001244 u16 port = ntohs(opts->addr.port);
1245
Geliang Tang22fb85f2020-12-09 15:51:20 -08001246 if (opts->ahmac) {
1247 u8 *bptr = (u8 *)ptr;
1248
Geliang Tang30f60ba2021-04-06 17:15:58 -07001249 put_unaligned_be16(port, bptr);
Geliang Tang22fb85f2020-12-09 15:51:20 -08001250 bptr += 2;
1251 put_unaligned_be64(opts->ahmac, bptr);
1252 bptr += 8;
1253 put_unaligned_be16(TCPOPT_NOP << 8 |
1254 TCPOPT_NOP, bptr);
1255
1256 ptr += 3;
1257 } else {
Geliang Tang30f60ba2021-04-06 17:15:58 -07001258 put_unaligned_be32(port << 16 |
Geliang Tang22fb85f2020-12-09 15:51:20 -08001259 TCPOPT_NOP << 8 |
1260 TCPOPT_NOP, ptr);
1261 ptr += 1;
1262 }
Peter Krystad3df523a2020-03-27 14:48:37 -07001263 }
1264 }
Peter Krystad3df523a2020-03-27 14:48:37 -07001265
1266 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
Geliang Tang6445e172021-03-12 17:16:11 -08001267 u8 i = 1;
1268
Peter Krystad3df523a2020-03-27 14:48:37 -07001269 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
Geliang Tang6445e172021-03-12 17:16:11 -08001270 TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1271 0, opts->rm_list.ids[0]);
1272
1273 while (i < opts->rm_list.nr) {
1274 u8 id1, id2, id3, id4;
1275
1276 id1 = opts->rm_list.ids[i];
1277 id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1278 id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1279 id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1280 put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1281 ptr += 1;
1282 i += 4;
1283 }
Peter Krystad3df523a2020-03-27 14:48:37 -07001284 }
1285
Geliang Tang06706542021-01-08 16:47:57 -08001286 if (OPTION_MPTCP_PRIO & opts->suboptions) {
1287 const struct sock *ssk = (const struct sock *)tp;
1288 struct mptcp_subflow_context *subflow;
1289
1290 subflow = mptcp_subflow_ctx(ssk);
1291 subflow->send_mp_prio = 0;
1292
1293 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1294 TCPOLEN_MPTCP_PRIO,
1295 opts->backup, TCPOPT_NOP);
1296 }
1297
Peter Krystadec3edaa2020-03-27 14:48:40 -07001298 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1299 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1300 TCPOLEN_MPTCP_MPJ_SYN,
1301 opts->backup, opts->join_id);
1302 put_unaligned_be32(opts->token, ptr);
1303 ptr += 1;
1304 put_unaligned_be32(opts->nonce, ptr);
1305 ptr += 1;
1306 }
1307
Peter Krystadf2962342020-03-27 14:48:39 -07001308 if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1309 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1310 TCPOLEN_MPTCP_MPJ_SYNACK,
1311 opts->backup, opts->join_id);
1312 put_unaligned_be64(opts->thmac, ptr);
1313 ptr += 2;
1314 put_unaligned_be32(opts->nonce, ptr);
1315 ptr += 1;
1316 }
1317
Peter Krystadec3edaa2020-03-27 14:48:40 -07001318 if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1319 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1320 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1321 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1322 ptr += 5;
1323 }
1324
Florian Westphaldc87efd2021-04-01 16:19:44 -07001325 if (OPTION_MPTCP_RST & opts->suboptions)
1326 *ptr++ = mptcp_option(MPTCPOPT_RST,
1327 TCPOLEN_MPTCP_RST,
1328 opts->reset_transient,
1329 opts->reset_reason);
1330
Mat Martineau6d0060f2020-01-21 16:56:23 -08001331 if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
1332 struct mptcp_ext *mpext = &opts->ext_copy;
1333 u8 len = TCPOLEN_MPTCP_DSS_BASE;
1334 u8 flags = 0;
1335
1336 if (mpext->use_ack) {
Christoph Paascha0c1d0e2020-05-14 08:53:03 -07001337 flags = MPTCP_DSS_HAS_ACK;
1338 if (mpext->ack64) {
1339 len += TCPOLEN_MPTCP_DSS_ACK64;
1340 flags |= MPTCP_DSS_ACK64;
1341 } else {
1342 len += TCPOLEN_MPTCP_DSS_ACK32;
1343 }
Mat Martineau6d0060f2020-01-21 16:56:23 -08001344 }
1345
1346 if (mpext->use_map) {
1347 len += TCPOLEN_MPTCP_DSS_MAP64;
1348
1349 /* Use only 64-bit mapping flags for now, add
1350 * support for optional 32-bit mappings later.
1351 */
1352 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1353 if (mpext->data_fin)
1354 flags |= MPTCP_DSS_DATA_FIN;
Geliang Tangc5b39e22021-06-17 16:46:11 -07001355
1356 if (opts->csum_reqd)
1357 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
Mat Martineau6d0060f2020-01-21 16:56:23 -08001358 }
1359
Peter Krystad3df523a2020-03-27 14:48:37 -07001360 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
Mat Martineau6d0060f2020-01-21 16:56:23 -08001361
1362 if (mpext->use_ack) {
Christoph Paascha0c1d0e2020-05-14 08:53:03 -07001363 if (mpext->ack64) {
1364 put_unaligned_be64(mpext->data_ack, ptr);
1365 ptr += 2;
1366 } else {
1367 put_unaligned_be32(mpext->data_ack32, ptr);
1368 ptr += 1;
1369 }
Mat Martineau6d0060f2020-01-21 16:56:23 -08001370 }
1371
1372 if (mpext->use_map) {
1373 put_unaligned_be64(mpext->data_seq, ptr);
1374 ptr += 2;
1375 put_unaligned_be32(mpext->subflow_seq, ptr);
1376 ptr += 1;
Geliang Tangc5b39e22021-06-17 16:46:11 -07001377 if (opts->csum_reqd) {
1378 put_unaligned_be32(mpext->data_len << 16 |
1379 mptcp_make_csum(mpext), ptr);
1380 } else {
1381 put_unaligned_be32(mpext->data_len << 16 |
1382 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1383 }
Mat Martineau6d0060f2020-01-21 16:56:23 -08001384 }
1385 }
Florian Westphalfa3fe2b2020-11-19 11:46:02 -08001386
1387 if (tp)
1388 mptcp_set_rwin(tp);
Peter Krystadeda7acd2020-01-21 16:56:16 -08001389}
Florian Westphaldc87efd2021-04-01 16:19:44 -07001390
1391__be32 mptcp_get_reset_option(const struct sk_buff *skb)
1392{
1393 const struct mptcp_ext *ext = mptcp_get_ext(skb);
1394 u8 flags, reason;
1395
1396 if (ext) {
1397 flags = ext->reset_transient;
1398 reason = ext->reset_reason;
1399
1400 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1401 flags, reason);
1402 }
1403
1404 return htonl(0u);
1405}
1406EXPORT_SYMBOL_GPL(mptcp_get_reset_option);