xref: /openbmc/linux/net/mptcp/options.c (revision c85adced953af8eb443852c12e8ea1142de91b7c)
1eda7acddSPeter Krystad // SPDX-License-Identifier: GPL-2.0
2eda7acddSPeter Krystad /* Multipath TCP
3eda7acddSPeter Krystad  *
4eda7acddSPeter Krystad  * Copyright (c) 2017 - 2019, Intel Corporation.
5eda7acddSPeter Krystad  */
6eda7acddSPeter Krystad 
7*c85adcedSGeliang Tang #define pr_fmt(fmt) "MPTCP: " fmt
8*c85adcedSGeliang Tang 
9eda7acddSPeter Krystad #include <linux/kernel.h>
10eda7acddSPeter Krystad #include <net/tcp.h>
11eda7acddSPeter Krystad #include <net/mptcp.h>
12eda7acddSPeter Krystad #include "protocol.h"
13eda7acddSPeter Krystad 
1465492c5aSPaolo Abeni static bool mptcp_cap_flag_sha256(u8 flags)
1565492c5aSPaolo Abeni {
1665492c5aSPaolo Abeni 	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
1765492c5aSPaolo Abeni }
1865492c5aSPaolo Abeni 
19cc7972eaSChristoph Paasch void mptcp_parse_option(const struct sk_buff *skb, const unsigned char *ptr,
20cc7972eaSChristoph Paasch 			int opsize, struct tcp_options_received *opt_rx)
21eda7acddSPeter Krystad {
22eda7acddSPeter Krystad 	struct mptcp_options_received *mp_opt = &opt_rx->mptcp;
23eda7acddSPeter Krystad 	u8 subtype = *ptr >> 4;
24648ef4b8SMat Martineau 	int expected_opsize;
25eda7acddSPeter Krystad 	u8 version;
26eda7acddSPeter Krystad 	u8 flags;
27eda7acddSPeter Krystad 
28eda7acddSPeter Krystad 	switch (subtype) {
29eda7acddSPeter Krystad 	case MPTCPOPT_MP_CAPABLE:
30cc7972eaSChristoph Paasch 		/* strict size checking */
31cc7972eaSChristoph Paasch 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
32cc7972eaSChristoph Paasch 			if (skb->len > tcp_hdr(skb)->doff << 2)
33cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
34cc7972eaSChristoph Paasch 			else
35cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
36cc7972eaSChristoph Paasch 		} else {
37cc7972eaSChristoph Paasch 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
38cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
39cc7972eaSChristoph Paasch 			else
40cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
41cc7972eaSChristoph Paasch 		}
42cc7972eaSChristoph Paasch 		if (opsize != expected_opsize)
43eda7acddSPeter Krystad 			break;
44eda7acddSPeter Krystad 
45cc7972eaSChristoph Paasch 		/* try to be gentle vs future versions on the initial syn */
46eda7acddSPeter Krystad 		version = *ptr++ & MPTCP_VERSION_MASK;
47cc7972eaSChristoph Paasch 		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
48eda7acddSPeter Krystad 			if (version != MPTCP_SUPPORTED_VERSION)
49eda7acddSPeter Krystad 				break;
50cc7972eaSChristoph Paasch 		} else if (version < MPTCP_SUPPORTED_VERSION) {
51cc7972eaSChristoph Paasch 			break;
52cc7972eaSChristoph Paasch 		}
53eda7acddSPeter Krystad 
54eda7acddSPeter Krystad 		flags = *ptr++;
5565492c5aSPaolo Abeni 		if (!mptcp_cap_flag_sha256(flags) ||
56eda7acddSPeter Krystad 		    (flags & MPTCP_CAP_EXTENSIBILITY))
57eda7acddSPeter Krystad 			break;
58eda7acddSPeter Krystad 
59eda7acddSPeter Krystad 		/* RFC 6824, Section 3.1:
60eda7acddSPeter Krystad 		 * "For the Checksum Required bit (labeled "A"), if either
61eda7acddSPeter Krystad 		 * host requires the use of checksums, checksums MUST be used.
62eda7acddSPeter Krystad 		 * In other words, the only way for checksums not to be used
63eda7acddSPeter Krystad 		 * is if both hosts in their SYNs set A=0."
64eda7acddSPeter Krystad 		 *
65eda7acddSPeter Krystad 		 * Section 3.3.0:
66eda7acddSPeter Krystad 		 * "If a checksum is not present when its use has been
67eda7acddSPeter Krystad 		 * negotiated, the receiver MUST close the subflow with a RST as
68eda7acddSPeter Krystad 		 * it is considered broken."
69eda7acddSPeter Krystad 		 *
70eda7acddSPeter Krystad 		 * We don't implement DSS checksum - fall back to TCP.
71eda7acddSPeter Krystad 		 */
72eda7acddSPeter Krystad 		if (flags & MPTCP_CAP_CHECKSUM_REQD)
73eda7acddSPeter Krystad 			break;
74eda7acddSPeter Krystad 
75eda7acddSPeter Krystad 		mp_opt->mp_capable = 1;
76cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
77eda7acddSPeter Krystad 			mp_opt->sndr_key = get_unaligned_be64(ptr);
78eda7acddSPeter Krystad 			ptr += 8;
79cc7972eaSChristoph Paasch 		}
80cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
81eda7acddSPeter Krystad 			mp_opt->rcvr_key = get_unaligned_be64(ptr);
82eda7acddSPeter Krystad 			ptr += 8;
83eda7acddSPeter Krystad 		}
84cc7972eaSChristoph Paasch 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
85cc7972eaSChristoph Paasch 			/* Section 3.1.:
86cc7972eaSChristoph Paasch 			 * "the data parameters in a MP_CAPABLE are semantically
87cc7972eaSChristoph Paasch 			 * equivalent to those in a DSS option and can be used
88cc7972eaSChristoph Paasch 			 * interchangeably."
89cc7972eaSChristoph Paasch 			 */
90cc7972eaSChristoph Paasch 			mp_opt->dss = 1;
91cc7972eaSChristoph Paasch 			mp_opt->use_map = 1;
92cc7972eaSChristoph Paasch 			mp_opt->mpc_map = 1;
93cc7972eaSChristoph Paasch 			mp_opt->data_len = get_unaligned_be16(ptr);
94cc7972eaSChristoph Paasch 			ptr += 2;
95cc7972eaSChristoph Paasch 		}
96cc7972eaSChristoph Paasch 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
97cc7972eaSChristoph Paasch 			 version, flags, opsize, mp_opt->sndr_key,
98cc7972eaSChristoph Paasch 			 mp_opt->rcvr_key, mp_opt->data_len);
99eda7acddSPeter Krystad 		break;
100eda7acddSPeter Krystad 
101f296234cSPeter Krystad 	case MPTCPOPT_MP_JOIN:
102f296234cSPeter Krystad 		mp_opt->mp_join = 1;
103f296234cSPeter Krystad 		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
104f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
105f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
106f296234cSPeter Krystad 			mp_opt->token = get_unaligned_be32(ptr);
107f296234cSPeter Krystad 			ptr += 4;
108f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
109f296234cSPeter Krystad 			ptr += 4;
110f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
111f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
112f296234cSPeter Krystad 				 mp_opt->token, mp_opt->nonce);
113f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
114f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
115f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
116f296234cSPeter Krystad 			mp_opt->thmac = get_unaligned_be64(ptr);
117f296234cSPeter Krystad 			ptr += 8;
118f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
119f296234cSPeter Krystad 			ptr += 4;
120f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
121f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
122f296234cSPeter Krystad 				 mp_opt->thmac, mp_opt->nonce);
123f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
124f296234cSPeter Krystad 			ptr += 2;
125f296234cSPeter Krystad 			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
126f296234cSPeter Krystad 			pr_debug("MP_JOIN hmac");
127f296234cSPeter Krystad 		} else {
128f296234cSPeter Krystad 			pr_warn("MP_JOIN bad option size");
129f296234cSPeter Krystad 			mp_opt->mp_join = 0;
130f296234cSPeter Krystad 		}
131f296234cSPeter Krystad 		break;
132f296234cSPeter Krystad 
133eda7acddSPeter Krystad 	case MPTCPOPT_DSS:
134eda7acddSPeter Krystad 		pr_debug("DSS");
135648ef4b8SMat Martineau 		ptr++;
136648ef4b8SMat Martineau 
137cc7972eaSChristoph Paasch 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
138cc7972eaSChristoph Paasch 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
139cc7972eaSChristoph Paasch 		 * map info accordingly
140cc7972eaSChristoph Paasch 		 */
141cc7972eaSChristoph Paasch 		mp_opt->mpc_map = 0;
142648ef4b8SMat Martineau 		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
143648ef4b8SMat Martineau 		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
144648ef4b8SMat Martineau 		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
145648ef4b8SMat Martineau 		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
146648ef4b8SMat Martineau 		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
147648ef4b8SMat Martineau 		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
148648ef4b8SMat Martineau 
149648ef4b8SMat Martineau 		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
150648ef4b8SMat Martineau 			 mp_opt->data_fin, mp_opt->dsn64,
151648ef4b8SMat Martineau 			 mp_opt->use_map, mp_opt->ack64,
152648ef4b8SMat Martineau 			 mp_opt->use_ack);
153648ef4b8SMat Martineau 
154648ef4b8SMat Martineau 		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
155648ef4b8SMat Martineau 
156648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
157648ef4b8SMat Martineau 			if (mp_opt->ack64)
158648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
159648ef4b8SMat Martineau 			else
160648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
161648ef4b8SMat Martineau 		}
162648ef4b8SMat Martineau 
163648ef4b8SMat Martineau 		if (mp_opt->use_map) {
164648ef4b8SMat Martineau 			if (mp_opt->dsn64)
165648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
166648ef4b8SMat Martineau 			else
167648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
168648ef4b8SMat Martineau 		}
169648ef4b8SMat Martineau 
170648ef4b8SMat Martineau 		/* RFC 6824, Section 3.3:
171648ef4b8SMat Martineau 		 * If a checksum is present, but its use had
172648ef4b8SMat Martineau 		 * not been negotiated in the MP_CAPABLE handshake,
173648ef4b8SMat Martineau 		 * the checksum field MUST be ignored.
174648ef4b8SMat Martineau 		 */
175648ef4b8SMat Martineau 		if (opsize != expected_opsize &&
176648ef4b8SMat Martineau 		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
177648ef4b8SMat Martineau 			break;
178648ef4b8SMat Martineau 
179eda7acddSPeter Krystad 		mp_opt->dss = 1;
180648ef4b8SMat Martineau 
181648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
182648ef4b8SMat Martineau 			if (mp_opt->ack64) {
183648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be64(ptr);
184648ef4b8SMat Martineau 				ptr += 8;
185648ef4b8SMat Martineau 			} else {
186648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be32(ptr);
187648ef4b8SMat Martineau 				ptr += 4;
188648ef4b8SMat Martineau 			}
189648ef4b8SMat Martineau 
190648ef4b8SMat Martineau 			pr_debug("data_ack=%llu", mp_opt->data_ack);
191648ef4b8SMat Martineau 		}
192648ef4b8SMat Martineau 
193648ef4b8SMat Martineau 		if (mp_opt->use_map) {
194648ef4b8SMat Martineau 			if (mp_opt->dsn64) {
195648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be64(ptr);
196648ef4b8SMat Martineau 				ptr += 8;
197648ef4b8SMat Martineau 			} else {
198648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be32(ptr);
199648ef4b8SMat Martineau 				ptr += 4;
200648ef4b8SMat Martineau 			}
201648ef4b8SMat Martineau 
202648ef4b8SMat Martineau 			mp_opt->subflow_seq = get_unaligned_be32(ptr);
203648ef4b8SMat Martineau 			ptr += 4;
204648ef4b8SMat Martineau 
205648ef4b8SMat Martineau 			mp_opt->data_len = get_unaligned_be16(ptr);
206648ef4b8SMat Martineau 			ptr += 2;
207648ef4b8SMat Martineau 
208648ef4b8SMat Martineau 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
209648ef4b8SMat Martineau 				 mp_opt->data_seq, mp_opt->subflow_seq,
210648ef4b8SMat Martineau 				 mp_opt->data_len);
211648ef4b8SMat Martineau 		}
212648ef4b8SMat Martineau 
213eda7acddSPeter Krystad 		break;
214eda7acddSPeter Krystad 
2153df523abSPeter Krystad 	case MPTCPOPT_ADD_ADDR:
2163df523abSPeter Krystad 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
2173df523abSPeter Krystad 		if (!mp_opt->echo) {
2183df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
2193df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
2203df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
2213df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2223df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
2233df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
2243df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
2253df523abSPeter Krystad #endif
2263df523abSPeter Krystad 			else
2273df523abSPeter Krystad 				break;
2283df523abSPeter Krystad 		} else {
2293df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
2303df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
2313df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
2323df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2333df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
2343df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
2353df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
2363df523abSPeter Krystad #endif
2373df523abSPeter Krystad 			else
2383df523abSPeter Krystad 				break;
2393df523abSPeter Krystad 		}
2403df523abSPeter Krystad 
2413df523abSPeter Krystad 		mp_opt->add_addr = 1;
2423df523abSPeter Krystad 		mp_opt->port = 0;
2433df523abSPeter Krystad 		mp_opt->addr_id = *ptr++;
2443df523abSPeter Krystad 		pr_debug("ADD_ADDR: id=%d", mp_opt->addr_id);
2453df523abSPeter Krystad 		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
2463df523abSPeter Krystad 			memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4);
2473df523abSPeter Krystad 			ptr += 4;
2483df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
2493df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
2503df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
2513df523abSPeter Krystad 				ptr += 2;
2523df523abSPeter Krystad 			}
2533df523abSPeter Krystad 		}
2543df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2553df523abSPeter Krystad 		else {
2563df523abSPeter Krystad 			memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16);
2573df523abSPeter Krystad 			ptr += 16;
2583df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
2593df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
2603df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
2613df523abSPeter Krystad 				ptr += 2;
2623df523abSPeter Krystad 			}
2633df523abSPeter Krystad 		}
2643df523abSPeter Krystad #endif
2653df523abSPeter Krystad 		if (!mp_opt->echo) {
2663df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
2673df523abSPeter Krystad 			ptr += 8;
2683df523abSPeter Krystad 		}
2693df523abSPeter Krystad 		break;
2703df523abSPeter Krystad 
2713df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
2723df523abSPeter Krystad 		if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE)
2733df523abSPeter Krystad 			break;
2743df523abSPeter Krystad 
2753df523abSPeter Krystad 		mp_opt->rm_addr = 1;
2763df523abSPeter Krystad 		mp_opt->rm_id = *ptr++;
2773df523abSPeter Krystad 		pr_debug("RM_ADDR: id=%d", mp_opt->rm_id);
2783df523abSPeter Krystad 		break;
2793df523abSPeter Krystad 
280eda7acddSPeter Krystad 	default:
281eda7acddSPeter Krystad 		break;
282eda7acddSPeter Krystad 	}
283eda7acddSPeter Krystad }
284eda7acddSPeter Krystad 
285cec37a6eSPeter Krystad void mptcp_get_options(const struct sk_buff *skb,
286cec37a6eSPeter Krystad 		       struct tcp_options_received *opt_rx)
287cec37a6eSPeter Krystad {
288cec37a6eSPeter Krystad 	const unsigned char *ptr;
289cec37a6eSPeter Krystad 	const struct tcphdr *th = tcp_hdr(skb);
290cec37a6eSPeter Krystad 	int length = (th->doff * 4) - sizeof(struct tcphdr);
291cec37a6eSPeter Krystad 
292cec37a6eSPeter Krystad 	ptr = (const unsigned char *)(th + 1);
293cec37a6eSPeter Krystad 
294cec37a6eSPeter Krystad 	while (length > 0) {
295cec37a6eSPeter Krystad 		int opcode = *ptr++;
296cec37a6eSPeter Krystad 		int opsize;
297cec37a6eSPeter Krystad 
298cec37a6eSPeter Krystad 		switch (opcode) {
299cec37a6eSPeter Krystad 		case TCPOPT_EOL:
300cec37a6eSPeter Krystad 			return;
301cec37a6eSPeter Krystad 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
302cec37a6eSPeter Krystad 			length--;
303cec37a6eSPeter Krystad 			continue;
304cec37a6eSPeter Krystad 		default:
305cec37a6eSPeter Krystad 			opsize = *ptr++;
306cec37a6eSPeter Krystad 			if (opsize < 2) /* "silly options" */
307cec37a6eSPeter Krystad 				return;
308cec37a6eSPeter Krystad 			if (opsize > length)
309cec37a6eSPeter Krystad 				return;	/* don't parse partial options */
310cec37a6eSPeter Krystad 			if (opcode == TCPOPT_MPTCP)
311cc7972eaSChristoph Paasch 				mptcp_parse_option(skb, ptr, opsize, opt_rx);
312cec37a6eSPeter Krystad 			ptr += opsize - 2;
313cec37a6eSPeter Krystad 			length -= opsize;
314cec37a6eSPeter Krystad 		}
315cec37a6eSPeter Krystad 	}
316cec37a6eSPeter Krystad }
317cec37a6eSPeter Krystad 
318cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
319cc7972eaSChristoph Paasch 		       unsigned int *size, struct mptcp_out_options *opts)
320cec37a6eSPeter Krystad {
321cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
322cec37a6eSPeter Krystad 
323cc7972eaSChristoph Paasch 	/* we will use snd_isn to detect first pkt [re]transmission
324cc7972eaSChristoph Paasch 	 * in mptcp_established_options_mp()
325cc7972eaSChristoph Paasch 	 */
326cc7972eaSChristoph Paasch 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
327cec37a6eSPeter Krystad 	if (subflow->request_mptcp) {
328cec37a6eSPeter Krystad 		pr_debug("local_key=%llu", subflow->local_key);
329cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
330cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
331cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYN;
332cec37a6eSPeter Krystad 		return true;
333ec3edaa7SPeter Krystad 	} else if (subflow->request_join) {
334ec3edaa7SPeter Krystad 		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
335ec3edaa7SPeter Krystad 			 subflow->local_nonce);
336ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
337ec3edaa7SPeter Krystad 		opts->join_id = subflow->local_id;
338ec3edaa7SPeter Krystad 		opts->token = subflow->remote_token;
339ec3edaa7SPeter Krystad 		opts->nonce = subflow->local_nonce;
340ec3edaa7SPeter Krystad 		opts->backup = subflow->request_bkup;
341ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYN;
342ec3edaa7SPeter Krystad 		return true;
343cec37a6eSPeter Krystad 	}
344cec37a6eSPeter Krystad 	return false;
345cec37a6eSPeter Krystad }
346cec37a6eSPeter Krystad 
347cec37a6eSPeter Krystad void mptcp_rcv_synsent(struct sock *sk)
348cec37a6eSPeter Krystad {
349cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
350cec37a6eSPeter Krystad 	struct tcp_sock *tp = tcp_sk(sk);
351cec37a6eSPeter Krystad 
352cec37a6eSPeter Krystad 	if (subflow->request_mptcp && tp->rx_opt.mptcp.mp_capable) {
353cec37a6eSPeter Krystad 		subflow->mp_capable = 1;
354d22f4988SChristoph Paasch 		subflow->can_ack = 1;
355cec37a6eSPeter Krystad 		subflow->remote_key = tp->rx_opt.mptcp.sndr_key;
356ec3edaa7SPeter Krystad 		pr_debug("subflow=%p, remote_key=%llu", subflow,
357ec3edaa7SPeter Krystad 			 subflow->remote_key);
358ec3edaa7SPeter Krystad 	} else if (subflow->request_join && tp->rx_opt.mptcp.mp_join) {
359ec3edaa7SPeter Krystad 		subflow->mp_join = 1;
360ec3edaa7SPeter Krystad 		subflow->thmac = tp->rx_opt.mptcp.thmac;
361ec3edaa7SPeter Krystad 		subflow->remote_nonce = tp->rx_opt.mptcp.nonce;
362ec3edaa7SPeter Krystad 		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u", subflow,
363ec3edaa7SPeter Krystad 			 subflow->thmac, subflow->remote_nonce);
364ec3edaa7SPeter Krystad 	} else if (subflow->request_mptcp) {
365cec37a6eSPeter Krystad 		tcp_sk(sk)->is_mptcp = 0;
366cec37a6eSPeter Krystad 	}
367cec37a6eSPeter Krystad }
368cec37a6eSPeter Krystad 
369ec3edaa7SPeter Krystad /* MP_JOIN client subflow must wait for 4th ack before sending any data:
370ec3edaa7SPeter Krystad  * TCP can't schedule delack timer before the subflow is fully established.
371ec3edaa7SPeter Krystad  * MPTCP uses the delack timer to do 3rd ack retransmissions
372ec3edaa7SPeter Krystad  */
373ec3edaa7SPeter Krystad static void schedule_3rdack_retransmission(struct sock *sk)
374ec3edaa7SPeter Krystad {
375ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
376ec3edaa7SPeter Krystad 	struct tcp_sock *tp = tcp_sk(sk);
377ec3edaa7SPeter Krystad 	unsigned long timeout;
378ec3edaa7SPeter Krystad 
379ec3edaa7SPeter Krystad 	/* reschedule with a timeout above RTT, as we must look only for drop */
380ec3edaa7SPeter Krystad 	if (tp->srtt_us)
381ec3edaa7SPeter Krystad 		timeout = tp->srtt_us << 1;
382ec3edaa7SPeter Krystad 	else
383ec3edaa7SPeter Krystad 		timeout = TCP_TIMEOUT_INIT;
384ec3edaa7SPeter Krystad 
385ec3edaa7SPeter Krystad 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
386ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
387ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = timeout;
388ec3edaa7SPeter Krystad 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
389ec3edaa7SPeter Krystad }
390ec3edaa7SPeter Krystad 
391ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk)
392ec3edaa7SPeter Krystad {
393ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
394ec3edaa7SPeter Krystad 
395ec3edaa7SPeter Krystad 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
396ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = 0;
397ec3edaa7SPeter Krystad 	icsk->icsk_ack.ato = 0;
398ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
399ec3edaa7SPeter Krystad }
400ec3edaa7SPeter Krystad 
401cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
402cc7972eaSChristoph Paasch 					 unsigned int *size,
4036d0060f6SMat Martineau 					 unsigned int remaining,
404cec37a6eSPeter Krystad 					 struct mptcp_out_options *opts)
405cec37a6eSPeter Krystad {
406cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
407cc7972eaSChristoph Paasch 	struct mptcp_ext *mpext;
408cc7972eaSChristoph Paasch 	unsigned int data_len;
409cec37a6eSPeter Krystad 
410ec3edaa7SPeter Krystad 	/* When skb is not available, we better over-estimate the emitted
411ec3edaa7SPeter Krystad 	 * options len. A full DSS option (28 bytes) is longer than
412ec3edaa7SPeter Krystad 	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
413ec3edaa7SPeter Krystad 	 * tell the caller to defer the estimate to
414ec3edaa7SPeter Krystad 	 * mptcp_established_options_dss(), which will reserve enough space.
415cc7972eaSChristoph Paasch 	 */
416ec3edaa7SPeter Krystad 	if (!skb)
417ec3edaa7SPeter Krystad 		return false;
418ec3edaa7SPeter Krystad 
419ec3edaa7SPeter Krystad 	/* MPC/MPJ needed only on 3rd ack packet */
420ec3edaa7SPeter Krystad 	if (subflow->fully_established ||
421ec3edaa7SPeter Krystad 	    subflow->snd_isn != TCP_SKB_CB(skb)->seq)
422ec3edaa7SPeter Krystad 		return false;
423ec3edaa7SPeter Krystad 
424ec3edaa7SPeter Krystad 	if (subflow->mp_capable) {
425cc7972eaSChristoph Paasch 		mpext = mptcp_get_ext(skb);
426cc7972eaSChristoph Paasch 		data_len = mpext ? mpext->data_len : 0;
427cc7972eaSChristoph Paasch 
428cc7972eaSChristoph Paasch 		/* we will check ext_copy.data_len in mptcp_write_options() to
429cc7972eaSChristoph Paasch 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
430cc7972eaSChristoph Paasch 		 * TCPOLEN_MPTCP_MPC_ACK
431cc7972eaSChristoph Paasch 		 */
432cc7972eaSChristoph Paasch 		opts->ext_copy.data_len = data_len;
433cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
434cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
435cec37a6eSPeter Krystad 		opts->rcvr_key = subflow->remote_key;
436cc7972eaSChristoph Paasch 
437cc7972eaSChristoph Paasch 		/* Section 3.1.
438cc7972eaSChristoph Paasch 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
439cc7972eaSChristoph Paasch 		 * packets that start the first subflow of an MPTCP connection,
440cc7972eaSChristoph Paasch 		 * as well as the first packet that carries data
441cc7972eaSChristoph Paasch 		 */
442cc7972eaSChristoph Paasch 		if (data_len > 0)
443cc7972eaSChristoph Paasch 			*size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
444cc7972eaSChristoph Paasch 		else
445cec37a6eSPeter Krystad 			*size = TCPOLEN_MPTCP_MPC_ACK;
446cc7972eaSChristoph Paasch 
447cc7972eaSChristoph Paasch 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
448cc7972eaSChristoph Paasch 			 subflow, subflow->local_key, subflow->remote_key,
449cc7972eaSChristoph Paasch 			 data_len);
450cc7972eaSChristoph Paasch 
451cec37a6eSPeter Krystad 		return true;
452ec3edaa7SPeter Krystad 	} else if (subflow->mp_join) {
453ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
454ec3edaa7SPeter Krystad 		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
455ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_ACK;
456ec3edaa7SPeter Krystad 		pr_debug("subflow=%p", subflow);
457ec3edaa7SPeter Krystad 
458ec3edaa7SPeter Krystad 		schedule_3rdack_retransmission(sk);
459ec3edaa7SPeter Krystad 		return true;
460cec37a6eSPeter Krystad 	}
461cec37a6eSPeter Krystad 	return false;
462cec37a6eSPeter Krystad }
463cec37a6eSPeter Krystad 
4646d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
4656d0060f6SMat Martineau 				 struct mptcp_ext *ext)
4666d0060f6SMat Martineau {
4676d0060f6SMat Martineau 	if (!ext->use_map) {
4686d0060f6SMat Martineau 		/* RFC6824 requires a DSS mapping with specific values
4696d0060f6SMat Martineau 		 * if DATA_FIN is set but no data payload is mapped
4706d0060f6SMat Martineau 		 */
4716d37a0b8SMat Martineau 		ext->data_fin = 1;
4726d0060f6SMat Martineau 		ext->use_map = 1;
4736d0060f6SMat Martineau 		ext->dsn64 = 1;
47476c42a29SMat Martineau 		ext->data_seq = subflow->data_fin_tx_seq;
4756d0060f6SMat Martineau 		ext->subflow_seq = 0;
4766d0060f6SMat Martineau 		ext->data_len = 1;
4776d37a0b8SMat Martineau 	} else if (ext->data_seq + ext->data_len == subflow->data_fin_tx_seq) {
4786d37a0b8SMat Martineau 		/* If there's an existing DSS mapping and it is the
4796d37a0b8SMat Martineau 		 * final mapping, DATA_FIN consumes 1 additional byte of
4806d37a0b8SMat Martineau 		 * mapping space.
4816d0060f6SMat Martineau 		 */
4826d37a0b8SMat Martineau 		ext->data_fin = 1;
4836d0060f6SMat Martineau 		ext->data_len++;
4846d0060f6SMat Martineau 	}
4856d0060f6SMat Martineau }
4866d0060f6SMat Martineau 
4876d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
4886d0060f6SMat Martineau 					  unsigned int *size,
4896d0060f6SMat Martineau 					  unsigned int remaining,
4906d0060f6SMat Martineau 					  struct mptcp_out_options *opts)
4916d0060f6SMat Martineau {
4926d0060f6SMat Martineau 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
4936d0060f6SMat Martineau 	unsigned int dss_size = 0;
4946d0060f6SMat Martineau 	struct mptcp_ext *mpext;
4956d0060f6SMat Martineau 	struct mptcp_sock *msk;
4966d0060f6SMat Martineau 	unsigned int ack_size;
497d22f4988SChristoph Paasch 	bool ret = false;
4986d0060f6SMat Martineau 	u8 tcp_fin;
4996d0060f6SMat Martineau 
5006d0060f6SMat Martineau 	if (skb) {
5016d0060f6SMat Martineau 		mpext = mptcp_get_ext(skb);
5026d0060f6SMat Martineau 		tcp_fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
5036d0060f6SMat Martineau 	} else {
5046d0060f6SMat Martineau 		mpext = NULL;
5056d0060f6SMat Martineau 		tcp_fin = 0;
5066d0060f6SMat Martineau 	}
5076d0060f6SMat Martineau 
5086d0060f6SMat Martineau 	if (!skb || (mpext && mpext->use_map) || tcp_fin) {
5096d0060f6SMat Martineau 		unsigned int map_size;
5106d0060f6SMat Martineau 
5116d0060f6SMat Martineau 		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
5126d0060f6SMat Martineau 
5136d0060f6SMat Martineau 		remaining -= map_size;
5146d0060f6SMat Martineau 		dss_size = map_size;
5156d0060f6SMat Martineau 		if (mpext)
5166d0060f6SMat Martineau 			opts->ext_copy = *mpext;
5176d0060f6SMat Martineau 
51876c42a29SMat Martineau 		if (skb && tcp_fin && subflow->data_fin_tx_enable)
5196d0060f6SMat Martineau 			mptcp_write_data_fin(subflow, &opts->ext_copy);
520d22f4988SChristoph Paasch 		ret = true;
521d22f4988SChristoph Paasch 	}
522d22f4988SChristoph Paasch 
5232398e399SPaolo Abeni 	/* passive sockets msk will set the 'can_ack' after accept(), even
5242398e399SPaolo Abeni 	 * if the first subflow may have the already the remote key handy
5252398e399SPaolo Abeni 	 */
526d22f4988SChristoph Paasch 	opts->ext_copy.use_ack = 0;
527d22f4988SChristoph Paasch 	msk = mptcp_sk(subflow->conn);
528dc093db5SPaolo Abeni 	if (!READ_ONCE(msk->can_ack)) {
529d22f4988SChristoph Paasch 		*size = ALIGN(dss_size, 4);
530d22f4988SChristoph Paasch 		return ret;
5316d0060f6SMat Martineau 	}
5326d0060f6SMat Martineau 
5336d0060f6SMat Martineau 	ack_size = TCPOLEN_MPTCP_DSS_ACK64;
5346d0060f6SMat Martineau 
5356d0060f6SMat Martineau 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
5366d0060f6SMat Martineau 	if (dss_size == 0)
5376d0060f6SMat Martineau 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
5386d0060f6SMat Martineau 
5396d0060f6SMat Martineau 	dss_size += ack_size;
5406d0060f6SMat Martineau 
541dc093db5SPaolo Abeni 	opts->ext_copy.data_ack = msk->ack_seq;
5426d0060f6SMat Martineau 	opts->ext_copy.ack64 = 1;
5436d0060f6SMat Martineau 	opts->ext_copy.use_ack = 1;
5446d0060f6SMat Martineau 
5456d0060f6SMat Martineau 	*size = ALIGN(dss_size, 4);
5466d0060f6SMat Martineau 	return true;
5476d0060f6SMat Martineau }
5486d0060f6SMat Martineau 
5493df523abSPeter Krystad static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
5503df523abSPeter Krystad 				  struct in_addr *addr)
5513df523abSPeter Krystad {
5523df523abSPeter Krystad 	u8 hmac[MPTCP_ADDR_HMAC_LEN];
5533df523abSPeter Krystad 	u8 msg[7];
5543df523abSPeter Krystad 
5553df523abSPeter Krystad 	msg[0] = addr_id;
5563df523abSPeter Krystad 	memcpy(&msg[1], &addr->s_addr, 4);
5573df523abSPeter Krystad 	msg[5] = 0;
5583df523abSPeter Krystad 	msg[6] = 0;
5593df523abSPeter Krystad 
5603df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
5613df523abSPeter Krystad 
5623df523abSPeter Krystad 	return get_unaligned_be64(hmac);
5633df523abSPeter Krystad }
5643df523abSPeter Krystad 
5653df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
5663df523abSPeter Krystad static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
5673df523abSPeter Krystad 				   struct in6_addr *addr)
5683df523abSPeter Krystad {
5693df523abSPeter Krystad 	u8 hmac[MPTCP_ADDR_HMAC_LEN];
5703df523abSPeter Krystad 	u8 msg[19];
5713df523abSPeter Krystad 
5723df523abSPeter Krystad 	msg[0] = addr_id;
5733df523abSPeter Krystad 	memcpy(&msg[1], &addr->s6_addr, 16);
5743df523abSPeter Krystad 	msg[17] = 0;
5753df523abSPeter Krystad 	msg[18] = 0;
5763df523abSPeter Krystad 
5773df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
5783df523abSPeter Krystad 
5793df523abSPeter Krystad 	return get_unaligned_be64(hmac);
5803df523abSPeter Krystad }
5813df523abSPeter Krystad #endif
5823df523abSPeter Krystad 
5833df523abSPeter Krystad static bool mptcp_established_options_addr(struct sock *sk,
5843df523abSPeter Krystad 					   unsigned int *size,
5853df523abSPeter Krystad 					   unsigned int remaining,
5863df523abSPeter Krystad 					   struct mptcp_out_options *opts)
5873df523abSPeter Krystad {
5883df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
5893df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
5901b1c7a0eSPeter Krystad 	struct mptcp_addr_info saddr;
5911b1c7a0eSPeter Krystad 	int len;
5923df523abSPeter Krystad 
5931b1c7a0eSPeter Krystad 	if (!mptcp_pm_should_signal(msk) ||
5941b1c7a0eSPeter Krystad 	    !(mptcp_pm_addr_signal(msk, remaining, &saddr)))
5953df523abSPeter Krystad 		return false;
5961b1c7a0eSPeter Krystad 
5971b1c7a0eSPeter Krystad 	len = mptcp_add_addr_len(saddr.family);
5981b1c7a0eSPeter Krystad 	if (remaining < len)
5991b1c7a0eSPeter Krystad 		return false;
6001b1c7a0eSPeter Krystad 
6011b1c7a0eSPeter Krystad 	*size = len;
6021b1c7a0eSPeter Krystad 	opts->addr_id = saddr.id;
6031b1c7a0eSPeter Krystad 	if (saddr.family == AF_INET) {
6043df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
6051b1c7a0eSPeter Krystad 		opts->addr = saddr.addr;
6063df523abSPeter Krystad 		opts->ahmac = add_addr_generate_hmac(msk->local_key,
6073df523abSPeter Krystad 						     msk->remote_key,
6083df523abSPeter Krystad 						     opts->addr_id,
6093df523abSPeter Krystad 						     &opts->addr);
6103df523abSPeter Krystad 	}
6113df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
6121b1c7a0eSPeter Krystad 	else if (saddr.family == AF_INET6) {
6133df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
6141b1c7a0eSPeter Krystad 		opts->addr6 = saddr.addr6;
6153df523abSPeter Krystad 		opts->ahmac = add_addr6_generate_hmac(msk->local_key,
6163df523abSPeter Krystad 						      msk->remote_key,
6173df523abSPeter Krystad 						      opts->addr_id,
6183df523abSPeter Krystad 						      &opts->addr6);
6193df523abSPeter Krystad 	}
6203df523abSPeter Krystad #endif
6213df523abSPeter Krystad 	pr_debug("addr_id=%d, ahmac=%llu", opts->addr_id, opts->ahmac);
6223df523abSPeter Krystad 
6233df523abSPeter Krystad 	return true;
6243df523abSPeter Krystad }
6253df523abSPeter Krystad 
6266d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
6276d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
6286d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
6296d0060f6SMat Martineau {
6306d0060f6SMat Martineau 	unsigned int opt_size = 0;
6316d0060f6SMat Martineau 	bool ret = false;
6326d0060f6SMat Martineau 
6333df523abSPeter Krystad 	opts->suboptions = 0;
6343df523abSPeter Krystad 
635cc7972eaSChristoph Paasch 	if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
6366d0060f6SMat Martineau 		ret = true;
6376d0060f6SMat Martineau 	else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
6386d0060f6SMat Martineau 					       opts))
6396d0060f6SMat Martineau 		ret = true;
6406d0060f6SMat Martineau 
6416d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
6426d0060f6SMat Martineau 	 * TCP option space would be fatal
6436d0060f6SMat Martineau 	 */
6446d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
6456d0060f6SMat Martineau 		return false;
6466d0060f6SMat Martineau 
6476d0060f6SMat Martineau 	*size += opt_size;
6486d0060f6SMat Martineau 	remaining -= opt_size;
6493df523abSPeter Krystad 	if (mptcp_established_options_addr(sk, &opt_size, remaining, opts)) {
6503df523abSPeter Krystad 		*size += opt_size;
6513df523abSPeter Krystad 		remaining -= opt_size;
6523df523abSPeter Krystad 		ret = true;
6533df523abSPeter Krystad 	}
6546d0060f6SMat Martineau 
6556d0060f6SMat Martineau 	return ret;
6566d0060f6SMat Martineau }
6576d0060f6SMat Martineau 
658cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
659cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
660cec37a6eSPeter Krystad {
661cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
662cec37a6eSPeter Krystad 
663cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
664cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
665cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
666cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
667cec37a6eSPeter Krystad 		pr_debug("subflow_req=%p, local_key=%llu",
668cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
669cec37a6eSPeter Krystad 		return true;
670f296234cSPeter Krystad 	} else if (subflow_req->mp_join) {
671f296234cSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
672f296234cSPeter Krystad 		opts->backup = subflow_req->backup;
673f296234cSPeter Krystad 		opts->join_id = subflow_req->local_id;
674f296234cSPeter Krystad 		opts->thmac = subflow_req->thmac;
675f296234cSPeter Krystad 		opts->nonce = subflow_req->local_nonce;
676f296234cSPeter Krystad 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
677f296234cSPeter Krystad 			 subflow_req, opts->backup, opts->join_id,
678f296234cSPeter Krystad 			 opts->thmac, opts->nonce);
679f296234cSPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
680f296234cSPeter Krystad 		return true;
681cec37a6eSPeter Krystad 	}
682cec37a6eSPeter Krystad 	return false;
683cec37a6eSPeter Krystad }
684cec37a6eSPeter Krystad 
685f296234cSPeter Krystad static bool check_fully_established(struct mptcp_sock *msk, struct sock *sk,
686f296234cSPeter Krystad 				    struct mptcp_subflow_context *subflow,
687d22f4988SChristoph Paasch 				    struct sk_buff *skb,
688d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
689d22f4988SChristoph Paasch {
690d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
691f296234cSPeter Krystad 	 * will make the subflow fully established
692d22f4988SChristoph Paasch 	 */
693f296234cSPeter Krystad 	if (likely(subflow->fully_established)) {
694f296234cSPeter Krystad 		/* on passive sockets, check for 3rd ack retransmission
695f296234cSPeter Krystad 		 * note that msk is always set by subflow_syn_recv_sock()
696f296234cSPeter Krystad 		 * for mp_join subflows
697f296234cSPeter Krystad 		 */
698f296234cSPeter Krystad 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
699f296234cSPeter Krystad 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
700f296234cSPeter Krystad 		    subflow->mp_join && mp_opt->mp_join &&
701f296234cSPeter Krystad 		    READ_ONCE(msk->pm.server_side))
702f296234cSPeter Krystad 			tcp_send_ack(sk);
703f296234cSPeter Krystad 		goto fully_established;
704f296234cSPeter Krystad 	}
705d22f4988SChristoph Paasch 
706f296234cSPeter Krystad 	/* we should process OoO packets before the first subflow is fully
707f296234cSPeter Krystad 	 * established, but not expected for MP_JOIN subflows
708f296234cSPeter Krystad 	 */
709f296234cSPeter Krystad 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1)
710f296234cSPeter Krystad 		return subflow->mp_capable;
711f296234cSPeter Krystad 
712f296234cSPeter Krystad 	if (mp_opt->use_ack) {
713f296234cSPeter Krystad 		/* subflows are fully established as soon as we get any
714f296234cSPeter Krystad 		 * additional ack.
715f296234cSPeter Krystad 		 */
7160be534f5SPaolo Abeni 		subflow->fully_established = 1;
717f296234cSPeter Krystad 		goto fully_established;
718f296234cSPeter Krystad 	}
719d22f4988SChristoph Paasch 
720f296234cSPeter Krystad 	WARN_ON_ONCE(subflow->can_ack);
721d22f4988SChristoph Paasch 
722d22f4988SChristoph Paasch 	/* If the first established packet does not contain MP_CAPABLE + data
723d22f4988SChristoph Paasch 	 * then fallback to TCP
724d22f4988SChristoph Paasch 	 */
725d22f4988SChristoph Paasch 	if (!mp_opt->mp_capable) {
726d22f4988SChristoph Paasch 		subflow->mp_capable = 0;
727f296234cSPeter Krystad 		tcp_sk(sk)->is_mptcp = 0;
728d22f4988SChristoph Paasch 		return false;
729d22f4988SChristoph Paasch 	}
730f296234cSPeter Krystad 
731f296234cSPeter Krystad 	subflow->fully_established = 1;
732d22f4988SChristoph Paasch 	subflow->remote_key = mp_opt->sndr_key;
733d22f4988SChristoph Paasch 	subflow->can_ack = 1;
734f296234cSPeter Krystad 
735f296234cSPeter Krystad fully_established:
736f296234cSPeter Krystad 	if (likely(subflow->pm_notified))
737f296234cSPeter Krystad 		return true;
738f296234cSPeter Krystad 
739f296234cSPeter Krystad 	subflow->pm_notified = 1;
740ec3edaa7SPeter Krystad 	if (subflow->mp_join) {
741ec3edaa7SPeter Krystad 		clear_3rdack_retransmission(sk);
742f296234cSPeter Krystad 		mptcp_pm_subflow_established(msk, subflow);
743ec3edaa7SPeter Krystad 	} else {
744f296234cSPeter Krystad 		mptcp_pm_fully_established(msk);
745ec3edaa7SPeter Krystad 	}
746d22f4988SChristoph Paasch 	return true;
747d22f4988SChristoph Paasch }
748d22f4988SChristoph Paasch 
749cc9d2566SPaolo Abeni static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
750cc9d2566SPaolo Abeni {
751cc9d2566SPaolo Abeni 	u32 old_ack32, cur_ack32;
752cc9d2566SPaolo Abeni 
753cc9d2566SPaolo Abeni 	if (use_64bit)
754cc9d2566SPaolo Abeni 		return cur_ack;
755cc9d2566SPaolo Abeni 
756cc9d2566SPaolo Abeni 	old_ack32 = (u32)old_ack;
757cc9d2566SPaolo Abeni 	cur_ack32 = (u32)cur_ack;
758cc9d2566SPaolo Abeni 	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
759cc9d2566SPaolo Abeni 	if (unlikely(before(cur_ack32, old_ack32)))
760cc9d2566SPaolo Abeni 		return cur_ack + (1LL << 32);
761cc9d2566SPaolo Abeni 	return cur_ack;
762cc9d2566SPaolo Abeni }
763cc9d2566SPaolo Abeni 
764cc9d2566SPaolo Abeni static void update_una(struct mptcp_sock *msk,
765cc9d2566SPaolo Abeni 		       struct mptcp_options_received *mp_opt)
766cc9d2566SPaolo Abeni {
767cc9d2566SPaolo Abeni 	u64 new_snd_una, snd_una, old_snd_una = atomic64_read(&msk->snd_una);
768cc9d2566SPaolo Abeni 	u64 write_seq = READ_ONCE(msk->write_seq);
769cc9d2566SPaolo Abeni 
770cc9d2566SPaolo Abeni 	/* avoid ack expansion on update conflict, to reduce the risk of
771cc9d2566SPaolo Abeni 	 * wrongly expanding to a future ack sequence number, which is way
772cc9d2566SPaolo Abeni 	 * more dangerous than missing an ack
773cc9d2566SPaolo Abeni 	 */
774cc9d2566SPaolo Abeni 	new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
775cc9d2566SPaolo Abeni 
776cc9d2566SPaolo Abeni 	/* ACK for data not even sent yet? Ignore. */
777cc9d2566SPaolo Abeni 	if (after64(new_snd_una, write_seq))
778cc9d2566SPaolo Abeni 		new_snd_una = old_snd_una;
779cc9d2566SPaolo Abeni 
780cc9d2566SPaolo Abeni 	while (after64(new_snd_una, old_snd_una)) {
781cc9d2566SPaolo Abeni 		snd_una = old_snd_una;
782cc9d2566SPaolo Abeni 		old_snd_una = atomic64_cmpxchg(&msk->snd_una, snd_una,
783cc9d2566SPaolo Abeni 					       new_snd_una);
784b51f9b80SPaolo Abeni 		if (old_snd_una == snd_una) {
785b51f9b80SPaolo Abeni 			mptcp_data_acked((struct sock *)msk);
786cc9d2566SPaolo Abeni 			break;
787cc9d2566SPaolo Abeni 		}
788cc9d2566SPaolo Abeni 	}
789b51f9b80SPaolo Abeni }
790cc9d2566SPaolo Abeni 
7911b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk,
7921b1c7a0eSPeter Krystad 				struct mptcp_options_received *mp_opt)
7931b1c7a0eSPeter Krystad {
7941b1c7a0eSPeter Krystad 	u64 hmac = 0;
7951b1c7a0eSPeter Krystad 
7961b1c7a0eSPeter Krystad 	if (mp_opt->echo)
7971b1c7a0eSPeter Krystad 		return true;
7981b1c7a0eSPeter Krystad 
7991b1c7a0eSPeter Krystad 	if (mp_opt->family == MPTCP_ADDR_IPVERSION_4)
8001b1c7a0eSPeter Krystad 		hmac = add_addr_generate_hmac(msk->remote_key,
8011b1c7a0eSPeter Krystad 					      msk->local_key,
8021b1c7a0eSPeter Krystad 					      mp_opt->addr_id, &mp_opt->addr);
8031b1c7a0eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
8041b1c7a0eSPeter Krystad 	else
8051b1c7a0eSPeter Krystad 		hmac = add_addr6_generate_hmac(msk->remote_key,
8061b1c7a0eSPeter Krystad 					       msk->local_key,
8071b1c7a0eSPeter Krystad 					       mp_opt->addr_id, &mp_opt->addr6);
8081b1c7a0eSPeter Krystad #endif
8091b1c7a0eSPeter Krystad 
8101b1c7a0eSPeter Krystad 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
8111b1c7a0eSPeter Krystad 		 msk, (unsigned long long)hmac,
8121b1c7a0eSPeter Krystad 		 (unsigned long long)mp_opt->ahmac);
8131b1c7a0eSPeter Krystad 
8141b1c7a0eSPeter Krystad 	return hmac == mp_opt->ahmac;
8151b1c7a0eSPeter Krystad }
8161b1c7a0eSPeter Krystad 
817648ef4b8SMat Martineau void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
818648ef4b8SMat Martineau 			    struct tcp_options_received *opt_rx)
819648ef4b8SMat Martineau {
820d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
8211b1c7a0eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
822648ef4b8SMat Martineau 	struct mptcp_options_received *mp_opt;
823648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
824648ef4b8SMat Martineau 
825648ef4b8SMat Martineau 	mp_opt = &opt_rx->mptcp;
826f296234cSPeter Krystad 	if (!check_fully_established(msk, sk, subflow, skb, mp_opt))
827d22f4988SChristoph Paasch 		return;
828648ef4b8SMat Martineau 
8291b1c7a0eSPeter Krystad 	if (mp_opt->add_addr && add_addr_hmac_valid(msk, mp_opt)) {
8301b1c7a0eSPeter Krystad 		struct mptcp_addr_info addr;
8311b1c7a0eSPeter Krystad 
8321b1c7a0eSPeter Krystad 		addr.port = htons(mp_opt->port);
8331b1c7a0eSPeter Krystad 		addr.id = mp_opt->addr_id;
8341b1c7a0eSPeter Krystad 		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
8351b1c7a0eSPeter Krystad 			addr.family = AF_INET;
8361b1c7a0eSPeter Krystad 			addr.addr = mp_opt->addr;
8371b1c7a0eSPeter Krystad 		}
8381b1c7a0eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
8391b1c7a0eSPeter Krystad 		else if (mp_opt->family == MPTCP_ADDR_IPVERSION_6) {
8401b1c7a0eSPeter Krystad 			addr.family = AF_INET6;
8411b1c7a0eSPeter Krystad 			addr.addr6 = mp_opt->addr6;
8421b1c7a0eSPeter Krystad 		}
8431b1c7a0eSPeter Krystad #endif
8441b1c7a0eSPeter Krystad 		if (!mp_opt->echo)
8451b1c7a0eSPeter Krystad 			mptcp_pm_add_addr_received(msk, &addr);
8461b1c7a0eSPeter Krystad 		mp_opt->add_addr = 0;
8471b1c7a0eSPeter Krystad 	}
8481b1c7a0eSPeter Krystad 
849648ef4b8SMat Martineau 	if (!mp_opt->dss)
850648ef4b8SMat Martineau 		return;
851648ef4b8SMat Martineau 
852cc9d2566SPaolo Abeni 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
853cc9d2566SPaolo Abeni 	 * monodirectional flows will stuck
854cc9d2566SPaolo Abeni 	 */
855cc9d2566SPaolo Abeni 	if (mp_opt->use_ack)
856cc9d2566SPaolo Abeni 		update_una(msk, mp_opt);
857cc9d2566SPaolo Abeni 
858648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
859648ef4b8SMat Martineau 	if (!mpext)
860648ef4b8SMat Martineau 		return;
861648ef4b8SMat Martineau 
862648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
863648ef4b8SMat Martineau 
864648ef4b8SMat Martineau 	if (mp_opt->use_map) {
865cc7972eaSChristoph Paasch 		if (mp_opt->mpc_map) {
866cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
867cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
868cc7972eaSChristoph Paasch 			 */
869cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
870cc7972eaSChristoph Paasch 					     &mpext->data_seq);
871cc7972eaSChristoph Paasch 			mpext->data_seq++;
872cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
873cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
874cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
875cc7972eaSChristoph Paasch 		} else {
876648ef4b8SMat Martineau 			mpext->data_seq = mp_opt->data_seq;
877648ef4b8SMat Martineau 			mpext->subflow_seq = mp_opt->subflow_seq;
878cc7972eaSChristoph Paasch 			mpext->dsn64 = mp_opt->dsn64;
879cc7972eaSChristoph Paasch 		}
880648ef4b8SMat Martineau 		mpext->data_len = mp_opt->data_len;
881648ef4b8SMat Martineau 		mpext->use_map = 1;
882648ef4b8SMat Martineau 	}
883648ef4b8SMat Martineau 
884648ef4b8SMat Martineau 	mpext->data_fin = mp_opt->data_fin;
885648ef4b8SMat Martineau }
886648ef4b8SMat Martineau 
887eda7acddSPeter Krystad void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts)
888eda7acddSPeter Krystad {
889cc7972eaSChristoph Paasch 	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
890eda7acddSPeter Krystad 	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
891eda7acddSPeter Krystad 		u8 len;
892eda7acddSPeter Krystad 
893eda7acddSPeter Krystad 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
894eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
895cec37a6eSPeter Krystad 		else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
896cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
897cc7972eaSChristoph Paasch 		else if (opts->ext_copy.data_len)
898cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
899eda7acddSPeter Krystad 		else
900eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
901eda7acddSPeter Krystad 
9023df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
9033df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
90465492c5aSPaolo Abeni 				      MPTCP_CAP_HMAC_SHA256);
905cc7972eaSChristoph Paasch 
906cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
907cc7972eaSChristoph Paasch 		    opts->suboptions))
908cc7972eaSChristoph Paasch 			goto mp_capable_done;
909cc7972eaSChristoph Paasch 
910eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
911eda7acddSPeter Krystad 		ptr += 2;
912cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
913cc7972eaSChristoph Paasch 			goto mp_capable_done;
914cc7972eaSChristoph Paasch 
915eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
916eda7acddSPeter Krystad 		ptr += 2;
917cc7972eaSChristoph Paasch 		if (!opts->ext_copy.data_len)
918cc7972eaSChristoph Paasch 			goto mp_capable_done;
919cc7972eaSChristoph Paasch 
920cc7972eaSChristoph Paasch 		put_unaligned_be32(opts->ext_copy.data_len << 16 |
921cc7972eaSChristoph Paasch 				   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
922cc7972eaSChristoph Paasch 		ptr += 1;
923eda7acddSPeter Krystad 	}
9246d0060f6SMat Martineau 
925cc7972eaSChristoph Paasch mp_capable_done:
9263df523abSPeter Krystad 	if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
9273df523abSPeter Krystad 		if (opts->ahmac)
9283df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
9293df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR, 0,
9303df523abSPeter Krystad 					      opts->addr_id);
9313df523abSPeter Krystad 		else
9323df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
9333df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR_BASE,
9343df523abSPeter Krystad 					      MPTCP_ADDR_ECHO,
9353df523abSPeter Krystad 					      opts->addr_id);
9363df523abSPeter Krystad 		memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4);
9373df523abSPeter Krystad 		ptr += 1;
9383df523abSPeter Krystad 		if (opts->ahmac) {
9393df523abSPeter Krystad 			put_unaligned_be64(opts->ahmac, ptr);
9403df523abSPeter Krystad 			ptr += 2;
9413df523abSPeter Krystad 		}
9423df523abSPeter Krystad 	}
9433df523abSPeter Krystad 
9443df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
9453df523abSPeter Krystad 	if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) {
9463df523abSPeter Krystad 		if (opts->ahmac)
9473df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
9483df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR6, 0,
9493df523abSPeter Krystad 					      opts->addr_id);
9503df523abSPeter Krystad 		else
9513df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
9523df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR6_BASE,
9533df523abSPeter Krystad 					      MPTCP_ADDR_ECHO,
9543df523abSPeter Krystad 					      opts->addr_id);
9553df523abSPeter Krystad 		memcpy((u8 *)ptr, opts->addr6.s6_addr, 16);
9563df523abSPeter Krystad 		ptr += 4;
9573df523abSPeter Krystad 		if (opts->ahmac) {
9583df523abSPeter Krystad 			put_unaligned_be64(opts->ahmac, ptr);
9593df523abSPeter Krystad 			ptr += 2;
9603df523abSPeter Krystad 		}
9613df523abSPeter Krystad 	}
9623df523abSPeter Krystad #endif
9633df523abSPeter Krystad 
9643df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
9653df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
9663df523abSPeter Krystad 				      TCPOLEN_MPTCP_RM_ADDR_BASE,
9673df523abSPeter Krystad 				      0, opts->rm_id);
9683df523abSPeter Krystad 	}
9693df523abSPeter Krystad 
970ec3edaa7SPeter Krystad 	if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
971ec3edaa7SPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
972ec3edaa7SPeter Krystad 				      TCPOLEN_MPTCP_MPJ_SYN,
973ec3edaa7SPeter Krystad 				      opts->backup, opts->join_id);
974ec3edaa7SPeter Krystad 		put_unaligned_be32(opts->token, ptr);
975ec3edaa7SPeter Krystad 		ptr += 1;
976ec3edaa7SPeter Krystad 		put_unaligned_be32(opts->nonce, ptr);
977ec3edaa7SPeter Krystad 		ptr += 1;
978ec3edaa7SPeter Krystad 	}
979ec3edaa7SPeter Krystad 
980f296234cSPeter Krystad 	if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
981f296234cSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
982f296234cSPeter Krystad 				      TCPOLEN_MPTCP_MPJ_SYNACK,
983f296234cSPeter Krystad 				      opts->backup, opts->join_id);
984f296234cSPeter Krystad 		put_unaligned_be64(opts->thmac, ptr);
985f296234cSPeter Krystad 		ptr += 2;
986f296234cSPeter Krystad 		put_unaligned_be32(opts->nonce, ptr);
987f296234cSPeter Krystad 		ptr += 1;
988f296234cSPeter Krystad 	}
989f296234cSPeter Krystad 
990ec3edaa7SPeter Krystad 	if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
991ec3edaa7SPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
992ec3edaa7SPeter Krystad 				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
993ec3edaa7SPeter Krystad 		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
994ec3edaa7SPeter Krystad 		ptr += 5;
995ec3edaa7SPeter Krystad 	}
996ec3edaa7SPeter Krystad 
9976d0060f6SMat Martineau 	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
9986d0060f6SMat Martineau 		struct mptcp_ext *mpext = &opts->ext_copy;
9996d0060f6SMat Martineau 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
10006d0060f6SMat Martineau 		u8 flags = 0;
10016d0060f6SMat Martineau 
10026d0060f6SMat Martineau 		if (mpext->use_ack) {
10036d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_ACK64;
10046d0060f6SMat Martineau 			flags = MPTCP_DSS_HAS_ACK | MPTCP_DSS_ACK64;
10056d0060f6SMat Martineau 		}
10066d0060f6SMat Martineau 
10076d0060f6SMat Martineau 		if (mpext->use_map) {
10086d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_MAP64;
10096d0060f6SMat Martineau 
10106d0060f6SMat Martineau 			/* Use only 64-bit mapping flags for now, add
10116d0060f6SMat Martineau 			 * support for optional 32-bit mappings later.
10126d0060f6SMat Martineau 			 */
10136d0060f6SMat Martineau 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
10146d0060f6SMat Martineau 			if (mpext->data_fin)
10156d0060f6SMat Martineau 				flags |= MPTCP_DSS_DATA_FIN;
10166d0060f6SMat Martineau 		}
10176d0060f6SMat Martineau 
10183df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
10196d0060f6SMat Martineau 
10206d0060f6SMat Martineau 		if (mpext->use_ack) {
10216d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_ack, ptr);
10226d0060f6SMat Martineau 			ptr += 2;
10236d0060f6SMat Martineau 		}
10246d0060f6SMat Martineau 
10256d0060f6SMat Martineau 		if (mpext->use_map) {
10266d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_seq, ptr);
10276d0060f6SMat Martineau 			ptr += 2;
10286d0060f6SMat Martineau 			put_unaligned_be32(mpext->subflow_seq, ptr);
10296d0060f6SMat Martineau 			ptr += 1;
10306d0060f6SMat Martineau 			put_unaligned_be32(mpext->data_len << 16 |
10316d0060f6SMat Martineau 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
10326d0060f6SMat Martineau 		}
10336d0060f6SMat Martineau 	}
1034eda7acddSPeter Krystad }
1035