xref: /openbmc/linux/net/mptcp/options.c (revision 1b1c7a0ef7f323f37281b134ade17baa94779787)
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 
1813df523abSPeter Krystad 	case MPTCPOPT_ADD_ADDR:
1823df523abSPeter Krystad 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
1833df523abSPeter Krystad 		if (!mp_opt->echo) {
1843df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
1853df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
1863df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
1873df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1883df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
1893df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
1903df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
1913df523abSPeter Krystad #endif
1923df523abSPeter Krystad 			else
1933df523abSPeter Krystad 				break;
1943df523abSPeter Krystad 		} else {
1953df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
1963df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
1973df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_4;
1983df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1993df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
2003df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
2013df523abSPeter Krystad 				mp_opt->family = MPTCP_ADDR_IPVERSION_6;
2023df523abSPeter Krystad #endif
2033df523abSPeter Krystad 			else
2043df523abSPeter Krystad 				break;
2053df523abSPeter Krystad 		}
2063df523abSPeter Krystad 
2073df523abSPeter Krystad 		mp_opt->add_addr = 1;
2083df523abSPeter Krystad 		mp_opt->port = 0;
2093df523abSPeter Krystad 		mp_opt->addr_id = *ptr++;
2103df523abSPeter Krystad 		pr_debug("ADD_ADDR: id=%d", mp_opt->addr_id);
2113df523abSPeter Krystad 		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
2123df523abSPeter Krystad 			memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4);
2133df523abSPeter Krystad 			ptr += 4;
2143df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
2153df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
2163df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
2173df523abSPeter Krystad 				ptr += 2;
2183df523abSPeter Krystad 			}
2193df523abSPeter Krystad 		}
2203df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2213df523abSPeter Krystad 		else {
2223df523abSPeter Krystad 			memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16);
2233df523abSPeter Krystad 			ptr += 16;
2243df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
2253df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
2263df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
2273df523abSPeter Krystad 				ptr += 2;
2283df523abSPeter Krystad 			}
2293df523abSPeter Krystad 		}
2303df523abSPeter Krystad #endif
2313df523abSPeter Krystad 		if (!mp_opt->echo) {
2323df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
2333df523abSPeter Krystad 			ptr += 8;
2343df523abSPeter Krystad 		}
2353df523abSPeter Krystad 		break;
2363df523abSPeter Krystad 
2373df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
2383df523abSPeter Krystad 		if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE)
2393df523abSPeter Krystad 			break;
2403df523abSPeter Krystad 
2413df523abSPeter Krystad 		mp_opt->rm_addr = 1;
2423df523abSPeter Krystad 		mp_opt->rm_id = *ptr++;
2433df523abSPeter Krystad 		pr_debug("RM_ADDR: id=%d", mp_opt->rm_id);
2443df523abSPeter Krystad 		break;
2453df523abSPeter 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 
4543df523abSPeter Krystad static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
4553df523abSPeter Krystad 				  struct in_addr *addr)
4563df523abSPeter Krystad {
4573df523abSPeter Krystad 	u8 hmac[MPTCP_ADDR_HMAC_LEN];
4583df523abSPeter Krystad 	u8 msg[7];
4593df523abSPeter Krystad 
4603df523abSPeter Krystad 	msg[0] = addr_id;
4613df523abSPeter Krystad 	memcpy(&msg[1], &addr->s_addr, 4);
4623df523abSPeter Krystad 	msg[5] = 0;
4633df523abSPeter Krystad 	msg[6] = 0;
4643df523abSPeter Krystad 
4653df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
4663df523abSPeter Krystad 
4673df523abSPeter Krystad 	return get_unaligned_be64(hmac);
4683df523abSPeter Krystad }
4693df523abSPeter Krystad 
4703df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
4713df523abSPeter Krystad static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
4723df523abSPeter Krystad 				   struct in6_addr *addr)
4733df523abSPeter Krystad {
4743df523abSPeter Krystad 	u8 hmac[MPTCP_ADDR_HMAC_LEN];
4753df523abSPeter Krystad 	u8 msg[19];
4763df523abSPeter Krystad 
4773df523abSPeter Krystad 	msg[0] = addr_id;
4783df523abSPeter Krystad 	memcpy(&msg[1], &addr->s6_addr, 16);
4793df523abSPeter Krystad 	msg[17] = 0;
4803df523abSPeter Krystad 	msg[18] = 0;
4813df523abSPeter Krystad 
4823df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
4833df523abSPeter Krystad 
4843df523abSPeter Krystad 	return get_unaligned_be64(hmac);
4853df523abSPeter Krystad }
4863df523abSPeter Krystad #endif
4873df523abSPeter Krystad 
4883df523abSPeter Krystad static bool mptcp_established_options_addr(struct sock *sk,
4893df523abSPeter Krystad 					   unsigned int *size,
4903df523abSPeter Krystad 					   unsigned int remaining,
4913df523abSPeter Krystad 					   struct mptcp_out_options *opts)
4923df523abSPeter Krystad {
4933df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
4943df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
495*1b1c7a0eSPeter Krystad 	struct mptcp_addr_info saddr;
496*1b1c7a0eSPeter Krystad 	int len;
4973df523abSPeter Krystad 
498*1b1c7a0eSPeter Krystad 	if (!mptcp_pm_should_signal(msk) ||
499*1b1c7a0eSPeter Krystad 	    !(mptcp_pm_addr_signal(msk, remaining, &saddr)))
5003df523abSPeter Krystad 		return false;
501*1b1c7a0eSPeter Krystad 
502*1b1c7a0eSPeter Krystad 	len = mptcp_add_addr_len(saddr.family);
503*1b1c7a0eSPeter Krystad 	if (remaining < len)
504*1b1c7a0eSPeter Krystad 		return false;
505*1b1c7a0eSPeter Krystad 
506*1b1c7a0eSPeter Krystad 	*size = len;
507*1b1c7a0eSPeter Krystad 	opts->addr_id = saddr.id;
508*1b1c7a0eSPeter Krystad 	if (saddr.family == AF_INET) {
5093df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
510*1b1c7a0eSPeter Krystad 		opts->addr = saddr.addr;
5113df523abSPeter Krystad 		opts->ahmac = add_addr_generate_hmac(msk->local_key,
5123df523abSPeter Krystad 						     msk->remote_key,
5133df523abSPeter Krystad 						     opts->addr_id,
5143df523abSPeter Krystad 						     &opts->addr);
5153df523abSPeter Krystad 	}
5163df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
517*1b1c7a0eSPeter Krystad 	else if (saddr.family == AF_INET6) {
5183df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
519*1b1c7a0eSPeter Krystad 		opts->addr6 = saddr.addr6;
5203df523abSPeter Krystad 		opts->ahmac = add_addr6_generate_hmac(msk->local_key,
5213df523abSPeter Krystad 						      msk->remote_key,
5223df523abSPeter Krystad 						      opts->addr_id,
5233df523abSPeter Krystad 						      &opts->addr6);
5243df523abSPeter Krystad 	}
5253df523abSPeter Krystad #endif
5263df523abSPeter Krystad 	pr_debug("addr_id=%d, ahmac=%llu", opts->addr_id, opts->ahmac);
5273df523abSPeter Krystad 
5283df523abSPeter Krystad 	return true;
5293df523abSPeter Krystad }
5303df523abSPeter Krystad 
5316d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
5326d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
5336d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
5346d0060f6SMat Martineau {
5356d0060f6SMat Martineau 	unsigned int opt_size = 0;
5366d0060f6SMat Martineau 	bool ret = false;
5376d0060f6SMat Martineau 
5383df523abSPeter Krystad 	opts->suboptions = 0;
5393df523abSPeter Krystad 
540cc7972eaSChristoph Paasch 	if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
5416d0060f6SMat Martineau 		ret = true;
5426d0060f6SMat Martineau 	else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
5436d0060f6SMat Martineau 					       opts))
5446d0060f6SMat Martineau 		ret = true;
5456d0060f6SMat Martineau 
5466d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
5476d0060f6SMat Martineau 	 * TCP option space would be fatal
5486d0060f6SMat Martineau 	 */
5496d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
5506d0060f6SMat Martineau 		return false;
5516d0060f6SMat Martineau 
5526d0060f6SMat Martineau 	*size += opt_size;
5536d0060f6SMat Martineau 	remaining -= opt_size;
5543df523abSPeter Krystad 	if (mptcp_established_options_addr(sk, &opt_size, remaining, opts)) {
5553df523abSPeter Krystad 		*size += opt_size;
5563df523abSPeter Krystad 		remaining -= opt_size;
5573df523abSPeter Krystad 		ret = true;
5583df523abSPeter Krystad 	}
5596d0060f6SMat Martineau 
5606d0060f6SMat Martineau 	return ret;
5616d0060f6SMat Martineau }
5626d0060f6SMat Martineau 
563cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
564cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
565cec37a6eSPeter Krystad {
566cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
567cec37a6eSPeter Krystad 
568cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
569cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
570cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
571cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
572cec37a6eSPeter Krystad 		pr_debug("subflow_req=%p, local_key=%llu",
573cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
574cec37a6eSPeter Krystad 		return true;
575cec37a6eSPeter Krystad 	}
576cec37a6eSPeter Krystad 	return false;
577cec37a6eSPeter Krystad }
578cec37a6eSPeter Krystad 
5790be534f5SPaolo Abeni static bool check_fully_established(struct mptcp_subflow_context *subflow,
580d22f4988SChristoph Paasch 				    struct sk_buff *skb,
581d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
582d22f4988SChristoph Paasch {
583d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
584d22f4988SChristoph Paasch 	 * are relevant
585d22f4988SChristoph Paasch 	 */
5860be534f5SPaolo Abeni 	if (likely(subflow->fully_established ||
587d22f4988SChristoph Paasch 		   TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1))
588d22f4988SChristoph Paasch 		return true;
589d22f4988SChristoph Paasch 
590d22f4988SChristoph Paasch 	if (mp_opt->use_ack)
5910be534f5SPaolo Abeni 		subflow->fully_established = 1;
592d22f4988SChristoph Paasch 
593d22f4988SChristoph Paasch 	if (subflow->can_ack)
594d22f4988SChristoph Paasch 		return true;
595d22f4988SChristoph Paasch 
596d22f4988SChristoph Paasch 	/* If the first established packet does not contain MP_CAPABLE + data
597d22f4988SChristoph Paasch 	 * then fallback to TCP
598d22f4988SChristoph Paasch 	 */
599d22f4988SChristoph Paasch 	if (!mp_opt->mp_capable) {
600d22f4988SChristoph Paasch 		subflow->mp_capable = 0;
601d22f4988SChristoph Paasch 		tcp_sk(mptcp_subflow_tcp_sock(subflow))->is_mptcp = 0;
602d22f4988SChristoph Paasch 		return false;
603d22f4988SChristoph Paasch 	}
604d22f4988SChristoph Paasch 	subflow->remote_key = mp_opt->sndr_key;
605d22f4988SChristoph Paasch 	subflow->can_ack = 1;
606d22f4988SChristoph Paasch 	return true;
607d22f4988SChristoph Paasch }
608d22f4988SChristoph Paasch 
609*1b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk,
610*1b1c7a0eSPeter Krystad 				struct mptcp_options_received *mp_opt)
611*1b1c7a0eSPeter Krystad {
612*1b1c7a0eSPeter Krystad 	u64 hmac = 0;
613*1b1c7a0eSPeter Krystad 
614*1b1c7a0eSPeter Krystad 	if (mp_opt->echo)
615*1b1c7a0eSPeter Krystad 		return true;
616*1b1c7a0eSPeter Krystad 
617*1b1c7a0eSPeter Krystad 	if (mp_opt->family == MPTCP_ADDR_IPVERSION_4)
618*1b1c7a0eSPeter Krystad 		hmac = add_addr_generate_hmac(msk->remote_key,
619*1b1c7a0eSPeter Krystad 					      msk->local_key,
620*1b1c7a0eSPeter Krystad 					      mp_opt->addr_id, &mp_opt->addr);
621*1b1c7a0eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
622*1b1c7a0eSPeter Krystad 	else
623*1b1c7a0eSPeter Krystad 		hmac = add_addr6_generate_hmac(msk->remote_key,
624*1b1c7a0eSPeter Krystad 					       msk->local_key,
625*1b1c7a0eSPeter Krystad 					       mp_opt->addr_id, &mp_opt->addr6);
626*1b1c7a0eSPeter Krystad #endif
627*1b1c7a0eSPeter Krystad 
628*1b1c7a0eSPeter Krystad 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
629*1b1c7a0eSPeter Krystad 		 msk, (unsigned long long)hmac,
630*1b1c7a0eSPeter Krystad 		 (unsigned long long)mp_opt->ahmac);
631*1b1c7a0eSPeter Krystad 
632*1b1c7a0eSPeter Krystad 	return hmac == mp_opt->ahmac;
633*1b1c7a0eSPeter Krystad }
634*1b1c7a0eSPeter Krystad 
635648ef4b8SMat Martineau void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
636648ef4b8SMat Martineau 			    struct tcp_options_received *opt_rx)
637648ef4b8SMat Martineau {
638d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
639*1b1c7a0eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
640648ef4b8SMat Martineau 	struct mptcp_options_received *mp_opt;
641648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
642648ef4b8SMat Martineau 
643648ef4b8SMat Martineau 	mp_opt = &opt_rx->mptcp;
6440be534f5SPaolo Abeni 	if (!check_fully_established(subflow, skb, mp_opt))
645d22f4988SChristoph Paasch 		return;
646648ef4b8SMat Martineau 
647*1b1c7a0eSPeter Krystad 	if (mp_opt->add_addr && add_addr_hmac_valid(msk, mp_opt)) {
648*1b1c7a0eSPeter Krystad 		struct mptcp_addr_info addr;
649*1b1c7a0eSPeter Krystad 
650*1b1c7a0eSPeter Krystad 		addr.port = htons(mp_opt->port);
651*1b1c7a0eSPeter Krystad 		addr.id = mp_opt->addr_id;
652*1b1c7a0eSPeter Krystad 		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
653*1b1c7a0eSPeter Krystad 			addr.family = AF_INET;
654*1b1c7a0eSPeter Krystad 			addr.addr = mp_opt->addr;
655*1b1c7a0eSPeter Krystad 		}
656*1b1c7a0eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
657*1b1c7a0eSPeter Krystad 		else if (mp_opt->family == MPTCP_ADDR_IPVERSION_6) {
658*1b1c7a0eSPeter Krystad 			addr.family = AF_INET6;
659*1b1c7a0eSPeter Krystad 			addr.addr6 = mp_opt->addr6;
660*1b1c7a0eSPeter Krystad 		}
661*1b1c7a0eSPeter Krystad #endif
662*1b1c7a0eSPeter Krystad 		if (!mp_opt->echo)
663*1b1c7a0eSPeter Krystad 			mptcp_pm_add_addr_received(msk, &addr);
664*1b1c7a0eSPeter Krystad 		mp_opt->add_addr = 0;
665*1b1c7a0eSPeter Krystad 	}
666*1b1c7a0eSPeter Krystad 
667648ef4b8SMat Martineau 	if (!mp_opt->dss)
668648ef4b8SMat Martineau 		return;
669648ef4b8SMat Martineau 
670648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
671648ef4b8SMat Martineau 	if (!mpext)
672648ef4b8SMat Martineau 		return;
673648ef4b8SMat Martineau 
674648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
675648ef4b8SMat Martineau 
676648ef4b8SMat Martineau 	if (mp_opt->use_map) {
677cc7972eaSChristoph Paasch 		if (mp_opt->mpc_map) {
678cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
679cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
680cc7972eaSChristoph Paasch 			 */
681cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
682cc7972eaSChristoph Paasch 					     &mpext->data_seq);
683cc7972eaSChristoph Paasch 			mpext->data_seq++;
684cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
685cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
686cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
687cc7972eaSChristoph Paasch 		} else {
688648ef4b8SMat Martineau 			mpext->data_seq = mp_opt->data_seq;
689648ef4b8SMat Martineau 			mpext->subflow_seq = mp_opt->subflow_seq;
690cc7972eaSChristoph Paasch 			mpext->dsn64 = mp_opt->dsn64;
691cc7972eaSChristoph Paasch 		}
692648ef4b8SMat Martineau 		mpext->data_len = mp_opt->data_len;
693648ef4b8SMat Martineau 		mpext->use_map = 1;
694648ef4b8SMat Martineau 	}
695648ef4b8SMat Martineau 
696648ef4b8SMat Martineau 	if (mp_opt->use_ack) {
697648ef4b8SMat Martineau 		mpext->data_ack = mp_opt->data_ack;
698648ef4b8SMat Martineau 		mpext->use_ack = 1;
699648ef4b8SMat Martineau 		mpext->ack64 = mp_opt->ack64;
700648ef4b8SMat Martineau 	}
701648ef4b8SMat Martineau 
702648ef4b8SMat Martineau 	mpext->data_fin = mp_opt->data_fin;
703*1b1c7a0eSPeter Krystad 
704*1b1c7a0eSPeter Krystad 	mptcp_pm_fully_established(msk);
705648ef4b8SMat Martineau }
706648ef4b8SMat Martineau 
707eda7acddSPeter Krystad void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts)
708eda7acddSPeter Krystad {
709cc7972eaSChristoph Paasch 	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
710eda7acddSPeter Krystad 	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
711eda7acddSPeter Krystad 		u8 len;
712eda7acddSPeter Krystad 
713eda7acddSPeter Krystad 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
714eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
715cec37a6eSPeter Krystad 		else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
716cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
717cc7972eaSChristoph Paasch 		else if (opts->ext_copy.data_len)
718cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
719eda7acddSPeter Krystad 		else
720eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
721eda7acddSPeter Krystad 
7223df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
7233df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
72465492c5aSPaolo Abeni 				      MPTCP_CAP_HMAC_SHA256);
725cc7972eaSChristoph Paasch 
726cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
727cc7972eaSChristoph Paasch 		    opts->suboptions))
728cc7972eaSChristoph Paasch 			goto mp_capable_done;
729cc7972eaSChristoph Paasch 
730eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
731eda7acddSPeter Krystad 		ptr += 2;
732cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
733cc7972eaSChristoph Paasch 			goto mp_capable_done;
734cc7972eaSChristoph Paasch 
735eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
736eda7acddSPeter Krystad 		ptr += 2;
737cc7972eaSChristoph Paasch 		if (!opts->ext_copy.data_len)
738cc7972eaSChristoph Paasch 			goto mp_capable_done;
739cc7972eaSChristoph Paasch 
740cc7972eaSChristoph Paasch 		put_unaligned_be32(opts->ext_copy.data_len << 16 |
741cc7972eaSChristoph Paasch 				   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
742cc7972eaSChristoph Paasch 		ptr += 1;
743eda7acddSPeter Krystad 	}
7446d0060f6SMat Martineau 
745cc7972eaSChristoph Paasch mp_capable_done:
7463df523abSPeter Krystad 	if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
7473df523abSPeter Krystad 		if (opts->ahmac)
7483df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
7493df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR, 0,
7503df523abSPeter Krystad 					      opts->addr_id);
7513df523abSPeter Krystad 		else
7523df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
7533df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR_BASE,
7543df523abSPeter Krystad 					      MPTCP_ADDR_ECHO,
7553df523abSPeter Krystad 					      opts->addr_id);
7563df523abSPeter Krystad 		memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4);
7573df523abSPeter Krystad 		ptr += 1;
7583df523abSPeter Krystad 		if (opts->ahmac) {
7593df523abSPeter Krystad 			put_unaligned_be64(opts->ahmac, ptr);
7603df523abSPeter Krystad 			ptr += 2;
7613df523abSPeter Krystad 		}
7623df523abSPeter Krystad 	}
7633df523abSPeter Krystad 
7643df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
7653df523abSPeter Krystad 	if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) {
7663df523abSPeter Krystad 		if (opts->ahmac)
7673df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
7683df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR6, 0,
7693df523abSPeter Krystad 					      opts->addr_id);
7703df523abSPeter Krystad 		else
7713df523abSPeter Krystad 			*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
7723df523abSPeter Krystad 					      TCPOLEN_MPTCP_ADD_ADDR6_BASE,
7733df523abSPeter Krystad 					      MPTCP_ADDR_ECHO,
7743df523abSPeter Krystad 					      opts->addr_id);
7753df523abSPeter Krystad 		memcpy((u8 *)ptr, opts->addr6.s6_addr, 16);
7763df523abSPeter Krystad 		ptr += 4;
7773df523abSPeter Krystad 		if (opts->ahmac) {
7783df523abSPeter Krystad 			put_unaligned_be64(opts->ahmac, ptr);
7793df523abSPeter Krystad 			ptr += 2;
7803df523abSPeter Krystad 		}
7813df523abSPeter Krystad 	}
7823df523abSPeter Krystad #endif
7833df523abSPeter Krystad 
7843df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
7853df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
7863df523abSPeter Krystad 				      TCPOLEN_MPTCP_RM_ADDR_BASE,
7873df523abSPeter Krystad 				      0, opts->rm_id);
7883df523abSPeter Krystad 	}
7893df523abSPeter Krystad 
7906d0060f6SMat Martineau 	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
7916d0060f6SMat Martineau 		struct mptcp_ext *mpext = &opts->ext_copy;
7926d0060f6SMat Martineau 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
7936d0060f6SMat Martineau 		u8 flags = 0;
7946d0060f6SMat Martineau 
7956d0060f6SMat Martineau 		if (mpext->use_ack) {
7966d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_ACK64;
7976d0060f6SMat Martineau 			flags = MPTCP_DSS_HAS_ACK | MPTCP_DSS_ACK64;
7986d0060f6SMat Martineau 		}
7996d0060f6SMat Martineau 
8006d0060f6SMat Martineau 		if (mpext->use_map) {
8016d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_MAP64;
8026d0060f6SMat Martineau 
8036d0060f6SMat Martineau 			/* Use only 64-bit mapping flags for now, add
8046d0060f6SMat Martineau 			 * support for optional 32-bit mappings later.
8056d0060f6SMat Martineau 			 */
8066d0060f6SMat Martineau 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
8076d0060f6SMat Martineau 			if (mpext->data_fin)
8086d0060f6SMat Martineau 				flags |= MPTCP_DSS_DATA_FIN;
8096d0060f6SMat Martineau 		}
8106d0060f6SMat Martineau 
8113df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
8126d0060f6SMat Martineau 
8136d0060f6SMat Martineau 		if (mpext->use_ack) {
8146d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_ack, ptr);
8156d0060f6SMat Martineau 			ptr += 2;
8166d0060f6SMat Martineau 		}
8176d0060f6SMat Martineau 
8186d0060f6SMat Martineau 		if (mpext->use_map) {
8196d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_seq, ptr);
8206d0060f6SMat Martineau 			ptr += 2;
8216d0060f6SMat Martineau 			put_unaligned_be32(mpext->subflow_seq, ptr);
8226d0060f6SMat Martineau 			ptr += 1;
8236d0060f6SMat Martineau 			put_unaligned_be32(mpext->data_len << 16 |
8246d0060f6SMat Martineau 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
8256d0060f6SMat Martineau 		}
8266d0060f6SMat Martineau 	}
827eda7acddSPeter Krystad }
828