Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
7#define pr_fmt(fmt) "MPTCP: " fmt
8
9#include <linux/kernel.h>
10#include <crypto/sha2.h>
11#include <net/tcp.h>
12#include <net/mptcp.h>
13#include "protocol.h"
14#include "mib.h"
15
16#include <trace/events/mptcp.h>
17
18static bool mptcp_cap_flag_sha256(u8 flags)
19{
20 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
21}
22
23static void mptcp_parse_option(const struct sk_buff *skb,
24 const unsigned char *ptr, int opsize,
25 struct mptcp_options_received *mp_opt)
26{
27 u8 subtype = *ptr >> 4;
28 int expected_opsize;
29 u16 subopt;
30 u8 version;
31 u8 flags;
32 u8 i;
33
34 switch (subtype) {
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;
40 else
41 expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
42 subopt = OPTION_MPTCP_MPC_ACK;
43 } else {
44 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) {
45 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
46 subopt = OPTION_MPTCP_MPC_SYNACK;
47 } else {
48 expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
49 subopt = OPTION_MPTCP_MPC_SYN;
50 }
51 }
52
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
59 * broken
60 * We parse even option with mismatching csum presence, so that
61 * later in subflow_data_ready we can trigger the reset.
62 */
63 if (opsize != expected_opsize &&
64 (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
65 opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
66 break;
67
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)
72 break;
73 } else if (version < MPTCP_SUPPORTED_VERSION) {
74 break;
75 }
76
77 flags = *ptr++;
78 if (!mptcp_cap_flag_sha256(flags) ||
79 (flags & MPTCP_CAP_EXTENSIBILITY))
80 break;
81
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."
87 */
88 if (flags & MPTCP_CAP_CHECKSUM_REQD)
89 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
90
91 mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
92
93 mp_opt->suboptions |= subopt;
94 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
95 mp_opt->sndr_key = get_unaligned_be64(ptr);
96 ptr += 8;
97 }
98 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
99 mp_opt->rcvr_key = get_unaligned_be64(ptr);
100 ptr += 8;
101 }
102 if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
103 /* Section 3.1.:
104 * "the data parameters in a MP_CAPABLE are semantically
105 * equivalent to those in a DSS option and can be used
106 * interchangeably."
107 */
108 mp_opt->suboptions |= OPTION_MPTCP_DSS;
109 mp_opt->use_map = 1;
110 mp_opt->mpc_map = 1;
111 mp_opt->data_len = get_unaligned_be16(ptr);
112 ptr += 2;
113 }
114 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
115 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
116 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
117 ptr += 2;
118 }
119 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u\n",
120 version, flags, opsize, mp_opt->sndr_key,
121 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
122 break;
123
124 case MPTCPOPT_MP_JOIN:
125 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
126 mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYN;
127 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
128 mp_opt->join_id = *ptr++;
129 mp_opt->token = get_unaligned_be32(ptr);
130 ptr += 4;
131 mp_opt->nonce = get_unaligned_be32(ptr);
132 ptr += 4;
133 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u\n",
134 mp_opt->backup, mp_opt->join_id,
135 mp_opt->token, mp_opt->nonce);
136 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
137 mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYNACK;
138 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
139 mp_opt->join_id = *ptr++;
140 mp_opt->thmac = get_unaligned_be64(ptr);
141 ptr += 8;
142 mp_opt->nonce = get_unaligned_be32(ptr);
143 ptr += 4;
144 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
145 mp_opt->backup, mp_opt->join_id,
146 mp_opt->thmac, mp_opt->nonce);
147 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
148 mp_opt->suboptions |= OPTION_MPTCP_MPJ_ACK;
149 ptr += 2;
150 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
151 pr_debug("MP_JOIN hmac\n");
152 }
153 break;
154
155 case MPTCPOPT_DSS:
156 pr_debug("DSS\n");
157 ptr++;
158
159 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
160 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
161 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
162 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
163 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
164 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
165
166 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d\n",
167 mp_opt->data_fin, mp_opt->dsn64,
168 mp_opt->use_map, mp_opt->ack64,
169 mp_opt->use_ack);
170
171 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
172
173 if (mp_opt->use_ack) {
174 if (mp_opt->ack64)
175 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
176 else
177 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
178 }
179
180 if (mp_opt->use_map) {
181 if (mp_opt->dsn64)
182 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
183 else
184 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
185 }
186
187 /* Always parse any csum presence combination, we will enforce
188 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
189 */
190 if (opsize != expected_opsize &&
191 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
192 break;
193
194 mp_opt->suboptions |= OPTION_MPTCP_DSS;
195 if (mp_opt->use_ack) {
196 if (mp_opt->ack64) {
197 mp_opt->data_ack = get_unaligned_be64(ptr);
198 ptr += 8;
199 } else {
200 mp_opt->data_ack = get_unaligned_be32(ptr);
201 ptr += 4;
202 }
203
204 pr_debug("data_ack=%llu\n", mp_opt->data_ack);
205 }
206
207 if (mp_opt->use_map) {
208 if (mp_opt->dsn64) {
209 mp_opt->data_seq = get_unaligned_be64(ptr);
210 ptr += 8;
211 } else {
212 mp_opt->data_seq = get_unaligned_be32(ptr);
213 ptr += 4;
214 }
215
216 mp_opt->subflow_seq = get_unaligned_be32(ptr);
217 ptr += 4;
218
219 mp_opt->data_len = get_unaligned_be16(ptr);
220 ptr += 2;
221
222 if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
223 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
224 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
225 ptr += 2;
226 }
227
228 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
229 mp_opt->data_seq, mp_opt->subflow_seq,
230 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
231 mp_opt->csum);
232 }
233
234 break;
235
236 case MPTCPOPT_ADD_ADDR:
237 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
238 if (!mp_opt->echo) {
239 if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
240 opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
241 mp_opt->addr.family = AF_INET;
242#if IS_ENABLED(CONFIG_MPTCP_IPV6)
243 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
244 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
245 mp_opt->addr.family = AF_INET6;
246#endif
247 else
248 break;
249 } else {
250 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
251 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
252 mp_opt->addr.family = AF_INET;
253#if IS_ENABLED(CONFIG_MPTCP_IPV6)
254 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
255 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
256 mp_opt->addr.family = AF_INET6;
257#endif
258 else
259 break;
260 }
261
262 mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
263 mp_opt->addr.id = *ptr++;
264 mp_opt->addr.port = 0;
265 mp_opt->ahmac = 0;
266 if (mp_opt->addr.family == AF_INET) {
267 memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
268 ptr += 4;
269 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
270 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
271 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
272 ptr += 2;
273 }
274 }
275#if IS_ENABLED(CONFIG_MPTCP_IPV6)
276 else {
277 memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
278 ptr += 16;
279 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
280 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
281 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
282 ptr += 2;
283 }
284 }
285#endif
286 if (!mp_opt->echo) {
287 mp_opt->ahmac = get_unaligned_be64(ptr);
288 ptr += 8;
289 }
290 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d\n",
291 (mp_opt->addr.family == AF_INET6) ? "6" : "",
292 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
293 break;
294
295 case MPTCPOPT_RM_ADDR:
296 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
297 opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
298 break;
299
300 ptr++;
301
302 mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
303 mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
304 for (i = 0; i < mp_opt->rm_list.nr; i++)
305 mp_opt->rm_list.ids[i] = *ptr++;
306 pr_debug("RM_ADDR: rm_list_nr=%d\n", mp_opt->rm_list.nr);
307 break;
308
309 case MPTCPOPT_MP_PRIO:
310 if (opsize != TCPOLEN_MPTCP_PRIO)
311 break;
312
313 mp_opt->suboptions |= OPTION_MPTCP_PRIO;
314 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
315 pr_debug("MP_PRIO: prio=%d\n", mp_opt->backup);
316 break;
317
318 case MPTCPOPT_MP_FASTCLOSE:
319 if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
320 break;
321
322 ptr += 2;
323 mp_opt->rcvr_key = get_unaligned_be64(ptr);
324 ptr += 8;
325 mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
326 pr_debug("MP_FASTCLOSE: recv_key=%llu\n", mp_opt->rcvr_key);
327 break;
328
329 case MPTCPOPT_RST:
330 if (opsize != TCPOLEN_MPTCP_RST)
331 break;
332
333 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
334 break;
335
336 mp_opt->suboptions |= OPTION_MPTCP_RST;
337 flags = *ptr++;
338 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
339 mp_opt->reset_reason = *ptr;
340 pr_debug("MP_RST: transient=%u reason=%u\n",
341 mp_opt->reset_transient, mp_opt->reset_reason);
342 break;
343
344 case MPTCPOPT_MP_FAIL:
345 if (opsize != TCPOLEN_MPTCP_FAIL)
346 break;
347
348 ptr += 2;
349 mp_opt->suboptions |= OPTION_MPTCP_FAIL;
350 mp_opt->fail_seq = get_unaligned_be64(ptr);
351 pr_debug("MP_FAIL: data_seq=%llu\n", mp_opt->fail_seq);
352 break;
353
354 default:
355 break;
356 }
357}
358
359void mptcp_get_options(const struct sk_buff *skb,
360 struct mptcp_options_received *mp_opt)
361{
362 const struct tcphdr *th = tcp_hdr(skb);
363 const unsigned char *ptr;
364 int length;
365
366 /* Ensure that casting the whole status to u32 is efficient and safe */
367 BUILD_BUG_ON(sizeof_field(struct mptcp_options_received, status) != sizeof(u32));
368 BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_options_received, status),
369 sizeof(u32)));
370 *(u32 *)&mp_opt->status = 0;
371
372 length = (th->doff * 4) - sizeof(struct tcphdr);
373 ptr = (const unsigned char *)(th + 1);
374
375 while (length > 0) {
376 int opcode = *ptr++;
377 int opsize;
378
379 switch (opcode) {
380 case TCPOPT_EOL:
381 return;
382 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
383 length--;
384 continue;
385 default:
386 if (length < 2)
387 return;
388 opsize = *ptr++;
389 if (opsize < 2) /* "silly options" */
390 return;
391 if (opsize > length)
392 return; /* don't parse partial options */
393 if (opcode == TCPOPT_MPTCP)
394 mptcp_parse_option(skb, ptr, opsize, mp_opt);
395 ptr += opsize - 2;
396 length -= opsize;
397 }
398 }
399}
400
401bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
402 unsigned int *size, struct mptcp_out_options *opts)
403{
404 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
405
406 /* we will use snd_isn to detect first pkt [re]transmission
407 * in mptcp_established_options_mp()
408 */
409 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
410 if (subflow->request_mptcp) {
411 opts->suboptions = OPTION_MPTCP_MPC_SYN;
412 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
413 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
414 *size = TCPOLEN_MPTCP_MPC_SYN;
415 return true;
416 } else if (subflow->request_join) {
417 pr_debug("remote_token=%u, nonce=%u\n", subflow->remote_token,
418 subflow->local_nonce);
419 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
420 opts->join_id = subflow->local_id;
421 opts->token = subflow->remote_token;
422 opts->nonce = subflow->local_nonce;
423 opts->backup = subflow->request_bkup;
424 *size = TCPOLEN_MPTCP_MPJ_SYN;
425 return true;
426 }
427 return false;
428}
429
430static void clear_3rdack_retransmission(struct sock *sk)
431{
432 struct inet_connection_sock *icsk = inet_csk(sk);
433
434 sk_stop_timer(sk, &icsk->icsk_delack_timer);
435 icsk->icsk_ack.timeout = 0;
436 icsk->icsk_ack.ato = 0;
437 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
438}
439
440static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
441 bool snd_data_fin_enable,
442 unsigned int *size,
443 struct mptcp_out_options *opts)
444{
445 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
446 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
447 struct mptcp_ext *mpext;
448 unsigned int data_len;
449 u8 len;
450
451 /* When skb is not available, we better over-estimate the emitted
452 * options len. A full DSS option (28 bytes) is longer than
453 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
454 * tell the caller to defer the estimate to
455 * mptcp_established_options_dss(), which will reserve enough space.
456 */
457 if (!skb)
458 return false;
459
460 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
461 if (READ_ONCE(subflow->fully_established) || snd_data_fin_enable ||
462 subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
463 sk->sk_state != TCP_ESTABLISHED)
464 return false;
465
466 if (subflow->mp_capable) {
467 mpext = mptcp_get_ext(skb);
468 data_len = mpext ? mpext->data_len : 0;
469
470 /* we will check ops->data_len in mptcp_write_options() to
471 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
472 * TCPOLEN_MPTCP_MPC_ACK
473 */
474 opts->data_len = data_len;
475 opts->suboptions = OPTION_MPTCP_MPC_ACK;
476 opts->sndr_key = subflow->local_key;
477 opts->rcvr_key = subflow->remote_key;
478 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
479 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
480
481 /* Section 3.1.
482 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
483 * packets that start the first subflow of an MPTCP connection,
484 * as well as the first packet that carries data
485 */
486 if (data_len > 0) {
487 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
488 if (opts->csum_reqd) {
489 /* we need to propagate more info to csum the pseudo hdr */
490 opts->data_seq = mpext->data_seq;
491 opts->subflow_seq = mpext->subflow_seq;
492 opts->csum = mpext->csum;
493 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
494 }
495 *size = ALIGN(len, 4);
496 } else {
497 *size = TCPOLEN_MPTCP_MPC_ACK;
498 }
499
500 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d\n",
501 subflow, subflow->local_key, subflow->remote_key,
502 data_len);
503
504 return true;
505 } else if (subflow->mp_join) {
506 opts->suboptions = OPTION_MPTCP_MPJ_ACK;
507 memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
508 *size = TCPOLEN_MPTCP_MPJ_ACK;
509 pr_debug("subflow=%p\n", subflow);
510
511 /* we can use the full delegate action helper only from BH context
512 * If we are in process context - sk is flushing the backlog at
513 * socket lock release time - just set the appropriate flag, will
514 * be handled by the release callback
515 */
516 if (sock_owned_by_user(sk))
517 set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
518 else
519 mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
520 return true;
521 }
522 return false;
523}
524
525static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
526 struct sk_buff *skb, struct mptcp_ext *ext)
527{
528 /* The write_seq value has already been incremented, so the actual
529 * sequence number for the DATA_FIN is one less.
530 */
531 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
532
533 if (!ext->use_map || !skb->len) {
534 /* RFC6824 requires a DSS mapping with specific values
535 * if DATA_FIN is set but no data payload is mapped
536 */
537 ext->data_fin = 1;
538 ext->use_map = 1;
539 ext->dsn64 = 1;
540 ext->data_seq = data_fin_tx_seq;
541 ext->subflow_seq = 0;
542 ext->data_len = 1;
543 } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
544 /* If there's an existing DSS mapping and it is the
545 * final mapping, DATA_FIN consumes 1 additional byte of
546 * mapping space.
547 */
548 ext->data_fin = 1;
549 ext->data_len++;
550 }
551}
552
553static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
554 bool snd_data_fin_enable,
555 unsigned int *size,
556 struct mptcp_out_options *opts)
557{
558 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
559 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
560 unsigned int dss_size = 0;
561 struct mptcp_ext *mpext;
562 unsigned int ack_size;
563 bool ret = false;
564 u64 ack_seq;
565
566 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
567 mpext = skb ? mptcp_get_ext(skb) : NULL;
568
569 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
570 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
571
572 if (mpext) {
573 if (opts->csum_reqd)
574 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
575
576 opts->ext_copy = *mpext;
577 }
578
579 dss_size = map_size;
580 if (skb && snd_data_fin_enable)
581 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
582 opts->suboptions = OPTION_MPTCP_DSS;
583 ret = true;
584 }
585
586 /* passive sockets msk will set the 'can_ack' after accept(), even
587 * if the first subflow may have the already the remote key handy
588 */
589 opts->ext_copy.use_ack = 0;
590 if (!READ_ONCE(msk->can_ack)) {
591 *size = ALIGN(dss_size, 4);
592 return ret;
593 }
594
595 ack_seq = READ_ONCE(msk->ack_seq);
596 if (READ_ONCE(msk->use_64bit_ack)) {
597 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
598 opts->ext_copy.data_ack = ack_seq;
599 opts->ext_copy.ack64 = 1;
600 } else {
601 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
602 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
603 opts->ext_copy.ack64 = 0;
604 }
605 opts->ext_copy.use_ack = 1;
606 opts->suboptions = OPTION_MPTCP_DSS;
607
608 /* Add kind/length/subtype/flag overhead if mapping is not populated */
609 if (dss_size == 0)
610 ack_size += TCPOLEN_MPTCP_DSS_BASE;
611
612 dss_size += ack_size;
613
614 *size = ALIGN(dss_size, 4);
615 return true;
616}
617
618static u64 add_addr_generate_hmac(u64 key1, u64 key2,
619 struct mptcp_addr_info *addr)
620{
621 u16 port = ntohs(addr->port);
622 u8 hmac[SHA256_DIGEST_SIZE];
623 u8 msg[19];
624 int i = 0;
625
626 msg[i++] = addr->id;
627 if (addr->family == AF_INET) {
628 memcpy(&msg[i], &addr->addr.s_addr, 4);
629 i += 4;
630 }
631#if IS_ENABLED(CONFIG_MPTCP_IPV6)
632 else if (addr->family == AF_INET6) {
633 memcpy(&msg[i], &addr->addr6.s6_addr, 16);
634 i += 16;
635 }
636#endif
637 msg[i++] = port >> 8;
638 msg[i++] = port & 0xFF;
639
640 mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
641
642 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
643}
644
645static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
646 unsigned int *size,
647 unsigned int remaining,
648 struct mptcp_out_options *opts)
649{
650 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
651 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
652 bool drop_other_suboptions = false;
653 unsigned int opt_size = *size;
654 bool echo;
655 int len;
656
657 /* add addr will strip the existing options, be sure to avoid breaking
658 * MPC/MPJ handshakes
659 */
660 if (!mptcp_pm_should_add_signal(msk) ||
661 (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
662 !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
663 &echo, &drop_other_suboptions))
664 return false;
665
666 /*
667 * Later on, mptcp_write_options() will enforce mutually exclusion with
668 * DSS, bail out if such option is set and we can't drop it.
669 */
670 if (drop_other_suboptions)
671 remaining += opt_size;
672 else if (opts->suboptions & OPTION_MPTCP_DSS)
673 return false;
674
675 len = mptcp_add_addr_len(opts->addr.family, echo, !!opts->addr.port);
676 if (remaining < len)
677 return false;
678
679 *size = len;
680 if (drop_other_suboptions) {
681 pr_debug("drop other suboptions\n");
682 opts->suboptions = 0;
683
684 /* note that e.g. DSS could have written into the memory
685 * aliased by ahmac, we must reset the field here
686 * to avoid appending the hmac even for ADD_ADDR echo
687 * options
688 */
689 opts->ahmac = 0;
690 *size -= opt_size;
691 }
692 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
693 if (!echo) {
694 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
695 opts->ahmac = add_addr_generate_hmac(READ_ONCE(msk->local_key),
696 READ_ONCE(msk->remote_key),
697 &opts->addr);
698 } else {
699 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
700 }
701 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d\n",
702 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
703
704 return true;
705}
706
707static bool mptcp_established_options_rm_addr(struct sock *sk,
708 unsigned int *size,
709 unsigned int remaining,
710 struct mptcp_out_options *opts)
711{
712 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
713 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
714 struct mptcp_rm_list rm_list;
715 int i, len;
716
717 if (!mptcp_pm_should_rm_signal(msk) ||
718 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
719 return false;
720
721 len = mptcp_rm_addr_len(&rm_list);
722 if (len < 0)
723 return false;
724 if (remaining < len)
725 return false;
726
727 *size = len;
728 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
729 opts->rm_list = rm_list;
730
731 for (i = 0; i < opts->rm_list.nr; i++)
732 pr_debug("rm_list_ids[%d]=%d\n", i, opts->rm_list.ids[i]);
733 MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
734 return true;
735}
736
737static bool mptcp_established_options_mp_prio(struct sock *sk,
738 unsigned int *size,
739 unsigned int remaining,
740 struct mptcp_out_options *opts)
741{
742 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
743
744 /* can't send MP_PRIO with MPC, as they share the same option space:
745 * 'backup'. Also it makes no sense at all
746 */
747 if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
748 return false;
749
750 /* account for the trailing 'nop' option */
751 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
752 return false;
753
754 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
755 opts->suboptions |= OPTION_MPTCP_PRIO;
756 opts->backup = subflow->request_bkup;
757
758 pr_debug("prio=%d\n", opts->backup);
759
760 return true;
761}
762
763static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
764 unsigned int *size,
765 unsigned int remaining,
766 struct mptcp_out_options *opts)
767{
768 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
769
770 if (remaining < TCPOLEN_MPTCP_RST)
771 return false;
772
773 *size = TCPOLEN_MPTCP_RST;
774 opts->suboptions |= OPTION_MPTCP_RST;
775 opts->reset_transient = subflow->reset_transient;
776 opts->reset_reason = subflow->reset_reason;
777 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
778
779 return true;
780}
781
782static bool mptcp_established_options_fastclose(struct sock *sk,
783 unsigned int *size,
784 unsigned int remaining,
785 struct mptcp_out_options *opts)
786{
787 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
788 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
789
790 if (likely(!subflow->send_fastclose))
791 return false;
792
793 if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
794 return false;
795
796 *size = TCPOLEN_MPTCP_FASTCLOSE;
797 opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
798 opts->rcvr_key = READ_ONCE(msk->remote_key);
799
800 pr_debug("FASTCLOSE key=%llu\n", opts->rcvr_key);
801 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
802 return true;
803}
804
805static bool mptcp_established_options_mp_fail(struct sock *sk,
806 unsigned int *size,
807 unsigned int remaining,
808 struct mptcp_out_options *opts)
809{
810 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
811
812 if (likely(!subflow->send_mp_fail))
813 return false;
814
815 if (remaining < TCPOLEN_MPTCP_FAIL)
816 return false;
817
818 *size = TCPOLEN_MPTCP_FAIL;
819 opts->suboptions |= OPTION_MPTCP_FAIL;
820 opts->fail_seq = subflow->map_seq;
821
822 pr_debug("MP_FAIL fail_seq=%llu\n", opts->fail_seq);
823 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
824
825 return true;
826}
827
828bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
829 unsigned int *size, unsigned int remaining,
830 struct mptcp_out_options *opts)
831{
832 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
833 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
834 unsigned int opt_size = 0;
835 bool snd_data_fin;
836 bool ret = false;
837
838 opts->suboptions = 0;
839
840 if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
841 return false;
842
843 if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
844 if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
845 mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
846 *size += opt_size;
847 remaining -= opt_size;
848 }
849 /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
850 if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
851 *size += opt_size;
852 remaining -= opt_size;
853 }
854 return true;
855 }
856
857 snd_data_fin = mptcp_data_fin_enabled(msk);
858 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
859 ret = true;
860 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
861 unsigned int mp_fail_size;
862
863 ret = true;
864 if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
865 remaining - opt_size, opts)) {
866 *size += opt_size + mp_fail_size;
867 remaining -= opt_size - mp_fail_size;
868 return true;
869 }
870 }
871
872 /* we reserved enough space for the above options, and exceeding the
873 * TCP option space would be fatal
874 */
875 if (WARN_ON_ONCE(opt_size > remaining))
876 return false;
877
878 *size += opt_size;
879 remaining -= opt_size;
880 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
881 *size += opt_size;
882 remaining -= opt_size;
883 ret = true;
884 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
885 *size += opt_size;
886 remaining -= opt_size;
887 ret = true;
888 }
889
890 if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
891 *size += opt_size;
892 remaining -= opt_size;
893 ret = true;
894 }
895
896 return ret;
897}
898
899bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
900 struct mptcp_out_options *opts)
901{
902 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
903
904 if (subflow_req->mp_capable) {
905 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
906 opts->sndr_key = subflow_req->local_key;
907 opts->csum_reqd = subflow_req->csum_reqd;
908 opts->allow_join_id0 = subflow_req->allow_join_id0;
909 *size = TCPOLEN_MPTCP_MPC_SYNACK;
910 pr_debug("subflow_req=%p, local_key=%llu\n",
911 subflow_req, subflow_req->local_key);
912 return true;
913 } else if (subflow_req->mp_join) {
914 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
915 opts->backup = subflow_req->request_bkup;
916 opts->join_id = subflow_req->local_id;
917 opts->thmac = subflow_req->thmac;
918 opts->nonce = subflow_req->local_nonce;
919 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
920 subflow_req, opts->backup, opts->join_id,
921 opts->thmac, opts->nonce);
922 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
923 return true;
924 }
925 return false;
926}
927
928static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
929 struct mptcp_subflow_context *subflow,
930 struct sk_buff *skb,
931 struct mptcp_options_received *mp_opt)
932{
933 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
934 * will make the subflow fully established
935 */
936 if (likely(READ_ONCE(subflow->fully_established))) {
937 /* on passive sockets, check for 3rd ack retransmission
938 * note that msk is always set by subflow_syn_recv_sock()
939 * for mp_join subflows
940 */
941 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
942 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
943 subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
944 !subflow->request_join)
945 tcp_send_ack(ssk);
946 goto check_notify;
947 }
948
949 /* we must process OoO packets before the first subflow is fully
950 * established. OoO packets are instead a protocol violation
951 * for MP_JOIN subflows as the peer must not send any data
952 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
953 */
954 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
955 if (subflow->mp_join)
956 goto reset;
957 if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
958 goto set_fully_established;
959 return subflow->mp_capable;
960 }
961
962 if (subflow->remote_key_valid &&
963 (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
964 ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) &&
965 (!mp_opt->echo || subflow->mp_join)))) {
966 /* subflows are fully established as soon as we get any
967 * additional ack, including ADD_ADDR.
968 */
969 goto set_fully_established;
970 }
971
972 /* If the first established packet does not contain MP_CAPABLE + data
973 * then fallback to TCP. Fallback scenarios requires a reset for
974 * MP_JOIN subflows.
975 */
976 if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
977 if (subflow->mp_join)
978 goto reset;
979 subflow->mp_capable = 0;
980 pr_fallback(msk);
981 mptcp_do_fallback(ssk);
982 return false;
983 }
984
985 if (mp_opt->deny_join_id0)
986 WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
987
988 if (unlikely(!READ_ONCE(msk->pm.server_side)))
989 pr_warn_once("bogus mpc option on established client sk");
990
991set_fully_established:
992 mptcp_data_lock((struct sock *)msk);
993 __mptcp_subflow_fully_established(msk, subflow, mp_opt);
994 mptcp_data_unlock((struct sock *)msk);
995
996check_notify:
997 /* if the subflow is not already linked into the conn_list, we can't
998 * notify the PM: this subflow is still on the listener queue
999 * and the PM possibly acquiring the subflow lock could race with
1000 * the listener close
1001 */
1002 if (likely(subflow->pm_notified) || list_empty(&subflow->node))
1003 return true;
1004
1005 subflow->pm_notified = 1;
1006 if (subflow->mp_join) {
1007 clear_3rdack_retransmission(ssk);
1008 mptcp_pm_subflow_established(msk);
1009 } else {
1010 mptcp_pm_fully_established(msk, ssk);
1011 }
1012 return true;
1013
1014reset:
1015 mptcp_subflow_reset(ssk);
1016 return false;
1017}
1018
1019u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1020{
1021 u32 old_seq32, cur_seq32;
1022
1023 old_seq32 = (u32)old_seq;
1024 cur_seq32 = (u32)cur_seq;
1025 cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
1026 if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
1027 return cur_seq + (1LL << 32);
1028
1029 /* reverse wrap could happen, too */
1030 if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
1031 return cur_seq - (1LL << 32);
1032 return cur_seq;
1033}
1034
1035static void __mptcp_snd_una_update(struct mptcp_sock *msk, u64 new_snd_una)
1036{
1037 msk->bytes_acked += new_snd_una - msk->snd_una;
1038 WRITE_ONCE(msk->snd_una, new_snd_una);
1039}
1040
1041static void ack_update_msk(struct mptcp_sock *msk,
1042 struct sock *ssk,
1043 struct mptcp_options_received *mp_opt)
1044{
1045 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1046 struct sock *sk = (struct sock *)msk;
1047 u64 old_snd_una;
1048
1049 mptcp_data_lock(sk);
1050
1051 /* avoid ack expansion on update conflict, to reduce the risk of
1052 * wrongly expanding to a future ack sequence number, which is way
1053 * more dangerous than missing an ack
1054 */
1055 old_snd_una = msk->snd_una;
1056 new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1057
1058 /* ACK for data not even sent yet? Ignore.*/
1059 if (unlikely(after64(new_snd_una, snd_nxt)))
1060 new_snd_una = old_snd_una;
1061
1062 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1063
1064 if (after64(new_wnd_end, msk->wnd_end))
1065 WRITE_ONCE(msk->wnd_end, new_wnd_end);
1066
1067 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1068 if (after64(msk->wnd_end, snd_nxt))
1069 __mptcp_check_push(sk, ssk);
1070
1071 if (after64(new_snd_una, old_snd_una)) {
1072 __mptcp_snd_una_update(msk, new_snd_una);
1073 __mptcp_data_acked(sk);
1074 }
1075 msk->last_ack_recv = tcp_jiffies32;
1076 mptcp_data_unlock(sk);
1077
1078 trace_ack_update_msk(mp_opt->data_ack,
1079 old_snd_una, new_snd_una,
1080 new_wnd_end, READ_ONCE(msk->wnd_end));
1081}
1082
1083bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1084{
1085 /* Skip if DATA_FIN was already received.
1086 * If updating simultaneously with the recvmsg loop, values
1087 * should match. If they mismatch, the peer is misbehaving and
1088 * we will prefer the most recent information.
1089 */
1090 if (READ_ONCE(msk->rcv_data_fin))
1091 return false;
1092
1093 WRITE_ONCE(msk->rcv_data_fin_seq,
1094 mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1095 WRITE_ONCE(msk->rcv_data_fin, 1);
1096
1097 return true;
1098}
1099
1100static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1101 struct mptcp_options_received *mp_opt)
1102{
1103 u64 hmac = 0;
1104
1105 if (mp_opt->echo)
1106 return true;
1107
1108 hmac = add_addr_generate_hmac(READ_ONCE(msk->remote_key),
1109 READ_ONCE(msk->local_key),
1110 &mp_opt->addr);
1111
1112 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1113 msk, hmac, mp_opt->ahmac);
1114
1115 return hmac == mp_opt->ahmac;
1116}
1117
1118/* Return false if a subflow has been reset, else return true */
1119bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1120{
1121 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1122 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1123 struct mptcp_options_received mp_opt;
1124 struct mptcp_ext *mpext;
1125
1126 if (__mptcp_check_fallback(msk)) {
1127 /* Keep it simple and unconditionally trigger send data cleanup and
1128 * pending queue spooling. We will need to acquire the data lock
1129 * for more accurate checks, and once the lock is acquired, such
1130 * helpers are cheap.
1131 */
1132 mptcp_data_lock(subflow->conn);
1133 if (sk_stream_memory_free(sk))
1134 __mptcp_check_push(subflow->conn, sk);
1135
1136 /* on fallback we just need to ignore the msk-level snd_una, as
1137 * this is really plain TCP
1138 */
1139 __mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
1140
1141 __mptcp_data_acked(subflow->conn);
1142 mptcp_data_unlock(subflow->conn);
1143 return true;
1144 }
1145
1146 mptcp_get_options(skb, &mp_opt);
1147
1148 /* The subflow can be in close state only if check_fully_established()
1149 * just sent a reset. If so, tell the caller to ignore the current packet.
1150 */
1151 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1152 return sk->sk_state != TCP_CLOSE;
1153
1154 if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1155 if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1156 READ_ONCE(msk->local_key) == mp_opt.rcvr_key) {
1157 WRITE_ONCE(msk->rcv_fastclose, true);
1158 mptcp_schedule_work((struct sock *)msk);
1159 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
1160 }
1161
1162 if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1163 add_addr_hmac_valid(msk, &mp_opt)) {
1164 if (!mp_opt.echo) {
1165 mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1166 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1167 } else {
1168 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1169 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1170 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1171 }
1172
1173 if (mp_opt.addr.port)
1174 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1175 }
1176
1177 if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1178 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1179
1180 if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1181 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1182 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1183 }
1184
1185 if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1186 mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1187 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1188 }
1189
1190 if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1191 subflow->reset_seen = 1;
1192 subflow->reset_reason = mp_opt.reset_reason;
1193 subflow->reset_transient = mp_opt.reset_transient;
1194 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1195 }
1196
1197 if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1198 return true;
1199 }
1200
1201 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1202 * monodirectional flows will stuck
1203 */
1204 if (mp_opt.use_ack)
1205 ack_update_msk(msk, sk, &mp_opt);
1206
1207 /* Zero-data-length packets are dropped by the caller and not
1208 * propagated to the MPTCP layer, so the skb extension does not
1209 * need to be allocated or populated. DATA_FIN information, if
1210 * present, needs to be updated here before the skb is freed.
1211 */
1212 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1213 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1214 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
1215 mptcp_schedule_work((struct sock *)msk);
1216
1217 return true;
1218 }
1219
1220 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1221 if (!mpext)
1222 return true;
1223
1224 memset(mpext, 0, sizeof(*mpext));
1225
1226 if (likely(mp_opt.use_map)) {
1227 if (mp_opt.mpc_map) {
1228 /* this is an MP_CAPABLE carrying MPTCP data
1229 * we know this map the first chunk of data
1230 */
1231 mptcp_crypto_key_sha(subflow->remote_key, NULL,
1232 &mpext->data_seq);
1233 mpext->data_seq++;
1234 mpext->subflow_seq = 1;
1235 mpext->dsn64 = 1;
1236 mpext->mpc_map = 1;
1237 mpext->data_fin = 0;
1238 } else {
1239 mpext->data_seq = mp_opt.data_seq;
1240 mpext->subflow_seq = mp_opt.subflow_seq;
1241 mpext->dsn64 = mp_opt.dsn64;
1242 mpext->data_fin = mp_opt.data_fin;
1243 }
1244 mpext->data_len = mp_opt.data_len;
1245 mpext->use_map = 1;
1246 mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1247
1248 if (mpext->csum_reqd)
1249 mpext->csum = mp_opt.csum;
1250 }
1251
1252 return true;
1253}
1254
1255static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
1256{
1257 const struct sock *ssk = (const struct sock *)tp;
1258 struct mptcp_subflow_context *subflow;
1259 u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
1260 struct mptcp_sock *msk;
1261 u32 new_win;
1262 u64 win;
1263
1264 subflow = mptcp_subflow_ctx(ssk);
1265 msk = mptcp_sk(subflow->conn);
1266
1267 ack_seq = READ_ONCE(msk->ack_seq);
1268 rcv_wnd_new = ack_seq + tp->rcv_wnd;
1269
1270 rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1271 if (after64(rcv_wnd_new, rcv_wnd_old)) {
1272 u64 rcv_wnd;
1273
1274 for (;;) {
1275 rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1276
1277 if (rcv_wnd == rcv_wnd_old)
1278 break;
1279
1280 rcv_wnd_old = rcv_wnd;
1281 if (before64(rcv_wnd_new, rcv_wnd_old)) {
1282 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1283 goto raise_win;
1284 }
1285 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1286 }
1287 goto update_wspace;
1288 }
1289
1290 if (rcv_wnd_new != rcv_wnd_old) {
1291raise_win:
1292 win = rcv_wnd_old - ack_seq;
1293 tp->rcv_wnd = min_t(u64, win, U32_MAX);
1294 new_win = tp->rcv_wnd;
1295
1296 /* Make sure we do not exceed the maximum possible
1297 * scaled window.
1298 */
1299 if (unlikely(th->syn))
1300 new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1301 if (!tp->rx_opt.rcv_wscale &&
1302 READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1303 new_win = min(new_win, MAX_TCP_WINDOW);
1304 else
1305 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1306
1307 /* RFC1323 scaling applied */
1308 new_win >>= tp->rx_opt.rcv_wscale;
1309 th->window = htons(new_win);
1310 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1311 }
1312
1313update_wspace:
1314 WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
1315}
1316
1317__sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1318{
1319 struct csum_pseudo_header header;
1320 __wsum csum;
1321
1322 /* cfr RFC 8684 3.3.1.:
1323 * the data sequence number used in the pseudo-header is
1324 * always the 64-bit value, irrespective of what length is used in the
1325 * DSS option itself.
1326 */
1327 header.data_seq = cpu_to_be64(data_seq);
1328 header.subflow_seq = htonl(subflow_seq);
1329 header.data_len = htons(data_len);
1330 header.csum = 0;
1331
1332 csum = csum_partial(&header, sizeof(header), sum);
1333 return csum_fold(csum);
1334}
1335
1336static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1337{
1338 return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1339 ~csum_unfold(mpext->csum));
1340}
1341
1342static void put_len_csum(u16 len, __sum16 csum, void *data)
1343{
1344 __sum16 *sumptr = data + 2;
1345 __be16 *ptr = data;
1346
1347 put_unaligned_be16(len, ptr);
1348
1349 put_unaligned(csum, sumptr);
1350}
1351
1352void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1353 struct mptcp_out_options *opts)
1354{
1355 const struct sock *ssk = (const struct sock *)tp;
1356 struct mptcp_subflow_context *subflow;
1357
1358 /* Which options can be used together?
1359 *
1360 * X: mutually exclusive
1361 * O: often used together
1362 * C: can be used together in some cases
1363 * P: could be used together but we prefer not to (optimisations)
1364 *
1365 * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
1366 * ------|------|------|------|------|------|------|------|------|
1367 * MPC |------|------|------|------|------|------|------|------|
1368 * MPJ | X |------|------|------|------|------|------|------|
1369 * DSS | X | X |------|------|------|------|------|------|
1370 * ADD | X | X | P |------|------|------|------|------|
1371 * RM | C | C | C | P |------|------|------|------|
1372 * PRIO | X | C | C | C | C |------|------|------|
1373 * FAIL | X | X | C | X | X | X |------|------|
1374 * FC | X | X | X | X | X | X | X |------|
1375 * RST | X | X | X | X | X | X | O | O |
1376 * ------|------|------|------|------|------|------|------|------|
1377 *
1378 * The same applies in mptcp_established_options() function.
1379 */
1380 if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1381 struct mptcp_ext *mpext = &opts->ext_copy;
1382 u8 len = TCPOLEN_MPTCP_DSS_BASE;
1383 u8 flags = 0;
1384
1385 if (mpext->use_ack) {
1386 flags = MPTCP_DSS_HAS_ACK;
1387 if (mpext->ack64) {
1388 len += TCPOLEN_MPTCP_DSS_ACK64;
1389 flags |= MPTCP_DSS_ACK64;
1390 } else {
1391 len += TCPOLEN_MPTCP_DSS_ACK32;
1392 }
1393 }
1394
1395 if (mpext->use_map) {
1396 len += TCPOLEN_MPTCP_DSS_MAP64;
1397
1398 /* Use only 64-bit mapping flags for now, add
1399 * support for optional 32-bit mappings later.
1400 */
1401 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1402 if (mpext->data_fin)
1403 flags |= MPTCP_DSS_DATA_FIN;
1404
1405 if (opts->csum_reqd)
1406 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1407 }
1408
1409 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1410
1411 if (mpext->use_ack) {
1412 if (mpext->ack64) {
1413 put_unaligned_be64(mpext->data_ack, ptr);
1414 ptr += 2;
1415 } else {
1416 put_unaligned_be32(mpext->data_ack32, ptr);
1417 ptr += 1;
1418 }
1419 }
1420
1421 if (mpext->use_map) {
1422 put_unaligned_be64(mpext->data_seq, ptr);
1423 ptr += 2;
1424 put_unaligned_be32(mpext->subflow_seq, ptr);
1425 ptr += 1;
1426 if (opts->csum_reqd) {
1427 /* data_len == 0 is reserved for the infinite mapping,
1428 * the checksum will also be set to 0.
1429 */
1430 put_len_csum(mpext->data_len,
1431 (mpext->data_len ? mptcp_make_csum(mpext) : 0),
1432 ptr);
1433 } else {
1434 put_unaligned_be32(mpext->data_len << 16 |
1435 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1436 }
1437 ptr += 1;
1438 }
1439
1440 /* We might need to add MP_FAIL options in rare cases */
1441 if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1442 goto mp_fail;
1443 } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1444 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1445
1446 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1447 len = TCPOLEN_MPTCP_MPC_SYN;
1448 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1449 len = TCPOLEN_MPTCP_MPC_SYNACK;
1450 } else if (opts->data_len) {
1451 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1452 if (opts->csum_reqd)
1453 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1454 } else {
1455 len = TCPOLEN_MPTCP_MPC_ACK;
1456 }
1457
1458 if (opts->csum_reqd)
1459 flag |= MPTCP_CAP_CHECKSUM_REQD;
1460
1461 if (!opts->allow_join_id0)
1462 flag |= MPTCP_CAP_DENY_JOIN_ID0;
1463
1464 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1465 MPTCP_SUPPORTED_VERSION,
1466 flag);
1467
1468 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1469 opts->suboptions))
1470 goto mp_capable_done;
1471
1472 put_unaligned_be64(opts->sndr_key, ptr);
1473 ptr += 2;
1474 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1475 goto mp_capable_done;
1476
1477 put_unaligned_be64(opts->rcvr_key, ptr);
1478 ptr += 2;
1479 if (!opts->data_len)
1480 goto mp_capable_done;
1481
1482 if (opts->csum_reqd) {
1483 put_len_csum(opts->data_len,
1484 __mptcp_make_csum(opts->data_seq,
1485 opts->subflow_seq,
1486 opts->data_len,
1487 ~csum_unfold(opts->csum)),
1488 ptr);
1489 } else {
1490 put_unaligned_be32(opts->data_len << 16 |
1491 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1492 }
1493 ptr += 1;
1494
1495 /* MPC is additionally mutually exclusive with MP_PRIO */
1496 goto mp_capable_done;
1497 } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
1498 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1499 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1500 TCPOLEN_MPTCP_MPJ_SYN,
1501 opts->backup, opts->join_id);
1502 put_unaligned_be32(opts->token, ptr);
1503 ptr += 1;
1504 put_unaligned_be32(opts->nonce, ptr);
1505 ptr += 1;
1506 } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1507 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1508 TCPOLEN_MPTCP_MPJ_SYNACK,
1509 opts->backup, opts->join_id);
1510 put_unaligned_be64(opts->thmac, ptr);
1511 ptr += 2;
1512 put_unaligned_be32(opts->nonce, ptr);
1513 ptr += 1;
1514 } else {
1515 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1516 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1517 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1518 ptr += 5;
1519 }
1520 } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1521 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1522 u8 echo = MPTCP_ADDR_ECHO;
1523
1524#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1525 if (opts->addr.family == AF_INET6)
1526 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1527#endif
1528
1529 if (opts->addr.port)
1530 len += TCPOLEN_MPTCP_PORT_LEN;
1531
1532 if (opts->ahmac) {
1533 len += sizeof(opts->ahmac);
1534 echo = 0;
1535 }
1536
1537 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1538 len, echo, opts->addr.id);
1539 if (opts->addr.family == AF_INET) {
1540 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1541 ptr += 1;
1542 }
1543#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1544 else if (opts->addr.family == AF_INET6) {
1545 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1546 ptr += 4;
1547 }
1548#endif
1549
1550 if (!opts->addr.port) {
1551 if (opts->ahmac) {
1552 put_unaligned_be64(opts->ahmac, ptr);
1553 ptr += 2;
1554 }
1555 } else {
1556 u16 port = ntohs(opts->addr.port);
1557
1558 if (opts->ahmac) {
1559 u8 *bptr = (u8 *)ptr;
1560
1561 put_unaligned_be16(port, bptr);
1562 bptr += 2;
1563 put_unaligned_be64(opts->ahmac, bptr);
1564 bptr += 8;
1565 put_unaligned_be16(TCPOPT_NOP << 8 |
1566 TCPOPT_NOP, bptr);
1567
1568 ptr += 3;
1569 } else {
1570 put_unaligned_be32(port << 16 |
1571 TCPOPT_NOP << 8 |
1572 TCPOPT_NOP, ptr);
1573 ptr += 1;
1574 }
1575 }
1576 } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1577 /* FASTCLOSE is mutually exclusive with others except RST */
1578 *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1579 TCPOLEN_MPTCP_FASTCLOSE,
1580 0, 0);
1581 put_unaligned_be64(opts->rcvr_key, ptr);
1582 ptr += 2;
1583
1584 if (OPTION_MPTCP_RST & opts->suboptions)
1585 goto mp_rst;
1586 return;
1587 } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1588mp_fail:
1589 /* MP_FAIL is mutually exclusive with others except RST */
1590 subflow = mptcp_subflow_ctx(ssk);
1591 subflow->send_mp_fail = 0;
1592
1593 *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1594 TCPOLEN_MPTCP_FAIL,
1595 0, 0);
1596 put_unaligned_be64(opts->fail_seq, ptr);
1597 ptr += 2;
1598
1599 if (OPTION_MPTCP_RST & opts->suboptions)
1600 goto mp_rst;
1601 return;
1602 } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1603mp_rst:
1604 *ptr++ = mptcp_option(MPTCPOPT_RST,
1605 TCPOLEN_MPTCP_RST,
1606 opts->reset_transient,
1607 opts->reset_reason);
1608 return;
1609 }
1610
1611 if (OPTION_MPTCP_PRIO & opts->suboptions) {
1612 subflow = mptcp_subflow_ctx(ssk);
1613 subflow->send_mp_prio = 0;
1614
1615 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1616 TCPOLEN_MPTCP_PRIO,
1617 opts->backup, TCPOPT_NOP);
1618
1619 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
1620 }
1621
1622mp_capable_done:
1623 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1624 u8 i = 1;
1625
1626 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1627 TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1628 0, opts->rm_list.ids[0]);
1629
1630 while (i < opts->rm_list.nr) {
1631 u8 id1, id2, id3, id4;
1632
1633 id1 = opts->rm_list.ids[i];
1634 id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1635 id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1636 id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1637 put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1638 ptr += 1;
1639 i += 4;
1640 }
1641 }
1642
1643 if (tp)
1644 mptcp_set_rwin(tp, th);
1645}
1646
1647__be32 mptcp_get_reset_option(const struct sk_buff *skb)
1648{
1649 const struct mptcp_ext *ext = mptcp_get_ext(skb);
1650 u8 flags, reason;
1651
1652 if (ext) {
1653 flags = ext->reset_transient;
1654 reason = ext->reset_reason;
1655
1656 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1657 flags, reason);
1658 }
1659
1660 return htonl(0u);
1661}
1662EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
1// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
7#define pr_fmt(fmt) "MPTCP: " fmt
8
9#include <linux/kernel.h>
10#include <crypto/sha2.h>
11#include <net/tcp.h>
12#include <net/mptcp.h>
13#include "protocol.h"
14#include "mib.h"
15
16#include <trace/events/mptcp.h>
17
18static bool mptcp_cap_flag_sha256(u8 flags)
19{
20 return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
21}
22
23static void mptcp_parse_option(const struct sk_buff *skb,
24 const unsigned char *ptr, int opsize,
25 struct mptcp_options_received *mp_opt)
26{
27 u8 subtype = *ptr >> 4;
28 int expected_opsize;
29 u16 subopt;
30 u8 version;
31 u8 flags;
32 u8 i;
33
34 switch (subtype) {
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;
40 else
41 expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
42 subopt = OPTION_MPTCP_MPC_ACK;
43 } else {
44 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) {
45 expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
46 subopt = OPTION_MPTCP_MPC_SYNACK;
47 } else {
48 expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
49 subopt = OPTION_MPTCP_MPC_SYN;
50 }
51 }
52
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
59 * broken
60 * We parse even option with mismatching csum presence, so that
61 * later in subflow_data_ready we can trigger the reset.
62 */
63 if (opsize != expected_opsize &&
64 (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
65 opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
66 break;
67
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)
72 break;
73 } else if (version < MPTCP_SUPPORTED_VERSION) {
74 break;
75 }
76
77 flags = *ptr++;
78 if (!mptcp_cap_flag_sha256(flags) ||
79 (flags & MPTCP_CAP_EXTENSIBILITY))
80 break;
81
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."
87 */
88 if (flags & MPTCP_CAP_CHECKSUM_REQD)
89 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
90
91 mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
92
93 mp_opt->suboptions |= subopt;
94 if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
95 mp_opt->sndr_key = get_unaligned_be64(ptr);
96 ptr += 8;
97 }
98 if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
99 mp_opt->rcvr_key = get_unaligned_be64(ptr);
100 ptr += 8;
101 }
102 if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
103 /* Section 3.1.:
104 * "the data parameters in a MP_CAPABLE are semantically
105 * equivalent to those in a DSS option and can be used
106 * interchangeably."
107 */
108 mp_opt->suboptions |= OPTION_MPTCP_DSS;
109 mp_opt->use_map = 1;
110 mp_opt->mpc_map = 1;
111 mp_opt->data_len = get_unaligned_be16(ptr);
112 ptr += 2;
113 }
114 if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
115 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
116 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
117 ptr += 2;
118 }
119 pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
120 version, flags, opsize, mp_opt->sndr_key,
121 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
122 break;
123
124 case MPTCPOPT_MP_JOIN:
125 mp_opt->suboptions |= OPTIONS_MPTCP_MPJ;
126 if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
127 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
128 mp_opt->join_id = *ptr++;
129 mp_opt->token = get_unaligned_be32(ptr);
130 ptr += 4;
131 mp_opt->nonce = get_unaligned_be32(ptr);
132 ptr += 4;
133 pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
134 mp_opt->backup, mp_opt->join_id,
135 mp_opt->token, mp_opt->nonce);
136 } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
137 mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
138 mp_opt->join_id = *ptr++;
139 mp_opt->thmac = get_unaligned_be64(ptr);
140 ptr += 8;
141 mp_opt->nonce = get_unaligned_be32(ptr);
142 ptr += 4;
143 pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
144 mp_opt->backup, mp_opt->join_id,
145 mp_opt->thmac, mp_opt->nonce);
146 } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
147 ptr += 2;
148 memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
149 pr_debug("MP_JOIN hmac");
150 } else {
151 mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ;
152 }
153 break;
154
155 case MPTCPOPT_DSS:
156 pr_debug("DSS");
157 ptr++;
158
159 /* we must clear 'mpc_map' be able to detect MP_CAPABLE
160 * map vs DSS map in mptcp_incoming_options(), and reconstruct
161 * map info accordingly
162 */
163 mp_opt->mpc_map = 0;
164 flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
165 mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
166 mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
167 mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
168 mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
169 mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
170
171 pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
172 mp_opt->data_fin, mp_opt->dsn64,
173 mp_opt->use_map, mp_opt->ack64,
174 mp_opt->use_ack);
175
176 expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
177
178 if (mp_opt->use_ack) {
179 if (mp_opt->ack64)
180 expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
181 else
182 expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
183 }
184
185 if (mp_opt->use_map) {
186 if (mp_opt->dsn64)
187 expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
188 else
189 expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
190 }
191
192 /* Always parse any csum presence combination, we will enforce
193 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
194 */
195 if (opsize != expected_opsize &&
196 opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
197 break;
198
199 mp_opt->suboptions |= OPTION_MPTCP_DSS;
200 if (mp_opt->use_ack) {
201 if (mp_opt->ack64) {
202 mp_opt->data_ack = get_unaligned_be64(ptr);
203 ptr += 8;
204 } else {
205 mp_opt->data_ack = get_unaligned_be32(ptr);
206 ptr += 4;
207 }
208
209 pr_debug("data_ack=%llu", mp_opt->data_ack);
210 }
211
212 if (mp_opt->use_map) {
213 if (mp_opt->dsn64) {
214 mp_opt->data_seq = get_unaligned_be64(ptr);
215 ptr += 8;
216 } else {
217 mp_opt->data_seq = get_unaligned_be32(ptr);
218 ptr += 4;
219 }
220
221 mp_opt->subflow_seq = get_unaligned_be32(ptr);
222 ptr += 4;
223
224 mp_opt->data_len = get_unaligned_be16(ptr);
225 ptr += 2;
226
227 if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
228 mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
229 mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
230 ptr += 2;
231 }
232
233 pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
234 mp_opt->data_seq, mp_opt->subflow_seq,
235 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
236 mp_opt->csum);
237 }
238
239 break;
240
241 case MPTCPOPT_ADD_ADDR:
242 mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
243 if (!mp_opt->echo) {
244 if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
245 opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
246 mp_opt->addr.family = AF_INET;
247#if IS_ENABLED(CONFIG_MPTCP_IPV6)
248 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
249 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
250 mp_opt->addr.family = AF_INET6;
251#endif
252 else
253 break;
254 } else {
255 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
256 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
257 mp_opt->addr.family = AF_INET;
258#if IS_ENABLED(CONFIG_MPTCP_IPV6)
259 else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
260 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
261 mp_opt->addr.family = AF_INET6;
262#endif
263 else
264 break;
265 }
266
267 mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
268 mp_opt->addr.id = *ptr++;
269 mp_opt->addr.port = 0;
270 mp_opt->ahmac = 0;
271 if (mp_opt->addr.family == AF_INET) {
272 memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
273 ptr += 4;
274 if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
275 opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
276 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
277 ptr += 2;
278 }
279 }
280#if IS_ENABLED(CONFIG_MPTCP_IPV6)
281 else {
282 memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
283 ptr += 16;
284 if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
285 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
286 mp_opt->addr.port = htons(get_unaligned_be16(ptr));
287 ptr += 2;
288 }
289 }
290#endif
291 if (!mp_opt->echo) {
292 mp_opt->ahmac = get_unaligned_be64(ptr);
293 ptr += 8;
294 }
295 pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
296 (mp_opt->addr.family == AF_INET6) ? "6" : "",
297 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
298 break;
299
300 case MPTCPOPT_RM_ADDR:
301 if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
302 opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
303 break;
304
305 ptr++;
306
307 mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
308 mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
309 for (i = 0; i < mp_opt->rm_list.nr; i++)
310 mp_opt->rm_list.ids[i] = *ptr++;
311 pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
312 break;
313
314 case MPTCPOPT_MP_PRIO:
315 if (opsize != TCPOLEN_MPTCP_PRIO)
316 break;
317
318 mp_opt->suboptions |= OPTION_MPTCP_PRIO;
319 mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
320 pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
321 break;
322
323 case MPTCPOPT_MP_FASTCLOSE:
324 if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
325 break;
326
327 ptr += 2;
328 mp_opt->rcvr_key = get_unaligned_be64(ptr);
329 ptr += 8;
330 mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
331 pr_debug("MP_FASTCLOSE: recv_key=%llu", mp_opt->rcvr_key);
332 break;
333
334 case MPTCPOPT_RST:
335 if (opsize != TCPOLEN_MPTCP_RST)
336 break;
337
338 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
339 break;
340
341 mp_opt->suboptions |= OPTION_MPTCP_RST;
342 flags = *ptr++;
343 mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
344 mp_opt->reset_reason = *ptr;
345 pr_debug("MP_RST: transient=%u reason=%u",
346 mp_opt->reset_transient, mp_opt->reset_reason);
347 break;
348
349 case MPTCPOPT_MP_FAIL:
350 if (opsize != TCPOLEN_MPTCP_FAIL)
351 break;
352
353 ptr += 2;
354 mp_opt->suboptions |= OPTION_MPTCP_FAIL;
355 mp_opt->fail_seq = get_unaligned_be64(ptr);
356 pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
357 break;
358
359 default:
360 break;
361 }
362}
363
364void mptcp_get_options(const struct sk_buff *skb,
365 struct mptcp_options_received *mp_opt)
366{
367 const struct tcphdr *th = tcp_hdr(skb);
368 const unsigned char *ptr;
369 int length;
370
371 /* initialize option status */
372 mp_opt->suboptions = 0;
373
374 length = (th->doff * 4) - sizeof(struct tcphdr);
375 ptr = (const unsigned char *)(th + 1);
376
377 while (length > 0) {
378 int opcode = *ptr++;
379 int opsize;
380
381 switch (opcode) {
382 case TCPOPT_EOL:
383 return;
384 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
385 length--;
386 continue;
387 default:
388 if (length < 2)
389 return;
390 opsize = *ptr++;
391 if (opsize < 2) /* "silly options" */
392 return;
393 if (opsize > length)
394 return; /* don't parse partial options */
395 if (opcode == TCPOPT_MPTCP)
396 mptcp_parse_option(skb, ptr, opsize, mp_opt);
397 ptr += opsize - 2;
398 length -= opsize;
399 }
400 }
401}
402
403bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
404 unsigned int *size, struct mptcp_out_options *opts)
405{
406 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
407
408 /* we will use snd_isn to detect first pkt [re]transmission
409 * in mptcp_established_options_mp()
410 */
411 subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
412 if (subflow->request_mptcp) {
413 opts->suboptions = OPTION_MPTCP_MPC_SYN;
414 opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
415 opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
416 *size = TCPOLEN_MPTCP_MPC_SYN;
417 return true;
418 } else if (subflow->request_join) {
419 pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
420 subflow->local_nonce);
421 opts->suboptions = OPTION_MPTCP_MPJ_SYN;
422 opts->join_id = subflow->local_id;
423 opts->token = subflow->remote_token;
424 opts->nonce = subflow->local_nonce;
425 opts->backup = subflow->request_bkup;
426 *size = TCPOLEN_MPTCP_MPJ_SYN;
427 return true;
428 }
429 return false;
430}
431
432static void clear_3rdack_retransmission(struct sock *sk)
433{
434 struct inet_connection_sock *icsk = inet_csk(sk);
435
436 sk_stop_timer(sk, &icsk->icsk_delack_timer);
437 icsk->icsk_ack.timeout = 0;
438 icsk->icsk_ack.ato = 0;
439 icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
440}
441
442static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
443 bool snd_data_fin_enable,
444 unsigned int *size,
445 unsigned int remaining,
446 struct mptcp_out_options *opts)
447{
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;
452 u8 len;
453
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.
459 */
460 if (!skb)
461 return false;
462
463 /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
464 if (subflow->fully_established || snd_data_fin_enable ||
465 subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
466 sk->sk_state != TCP_ESTABLISHED)
467 return false;
468
469 if (subflow->mp_capable) {
470 mpext = mptcp_get_ext(skb);
471 data_len = mpext ? mpext->data_len : 0;
472
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
476 */
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));
483
484 /* Section 3.1.
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
488 */
489 if (data_len > 0) {
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;
497 }
498 *size = ALIGN(len, 4);
499 } else {
500 *size = TCPOLEN_MPTCP_MPC_ACK;
501 }
502
503 pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
504 subflow, subflow->local_key, subflow->remote_key,
505 data_len);
506
507 return true;
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", subflow);
513
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
518 */
519 if (sock_owned_by_user(sk))
520 set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
521 else
522 mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
523 return true;
524 }
525 return false;
526}
527
528static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
529 struct sk_buff *skb, struct mptcp_ext *ext)
530{
531 /* The write_seq value has already been incremented, so the actual
532 * sequence number for the DATA_FIN is one less.
533 */
534 u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
535
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
539 */
540 ext->data_fin = 1;
541 ext->use_map = 1;
542 ext->dsn64 = 1;
543 ext->data_seq = data_fin_tx_seq;
544 ext->subflow_seq = 0;
545 ext->data_len = 1;
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
549 * mapping space.
550 */
551 ext->data_fin = 1;
552 ext->data_len++;
553 }
554}
555
556static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
557 bool snd_data_fin_enable,
558 unsigned int *size,
559 unsigned int remaining,
560 struct mptcp_out_options *opts)
561{
562 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
563 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
564 unsigned int dss_size = 0;
565 struct mptcp_ext *mpext;
566 unsigned int ack_size;
567 bool ret = false;
568 u64 ack_seq;
569
570 opts->csum_reqd = READ_ONCE(msk->csum_enabled);
571 mpext = skb ? mptcp_get_ext(skb) : NULL;
572
573 if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
574 unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
575
576 if (mpext) {
577 if (opts->csum_reqd)
578 map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
579
580 opts->ext_copy = *mpext;
581 }
582
583 remaining -= map_size;
584 dss_size = map_size;
585 if (skb && snd_data_fin_enable)
586 mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
587 opts->suboptions = OPTION_MPTCP_DSS;
588 ret = true;
589 }
590
591 /* passive sockets msk will set the 'can_ack' after accept(), even
592 * if the first subflow may have the already the remote key handy
593 */
594 opts->ext_copy.use_ack = 0;
595 if (!READ_ONCE(msk->can_ack)) {
596 *size = ALIGN(dss_size, 4);
597 return ret;
598 }
599
600 ack_seq = READ_ONCE(msk->ack_seq);
601 if (READ_ONCE(msk->use_64bit_ack)) {
602 ack_size = TCPOLEN_MPTCP_DSS_ACK64;
603 opts->ext_copy.data_ack = ack_seq;
604 opts->ext_copy.ack64 = 1;
605 } else {
606 ack_size = TCPOLEN_MPTCP_DSS_ACK32;
607 opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
608 opts->ext_copy.ack64 = 0;
609 }
610 opts->ext_copy.use_ack = 1;
611 opts->suboptions = OPTION_MPTCP_DSS;
612 WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
613
614 /* Add kind/length/subtype/flag overhead if mapping is not populated */
615 if (dss_size == 0)
616 ack_size += TCPOLEN_MPTCP_DSS_BASE;
617
618 dss_size += ack_size;
619
620 *size = ALIGN(dss_size, 4);
621 return true;
622}
623
624static u64 add_addr_generate_hmac(u64 key1, u64 key2,
625 struct mptcp_addr_info *addr)
626{
627 u16 port = ntohs(addr->port);
628 u8 hmac[SHA256_DIGEST_SIZE];
629 u8 msg[19];
630 int i = 0;
631
632 msg[i++] = addr->id;
633 if (addr->family == AF_INET) {
634 memcpy(&msg[i], &addr->addr.s_addr, 4);
635 i += 4;
636 }
637#if IS_ENABLED(CONFIG_MPTCP_IPV6)
638 else if (addr->family == AF_INET6) {
639 memcpy(&msg[i], &addr->addr6.s6_addr, 16);
640 i += 16;
641 }
642#endif
643 msg[i++] = port >> 8;
644 msg[i++] = port & 0xFF;
645
646 mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
647
648 return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
649}
650
651static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
652 unsigned int *size,
653 unsigned int remaining,
654 struct mptcp_out_options *opts)
655{
656 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
657 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
658 bool drop_other_suboptions = false;
659 unsigned int opt_size = *size;
660 bool echo;
661 int len;
662
663 /* add addr will strip the existing options, be sure to avoid breaking
664 * MPC/MPJ handshakes
665 */
666 if (!mptcp_pm_should_add_signal(msk) ||
667 (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
668 !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
669 &echo, &drop_other_suboptions))
670 return false;
671
672 if (drop_other_suboptions)
673 remaining += opt_size;
674 len = mptcp_add_addr_len(opts->addr.family, echo, !!opts->addr.port);
675 if (remaining < len)
676 return false;
677
678 *size = len;
679 if (drop_other_suboptions) {
680 pr_debug("drop other suboptions");
681 opts->suboptions = 0;
682
683 /* note that e.g. DSS could have written into the memory
684 * aliased by ahmac, we must reset the field here
685 * to avoid appending the hmac even for ADD_ADDR echo
686 * options
687 */
688 opts->ahmac = 0;
689 *size -= opt_size;
690 }
691 opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
692 if (!echo) {
693 opts->ahmac = add_addr_generate_hmac(msk->local_key,
694 msk->remote_key,
695 &opts->addr);
696 }
697 pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
698 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
699
700 return true;
701}
702
703static bool mptcp_established_options_rm_addr(struct sock *sk,
704 unsigned int *size,
705 unsigned int remaining,
706 struct mptcp_out_options *opts)
707{
708 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
709 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
710 struct mptcp_rm_list rm_list;
711 int i, len;
712
713 if (!mptcp_pm_should_rm_signal(msk) ||
714 !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
715 return false;
716
717 len = mptcp_rm_addr_len(&rm_list);
718 if (len < 0)
719 return false;
720 if (remaining < len)
721 return false;
722
723 *size = len;
724 opts->suboptions |= OPTION_MPTCP_RM_ADDR;
725 opts->rm_list = rm_list;
726
727 for (i = 0; i < opts->rm_list.nr; i++)
728 pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
729
730 return true;
731}
732
733static bool mptcp_established_options_mp_prio(struct sock *sk,
734 unsigned int *size,
735 unsigned int remaining,
736 struct mptcp_out_options *opts)
737{
738 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
739
740 /* can't send MP_PRIO with MPC, as they share the same option space:
741 * 'backup'. Also it makes no sense at all
742 */
743 if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
744 return false;
745
746 /* account for the trailing 'nop' option */
747 if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
748 return false;
749
750 *size = TCPOLEN_MPTCP_PRIO_ALIGN;
751 opts->suboptions |= OPTION_MPTCP_PRIO;
752 opts->backup = subflow->request_bkup;
753
754 pr_debug("prio=%d", opts->backup);
755
756 return true;
757}
758
759static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
760 unsigned int *size,
761 unsigned int remaining,
762 struct mptcp_out_options *opts)
763{
764 const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
765
766 if (remaining < TCPOLEN_MPTCP_RST)
767 return false;
768
769 *size = TCPOLEN_MPTCP_RST;
770 opts->suboptions |= OPTION_MPTCP_RST;
771 opts->reset_transient = subflow->reset_transient;
772 opts->reset_reason = subflow->reset_reason;
773 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
774
775 return true;
776}
777
778static bool mptcp_established_options_fastclose(struct sock *sk,
779 unsigned int *size,
780 unsigned int remaining,
781 struct mptcp_out_options *opts)
782{
783 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
784 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
785
786 if (likely(!subflow->send_fastclose))
787 return false;
788
789 if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
790 return false;
791
792 *size = TCPOLEN_MPTCP_FASTCLOSE;
793 opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
794 opts->rcvr_key = msk->remote_key;
795
796 pr_debug("FASTCLOSE key=%llu", opts->rcvr_key);
797 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
798 return true;
799}
800
801static bool mptcp_established_options_mp_fail(struct sock *sk,
802 unsigned int *size,
803 unsigned int remaining,
804 struct mptcp_out_options *opts)
805{
806 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
807
808 if (likely(!subflow->send_mp_fail))
809 return false;
810
811 if (remaining < TCPOLEN_MPTCP_FAIL)
812 return false;
813
814 *size = TCPOLEN_MPTCP_FAIL;
815 opts->suboptions |= OPTION_MPTCP_FAIL;
816 opts->fail_seq = subflow->map_seq;
817
818 pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
819 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
820
821 return true;
822}
823
824bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
825 unsigned int *size, unsigned int remaining,
826 struct mptcp_out_options *opts)
827{
828 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
829 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
830 unsigned int opt_size = 0;
831 bool snd_data_fin;
832 bool ret = false;
833
834 opts->suboptions = 0;
835
836 if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
837 return false;
838
839 if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
840 if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
841 mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
842 *size += opt_size;
843 remaining -= opt_size;
844 }
845 /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
846 if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
847 *size += opt_size;
848 remaining -= opt_size;
849 }
850 return true;
851 }
852
853 snd_data_fin = mptcp_data_fin_enabled(msk);
854 if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
855 ret = true;
856 else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) {
857 unsigned int mp_fail_size;
858
859 ret = true;
860 if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
861 remaining - opt_size, opts)) {
862 *size += opt_size + mp_fail_size;
863 remaining -= opt_size - mp_fail_size;
864 return true;
865 }
866 }
867
868 /* we reserved enough space for the above options, and exceeding the
869 * TCP option space would be fatal
870 */
871 if (WARN_ON_ONCE(opt_size > remaining))
872 return false;
873
874 *size += opt_size;
875 remaining -= opt_size;
876 if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
877 *size += opt_size;
878 remaining -= opt_size;
879 ret = true;
880 } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
881 *size += opt_size;
882 remaining -= opt_size;
883 ret = true;
884 }
885
886 if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
887 *size += opt_size;
888 remaining -= opt_size;
889 ret = true;
890 }
891
892 return ret;
893}
894
895bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
896 struct mptcp_out_options *opts)
897{
898 struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
899
900 if (subflow_req->mp_capable) {
901 opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
902 opts->sndr_key = subflow_req->local_key;
903 opts->csum_reqd = subflow_req->csum_reqd;
904 opts->allow_join_id0 = subflow_req->allow_join_id0;
905 *size = TCPOLEN_MPTCP_MPC_SYNACK;
906 pr_debug("subflow_req=%p, local_key=%llu",
907 subflow_req, subflow_req->local_key);
908 return true;
909 } else if (subflow_req->mp_join) {
910 opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
911 opts->backup = subflow_req->backup;
912 opts->join_id = subflow_req->local_id;
913 opts->thmac = subflow_req->thmac;
914 opts->nonce = subflow_req->local_nonce;
915 pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
916 subflow_req, opts->backup, opts->join_id,
917 opts->thmac, opts->nonce);
918 *size = TCPOLEN_MPTCP_MPJ_SYNACK;
919 return true;
920 }
921 return false;
922}
923
924static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
925 struct mptcp_subflow_context *subflow,
926 struct sk_buff *skb,
927 struct mptcp_options_received *mp_opt)
928{
929 /* here we can process OoO, in-window pkts, only in-sequence 4th ack
930 * will make the subflow fully established
931 */
932 if (likely(subflow->fully_established)) {
933 /* on passive sockets, check for 3rd ack retransmission
934 * note that msk is always set by subflow_syn_recv_sock()
935 * for mp_join subflows
936 */
937 if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
938 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
939 subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
940 !subflow->request_join)
941 tcp_send_ack(ssk);
942 goto check_notify;
943 }
944
945 /* we must process OoO packets before the first subflow is fully
946 * established. OoO packets are instead a protocol violation
947 * for MP_JOIN subflows as the peer must not send any data
948 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
949 */
950 if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
951 if (subflow->mp_join)
952 goto reset;
953 if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
954 goto set_fully_established;
955 return subflow->mp_capable;
956 }
957
958 if (subflow->remote_key_valid &&
959 (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
960 ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo))) {
961 /* subflows are fully established as soon as we get any
962 * additional ack, including ADD_ADDR.
963 */
964 subflow->fully_established = 1;
965 WRITE_ONCE(msk->fully_established, true);
966 goto check_notify;
967 }
968
969 /* If the first established packet does not contain MP_CAPABLE + data
970 * then fallback to TCP. Fallback scenarios requires a reset for
971 * MP_JOIN subflows.
972 */
973 if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
974 if (subflow->mp_join)
975 goto reset;
976 subflow->mp_capable = 0;
977 pr_fallback(msk);
978 mptcp_do_fallback(ssk);
979 return false;
980 }
981
982 if (mp_opt->deny_join_id0)
983 WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
984
985set_fully_established:
986 if (unlikely(!READ_ONCE(msk->pm.server_side)))
987 pr_warn_once("bogus mpc option on established client sk");
988 mptcp_subflow_fully_established(subflow, mp_opt);
989
990check_notify:
991 /* if the subflow is not already linked into the conn_list, we can't
992 * notify the PM: this subflow is still on the listener queue
993 * and the PM possibly acquiring the subflow lock could race with
994 * the listener close
995 */
996 if (likely(subflow->pm_notified) || list_empty(&subflow->node))
997 return true;
998
999 subflow->pm_notified = 1;
1000 if (subflow->mp_join) {
1001 clear_3rdack_retransmission(ssk);
1002 mptcp_pm_subflow_established(msk);
1003 } else {
1004 mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
1005 }
1006 return true;
1007
1008reset:
1009 mptcp_subflow_reset(ssk);
1010 return false;
1011}
1012
1013u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1014{
1015 u32 old_seq32, cur_seq32;
1016
1017 old_seq32 = (u32)old_seq;
1018 cur_seq32 = (u32)cur_seq;
1019 cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
1020 if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
1021 return cur_seq + (1LL << 32);
1022
1023 /* reverse wrap could happen, too */
1024 if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
1025 return cur_seq - (1LL << 32);
1026 return cur_seq;
1027}
1028
1029static void ack_update_msk(struct mptcp_sock *msk,
1030 struct sock *ssk,
1031 struct mptcp_options_received *mp_opt)
1032{
1033 u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1034 struct sock *sk = (struct sock *)msk;
1035 u64 old_snd_una;
1036
1037 mptcp_data_lock(sk);
1038
1039 /* avoid ack expansion on update conflict, to reduce the risk of
1040 * wrongly expanding to a future ack sequence number, which is way
1041 * more dangerous than missing an ack
1042 */
1043 old_snd_una = msk->snd_una;
1044 new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1045
1046 /* ACK for data not even sent yet? Ignore.*/
1047 if (unlikely(after64(new_snd_una, snd_nxt)))
1048 new_snd_una = old_snd_una;
1049
1050 new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1051
1052 if (after64(new_wnd_end, msk->wnd_end))
1053 msk->wnd_end = new_wnd_end;
1054
1055 /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1056 if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1057 __mptcp_check_push(sk, ssk);
1058
1059 if (after64(new_snd_una, old_snd_una)) {
1060 msk->snd_una = new_snd_una;
1061 __mptcp_data_acked(sk);
1062 }
1063 mptcp_data_unlock(sk);
1064
1065 trace_ack_update_msk(mp_opt->data_ack,
1066 old_snd_una, new_snd_una,
1067 new_wnd_end, msk->wnd_end);
1068}
1069
1070bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1071{
1072 /* Skip if DATA_FIN was already received.
1073 * If updating simultaneously with the recvmsg loop, values
1074 * should match. If they mismatch, the peer is misbehaving and
1075 * we will prefer the most recent information.
1076 */
1077 if (READ_ONCE(msk->rcv_data_fin))
1078 return false;
1079
1080 WRITE_ONCE(msk->rcv_data_fin_seq,
1081 mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1082 WRITE_ONCE(msk->rcv_data_fin, 1);
1083
1084 return true;
1085}
1086
1087static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1088 struct mptcp_options_received *mp_opt)
1089{
1090 u64 hmac = 0;
1091
1092 if (mp_opt->echo)
1093 return true;
1094
1095 hmac = add_addr_generate_hmac(msk->remote_key,
1096 msk->local_key,
1097 &mp_opt->addr);
1098
1099 pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1100 msk, hmac, mp_opt->ahmac);
1101
1102 return hmac == mp_opt->ahmac;
1103}
1104
1105/* Return false if a subflow has been reset, else return true */
1106bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1107{
1108 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1109 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1110 struct mptcp_options_received mp_opt;
1111 struct mptcp_ext *mpext;
1112
1113 if (__mptcp_check_fallback(msk)) {
1114 /* Keep it simple and unconditionally trigger send data cleanup and
1115 * pending queue spooling. We will need to acquire the data lock
1116 * for more accurate checks, and once the lock is acquired, such
1117 * helpers are cheap.
1118 */
1119 mptcp_data_lock(subflow->conn);
1120 if (sk_stream_memory_free(sk))
1121 __mptcp_check_push(subflow->conn, sk);
1122 __mptcp_data_acked(subflow->conn);
1123 mptcp_data_unlock(subflow->conn);
1124 return true;
1125 }
1126
1127 mptcp_get_options(skb, &mp_opt);
1128
1129 /* The subflow can be in close state only if check_fully_established()
1130 * just sent a reset. If so, tell the caller to ignore the current packet.
1131 */
1132 if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1133 return sk->sk_state != TCP_CLOSE;
1134
1135 if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1136 if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1137 msk->local_key == mp_opt.rcvr_key) {
1138 WRITE_ONCE(msk->rcv_fastclose, true);
1139 mptcp_schedule_work((struct sock *)msk);
1140 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
1141 }
1142
1143 if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1144 add_addr_hmac_valid(msk, &mp_opt)) {
1145 if (!mp_opt.echo) {
1146 mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1147 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1148 } else {
1149 mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1150 mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1151 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1152 }
1153
1154 if (mp_opt.addr.port)
1155 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1156 }
1157
1158 if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1159 mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1160
1161 if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1162 mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1163 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1164 }
1165
1166 if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1167 mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1168 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1169 }
1170
1171 if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1172 subflow->reset_seen = 1;
1173 subflow->reset_reason = mp_opt.reset_reason;
1174 subflow->reset_transient = mp_opt.reset_transient;
1175 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1176 }
1177
1178 if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1179 return true;
1180 }
1181
1182 /* we can't wait for recvmsg() to update the ack_seq, otherwise
1183 * monodirectional flows will stuck
1184 */
1185 if (mp_opt.use_ack)
1186 ack_update_msk(msk, sk, &mp_opt);
1187
1188 /* Zero-data-length packets are dropped by the caller and not
1189 * propagated to the MPTCP layer, so the skb extension does not
1190 * need to be allocated or populated. DATA_FIN information, if
1191 * present, needs to be updated here before the skb is freed.
1192 */
1193 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1194 if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1195 mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
1196 schedule_work(&msk->work))
1197 sock_hold(subflow->conn);
1198
1199 return true;
1200 }
1201
1202 mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1203 if (!mpext)
1204 return true;
1205
1206 memset(mpext, 0, sizeof(*mpext));
1207
1208 if (likely(mp_opt.use_map)) {
1209 if (mp_opt.mpc_map) {
1210 /* this is an MP_CAPABLE carrying MPTCP data
1211 * we know this map the first chunk of data
1212 */
1213 mptcp_crypto_key_sha(subflow->remote_key, NULL,
1214 &mpext->data_seq);
1215 mpext->data_seq++;
1216 mpext->subflow_seq = 1;
1217 mpext->dsn64 = 1;
1218 mpext->mpc_map = 1;
1219 mpext->data_fin = 0;
1220 } else {
1221 mpext->data_seq = mp_opt.data_seq;
1222 mpext->subflow_seq = mp_opt.subflow_seq;
1223 mpext->dsn64 = mp_opt.dsn64;
1224 mpext->data_fin = mp_opt.data_fin;
1225 }
1226 mpext->data_len = mp_opt.data_len;
1227 mpext->use_map = 1;
1228 mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1229
1230 if (mpext->csum_reqd)
1231 mpext->csum = mp_opt.csum;
1232 }
1233
1234 return true;
1235}
1236
1237static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
1238{
1239 const struct sock *ssk = (const struct sock *)tp;
1240 struct mptcp_subflow_context *subflow;
1241 u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
1242 struct mptcp_sock *msk;
1243 u32 new_win;
1244 u64 win;
1245
1246 subflow = mptcp_subflow_ctx(ssk);
1247 msk = mptcp_sk(subflow->conn);
1248
1249 ack_seq = READ_ONCE(msk->ack_seq);
1250 rcv_wnd_new = ack_seq + tp->rcv_wnd;
1251
1252 rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1253 if (after64(rcv_wnd_new, rcv_wnd_old)) {
1254 u64 rcv_wnd;
1255
1256 for (;;) {
1257 rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1258
1259 if (rcv_wnd == rcv_wnd_old)
1260 break;
1261 if (before64(rcv_wnd_new, rcv_wnd)) {
1262 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1263 goto raise_win;
1264 }
1265 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1266 rcv_wnd_old = rcv_wnd;
1267 }
1268 return;
1269 }
1270
1271 if (rcv_wnd_new != rcv_wnd_old) {
1272raise_win:
1273 win = rcv_wnd_old - ack_seq;
1274 tp->rcv_wnd = min_t(u64, win, U32_MAX);
1275 new_win = tp->rcv_wnd;
1276
1277 /* Make sure we do not exceed the maximum possible
1278 * scaled window.
1279 */
1280 if (unlikely(th->syn))
1281 new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1282 if (!tp->rx_opt.rcv_wscale &&
1283 READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1284 new_win = min(new_win, MAX_TCP_WINDOW);
1285 else
1286 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1287
1288 /* RFC1323 scaling applied */
1289 new_win >>= tp->rx_opt.rcv_wscale;
1290 th->window = htons(new_win);
1291 MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1292 }
1293}
1294
1295__sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1296{
1297 struct csum_pseudo_header header;
1298 __wsum csum;
1299
1300 /* cfr RFC 8684 3.3.1.:
1301 * the data sequence number used in the pseudo-header is
1302 * always the 64-bit value, irrespective of what length is used in the
1303 * DSS option itself.
1304 */
1305 header.data_seq = cpu_to_be64(data_seq);
1306 header.subflow_seq = htonl(subflow_seq);
1307 header.data_len = htons(data_len);
1308 header.csum = 0;
1309
1310 csum = csum_partial(&header, sizeof(header), sum);
1311 return csum_fold(csum);
1312}
1313
1314static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1315{
1316 return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1317 ~csum_unfold(mpext->csum));
1318}
1319
1320static void put_len_csum(u16 len, __sum16 csum, void *data)
1321{
1322 __sum16 *sumptr = data + 2;
1323 __be16 *ptr = data;
1324
1325 put_unaligned_be16(len, ptr);
1326
1327 put_unaligned(csum, sumptr);
1328}
1329
1330void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1331 struct mptcp_out_options *opts)
1332{
1333 const struct sock *ssk = (const struct sock *)tp;
1334 struct mptcp_subflow_context *subflow;
1335
1336 /* Which options can be used together?
1337 *
1338 * X: mutually exclusive
1339 * O: often used together
1340 * C: can be used together in some cases
1341 * P: could be used together but we prefer not to (optimisations)
1342 *
1343 * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
1344 * ------|------|------|------|------|------|------|------|------|
1345 * MPC |------|------|------|------|------|------|------|------|
1346 * MPJ | X |------|------|------|------|------|------|------|
1347 * DSS | X | X |------|------|------|------|------|------|
1348 * ADD | X | X | P |------|------|------|------|------|
1349 * RM | C | C | C | P |------|------|------|------|
1350 * PRIO | X | C | C | C | C |------|------|------|
1351 * FAIL | X | X | C | X | X | X |------|------|
1352 * FC | X | X | X | X | X | X | X |------|
1353 * RST | X | X | X | X | X | X | O | O |
1354 * ------|------|------|------|------|------|------|------|------|
1355 *
1356 * The same applies in mptcp_established_options() function.
1357 */
1358 if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1359 struct mptcp_ext *mpext = &opts->ext_copy;
1360 u8 len = TCPOLEN_MPTCP_DSS_BASE;
1361 u8 flags = 0;
1362
1363 if (mpext->use_ack) {
1364 flags = MPTCP_DSS_HAS_ACK;
1365 if (mpext->ack64) {
1366 len += TCPOLEN_MPTCP_DSS_ACK64;
1367 flags |= MPTCP_DSS_ACK64;
1368 } else {
1369 len += TCPOLEN_MPTCP_DSS_ACK32;
1370 }
1371 }
1372
1373 if (mpext->use_map) {
1374 len += TCPOLEN_MPTCP_DSS_MAP64;
1375
1376 /* Use only 64-bit mapping flags for now, add
1377 * support for optional 32-bit mappings later.
1378 */
1379 flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1380 if (mpext->data_fin)
1381 flags |= MPTCP_DSS_DATA_FIN;
1382
1383 if (opts->csum_reqd)
1384 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1385 }
1386
1387 *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1388
1389 if (mpext->use_ack) {
1390 if (mpext->ack64) {
1391 put_unaligned_be64(mpext->data_ack, ptr);
1392 ptr += 2;
1393 } else {
1394 put_unaligned_be32(mpext->data_ack32, ptr);
1395 ptr += 1;
1396 }
1397 }
1398
1399 if (mpext->use_map) {
1400 put_unaligned_be64(mpext->data_seq, ptr);
1401 ptr += 2;
1402 put_unaligned_be32(mpext->subflow_seq, ptr);
1403 ptr += 1;
1404 if (opts->csum_reqd) {
1405 /* data_len == 0 is reserved for the infinite mapping,
1406 * the checksum will also be set to 0.
1407 */
1408 put_len_csum(mpext->data_len,
1409 (mpext->data_len ? mptcp_make_csum(mpext) : 0),
1410 ptr);
1411 } else {
1412 put_unaligned_be32(mpext->data_len << 16 |
1413 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1414 }
1415 ptr += 1;
1416 }
1417
1418 /* We might need to add MP_FAIL options in rare cases */
1419 if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1420 goto mp_fail;
1421 } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1422 u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1423
1424 if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1425 len = TCPOLEN_MPTCP_MPC_SYN;
1426 } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1427 len = TCPOLEN_MPTCP_MPC_SYNACK;
1428 } else if (opts->data_len) {
1429 len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1430 if (opts->csum_reqd)
1431 len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1432 } else {
1433 len = TCPOLEN_MPTCP_MPC_ACK;
1434 }
1435
1436 if (opts->csum_reqd)
1437 flag |= MPTCP_CAP_CHECKSUM_REQD;
1438
1439 if (!opts->allow_join_id0)
1440 flag |= MPTCP_CAP_DENY_JOIN_ID0;
1441
1442 *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1443 MPTCP_SUPPORTED_VERSION,
1444 flag);
1445
1446 if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1447 opts->suboptions))
1448 goto mp_capable_done;
1449
1450 put_unaligned_be64(opts->sndr_key, ptr);
1451 ptr += 2;
1452 if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1453 goto mp_capable_done;
1454
1455 put_unaligned_be64(opts->rcvr_key, ptr);
1456 ptr += 2;
1457 if (!opts->data_len)
1458 goto mp_capable_done;
1459
1460 if (opts->csum_reqd) {
1461 put_len_csum(opts->data_len,
1462 __mptcp_make_csum(opts->data_seq,
1463 opts->subflow_seq,
1464 opts->data_len,
1465 ~csum_unfold(opts->csum)),
1466 ptr);
1467 } else {
1468 put_unaligned_be32(opts->data_len << 16 |
1469 TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1470 }
1471 ptr += 1;
1472
1473 /* MPC is additionally mutually exclusive with MP_PRIO */
1474 goto mp_capable_done;
1475 } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
1476 if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1477 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1478 TCPOLEN_MPTCP_MPJ_SYN,
1479 opts->backup, opts->join_id);
1480 put_unaligned_be32(opts->token, ptr);
1481 ptr += 1;
1482 put_unaligned_be32(opts->nonce, ptr);
1483 ptr += 1;
1484 } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1485 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1486 TCPOLEN_MPTCP_MPJ_SYNACK,
1487 opts->backup, opts->join_id);
1488 put_unaligned_be64(opts->thmac, ptr);
1489 ptr += 2;
1490 put_unaligned_be32(opts->nonce, ptr);
1491 ptr += 1;
1492 } else {
1493 *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1494 TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1495 memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1496 ptr += 5;
1497 }
1498 } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1499 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1500 u8 echo = MPTCP_ADDR_ECHO;
1501
1502#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1503 if (opts->addr.family == AF_INET6)
1504 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1505#endif
1506
1507 if (opts->addr.port)
1508 len += TCPOLEN_MPTCP_PORT_LEN;
1509
1510 if (opts->ahmac) {
1511 len += sizeof(opts->ahmac);
1512 echo = 0;
1513 }
1514
1515 *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1516 len, echo, opts->addr.id);
1517 if (opts->addr.family == AF_INET) {
1518 memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1519 ptr += 1;
1520 }
1521#if IS_ENABLED(CONFIG_MPTCP_IPV6)
1522 else if (opts->addr.family == AF_INET6) {
1523 memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1524 ptr += 4;
1525 }
1526#endif
1527
1528 if (!opts->addr.port) {
1529 if (opts->ahmac) {
1530 put_unaligned_be64(opts->ahmac, ptr);
1531 ptr += 2;
1532 }
1533 } else {
1534 u16 port = ntohs(opts->addr.port);
1535
1536 if (opts->ahmac) {
1537 u8 *bptr = (u8 *)ptr;
1538
1539 put_unaligned_be16(port, bptr);
1540 bptr += 2;
1541 put_unaligned_be64(opts->ahmac, bptr);
1542 bptr += 8;
1543 put_unaligned_be16(TCPOPT_NOP << 8 |
1544 TCPOPT_NOP, bptr);
1545
1546 ptr += 3;
1547 } else {
1548 put_unaligned_be32(port << 16 |
1549 TCPOPT_NOP << 8 |
1550 TCPOPT_NOP, ptr);
1551 ptr += 1;
1552 }
1553 }
1554 } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1555 /* FASTCLOSE is mutually exclusive with others except RST */
1556 *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1557 TCPOLEN_MPTCP_FASTCLOSE,
1558 0, 0);
1559 put_unaligned_be64(opts->rcvr_key, ptr);
1560 ptr += 2;
1561
1562 if (OPTION_MPTCP_RST & opts->suboptions)
1563 goto mp_rst;
1564 return;
1565 } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1566mp_fail:
1567 /* MP_FAIL is mutually exclusive with others except RST */
1568 subflow = mptcp_subflow_ctx(ssk);
1569 subflow->send_mp_fail = 0;
1570
1571 *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1572 TCPOLEN_MPTCP_FAIL,
1573 0, 0);
1574 put_unaligned_be64(opts->fail_seq, ptr);
1575 ptr += 2;
1576
1577 if (OPTION_MPTCP_RST & opts->suboptions)
1578 goto mp_rst;
1579 return;
1580 } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1581mp_rst:
1582 *ptr++ = mptcp_option(MPTCPOPT_RST,
1583 TCPOLEN_MPTCP_RST,
1584 opts->reset_transient,
1585 opts->reset_reason);
1586 return;
1587 }
1588
1589 if (OPTION_MPTCP_PRIO & opts->suboptions) {
1590 subflow = mptcp_subflow_ctx(ssk);
1591 subflow->send_mp_prio = 0;
1592
1593 *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1594 TCPOLEN_MPTCP_PRIO,
1595 opts->backup, TCPOPT_NOP);
1596
1597 MPTCP_INC_STATS(sock_net((const struct sock *)tp),
1598 MPTCP_MIB_MPPRIOTX);
1599 }
1600
1601mp_capable_done:
1602 if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1603 u8 i = 1;
1604
1605 *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1606 TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1607 0, opts->rm_list.ids[0]);
1608
1609 while (i < opts->rm_list.nr) {
1610 u8 id1, id2, id3, id4;
1611
1612 id1 = opts->rm_list.ids[i];
1613 id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1614 id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1615 id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1616 put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1617 ptr += 1;
1618 i += 4;
1619 }
1620 }
1621
1622 if (tp)
1623 mptcp_set_rwin(tp, th);
1624}
1625
1626__be32 mptcp_get_reset_option(const struct sk_buff *skb)
1627{
1628 const struct mptcp_ext *ext = mptcp_get_ext(skb);
1629 u8 flags, reason;
1630
1631 if (ext) {
1632 flags = ext->reset_transient;
1633 reason = ext->reset_reason;
1634
1635 return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1636 flags, reason);
1637 }
1638
1639 return htonl(0u);
1640}
1641EXPORT_SYMBOL_GPL(mptcp_get_reset_option);