xref: /openbmc/linux/net/mptcp/options.c (revision eaa2ffabfc35580da3fb1d31897fb696c514ea7a)
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 
7c85adcedSGeliang Tang #define pr_fmt(fmt) "MPTCP: " fmt
8c85adcedSGeliang Tang 
9eda7acddSPeter Krystad #include <linux/kernel.h>
10bd697222STodd Malsbary #include <crypto/sha.h>
11eda7acddSPeter Krystad #include <net/tcp.h>
12eda7acddSPeter Krystad #include <net/mptcp.h>
13eda7acddSPeter Krystad #include "protocol.h"
14a877de06SGeliang Tang #include "mib.h"
15eda7acddSPeter Krystad 
1665492c5aSPaolo Abeni static bool mptcp_cap_flag_sha256(u8 flags)
1765492c5aSPaolo Abeni {
1865492c5aSPaolo Abeni 	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
1965492c5aSPaolo Abeni }
2065492c5aSPaolo Abeni 
21cfde141eSPaolo Abeni static void mptcp_parse_option(const struct sk_buff *skb,
22cfde141eSPaolo Abeni 			       const unsigned char *ptr, int opsize,
23cfde141eSPaolo Abeni 			       struct mptcp_options_received *mp_opt)
24eda7acddSPeter Krystad {
25eda7acddSPeter Krystad 	u8 subtype = *ptr >> 4;
26648ef4b8SMat Martineau 	int expected_opsize;
27eda7acddSPeter Krystad 	u8 version;
28eda7acddSPeter Krystad 	u8 flags;
29eda7acddSPeter Krystad 
30eda7acddSPeter Krystad 	switch (subtype) {
31eda7acddSPeter Krystad 	case MPTCPOPT_MP_CAPABLE:
32cc7972eaSChristoph Paasch 		/* strict size checking */
33cc7972eaSChristoph Paasch 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
34cc7972eaSChristoph Paasch 			if (skb->len > tcp_hdr(skb)->doff << 2)
35cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
36cc7972eaSChristoph Paasch 			else
37cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
38cc7972eaSChristoph Paasch 		} else {
39cc7972eaSChristoph Paasch 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
40cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
41cc7972eaSChristoph Paasch 			else
42cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
43cc7972eaSChristoph Paasch 		}
44cc7972eaSChristoph Paasch 		if (opsize != expected_opsize)
45eda7acddSPeter Krystad 			break;
46eda7acddSPeter Krystad 
47cc7972eaSChristoph Paasch 		/* try to be gentle vs future versions on the initial syn */
48eda7acddSPeter Krystad 		version = *ptr++ & MPTCP_VERSION_MASK;
49cc7972eaSChristoph Paasch 		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
50eda7acddSPeter Krystad 			if (version != MPTCP_SUPPORTED_VERSION)
51eda7acddSPeter Krystad 				break;
52cc7972eaSChristoph Paasch 		} else if (version < MPTCP_SUPPORTED_VERSION) {
53cc7972eaSChristoph Paasch 			break;
54cc7972eaSChristoph Paasch 		}
55eda7acddSPeter Krystad 
56eda7acddSPeter Krystad 		flags = *ptr++;
5765492c5aSPaolo Abeni 		if (!mptcp_cap_flag_sha256(flags) ||
58eda7acddSPeter Krystad 		    (flags & MPTCP_CAP_EXTENSIBILITY))
59eda7acddSPeter Krystad 			break;
60eda7acddSPeter Krystad 
61eda7acddSPeter Krystad 		/* RFC 6824, Section 3.1:
62eda7acddSPeter Krystad 		 * "For the Checksum Required bit (labeled "A"), if either
63eda7acddSPeter Krystad 		 * host requires the use of checksums, checksums MUST be used.
64eda7acddSPeter Krystad 		 * In other words, the only way for checksums not to be used
65eda7acddSPeter Krystad 		 * is if both hosts in their SYNs set A=0."
66eda7acddSPeter Krystad 		 *
67eda7acddSPeter Krystad 		 * Section 3.3.0:
68eda7acddSPeter Krystad 		 * "If a checksum is not present when its use has been
69eda7acddSPeter Krystad 		 * negotiated, the receiver MUST close the subflow with a RST as
70eda7acddSPeter Krystad 		 * it is considered broken."
71eda7acddSPeter Krystad 		 *
72eda7acddSPeter Krystad 		 * We don't implement DSS checksum - fall back to TCP.
73eda7acddSPeter Krystad 		 */
74eda7acddSPeter Krystad 		if (flags & MPTCP_CAP_CHECKSUM_REQD)
75eda7acddSPeter Krystad 			break;
76eda7acddSPeter Krystad 
77eda7acddSPeter Krystad 		mp_opt->mp_capable = 1;
78cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
79eda7acddSPeter Krystad 			mp_opt->sndr_key = get_unaligned_be64(ptr);
80eda7acddSPeter Krystad 			ptr += 8;
81cc7972eaSChristoph Paasch 		}
82cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
83eda7acddSPeter Krystad 			mp_opt->rcvr_key = get_unaligned_be64(ptr);
84eda7acddSPeter Krystad 			ptr += 8;
85eda7acddSPeter Krystad 		}
86cc7972eaSChristoph Paasch 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
87cc7972eaSChristoph Paasch 			/* Section 3.1.:
88cc7972eaSChristoph Paasch 			 * "the data parameters in a MP_CAPABLE are semantically
89cc7972eaSChristoph Paasch 			 * equivalent to those in a DSS option and can be used
90cc7972eaSChristoph Paasch 			 * interchangeably."
91cc7972eaSChristoph Paasch 			 */
92cc7972eaSChristoph Paasch 			mp_opt->dss = 1;
93cc7972eaSChristoph Paasch 			mp_opt->use_map = 1;
94cc7972eaSChristoph Paasch 			mp_opt->mpc_map = 1;
95cc7972eaSChristoph Paasch 			mp_opt->data_len = get_unaligned_be16(ptr);
96cc7972eaSChristoph Paasch 			ptr += 2;
97cc7972eaSChristoph Paasch 		}
98cc7972eaSChristoph Paasch 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
99cc7972eaSChristoph Paasch 			 version, flags, opsize, mp_opt->sndr_key,
100cc7972eaSChristoph Paasch 			 mp_opt->rcvr_key, mp_opt->data_len);
101eda7acddSPeter Krystad 		break;
102eda7acddSPeter Krystad 
103f296234cSPeter Krystad 	case MPTCPOPT_MP_JOIN:
104f296234cSPeter Krystad 		mp_opt->mp_join = 1;
105f296234cSPeter Krystad 		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
106f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
107f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
108f296234cSPeter Krystad 			mp_opt->token = get_unaligned_be32(ptr);
109f296234cSPeter Krystad 			ptr += 4;
110f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
111f296234cSPeter Krystad 			ptr += 4;
112f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
113f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
114f296234cSPeter Krystad 				 mp_opt->token, mp_opt->nonce);
115f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
116f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
117f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
118f296234cSPeter Krystad 			mp_opt->thmac = get_unaligned_be64(ptr);
119f296234cSPeter Krystad 			ptr += 8;
120f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
121f296234cSPeter Krystad 			ptr += 4;
122f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
123f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
124f296234cSPeter Krystad 				 mp_opt->thmac, mp_opt->nonce);
125f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
126f296234cSPeter Krystad 			ptr += 2;
127f296234cSPeter Krystad 			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
128f296234cSPeter Krystad 			pr_debug("MP_JOIN hmac");
129f296234cSPeter Krystad 		} else {
130f296234cSPeter Krystad 			pr_warn("MP_JOIN bad option size");
131f296234cSPeter Krystad 			mp_opt->mp_join = 0;
132f296234cSPeter Krystad 		}
133f296234cSPeter Krystad 		break;
134f296234cSPeter Krystad 
135eda7acddSPeter Krystad 	case MPTCPOPT_DSS:
136eda7acddSPeter Krystad 		pr_debug("DSS");
137648ef4b8SMat Martineau 		ptr++;
138648ef4b8SMat Martineau 
139cc7972eaSChristoph Paasch 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
140cc7972eaSChristoph Paasch 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
141cc7972eaSChristoph Paasch 		 * map info accordingly
142cc7972eaSChristoph Paasch 		 */
143cc7972eaSChristoph Paasch 		mp_opt->mpc_map = 0;
144648ef4b8SMat Martineau 		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
145648ef4b8SMat Martineau 		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
146648ef4b8SMat Martineau 		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
147648ef4b8SMat Martineau 		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
148648ef4b8SMat Martineau 		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
149648ef4b8SMat Martineau 		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
150648ef4b8SMat Martineau 
151648ef4b8SMat Martineau 		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
152648ef4b8SMat Martineau 			 mp_opt->data_fin, mp_opt->dsn64,
153648ef4b8SMat Martineau 			 mp_opt->use_map, mp_opt->ack64,
154648ef4b8SMat Martineau 			 mp_opt->use_ack);
155648ef4b8SMat Martineau 
156648ef4b8SMat Martineau 		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
157648ef4b8SMat Martineau 
158648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
159648ef4b8SMat Martineau 			if (mp_opt->ack64)
160648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
161648ef4b8SMat Martineau 			else
162648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
163648ef4b8SMat Martineau 		}
164648ef4b8SMat Martineau 
165648ef4b8SMat Martineau 		if (mp_opt->use_map) {
166648ef4b8SMat Martineau 			if (mp_opt->dsn64)
167648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
168648ef4b8SMat Martineau 			else
169648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
170648ef4b8SMat Martineau 		}
171648ef4b8SMat Martineau 
172648ef4b8SMat Martineau 		/* RFC 6824, Section 3.3:
173648ef4b8SMat Martineau 		 * If a checksum is present, but its use had
174648ef4b8SMat Martineau 		 * not been negotiated in the MP_CAPABLE handshake,
175648ef4b8SMat Martineau 		 * the checksum field MUST be ignored.
176648ef4b8SMat Martineau 		 */
177648ef4b8SMat Martineau 		if (opsize != expected_opsize &&
178648ef4b8SMat Martineau 		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
179648ef4b8SMat Martineau 			break;
180648ef4b8SMat Martineau 
181eda7acddSPeter Krystad 		mp_opt->dss = 1;
182648ef4b8SMat Martineau 
183648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
184648ef4b8SMat Martineau 			if (mp_opt->ack64) {
185648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be64(ptr);
186648ef4b8SMat Martineau 				ptr += 8;
187648ef4b8SMat Martineau 			} else {
188648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be32(ptr);
189648ef4b8SMat Martineau 				ptr += 4;
190648ef4b8SMat Martineau 			}
191648ef4b8SMat Martineau 
192648ef4b8SMat Martineau 			pr_debug("data_ack=%llu", mp_opt->data_ack);
193648ef4b8SMat Martineau 		}
194648ef4b8SMat Martineau 
195648ef4b8SMat Martineau 		if (mp_opt->use_map) {
196648ef4b8SMat Martineau 			if (mp_opt->dsn64) {
197648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be64(ptr);
198648ef4b8SMat Martineau 				ptr += 8;
199648ef4b8SMat Martineau 			} else {
200648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be32(ptr);
201648ef4b8SMat Martineau 				ptr += 4;
202648ef4b8SMat Martineau 			}
203648ef4b8SMat Martineau 
204648ef4b8SMat Martineau 			mp_opt->subflow_seq = get_unaligned_be32(ptr);
205648ef4b8SMat Martineau 			ptr += 4;
206648ef4b8SMat Martineau 
207648ef4b8SMat Martineau 			mp_opt->data_len = get_unaligned_be16(ptr);
208648ef4b8SMat Martineau 			ptr += 2;
209648ef4b8SMat Martineau 
210648ef4b8SMat Martineau 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
211648ef4b8SMat Martineau 				 mp_opt->data_seq, mp_opt->subflow_seq,
212648ef4b8SMat Martineau 				 mp_opt->data_len);
213648ef4b8SMat Martineau 		}
214648ef4b8SMat Martineau 
215eda7acddSPeter Krystad 		break;
216eda7acddSPeter Krystad 
2173df523abSPeter Krystad 	case MPTCPOPT_ADD_ADDR:
2183df523abSPeter Krystad 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
2193df523abSPeter Krystad 		if (!mp_opt->echo) {
2203df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
2213df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
2223df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
2233df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2243df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
2253df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
2263df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
2273df523abSPeter Krystad #endif
2283df523abSPeter Krystad 			else
2293df523abSPeter Krystad 				break;
2303df523abSPeter Krystad 		} else {
2313df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
2323df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
2333df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
2343df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2353df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
2363df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
2373df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
2383df523abSPeter Krystad #endif
2393df523abSPeter Krystad 			else
2403df523abSPeter Krystad 				break;
2413df523abSPeter Krystad 		}
2423df523abSPeter Krystad 
2433df523abSPeter Krystad 		mp_opt->add_addr = 1;
2443df523abSPeter Krystad 		mp_opt->addr_id = *ptr++;
2456a6c05a8SGeliang Tang 		pr_debug("ADD_ADDR: id=%d, echo=%d", mp_opt->addr_id, mp_opt->echo);
2463df523abSPeter Krystad 		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
2473df523abSPeter Krystad 			memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4);
2483df523abSPeter Krystad 			ptr += 4;
2493df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
2503df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
2513df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
2523df523abSPeter Krystad 				ptr += 2;
2533df523abSPeter Krystad 			}
2543df523abSPeter Krystad 		}
2553df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2563df523abSPeter Krystad 		else {
2573df523abSPeter Krystad 			memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16);
2583df523abSPeter Krystad 			ptr += 16;
2593df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
2603df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
2613df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
2623df523abSPeter Krystad 				ptr += 2;
2633df523abSPeter Krystad 			}
2643df523abSPeter Krystad 		}
2653df523abSPeter Krystad #endif
2663df523abSPeter Krystad 		if (!mp_opt->echo) {
2673df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
2683df523abSPeter Krystad 			ptr += 8;
2693df523abSPeter Krystad 		}
2703df523abSPeter Krystad 		break;
2713df523abSPeter Krystad 
2723df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
2733df523abSPeter Krystad 		if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE)
2743df523abSPeter Krystad 			break;
2753df523abSPeter Krystad 
2768e60eed6SGeliang Tang 		ptr++;
2778e60eed6SGeliang Tang 
2783df523abSPeter Krystad 		mp_opt->rm_addr = 1;
2793df523abSPeter Krystad 		mp_opt->rm_id = *ptr++;
2803df523abSPeter Krystad 		pr_debug("RM_ADDR: id=%d", mp_opt->rm_id);
2813df523abSPeter Krystad 		break;
2823df523abSPeter Krystad 
283eda7acddSPeter Krystad 	default:
284eda7acddSPeter Krystad 		break;
285eda7acddSPeter Krystad 	}
286eda7acddSPeter Krystad }
287eda7acddSPeter Krystad 
288cec37a6eSPeter Krystad void mptcp_get_options(const struct sk_buff *skb,
289cfde141eSPaolo Abeni 		       struct mptcp_options_received *mp_opt)
290cec37a6eSPeter Krystad {
291cec37a6eSPeter Krystad 	const struct tcphdr *th = tcp_hdr(skb);
292cfde141eSPaolo Abeni 	const unsigned char *ptr;
293cfde141eSPaolo Abeni 	int length;
294cec37a6eSPeter Krystad 
295cfde141eSPaolo Abeni 	/* initialize option status */
296cfde141eSPaolo Abeni 	mp_opt->mp_capable = 0;
297cfde141eSPaolo Abeni 	mp_opt->mp_join = 0;
298cfde141eSPaolo Abeni 	mp_opt->add_addr = 0;
299fe2d9b1aSGeliang Tang 	mp_opt->ahmac = 0;
30065b8c8a6SGeliang Tang 	mp_opt->port = 0;
301cfde141eSPaolo Abeni 	mp_opt->rm_addr = 0;
302cfde141eSPaolo Abeni 	mp_opt->dss = 0;
303cfde141eSPaolo Abeni 
304cfde141eSPaolo Abeni 	length = (th->doff * 4) - sizeof(struct tcphdr);
305cec37a6eSPeter Krystad 	ptr = (const unsigned char *)(th + 1);
306cec37a6eSPeter Krystad 
307cec37a6eSPeter Krystad 	while (length > 0) {
308cec37a6eSPeter Krystad 		int opcode = *ptr++;
309cec37a6eSPeter Krystad 		int opsize;
310cec37a6eSPeter Krystad 
311cec37a6eSPeter Krystad 		switch (opcode) {
312cec37a6eSPeter Krystad 		case TCPOPT_EOL:
313cec37a6eSPeter Krystad 			return;
314cec37a6eSPeter Krystad 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
315cec37a6eSPeter Krystad 			length--;
316cec37a6eSPeter Krystad 			continue;
317cec37a6eSPeter Krystad 		default:
318cec37a6eSPeter Krystad 			opsize = *ptr++;
319cec37a6eSPeter Krystad 			if (opsize < 2) /* "silly options" */
320cec37a6eSPeter Krystad 				return;
321cec37a6eSPeter Krystad 			if (opsize > length)
322cec37a6eSPeter Krystad 				return;	/* don't parse partial options */
323cec37a6eSPeter Krystad 			if (opcode == TCPOPT_MPTCP)
324cfde141eSPaolo Abeni 				mptcp_parse_option(skb, ptr, opsize, mp_opt);
325cec37a6eSPeter Krystad 			ptr += opsize - 2;
326cec37a6eSPeter Krystad 			length -= opsize;
327cec37a6eSPeter Krystad 		}
328cec37a6eSPeter Krystad 	}
329cec37a6eSPeter Krystad }
330cec37a6eSPeter Krystad 
331cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
332cc7972eaSChristoph Paasch 		       unsigned int *size, struct mptcp_out_options *opts)
333cec37a6eSPeter Krystad {
334cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
335cec37a6eSPeter Krystad 
336cc7972eaSChristoph Paasch 	/* we will use snd_isn to detect first pkt [re]transmission
337cc7972eaSChristoph Paasch 	 * in mptcp_established_options_mp()
338cc7972eaSChristoph Paasch 	 */
339cc7972eaSChristoph Paasch 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
340cec37a6eSPeter Krystad 	if (subflow->request_mptcp) {
341cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
342cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYN;
343cec37a6eSPeter Krystad 		return true;
344ec3edaa7SPeter Krystad 	} else if (subflow->request_join) {
345ec3edaa7SPeter Krystad 		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
346ec3edaa7SPeter Krystad 			 subflow->local_nonce);
347ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
348ec3edaa7SPeter Krystad 		opts->join_id = subflow->local_id;
349ec3edaa7SPeter Krystad 		opts->token = subflow->remote_token;
350ec3edaa7SPeter Krystad 		opts->nonce = subflow->local_nonce;
351ec3edaa7SPeter Krystad 		opts->backup = subflow->request_bkup;
352ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYN;
353ec3edaa7SPeter Krystad 		return true;
354cec37a6eSPeter Krystad 	}
355cec37a6eSPeter Krystad 	return false;
356cec37a6eSPeter Krystad }
357cec37a6eSPeter Krystad 
358ec3edaa7SPeter Krystad /* MP_JOIN client subflow must wait for 4th ack before sending any data:
359ec3edaa7SPeter Krystad  * TCP can't schedule delack timer before the subflow is fully established.
360ec3edaa7SPeter Krystad  * MPTCP uses the delack timer to do 3rd ack retransmissions
361ec3edaa7SPeter Krystad  */
362ec3edaa7SPeter Krystad static void schedule_3rdack_retransmission(struct sock *sk)
363ec3edaa7SPeter Krystad {
364ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
365ec3edaa7SPeter Krystad 	struct tcp_sock *tp = tcp_sk(sk);
366ec3edaa7SPeter Krystad 	unsigned long timeout;
367ec3edaa7SPeter Krystad 
368ec3edaa7SPeter Krystad 	/* reschedule with a timeout above RTT, as we must look only for drop */
369ec3edaa7SPeter Krystad 	if (tp->srtt_us)
370ec3edaa7SPeter Krystad 		timeout = tp->srtt_us << 1;
371ec3edaa7SPeter Krystad 	else
372ec3edaa7SPeter Krystad 		timeout = TCP_TIMEOUT_INIT;
373ec3edaa7SPeter Krystad 
374ec3edaa7SPeter Krystad 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
375ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
376ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = timeout;
377ec3edaa7SPeter Krystad 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
378ec3edaa7SPeter Krystad }
379ec3edaa7SPeter Krystad 
380ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk)
381ec3edaa7SPeter Krystad {
382ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
383ec3edaa7SPeter Krystad 
384ec3edaa7SPeter Krystad 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
385ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = 0;
386ec3edaa7SPeter Krystad 	icsk->icsk_ack.ato = 0;
387ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
388ec3edaa7SPeter Krystad }
389ec3edaa7SPeter Krystad 
390cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
391cc7972eaSChristoph Paasch 					 unsigned int *size,
3926d0060f6SMat Martineau 					 unsigned int remaining,
393cec37a6eSPeter Krystad 					 struct mptcp_out_options *opts)
394cec37a6eSPeter Krystad {
395cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
396cc7972eaSChristoph Paasch 	struct mptcp_ext *mpext;
397cc7972eaSChristoph Paasch 	unsigned int data_len;
398cec37a6eSPeter Krystad 
399ec3edaa7SPeter Krystad 	/* When skb is not available, we better over-estimate the emitted
400ec3edaa7SPeter Krystad 	 * options len. A full DSS option (28 bytes) is longer than
401ec3edaa7SPeter Krystad 	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
402ec3edaa7SPeter Krystad 	 * tell the caller to defer the estimate to
403ec3edaa7SPeter Krystad 	 * mptcp_established_options_dss(), which will reserve enough space.
404cc7972eaSChristoph Paasch 	 */
405ec3edaa7SPeter Krystad 	if (!skb)
406ec3edaa7SPeter Krystad 		return false;
407ec3edaa7SPeter Krystad 
408ec3edaa7SPeter Krystad 	/* MPC/MPJ needed only on 3rd ack packet */
409ec3edaa7SPeter Krystad 	if (subflow->fully_established ||
410ec3edaa7SPeter Krystad 	    subflow->snd_isn != TCP_SKB_CB(skb)->seq)
411ec3edaa7SPeter Krystad 		return false;
412ec3edaa7SPeter Krystad 
413ec3edaa7SPeter Krystad 	if (subflow->mp_capable) {
414cc7972eaSChristoph Paasch 		mpext = mptcp_get_ext(skb);
415cc7972eaSChristoph Paasch 		data_len = mpext ? mpext->data_len : 0;
416cc7972eaSChristoph Paasch 
417cc7972eaSChristoph Paasch 		/* we will check ext_copy.data_len in mptcp_write_options() to
418cc7972eaSChristoph Paasch 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
419cc7972eaSChristoph Paasch 		 * TCPOLEN_MPTCP_MPC_ACK
420cc7972eaSChristoph Paasch 		 */
421cc7972eaSChristoph Paasch 		opts->ext_copy.data_len = data_len;
422cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
423cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
424cec37a6eSPeter Krystad 		opts->rcvr_key = subflow->remote_key;
425cc7972eaSChristoph Paasch 
426cc7972eaSChristoph Paasch 		/* Section 3.1.
427cc7972eaSChristoph Paasch 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
428cc7972eaSChristoph Paasch 		 * packets that start the first subflow of an MPTCP connection,
429cc7972eaSChristoph Paasch 		 * as well as the first packet that carries data
430cc7972eaSChristoph Paasch 		 */
431cc7972eaSChristoph Paasch 		if (data_len > 0)
432cc7972eaSChristoph Paasch 			*size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
433cc7972eaSChristoph Paasch 		else
434cec37a6eSPeter Krystad 			*size = TCPOLEN_MPTCP_MPC_ACK;
435cc7972eaSChristoph Paasch 
436cc7972eaSChristoph Paasch 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
437cc7972eaSChristoph Paasch 			 subflow, subflow->local_key, subflow->remote_key,
438cc7972eaSChristoph Paasch 			 data_len);
439cc7972eaSChristoph Paasch 
440cec37a6eSPeter Krystad 		return true;
441ec3edaa7SPeter Krystad 	} else if (subflow->mp_join) {
442ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
443ec3edaa7SPeter Krystad 		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
444ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_ACK;
445ec3edaa7SPeter Krystad 		pr_debug("subflow=%p", subflow);
446ec3edaa7SPeter Krystad 
447ec3edaa7SPeter Krystad 		schedule_3rdack_retransmission(sk);
448ec3edaa7SPeter Krystad 		return true;
449cec37a6eSPeter Krystad 	}
450cec37a6eSPeter Krystad 	return false;
451cec37a6eSPeter Krystad }
452cec37a6eSPeter Krystad 
4536d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
4549c29e361SPaolo Abeni 				 struct sk_buff *skb, struct mptcp_ext *ext)
4556d0060f6SMat Martineau {
456017512a0SPaolo Abeni 	/* The write_seq value has already been incremented, so the actual
457017512a0SPaolo Abeni 	 * sequence number for the DATA_FIN is one less.
458017512a0SPaolo Abeni 	 */
459017512a0SPaolo Abeni 	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
4607279da61SMat Martineau 
4619c29e361SPaolo Abeni 	if (!ext->use_map || !skb->len) {
4626d0060f6SMat Martineau 		/* RFC6824 requires a DSS mapping with specific values
4636d0060f6SMat Martineau 		 * if DATA_FIN is set but no data payload is mapped
4646d0060f6SMat Martineau 		 */
4656d37a0b8SMat Martineau 		ext->data_fin = 1;
4666d0060f6SMat Martineau 		ext->use_map = 1;
4676d0060f6SMat Martineau 		ext->dsn64 = 1;
468017512a0SPaolo Abeni 		ext->data_seq = data_fin_tx_seq;
4696d0060f6SMat Martineau 		ext->subflow_seq = 0;
4706d0060f6SMat Martineau 		ext->data_len = 1;
4717279da61SMat Martineau 	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
4726d37a0b8SMat Martineau 		/* If there's an existing DSS mapping and it is the
4736d37a0b8SMat Martineau 		 * final mapping, DATA_FIN consumes 1 additional byte of
4746d37a0b8SMat Martineau 		 * mapping space.
4756d0060f6SMat Martineau 		 */
4766d37a0b8SMat Martineau 		ext->data_fin = 1;
4776d0060f6SMat Martineau 		ext->data_len++;
4786d0060f6SMat Martineau 	}
4796d0060f6SMat Martineau }
4806d0060f6SMat Martineau 
4816d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
4826d0060f6SMat Martineau 					  unsigned int *size,
4836d0060f6SMat Martineau 					  unsigned int remaining,
4846d0060f6SMat Martineau 					  struct mptcp_out_options *opts)
4856d0060f6SMat Martineau {
4866d0060f6SMat Martineau 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
4877279da61SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
4886d0060f6SMat Martineau 	unsigned int dss_size = 0;
4897279da61SMat Martineau 	u64 snd_data_fin_enable;
4906d0060f6SMat Martineau 	struct mptcp_ext *mpext;
4916d0060f6SMat Martineau 	unsigned int ack_size;
492d22f4988SChristoph Paasch 	bool ret = false;
4936d0060f6SMat Martineau 
4940bac966aSMat Martineau 	mpext = skb ? mptcp_get_ext(skb) : NULL;
4957279da61SMat Martineau 	snd_data_fin_enable = READ_ONCE(msk->snd_data_fin_enable);
4966d0060f6SMat Martineau 
4977279da61SMat Martineau 	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
4986d0060f6SMat Martineau 		unsigned int map_size;
4996d0060f6SMat Martineau 
5006d0060f6SMat Martineau 		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
5016d0060f6SMat Martineau 
5026d0060f6SMat Martineau 		remaining -= map_size;
5036d0060f6SMat Martineau 		dss_size = map_size;
5046d0060f6SMat Martineau 		if (mpext)
5056d0060f6SMat Martineau 			opts->ext_copy = *mpext;
5066d0060f6SMat Martineau 
5077279da61SMat Martineau 		if (skb && snd_data_fin_enable)
5089c29e361SPaolo Abeni 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
509d22f4988SChristoph Paasch 		ret = true;
510d22f4988SChristoph Paasch 	}
511d22f4988SChristoph Paasch 
5122398e399SPaolo Abeni 	/* passive sockets msk will set the 'can_ack' after accept(), even
5132398e399SPaolo Abeni 	 * if the first subflow may have the already the remote key handy
5142398e399SPaolo Abeni 	 */
515d22f4988SChristoph Paasch 	opts->ext_copy.use_ack = 0;
516dc093db5SPaolo Abeni 	if (!READ_ONCE(msk->can_ack)) {
517d22f4988SChristoph Paasch 		*size = ALIGN(dss_size, 4);
518d22f4988SChristoph Paasch 		return ret;
5196d0060f6SMat Martineau 	}
5206d0060f6SMat Martineau 
52137198e93SDavide Caratti 	if (READ_ONCE(msk->use_64bit_ack)) {
5226d0060f6SMat Martineau 		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
523917944daSMat Martineau 		opts->ext_copy.data_ack = READ_ONCE(msk->ack_seq);
524a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 1;
525a0c1d0eaSChristoph Paasch 	} else {
526a0c1d0eaSChristoph Paasch 		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
527917944daSMat Martineau 		opts->ext_copy.data_ack32 = (uint32_t)READ_ONCE(msk->ack_seq);
528a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 0;
529a0c1d0eaSChristoph Paasch 	}
530a0c1d0eaSChristoph Paasch 	opts->ext_copy.use_ack = 1;
5316d0060f6SMat Martineau 
5326d0060f6SMat Martineau 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
5336d0060f6SMat Martineau 	if (dss_size == 0)
5346d0060f6SMat Martineau 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
5356d0060f6SMat Martineau 
5366d0060f6SMat Martineau 	dss_size += ack_size;
5376d0060f6SMat Martineau 
5386d0060f6SMat Martineau 	*size = ALIGN(dss_size, 4);
5396d0060f6SMat Martineau 	return true;
5406d0060f6SMat Martineau }
5416d0060f6SMat Martineau 
5423df523abSPeter Krystad static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
5433df523abSPeter Krystad 				  struct in_addr *addr)
5443df523abSPeter Krystad {
545bd697222STodd Malsbary 	u8 hmac[SHA256_DIGEST_SIZE];
5463df523abSPeter Krystad 	u8 msg[7];
5473df523abSPeter Krystad 
5483df523abSPeter Krystad 	msg[0] = addr_id;
5493df523abSPeter Krystad 	memcpy(&msg[1], &addr->s_addr, 4);
5503df523abSPeter Krystad 	msg[5] = 0;
5513df523abSPeter Krystad 	msg[6] = 0;
5523df523abSPeter Krystad 
5533df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
5543df523abSPeter Krystad 
555bd697222STodd Malsbary 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
5563df523abSPeter Krystad }
5573df523abSPeter Krystad 
5583df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
5593df523abSPeter Krystad static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
5603df523abSPeter Krystad 				   struct in6_addr *addr)
5613df523abSPeter Krystad {
562bd697222STodd Malsbary 	u8 hmac[SHA256_DIGEST_SIZE];
5633df523abSPeter Krystad 	u8 msg[19];
5643df523abSPeter Krystad 
5653df523abSPeter Krystad 	msg[0] = addr_id;
5663df523abSPeter Krystad 	memcpy(&msg[1], &addr->s6_addr, 16);
5673df523abSPeter Krystad 	msg[17] = 0;
5683df523abSPeter Krystad 	msg[18] = 0;
5693df523abSPeter Krystad 
5703df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
5713df523abSPeter Krystad 
572bd697222STodd Malsbary 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
5733df523abSPeter Krystad }
5743df523abSPeter Krystad #endif
5753df523abSPeter Krystad 
576f643b803SGeliang Tang static bool mptcp_established_options_add_addr(struct sock *sk,
5773df523abSPeter Krystad 					       unsigned int *size,
5783df523abSPeter Krystad 					       unsigned int remaining,
5793df523abSPeter Krystad 					       struct mptcp_out_options *opts)
5803df523abSPeter Krystad {
5813df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
5823df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
5831b1c7a0eSPeter Krystad 	struct mptcp_addr_info saddr;
5846a6c05a8SGeliang Tang 	bool echo;
5851b1c7a0eSPeter Krystad 	int len;
5863df523abSPeter Krystad 
587f643b803SGeliang Tang 	if (!mptcp_pm_should_add_signal(msk) ||
5886a6c05a8SGeliang Tang 	    !(mptcp_pm_add_addr_signal(msk, remaining, &saddr, &echo)))
5893df523abSPeter Krystad 		return false;
5901b1c7a0eSPeter Krystad 
591456afe01SMatthieu Baerts 	len = mptcp_add_addr_len(saddr.family, echo);
5921b1c7a0eSPeter Krystad 	if (remaining < len)
5931b1c7a0eSPeter Krystad 		return false;
5941b1c7a0eSPeter Krystad 
5951b1c7a0eSPeter Krystad 	*size = len;
5961b1c7a0eSPeter Krystad 	opts->addr_id = saddr.id;
5971b1c7a0eSPeter Krystad 	if (saddr.family == AF_INET) {
5983df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
5991b1c7a0eSPeter Krystad 		opts->addr = saddr.addr;
6006a6c05a8SGeliang Tang 		if (!echo) {
6013df523abSPeter Krystad 			opts->ahmac = add_addr_generate_hmac(msk->local_key,
6023df523abSPeter Krystad 							     msk->remote_key,
6033df523abSPeter Krystad 							     opts->addr_id,
6043df523abSPeter Krystad 							     &opts->addr);
6053df523abSPeter Krystad 		}
6066a6c05a8SGeliang Tang 	}
6073df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
6081b1c7a0eSPeter Krystad 	else if (saddr.family == AF_INET6) {
6093df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
6101b1c7a0eSPeter Krystad 		opts->addr6 = saddr.addr6;
6116a6c05a8SGeliang Tang 		if (!echo) {
6123df523abSPeter Krystad 			opts->ahmac = add_addr6_generate_hmac(msk->local_key,
6133df523abSPeter Krystad 							      msk->remote_key,
6143df523abSPeter Krystad 							      opts->addr_id,
6153df523abSPeter Krystad 							      &opts->addr6);
6163df523abSPeter Krystad 		}
6176a6c05a8SGeliang Tang 	}
6183df523abSPeter Krystad #endif
6196a6c05a8SGeliang Tang 	pr_debug("addr_id=%d, ahmac=%llu, echo=%d", opts->addr_id, opts->ahmac, echo);
6203df523abSPeter Krystad 
6213df523abSPeter Krystad 	return true;
6223df523abSPeter Krystad }
6233df523abSPeter Krystad 
6245cb104aeSGeliang Tang static bool mptcp_established_options_rm_addr(struct sock *sk,
6255cb104aeSGeliang Tang 					      unsigned int *size,
6265cb104aeSGeliang Tang 					      unsigned int remaining,
6275cb104aeSGeliang Tang 					      struct mptcp_out_options *opts)
6285cb104aeSGeliang Tang {
6295cb104aeSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
6305cb104aeSGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
6315cb104aeSGeliang Tang 	u8 rm_id;
6325cb104aeSGeliang Tang 
6335cb104aeSGeliang Tang 	if (!mptcp_pm_should_rm_signal(msk) ||
6345cb104aeSGeliang Tang 	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_id)))
6355cb104aeSGeliang Tang 		return false;
6365cb104aeSGeliang Tang 
6375cb104aeSGeliang Tang 	if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE)
6385cb104aeSGeliang Tang 		return false;
6395cb104aeSGeliang Tang 
6405cb104aeSGeliang Tang 	*size = TCPOLEN_MPTCP_RM_ADDR_BASE;
6415cb104aeSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
6425cb104aeSGeliang Tang 	opts->rm_id = rm_id;
6435cb104aeSGeliang Tang 
6445cb104aeSGeliang Tang 	pr_debug("rm_id=%d", opts->rm_id);
6455cb104aeSGeliang Tang 
6465cb104aeSGeliang Tang 	return true;
6475cb104aeSGeliang Tang }
6485cb104aeSGeliang Tang 
6496d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
6506d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
6516d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
6526d0060f6SMat Martineau {
6536d0060f6SMat Martineau 	unsigned int opt_size = 0;
6546d0060f6SMat Martineau 	bool ret = false;
6556d0060f6SMat Martineau 
6563df523abSPeter Krystad 	opts->suboptions = 0;
6573df523abSPeter Krystad 
658e1ff9e82SDavide Caratti 	if (unlikely(mptcp_check_fallback(sk)))
659e1ff9e82SDavide Caratti 		return false;
660e1ff9e82SDavide Caratti 
661d5824847SPaolo Abeni 	/* prevent adding of any MPTCP related options on reset packet
662d5824847SPaolo Abeni 	 * until we support MP_TCPRST/MP_FASTCLOSE
663d5824847SPaolo Abeni 	 */
664d5824847SPaolo Abeni 	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
665d5824847SPaolo Abeni 		return false;
666d5824847SPaolo Abeni 
667cc7972eaSChristoph Paasch 	if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
6686d0060f6SMat Martineau 		ret = true;
6696d0060f6SMat Martineau 	else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
6706d0060f6SMat Martineau 					       opts))
6716d0060f6SMat Martineau 		ret = true;
6726d0060f6SMat Martineau 
6736d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
6746d0060f6SMat Martineau 	 * TCP option space would be fatal
6756d0060f6SMat Martineau 	 */
6766d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
6776d0060f6SMat Martineau 		return false;
6786d0060f6SMat Martineau 
6796d0060f6SMat Martineau 	*size += opt_size;
6806d0060f6SMat Martineau 	remaining -= opt_size;
681f643b803SGeliang Tang 	if (mptcp_established_options_add_addr(sk, &opt_size, remaining, opts)) {
6823df523abSPeter Krystad 		*size += opt_size;
6833df523abSPeter Krystad 		remaining -= opt_size;
6843df523abSPeter Krystad 		ret = true;
6855cb104aeSGeliang Tang 	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
6865cb104aeSGeliang Tang 		*size += opt_size;
6875cb104aeSGeliang Tang 		remaining -= opt_size;
6885cb104aeSGeliang Tang 		ret = true;
6893df523abSPeter Krystad 	}
6906d0060f6SMat Martineau 
6916d0060f6SMat Martineau 	return ret;
6926d0060f6SMat Martineau }
6936d0060f6SMat Martineau 
694cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
695cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
696cec37a6eSPeter Krystad {
697cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
698cec37a6eSPeter Krystad 
699cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
700cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
701cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
702cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
703cec37a6eSPeter Krystad 		pr_debug("subflow_req=%p, local_key=%llu",
704cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
705cec37a6eSPeter Krystad 		return true;
706f296234cSPeter Krystad 	} else if (subflow_req->mp_join) {
707f296234cSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
708f296234cSPeter Krystad 		opts->backup = subflow_req->backup;
709f296234cSPeter Krystad 		opts->join_id = subflow_req->local_id;
710f296234cSPeter Krystad 		opts->thmac = subflow_req->thmac;
711f296234cSPeter Krystad 		opts->nonce = subflow_req->local_nonce;
712f296234cSPeter Krystad 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
713f296234cSPeter Krystad 			 subflow_req, opts->backup, opts->join_id,
714f296234cSPeter Krystad 			 opts->thmac, opts->nonce);
715f296234cSPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
716f296234cSPeter Krystad 		return true;
717cec37a6eSPeter Krystad 	}
718cec37a6eSPeter Krystad 	return false;
719cec37a6eSPeter Krystad }
720cec37a6eSPeter Krystad 
721d5824847SPaolo Abeni static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
722f296234cSPeter Krystad 				    struct mptcp_subflow_context *subflow,
723d22f4988SChristoph Paasch 				    struct sk_buff *skb,
724d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
725d22f4988SChristoph Paasch {
726d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
727f296234cSPeter Krystad 	 * will make the subflow fully established
728d22f4988SChristoph Paasch 	 */
729f296234cSPeter Krystad 	if (likely(subflow->fully_established)) {
730f296234cSPeter Krystad 		/* on passive sockets, check for 3rd ack retransmission
731f296234cSPeter Krystad 		 * note that msk is always set by subflow_syn_recv_sock()
732f296234cSPeter Krystad 		 * for mp_join subflows
733f296234cSPeter Krystad 		 */
734f296234cSPeter Krystad 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
735f296234cSPeter Krystad 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
736f296234cSPeter Krystad 		    subflow->mp_join && mp_opt->mp_join &&
737f296234cSPeter Krystad 		    READ_ONCE(msk->pm.server_side))
738d5824847SPaolo Abeni 			tcp_send_ack(ssk);
739f296234cSPeter Krystad 		goto fully_established;
740f296234cSPeter Krystad 	}
741d22f4988SChristoph Paasch 
742d5824847SPaolo Abeni 	/* we must process OoO packets before the first subflow is fully
743d5824847SPaolo Abeni 	 * established. OoO packets are instead a protocol violation
744d5824847SPaolo Abeni 	 * for MP_JOIN subflows as the peer must not send any data
745d5824847SPaolo Abeni 	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
746f296234cSPeter Krystad 	 */
747d5824847SPaolo Abeni 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
748d5824847SPaolo Abeni 		if (subflow->mp_join)
749d5824847SPaolo Abeni 			goto reset;
750f296234cSPeter Krystad 		return subflow->mp_capable;
751d5824847SPaolo Abeni 	}
752f296234cSPeter Krystad 
7535a91e32bSPaolo Abeni 	if (mp_opt->dss && mp_opt->use_ack) {
754f296234cSPeter Krystad 		/* subflows are fully established as soon as we get any
755f296234cSPeter Krystad 		 * additional ack.
756f296234cSPeter Krystad 		 */
7570be534f5SPaolo Abeni 		subflow->fully_established = 1;
758b93df08cSPaolo Abeni 		WRITE_ONCE(msk->fully_established, true);
759f296234cSPeter Krystad 		goto fully_established;
760f296234cSPeter Krystad 	}
761d22f4988SChristoph Paasch 
762d22f4988SChristoph Paasch 	/* If the first established packet does not contain MP_CAPABLE + data
763d5824847SPaolo Abeni 	 * then fallback to TCP. Fallback scenarios requires a reset for
764d5824847SPaolo Abeni 	 * MP_JOIN subflows.
765d22f4988SChristoph Paasch 	 */
766d22f4988SChristoph Paasch 	if (!mp_opt->mp_capable) {
767d5824847SPaolo Abeni 		if (subflow->mp_join)
768d5824847SPaolo Abeni 			goto reset;
769d22f4988SChristoph Paasch 		subflow->mp_capable = 0;
770e1ff9e82SDavide Caratti 		pr_fallback(msk);
771e1ff9e82SDavide Caratti 		__mptcp_do_fallback(msk);
772d22f4988SChristoph Paasch 		return false;
773d22f4988SChristoph Paasch 	}
774f296234cSPeter Krystad 
775d6085fe1SPaolo Abeni 	if (unlikely(!READ_ONCE(msk->pm.server_side)))
776d6085fe1SPaolo Abeni 		pr_warn_once("bogus mpc option on established client sk");
777b93df08cSPaolo Abeni 	mptcp_subflow_fully_established(subflow, mp_opt);
778f296234cSPeter Krystad 
779f296234cSPeter Krystad fully_established:
780f296234cSPeter Krystad 	if (likely(subflow->pm_notified))
781f296234cSPeter Krystad 		return true;
782f296234cSPeter Krystad 
783f296234cSPeter Krystad 	subflow->pm_notified = 1;
784ec3edaa7SPeter Krystad 	if (subflow->mp_join) {
785d5824847SPaolo Abeni 		clear_3rdack_retransmission(ssk);
786f296234cSPeter Krystad 		mptcp_pm_subflow_established(msk, subflow);
787ec3edaa7SPeter Krystad 	} else {
788f296234cSPeter Krystad 		mptcp_pm_fully_established(msk);
789ec3edaa7SPeter Krystad 	}
790d22f4988SChristoph Paasch 	return true;
791d5824847SPaolo Abeni 
792d5824847SPaolo Abeni reset:
793d5824847SPaolo Abeni 	mptcp_subflow_reset(ssk);
794d5824847SPaolo Abeni 	return false;
795d22f4988SChristoph Paasch }
796d22f4988SChristoph Paasch 
797cc9d2566SPaolo Abeni static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
798cc9d2566SPaolo Abeni {
799cc9d2566SPaolo Abeni 	u32 old_ack32, cur_ack32;
800cc9d2566SPaolo Abeni 
801cc9d2566SPaolo Abeni 	if (use_64bit)
802cc9d2566SPaolo Abeni 		return cur_ack;
803cc9d2566SPaolo Abeni 
804cc9d2566SPaolo Abeni 	old_ack32 = (u32)old_ack;
805cc9d2566SPaolo Abeni 	cur_ack32 = (u32)cur_ack;
806cc9d2566SPaolo Abeni 	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
807cc9d2566SPaolo Abeni 	if (unlikely(before(cur_ack32, old_ack32)))
808cc9d2566SPaolo Abeni 		return cur_ack + (1LL << 32);
809cc9d2566SPaolo Abeni 	return cur_ack;
810cc9d2566SPaolo Abeni }
811cc9d2566SPaolo Abeni 
812cc9d2566SPaolo Abeni static void update_una(struct mptcp_sock *msk,
813cc9d2566SPaolo Abeni 		       struct mptcp_options_received *mp_opt)
814cc9d2566SPaolo Abeni {
815cc9d2566SPaolo Abeni 	u64 new_snd_una, snd_una, old_snd_una = atomic64_read(&msk->snd_una);
816*eaa2ffabSPaolo Abeni 	u64 snd_nxt = READ_ONCE(msk->snd_nxt);
817cc9d2566SPaolo Abeni 
818cc9d2566SPaolo Abeni 	/* avoid ack expansion on update conflict, to reduce the risk of
819cc9d2566SPaolo Abeni 	 * wrongly expanding to a future ack sequence number, which is way
820cc9d2566SPaolo Abeni 	 * more dangerous than missing an ack
821cc9d2566SPaolo Abeni 	 */
822cc9d2566SPaolo Abeni 	new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
823cc9d2566SPaolo Abeni 
824cc9d2566SPaolo Abeni 	/* ACK for data not even sent yet? Ignore. */
825*eaa2ffabSPaolo Abeni 	if (after64(new_snd_una, snd_nxt))
826cc9d2566SPaolo Abeni 		new_snd_una = old_snd_una;
827cc9d2566SPaolo Abeni 
828cc9d2566SPaolo Abeni 	while (after64(new_snd_una, old_snd_una)) {
829cc9d2566SPaolo Abeni 		snd_una = old_snd_una;
830cc9d2566SPaolo Abeni 		old_snd_una = atomic64_cmpxchg(&msk->snd_una, snd_una,
831cc9d2566SPaolo Abeni 					       new_snd_una);
832b51f9b80SPaolo Abeni 		if (old_snd_una == snd_una) {
833b51f9b80SPaolo Abeni 			mptcp_data_acked((struct sock *)msk);
834cc9d2566SPaolo Abeni 			break;
835cc9d2566SPaolo Abeni 		}
836cc9d2566SPaolo Abeni 	}
837b51f9b80SPaolo Abeni }
838cc9d2566SPaolo Abeni 
8391a49b2c2SMat Martineau bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
8403721b9b6SMat Martineau {
8413721b9b6SMat Martineau 	/* Skip if DATA_FIN was already received.
8423721b9b6SMat Martineau 	 * If updating simultaneously with the recvmsg loop, values
8433721b9b6SMat Martineau 	 * should match. If they mismatch, the peer is misbehaving and
8443721b9b6SMat Martineau 	 * we will prefer the most recent information.
8453721b9b6SMat Martineau 	 */
8463721b9b6SMat Martineau 	if (READ_ONCE(msk->rcv_data_fin) || !READ_ONCE(msk->first))
8473721b9b6SMat Martineau 		return false;
8483721b9b6SMat Martineau 
8491a49b2c2SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin_seq,
8501a49b2c2SMat Martineau 		   expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
8513721b9b6SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin, 1);
8523721b9b6SMat Martineau 
8533721b9b6SMat Martineau 	return true;
8543721b9b6SMat Martineau }
8553721b9b6SMat Martineau 
8561b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk,
8571b1c7a0eSPeter Krystad 				struct mptcp_options_received *mp_opt)
8581b1c7a0eSPeter Krystad {
8591b1c7a0eSPeter Krystad 	u64 hmac = 0;
8601b1c7a0eSPeter Krystad 
8611b1c7a0eSPeter Krystad 	if (mp_opt->echo)
8621b1c7a0eSPeter Krystad 		return true;
8631b1c7a0eSPeter Krystad 
8641b1c7a0eSPeter Krystad 	if (mp_opt->family == MPTCP_ADDR_IPVERSION_4)
8651b1c7a0eSPeter Krystad 		hmac = add_addr_generate_hmac(msk->remote_key,
8661b1c7a0eSPeter Krystad 					      msk->local_key,
8671b1c7a0eSPeter Krystad 					      mp_opt->addr_id, &mp_opt->addr);
8681b1c7a0eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
8691b1c7a0eSPeter Krystad 	else
8701b1c7a0eSPeter Krystad 		hmac = add_addr6_generate_hmac(msk->remote_key,
8711b1c7a0eSPeter Krystad 					       msk->local_key,
8721b1c7a0eSPeter Krystad 					       mp_opt->addr_id, &mp_opt->addr6);
8731b1c7a0eSPeter Krystad #endif
8741b1c7a0eSPeter Krystad 
8751b1c7a0eSPeter Krystad 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
8761b1c7a0eSPeter Krystad 		 msk, (unsigned long long)hmac,
8771b1c7a0eSPeter Krystad 		 (unsigned long long)mp_opt->ahmac);
8781b1c7a0eSPeter Krystad 
8791b1c7a0eSPeter Krystad 	return hmac == mp_opt->ahmac;
8801b1c7a0eSPeter Krystad }
8811b1c7a0eSPeter Krystad 
88277d0cab9SFlorian Westphal void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
883648ef4b8SMat Martineau {
884d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
8851b1c7a0eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
886cfde141eSPaolo Abeni 	struct mptcp_options_received mp_opt;
887648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
888648ef4b8SMat Martineau 
889e1ff9e82SDavide Caratti 	if (__mptcp_check_fallback(msk))
890e1ff9e82SDavide Caratti 		return;
891e1ff9e82SDavide Caratti 
892cfde141eSPaolo Abeni 	mptcp_get_options(skb, &mp_opt);
893cfde141eSPaolo Abeni 	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
894d22f4988SChristoph Paasch 		return;
895648ef4b8SMat Martineau 
896cfde141eSPaolo Abeni 	if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
8971b1c7a0eSPeter Krystad 		struct mptcp_addr_info addr;
8981b1c7a0eSPeter Krystad 
899cfde141eSPaolo Abeni 		addr.port = htons(mp_opt.port);
900cfde141eSPaolo Abeni 		addr.id = mp_opt.addr_id;
901cfde141eSPaolo Abeni 		if (mp_opt.family == MPTCP_ADDR_IPVERSION_4) {
9021b1c7a0eSPeter Krystad 			addr.family = AF_INET;
903cfde141eSPaolo Abeni 			addr.addr = mp_opt.addr;
9041b1c7a0eSPeter Krystad 		}
9051b1c7a0eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
906cfde141eSPaolo Abeni 		else if (mp_opt.family == MPTCP_ADDR_IPVERSION_6) {
9071b1c7a0eSPeter Krystad 			addr.family = AF_INET6;
908cfde141eSPaolo Abeni 			addr.addr6 = mp_opt.addr6;
9091b1c7a0eSPeter Krystad 		}
9101b1c7a0eSPeter Krystad #endif
911a877de06SGeliang Tang 		if (!mp_opt.echo) {
9121b1c7a0eSPeter Krystad 			mptcp_pm_add_addr_received(msk, &addr);
913a877de06SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
914a877de06SGeliang Tang 		} else {
91500cfd77bSGeliang Tang 			mptcp_pm_del_add_timer(msk, &addr);
916a877de06SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
917a877de06SGeliang Tang 		}
918cfde141eSPaolo Abeni 		mp_opt.add_addr = 0;
9191b1c7a0eSPeter Krystad 	}
9201b1c7a0eSPeter Krystad 
921d0876b22SGeliang Tang 	if (mp_opt.rm_addr) {
922d0876b22SGeliang Tang 		mptcp_pm_rm_addr_received(msk, mp_opt.rm_id);
923d0876b22SGeliang Tang 		mp_opt.rm_addr = 0;
924d0876b22SGeliang Tang 	}
925d0876b22SGeliang Tang 
926cfde141eSPaolo Abeni 	if (!mp_opt.dss)
927648ef4b8SMat Martineau 		return;
928648ef4b8SMat Martineau 
929cc9d2566SPaolo Abeni 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
930cc9d2566SPaolo Abeni 	 * monodirectional flows will stuck
931cc9d2566SPaolo Abeni 	 */
932cfde141eSPaolo Abeni 	if (mp_opt.use_ack)
933cfde141eSPaolo Abeni 		update_una(msk, &mp_opt);
934cc9d2566SPaolo Abeni 
93506827b34SMat Martineau 	/* Zero-data-length packets are dropped by the caller and not
93606827b34SMat Martineau 	 * propagated to the MPTCP layer, so the skb extension does not
93706827b34SMat Martineau 	 * need to be allocated or populated. DATA_FIN information, if
93806827b34SMat Martineau 	 * present, needs to be updated here before the skb is freed.
93943b54c6eSMat Martineau 	 */
94043b54c6eSMat Martineau 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
94143b54c6eSMat Martineau 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
9421a49b2c2SMat Martineau 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
94343b54c6eSMat Martineau 		    schedule_work(&msk->work))
94443b54c6eSMat Martineau 			sock_hold(subflow->conn);
94506827b34SMat Martineau 
94606827b34SMat Martineau 		return;
94743b54c6eSMat Martineau 	}
94843b54c6eSMat Martineau 
949648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
950648ef4b8SMat Martineau 	if (!mpext)
951648ef4b8SMat Martineau 		return;
952648ef4b8SMat Martineau 
953648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
954648ef4b8SMat Martineau 
955cfde141eSPaolo Abeni 	if (mp_opt.use_map) {
956cfde141eSPaolo Abeni 		if (mp_opt.mpc_map) {
957cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
958cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
959cc7972eaSChristoph Paasch 			 */
960cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
961cc7972eaSChristoph Paasch 					     &mpext->data_seq);
962cc7972eaSChristoph Paasch 			mpext->data_seq++;
963cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
964cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
965cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
966a77895dbSPaolo Abeni 			mpext->data_fin = 0;
967cc7972eaSChristoph Paasch 		} else {
968cfde141eSPaolo Abeni 			mpext->data_seq = mp_opt.data_seq;
969cfde141eSPaolo Abeni 			mpext->subflow_seq = mp_opt.subflow_seq;
970cfde141eSPaolo Abeni 			mpext->dsn64 = mp_opt.dsn64;
971cfde141eSPaolo Abeni 			mpext->data_fin = mp_opt.data_fin;
972cc7972eaSChristoph Paasch 		}
973cfde141eSPaolo Abeni 		mpext->data_len = mp_opt.data_len;
974648ef4b8SMat Martineau 		mpext->use_map = 1;
975648ef4b8SMat Martineau 	}
976648ef4b8SMat Martineau }
977648ef4b8SMat Martineau 
978eda7acddSPeter Krystad void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts)
979eda7acddSPeter Krystad {
980cc7972eaSChristoph Paasch 	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
981eda7acddSPeter Krystad 	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
982eda7acddSPeter Krystad 		u8 len;
983eda7acddSPeter Krystad 
984eda7acddSPeter Krystad 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
985eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
986cec37a6eSPeter Krystad 		else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
987cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
988cc7972eaSChristoph Paasch 		else if (opts->ext_copy.data_len)
989cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
990eda7acddSPeter Krystad 		else
991eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
992eda7acddSPeter Krystad 
9933df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
9943df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
99565492c5aSPaolo Abeni 				      MPTCP_CAP_HMAC_SHA256);
996cc7972eaSChristoph Paasch 
997cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
998cc7972eaSChristoph Paasch 		    opts->suboptions))
999cc7972eaSChristoph Paasch 			goto mp_capable_done;
1000cc7972eaSChristoph Paasch 
1001eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
1002eda7acddSPeter Krystad 		ptr += 2;
1003cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1004cc7972eaSChristoph Paasch 			goto mp_capable_done;
1005cc7972eaSChristoph Paasch 
1006eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
1007eda7acddSPeter Krystad 		ptr += 2;
1008cc7972eaSChristoph Paasch 		if (!opts->ext_copy.data_len)
1009cc7972eaSChristoph Paasch 			goto mp_capable_done;
1010cc7972eaSChristoph Paasch 
1011cc7972eaSChristoph Paasch 		put_unaligned_be32(opts->ext_copy.data_len << 16 |
1012cc7972eaSChristoph Paasch 				   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1013cc7972eaSChristoph Paasch 		ptr += 1;
1014eda7acddSPeter Krystad 	}
10156d0060f6SMat Martineau 
1016cc7972eaSChristoph Paasch mp_capable_done:
10173df523abSPeter Krystad 	if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
10183df523abSPeter Krystad 		if (opts->ahmac)
10193df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
10203df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR, 0,
10213df523abSPeter Krystad 					      opts->addr_id);
10223df523abSPeter Krystad 		else
10233df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
10243df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR_BASE,
10253df523abSPeter Krystad 					      MPTCP_ADDR_ECHO,
10263df523abSPeter Krystad 					      opts->addr_id);
10273df523abSPeter Krystad 		memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4);
10283df523abSPeter Krystad 		ptr += 1;
10293df523abSPeter Krystad 		if (opts->ahmac) {
10303df523abSPeter Krystad 			put_unaligned_be64(opts->ahmac, ptr);
10313df523abSPeter Krystad 			ptr += 2;
10323df523abSPeter Krystad 		}
10333df523abSPeter Krystad 	}
10343df523abSPeter Krystad 
10353df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
10363df523abSPeter Krystad 	if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) {
10373df523abSPeter Krystad 		if (opts->ahmac)
10383df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
10393df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR6, 0,
10403df523abSPeter Krystad 					      opts->addr_id);
10413df523abSPeter Krystad 		else
10423df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
10433df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR6_BASE,
10443df523abSPeter Krystad 					      MPTCP_ADDR_ECHO,
10453df523abSPeter Krystad 					      opts->addr_id);
10463df523abSPeter Krystad 		memcpy((u8 *)ptr, opts->addr6.s6_addr, 16);
10473df523abSPeter Krystad 		ptr += 4;
10483df523abSPeter Krystad 		if (opts->ahmac) {
10493df523abSPeter Krystad 			put_unaligned_be64(opts->ahmac, ptr);
10503df523abSPeter Krystad 			ptr += 2;
10513df523abSPeter Krystad 		}
10523df523abSPeter Krystad 	}
10533df523abSPeter Krystad #endif
10543df523abSPeter Krystad 
10553df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
10563df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
10573df523abSPeter Krystad 				      TCPOLEN_MPTCP_RM_ADDR_BASE,
10583df523abSPeter Krystad 				      0, opts->rm_id);
10593df523abSPeter Krystad 	}
10603df523abSPeter Krystad 
1061ec3edaa7SPeter Krystad 	if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1062ec3edaa7SPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1063ec3edaa7SPeter Krystad 				      TCPOLEN_MPTCP_MPJ_SYN,
1064ec3edaa7SPeter Krystad 				      opts->backup, opts->join_id);
1065ec3edaa7SPeter Krystad 		put_unaligned_be32(opts->token, ptr);
1066ec3edaa7SPeter Krystad 		ptr += 1;
1067ec3edaa7SPeter Krystad 		put_unaligned_be32(opts->nonce, ptr);
1068ec3edaa7SPeter Krystad 		ptr += 1;
1069ec3edaa7SPeter Krystad 	}
1070ec3edaa7SPeter Krystad 
1071f296234cSPeter Krystad 	if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1072f296234cSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1073f296234cSPeter Krystad 				      TCPOLEN_MPTCP_MPJ_SYNACK,
1074f296234cSPeter Krystad 				      opts->backup, opts->join_id);
1075f296234cSPeter Krystad 		put_unaligned_be64(opts->thmac, ptr);
1076f296234cSPeter Krystad 		ptr += 2;
1077f296234cSPeter Krystad 		put_unaligned_be32(opts->nonce, ptr);
1078f296234cSPeter Krystad 		ptr += 1;
1079f296234cSPeter Krystad 	}
1080f296234cSPeter Krystad 
1081ec3edaa7SPeter Krystad 	if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1082ec3edaa7SPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1083ec3edaa7SPeter Krystad 				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1084ec3edaa7SPeter Krystad 		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1085ec3edaa7SPeter Krystad 		ptr += 5;
1086ec3edaa7SPeter Krystad 	}
1087ec3edaa7SPeter Krystad 
10886d0060f6SMat Martineau 	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
10896d0060f6SMat Martineau 		struct mptcp_ext *mpext = &opts->ext_copy;
10906d0060f6SMat Martineau 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
10916d0060f6SMat Martineau 		u8 flags = 0;
10926d0060f6SMat Martineau 
10936d0060f6SMat Martineau 		if (mpext->use_ack) {
1094a0c1d0eaSChristoph Paasch 			flags = MPTCP_DSS_HAS_ACK;
1095a0c1d0eaSChristoph Paasch 			if (mpext->ack64) {
10966d0060f6SMat Martineau 				len += TCPOLEN_MPTCP_DSS_ACK64;
1097a0c1d0eaSChristoph Paasch 				flags |= MPTCP_DSS_ACK64;
1098a0c1d0eaSChristoph Paasch 			} else {
1099a0c1d0eaSChristoph Paasch 				len += TCPOLEN_MPTCP_DSS_ACK32;
1100a0c1d0eaSChristoph Paasch 			}
11016d0060f6SMat Martineau 		}
11026d0060f6SMat Martineau 
11036d0060f6SMat Martineau 		if (mpext->use_map) {
11046d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_MAP64;
11056d0060f6SMat Martineau 
11066d0060f6SMat Martineau 			/* Use only 64-bit mapping flags for now, add
11076d0060f6SMat Martineau 			 * support for optional 32-bit mappings later.
11086d0060f6SMat Martineau 			 */
11096d0060f6SMat Martineau 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
11106d0060f6SMat Martineau 			if (mpext->data_fin)
11116d0060f6SMat Martineau 				flags |= MPTCP_DSS_DATA_FIN;
11126d0060f6SMat Martineau 		}
11136d0060f6SMat Martineau 
11143df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
11156d0060f6SMat Martineau 
11166d0060f6SMat Martineau 		if (mpext->use_ack) {
1117a0c1d0eaSChristoph Paasch 			if (mpext->ack64) {
11186d0060f6SMat Martineau 				put_unaligned_be64(mpext->data_ack, ptr);
11196d0060f6SMat Martineau 				ptr += 2;
1120a0c1d0eaSChristoph Paasch 			} else {
1121a0c1d0eaSChristoph Paasch 				put_unaligned_be32(mpext->data_ack32, ptr);
1122a0c1d0eaSChristoph Paasch 				ptr += 1;
1123a0c1d0eaSChristoph Paasch 			}
11246d0060f6SMat Martineau 		}
11256d0060f6SMat Martineau 
11266d0060f6SMat Martineau 		if (mpext->use_map) {
11276d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_seq, ptr);
11286d0060f6SMat Martineau 			ptr += 2;
11296d0060f6SMat Martineau 			put_unaligned_be32(mpext->subflow_seq, ptr);
11306d0060f6SMat Martineau 			ptr += 1;
11316d0060f6SMat Martineau 			put_unaligned_be32(mpext->data_len << 16 |
11326d0060f6SMat Martineau 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
11336d0060f6SMat Martineau 		}
11346d0060f6SMat Martineau 	}
1135eda7acddSPeter Krystad }
1136