xref: /openbmc/linux/net/mptcp/options.c (revision f5ad1c74)
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 static bool mptcp_cap_flag_sha256(u8 flags)
17 {
18 	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
19 }
20 
21 static void mptcp_parse_option(const struct sk_buff *skb,
22 			       const unsigned char *ptr, int opsize,
23 			       struct mptcp_options_received *mp_opt)
24 {
25 	u8 subtype = *ptr >> 4;
26 	int expected_opsize;
27 	u8 version;
28 	u8 flags;
29 
30 	switch (subtype) {
31 	case MPTCPOPT_MP_CAPABLE:
32 		/* strict size checking */
33 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
34 			if (skb->len > tcp_hdr(skb)->doff << 2)
35 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
36 			else
37 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
38 		} else {
39 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
40 				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
41 			else
42 				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
43 		}
44 		if (opsize != expected_opsize)
45 			break;
46 
47 		/* try to be gentle vs future versions on the initial syn */
48 		version = *ptr++ & MPTCP_VERSION_MASK;
49 		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
50 			if (version != MPTCP_SUPPORTED_VERSION)
51 				break;
52 		} else if (version < MPTCP_SUPPORTED_VERSION) {
53 			break;
54 		}
55 
56 		flags = *ptr++;
57 		if (!mptcp_cap_flag_sha256(flags) ||
58 		    (flags & MPTCP_CAP_EXTENSIBILITY))
59 			break;
60 
61 		/* RFC 6824, Section 3.1:
62 		 * "For the Checksum Required bit (labeled "A"), if either
63 		 * host requires the use of checksums, checksums MUST be used.
64 		 * In other words, the only way for checksums not to be used
65 		 * is if both hosts in their SYNs set A=0."
66 		 *
67 		 * Section 3.3.0:
68 		 * "If a checksum is not present when its use has been
69 		 * negotiated, the receiver MUST close the subflow with a RST as
70 		 * it is considered broken."
71 		 *
72 		 * We don't implement DSS checksum - fall back to TCP.
73 		 */
74 		if (flags & MPTCP_CAP_CHECKSUM_REQD)
75 			break;
76 
77 		mp_opt->mp_capable = 1;
78 		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
79 			mp_opt->sndr_key = get_unaligned_be64(ptr);
80 			ptr += 8;
81 		}
82 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
83 			mp_opt->rcvr_key = get_unaligned_be64(ptr);
84 			ptr += 8;
85 		}
86 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
87 			/* Section 3.1.:
88 			 * "the data parameters in a MP_CAPABLE are semantically
89 			 * equivalent to those in a DSS option and can be used
90 			 * interchangeably."
91 			 */
92 			mp_opt->dss = 1;
93 			mp_opt->use_map = 1;
94 			mp_opt->mpc_map = 1;
95 			mp_opt->data_len = get_unaligned_be16(ptr);
96 			ptr += 2;
97 		}
98 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
99 			 version, flags, opsize, mp_opt->sndr_key,
100 			 mp_opt->rcvr_key, mp_opt->data_len);
101 		break;
102 
103 	case MPTCPOPT_MP_JOIN:
104 		mp_opt->mp_join = 1;
105 		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
106 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
107 			mp_opt->join_id = *ptr++;
108 			mp_opt->token = get_unaligned_be32(ptr);
109 			ptr += 4;
110 			mp_opt->nonce = get_unaligned_be32(ptr);
111 			ptr += 4;
112 			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
113 				 mp_opt->backup, mp_opt->join_id,
114 				 mp_opt->token, mp_opt->nonce);
115 		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
116 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
117 			mp_opt->join_id = *ptr++;
118 			mp_opt->thmac = get_unaligned_be64(ptr);
119 			ptr += 8;
120 			mp_opt->nonce = get_unaligned_be32(ptr);
121 			ptr += 4;
122 			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
123 				 mp_opt->backup, mp_opt->join_id,
124 				 mp_opt->thmac, mp_opt->nonce);
125 		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
126 			ptr += 2;
127 			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
128 			pr_debug("MP_JOIN hmac");
129 		} else {
130 			pr_warn("MP_JOIN bad option size");
131 			mp_opt->mp_join = 0;
132 		}
133 		break;
134 
135 	case MPTCPOPT_DSS:
136 		pr_debug("DSS");
137 		ptr++;
138 
139 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
140 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
141 		 * map info accordingly
142 		 */
143 		mp_opt->mpc_map = 0;
144 		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
145 		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
146 		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
147 		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
148 		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
149 		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
150 
151 		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
152 			 mp_opt->data_fin, mp_opt->dsn64,
153 			 mp_opt->use_map, mp_opt->ack64,
154 			 mp_opt->use_ack);
155 
156 		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
157 
158 		if (mp_opt->use_ack) {
159 			if (mp_opt->ack64)
160 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
161 			else
162 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
163 		}
164 
165 		if (mp_opt->use_map) {
166 			if (mp_opt->dsn64)
167 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
168 			else
169 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
170 		}
171 
172 		/* RFC 6824, Section 3.3:
173 		 * If a checksum is present, but its use had
174 		 * not been negotiated in the MP_CAPABLE handshake,
175 		 * the checksum field MUST be ignored.
176 		 */
177 		if (opsize != expected_opsize &&
178 		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
179 			break;
180 
181 		mp_opt->dss = 1;
182 
183 		if (mp_opt->use_ack) {
184 			if (mp_opt->ack64) {
185 				mp_opt->data_ack = get_unaligned_be64(ptr);
186 				ptr += 8;
187 			} else {
188 				mp_opt->data_ack = get_unaligned_be32(ptr);
189 				ptr += 4;
190 			}
191 
192 			pr_debug("data_ack=%llu", mp_opt->data_ack);
193 		}
194 
195 		if (mp_opt->use_map) {
196 			if (mp_opt->dsn64) {
197 				mp_opt->data_seq = get_unaligned_be64(ptr);
198 				ptr += 8;
199 			} else {
200 				mp_opt->data_seq = get_unaligned_be32(ptr);
201 				ptr += 4;
202 			}
203 
204 			mp_opt->subflow_seq = get_unaligned_be32(ptr);
205 			ptr += 4;
206 
207 			mp_opt->data_len = get_unaligned_be16(ptr);
208 			ptr += 2;
209 
210 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
211 				 mp_opt->data_seq, mp_opt->subflow_seq,
212 				 mp_opt->data_len);
213 		}
214 
215 		break;
216 
217 	case MPTCPOPT_ADD_ADDR:
218 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
219 		if (!mp_opt->echo) {
220 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
221 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
222 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
223 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
224 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
225 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
226 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
227 #endif
228 			else
229 				break;
230 		} else {
231 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
232 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
233 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
234 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
235 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
236 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
237 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
238 #endif
239 			else
240 				break;
241 		}
242 
243 		mp_opt->add_addr = 1;
244 		mp_opt->addr_id = *ptr++;
245 		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
246 			memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4);
247 			ptr += 4;
248 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
249 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
250 				mp_opt->port = get_unaligned_be16(ptr);
251 				ptr += 2;
252 			}
253 		}
254 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
255 		else {
256 			memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16);
257 			ptr += 16;
258 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
259 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
260 				mp_opt->port = get_unaligned_be16(ptr);
261 				ptr += 2;
262 			}
263 		}
264 #endif
265 		if (!mp_opt->echo) {
266 			mp_opt->ahmac = get_unaligned_be64(ptr);
267 			ptr += 8;
268 		}
269 		pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
270 			 (mp_opt->family == MPTCP_ADDR_IPVERSION_6) ? "6" : "",
271 			 mp_opt->addr_id, mp_opt->ahmac, mp_opt->echo, mp_opt->port);
272 		break;
273 
274 	case MPTCPOPT_RM_ADDR:
275 		if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE)
276 			break;
277 
278 		ptr++;
279 
280 		mp_opt->rm_addr = 1;
281 		mp_opt->rm_id = *ptr++;
282 		pr_debug("RM_ADDR: id=%d", mp_opt->rm_id);
283 		break;
284 
285 	case MPTCPOPT_MP_FASTCLOSE:
286 		if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
287 			break;
288 
289 		ptr += 2;
290 		mp_opt->rcvr_key = get_unaligned_be64(ptr);
291 		ptr += 8;
292 		mp_opt->fastclose = 1;
293 		break;
294 
295 	default:
296 		break;
297 	}
298 }
299 
300 void mptcp_get_options(const struct sk_buff *skb,
301 		       struct mptcp_options_received *mp_opt)
302 {
303 	const struct tcphdr *th = tcp_hdr(skb);
304 	const unsigned char *ptr;
305 	int length;
306 
307 	/* initialize option status */
308 	mp_opt->mp_capable = 0;
309 	mp_opt->mp_join = 0;
310 	mp_opt->add_addr = 0;
311 	mp_opt->ahmac = 0;
312 	mp_opt->fastclose = 0;
313 	mp_opt->port = 0;
314 	mp_opt->rm_addr = 0;
315 	mp_opt->dss = 0;
316 
317 	length = (th->doff * 4) - sizeof(struct tcphdr);
318 	ptr = (const unsigned char *)(th + 1);
319 
320 	while (length > 0) {
321 		int opcode = *ptr++;
322 		int opsize;
323 
324 		switch (opcode) {
325 		case TCPOPT_EOL:
326 			return;
327 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
328 			length--;
329 			continue;
330 		default:
331 			opsize = *ptr++;
332 			if (opsize < 2) /* "silly options" */
333 				return;
334 			if (opsize > length)
335 				return;	/* don't parse partial options */
336 			if (opcode == TCPOPT_MPTCP)
337 				mptcp_parse_option(skb, ptr, opsize, mp_opt);
338 			ptr += opsize - 2;
339 			length -= opsize;
340 		}
341 	}
342 }
343 
344 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
345 		       unsigned int *size, struct mptcp_out_options *opts)
346 {
347 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
348 
349 	/* we will use snd_isn to detect first pkt [re]transmission
350 	 * in mptcp_established_options_mp()
351 	 */
352 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
353 	if (subflow->request_mptcp) {
354 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
355 		*size = TCPOLEN_MPTCP_MPC_SYN;
356 		return true;
357 	} else if (subflow->request_join) {
358 		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
359 			 subflow->local_nonce);
360 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
361 		opts->join_id = subflow->local_id;
362 		opts->token = subflow->remote_token;
363 		opts->nonce = subflow->local_nonce;
364 		opts->backup = subflow->request_bkup;
365 		*size = TCPOLEN_MPTCP_MPJ_SYN;
366 		return true;
367 	}
368 	return false;
369 }
370 
371 /* MP_JOIN client subflow must wait for 4th ack before sending any data:
372  * TCP can't schedule delack timer before the subflow is fully established.
373  * MPTCP uses the delack timer to do 3rd ack retransmissions
374  */
375 static void schedule_3rdack_retransmission(struct sock *sk)
376 {
377 	struct inet_connection_sock *icsk = inet_csk(sk);
378 	struct tcp_sock *tp = tcp_sk(sk);
379 	unsigned long timeout;
380 
381 	/* reschedule with a timeout above RTT, as we must look only for drop */
382 	if (tp->srtt_us)
383 		timeout = tp->srtt_us << 1;
384 	else
385 		timeout = TCP_TIMEOUT_INIT;
386 
387 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
388 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
389 	icsk->icsk_ack.timeout = timeout;
390 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
391 }
392 
393 static void clear_3rdack_retransmission(struct sock *sk)
394 {
395 	struct inet_connection_sock *icsk = inet_csk(sk);
396 
397 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
398 	icsk->icsk_ack.timeout = 0;
399 	icsk->icsk_ack.ato = 0;
400 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
401 }
402 
403 static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
404 					 unsigned int *size,
405 					 unsigned int remaining,
406 					 struct mptcp_out_options *opts)
407 {
408 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
409 	struct mptcp_ext *mpext;
410 	unsigned int data_len;
411 
412 	/* When skb is not available, we better over-estimate the emitted
413 	 * options len. A full DSS option (28 bytes) is longer than
414 	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
415 	 * tell the caller to defer the estimate to
416 	 * mptcp_established_options_dss(), which will reserve enough space.
417 	 */
418 	if (!skb)
419 		return false;
420 
421 	/* MPC/MPJ needed only on 3rd ack packet */
422 	if (subflow->fully_established ||
423 	    subflow->snd_isn != TCP_SKB_CB(skb)->seq)
424 		return false;
425 
426 	if (subflow->mp_capable) {
427 		mpext = mptcp_get_ext(skb);
428 		data_len = mpext ? mpext->data_len : 0;
429 
430 		/* we will check ext_copy.data_len in mptcp_write_options() to
431 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
432 		 * TCPOLEN_MPTCP_MPC_ACK
433 		 */
434 		opts->ext_copy.data_len = data_len;
435 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
436 		opts->sndr_key = subflow->local_key;
437 		opts->rcvr_key = subflow->remote_key;
438 
439 		/* Section 3.1.
440 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
441 		 * packets that start the first subflow of an MPTCP connection,
442 		 * as well as the first packet that carries data
443 		 */
444 		if (data_len > 0)
445 			*size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
446 		else
447 			*size = TCPOLEN_MPTCP_MPC_ACK;
448 
449 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
450 			 subflow, subflow->local_key, subflow->remote_key,
451 			 data_len);
452 
453 		return true;
454 	} else if (subflow->mp_join) {
455 		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
456 		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
457 		*size = TCPOLEN_MPTCP_MPJ_ACK;
458 		pr_debug("subflow=%p", subflow);
459 
460 		schedule_3rdack_retransmission(sk);
461 		return true;
462 	}
463 	return false;
464 }
465 
466 static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
467 				 struct sk_buff *skb, struct mptcp_ext *ext)
468 {
469 	/* The write_seq value has already been incremented, so the actual
470 	 * sequence number for the DATA_FIN is one less.
471 	 */
472 	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
473 
474 	if (!ext->use_map || !skb->len) {
475 		/* RFC6824 requires a DSS mapping with specific values
476 		 * if DATA_FIN is set but no data payload is mapped
477 		 */
478 		ext->data_fin = 1;
479 		ext->use_map = 1;
480 		ext->dsn64 = 1;
481 		ext->data_seq = data_fin_tx_seq;
482 		ext->subflow_seq = 0;
483 		ext->data_len = 1;
484 	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
485 		/* If there's an existing DSS mapping and it is the
486 		 * final mapping, DATA_FIN consumes 1 additional byte of
487 		 * mapping space.
488 		 */
489 		ext->data_fin = 1;
490 		ext->data_len++;
491 	}
492 }
493 
494 static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
495 					  unsigned int *size,
496 					  unsigned int remaining,
497 					  struct mptcp_out_options *opts)
498 {
499 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
500 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
501 	unsigned int dss_size = 0;
502 	u64 snd_data_fin_enable;
503 	struct mptcp_ext *mpext;
504 	unsigned int ack_size;
505 	bool ret = false;
506 
507 	mpext = skb ? mptcp_get_ext(skb) : NULL;
508 	snd_data_fin_enable = mptcp_data_fin_enabled(msk);
509 
510 	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
511 		unsigned int map_size;
512 
513 		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
514 
515 		remaining -= map_size;
516 		dss_size = map_size;
517 		if (mpext)
518 			opts->ext_copy = *mpext;
519 
520 		if (skb && snd_data_fin_enable)
521 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
522 		ret = true;
523 	}
524 
525 	/* passive sockets msk will set the 'can_ack' after accept(), even
526 	 * if the first subflow may have the already the remote key handy
527 	 */
528 	opts->ext_copy.use_ack = 0;
529 	if (!READ_ONCE(msk->can_ack)) {
530 		*size = ALIGN(dss_size, 4);
531 		return ret;
532 	}
533 
534 	if (READ_ONCE(msk->use_64bit_ack)) {
535 		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
536 		opts->ext_copy.data_ack = READ_ONCE(msk->ack_seq);
537 		opts->ext_copy.ack64 = 1;
538 	} else {
539 		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
540 		opts->ext_copy.data_ack32 = (uint32_t)READ_ONCE(msk->ack_seq);
541 		opts->ext_copy.ack64 = 0;
542 	}
543 	opts->ext_copy.use_ack = 1;
544 	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
545 
546 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
547 	if (dss_size == 0)
548 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
549 
550 	dss_size += ack_size;
551 
552 	*size = ALIGN(dss_size, 4);
553 	return true;
554 }
555 
556 static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
557 				  struct in_addr *addr)
558 {
559 	u8 hmac[SHA256_DIGEST_SIZE];
560 	u8 msg[7];
561 
562 	msg[0] = addr_id;
563 	memcpy(&msg[1], &addr->s_addr, 4);
564 	msg[5] = 0;
565 	msg[6] = 0;
566 
567 	mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
568 
569 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
570 }
571 
572 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
573 static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
574 				   struct in6_addr *addr)
575 {
576 	u8 hmac[SHA256_DIGEST_SIZE];
577 	u8 msg[19];
578 
579 	msg[0] = addr_id;
580 	memcpy(&msg[1], &addr->s6_addr, 16);
581 	msg[17] = 0;
582 	msg[18] = 0;
583 
584 	mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
585 
586 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
587 }
588 #endif
589 
590 static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
591 					       unsigned int *size,
592 					       unsigned int remaining,
593 					       struct mptcp_out_options *opts)
594 {
595 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
596 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
597 	bool drop_other_suboptions = false;
598 	unsigned int opt_size = *size;
599 	struct mptcp_addr_info saddr;
600 	bool echo;
601 	bool port;
602 	int len;
603 
604 	if ((mptcp_pm_should_add_signal_ipv6(msk) ||
605 	     mptcp_pm_should_add_signal_port(msk)) &&
606 	    skb && skb_is_tcp_pure_ack(skb)) {
607 		pr_debug("drop other suboptions");
608 		opts->suboptions = 0;
609 		remaining += opt_size;
610 		drop_other_suboptions = true;
611 	}
612 
613 	if (!mptcp_pm_should_add_signal(msk) ||
614 	    !(mptcp_pm_add_addr_signal(msk, remaining, &saddr, &echo, &port)))
615 		return false;
616 
617 	len = mptcp_add_addr_len(saddr.family, echo, port);
618 	if (remaining < len)
619 		return false;
620 
621 	*size = len;
622 	if (drop_other_suboptions)
623 		*size -= opt_size;
624 	opts->addr_id = saddr.id;
625 	if (port)
626 		opts->port = ntohs(saddr.port);
627 	if (saddr.family == AF_INET) {
628 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
629 		opts->addr = saddr.addr;
630 		if (!echo) {
631 			opts->ahmac = add_addr_generate_hmac(msk->local_key,
632 							     msk->remote_key,
633 							     opts->addr_id,
634 							     &opts->addr);
635 		}
636 	}
637 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
638 	else if (saddr.family == AF_INET6) {
639 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
640 		opts->addr6 = saddr.addr6;
641 		if (!echo) {
642 			opts->ahmac = add_addr6_generate_hmac(msk->local_key,
643 							      msk->remote_key,
644 							      opts->addr_id,
645 							      &opts->addr6);
646 		}
647 	}
648 #endif
649 	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
650 		 opts->addr_id, opts->ahmac, echo, opts->port);
651 
652 	return true;
653 }
654 
655 static bool mptcp_established_options_rm_addr(struct sock *sk,
656 					      unsigned int *size,
657 					      unsigned int remaining,
658 					      struct mptcp_out_options *opts)
659 {
660 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
661 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
662 	u8 rm_id;
663 
664 	if (!mptcp_pm_should_rm_signal(msk) ||
665 	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_id)))
666 		return false;
667 
668 	if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE)
669 		return false;
670 
671 	*size = TCPOLEN_MPTCP_RM_ADDR_BASE;
672 	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
673 	opts->rm_id = rm_id;
674 
675 	pr_debug("rm_id=%d", opts->rm_id);
676 
677 	return true;
678 }
679 
680 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
681 			       unsigned int *size, unsigned int remaining,
682 			       struct mptcp_out_options *opts)
683 {
684 	unsigned int opt_size = 0;
685 	bool ret = false;
686 
687 	opts->suboptions = 0;
688 
689 	if (unlikely(mptcp_check_fallback(sk)))
690 		return false;
691 
692 	/* prevent adding of any MPTCP related options on reset packet
693 	 * until we support MP_TCPRST/MP_FASTCLOSE
694 	 */
695 	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
696 		return false;
697 
698 	if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
699 		ret = true;
700 	else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
701 					       opts))
702 		ret = true;
703 
704 	/* we reserved enough space for the above options, and exceeding the
705 	 * TCP option space would be fatal
706 	 */
707 	if (WARN_ON_ONCE(opt_size > remaining))
708 		return false;
709 
710 	*size += opt_size;
711 	remaining -= opt_size;
712 	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
713 		*size += opt_size;
714 		remaining -= opt_size;
715 		ret = true;
716 	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
717 		*size += opt_size;
718 		remaining -= opt_size;
719 		ret = true;
720 	}
721 
722 	return ret;
723 }
724 
725 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
726 			  struct mptcp_out_options *opts)
727 {
728 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
729 
730 	if (subflow_req->mp_capable) {
731 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
732 		opts->sndr_key = subflow_req->local_key;
733 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
734 		pr_debug("subflow_req=%p, local_key=%llu",
735 			 subflow_req, subflow_req->local_key);
736 		return true;
737 	} else if (subflow_req->mp_join) {
738 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
739 		opts->backup = subflow_req->backup;
740 		opts->join_id = subflow_req->local_id;
741 		opts->thmac = subflow_req->thmac;
742 		opts->nonce = subflow_req->local_nonce;
743 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
744 			 subflow_req, opts->backup, opts->join_id,
745 			 opts->thmac, opts->nonce);
746 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
747 		return true;
748 	}
749 	return false;
750 }
751 
752 static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
753 				    struct mptcp_subflow_context *subflow,
754 				    struct sk_buff *skb,
755 				    struct mptcp_options_received *mp_opt)
756 {
757 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
758 	 * will make the subflow fully established
759 	 */
760 	if (likely(subflow->fully_established)) {
761 		/* on passive sockets, check for 3rd ack retransmission
762 		 * note that msk is always set by subflow_syn_recv_sock()
763 		 * for mp_join subflows
764 		 */
765 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
766 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
767 		    subflow->mp_join && mp_opt->mp_join &&
768 		    READ_ONCE(msk->pm.server_side))
769 			tcp_send_ack(ssk);
770 		goto fully_established;
771 	}
772 
773 	/* we must process OoO packets before the first subflow is fully
774 	 * established. OoO packets are instead a protocol violation
775 	 * for MP_JOIN subflows as the peer must not send any data
776 	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
777 	 */
778 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
779 		if (subflow->mp_join)
780 			goto reset;
781 		return subflow->mp_capable;
782 	}
783 
784 	if (mp_opt->dss && mp_opt->use_ack) {
785 		/* subflows are fully established as soon as we get any
786 		 * additional ack.
787 		 */
788 		subflow->fully_established = 1;
789 		WRITE_ONCE(msk->fully_established, true);
790 		goto fully_established;
791 	}
792 
793 	if (mp_opt->add_addr) {
794 		WRITE_ONCE(msk->fully_established, true);
795 		return true;
796 	}
797 
798 	/* If the first established packet does not contain MP_CAPABLE + data
799 	 * then fallback to TCP. Fallback scenarios requires a reset for
800 	 * MP_JOIN subflows.
801 	 */
802 	if (!mp_opt->mp_capable) {
803 		if (subflow->mp_join)
804 			goto reset;
805 		subflow->mp_capable = 0;
806 		pr_fallback(msk);
807 		__mptcp_do_fallback(msk);
808 		return false;
809 	}
810 
811 	if (unlikely(!READ_ONCE(msk->pm.server_side)))
812 		pr_warn_once("bogus mpc option on established client sk");
813 	mptcp_subflow_fully_established(subflow, mp_opt);
814 
815 fully_established:
816 	/* if the subflow is not already linked into the conn_list, we can't
817 	 * notify the PM: this subflow is still on the listener queue
818 	 * and the PM possibly acquiring the subflow lock could race with
819 	 * the listener close
820 	 */
821 	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
822 		return true;
823 
824 	subflow->pm_notified = 1;
825 	if (subflow->mp_join) {
826 		clear_3rdack_retransmission(ssk);
827 		mptcp_pm_subflow_established(msk, subflow);
828 	} else {
829 		mptcp_pm_fully_established(msk);
830 	}
831 	return true;
832 
833 reset:
834 	mptcp_subflow_reset(ssk);
835 	return false;
836 }
837 
838 static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
839 {
840 	u32 old_ack32, cur_ack32;
841 
842 	if (use_64bit)
843 		return cur_ack;
844 
845 	old_ack32 = (u32)old_ack;
846 	cur_ack32 = (u32)cur_ack;
847 	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
848 	if (unlikely(before(cur_ack32, old_ack32)))
849 		return cur_ack + (1LL << 32);
850 	return cur_ack;
851 }
852 
853 static void ack_update_msk(struct mptcp_sock *msk,
854 			   struct sock *ssk,
855 			   struct mptcp_options_received *mp_opt)
856 {
857 	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
858 	struct sock *sk = (struct sock *)msk;
859 	u64 old_snd_una;
860 
861 	mptcp_data_lock(sk);
862 
863 	/* avoid ack expansion on update conflict, to reduce the risk of
864 	 * wrongly expanding to a future ack sequence number, which is way
865 	 * more dangerous than missing an ack
866 	 */
867 	old_snd_una = msk->snd_una;
868 	new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
869 
870 	/* ACK for data not even sent yet? Ignore. */
871 	if (after64(new_snd_una, snd_nxt))
872 		new_snd_una = old_snd_una;
873 
874 	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
875 
876 	if (after64(new_wnd_end, msk->wnd_end)) {
877 		msk->wnd_end = new_wnd_end;
878 		__mptcp_wnd_updated(sk, ssk);
879 	}
880 
881 	if (after64(new_snd_una, old_snd_una)) {
882 		msk->snd_una = new_snd_una;
883 		__mptcp_data_acked(sk);
884 	}
885 	mptcp_data_unlock(sk);
886 }
887 
888 bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
889 {
890 	/* Skip if DATA_FIN was already received.
891 	 * If updating simultaneously with the recvmsg loop, values
892 	 * should match. If they mismatch, the peer is misbehaving and
893 	 * we will prefer the most recent information.
894 	 */
895 	if (READ_ONCE(msk->rcv_data_fin) || !READ_ONCE(msk->first))
896 		return false;
897 
898 	WRITE_ONCE(msk->rcv_data_fin_seq,
899 		   expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
900 	WRITE_ONCE(msk->rcv_data_fin, 1);
901 
902 	return true;
903 }
904 
905 static bool add_addr_hmac_valid(struct mptcp_sock *msk,
906 				struct mptcp_options_received *mp_opt)
907 {
908 	u64 hmac = 0;
909 
910 	if (mp_opt->echo)
911 		return true;
912 
913 	if (mp_opt->family == MPTCP_ADDR_IPVERSION_4)
914 		hmac = add_addr_generate_hmac(msk->remote_key,
915 					      msk->local_key,
916 					      mp_opt->addr_id, &mp_opt->addr);
917 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
918 	else
919 		hmac = add_addr6_generate_hmac(msk->remote_key,
920 					       msk->local_key,
921 					       mp_opt->addr_id, &mp_opt->addr6);
922 #endif
923 
924 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
925 		 msk, (unsigned long long)hmac,
926 		 (unsigned long long)mp_opt->ahmac);
927 
928 	return hmac == mp_opt->ahmac;
929 }
930 
931 void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
932 {
933 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
934 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
935 	struct mptcp_options_received mp_opt;
936 	struct mptcp_ext *mpext;
937 
938 	if (__mptcp_check_fallback(msk)) {
939 		/* Keep it simple and unconditionally trigger send data cleanup and
940 		 * pending queue spooling. We will need to acquire the data lock
941 		 * for more accurate checks, and once the lock is acquired, such
942 		 * helpers are cheap.
943 		 */
944 		mptcp_data_lock(subflow->conn);
945 		if (mptcp_send_head(subflow->conn))
946 			__mptcp_wnd_updated(subflow->conn, sk);
947 		__mptcp_data_acked(subflow->conn);
948 		mptcp_data_unlock(subflow->conn);
949 		return;
950 	}
951 
952 	mptcp_get_options(skb, &mp_opt);
953 	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
954 		return;
955 
956 	if (mp_opt.fastclose &&
957 	    msk->local_key == mp_opt.rcvr_key) {
958 		WRITE_ONCE(msk->rcv_fastclose, true);
959 		mptcp_schedule_work((struct sock *)msk);
960 	}
961 
962 	if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
963 		struct mptcp_addr_info addr;
964 
965 		addr.port = htons(mp_opt.port);
966 		addr.id = mp_opt.addr_id;
967 		if (mp_opt.family == MPTCP_ADDR_IPVERSION_4) {
968 			addr.family = AF_INET;
969 			addr.addr = mp_opt.addr;
970 		}
971 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
972 		else if (mp_opt.family == MPTCP_ADDR_IPVERSION_6) {
973 			addr.family = AF_INET6;
974 			addr.addr6 = mp_opt.addr6;
975 		}
976 #endif
977 		if (!mp_opt.echo) {
978 			mptcp_pm_add_addr_received(msk, &addr);
979 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
980 		} else {
981 			mptcp_pm_del_add_timer(msk, &addr);
982 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
983 		}
984 		mp_opt.add_addr = 0;
985 	}
986 
987 	if (mp_opt.rm_addr) {
988 		mptcp_pm_rm_addr_received(msk, mp_opt.rm_id);
989 		mp_opt.rm_addr = 0;
990 	}
991 
992 	if (!mp_opt.dss)
993 		return;
994 
995 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
996 	 * monodirectional flows will stuck
997 	 */
998 	if (mp_opt.use_ack)
999 		ack_update_msk(msk, sk, &mp_opt);
1000 
1001 	/* Zero-data-length packets are dropped by the caller and not
1002 	 * propagated to the MPTCP layer, so the skb extension does not
1003 	 * need to be allocated or populated. DATA_FIN information, if
1004 	 * present, needs to be updated here before the skb is freed.
1005 	 */
1006 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1007 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1008 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
1009 		    schedule_work(&msk->work))
1010 			sock_hold(subflow->conn);
1011 
1012 		return;
1013 	}
1014 
1015 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1016 	if (!mpext)
1017 		return;
1018 
1019 	memset(mpext, 0, sizeof(*mpext));
1020 
1021 	if (mp_opt.use_map) {
1022 		if (mp_opt.mpc_map) {
1023 			/* this is an MP_CAPABLE carrying MPTCP data
1024 			 * we know this map the first chunk of data
1025 			 */
1026 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
1027 					     &mpext->data_seq);
1028 			mpext->data_seq++;
1029 			mpext->subflow_seq = 1;
1030 			mpext->dsn64 = 1;
1031 			mpext->mpc_map = 1;
1032 			mpext->data_fin = 0;
1033 		} else {
1034 			mpext->data_seq = mp_opt.data_seq;
1035 			mpext->subflow_seq = mp_opt.subflow_seq;
1036 			mpext->dsn64 = mp_opt.dsn64;
1037 			mpext->data_fin = mp_opt.data_fin;
1038 		}
1039 		mpext->data_len = mp_opt.data_len;
1040 		mpext->use_map = 1;
1041 	}
1042 }
1043 
1044 static void mptcp_set_rwin(const struct tcp_sock *tp)
1045 {
1046 	const struct sock *ssk = (const struct sock *)tp;
1047 	const struct mptcp_subflow_context *subflow;
1048 	struct mptcp_sock *msk;
1049 	u64 ack_seq;
1050 
1051 	subflow = mptcp_subflow_ctx(ssk);
1052 	msk = mptcp_sk(subflow->conn);
1053 
1054 	ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1055 
1056 	if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1057 		WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1058 }
1059 
1060 void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1061 			 struct mptcp_out_options *opts)
1062 {
1063 	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
1064 	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
1065 		u8 len;
1066 
1067 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
1068 			len = TCPOLEN_MPTCP_MPC_SYN;
1069 		else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
1070 			len = TCPOLEN_MPTCP_MPC_SYNACK;
1071 		else if (opts->ext_copy.data_len)
1072 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1073 		else
1074 			len = TCPOLEN_MPTCP_MPC_ACK;
1075 
1076 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1077 				      MPTCP_SUPPORTED_VERSION,
1078 				      MPTCP_CAP_HMAC_SHA256);
1079 
1080 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1081 		    opts->suboptions))
1082 			goto mp_capable_done;
1083 
1084 		put_unaligned_be64(opts->sndr_key, ptr);
1085 		ptr += 2;
1086 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1087 			goto mp_capable_done;
1088 
1089 		put_unaligned_be64(opts->rcvr_key, ptr);
1090 		ptr += 2;
1091 		if (!opts->ext_copy.data_len)
1092 			goto mp_capable_done;
1093 
1094 		put_unaligned_be32(opts->ext_copy.data_len << 16 |
1095 				   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1096 		ptr += 1;
1097 	}
1098 
1099 mp_capable_done:
1100 	if ((OPTION_MPTCP_ADD_ADDR
1101 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1102 	     | OPTION_MPTCP_ADD_ADDR6
1103 #endif
1104 	    ) & opts->suboptions) {
1105 		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1106 		u8 echo = MPTCP_ADDR_ECHO;
1107 
1108 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1109 		if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions)
1110 			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1111 #endif
1112 
1113 		if (opts->port)
1114 			len += TCPOLEN_MPTCP_PORT_LEN;
1115 
1116 		if (opts->ahmac) {
1117 			len += sizeof(opts->ahmac);
1118 			echo = 0;
1119 		}
1120 
1121 		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1122 				      len, echo, opts->addr_id);
1123 		if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1124 			memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4);
1125 			ptr += 1;
1126 		}
1127 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1128 		else if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) {
1129 			memcpy((u8 *)ptr, opts->addr6.s6_addr, 16);
1130 			ptr += 4;
1131 		}
1132 #endif
1133 
1134 		if (!opts->port) {
1135 			if (opts->ahmac) {
1136 				put_unaligned_be64(opts->ahmac, ptr);
1137 				ptr += 2;
1138 			}
1139 		} else {
1140 			if (opts->ahmac) {
1141 				u8 *bptr = (u8 *)ptr;
1142 
1143 				put_unaligned_be16(opts->port, bptr);
1144 				bptr += 2;
1145 				put_unaligned_be64(opts->ahmac, bptr);
1146 				bptr += 8;
1147 				put_unaligned_be16(TCPOPT_NOP << 8 |
1148 						   TCPOPT_NOP, bptr);
1149 
1150 				ptr += 3;
1151 			} else {
1152 				put_unaligned_be32(opts->port << 16 |
1153 						   TCPOPT_NOP << 8 |
1154 						   TCPOPT_NOP, ptr);
1155 				ptr += 1;
1156 			}
1157 		}
1158 	}
1159 
1160 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1161 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1162 				      TCPOLEN_MPTCP_RM_ADDR_BASE,
1163 				      0, opts->rm_id);
1164 	}
1165 
1166 	if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1167 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1168 				      TCPOLEN_MPTCP_MPJ_SYN,
1169 				      opts->backup, opts->join_id);
1170 		put_unaligned_be32(opts->token, ptr);
1171 		ptr += 1;
1172 		put_unaligned_be32(opts->nonce, ptr);
1173 		ptr += 1;
1174 	}
1175 
1176 	if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1177 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1178 				      TCPOLEN_MPTCP_MPJ_SYNACK,
1179 				      opts->backup, opts->join_id);
1180 		put_unaligned_be64(opts->thmac, ptr);
1181 		ptr += 2;
1182 		put_unaligned_be32(opts->nonce, ptr);
1183 		ptr += 1;
1184 	}
1185 
1186 	if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1187 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1188 				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1189 		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1190 		ptr += 5;
1191 	}
1192 
1193 	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
1194 		struct mptcp_ext *mpext = &opts->ext_copy;
1195 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
1196 		u8 flags = 0;
1197 
1198 		if (mpext->use_ack) {
1199 			flags = MPTCP_DSS_HAS_ACK;
1200 			if (mpext->ack64) {
1201 				len += TCPOLEN_MPTCP_DSS_ACK64;
1202 				flags |= MPTCP_DSS_ACK64;
1203 			} else {
1204 				len += TCPOLEN_MPTCP_DSS_ACK32;
1205 			}
1206 		}
1207 
1208 		if (mpext->use_map) {
1209 			len += TCPOLEN_MPTCP_DSS_MAP64;
1210 
1211 			/* Use only 64-bit mapping flags for now, add
1212 			 * support for optional 32-bit mappings later.
1213 			 */
1214 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1215 			if (mpext->data_fin)
1216 				flags |= MPTCP_DSS_DATA_FIN;
1217 		}
1218 
1219 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1220 
1221 		if (mpext->use_ack) {
1222 			if (mpext->ack64) {
1223 				put_unaligned_be64(mpext->data_ack, ptr);
1224 				ptr += 2;
1225 			} else {
1226 				put_unaligned_be32(mpext->data_ack32, ptr);
1227 				ptr += 1;
1228 			}
1229 		}
1230 
1231 		if (mpext->use_map) {
1232 			put_unaligned_be64(mpext->data_seq, ptr);
1233 			ptr += 2;
1234 			put_unaligned_be32(mpext->subflow_seq, ptr);
1235 			ptr += 1;
1236 			put_unaligned_be32(mpext->data_len << 16 |
1237 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1238 		}
1239 	}
1240 
1241 	if (tp)
1242 		mptcp_set_rwin(tp);
1243 }
1244