xref: /openbmc/linux/net/mptcp/options.c (revision 067065422fcd625492efb7ba130adb8ac1bd8078)
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>
10a24d22b2SEric Biggers #include <crypto/sha2.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++;
2453df523abSPeter Krystad 		if (mp_opt->family == MPTCP_ADDR_IPVERSION_4) {
2463df523abSPeter Krystad 			memcpy((u8 *)&mp_opt->addr.s_addr, (u8 *)ptr, 4);
2473df523abSPeter Krystad 			ptr += 4;
2483df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
2493df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
2503df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
2513df523abSPeter Krystad 				ptr += 2;
2523df523abSPeter Krystad 			}
2533df523abSPeter Krystad 		}
2543df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2553df523abSPeter Krystad 		else {
2563df523abSPeter Krystad 			memcpy(mp_opt->addr6.s6_addr, (u8 *)ptr, 16);
2573df523abSPeter Krystad 			ptr += 16;
2583df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
2593df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
2603df523abSPeter Krystad 				mp_opt->port = get_unaligned_be16(ptr);
2613df523abSPeter Krystad 				ptr += 2;
2623df523abSPeter Krystad 			}
2633df523abSPeter Krystad 		}
2643df523abSPeter Krystad #endif
2653df523abSPeter Krystad 		if (!mp_opt->echo) {
2663df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
2673df523abSPeter Krystad 			ptr += 8;
2683df523abSPeter Krystad 		}
26990a4aea8SGeliang Tang 		pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
27090a4aea8SGeliang Tang 			 (mp_opt->family == MPTCP_ADDR_IPVERSION_6) ? "6" : "",
27190a4aea8SGeliang Tang 			 mp_opt->addr_id, mp_opt->ahmac, mp_opt->echo, mp_opt->port);
2723df523abSPeter Krystad 		break;
2733df523abSPeter Krystad 
2743df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
2753df523abSPeter Krystad 		if (opsize != TCPOLEN_MPTCP_RM_ADDR_BASE)
2763df523abSPeter Krystad 			break;
2773df523abSPeter Krystad 
2788e60eed6SGeliang Tang 		ptr++;
2798e60eed6SGeliang Tang 
2803df523abSPeter Krystad 		mp_opt->rm_addr = 1;
2813df523abSPeter Krystad 		mp_opt->rm_id = *ptr++;
2823df523abSPeter Krystad 		pr_debug("RM_ADDR: id=%d", mp_opt->rm_id);
2833df523abSPeter Krystad 		break;
2843df523abSPeter Krystad 
28550c504a2SFlorian Westphal 	case MPTCPOPT_MP_FASTCLOSE:
28650c504a2SFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
28750c504a2SFlorian Westphal 			break;
28850c504a2SFlorian Westphal 
28950c504a2SFlorian Westphal 		ptr += 2;
29050c504a2SFlorian Westphal 		mp_opt->rcvr_key = get_unaligned_be64(ptr);
29150c504a2SFlorian Westphal 		ptr += 8;
29250c504a2SFlorian Westphal 		mp_opt->fastclose = 1;
29350c504a2SFlorian Westphal 		break;
29450c504a2SFlorian Westphal 
295eda7acddSPeter Krystad 	default:
296eda7acddSPeter Krystad 		break;
297eda7acddSPeter Krystad 	}
298eda7acddSPeter Krystad }
299eda7acddSPeter Krystad 
300cec37a6eSPeter Krystad void mptcp_get_options(const struct sk_buff *skb,
301cfde141eSPaolo Abeni 		       struct mptcp_options_received *mp_opt)
302cec37a6eSPeter Krystad {
303cec37a6eSPeter Krystad 	const struct tcphdr *th = tcp_hdr(skb);
304cfde141eSPaolo Abeni 	const unsigned char *ptr;
305cfde141eSPaolo Abeni 	int length;
306cec37a6eSPeter Krystad 
307cfde141eSPaolo Abeni 	/* initialize option status */
308cfde141eSPaolo Abeni 	mp_opt->mp_capable = 0;
309cfde141eSPaolo Abeni 	mp_opt->mp_join = 0;
310cfde141eSPaolo Abeni 	mp_opt->add_addr = 0;
311fe2d9b1aSGeliang Tang 	mp_opt->ahmac = 0;
31250c504a2SFlorian Westphal 	mp_opt->fastclose = 0;
31365b8c8a6SGeliang Tang 	mp_opt->port = 0;
314cfde141eSPaolo Abeni 	mp_opt->rm_addr = 0;
315cfde141eSPaolo Abeni 	mp_opt->dss = 0;
316cfde141eSPaolo Abeni 
317cfde141eSPaolo Abeni 	length = (th->doff * 4) - sizeof(struct tcphdr);
318cec37a6eSPeter Krystad 	ptr = (const unsigned char *)(th + 1);
319cec37a6eSPeter Krystad 
320cec37a6eSPeter Krystad 	while (length > 0) {
321cec37a6eSPeter Krystad 		int opcode = *ptr++;
322cec37a6eSPeter Krystad 		int opsize;
323cec37a6eSPeter Krystad 
324cec37a6eSPeter Krystad 		switch (opcode) {
325cec37a6eSPeter Krystad 		case TCPOPT_EOL:
326cec37a6eSPeter Krystad 			return;
327cec37a6eSPeter Krystad 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
328cec37a6eSPeter Krystad 			length--;
329cec37a6eSPeter Krystad 			continue;
330cec37a6eSPeter Krystad 		default:
331cec37a6eSPeter Krystad 			opsize = *ptr++;
332cec37a6eSPeter Krystad 			if (opsize < 2) /* "silly options" */
333cec37a6eSPeter Krystad 				return;
334cec37a6eSPeter Krystad 			if (opsize > length)
335cec37a6eSPeter Krystad 				return;	/* don't parse partial options */
336cec37a6eSPeter Krystad 			if (opcode == TCPOPT_MPTCP)
337cfde141eSPaolo Abeni 				mptcp_parse_option(skb, ptr, opsize, mp_opt);
338cec37a6eSPeter Krystad 			ptr += opsize - 2;
339cec37a6eSPeter Krystad 			length -= opsize;
340cec37a6eSPeter Krystad 		}
341cec37a6eSPeter Krystad 	}
342cec37a6eSPeter Krystad }
343cec37a6eSPeter Krystad 
344cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
345cc7972eaSChristoph Paasch 		       unsigned int *size, struct mptcp_out_options *opts)
346cec37a6eSPeter Krystad {
347cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
348cec37a6eSPeter Krystad 
349cc7972eaSChristoph Paasch 	/* we will use snd_isn to detect first pkt [re]transmission
350cc7972eaSChristoph Paasch 	 * in mptcp_established_options_mp()
351cc7972eaSChristoph Paasch 	 */
352cc7972eaSChristoph Paasch 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
353cec37a6eSPeter Krystad 	if (subflow->request_mptcp) {
354cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
355cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYN;
356cec37a6eSPeter Krystad 		return true;
357ec3edaa7SPeter Krystad 	} else if (subflow->request_join) {
358ec3edaa7SPeter Krystad 		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
359ec3edaa7SPeter Krystad 			 subflow->local_nonce);
360ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
361ec3edaa7SPeter Krystad 		opts->join_id = subflow->local_id;
362ec3edaa7SPeter Krystad 		opts->token = subflow->remote_token;
363ec3edaa7SPeter Krystad 		opts->nonce = subflow->local_nonce;
364ec3edaa7SPeter Krystad 		opts->backup = subflow->request_bkup;
365ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYN;
366ec3edaa7SPeter Krystad 		return true;
367cec37a6eSPeter Krystad 	}
368cec37a6eSPeter Krystad 	return false;
369cec37a6eSPeter Krystad }
370cec37a6eSPeter Krystad 
371ec3edaa7SPeter Krystad /* MP_JOIN client subflow must wait for 4th ack before sending any data:
372ec3edaa7SPeter Krystad  * TCP can't schedule delack timer before the subflow is fully established.
373ec3edaa7SPeter Krystad  * MPTCP uses the delack timer to do 3rd ack retransmissions
374ec3edaa7SPeter Krystad  */
375ec3edaa7SPeter Krystad static void schedule_3rdack_retransmission(struct sock *sk)
376ec3edaa7SPeter Krystad {
377ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
378ec3edaa7SPeter Krystad 	struct tcp_sock *tp = tcp_sk(sk);
379ec3edaa7SPeter Krystad 	unsigned long timeout;
380ec3edaa7SPeter Krystad 
381ec3edaa7SPeter Krystad 	/* reschedule with a timeout above RTT, as we must look only for drop */
382ec3edaa7SPeter Krystad 	if (tp->srtt_us)
383ec3edaa7SPeter Krystad 		timeout = tp->srtt_us << 1;
384ec3edaa7SPeter Krystad 	else
385ec3edaa7SPeter Krystad 		timeout = TCP_TIMEOUT_INIT;
386ec3edaa7SPeter Krystad 
387ec3edaa7SPeter Krystad 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
388ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
389ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = timeout;
390ec3edaa7SPeter Krystad 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
391ec3edaa7SPeter Krystad }
392ec3edaa7SPeter Krystad 
393ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk)
394ec3edaa7SPeter Krystad {
395ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
396ec3edaa7SPeter Krystad 
397ec3edaa7SPeter Krystad 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
398ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = 0;
399ec3edaa7SPeter Krystad 	icsk->icsk_ack.ato = 0;
400ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
401ec3edaa7SPeter Krystad }
402ec3edaa7SPeter Krystad 
403cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
404cc7972eaSChristoph Paasch 					 unsigned int *size,
4056d0060f6SMat Martineau 					 unsigned int remaining,
406cec37a6eSPeter Krystad 					 struct mptcp_out_options *opts)
407cec37a6eSPeter Krystad {
408cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
409cc7972eaSChristoph Paasch 	struct mptcp_ext *mpext;
410cc7972eaSChristoph Paasch 	unsigned int data_len;
411cec37a6eSPeter Krystad 
412ec3edaa7SPeter Krystad 	/* When skb is not available, we better over-estimate the emitted
413ec3edaa7SPeter Krystad 	 * options len. A full DSS option (28 bytes) is longer than
414ec3edaa7SPeter Krystad 	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
415ec3edaa7SPeter Krystad 	 * tell the caller to defer the estimate to
416ec3edaa7SPeter Krystad 	 * mptcp_established_options_dss(), which will reserve enough space.
417cc7972eaSChristoph Paasch 	 */
418ec3edaa7SPeter Krystad 	if (!skb)
419ec3edaa7SPeter Krystad 		return false;
420ec3edaa7SPeter Krystad 
421ec3edaa7SPeter Krystad 	/* MPC/MPJ needed only on 3rd ack packet */
422ec3edaa7SPeter Krystad 	if (subflow->fully_established ||
423ec3edaa7SPeter Krystad 	    subflow->snd_isn != TCP_SKB_CB(skb)->seq)
424ec3edaa7SPeter Krystad 		return false;
425ec3edaa7SPeter Krystad 
426ec3edaa7SPeter Krystad 	if (subflow->mp_capable) {
427cc7972eaSChristoph Paasch 		mpext = mptcp_get_ext(skb);
428cc7972eaSChristoph Paasch 		data_len = mpext ? mpext->data_len : 0;
429cc7972eaSChristoph Paasch 
430cc7972eaSChristoph Paasch 		/* we will check ext_copy.data_len in mptcp_write_options() to
431cc7972eaSChristoph Paasch 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
432cc7972eaSChristoph Paasch 		 * TCPOLEN_MPTCP_MPC_ACK
433cc7972eaSChristoph Paasch 		 */
434cc7972eaSChristoph Paasch 		opts->ext_copy.data_len = data_len;
435cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
436cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
437cec37a6eSPeter Krystad 		opts->rcvr_key = subflow->remote_key;
438cc7972eaSChristoph Paasch 
439cc7972eaSChristoph Paasch 		/* Section 3.1.
440cc7972eaSChristoph Paasch 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
441cc7972eaSChristoph Paasch 		 * packets that start the first subflow of an MPTCP connection,
442cc7972eaSChristoph Paasch 		 * as well as the first packet that carries data
443cc7972eaSChristoph Paasch 		 */
444cc7972eaSChristoph Paasch 		if (data_len > 0)
445cc7972eaSChristoph Paasch 			*size = ALIGN(TCPOLEN_MPTCP_MPC_ACK_DATA, 4);
446cc7972eaSChristoph Paasch 		else
447cec37a6eSPeter Krystad 			*size = TCPOLEN_MPTCP_MPC_ACK;
448cc7972eaSChristoph Paasch 
449cc7972eaSChristoph Paasch 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
450cc7972eaSChristoph Paasch 			 subflow, subflow->local_key, subflow->remote_key,
451cc7972eaSChristoph Paasch 			 data_len);
452cc7972eaSChristoph Paasch 
453cec37a6eSPeter Krystad 		return true;
454ec3edaa7SPeter Krystad 	} else if (subflow->mp_join) {
455ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
456ec3edaa7SPeter Krystad 		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
457ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_ACK;
458ec3edaa7SPeter Krystad 		pr_debug("subflow=%p", subflow);
459ec3edaa7SPeter Krystad 
460ec3edaa7SPeter Krystad 		schedule_3rdack_retransmission(sk);
461ec3edaa7SPeter Krystad 		return true;
462cec37a6eSPeter Krystad 	}
463cec37a6eSPeter Krystad 	return false;
464cec37a6eSPeter Krystad }
465cec37a6eSPeter Krystad 
4666d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
4679c29e361SPaolo Abeni 				 struct sk_buff *skb, struct mptcp_ext *ext)
4686d0060f6SMat Martineau {
469017512a0SPaolo Abeni 	/* The write_seq value has already been incremented, so the actual
470017512a0SPaolo Abeni 	 * sequence number for the DATA_FIN is one less.
471017512a0SPaolo Abeni 	 */
472017512a0SPaolo Abeni 	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
4737279da61SMat Martineau 
4749c29e361SPaolo Abeni 	if (!ext->use_map || !skb->len) {
4756d0060f6SMat Martineau 		/* RFC6824 requires a DSS mapping with specific values
4766d0060f6SMat Martineau 		 * if DATA_FIN is set but no data payload is mapped
4776d0060f6SMat Martineau 		 */
4786d37a0b8SMat Martineau 		ext->data_fin = 1;
4796d0060f6SMat Martineau 		ext->use_map = 1;
4806d0060f6SMat Martineau 		ext->dsn64 = 1;
481017512a0SPaolo Abeni 		ext->data_seq = data_fin_tx_seq;
4826d0060f6SMat Martineau 		ext->subflow_seq = 0;
4836d0060f6SMat Martineau 		ext->data_len = 1;
4847279da61SMat Martineau 	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
4856d37a0b8SMat Martineau 		/* If there's an existing DSS mapping and it is the
4866d37a0b8SMat Martineau 		 * final mapping, DATA_FIN consumes 1 additional byte of
4876d37a0b8SMat Martineau 		 * mapping space.
4886d0060f6SMat Martineau 		 */
4896d37a0b8SMat Martineau 		ext->data_fin = 1;
4906d0060f6SMat Martineau 		ext->data_len++;
4916d0060f6SMat Martineau 	}
4926d0060f6SMat Martineau }
4936d0060f6SMat Martineau 
4946d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
4956d0060f6SMat Martineau 					  unsigned int *size,
4966d0060f6SMat Martineau 					  unsigned int remaining,
4976d0060f6SMat Martineau 					  struct mptcp_out_options *opts)
4986d0060f6SMat Martineau {
4996d0060f6SMat Martineau 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
5007279da61SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
5016d0060f6SMat Martineau 	unsigned int dss_size = 0;
5027279da61SMat Martineau 	u64 snd_data_fin_enable;
5036d0060f6SMat Martineau 	struct mptcp_ext *mpext;
5046d0060f6SMat Martineau 	unsigned int ack_size;
505d22f4988SChristoph Paasch 	bool ret = false;
5066d0060f6SMat Martineau 
5070bac966aSMat Martineau 	mpext = skb ? mptcp_get_ext(skb) : NULL;
508e16163b6SPaolo Abeni 	snd_data_fin_enable = mptcp_data_fin_enabled(msk);
5096d0060f6SMat Martineau 
5107279da61SMat Martineau 	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
5116d0060f6SMat Martineau 		unsigned int map_size;
5126d0060f6SMat Martineau 
5136d0060f6SMat Martineau 		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
5146d0060f6SMat Martineau 
5156d0060f6SMat Martineau 		remaining -= map_size;
5166d0060f6SMat Martineau 		dss_size = map_size;
5176d0060f6SMat Martineau 		if (mpext)
5186d0060f6SMat Martineau 			opts->ext_copy = *mpext;
5196d0060f6SMat Martineau 
5207279da61SMat Martineau 		if (skb && snd_data_fin_enable)
5219c29e361SPaolo Abeni 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
522d22f4988SChristoph Paasch 		ret = true;
523d22f4988SChristoph Paasch 	}
524d22f4988SChristoph Paasch 
5252398e399SPaolo Abeni 	/* passive sockets msk will set the 'can_ack' after accept(), even
5262398e399SPaolo Abeni 	 * if the first subflow may have the already the remote key handy
5272398e399SPaolo Abeni 	 */
528d22f4988SChristoph Paasch 	opts->ext_copy.use_ack = 0;
529dc093db5SPaolo Abeni 	if (!READ_ONCE(msk->can_ack)) {
530d22f4988SChristoph Paasch 		*size = ALIGN(dss_size, 4);
531d22f4988SChristoph Paasch 		return ret;
5326d0060f6SMat Martineau 	}
5336d0060f6SMat Martineau 
53437198e93SDavide Caratti 	if (READ_ONCE(msk->use_64bit_ack)) {
5356d0060f6SMat Martineau 		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
536917944daSMat Martineau 		opts->ext_copy.data_ack = READ_ONCE(msk->ack_seq);
537a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 1;
538a0c1d0eaSChristoph Paasch 	} else {
539a0c1d0eaSChristoph Paasch 		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
540917944daSMat Martineau 		opts->ext_copy.data_ack32 = (uint32_t)READ_ONCE(msk->ack_seq);
541a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 0;
542a0c1d0eaSChristoph Paasch 	}
543a0c1d0eaSChristoph Paasch 	opts->ext_copy.use_ack = 1;
544ea4ca586SPaolo Abeni 	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
5456d0060f6SMat Martineau 
5466d0060f6SMat Martineau 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
5476d0060f6SMat Martineau 	if (dss_size == 0)
5486d0060f6SMat Martineau 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
5496d0060f6SMat Martineau 
5506d0060f6SMat Martineau 	dss_size += ack_size;
5516d0060f6SMat Martineau 
5526d0060f6SMat Martineau 	*size = ALIGN(dss_size, 4);
5536d0060f6SMat Martineau 	return true;
5546d0060f6SMat Martineau }
5556d0060f6SMat Martineau 
5563df523abSPeter Krystad static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
5573df523abSPeter Krystad 				  struct in_addr *addr)
5583df523abSPeter Krystad {
559bd697222STodd Malsbary 	u8 hmac[SHA256_DIGEST_SIZE];
5603df523abSPeter Krystad 	u8 msg[7];
5613df523abSPeter Krystad 
5623df523abSPeter Krystad 	msg[0] = addr_id;
5633df523abSPeter Krystad 	memcpy(&msg[1], &addr->s_addr, 4);
5643df523abSPeter Krystad 	msg[5] = 0;
5653df523abSPeter Krystad 	msg[6] = 0;
5663df523abSPeter Krystad 
5673df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
5683df523abSPeter Krystad 
569bd697222STodd Malsbary 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
5703df523abSPeter Krystad }
5713df523abSPeter Krystad 
5723df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
5733df523abSPeter Krystad static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
5743df523abSPeter Krystad 				   struct in6_addr *addr)
5753df523abSPeter Krystad {
576bd697222STodd Malsbary 	u8 hmac[SHA256_DIGEST_SIZE];
5773df523abSPeter Krystad 	u8 msg[19];
5783df523abSPeter Krystad 
5793df523abSPeter Krystad 	msg[0] = addr_id;
5803df523abSPeter Krystad 	memcpy(&msg[1], &addr->s6_addr, 16);
5813df523abSPeter Krystad 	msg[17] = 0;
5823df523abSPeter Krystad 	msg[18] = 0;
5833df523abSPeter Krystad 
5843df523abSPeter Krystad 	mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
5853df523abSPeter Krystad 
586bd697222STodd Malsbary 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
5873df523abSPeter Krystad }
5883df523abSPeter Krystad #endif
5893df523abSPeter Krystad 
59084dfe367SGeliang Tang static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
5913df523abSPeter Krystad 					       unsigned int *size,
5923df523abSPeter Krystad 					       unsigned int remaining,
5933df523abSPeter Krystad 					       struct mptcp_out_options *opts)
5943df523abSPeter Krystad {
5953df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
5963df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
59784dfe367SGeliang Tang 	bool drop_other_suboptions = false;
59884dfe367SGeliang Tang 	unsigned int opt_size = *size;
5991b1c7a0eSPeter Krystad 	struct mptcp_addr_info saddr;
6006a6c05a8SGeliang Tang 	bool echo;
6014a2777a8SGeliang Tang 	bool port;
6021b1c7a0eSPeter Krystad 	int len;
6033df523abSPeter Krystad 
604fbe0f87aSGeliang Tang 	if ((mptcp_pm_should_add_signal_ipv6(msk) ||
605fbe0f87aSGeliang Tang 	     mptcp_pm_should_add_signal_port(msk)) &&
60684dfe367SGeliang Tang 	    skb && skb_is_tcp_pure_ack(skb)) {
60784dfe367SGeliang Tang 		pr_debug("drop other suboptions");
60884dfe367SGeliang Tang 		opts->suboptions = 0;
6093ae32c07SGeliang Tang 		opts->ext_copy.use_ack = 0;
6103ae32c07SGeliang Tang 		opts->ext_copy.use_map = 0;
61184dfe367SGeliang Tang 		remaining += opt_size;
61284dfe367SGeliang Tang 		drop_other_suboptions = true;
61384dfe367SGeliang Tang 	}
61484dfe367SGeliang Tang 
615f643b803SGeliang Tang 	if (!mptcp_pm_should_add_signal(msk) ||
6164a2777a8SGeliang Tang 	    !(mptcp_pm_add_addr_signal(msk, remaining, &saddr, &echo, &port)))
6173df523abSPeter Krystad 		return false;
6181b1c7a0eSPeter Krystad 
6194a2777a8SGeliang Tang 	len = mptcp_add_addr_len(saddr.family, echo, port);
6201b1c7a0eSPeter Krystad 	if (remaining < len)
6211b1c7a0eSPeter Krystad 		return false;
6221b1c7a0eSPeter Krystad 
6231b1c7a0eSPeter Krystad 	*size = len;
62484dfe367SGeliang Tang 	if (drop_other_suboptions)
62584dfe367SGeliang Tang 		*size -= opt_size;
6261b1c7a0eSPeter Krystad 	opts->addr_id = saddr.id;
6274a2777a8SGeliang Tang 	if (port)
6284a2777a8SGeliang Tang 		opts->port = ntohs(saddr.port);
6291b1c7a0eSPeter Krystad 	if (saddr.family == AF_INET) {
6303df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
6311b1c7a0eSPeter Krystad 		opts->addr = saddr.addr;
6326a6c05a8SGeliang Tang 		if (!echo) {
6333df523abSPeter Krystad 			opts->ahmac = add_addr_generate_hmac(msk->local_key,
6343df523abSPeter Krystad 							     msk->remote_key,
6353df523abSPeter Krystad 							     opts->addr_id,
6363df523abSPeter Krystad 							     &opts->addr);
6373df523abSPeter Krystad 		}
6386a6c05a8SGeliang Tang 	}
6393df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
6401b1c7a0eSPeter Krystad 	else if (saddr.family == AF_INET6) {
6413df523abSPeter Krystad 		opts->suboptions |= OPTION_MPTCP_ADD_ADDR6;
6421b1c7a0eSPeter Krystad 		opts->addr6 = saddr.addr6;
6436a6c05a8SGeliang Tang 		if (!echo) {
6443df523abSPeter Krystad 			opts->ahmac = add_addr6_generate_hmac(msk->local_key,
6453df523abSPeter Krystad 							      msk->remote_key,
6463df523abSPeter Krystad 							      opts->addr_id,
6473df523abSPeter Krystad 							      &opts->addr6);
6483df523abSPeter Krystad 		}
6496a6c05a8SGeliang Tang 	}
6503df523abSPeter Krystad #endif
6514a2777a8SGeliang Tang 	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
6524a2777a8SGeliang Tang 		 opts->addr_id, opts->ahmac, echo, opts->port);
6533df523abSPeter Krystad 
6543df523abSPeter Krystad 	return true;
6553df523abSPeter Krystad }
6563df523abSPeter Krystad 
6575cb104aeSGeliang Tang static bool mptcp_established_options_rm_addr(struct sock *sk,
6585cb104aeSGeliang Tang 					      unsigned int *size,
6595cb104aeSGeliang Tang 					      unsigned int remaining,
6605cb104aeSGeliang Tang 					      struct mptcp_out_options *opts)
6615cb104aeSGeliang Tang {
6625cb104aeSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
6635cb104aeSGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
6645cb104aeSGeliang Tang 	u8 rm_id;
6655cb104aeSGeliang Tang 
6665cb104aeSGeliang Tang 	if (!mptcp_pm_should_rm_signal(msk) ||
6675cb104aeSGeliang Tang 	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_id)))
6685cb104aeSGeliang Tang 		return false;
6695cb104aeSGeliang Tang 
6705cb104aeSGeliang Tang 	if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE)
6715cb104aeSGeliang Tang 		return false;
6725cb104aeSGeliang Tang 
6735cb104aeSGeliang Tang 	*size = TCPOLEN_MPTCP_RM_ADDR_BASE;
6745cb104aeSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
6755cb104aeSGeliang Tang 	opts->rm_id = rm_id;
6765cb104aeSGeliang Tang 
6775cb104aeSGeliang Tang 	pr_debug("rm_id=%d", opts->rm_id);
6785cb104aeSGeliang Tang 
6795cb104aeSGeliang Tang 	return true;
6805cb104aeSGeliang Tang }
6815cb104aeSGeliang Tang 
682*06706542SGeliang Tang static bool mptcp_established_options_mp_prio(struct sock *sk,
683*06706542SGeliang Tang 					      unsigned int *size,
684*06706542SGeliang Tang 					      unsigned int remaining,
685*06706542SGeliang Tang 					      struct mptcp_out_options *opts)
686*06706542SGeliang Tang {
687*06706542SGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
688*06706542SGeliang Tang 
689*06706542SGeliang Tang 	if (!subflow->send_mp_prio)
690*06706542SGeliang Tang 		return false;
691*06706542SGeliang Tang 
692*06706542SGeliang Tang 	if (remaining < TCPOLEN_MPTCP_PRIO)
693*06706542SGeliang Tang 		return false;
694*06706542SGeliang Tang 
695*06706542SGeliang Tang 	*size = TCPOLEN_MPTCP_PRIO;
696*06706542SGeliang Tang 	opts->suboptions |= OPTION_MPTCP_PRIO;
697*06706542SGeliang Tang 	opts->backup = subflow->request_bkup;
698*06706542SGeliang Tang 
699*06706542SGeliang Tang 	pr_debug("prio=%d", opts->backup);
700*06706542SGeliang Tang 
701*06706542SGeliang Tang 	return true;
702*06706542SGeliang Tang }
703*06706542SGeliang Tang 
7046d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
7056d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
7066d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
7076d0060f6SMat Martineau {
7086d0060f6SMat Martineau 	unsigned int opt_size = 0;
7096d0060f6SMat Martineau 	bool ret = false;
7106d0060f6SMat Martineau 
7113df523abSPeter Krystad 	opts->suboptions = 0;
7123df523abSPeter Krystad 
713e1ff9e82SDavide Caratti 	if (unlikely(mptcp_check_fallback(sk)))
714e1ff9e82SDavide Caratti 		return false;
715e1ff9e82SDavide Caratti 
716d5824847SPaolo Abeni 	/* prevent adding of any MPTCP related options on reset packet
717d5824847SPaolo Abeni 	 * until we support MP_TCPRST/MP_FASTCLOSE
718d5824847SPaolo Abeni 	 */
719d5824847SPaolo Abeni 	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
720d5824847SPaolo Abeni 		return false;
721d5824847SPaolo Abeni 
722cc7972eaSChristoph Paasch 	if (mptcp_established_options_mp(sk, skb, &opt_size, remaining, opts))
7236d0060f6SMat Martineau 		ret = true;
7246d0060f6SMat Martineau 	else if (mptcp_established_options_dss(sk, skb, &opt_size, remaining,
7256d0060f6SMat Martineau 					       opts))
7266d0060f6SMat Martineau 		ret = true;
7276d0060f6SMat Martineau 
7286d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
7296d0060f6SMat Martineau 	 * TCP option space would be fatal
7306d0060f6SMat Martineau 	 */
7316d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
7326d0060f6SMat Martineau 		return false;
7336d0060f6SMat Martineau 
7346d0060f6SMat Martineau 	*size += opt_size;
7356d0060f6SMat Martineau 	remaining -= opt_size;
73684dfe367SGeliang Tang 	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
7373df523abSPeter Krystad 		*size += opt_size;
7383df523abSPeter Krystad 		remaining -= opt_size;
7393df523abSPeter Krystad 		ret = true;
7405cb104aeSGeliang Tang 	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
7415cb104aeSGeliang Tang 		*size += opt_size;
7425cb104aeSGeliang Tang 		remaining -= opt_size;
7435cb104aeSGeliang Tang 		ret = true;
7443df523abSPeter Krystad 	}
7456d0060f6SMat Martineau 
746*06706542SGeliang Tang 	if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
747*06706542SGeliang Tang 		*size += opt_size;
748*06706542SGeliang Tang 		remaining -= opt_size;
749*06706542SGeliang Tang 		ret = true;
750*06706542SGeliang Tang 	}
751*06706542SGeliang Tang 
7526d0060f6SMat Martineau 	return ret;
7536d0060f6SMat Martineau }
7546d0060f6SMat Martineau 
755cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
756cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
757cec37a6eSPeter Krystad {
758cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
759cec37a6eSPeter Krystad 
760cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
761cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
762cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
763cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
764cec37a6eSPeter Krystad 		pr_debug("subflow_req=%p, local_key=%llu",
765cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
766cec37a6eSPeter Krystad 		return true;
767f296234cSPeter Krystad 	} else if (subflow_req->mp_join) {
768f296234cSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
769f296234cSPeter Krystad 		opts->backup = subflow_req->backup;
770f296234cSPeter Krystad 		opts->join_id = subflow_req->local_id;
771f296234cSPeter Krystad 		opts->thmac = subflow_req->thmac;
772f296234cSPeter Krystad 		opts->nonce = subflow_req->local_nonce;
773f296234cSPeter Krystad 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
774f296234cSPeter Krystad 			 subflow_req, opts->backup, opts->join_id,
775f296234cSPeter Krystad 			 opts->thmac, opts->nonce);
776f296234cSPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
777f296234cSPeter Krystad 		return true;
778cec37a6eSPeter Krystad 	}
779cec37a6eSPeter Krystad 	return false;
780cec37a6eSPeter Krystad }
781cec37a6eSPeter Krystad 
782d5824847SPaolo Abeni static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
783f296234cSPeter Krystad 				    struct mptcp_subflow_context *subflow,
784d22f4988SChristoph Paasch 				    struct sk_buff *skb,
785d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
786d22f4988SChristoph Paasch {
787d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
788f296234cSPeter Krystad 	 * will make the subflow fully established
789d22f4988SChristoph Paasch 	 */
790f296234cSPeter Krystad 	if (likely(subflow->fully_established)) {
791f296234cSPeter Krystad 		/* on passive sockets, check for 3rd ack retransmission
792f296234cSPeter Krystad 		 * note that msk is always set by subflow_syn_recv_sock()
793f296234cSPeter Krystad 		 * for mp_join subflows
794f296234cSPeter Krystad 		 */
795f296234cSPeter Krystad 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
796f296234cSPeter Krystad 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
797f296234cSPeter Krystad 		    subflow->mp_join && mp_opt->mp_join &&
798f296234cSPeter Krystad 		    READ_ONCE(msk->pm.server_side))
799d5824847SPaolo Abeni 			tcp_send_ack(ssk);
800f296234cSPeter Krystad 		goto fully_established;
801f296234cSPeter Krystad 	}
802d22f4988SChristoph Paasch 
803d5824847SPaolo Abeni 	/* we must process OoO packets before the first subflow is fully
804d5824847SPaolo Abeni 	 * established. OoO packets are instead a protocol violation
805d5824847SPaolo Abeni 	 * for MP_JOIN subflows as the peer must not send any data
806d5824847SPaolo Abeni 	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
807f296234cSPeter Krystad 	 */
808d5824847SPaolo Abeni 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
809d5824847SPaolo Abeni 		if (subflow->mp_join)
810d5824847SPaolo Abeni 			goto reset;
811f296234cSPeter Krystad 		return subflow->mp_capable;
812d5824847SPaolo Abeni 	}
813f296234cSPeter Krystad 
8145a91e32bSPaolo Abeni 	if (mp_opt->dss && mp_opt->use_ack) {
815f296234cSPeter Krystad 		/* subflows are fully established as soon as we get any
816f296234cSPeter Krystad 		 * additional ack.
817f296234cSPeter Krystad 		 */
8180be534f5SPaolo Abeni 		subflow->fully_established = 1;
819b93df08cSPaolo Abeni 		WRITE_ONCE(msk->fully_established, true);
820f296234cSPeter Krystad 		goto fully_established;
821f296234cSPeter Krystad 	}
822d22f4988SChristoph Paasch 
82384dfe367SGeliang Tang 	if (mp_opt->add_addr) {
82484dfe367SGeliang Tang 		WRITE_ONCE(msk->fully_established, true);
82584dfe367SGeliang Tang 		return true;
82684dfe367SGeliang Tang 	}
82784dfe367SGeliang Tang 
828d22f4988SChristoph Paasch 	/* If the first established packet does not contain MP_CAPABLE + data
829d5824847SPaolo Abeni 	 * then fallback to TCP. Fallback scenarios requires a reset for
830d5824847SPaolo Abeni 	 * MP_JOIN subflows.
831d22f4988SChristoph Paasch 	 */
832d22f4988SChristoph Paasch 	if (!mp_opt->mp_capable) {
833d5824847SPaolo Abeni 		if (subflow->mp_join)
834d5824847SPaolo Abeni 			goto reset;
835d22f4988SChristoph Paasch 		subflow->mp_capable = 0;
836e1ff9e82SDavide Caratti 		pr_fallback(msk);
837e1ff9e82SDavide Caratti 		__mptcp_do_fallback(msk);
838d22f4988SChristoph Paasch 		return false;
839d22f4988SChristoph Paasch 	}
840f296234cSPeter Krystad 
841d6085fe1SPaolo Abeni 	if (unlikely(!READ_ONCE(msk->pm.server_side)))
842d6085fe1SPaolo Abeni 		pr_warn_once("bogus mpc option on established client sk");
843b93df08cSPaolo Abeni 	mptcp_subflow_fully_established(subflow, mp_opt);
844f296234cSPeter Krystad 
845f296234cSPeter Krystad fully_established:
8465b950ff4SPaolo Abeni 	/* if the subflow is not already linked into the conn_list, we can't
8475b950ff4SPaolo Abeni 	 * notify the PM: this subflow is still on the listener queue
8485b950ff4SPaolo Abeni 	 * and the PM possibly acquiring the subflow lock could race with
8495b950ff4SPaolo Abeni 	 * the listener close
8505b950ff4SPaolo Abeni 	 */
8515b950ff4SPaolo Abeni 	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
852f296234cSPeter Krystad 		return true;
853f296234cSPeter Krystad 
854f296234cSPeter Krystad 	subflow->pm_notified = 1;
855ec3edaa7SPeter Krystad 	if (subflow->mp_join) {
856d5824847SPaolo Abeni 		clear_3rdack_retransmission(ssk);
857f296234cSPeter Krystad 		mptcp_pm_subflow_established(msk, subflow);
858ec3edaa7SPeter Krystad 	} else {
859f296234cSPeter Krystad 		mptcp_pm_fully_established(msk);
860ec3edaa7SPeter Krystad 	}
861d22f4988SChristoph Paasch 	return true;
862d5824847SPaolo Abeni 
863d5824847SPaolo Abeni reset:
864d5824847SPaolo Abeni 	mptcp_subflow_reset(ssk);
865d5824847SPaolo Abeni 	return false;
866d22f4988SChristoph Paasch }
867d22f4988SChristoph Paasch 
868cc9d2566SPaolo Abeni static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
869cc9d2566SPaolo Abeni {
870cc9d2566SPaolo Abeni 	u32 old_ack32, cur_ack32;
871cc9d2566SPaolo Abeni 
872cc9d2566SPaolo Abeni 	if (use_64bit)
873cc9d2566SPaolo Abeni 		return cur_ack;
874cc9d2566SPaolo Abeni 
875cc9d2566SPaolo Abeni 	old_ack32 = (u32)old_ack;
876cc9d2566SPaolo Abeni 	cur_ack32 = (u32)cur_ack;
877cc9d2566SPaolo Abeni 	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
878cc9d2566SPaolo Abeni 	if (unlikely(before(cur_ack32, old_ack32)))
879cc9d2566SPaolo Abeni 		return cur_ack + (1LL << 32);
880cc9d2566SPaolo Abeni 	return cur_ack;
881cc9d2566SPaolo Abeni }
882cc9d2566SPaolo Abeni 
8836f8a612aSFlorian Westphal static void ack_update_msk(struct mptcp_sock *msk,
8846e628cd3SPaolo Abeni 			   struct sock *ssk,
885cc9d2566SPaolo Abeni 			   struct mptcp_options_received *mp_opt)
886cc9d2566SPaolo Abeni {
8877439d687SPaolo Abeni 	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
8886f8a612aSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
8897439d687SPaolo Abeni 	u64 old_snd_una;
8907439d687SPaolo Abeni 
8917439d687SPaolo Abeni 	mptcp_data_lock(sk);
892cc9d2566SPaolo Abeni 
893cc9d2566SPaolo Abeni 	/* avoid ack expansion on update conflict, to reduce the risk of
894cc9d2566SPaolo Abeni 	 * wrongly expanding to a future ack sequence number, which is way
895cc9d2566SPaolo Abeni 	 * more dangerous than missing an ack
896cc9d2566SPaolo Abeni 	 */
8977439d687SPaolo Abeni 	old_snd_una = msk->snd_una;
898cc9d2566SPaolo Abeni 	new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
899cc9d2566SPaolo Abeni 
900cc9d2566SPaolo Abeni 	/* ACK for data not even sent yet? Ignore. */
901eaa2ffabSPaolo Abeni 	if (after64(new_snd_una, snd_nxt))
902cc9d2566SPaolo Abeni 		new_snd_una = old_snd_una;
903cc9d2566SPaolo Abeni 
9046f8a612aSFlorian Westphal 	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
9056f8a612aSFlorian Westphal 
906219d0499SPaolo Abeni 	if (after64(new_wnd_end, msk->wnd_end))
9077439d687SPaolo Abeni 		msk->wnd_end = new_wnd_end;
908219d0499SPaolo Abeni 
909219d0499SPaolo Abeni 	/* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
910219d0499SPaolo Abeni 	if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)) &&
911219d0499SPaolo Abeni 	    sk_stream_memory_free(ssk))
912219d0499SPaolo Abeni 		__mptcp_check_push(sk, ssk);
9136f8a612aSFlorian Westphal 
9147439d687SPaolo Abeni 	if (after64(new_snd_una, old_snd_una)) {
9157439d687SPaolo Abeni 		msk->snd_una = new_snd_una;
9167439d687SPaolo Abeni 		__mptcp_data_acked(sk);
917cc9d2566SPaolo Abeni 	}
9187439d687SPaolo Abeni 	mptcp_data_unlock(sk);
919b51f9b80SPaolo Abeni }
920cc9d2566SPaolo Abeni 
9211a49b2c2SMat Martineau bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
9223721b9b6SMat Martineau {
9233721b9b6SMat Martineau 	/* Skip if DATA_FIN was already received.
9243721b9b6SMat Martineau 	 * If updating simultaneously with the recvmsg loop, values
9253721b9b6SMat Martineau 	 * should match. If they mismatch, the peer is misbehaving and
9263721b9b6SMat Martineau 	 * we will prefer the most recent information.
9273721b9b6SMat Martineau 	 */
9283721b9b6SMat Martineau 	if (READ_ONCE(msk->rcv_data_fin) || !READ_ONCE(msk->first))
9293721b9b6SMat Martineau 		return false;
9303721b9b6SMat Martineau 
9311a49b2c2SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin_seq,
9321a49b2c2SMat Martineau 		   expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
9333721b9b6SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin, 1);
9343721b9b6SMat Martineau 
9353721b9b6SMat Martineau 	return true;
9363721b9b6SMat Martineau }
9373721b9b6SMat Martineau 
9381b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk,
9391b1c7a0eSPeter Krystad 				struct mptcp_options_received *mp_opt)
9401b1c7a0eSPeter Krystad {
9411b1c7a0eSPeter Krystad 	u64 hmac = 0;
9421b1c7a0eSPeter Krystad 
9431b1c7a0eSPeter Krystad 	if (mp_opt->echo)
9441b1c7a0eSPeter Krystad 		return true;
9451b1c7a0eSPeter Krystad 
9461b1c7a0eSPeter Krystad 	if (mp_opt->family == MPTCP_ADDR_IPVERSION_4)
9471b1c7a0eSPeter Krystad 		hmac = add_addr_generate_hmac(msk->remote_key,
9481b1c7a0eSPeter Krystad 					      msk->local_key,
9491b1c7a0eSPeter Krystad 					      mp_opt->addr_id, &mp_opt->addr);
9501b1c7a0eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
9511b1c7a0eSPeter Krystad 	else
9521b1c7a0eSPeter Krystad 		hmac = add_addr6_generate_hmac(msk->remote_key,
9531b1c7a0eSPeter Krystad 					       msk->local_key,
9541b1c7a0eSPeter Krystad 					       mp_opt->addr_id, &mp_opt->addr6);
9551b1c7a0eSPeter Krystad #endif
9561b1c7a0eSPeter Krystad 
9571b1c7a0eSPeter Krystad 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
9581b1c7a0eSPeter Krystad 		 msk, (unsigned long long)hmac,
9591b1c7a0eSPeter Krystad 		 (unsigned long long)mp_opt->ahmac);
9601b1c7a0eSPeter Krystad 
9611b1c7a0eSPeter Krystad 	return hmac == mp_opt->ahmac;
9621b1c7a0eSPeter Krystad }
9631b1c7a0eSPeter Krystad 
96477d0cab9SFlorian Westphal void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
965648ef4b8SMat Martineau {
966d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
9671b1c7a0eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
968cfde141eSPaolo Abeni 	struct mptcp_options_received mp_opt;
969648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
970648ef4b8SMat Martineau 
9716e628cd3SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
9726e628cd3SPaolo Abeni 		/* Keep it simple and unconditionally trigger send data cleanup and
9736e628cd3SPaolo Abeni 		 * pending queue spooling. We will need to acquire the data lock
9746e628cd3SPaolo Abeni 		 * for more accurate checks, and once the lock is acquired, such
9756e628cd3SPaolo Abeni 		 * helpers are cheap.
9766e628cd3SPaolo Abeni 		 */
9776e628cd3SPaolo Abeni 		mptcp_data_lock(subflow->conn);
978219d0499SPaolo Abeni 		if (sk_stream_memory_free(sk))
979219d0499SPaolo Abeni 			__mptcp_check_push(subflow->conn, sk);
9806e628cd3SPaolo Abeni 		__mptcp_data_acked(subflow->conn);
9816e628cd3SPaolo Abeni 		mptcp_data_unlock(subflow->conn);
982e1ff9e82SDavide Caratti 		return;
9836e628cd3SPaolo Abeni 	}
984e1ff9e82SDavide Caratti 
985cfde141eSPaolo Abeni 	mptcp_get_options(skb, &mp_opt);
986cfde141eSPaolo Abeni 	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
987d22f4988SChristoph Paasch 		return;
988648ef4b8SMat Martineau 
98950c504a2SFlorian Westphal 	if (mp_opt.fastclose &&
99050c504a2SFlorian Westphal 	    msk->local_key == mp_opt.rcvr_key) {
99150c504a2SFlorian Westphal 		WRITE_ONCE(msk->rcv_fastclose, true);
99250c504a2SFlorian Westphal 		mptcp_schedule_work((struct sock *)msk);
99350c504a2SFlorian Westphal 	}
99450c504a2SFlorian Westphal 
995cfde141eSPaolo Abeni 	if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
9961b1c7a0eSPeter Krystad 		struct mptcp_addr_info addr;
9971b1c7a0eSPeter Krystad 
998cfde141eSPaolo Abeni 		addr.port = htons(mp_opt.port);
999cfde141eSPaolo Abeni 		addr.id = mp_opt.addr_id;
1000cfde141eSPaolo Abeni 		if (mp_opt.family == MPTCP_ADDR_IPVERSION_4) {
10011b1c7a0eSPeter Krystad 			addr.family = AF_INET;
1002cfde141eSPaolo Abeni 			addr.addr = mp_opt.addr;
10031b1c7a0eSPeter Krystad 		}
10041b1c7a0eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1005cfde141eSPaolo Abeni 		else if (mp_opt.family == MPTCP_ADDR_IPVERSION_6) {
10061b1c7a0eSPeter Krystad 			addr.family = AF_INET6;
1007cfde141eSPaolo Abeni 			addr.addr6 = mp_opt.addr6;
10081b1c7a0eSPeter Krystad 		}
10091b1c7a0eSPeter Krystad #endif
1010a877de06SGeliang Tang 		if (!mp_opt.echo) {
10111b1c7a0eSPeter Krystad 			mptcp_pm_add_addr_received(msk, &addr);
1012a877de06SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1013a877de06SGeliang Tang 		} else {
101400cfd77bSGeliang Tang 			mptcp_pm_del_add_timer(msk, &addr);
1015a877de06SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1016a877de06SGeliang Tang 		}
1017cfde141eSPaolo Abeni 		mp_opt.add_addr = 0;
10181b1c7a0eSPeter Krystad 	}
10191b1c7a0eSPeter Krystad 
1020d0876b22SGeliang Tang 	if (mp_opt.rm_addr) {
1021d0876b22SGeliang Tang 		mptcp_pm_rm_addr_received(msk, mp_opt.rm_id);
1022d0876b22SGeliang Tang 		mp_opt.rm_addr = 0;
1023d0876b22SGeliang Tang 	}
1024d0876b22SGeliang Tang 
1025cfde141eSPaolo Abeni 	if (!mp_opt.dss)
1026648ef4b8SMat Martineau 		return;
1027648ef4b8SMat Martineau 
1028cc9d2566SPaolo Abeni 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
1029cc9d2566SPaolo Abeni 	 * monodirectional flows will stuck
1030cc9d2566SPaolo Abeni 	 */
1031cfde141eSPaolo Abeni 	if (mp_opt.use_ack)
10326f8a612aSFlorian Westphal 		ack_update_msk(msk, sk, &mp_opt);
1033cc9d2566SPaolo Abeni 
103406827b34SMat Martineau 	/* Zero-data-length packets are dropped by the caller and not
103506827b34SMat Martineau 	 * propagated to the MPTCP layer, so the skb extension does not
103606827b34SMat Martineau 	 * need to be allocated or populated. DATA_FIN information, if
103706827b34SMat Martineau 	 * present, needs to be updated here before the skb is freed.
103843b54c6eSMat Martineau 	 */
103943b54c6eSMat Martineau 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
104043b54c6eSMat Martineau 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
10411a49b2c2SMat Martineau 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
104243b54c6eSMat Martineau 		    schedule_work(&msk->work))
104343b54c6eSMat Martineau 			sock_hold(subflow->conn);
104406827b34SMat Martineau 
104506827b34SMat Martineau 		return;
104643b54c6eSMat Martineau 	}
104743b54c6eSMat Martineau 
1048648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1049648ef4b8SMat Martineau 	if (!mpext)
1050648ef4b8SMat Martineau 		return;
1051648ef4b8SMat Martineau 
1052648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
1053648ef4b8SMat Martineau 
1054cfde141eSPaolo Abeni 	if (mp_opt.use_map) {
1055cfde141eSPaolo Abeni 		if (mp_opt.mpc_map) {
1056cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
1057cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
1058cc7972eaSChristoph Paasch 			 */
1059cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
1060cc7972eaSChristoph Paasch 					     &mpext->data_seq);
1061cc7972eaSChristoph Paasch 			mpext->data_seq++;
1062cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
1063cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
1064cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
1065a77895dbSPaolo Abeni 			mpext->data_fin = 0;
1066cc7972eaSChristoph Paasch 		} else {
1067cfde141eSPaolo Abeni 			mpext->data_seq = mp_opt.data_seq;
1068cfde141eSPaolo Abeni 			mpext->subflow_seq = mp_opt.subflow_seq;
1069cfde141eSPaolo Abeni 			mpext->dsn64 = mp_opt.dsn64;
1070cfde141eSPaolo Abeni 			mpext->data_fin = mp_opt.data_fin;
1071cc7972eaSChristoph Paasch 		}
1072cfde141eSPaolo Abeni 		mpext->data_len = mp_opt.data_len;
1073648ef4b8SMat Martineau 		mpext->use_map = 1;
1074648ef4b8SMat Martineau 	}
1075648ef4b8SMat Martineau }
1076648ef4b8SMat Martineau 
1077fa3fe2b1SFlorian Westphal static void mptcp_set_rwin(const struct tcp_sock *tp)
1078fa3fe2b1SFlorian Westphal {
1079fa3fe2b1SFlorian Westphal 	const struct sock *ssk = (const struct sock *)tp;
1080fa3fe2b1SFlorian Westphal 	const struct mptcp_subflow_context *subflow;
1081fa3fe2b1SFlorian Westphal 	struct mptcp_sock *msk;
1082fa3fe2b1SFlorian Westphal 	u64 ack_seq;
1083fa3fe2b1SFlorian Westphal 
1084fa3fe2b1SFlorian Westphal 	subflow = mptcp_subflow_ctx(ssk);
1085fa3fe2b1SFlorian Westphal 	msk = mptcp_sk(subflow->conn);
1086fa3fe2b1SFlorian Westphal 
1087fa3fe2b1SFlorian Westphal 	ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1088fa3fe2b1SFlorian Westphal 
1089fa3fe2b1SFlorian Westphal 	if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1090fa3fe2b1SFlorian Westphal 		WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1091fa3fe2b1SFlorian Westphal }
1092fa3fe2b1SFlorian Westphal 
1093fa3fe2b1SFlorian Westphal void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1094fa3fe2b1SFlorian Westphal 			 struct mptcp_out_options *opts)
1095eda7acddSPeter Krystad {
1096cc7972eaSChristoph Paasch 	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
1097eda7acddSPeter Krystad 	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
1098eda7acddSPeter Krystad 		u8 len;
1099eda7acddSPeter Krystad 
1100eda7acddSPeter Krystad 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions)
1101eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
1102cec37a6eSPeter Krystad 		else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions)
1103cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
1104cc7972eaSChristoph Paasch 		else if (opts->ext_copy.data_len)
1105cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1106eda7acddSPeter Krystad 		else
1107eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
1108eda7acddSPeter Krystad 
11093df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
11103df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
111165492c5aSPaolo Abeni 				      MPTCP_CAP_HMAC_SHA256);
1112cc7972eaSChristoph Paasch 
1113cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1114cc7972eaSChristoph Paasch 		    opts->suboptions))
1115cc7972eaSChristoph Paasch 			goto mp_capable_done;
1116cc7972eaSChristoph Paasch 
1117eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
1118eda7acddSPeter Krystad 		ptr += 2;
1119cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1120cc7972eaSChristoph Paasch 			goto mp_capable_done;
1121cc7972eaSChristoph Paasch 
1122eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
1123eda7acddSPeter Krystad 		ptr += 2;
1124cc7972eaSChristoph Paasch 		if (!opts->ext_copy.data_len)
1125cc7972eaSChristoph Paasch 			goto mp_capable_done;
1126cc7972eaSChristoph Paasch 
1127cc7972eaSChristoph Paasch 		put_unaligned_be32(opts->ext_copy.data_len << 16 |
1128cc7972eaSChristoph Paasch 				   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1129cc7972eaSChristoph Paasch 		ptr += 1;
1130eda7acddSPeter Krystad 	}
11316d0060f6SMat Martineau 
1132cc7972eaSChristoph Paasch mp_capable_done:
1133e1ef6832SGeliang Tang 	if ((OPTION_MPTCP_ADD_ADDR
1134e1ef6832SGeliang Tang #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1135e1ef6832SGeliang Tang 	     | OPTION_MPTCP_ADD_ADDR6
1136e1ef6832SGeliang Tang #endif
1137e1ef6832SGeliang Tang 	    ) & opts->suboptions) {
11386eb3d1e3SGeliang Tang 		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
11396eb3d1e3SGeliang Tang 		u8 echo = MPTCP_ADDR_ECHO;
11403df523abSPeter Krystad 
11413df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1142e1ef6832SGeliang Tang 		if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions)
1143e1ef6832SGeliang Tang 			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1144e1ef6832SGeliang Tang #endif
1145e1ef6832SGeliang Tang 
114622fb85ffSGeliang Tang 		if (opts->port)
114722fb85ffSGeliang Tang 			len += TCPOLEN_MPTCP_PORT_LEN;
114822fb85ffSGeliang Tang 
11496eb3d1e3SGeliang Tang 		if (opts->ahmac) {
11506eb3d1e3SGeliang Tang 			len += sizeof(opts->ahmac);
11516eb3d1e3SGeliang Tang 			echo = 0;
11526eb3d1e3SGeliang Tang 		}
11536eb3d1e3SGeliang Tang 
11543df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
11556eb3d1e3SGeliang Tang 				      len, echo, opts->addr_id);
1156e1ef6832SGeliang Tang 		if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
11573df523abSPeter Krystad 			memcpy((u8 *)ptr, (u8 *)&opts->addr.s_addr, 4);
11583df523abSPeter Krystad 			ptr += 1;
11593df523abSPeter Krystad 		}
11603df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1161e1ef6832SGeliang Tang 		else if (OPTION_MPTCP_ADD_ADDR6 & opts->suboptions) {
11623df523abSPeter Krystad 			memcpy((u8 *)ptr, opts->addr6.s6_addr, 16);
11633df523abSPeter Krystad 			ptr += 4;
1164e1ef6832SGeliang Tang 		}
1165e1ef6832SGeliang Tang #endif
116622fb85ffSGeliang Tang 
116722fb85ffSGeliang Tang 		if (!opts->port) {
11683df523abSPeter Krystad 			if (opts->ahmac) {
11693df523abSPeter Krystad 				put_unaligned_be64(opts->ahmac, ptr);
11703df523abSPeter Krystad 				ptr += 2;
11713df523abSPeter Krystad 			}
117222fb85ffSGeliang Tang 		} else {
117322fb85ffSGeliang Tang 			if (opts->ahmac) {
117422fb85ffSGeliang Tang 				u8 *bptr = (u8 *)ptr;
117522fb85ffSGeliang Tang 
117622fb85ffSGeliang Tang 				put_unaligned_be16(opts->port, bptr);
117722fb85ffSGeliang Tang 				bptr += 2;
117822fb85ffSGeliang Tang 				put_unaligned_be64(opts->ahmac, bptr);
117922fb85ffSGeliang Tang 				bptr += 8;
118022fb85ffSGeliang Tang 				put_unaligned_be16(TCPOPT_NOP << 8 |
118122fb85ffSGeliang Tang 						   TCPOPT_NOP, bptr);
118222fb85ffSGeliang Tang 
118322fb85ffSGeliang Tang 				ptr += 3;
118422fb85ffSGeliang Tang 			} else {
118522fb85ffSGeliang Tang 				put_unaligned_be32(opts->port << 16 |
118622fb85ffSGeliang Tang 						   TCPOPT_NOP << 8 |
118722fb85ffSGeliang Tang 						   TCPOPT_NOP, ptr);
118822fb85ffSGeliang Tang 				ptr += 1;
11893df523abSPeter Krystad 			}
119022fb85ffSGeliang Tang 		}
11913df523abSPeter Krystad 	}
11923df523abSPeter Krystad 
11933df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
11943df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
11953df523abSPeter Krystad 				      TCPOLEN_MPTCP_RM_ADDR_BASE,
11963df523abSPeter Krystad 				      0, opts->rm_id);
11973df523abSPeter Krystad 	}
11983df523abSPeter Krystad 
1199*06706542SGeliang Tang 	if (OPTION_MPTCP_PRIO & opts->suboptions) {
1200*06706542SGeliang Tang 		const struct sock *ssk = (const struct sock *)tp;
1201*06706542SGeliang Tang 		struct mptcp_subflow_context *subflow;
1202*06706542SGeliang Tang 
1203*06706542SGeliang Tang 		subflow = mptcp_subflow_ctx(ssk);
1204*06706542SGeliang Tang 		subflow->send_mp_prio = 0;
1205*06706542SGeliang Tang 
1206*06706542SGeliang Tang 		*ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1207*06706542SGeliang Tang 				      TCPOLEN_MPTCP_PRIO,
1208*06706542SGeliang Tang 				      opts->backup, TCPOPT_NOP);
1209*06706542SGeliang Tang 	}
1210*06706542SGeliang Tang 
1211ec3edaa7SPeter Krystad 	if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1212ec3edaa7SPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1213ec3edaa7SPeter Krystad 				      TCPOLEN_MPTCP_MPJ_SYN,
1214ec3edaa7SPeter Krystad 				      opts->backup, opts->join_id);
1215ec3edaa7SPeter Krystad 		put_unaligned_be32(opts->token, ptr);
1216ec3edaa7SPeter Krystad 		ptr += 1;
1217ec3edaa7SPeter Krystad 		put_unaligned_be32(opts->nonce, ptr);
1218ec3edaa7SPeter Krystad 		ptr += 1;
1219ec3edaa7SPeter Krystad 	}
1220ec3edaa7SPeter Krystad 
1221f296234cSPeter Krystad 	if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1222f296234cSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1223f296234cSPeter Krystad 				      TCPOLEN_MPTCP_MPJ_SYNACK,
1224f296234cSPeter Krystad 				      opts->backup, opts->join_id);
1225f296234cSPeter Krystad 		put_unaligned_be64(opts->thmac, ptr);
1226f296234cSPeter Krystad 		ptr += 2;
1227f296234cSPeter Krystad 		put_unaligned_be32(opts->nonce, ptr);
1228f296234cSPeter Krystad 		ptr += 1;
1229f296234cSPeter Krystad 	}
1230f296234cSPeter Krystad 
1231ec3edaa7SPeter Krystad 	if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1232ec3edaa7SPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1233ec3edaa7SPeter Krystad 				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1234ec3edaa7SPeter Krystad 		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1235ec3edaa7SPeter Krystad 		ptr += 5;
1236ec3edaa7SPeter Krystad 	}
1237ec3edaa7SPeter Krystad 
12386d0060f6SMat Martineau 	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
12396d0060f6SMat Martineau 		struct mptcp_ext *mpext = &opts->ext_copy;
12406d0060f6SMat Martineau 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
12416d0060f6SMat Martineau 		u8 flags = 0;
12426d0060f6SMat Martineau 
12436d0060f6SMat Martineau 		if (mpext->use_ack) {
1244a0c1d0eaSChristoph Paasch 			flags = MPTCP_DSS_HAS_ACK;
1245a0c1d0eaSChristoph Paasch 			if (mpext->ack64) {
12466d0060f6SMat Martineau 				len += TCPOLEN_MPTCP_DSS_ACK64;
1247a0c1d0eaSChristoph Paasch 				flags |= MPTCP_DSS_ACK64;
1248a0c1d0eaSChristoph Paasch 			} else {
1249a0c1d0eaSChristoph Paasch 				len += TCPOLEN_MPTCP_DSS_ACK32;
1250a0c1d0eaSChristoph Paasch 			}
12516d0060f6SMat Martineau 		}
12526d0060f6SMat Martineau 
12536d0060f6SMat Martineau 		if (mpext->use_map) {
12546d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_MAP64;
12556d0060f6SMat Martineau 
12566d0060f6SMat Martineau 			/* Use only 64-bit mapping flags for now, add
12576d0060f6SMat Martineau 			 * support for optional 32-bit mappings later.
12586d0060f6SMat Martineau 			 */
12596d0060f6SMat Martineau 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
12606d0060f6SMat Martineau 			if (mpext->data_fin)
12616d0060f6SMat Martineau 				flags |= MPTCP_DSS_DATA_FIN;
12626d0060f6SMat Martineau 		}
12636d0060f6SMat Martineau 
12643df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
12656d0060f6SMat Martineau 
12666d0060f6SMat Martineau 		if (mpext->use_ack) {
1267a0c1d0eaSChristoph Paasch 			if (mpext->ack64) {
12686d0060f6SMat Martineau 				put_unaligned_be64(mpext->data_ack, ptr);
12696d0060f6SMat Martineau 				ptr += 2;
1270a0c1d0eaSChristoph Paasch 			} else {
1271a0c1d0eaSChristoph Paasch 				put_unaligned_be32(mpext->data_ack32, ptr);
1272a0c1d0eaSChristoph Paasch 				ptr += 1;
1273a0c1d0eaSChristoph Paasch 			}
12746d0060f6SMat Martineau 		}
12756d0060f6SMat Martineau 
12766d0060f6SMat Martineau 		if (mpext->use_map) {
12776d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_seq, ptr);
12786d0060f6SMat Martineau 			ptr += 2;
12796d0060f6SMat Martineau 			put_unaligned_be32(mpext->subflow_seq, ptr);
12806d0060f6SMat Martineau 			ptr += 1;
12816d0060f6SMat Martineau 			put_unaligned_be32(mpext->data_len << 16 |
12826d0060f6SMat Martineau 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
12836d0060f6SMat Martineau 		}
12846d0060f6SMat Martineau 	}
1285fa3fe2b1SFlorian Westphal 
1286fa3fe2b1SFlorian Westphal 	if (tp)
1287fa3fe2b1SFlorian Westphal 		mptcp_set_rwin(tp);
1288eda7acddSPeter Krystad }
1289