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