xref: /openbmc/linux/net/mptcp/options.c (revision 3df523ab582c52f745f9a73b9ebf9368ede555ac)
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 
7eda7acddSPeter Krystad #include <linux/kernel.h>
8eda7acddSPeter Krystad #include <net/tcp.h>
9eda7acddSPeter Krystad #include <net/mptcp.h>
10eda7acddSPeter Krystad #include "protocol.h"
11eda7acddSPeter Krystad 
1265492c5aSPaolo Abeni static bool mptcp_cap_flag_sha256(u8 flags)
1365492c5aSPaolo Abeni {
1465492c5aSPaolo Abeni 	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
1565492c5aSPaolo Abeni }
1665492c5aSPaolo Abeni 
17cc7972eaSChristoph Paasch void mptcp_parse_option(const struct sk_buff *skb, const unsigned char *ptr,
18cc7972eaSChristoph Paasch 			int opsize, struct tcp_options_received *opt_rx)
19eda7acddSPeter Krystad {
20eda7acddSPeter Krystad 	struct mptcp_options_received *mp_opt = &opt_rx->mptcp;
21eda7acddSPeter Krystad 	u8 subtype = *ptr >> 4;
22648ef4b8SMat Martineau 	int expected_opsize;
23eda7acddSPeter Krystad 	u8 version;
24eda7acddSPeter Krystad 	u8 flags;
25eda7acddSPeter Krystad 
26eda7acddSPeter Krystad 	switch (subtype) {
27eda7acddSPeter Krystad 	case MPTCPOPT_MP_CAPABLE:
28cc7972eaSChristoph Paasch 		/* strict size checking */
29cc7972eaSChristoph Paasch 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
30cc7972eaSChristoph Paasch 			if (skb->len > tcp_hdr(skb)->doff << 2)
31cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
32cc7972eaSChristoph Paasch 			else
33cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
34cc7972eaSChristoph Paasch 		} else {
35cc7972eaSChristoph Paasch 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
36cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
37cc7972eaSChristoph Paasch 			else
38cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
39cc7972eaSChristoph Paasch 		}
40cc7972eaSChristoph Paasch 		if (opsize != expected_opsize)
41eda7acddSPeter Krystad 			break;
42eda7acddSPeter Krystad 
43cc7972eaSChristoph Paasch 		/* try to be gentle vs future versions on the initial syn */
44eda7acddSPeter Krystad 		version = *ptr++ & MPTCP_VERSION_MASK;
45cc7972eaSChristoph Paasch 		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
46eda7acddSPeter Krystad 			if (version != MPTCP_SUPPORTED_VERSION)
47eda7acddSPeter Krystad 				break;
48cc7972eaSChristoph Paasch 		} else if (version < MPTCP_SUPPORTED_VERSION) {
49cc7972eaSChristoph Paasch 			break;
50cc7972eaSChristoph Paasch 		}
51eda7acddSPeter Krystad 
52eda7acddSPeter Krystad 		flags = *ptr++;
5365492c5aSPaolo Abeni 		if (!mptcp_cap_flag_sha256(flags) ||
54eda7acddSPeter Krystad 		    (flags & MPTCP_CAP_EXTENSIBILITY))
55eda7acddSPeter Krystad 			break;
56eda7acddSPeter Krystad 
57eda7acddSPeter Krystad 		/* RFC 6824, Section 3.1:
58eda7acddSPeter Krystad 		 * "For the Checksum Required bit (labeled "A"), if either
59eda7acddSPeter Krystad 		 * host requires the use of checksums, checksums MUST be used.
60eda7acddSPeter Krystad 		 * In other words, the only way for checksums not to be used
61eda7acddSPeter Krystad 		 * is if both hosts in their SYNs set A=0."
62eda7acddSPeter Krystad 		 *
63eda7acddSPeter Krystad 		 * Section 3.3.0:
64eda7acddSPeter Krystad 		 * "If a checksum is not present when its use has been
65eda7acddSPeter Krystad 		 * negotiated, the receiver MUST close the subflow with a RST as
66eda7acddSPeter Krystad 		 * it is considered broken."
67eda7acddSPeter Krystad 		 *
68eda7acddSPeter Krystad 		 * We don't implement DSS checksum - fall back to TCP.
69eda7acddSPeter Krystad 		 */
70eda7acddSPeter Krystad 		if (flags & MPTCP_CAP_CHECKSUM_REQD)
71eda7acddSPeter Krystad 			break;
72eda7acddSPeter Krystad 
73eda7acddSPeter Krystad 		mp_opt->mp_capable = 1;
74cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
75eda7acddSPeter Krystad 			mp_opt->sndr_key = get_unaligned_be64(ptr);
76eda7acddSPeter Krystad 			ptr += 8;
77cc7972eaSChristoph Paasch 		}
78cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
79eda7acddSPeter Krystad 			mp_opt->rcvr_key = get_unaligned_be64(ptr);
80eda7acddSPeter Krystad 			ptr += 8;
81eda7acddSPeter Krystad 		}
82cc7972eaSChristoph Paasch 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
83cc7972eaSChristoph Paasch 			/* Section 3.1.:
84cc7972eaSChristoph Paasch 			 * "the data parameters in a MP_CAPABLE are semantically
85cc7972eaSChristoph Paasch 			 * equivalent to those in a DSS option and can be used
86cc7972eaSChristoph Paasch 			 * interchangeably."
87cc7972eaSChristoph Paasch 			 */
88cc7972eaSChristoph Paasch 			mp_opt->dss = 1;
89cc7972eaSChristoph Paasch 			mp_opt->use_map = 1;
90cc7972eaSChristoph Paasch 			mp_opt->mpc_map = 1;
91cc7972eaSChristoph Paasch 			mp_opt->data_len = get_unaligned_be16(ptr);
92cc7972eaSChristoph Paasch 			ptr += 2;
93cc7972eaSChristoph Paasch 		}
94cc7972eaSChristoph Paasch 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
95cc7972eaSChristoph Paasch 			 version, flags, opsize, mp_opt->sndr_key,
96cc7972eaSChristoph Paasch 			 mp_opt->rcvr_key, mp_opt->data_len);
97eda7acddSPeter Krystad 		break;
98eda7acddSPeter Krystad 
99eda7acddSPeter Krystad 	case MPTCPOPT_DSS:
100eda7acddSPeter Krystad 		pr_debug("DSS");
101648ef4b8SMat Martineau 		ptr++;
102648ef4b8SMat Martineau 
103cc7972eaSChristoph Paasch 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
104cc7972eaSChristoph Paasch 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
105cc7972eaSChristoph Paasch 		 * map info accordingly
106cc7972eaSChristoph Paasch 		 */
107cc7972eaSChristoph Paasch 		mp_opt->mpc_map = 0;
108648ef4b8SMat Martineau 		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
109648ef4b8SMat Martineau 		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
110648ef4b8SMat Martineau 		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
111648ef4b8SMat Martineau 		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
112648ef4b8SMat Martineau 		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
113648ef4b8SMat Martineau 		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
114648ef4b8SMat Martineau 
115648ef4b8SMat Martineau 		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
116648ef4b8SMat Martineau 			 mp_opt->data_fin, mp_opt->dsn64,
117648ef4b8SMat Martineau 			 mp_opt->use_map, mp_opt->ack64,
118648ef4b8SMat Martineau 			 mp_opt->use_ack);
119648ef4b8SMat Martineau 
120648ef4b8SMat Martineau 		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
121648ef4b8SMat Martineau 
122648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
123648ef4b8SMat Martineau 			if (mp_opt->ack64)
124648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
125648ef4b8SMat Martineau 			else
126648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
127648ef4b8SMat Martineau 		}
128648ef4b8SMat Martineau 
129648ef4b8SMat Martineau 		if (mp_opt->use_map) {
130648ef4b8SMat Martineau 			if (mp_opt->dsn64)
131648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
132648ef4b8SMat Martineau 			else
133648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
134648ef4b8SMat Martineau 		}
135648ef4b8SMat Martineau 
136648ef4b8SMat Martineau 		/* RFC 6824, Section 3.3:
137648ef4b8SMat Martineau 		 * If a checksum is present, but its use had
138648ef4b8SMat Martineau 		 * not been negotiated in the MP_CAPABLE handshake,
139648ef4b8SMat Martineau 		 * the checksum field MUST be ignored.
140648ef4b8SMat Martineau 		 */
141648ef4b8SMat Martineau 		if (opsize != expected_opsize &&
142648ef4b8SMat Martineau 		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
143648ef4b8SMat Martineau 			break;
144648ef4b8SMat Martineau 
145eda7acddSPeter Krystad 		mp_opt->dss = 1;
146648ef4b8SMat Martineau 
147648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
148648ef4b8SMat Martineau 			if (mp_opt->ack64) {
149648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be64(ptr);
150648ef4b8SMat Martineau 				ptr += 8;
151648ef4b8SMat Martineau 			} else {
152648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be32(ptr);
153648ef4b8SMat Martineau 				ptr += 4;
154648ef4b8SMat Martineau 			}
155648ef4b8SMat Martineau 
156648ef4b8SMat Martineau 			pr_debug("data_ack=%llu", mp_opt->data_ack);
157648ef4b8SMat Martineau 		}
158648ef4b8SMat Martineau 
159648ef4b8SMat Martineau 		if (mp_opt->use_map) {
160648ef4b8SMat Martineau 			if (mp_opt->dsn64) {
161648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be64(ptr);
162648ef4b8SMat Martineau 				ptr += 8;
163648ef4b8SMat Martineau 			} else {
164648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be32(ptr);
165648ef4b8SMat Martineau 				ptr += 4;
166648ef4b8SMat Martineau 			}
167648ef4b8SMat Martineau 
168648ef4b8SMat Martineau 			mp_opt->subflow_seq = get_unaligned_be32(ptr);
169648ef4b8SMat Martineau 			ptr += 4;
170648ef4b8SMat Martineau 
171648ef4b8SMat Martineau 			mp_opt->data_len = get_unaligned_be16(ptr);
172648ef4b8SMat Martineau 			ptr += 2;
173648ef4b8SMat Martineau 
174648ef4b8SMat Martineau 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
175648ef4b8SMat Martineau 				 mp_opt->data_seq, mp_opt->subflow_seq,
176648ef4b8SMat Martineau 				 mp_opt->data_len);
177648ef4b8SMat Martineau 		}
178648ef4b8SMat Martineau 
179eda7acddSPeter Krystad 		break;
180eda7acddSPeter Krystad 
181*3df523abSPeter Krystad 	case MPTCPOPT_ADD_ADDR:
182*3df523abSPeter Krystad 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
183*3df523abSPeter Krystad 		if (!mp_opt->echo) {
184*3df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
185*3df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
186*3df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
187*3df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
188*3df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
189*3df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
190*3df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
191*3df523abSPeter Krystad #endif
192*3df523abSPeter Krystad 			else
193*3df523abSPeter Krystad 				break;
194*3df523abSPeter Krystad 		} else {
195*3df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
196*3df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
197*3df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
198*3df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
199*3df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
200*3df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
201*3df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
202*3df523abSPeter Krystad #endif
203*3df523abSPeter Krystad 			else
204*3df523abSPeter Krystad 				break;
205*3df523abSPeter Krystad 		}
206*3df523abSPeter Krystad 
207*3df523abSPeter Krystad 		mp_opt->add_addr = 1;
208*3df523abSPeter Krystad 		mp_opt->port = 0;
209*3df523abSPeter Krystad 		mp_opt->addr_id = *ptr++;
210*3df523abSPeter Krystad 		pr_debug("ADD_ADDR: id=%d", mp_opt->addr_id);
211*3df523abSPeter Krystad 		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
212*3df523abSPeter Krystad 			memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4);
213*3df523abSPeter Krystad 			ptr += 4;
214*3df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
215*3df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
216*3df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
217*3df523abSPeter Krystad 				ptr += 2;
218*3df523abSPeter Krystad 			}
219*3df523abSPeter Krystad 		}
220*3df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
221*3df523abSPeter Krystad 		else {
222*3df523abSPeter Krystad 			memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16);
223*3df523abSPeter Krystad 			ptr += 16;
224*3df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
225*3df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
226*3df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
227*3df523abSPeter Krystad 				ptr += 2;
228*3df523abSPeter Krystad 			}
229*3df523abSPeter Krystad 		}
230*3df523abSPeter Krystad #endif
231*3df523abSPeter Krystad 		if (!mp_opt->echo) {
232*3df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
233*3df523abSPeter Krystad 			ptr += 8;
234*3df523abSPeter Krystad 		}
235*3df523abSPeter Krystad 		break;
236*3df523abSPeter Krystad 
237*3df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
238*3df523abSPeter Krystad 		if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE)
239*3df523abSPeter Krystad 			break;
240*3df523abSPeter Krystad 
241*3df523abSPeter Krystad 		mp_opt->rm_addr = 1;
242*3df523abSPeter Krystad 		mp_opt->rm_id = *ptr++;
243*3df523abSPeter Krystad 		pr_debug("RM_ADDR: id=%d", mp_opt->rm_id);
244*3df523abSPeter Krystad 		break;
245*3df523abSPeter Krystad 
246eda7acddSPeter Krystad 	default:
247eda7acddSPeter Krystad 		break;
248eda7acddSPeter Krystad 	}
249eda7acddSPeter Krystad }
250eda7acddSPeter Krystad 
251cec37a6eSPeter Krystad void mptcp_get_options(const struct sk_buff *skb,
252cec37a6eSPeter Krystad 		       struct tcp_options_received *opt_rx)
253cec37a6eSPeter Krystad {
254cec37a6eSPeter Krystad 	const unsigned char *ptr;
255cec37a6eSPeter Krystad 	const struct tcphdr *th = tcp_hdr(skb);
256cec37a6eSPeter Krystad 	int length = (th->doff * 4) - sizeof(struct tcphdr);
257cec37a6eSPeter Krystad 
258cec37a6eSPeter Krystad 	ptr = (const unsigned char *)(th + 1);
259cec37a6eSPeter Krystad 
260cec37a6eSPeter Krystad 	while (length > 0) {
261cec37a6eSPeter Krystad 		int opcode = *ptr++;
262cec37a6eSPeter Krystad 		int opsize;
263cec37a6eSPeter Krystad 
264cec37a6eSPeter Krystad 		switch (opcode) {
265cec37a6eSPeter Krystad 		case TCPOPT_EOL:
266cec37a6eSPeter Krystad 			return;
267cec37a6eSPeter Krystad 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
268cec37a6eSPeter Krystad 			length--;
269cec37a6eSPeter Krystad 			continue;
270cec37a6eSPeter Krystad 		default:
271cec37a6eSPeter Krystad 			opsize = *ptr++;
272cec37a6eSPeter Krystad 			if (opsize < 2) /* "silly options" */
273cec37a6eSPeter Krystad 				return;
274cec37a6eSPeter Krystad 			if (opsize > length)
275cec37a6eSPeter Krystad 				return;	/* don't parse partial options */
276cec37a6eSPeter Krystad 			if (opcode == TCPOPT_MPTCP)
277cc7972eaSChristoph Paasch 				mptcp_parse_option(skb, ptr, opsize, opt_rx);
278cec37a6eSPeter Krystad 			ptr += opsize - 2;
279cec37a6eSPeter Krystad 			length -= opsize;
280cec37a6eSPeter Krystad 		}
281cec37a6eSPeter Krystad 	}
282cec37a6eSPeter Krystad }
283cec37a6eSPeter Krystad 
284cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
285cc7972eaSChristoph Paasch 		       unsigned int *size, struct mptcp_out_options *opts)
286cec37a6eSPeter Krystad {
287cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
288cec37a6eSPeter Krystad 
289cc7972eaSChristoph Paasch 	/* we will use snd_isn to detect first pkt [re]transmission
290cc7972eaSChristoph Paasch 	 * in mptcp_established_options_mp()
291cc7972eaSChristoph Paasch 	 */
292cc7972eaSChristoph Paasch 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
293cec37a6eSPeter Krystad 	if (subflow->request_mptcp) {
294cec37a6eSPeter Krystad 		pr_debug("local_key=%llu", subflow->local_key);
295cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
296cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
297cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYN;
298cec37a6eSPeter Krystad 		return true;
299cec37a6eSPeter Krystad 	}
300cec37a6eSPeter Krystad 	return false;
301cec37a6eSPeter Krystad }
302cec37a6eSPeter Krystad 
303cec37a6eSPeter Krystad void mptcp_rcv_synsent(struct sock *sk)
304cec37a6eSPeter Krystad {
305cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
306cec37a6eSPeter Krystad 	struct tcp_sock *tp = tcp_sk(sk);
307cec37a6eSPeter Krystad 
308cec37a6eSPeter Krystad 	pr_debug("subflow=%p", subflow);
309cec37a6eSPeter Krystad 	if (subflow->request_mptcp && tp->rx_opt.mptcp.mp_capable) {
310cec37a6eSPeter Krystad 		subflow->mp_capable = 1;
311d22f4988SChristoph Paasch 		subflow->can_ack = 1;
312cec37a6eSPeter Krystad 		subflow->remote_key = tp->rx_opt.mptcp.sndr_key;
313cec37a6eSPeter Krystad 	} else {
314cec37a6eSPeter Krystad 		tcp_sk(sk)->is_mptcp = 0;
315cec37a6eSPeter Krystad 	}
316cec37a6eSPeter Krystad }
317cec37a6eSPeter Krystad 
318cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
319cc7972eaSChristoph Paasch 					 unsigned int *size,
3206d0060f6SMat Martineau 					 unsigned int remaining,
321cec37a6eSPeter Krystad 					 struct mptcp_out_options *opts)
322cec37a6eSPeter Krystad {
323cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
324cc7972eaSChristoph Paasch 	struct mptcp_ext *mpext;
325cc7972eaSChristoph Paasch 	unsigned int data_len;
326cec37a6eSPeter Krystad 
3270be534f5SPaolo Abeni 	pr_debug("subflow=%p fully established=%d seq=%x:%x remaining=%d",
3280be534f5SPaolo Abeni 		 subflow, subflow->fully_established, subflow->snd_isn,
329cc7972eaSChristoph Paasch 		 skb ? TCP_SKB_CB(skb)->seq : 0, remaining);
330cc7972eaSChristoph Paasch 
3310be534f5SPaolo Abeni 	if (subflow->mp_capable && !subflow->fully_established && skb &&
332cc7972eaSChristoph Paasch 	    subflow->snd_isn == TCP_SKB_CB(skb)->seq) {
333cc7972eaSChristoph Paasch 		/* When skb is not available, we better over-estimate the
334cc7972eaSChristoph Paasch 		 * emitted options len. A full DSS option is longer than
335cc7972eaSChristoph Paasch 		 * TCPOLEN_MPTCP_MPC_ACK_DATA, so let's the caller try to fit
336cc7972eaSChristoph Paasch 		 * that.
337cc7972eaSChristoph Paasch 		 */
338cc7972eaSChristoph Paasch 		mpext = mptcp_get_ext(skb);
339cc7972eaSChristoph Paasch 		data_len = mpext ? mpext->data_len : 0;
340cc7972eaSChristoph Paasch 
341cc7972eaSChristoph Paasch 		/* we will check ext_copy.data_len in mptcp_write_options() to
342cc7972eaSChristoph Paasch 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
343cc7972eaSChristoph Paasch 		 * TCPOLEN_MPTCP_MPC_ACK
344cc7972eaSChristoph Paasch 		 */
345cc7972eaSChristoph Paasch 		opts->ext_copy.data_len = data_len;
346cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
347cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
348cec37a6eSPeter Krystad 		opts->rcvr_key = subflow->remote_key;
349cc7972eaSChristoph Paasch 
350cc7972eaSChristoph Paasch 		/* Section 3.1.
351cc7972eaSChristoph Paasch 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
352cc7972eaSChristoph Paasch 		 * packets that start the first subflow of an MPTCP connection,
353cc7972eaSChristoph Paasch 		 * as well as the first packet that carries data
354cc7972eaSChristoph Paasch 		 */
355cc7972eaSChristoph Paasch 		if (data_len > 0)
356cc7972eaSChristoph Paasch 			*size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
357cc7972eaSChristoph Paasch 		else
358cec37a6eSPeter Krystad 			*size = TCPOLEN_MPTCP_MPC_ACK;
359cc7972eaSChristoph Paasch 
360cc7972eaSChristoph Paasch 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
361cc7972eaSChristoph Paasch 			 subflow, subflow->local_key, subflow->remote_key,
362cc7972eaSChristoph Paasch 			 data_len);
363cc7972eaSChristoph Paasch 
364cec37a6eSPeter Krystad 		return true;
365cec37a6eSPeter Krystad 	}
366cec37a6eSPeter Krystad 	return false;
367cec37a6eSPeter Krystad }
368cec37a6eSPeter Krystad 
3696d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
3706d0060f6SMat Martineau 				 struct mptcp_ext *ext)
3716d0060f6SMat Martineau {
3726d0060f6SMat Martineau 	if (!ext->use_map) {
3736d0060f6SMat Martineau 		/* RFC6824 requires a DSS mapping with specific values
3746d0060f6SMat Martineau 		 * if DATA_FIN is set but no data payload is mapped
3756d0060f6SMat Martineau 		 */
3766d37a0b8SMat Martineau 		ext->data_fin = 1;
3776d0060f6SMat Martineau 		ext->use_map = 1;
3786d0060f6SMat Martineau 		ext->dsn64 = 1;
37976c42a29SMat Martineau 		ext->data_seq = subflow->data_fin_tx_seq;
3806d0060f6SMat Martineau 		ext->subflow_seq = 0;
3816d0060f6SMat Martineau 		ext->data_len = 1;
3826d37a0b8SMat Martineau 	} else if (ext->data_seq + ext->data_len == subflow->data_fin_tx_seq) {
3836d37a0b8SMat Martineau 		/* If there's an existing DSS mapping and it is the
3846d37a0b8SMat Martineau 		 * final mapping, DATA_FIN consumes 1 additional byte of
3856d37a0b8SMat Martineau 		 * mapping space.
3866d0060f6SMat Martineau 		 */
3876d37a0b8SMat Martineau 		ext->data_fin = 1;
3886d0060f6SMat Martineau 		ext->data_len++;
3896d0060f6SMat Martineau 	}
3906d0060f6SMat Martineau }
3916d0060f6SMat Martineau 
3926d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
3936d0060f6SMat Martineau 					  unsigned int *size,
3946d0060f6SMat Martineau 					  unsigned int remaining,
3956d0060f6SMat Martineau 					  struct mptcp_out_options *opts)
3966d0060f6SMat Martineau {
3976d0060f6SMat Martineau 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
3986d0060f6SMat Martineau 	unsigned int dss_size = 0;
3996d0060f6SMat Martineau 	struct mptcp_ext *mpext;
4006d0060f6SMat Martineau 	struct mptcp_sock *msk;
4016d0060f6SMat Martineau 	unsigned int ack_size;
402d22f4988SChristoph Paasch 	bool ret = false;
4036d0060f6SMat Martineau 	u8 tcp_fin;
4046d0060f6SMat Martineau 
4056d0060f6SMat Martineau 	if (skb) {
4066d0060f6SMat Martineau 		mpext = mptcp_get_ext(skb);
4076d0060f6SMat Martineau 		tcp_fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
4086d0060f6SMat Martineau 	} else {
4096d0060f6SMat Martineau 		mpext = NULL;
4106d0060f6SMat Martineau 		tcp_fin = 0;
4116d0060f6SMat Martineau 	}
4126d0060f6SMat Martineau 
4136d0060f6SMat Martineau 	if (!skb || (mpext && mpext->use_map) || tcp_fin) {
4146d0060f6SMat Martineau 		unsigned int map_size;
4156d0060f6SMat Martineau 
4166d0060f6SMat Martineau 		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
4176d0060f6SMat Martineau 
4186d0060f6SMat Martineau 		remaining -= map_size;
4196d0060f6SMat Martineau 		dss_size = map_size;
4206d0060f6SMat Martineau 		if (mpext)
4216d0060f6SMat Martineau 			opts->ext_copy = *mpext;
4226d0060f6SMat Martineau 
42376c42a29SMat Martineau 		if (skb && tcp_fin && subflow->data_fin_tx_enable)
4246d0060f6SMat Martineau 			mptcp_write_data_fin(subflow, &opts->ext_copy);
425d22f4988SChristoph Paasch 		ret = true;
426d22f4988SChristoph Paasch 	}
427d22f4988SChristoph Paasch 
4282398e399SPaolo Abeni 	/* passive sockets msk will set the 'can_ack' after accept(), even
4292398e399SPaolo Abeni 	 * if the first subflow may have the already the remote key handy
4302398e399SPaolo Abeni 	 */
431d22f4988SChristoph Paasch 	opts->ext_copy.use_ack = 0;
432d22f4988SChristoph Paasch 	msk = mptcp_sk(subflow->conn);
433dc093db5SPaolo Abeni 	if (!READ_ONCE(msk->can_ack)) {
434d22f4988SChristoph Paasch 		*size = ALIGN(dss_size, 4);
435d22f4988SChristoph Paasch 		return ret;
4366d0060f6SMat Martineau 	}
4376d0060f6SMat Martineau 
4386d0060f6SMat Martineau 	ack_size = TCPOLEN_MPTCP_DSS_ACK64;
4396d0060f6SMat Martineau 
4406d0060f6SMat Martineau 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
4416d0060f6SMat Martineau 	if (dss_size == 0)
4426d0060f6SMat Martineau 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
4436d0060f6SMat Martineau 
4446d0060f6SMat Martineau 	dss_size += ack_size;
4456d0060f6SMat Martineau 
446dc093db5SPaolo Abeni 	opts->ext_copy.data_ack = msk->ack_seq;
4476d0060f6SMat Martineau 	opts->ext_copy.ack64 = 1;
4486d0060f6SMat Martineau 	opts->ext_copy.use_ack = 1;
4496d0060f6SMat Martineau 
4506d0060f6SMat Martineau 	*size = ALIGN(dss_size, 4);
4516d0060f6SMat Martineau 	return true;
4526d0060f6SMat Martineau }
4536d0060f6SMat Martineau 
454*3df523abSPeter Krystad static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
455*3df523abSPeter Krystad 				  struct in_addr *addr)
456*3df523abSPeter Krystad {
457*3df523abSPeter Krystad 	u8 hmac[MPTCP_ADDR_HMAC_LEN];
458*3df523abSPeter Krystad 	u8 msg[7];
459*3df523abSPeter Krystad 
460*3df523abSPeter Krystad 	msg[0] = addr_id;
461*3df523abSPeter Krystad 	memcpy(&msg[1], &addr->s_addr, 4);
462*3df523abSPeter Krystad 	msg[5] = 0;
463*3df523abSPeter Krystad 	msg[6] = 0;
464*3df523abSPeter Krystad 
465*3df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
466*3df523abSPeter Krystad 
467*3df523abSPeter Krystad 	return get_unaligned_be64(hmac);
468*3df523abSPeter Krystad }
469*3df523abSPeter Krystad 
470*3df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
471*3df523abSPeter Krystad static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
472*3df523abSPeter Krystad 				   struct in6_addr *addr)
473*3df523abSPeter Krystad {
474*3df523abSPeter Krystad 	u8 hmac[MPTCP_ADDR_HMAC_LEN];
475*3df523abSPeter Krystad 	u8 msg[19];
476*3df523abSPeter Krystad 
477*3df523abSPeter Krystad 	msg[0] = addr_id;
478*3df523abSPeter Krystad 	memcpy(&msg[1], &addr->s6_addr, 16);
479*3df523abSPeter Krystad 	msg[17] = 0;
480*3df523abSPeter Krystad 	msg[18] = 0;
481*3df523abSPeter Krystad 
482*3df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
483*3df523abSPeter Krystad 
484*3df523abSPeter Krystad 	return get_unaligned_be64(hmac);
485*3df523abSPeter Krystad }
486*3df523abSPeter Krystad #endif
487*3df523abSPeter Krystad 
488*3df523abSPeter Krystad static bool mptcp_established_options_addr(struct sock *sk,
489*3df523abSPeter Krystad 					   unsigned int *size,
490*3df523abSPeter Krystad 					   unsigned int remaining,
491*3df523abSPeter Krystad 					   struct mptcp_out_options *opts)
492*3df523abSPeter Krystad {
493*3df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
494*3df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
495*3df523abSPeter Krystad 	struct sockaddr_storage saddr;
496*3df523abSPeter Krystad 	u8 id;
497*3df523abSPeter Krystad 
498*3df523abSPeter Krystad 	id = 0;
499*3df523abSPeter Krystad 	memset(&saddr, 0, sizeof(saddr));
500*3df523abSPeter Krystad 
501*3df523abSPeter Krystad 	if (saddr.ss_family == AF_INET) {
502*3df523abSPeter Krystad 		if (remaining < TCPOLEN_MPTCP_ADD_ADDR)
503*3df523abSPeter Krystad 			return false;
504*3df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
505*3df523abSPeter Krystad 		opts->addr_id = id;
506*3df523abSPeter Krystad 		opts->addr = ((struct sockaddr_in *)&saddr)->sin_addr;
507*3df523abSPeter Krystad 		opts->ahmac = add_addr_generate_hmac(msk->local_key,
508*3df523abSPeter Krystad 						     msk->remote_key,
509*3df523abSPeter Krystad 						     opts->addr_id,
510*3df523abSPeter Krystad 						     &opts->addr);
511*3df523abSPeter Krystad 		*size = TCPOLEN_MPTCP_ADD_ADDR;
512*3df523abSPeter Krystad 	}
513*3df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
514*3df523abSPeter Krystad 	else if (saddr.ss_family == AF_INET6) {
515*3df523abSPeter Krystad 		if (remaining < TCPOLEN_MPTCP_ADD_ADDR6)
516*3df523abSPeter Krystad 			return false;
517*3df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
518*3df523abSPeter Krystad 		opts->addr_id = id;
519*3df523abSPeter Krystad 		opts->ahmac = add_addr6_generate_hmac(msk->local_key,
520*3df523abSPeter Krystad 						      msk->remote_key,
521*3df523abSPeter Krystad 						      opts->addr_id,
522*3df523abSPeter Krystad 						      &opts->addr6);
523*3df523abSPeter Krystad 		opts->addr6 = ((struct sockaddr_in6 *)&saddr)->sin6_addr;
524*3df523abSPeter Krystad 		*size = TCPOLEN_MPTCP_ADD_ADDR6;
525*3df523abSPeter Krystad 	}
526*3df523abSPeter Krystad #endif
527*3df523abSPeter Krystad 	pr_debug("addr_id=%d, ahmac=%llu", opts->addr_id, opts->ahmac);
528*3df523abSPeter Krystad 
529*3df523abSPeter Krystad 	return true;
530*3df523abSPeter Krystad }
531*3df523abSPeter Krystad 
5326d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
5336d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
5346d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
5356d0060f6SMat Martineau {
5366d0060f6SMat Martineau 	unsigned int opt_size = 0;
5376d0060f6SMat Martineau 	bool ret = false;
5386d0060f6SMat Martineau 
539*3df523abSPeter Krystad 	opts->suboptions = 0;
540*3df523abSPeter Krystad 
541cc7972eaSChristoph Paasch 	if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
5426d0060f6SMat Martineau 		ret = true;
5436d0060f6SMat Martineau 	else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
5446d0060f6SMat Martineau 					       opts))
5456d0060f6SMat Martineau 		ret = true;
5466d0060f6SMat Martineau 
5476d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
5486d0060f6SMat Martineau 	 * TCP option space would be fatal
5496d0060f6SMat Martineau 	 */
5506d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
5516d0060f6SMat Martineau 		return false;
5526d0060f6SMat Martineau 
5536d0060f6SMat Martineau 	*size += opt_size;
5546d0060f6SMat Martineau 	remaining -= opt_size;
555*3df523abSPeter Krystad 	if (mptcp_established_options_addr(sk, &opt_size, remaining, opts)) {
556*3df523abSPeter Krystad 		*size += opt_size;
557*3df523abSPeter Krystad 		remaining -= opt_size;
558*3df523abSPeter Krystad 		ret = true;
559*3df523abSPeter Krystad 	}
5606d0060f6SMat Martineau 
5616d0060f6SMat Martineau 	return ret;
5626d0060f6SMat Martineau }
5636d0060f6SMat Martineau 
564cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
565cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
566cec37a6eSPeter Krystad {
567cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
568cec37a6eSPeter Krystad 
569cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
570cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
571cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
572cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
573cec37a6eSPeter Krystad 		pr_debug("subflow_req=%p, local_key=%llu",
574cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
575cec37a6eSPeter Krystad 		return true;
576cec37a6eSPeter Krystad 	}
577cec37a6eSPeter Krystad 	return false;
578cec37a6eSPeter Krystad }
579cec37a6eSPeter Krystad 
5800be534f5SPaolo Abeni static bool check_fully_established(struct mptcp_subflow_context *subflow,
581d22f4988SChristoph Paasch 				    struct sk_buff *skb,
582d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
583d22f4988SChristoph Paasch {
584d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
585d22f4988SChristoph Paasch 	 * are relevant
586d22f4988SChristoph Paasch 	 */
5870be534f5SPaolo Abeni 	if (likely(subflow->fully_established ||
588d22f4988SChristoph Paasch 		   TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1))
589d22f4988SChristoph Paasch 		return true;
590d22f4988SChristoph Paasch 
591d22f4988SChristoph Paasch 	if (mp_opt->use_ack)
5920be534f5SPaolo Abeni 		subflow->fully_established = 1;
593d22f4988SChristoph Paasch 
594d22f4988SChristoph Paasch 	if (subflow->can_ack)
595d22f4988SChristoph Paasch 		return true;
596d22f4988SChristoph Paasch 
597d22f4988SChristoph Paasch 	/* If the first established packet does not contain MP_CAPABLE + data
598d22f4988SChristoph Paasch 	 * then fallback to TCP
599d22f4988SChristoph Paasch 	 */
600d22f4988SChristoph Paasch 	if (!mp_opt->mp_capable) {
601d22f4988SChristoph Paasch 		subflow->mp_capable = 0;
602d22f4988SChristoph Paasch 		tcp_sk(mptcp_subflow_tcp_sock(subflow))->is_mptcp = 0;
603d22f4988SChristoph Paasch 		return false;
604d22f4988SChristoph Paasch 	}
605d22f4988SChristoph Paasch 	subflow->remote_key = mp_opt->sndr_key;
606d22f4988SChristoph Paasch 	subflow->can_ack = 1;
607d22f4988SChristoph Paasch 	return true;
608d22f4988SChristoph Paasch }
609d22f4988SChristoph Paasch 
610648ef4b8SMat Martineau void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
611648ef4b8SMat Martineau 			    struct tcp_options_received *opt_rx)
612648ef4b8SMat Martineau {
613d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
614648ef4b8SMat Martineau 	struct mptcp_options_received *mp_opt;
615648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
616648ef4b8SMat Martineau 
617648ef4b8SMat Martineau 	mp_opt = &opt_rx->mptcp;
6180be534f5SPaolo Abeni 	if (!check_fully_established(subflow, skb, mp_opt))
619d22f4988SChristoph Paasch 		return;
620648ef4b8SMat Martineau 
621648ef4b8SMat Martineau 	if (!mp_opt->dss)
622648ef4b8SMat Martineau 		return;
623648ef4b8SMat Martineau 
624648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
625648ef4b8SMat Martineau 	if (!mpext)
626648ef4b8SMat Martineau 		return;
627648ef4b8SMat Martineau 
628648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
629648ef4b8SMat Martineau 
630648ef4b8SMat Martineau 	if (mp_opt->use_map) {
631cc7972eaSChristoph Paasch 		if (mp_opt->mpc_map) {
632cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
633cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
634cc7972eaSChristoph Paasch 			 */
635cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
636cc7972eaSChristoph Paasch 					     &mpext->data_seq);
637cc7972eaSChristoph Paasch 			mpext->data_seq++;
638cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
639cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
640cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
641cc7972eaSChristoph Paasch 		} else {
642648ef4b8SMat Martineau 			mpext->data_seq = mp_opt->data_seq;
643648ef4b8SMat Martineau 			mpext->subflow_seq = mp_opt->subflow_seq;
644cc7972eaSChristoph Paasch 			mpext->dsn64 = mp_opt->dsn64;
645cc7972eaSChristoph Paasch 		}
646648ef4b8SMat Martineau 		mpext->data_len = mp_opt->data_len;
647648ef4b8SMat Martineau 		mpext->use_map = 1;
648648ef4b8SMat Martineau 	}
649648ef4b8SMat Martineau 
650648ef4b8SMat Martineau 	if (mp_opt->use_ack) {
651648ef4b8SMat Martineau 		mpext->data_ack = mp_opt->data_ack;
652648ef4b8SMat Martineau 		mpext->use_ack = 1;
653648ef4b8SMat Martineau 		mpext->ack64 = mp_opt->ack64;
654648ef4b8SMat Martineau 	}
655648ef4b8SMat Martineau 
656648ef4b8SMat Martineau 	mpext->data_fin = mp_opt->data_fin;
657648ef4b8SMat Martineau }
658648ef4b8SMat Martineau 
659eda7acddSPeter Krystad void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts)
660eda7acddSPeter Krystad {
661cc7972eaSChristoph Paasch 	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
662eda7acddSPeter Krystad 	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
663eda7acddSPeter Krystad 		u8 len;
664eda7acddSPeter Krystad 
665eda7acddSPeter Krystad 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
666eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
667cec37a6eSPeter Krystad 		else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
668cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
669cc7972eaSChristoph Paasch 		else if (opts->ext_copy.data_len)
670cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
671eda7acddSPeter Krystad 		else
672eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
673eda7acddSPeter Krystad 
674*3df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
675*3df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
67665492c5aSPaolo Abeni 				      MPTCP_CAP_HMAC_SHA256);
677cc7972eaSChristoph Paasch 
678cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
679cc7972eaSChristoph Paasch 		    opts->suboptions))
680cc7972eaSChristoph Paasch 			goto mp_capable_done;
681cc7972eaSChristoph Paasch 
682eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
683eda7acddSPeter Krystad 		ptr += 2;
684cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
685cc7972eaSChristoph Paasch 			goto mp_capable_done;
686cc7972eaSChristoph Paasch 
687eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
688eda7acddSPeter Krystad 		ptr += 2;
689cc7972eaSChristoph Paasch 		if (!opts->ext_copy.data_len)
690cc7972eaSChristoph Paasch 			goto mp_capable_done;
691cc7972eaSChristoph Paasch 
692cc7972eaSChristoph Paasch 		put_unaligned_be32(opts->ext_copy.data_len << 16 |
693cc7972eaSChristoph Paasch 				   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
694cc7972eaSChristoph Paasch 		ptr += 1;
695eda7acddSPeter Krystad 	}
6966d0060f6SMat Martineau 
697cc7972eaSChristoph Paasch mp_capable_done:
698*3df523abSPeter Krystad 	if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
699*3df523abSPeter Krystad 		if (opts->ahmac)
700*3df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
701*3df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR, 0,
702*3df523abSPeter Krystad 					      opts->addr_id);
703*3df523abSPeter Krystad 		else
704*3df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
705*3df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR_BASE,
706*3df523abSPeter Krystad 					      MPTCP_ADDR_ECHO,
707*3df523abSPeter Krystad 					      opts->addr_id);
708*3df523abSPeter Krystad 		memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4);
709*3df523abSPeter Krystad 		ptr += 1;
710*3df523abSPeter Krystad 		if (opts->ahmac) {
711*3df523abSPeter Krystad 			put_unaligned_be64(opts->ahmac, ptr);
712*3df523abSPeter Krystad 			ptr += 2;
713*3df523abSPeter Krystad 		}
714*3df523abSPeter Krystad 	}
715*3df523abSPeter Krystad 
716*3df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
717*3df523abSPeter Krystad 	if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) {
718*3df523abSPeter Krystad 		if (opts->ahmac)
719*3df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
720*3df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR6, 0,
721*3df523abSPeter Krystad 					      opts->addr_id);
722*3df523abSPeter Krystad 		else
723*3df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
724*3df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR6_BASE,
725*3df523abSPeter Krystad 					      MPTCP_ADDR_ECHO,
726*3df523abSPeter Krystad 					      opts->addr_id);
727*3df523abSPeter Krystad 		memcpy((u8 *)ptr, opts->addr6.s6_addr, 16);
728*3df523abSPeter Krystad 		ptr += 4;
729*3df523abSPeter Krystad 		if (opts->ahmac) {
730*3df523abSPeter Krystad 			put_unaligned_be64(opts->ahmac, ptr);
731*3df523abSPeter Krystad 			ptr += 2;
732*3df523abSPeter Krystad 		}
733*3df523abSPeter Krystad 	}
734*3df523abSPeter Krystad #endif
735*3df523abSPeter Krystad 
736*3df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
737*3df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
738*3df523abSPeter Krystad 				      TCPOLEN_MPTCP_RM_ADDR_BASE,
739*3df523abSPeter Krystad 				      0, opts->rm_id);
740*3df523abSPeter Krystad 	}
741*3df523abSPeter Krystad 
7426d0060f6SMat Martineau 	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
7436d0060f6SMat Martineau 		struct mptcp_ext *mpext = &opts->ext_copy;
7446d0060f6SMat Martineau 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
7456d0060f6SMat Martineau 		u8 flags = 0;
7466d0060f6SMat Martineau 
7476d0060f6SMat Martineau 		if (mpext->use_ack) {
7486d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_ACK64;
7496d0060f6SMat Martineau 			flags = MPTCP_DSS_HAS_ACK | MPTCP_DSS_ACK64;
7506d0060f6SMat Martineau 		}
7516d0060f6SMat Martineau 
7526d0060f6SMat Martineau 		if (mpext->use_map) {
7536d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_MAP64;
7546d0060f6SMat Martineau 
7556d0060f6SMat Martineau 			/* Use only 64-bit mapping flags for now, add
7566d0060f6SMat Martineau 			 * support for optional 32-bit mappings later.
7576d0060f6SMat Martineau 			 */
7586d0060f6SMat Martineau 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
7596d0060f6SMat Martineau 			if (mpext->data_fin)
7606d0060f6SMat Martineau 				flags |= MPTCP_DSS_DATA_FIN;
7616d0060f6SMat Martineau 		}
7626d0060f6SMat Martineau 
763*3df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
7646d0060f6SMat Martineau 
7656d0060f6SMat Martineau 		if (mpext->use_ack) {
7666d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_ack, ptr);
7676d0060f6SMat Martineau 			ptr += 2;
7686d0060f6SMat Martineau 		}
7696d0060f6SMat Martineau 
7706d0060f6SMat Martineau 		if (mpext->use_map) {
7716d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_seq, ptr);
7726d0060f6SMat Martineau 			ptr += 2;
7736d0060f6SMat Martineau 			put_unaligned_be32(mpext->subflow_seq, ptr);
7746d0060f6SMat Martineau 			ptr += 1;
7756d0060f6SMat Martineau 			put_unaligned_be32(mpext->data_len << 16 |
7766d0060f6SMat Martineau 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
7776d0060f6SMat Martineau 		}
7786d0060f6SMat Martineau 	}
779eda7acddSPeter Krystad }
780