xref: /openbmc/linux/net/mptcp/options.c (revision 901181b7)
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 
18 static bool mptcp_cap_flag_sha256(u8 flags)
19 {
20 	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
21 }
22 
23 static void mptcp_parse_option(const struct sk_buff *skb,
24 			       const unsigned char *ptr, int opsize,
25 			       struct mptcp_options_received *mp_opt)
26 {
27 	u8 subtype = *ptr >> 4;
28 	int expected_opsize;
29 	u8 version;
30 	u8 flags;
31 	u8 i;
32 
33 	switch (subtype) {
34 	case MPTCPOPT_MP_CAPABLE:
35 		/* strict size checking */
36 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
37 			if (skb->len > tcp_hdr(skb)->doff << 2)
38 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
39 			else
40 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
41 		} else {
42 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
43 				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
44 			else
45 				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
46 		}
47 
48 		/* Cfr RFC 8684 Section 3.3.0:
49 		 * If a checksum is present but its use had
50 		 * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
51 		 * close the subflow with a RST, as it is not behaving as negotiated.
52 		 * If a checksum is not present when its use has been negotiated, the
53 		 * receiver MUST close the subflow with a RST, as it is considered
54 		 * broken
55 		 * We parse even option with mismatching csum presence, so that
56 		 * later in subflow_data_ready we can trigger the reset.
57 		 */
58 		if (opsize != expected_opsize &&
59 		    (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
60 		     opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
61 			break;
62 
63 		/* try to be gentle vs future versions on the initial syn */
64 		version = *ptr++ & MPTCP_VERSION_MASK;
65 		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
66 			if (version != MPTCP_SUPPORTED_VERSION)
67 				break;
68 		} else if (version < MPTCP_SUPPORTED_VERSION) {
69 			break;
70 		}
71 
72 		flags = *ptr++;
73 		if (!mptcp_cap_flag_sha256(flags) ||
74 		    (flags & MPTCP_CAP_EXTENSIBILITY))
75 			break;
76 
77 		/* RFC 6824, Section 3.1:
78 		 * "For the Checksum Required bit (labeled "A"), if either
79 		 * host requires the use of checksums, checksums MUST be used.
80 		 * In other words, the only way for checksums not to be used
81 		 * is if both hosts in their SYNs set A=0."
82 		 */
83 		if (flags & MPTCP_CAP_CHECKSUM_REQD)
84 			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
85 
86 		mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
87 
88 		mp_opt->suboptions |= OPTIONS_MPTCP_MPC;
89 		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
90 			mp_opt->sndr_key = get_unaligned_be64(ptr);
91 			ptr += 8;
92 		}
93 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
94 			mp_opt->rcvr_key = get_unaligned_be64(ptr);
95 			ptr += 8;
96 		}
97 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
98 			/* Section 3.1.:
99 			 * "the data parameters in a MP_CAPABLE are semantically
100 			 * equivalent to those in a DSS option and can be used
101 			 * interchangeably."
102 			 */
103 			mp_opt->suboptions |= OPTION_MPTCP_DSS;
104 			mp_opt->use_map = 1;
105 			mp_opt->mpc_map = 1;
106 			mp_opt->data_len = get_unaligned_be16(ptr);
107 			ptr += 2;
108 		}
109 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
110 			mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
111 			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
112 			ptr += 2;
113 		}
114 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
115 			 version, flags, opsize, mp_opt->sndr_key,
116 			 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
117 		break;
118 
119 	case MPTCPOPT_MP_JOIN:
120 		mp_opt->suboptions |= OPTIONS_MPTCP_MPJ;
121 		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
122 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
123 			mp_opt->join_id = *ptr++;
124 			mp_opt->token = get_unaligned_be32(ptr);
125 			ptr += 4;
126 			mp_opt->nonce = get_unaligned_be32(ptr);
127 			ptr += 4;
128 			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
129 				 mp_opt->backup, mp_opt->join_id,
130 				 mp_opt->token, mp_opt->nonce);
131 		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
132 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
133 			mp_opt->join_id = *ptr++;
134 			mp_opt->thmac = get_unaligned_be64(ptr);
135 			ptr += 8;
136 			mp_opt->nonce = get_unaligned_be32(ptr);
137 			ptr += 4;
138 			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
139 				 mp_opt->backup, mp_opt->join_id,
140 				 mp_opt->thmac, mp_opt->nonce);
141 		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
142 			ptr += 2;
143 			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
144 			pr_debug("MP_JOIN hmac");
145 		} else {
146 			mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ;
147 		}
148 		break;
149 
150 	case MPTCPOPT_DSS:
151 		pr_debug("DSS");
152 		ptr++;
153 
154 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
155 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
156 		 * map info accordingly
157 		 */
158 		mp_opt->mpc_map = 0;
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",
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", 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 = (__force __sum16)get_unaligned_be16(ptr);
225 				ptr += 2;
226 			}
227 
228 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
229 				 mp_opt->data_seq, mp_opt->subflow_seq,
230 				 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
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",
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", 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", 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 		break;
327 
328 	case MPTCPOPT_RST:
329 		if (opsize != TCPOLEN_MPTCP_RST)
330 			break;
331 
332 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
333 			break;
334 
335 		mp_opt->suboptions |= OPTION_MPTCP_RST;
336 		flags = *ptr++;
337 		mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
338 		mp_opt->reset_reason = *ptr;
339 		break;
340 
341 	case MPTCPOPT_MP_FAIL:
342 		if (opsize != TCPOLEN_MPTCP_FAIL)
343 			break;
344 
345 		ptr += 2;
346 		mp_opt->suboptions |= OPTION_MPTCP_FAIL;
347 		mp_opt->fail_seq = get_unaligned_be64(ptr);
348 		pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
349 		break;
350 
351 	default:
352 		break;
353 	}
354 }
355 
356 void mptcp_get_options(const struct sock *sk,
357 		       const struct sk_buff *skb,
358 		       struct mptcp_options_received *mp_opt)
359 {
360 	const struct tcphdr *th = tcp_hdr(skb);
361 	const unsigned char *ptr;
362 	int length;
363 
364 	/* initialize option status */
365 	mp_opt->suboptions = 0;
366 
367 	length = (th->doff * 4) - sizeof(struct tcphdr);
368 	ptr = (const unsigned char *)(th + 1);
369 
370 	while (length > 0) {
371 		int opcode = *ptr++;
372 		int opsize;
373 
374 		switch (opcode) {
375 		case TCPOPT_EOL:
376 			return;
377 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
378 			length--;
379 			continue;
380 		default:
381 			if (length < 2)
382 				return;
383 			opsize = *ptr++;
384 			if (opsize < 2) /* "silly options" */
385 				return;
386 			if (opsize > length)
387 				return;	/* don't parse partial options */
388 			if (opcode == TCPOPT_MPTCP)
389 				mptcp_parse_option(skb, ptr, opsize, mp_opt);
390 			ptr += opsize - 2;
391 			length -= opsize;
392 		}
393 	}
394 }
395 
396 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
397 		       unsigned int *size, struct mptcp_out_options *opts)
398 {
399 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
400 
401 	/* we will use snd_isn to detect first pkt [re]transmission
402 	 * in mptcp_established_options_mp()
403 	 */
404 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
405 	if (subflow->request_mptcp) {
406 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
407 		opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
408 		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
409 		*size = TCPOLEN_MPTCP_MPC_SYN;
410 		return true;
411 	} else if (subflow->request_join) {
412 		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
413 			 subflow->local_nonce);
414 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
415 		opts->join_id = subflow->local_id;
416 		opts->token = subflow->remote_token;
417 		opts->nonce = subflow->local_nonce;
418 		opts->backup = subflow->request_bkup;
419 		*size = TCPOLEN_MPTCP_MPJ_SYN;
420 		return true;
421 	}
422 	return false;
423 }
424 
425 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
426  * TCP can't schedule delack timer before the subflow is fully established.
427  * MPTCP uses the delack timer to do 3rd ack retransmissions
428  */
429 static void schedule_3rdack_retransmission(struct sock *sk)
430 {
431 	struct inet_connection_sock *icsk = inet_csk(sk);
432 	struct tcp_sock *tp = tcp_sk(sk);
433 	unsigned long timeout;
434 
435 	/* reschedule with a timeout above RTT, as we must look only for drop */
436 	if (tp->srtt_us)
437 		timeout = tp->srtt_us << 1;
438 	else
439 		timeout = TCP_TIMEOUT_INIT;
440 
441 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
442 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
443 	icsk->icsk_ack.timeout = timeout;
444 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
445 }
446 
447 static void clear_3rdack_retransmission(struct sock *sk)
448 {
449 	struct inet_connection_sock *icsk = inet_csk(sk);
450 
451 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
452 	icsk->icsk_ack.timeout = 0;
453 	icsk->icsk_ack.ato = 0;
454 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
455 }
456 
457 static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
458 					 bool snd_data_fin_enable,
459 					 unsigned int *size,
460 					 unsigned int remaining,
461 					 struct mptcp_out_options *opts)
462 {
463 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
464 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
465 	struct mptcp_ext *mpext;
466 	unsigned int data_len;
467 	u8 len;
468 
469 	/* When skb is not available, we better over-estimate the emitted
470 	 * options len. A full DSS option (28 bytes) is longer than
471 	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
472 	 * tell the caller to defer the estimate to
473 	 * mptcp_established_options_dss(), which will reserve enough space.
474 	 */
475 	if (!skb)
476 		return false;
477 
478 	/* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
479 	if (subflow->fully_established || snd_data_fin_enable ||
480 	    subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
481 	    sk->sk_state != TCP_ESTABLISHED)
482 		return false;
483 
484 	if (subflow->mp_capable) {
485 		mpext = mptcp_get_ext(skb);
486 		data_len = mpext ? mpext->data_len : 0;
487 
488 		/* we will check ops->data_len in mptcp_write_options() to
489 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
490 		 * TCPOLEN_MPTCP_MPC_ACK
491 		 */
492 		opts->data_len = data_len;
493 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
494 		opts->sndr_key = subflow->local_key;
495 		opts->rcvr_key = subflow->remote_key;
496 		opts->csum_reqd = READ_ONCE(msk->csum_enabled);
497 		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
498 
499 		/* Section 3.1.
500 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
501 		 * packets that start the first subflow of an MPTCP connection,
502 		 * as well as the first packet that carries data
503 		 */
504 		if (data_len > 0) {
505 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
506 			if (opts->csum_reqd) {
507 				/* we need to propagate more info to csum the pseudo hdr */
508 				opts->data_seq = mpext->data_seq;
509 				opts->subflow_seq = mpext->subflow_seq;
510 				opts->csum = mpext->csum;
511 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
512 			}
513 			*size = ALIGN(len, 4);
514 		} else {
515 			*size = TCPOLEN_MPTCP_MPC_ACK;
516 		}
517 
518 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
519 			 subflow, subflow->local_key, subflow->remote_key,
520 			 data_len);
521 
522 		return true;
523 	} else if (subflow->mp_join) {
524 		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
525 		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
526 		*size = TCPOLEN_MPTCP_MPJ_ACK;
527 		pr_debug("subflow=%p", subflow);
528 
529 		schedule_3rdack_retransmission(sk);
530 		return true;
531 	}
532 	return false;
533 }
534 
535 static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
536 				 struct sk_buff *skb, struct mptcp_ext *ext)
537 {
538 	/* The write_seq value has already been incremented, so the actual
539 	 * sequence number for the DATA_FIN is one less.
540 	 */
541 	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
542 
543 	if (!ext->use_map || !skb->len) {
544 		/* RFC6824 requires a DSS mapping with specific values
545 		 * if DATA_FIN is set but no data payload is mapped
546 		 */
547 		ext->data_fin = 1;
548 		ext->use_map = 1;
549 		ext->dsn64 = 1;
550 		ext->data_seq = data_fin_tx_seq;
551 		ext->subflow_seq = 0;
552 		ext->data_len = 1;
553 	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
554 		/* If there's an existing DSS mapping and it is the
555 		 * final mapping, DATA_FIN consumes 1 additional byte of
556 		 * mapping space.
557 		 */
558 		ext->data_fin = 1;
559 		ext->data_len++;
560 	}
561 }
562 
563 static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
564 					  bool snd_data_fin_enable,
565 					  unsigned int *size,
566 					  unsigned int remaining,
567 					  struct mptcp_out_options *opts)
568 {
569 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
570 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
571 	unsigned int dss_size = 0;
572 	struct mptcp_ext *mpext;
573 	unsigned int ack_size;
574 	bool ret = false;
575 	u64 ack_seq;
576 
577 	opts->csum_reqd = READ_ONCE(msk->csum_enabled);
578 	mpext = skb ? mptcp_get_ext(skb) : NULL;
579 
580 	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
581 		unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
582 
583 		if (mpext) {
584 			if (opts->csum_reqd)
585 				map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
586 
587 			opts->ext_copy = *mpext;
588 		}
589 
590 		remaining -= map_size;
591 		dss_size = map_size;
592 		if (skb && snd_data_fin_enable)
593 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
594 		opts->suboptions = OPTION_MPTCP_DSS;
595 		ret = true;
596 	}
597 
598 	/* passive sockets msk will set the 'can_ack' after accept(), even
599 	 * if the first subflow may have the already the remote key handy
600 	 */
601 	opts->ext_copy.use_ack = 0;
602 	if (!READ_ONCE(msk->can_ack)) {
603 		*size = ALIGN(dss_size, 4);
604 		return ret;
605 	}
606 
607 	ack_seq = READ_ONCE(msk->ack_seq);
608 	if (READ_ONCE(msk->use_64bit_ack)) {
609 		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
610 		opts->ext_copy.data_ack = ack_seq;
611 		opts->ext_copy.ack64 = 1;
612 	} else {
613 		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
614 		opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
615 		opts->ext_copy.ack64 = 0;
616 	}
617 	opts->ext_copy.use_ack = 1;
618 	opts->suboptions = OPTION_MPTCP_DSS;
619 	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
620 
621 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
622 	if (dss_size == 0)
623 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
624 
625 	dss_size += ack_size;
626 
627 	*size = ALIGN(dss_size, 4);
628 	return true;
629 }
630 
631 static u64 add_addr_generate_hmac(u64 key1, u64 key2,
632 				  struct mptcp_addr_info *addr)
633 {
634 	u16 port = ntohs(addr->port);
635 	u8 hmac[SHA256_DIGEST_SIZE];
636 	u8 msg[19];
637 	int i = 0;
638 
639 	msg[i++] = addr->id;
640 	if (addr->family == AF_INET) {
641 		memcpy(&msg[i], &addr->addr.s_addr, 4);
642 		i += 4;
643 	}
644 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
645 	else if (addr->family == AF_INET6) {
646 		memcpy(&msg[i], &addr->addr6.s6_addr, 16);
647 		i += 16;
648 	}
649 #endif
650 	msg[i++] = port >> 8;
651 	msg[i++] = port & 0xFF;
652 
653 	mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
654 
655 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
656 }
657 
658 static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
659 					       unsigned int *size,
660 					       unsigned int remaining,
661 					       struct mptcp_out_options *opts)
662 {
663 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
664 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
665 	bool drop_other_suboptions = false;
666 	unsigned int opt_size = *size;
667 	bool echo;
668 	bool port;
669 	int len;
670 
671 	/* add addr will strip the existing options, be sure to avoid breaking
672 	 * MPC/MPJ handshakes
673 	 */
674 	if (!mptcp_pm_should_add_signal(msk) ||
675 	    (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
676 	    !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
677 		    &echo, &port, &drop_other_suboptions))
678 		return false;
679 
680 	if (drop_other_suboptions)
681 		remaining += opt_size;
682 	len = mptcp_add_addr_len(opts->addr.family, echo, port);
683 	if (remaining < len)
684 		return false;
685 
686 	*size = len;
687 	if (drop_other_suboptions) {
688 		pr_debug("drop other suboptions");
689 		opts->suboptions = 0;
690 
691 		/* note that e.g. DSS could have written into the memory
692 		 * aliased by ahmac, we must reset the field here
693 		 * to avoid appending the hmac even for ADD_ADDR echo
694 		 * options
695 		 */
696 		opts->ahmac = 0;
697 		*size -= opt_size;
698 	}
699 	opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
700 	if (!echo) {
701 		opts->ahmac = add_addr_generate_hmac(msk->local_key,
702 						     msk->remote_key,
703 						     &opts->addr);
704 	}
705 	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
706 		 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
707 
708 	return true;
709 }
710 
711 static bool mptcp_established_options_rm_addr(struct sock *sk,
712 					      unsigned int *size,
713 					      unsigned int remaining,
714 					      struct mptcp_out_options *opts)
715 {
716 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
717 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
718 	struct mptcp_rm_list rm_list;
719 	int i, len;
720 
721 	if (!mptcp_pm_should_rm_signal(msk) ||
722 	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
723 		return false;
724 
725 	len = mptcp_rm_addr_len(&rm_list);
726 	if (len < 0)
727 		return false;
728 	if (remaining < len)
729 		return false;
730 
731 	*size = len;
732 	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
733 	opts->rm_list = rm_list;
734 
735 	for (i = 0; i < opts->rm_list.nr; i++)
736 		pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
737 
738 	return true;
739 }
740 
741 static bool mptcp_established_options_mp_prio(struct sock *sk,
742 					      unsigned int *size,
743 					      unsigned int remaining,
744 					      struct mptcp_out_options *opts)
745 {
746 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
747 
748 	/* can't send MP_PRIO with MPC, as they share the same option space:
749 	 * 'backup'. Also it makes no sense at all
750 	 */
751 	if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
752 		return false;
753 
754 	/* account for the trailing 'nop' option */
755 	if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
756 		return false;
757 
758 	*size = TCPOLEN_MPTCP_PRIO_ALIGN;
759 	opts->suboptions |= OPTION_MPTCP_PRIO;
760 	opts->backup = subflow->request_bkup;
761 
762 	pr_debug("prio=%d", opts->backup);
763 
764 	return true;
765 }
766 
767 static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
768 						   unsigned int *size,
769 						   unsigned int remaining,
770 						   struct mptcp_out_options *opts)
771 {
772 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
773 
774 	if (remaining < TCPOLEN_MPTCP_RST)
775 		return false;
776 
777 	*size = TCPOLEN_MPTCP_RST;
778 	opts->suboptions |= OPTION_MPTCP_RST;
779 	opts->reset_transient = subflow->reset_transient;
780 	opts->reset_reason = subflow->reset_reason;
781 
782 	return true;
783 }
784 
785 static bool mptcp_established_options_mp_fail(struct sock *sk,
786 					      unsigned int *size,
787 					      unsigned int remaining,
788 					      struct mptcp_out_options *opts)
789 {
790 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
791 
792 	if (likely(!subflow->send_mp_fail))
793 		return false;
794 
795 	if (remaining < TCPOLEN_MPTCP_FAIL)
796 		return false;
797 
798 	*size = TCPOLEN_MPTCP_FAIL;
799 	opts->suboptions |= OPTION_MPTCP_FAIL;
800 	opts->fail_seq = subflow->map_seq;
801 
802 	pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
803 
804 	return true;
805 }
806 
807 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
808 			       unsigned int *size, unsigned int remaining,
809 			       struct mptcp_out_options *opts)
810 {
811 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
812 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
813 	unsigned int opt_size = 0;
814 	bool snd_data_fin;
815 	bool ret = false;
816 
817 	opts->suboptions = 0;
818 
819 	if (unlikely(__mptcp_check_fallback(msk)))
820 		return false;
821 
822 	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
823 		if (mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
824 			*size += opt_size;
825 			remaining -= opt_size;
826 		}
827 		if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
828 			*size += opt_size;
829 			remaining -= opt_size;
830 		}
831 		return true;
832 	}
833 
834 	snd_data_fin = mptcp_data_fin_enabled(msk);
835 	if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
836 		ret = true;
837 	else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) {
838 		ret = true;
839 		if (mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
840 			*size += opt_size;
841 			remaining -= opt_size;
842 			return true;
843 		}
844 	}
845 
846 	/* we reserved enough space for the above options, and exceeding the
847 	 * TCP option space would be fatal
848 	 */
849 	if (WARN_ON_ONCE(opt_size > remaining))
850 		return false;
851 
852 	*size += opt_size;
853 	remaining -= opt_size;
854 	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
855 		*size += opt_size;
856 		remaining -= opt_size;
857 		ret = true;
858 	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
859 		*size += opt_size;
860 		remaining -= opt_size;
861 		ret = true;
862 	}
863 
864 	if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
865 		*size += opt_size;
866 		remaining -= opt_size;
867 		ret = true;
868 	}
869 
870 	return ret;
871 }
872 
873 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
874 			  struct mptcp_out_options *opts)
875 {
876 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
877 
878 	if (subflow_req->mp_capable) {
879 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
880 		opts->sndr_key = subflow_req->local_key;
881 		opts->csum_reqd = subflow_req->csum_reqd;
882 		opts->allow_join_id0 = subflow_req->allow_join_id0;
883 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
884 		pr_debug("subflow_req=%p, local_key=%llu",
885 			 subflow_req, subflow_req->local_key);
886 		return true;
887 	} else if (subflow_req->mp_join) {
888 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
889 		opts->backup = subflow_req->backup;
890 		opts->join_id = subflow_req->local_id;
891 		opts->thmac = subflow_req->thmac;
892 		opts->nonce = subflow_req->local_nonce;
893 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
894 			 subflow_req, opts->backup, opts->join_id,
895 			 opts->thmac, opts->nonce);
896 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
897 		return true;
898 	}
899 	return false;
900 }
901 
902 static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
903 				    struct mptcp_subflow_context *subflow,
904 				    struct sk_buff *skb,
905 				    struct mptcp_options_received *mp_opt)
906 {
907 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
908 	 * will make the subflow fully established
909 	 */
910 	if (likely(subflow->fully_established)) {
911 		/* on passive sockets, check for 3rd ack retransmission
912 		 * note that msk is always set by subflow_syn_recv_sock()
913 		 * for mp_join subflows
914 		 */
915 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
916 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
917 		    subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
918 		    READ_ONCE(msk->pm.server_side))
919 			tcp_send_ack(ssk);
920 		goto fully_established;
921 	}
922 
923 	/* we must process OoO packets before the first subflow is fully
924 	 * established. OoO packets are instead a protocol violation
925 	 * for MP_JOIN subflows as the peer must not send any data
926 	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
927 	 */
928 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
929 		if (subflow->mp_join)
930 			goto reset;
931 		return subflow->mp_capable;
932 	}
933 
934 	if (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
935 	    ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo)) {
936 		/* subflows are fully established as soon as we get any
937 		 * additional ack, including ADD_ADDR.
938 		 */
939 		subflow->fully_established = 1;
940 		WRITE_ONCE(msk->fully_established, true);
941 		goto fully_established;
942 	}
943 
944 	/* If the first established packet does not contain MP_CAPABLE + data
945 	 * then fallback to TCP. Fallback scenarios requires a reset for
946 	 * MP_JOIN subflows.
947 	 */
948 	if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
949 		if (subflow->mp_join)
950 			goto reset;
951 		subflow->mp_capable = 0;
952 		pr_fallback(msk);
953 		__mptcp_do_fallback(msk);
954 		return false;
955 	}
956 
957 	if (mp_opt->deny_join_id0)
958 		WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
959 
960 	if (unlikely(!READ_ONCE(msk->pm.server_side)))
961 		pr_warn_once("bogus mpc option on established client sk");
962 	mptcp_subflow_fully_established(subflow, mp_opt);
963 
964 fully_established:
965 	/* if the subflow is not already linked into the conn_list, we can't
966 	 * notify the PM: this subflow is still on the listener queue
967 	 * and the PM possibly acquiring the subflow lock could race with
968 	 * the listener close
969 	 */
970 	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
971 		return true;
972 
973 	subflow->pm_notified = 1;
974 	if (subflow->mp_join) {
975 		clear_3rdack_retransmission(ssk);
976 		mptcp_pm_subflow_established(msk);
977 	} else {
978 		mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
979 	}
980 	return true;
981 
982 reset:
983 	mptcp_subflow_reset(ssk);
984 	return false;
985 }
986 
987 u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
988 {
989 	u32 old_seq32, cur_seq32;
990 
991 	old_seq32 = (u32)old_seq;
992 	cur_seq32 = (u32)cur_seq;
993 	cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
994 	if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
995 		return cur_seq + (1LL << 32);
996 
997 	/* reverse wrap could happen, too */
998 	if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
999 		return cur_seq - (1LL << 32);
1000 	return cur_seq;
1001 }
1002 
1003 static void ack_update_msk(struct mptcp_sock *msk,
1004 			   struct sock *ssk,
1005 			   struct mptcp_options_received *mp_opt)
1006 {
1007 	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1008 	struct sock *sk = (struct sock *)msk;
1009 	u64 old_snd_una;
1010 
1011 	mptcp_data_lock(sk);
1012 
1013 	/* avoid ack expansion on update conflict, to reduce the risk of
1014 	 * wrongly expanding to a future ack sequence number, which is way
1015 	 * more dangerous than missing an ack
1016 	 */
1017 	old_snd_una = msk->snd_una;
1018 	new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1019 
1020 	/* ACK for data not even sent yet? Ignore.*/
1021 	if (unlikely(after64(new_snd_una, snd_nxt)))
1022 		new_snd_una = old_snd_una;
1023 
1024 	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1025 
1026 	if (after64(new_wnd_end, msk->wnd_end))
1027 		msk->wnd_end = new_wnd_end;
1028 
1029 	/* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1030 	if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1031 		__mptcp_check_push(sk, ssk);
1032 
1033 	if (after64(new_snd_una, old_snd_una)) {
1034 		msk->snd_una = new_snd_una;
1035 		__mptcp_data_acked(sk);
1036 	}
1037 	mptcp_data_unlock(sk);
1038 
1039 	trace_ack_update_msk(mp_opt->data_ack,
1040 			     old_snd_una, new_snd_una,
1041 			     new_wnd_end, msk->wnd_end);
1042 }
1043 
1044 bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1045 {
1046 	/* Skip if DATA_FIN was already received.
1047 	 * If updating simultaneously with the recvmsg loop, values
1048 	 * should match. If they mismatch, the peer is misbehaving and
1049 	 * we will prefer the most recent information.
1050 	 */
1051 	if (READ_ONCE(msk->rcv_data_fin))
1052 		return false;
1053 
1054 	WRITE_ONCE(msk->rcv_data_fin_seq,
1055 		   mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1056 	WRITE_ONCE(msk->rcv_data_fin, 1);
1057 
1058 	return true;
1059 }
1060 
1061 static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1062 				struct mptcp_options_received *mp_opt)
1063 {
1064 	u64 hmac = 0;
1065 
1066 	if (mp_opt->echo)
1067 		return true;
1068 
1069 	hmac = add_addr_generate_hmac(msk->remote_key,
1070 				      msk->local_key,
1071 				      &mp_opt->addr);
1072 
1073 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1074 		 msk, (unsigned long long)hmac,
1075 		 (unsigned long long)mp_opt->ahmac);
1076 
1077 	return hmac == mp_opt->ahmac;
1078 }
1079 
1080 /* Return false if a subflow has been reset, else return true */
1081 bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1082 {
1083 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1084 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1085 	struct mptcp_options_received mp_opt;
1086 	struct mptcp_ext *mpext;
1087 
1088 	if (__mptcp_check_fallback(msk)) {
1089 		/* Keep it simple and unconditionally trigger send data cleanup and
1090 		 * pending queue spooling. We will need to acquire the data lock
1091 		 * for more accurate checks, and once the lock is acquired, such
1092 		 * helpers are cheap.
1093 		 */
1094 		mptcp_data_lock(subflow->conn);
1095 		if (sk_stream_memory_free(sk))
1096 			__mptcp_check_push(subflow->conn, sk);
1097 		__mptcp_data_acked(subflow->conn);
1098 		mptcp_data_unlock(subflow->conn);
1099 		return true;
1100 	}
1101 
1102 	mptcp_get_options(sk, skb, &mp_opt);
1103 
1104 	/* The subflow can be in close state only if check_fully_established()
1105 	 * just sent a reset. If so, tell the caller to ignore the current packet.
1106 	 */
1107 	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1108 		return sk->sk_state != TCP_CLOSE;
1109 
1110 	if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1111 		if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1112 		    msk->local_key == mp_opt.rcvr_key) {
1113 			WRITE_ONCE(msk->rcv_fastclose, true);
1114 			mptcp_schedule_work((struct sock *)msk);
1115 		}
1116 
1117 		if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1118 		    add_addr_hmac_valid(msk, &mp_opt)) {
1119 			if (!mp_opt.echo) {
1120 				mptcp_pm_add_addr_received(msk, &mp_opt.addr);
1121 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1122 			} else {
1123 				mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1124 				mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1125 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1126 			}
1127 
1128 			if (mp_opt.addr.port)
1129 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1130 		}
1131 
1132 		if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1133 			mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1134 
1135 		if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1136 			mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1137 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1138 		}
1139 
1140 		if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1141 			mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1142 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1143 		}
1144 
1145 		if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1146 			subflow->reset_seen = 1;
1147 			subflow->reset_reason = mp_opt.reset_reason;
1148 			subflow->reset_transient = mp_opt.reset_transient;
1149 		}
1150 
1151 		if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1152 			return true;
1153 	}
1154 
1155 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
1156 	 * monodirectional flows will stuck
1157 	 */
1158 	if (mp_opt.use_ack)
1159 		ack_update_msk(msk, sk, &mp_opt);
1160 
1161 	/* Zero-data-length packets are dropped by the caller and not
1162 	 * propagated to the MPTCP layer, so the skb extension does not
1163 	 * need to be allocated or populated. DATA_FIN information, if
1164 	 * present, needs to be updated here before the skb is freed.
1165 	 */
1166 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1167 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1168 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
1169 		    schedule_work(&msk->work))
1170 			sock_hold(subflow->conn);
1171 
1172 		return true;
1173 	}
1174 
1175 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1176 	if (!mpext)
1177 		return true;
1178 
1179 	memset(mpext, 0, sizeof(*mpext));
1180 
1181 	if (likely(mp_opt.use_map)) {
1182 		if (mp_opt.mpc_map) {
1183 			/* this is an MP_CAPABLE carrying MPTCP data
1184 			 * we know this map the first chunk of data
1185 			 */
1186 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
1187 					     &mpext->data_seq);
1188 			mpext->data_seq++;
1189 			mpext->subflow_seq = 1;
1190 			mpext->dsn64 = 1;
1191 			mpext->mpc_map = 1;
1192 			mpext->data_fin = 0;
1193 		} else {
1194 			mpext->data_seq = mp_opt.data_seq;
1195 			mpext->subflow_seq = mp_opt.subflow_seq;
1196 			mpext->dsn64 = mp_opt.dsn64;
1197 			mpext->data_fin = mp_opt.data_fin;
1198 		}
1199 		mpext->data_len = mp_opt.data_len;
1200 		mpext->use_map = 1;
1201 		mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1202 
1203 		if (mpext->csum_reqd)
1204 			mpext->csum = mp_opt.csum;
1205 	}
1206 
1207 	return true;
1208 }
1209 
1210 static void mptcp_set_rwin(const struct tcp_sock *tp)
1211 {
1212 	const struct sock *ssk = (const struct sock *)tp;
1213 	const struct mptcp_subflow_context *subflow;
1214 	struct mptcp_sock *msk;
1215 	u64 ack_seq;
1216 
1217 	subflow = mptcp_subflow_ctx(ssk);
1218 	msk = mptcp_sk(subflow->conn);
1219 
1220 	ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1221 
1222 	if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1223 		WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1224 }
1225 
1226 static u16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __sum16 sum)
1227 {
1228 	struct csum_pseudo_header header;
1229 	__wsum csum;
1230 
1231 	/* cfr RFC 8684 3.3.1.:
1232 	 * the data sequence number used in the pseudo-header is
1233 	 * always the 64-bit value, irrespective of what length is used in the
1234 	 * DSS option itself.
1235 	 */
1236 	header.data_seq = cpu_to_be64(data_seq);
1237 	header.subflow_seq = htonl(subflow_seq);
1238 	header.data_len = htons(data_len);
1239 	header.csum = 0;
1240 
1241 	csum = csum_partial(&header, sizeof(header), ~csum_unfold(sum));
1242 	return (__force u16)csum_fold(csum);
1243 }
1244 
1245 static u16 mptcp_make_csum(const struct mptcp_ext *mpext)
1246 {
1247 	return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1248 				 mpext->csum);
1249 }
1250 
1251 void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1252 			 struct mptcp_out_options *opts)
1253 {
1254 	if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1255 		const struct sock *ssk = (const struct sock *)tp;
1256 		struct mptcp_subflow_context *subflow;
1257 
1258 		subflow = mptcp_subflow_ctx(ssk);
1259 		subflow->send_mp_fail = 0;
1260 
1261 		*ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1262 				      TCPOLEN_MPTCP_FAIL,
1263 				      0, 0);
1264 		put_unaligned_be64(opts->fail_seq, ptr);
1265 		ptr += 2;
1266 	}
1267 
1268 	/* RST is mutually exclusive with everything else */
1269 	if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1270 		*ptr++ = mptcp_option(MPTCPOPT_RST,
1271 				      TCPOLEN_MPTCP_RST,
1272 				      opts->reset_transient,
1273 				      opts->reset_reason);
1274 		return;
1275 	}
1276 
1277 	/* DSS, MPC, MPJ and ADD_ADDR are mutually exclusive, see
1278 	 * mptcp_established_options*()
1279 	 */
1280 	if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1281 		struct mptcp_ext *mpext = &opts->ext_copy;
1282 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
1283 		u8 flags = 0;
1284 
1285 		if (mpext->use_ack) {
1286 			flags = MPTCP_DSS_HAS_ACK;
1287 			if (mpext->ack64) {
1288 				len += TCPOLEN_MPTCP_DSS_ACK64;
1289 				flags |= MPTCP_DSS_ACK64;
1290 			} else {
1291 				len += TCPOLEN_MPTCP_DSS_ACK32;
1292 			}
1293 		}
1294 
1295 		if (mpext->use_map) {
1296 			len += TCPOLEN_MPTCP_DSS_MAP64;
1297 
1298 			/* Use only 64-bit mapping flags for now, add
1299 			 * support for optional 32-bit mappings later.
1300 			 */
1301 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1302 			if (mpext->data_fin)
1303 				flags |= MPTCP_DSS_DATA_FIN;
1304 
1305 			if (opts->csum_reqd)
1306 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1307 		}
1308 
1309 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1310 
1311 		if (mpext->use_ack) {
1312 			if (mpext->ack64) {
1313 				put_unaligned_be64(mpext->data_ack, ptr);
1314 				ptr += 2;
1315 			} else {
1316 				put_unaligned_be32(mpext->data_ack32, ptr);
1317 				ptr += 1;
1318 			}
1319 		}
1320 
1321 		if (mpext->use_map) {
1322 			put_unaligned_be64(mpext->data_seq, ptr);
1323 			ptr += 2;
1324 			put_unaligned_be32(mpext->subflow_seq, ptr);
1325 			ptr += 1;
1326 			if (opts->csum_reqd) {
1327 				put_unaligned_be32(mpext->data_len << 16 |
1328 						   mptcp_make_csum(mpext), ptr);
1329 			} else {
1330 				put_unaligned_be32(mpext->data_len << 16 |
1331 						   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1332 			}
1333 		}
1334 	} else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1335 		u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1336 
1337 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1338 			len = TCPOLEN_MPTCP_MPC_SYN;
1339 		} else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1340 			len = TCPOLEN_MPTCP_MPC_SYNACK;
1341 		} else if (opts->data_len) {
1342 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1343 			if (opts->csum_reqd)
1344 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1345 		} else {
1346 			len = TCPOLEN_MPTCP_MPC_ACK;
1347 		}
1348 
1349 		if (opts->csum_reqd)
1350 			flag |= MPTCP_CAP_CHECKSUM_REQD;
1351 
1352 		if (!opts->allow_join_id0)
1353 			flag |= MPTCP_CAP_DENY_JOIN_ID0;
1354 
1355 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1356 				      MPTCP_SUPPORTED_VERSION,
1357 				      flag);
1358 
1359 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1360 		    opts->suboptions))
1361 			goto mp_capable_done;
1362 
1363 		put_unaligned_be64(opts->sndr_key, ptr);
1364 		ptr += 2;
1365 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1366 			goto mp_capable_done;
1367 
1368 		put_unaligned_be64(opts->rcvr_key, ptr);
1369 		ptr += 2;
1370 		if (!opts->data_len)
1371 			goto mp_capable_done;
1372 
1373 		if (opts->csum_reqd) {
1374 			put_unaligned_be32(opts->data_len << 16 |
1375 					   __mptcp_make_csum(opts->data_seq,
1376 							     opts->subflow_seq,
1377 							     opts->data_len,
1378 							     opts->csum), ptr);
1379 		} else {
1380 			put_unaligned_be32(opts->data_len << 16 |
1381 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1382 		}
1383 		ptr += 1;
1384 
1385 		/* MPC is additionally mutually exclusive with MP_PRIO */
1386 		goto mp_capable_done;
1387 	} else if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1388 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1389 				      TCPOLEN_MPTCP_MPJ_SYN,
1390 				      opts->backup, opts->join_id);
1391 		put_unaligned_be32(opts->token, ptr);
1392 		ptr += 1;
1393 		put_unaligned_be32(opts->nonce, ptr);
1394 		ptr += 1;
1395 	} else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1396 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1397 				      TCPOLEN_MPTCP_MPJ_SYNACK,
1398 				      opts->backup, opts->join_id);
1399 		put_unaligned_be64(opts->thmac, ptr);
1400 		ptr += 2;
1401 		put_unaligned_be32(opts->nonce, ptr);
1402 		ptr += 1;
1403 	} else if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1404 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1405 				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1406 		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1407 		ptr += 5;
1408 	} else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1409 		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1410 		u8 echo = MPTCP_ADDR_ECHO;
1411 
1412 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1413 		if (opts->addr.family == AF_INET6)
1414 			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1415 #endif
1416 
1417 		if (opts->addr.port)
1418 			len += TCPOLEN_MPTCP_PORT_LEN;
1419 
1420 		if (opts->ahmac) {
1421 			len += sizeof(opts->ahmac);
1422 			echo = 0;
1423 		}
1424 
1425 		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1426 				      len, echo, opts->addr.id);
1427 		if (opts->addr.family == AF_INET) {
1428 			memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1429 			ptr += 1;
1430 		}
1431 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1432 		else if (opts->addr.family == AF_INET6) {
1433 			memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1434 			ptr += 4;
1435 		}
1436 #endif
1437 
1438 		if (!opts->addr.port) {
1439 			if (opts->ahmac) {
1440 				put_unaligned_be64(opts->ahmac, ptr);
1441 				ptr += 2;
1442 			}
1443 		} else {
1444 			u16 port = ntohs(opts->addr.port);
1445 
1446 			if (opts->ahmac) {
1447 				u8 *bptr = (u8 *)ptr;
1448 
1449 				put_unaligned_be16(port, bptr);
1450 				bptr += 2;
1451 				put_unaligned_be64(opts->ahmac, bptr);
1452 				bptr += 8;
1453 				put_unaligned_be16(TCPOPT_NOP << 8 |
1454 						   TCPOPT_NOP, bptr);
1455 
1456 				ptr += 3;
1457 			} else {
1458 				put_unaligned_be32(port << 16 |
1459 						   TCPOPT_NOP << 8 |
1460 						   TCPOPT_NOP, ptr);
1461 				ptr += 1;
1462 			}
1463 		}
1464 	}
1465 
1466 	if (OPTION_MPTCP_PRIO & opts->suboptions) {
1467 		const struct sock *ssk = (const struct sock *)tp;
1468 		struct mptcp_subflow_context *subflow;
1469 
1470 		subflow = mptcp_subflow_ctx(ssk);
1471 		subflow->send_mp_prio = 0;
1472 
1473 		*ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1474 				      TCPOLEN_MPTCP_PRIO,
1475 				      opts->backup, TCPOPT_NOP);
1476 	}
1477 
1478 mp_capable_done:
1479 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1480 		u8 i = 1;
1481 
1482 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1483 				      TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1484 				      0, opts->rm_list.ids[0]);
1485 
1486 		while (i < opts->rm_list.nr) {
1487 			u8 id1, id2, id3, id4;
1488 
1489 			id1 = opts->rm_list.ids[i];
1490 			id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1491 			id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1492 			id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1493 			put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1494 			ptr += 1;
1495 			i += 4;
1496 		}
1497 	}
1498 
1499 	if (tp)
1500 		mptcp_set_rwin(tp);
1501 }
1502 
1503 __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1504 {
1505 	const struct mptcp_ext *ext = mptcp_get_ext(skb);
1506 	u8 flags, reason;
1507 
1508 	if (ext) {
1509 		flags = ext->reset_transient;
1510 		reason = ext->reset_reason;
1511 
1512 		return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1513 				    flags, reason);
1514 	}
1515 
1516 	return htonl(0u);
1517 }
1518 EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
1519