xref: /openbmc/linux/net/mptcp/options.c (revision 13ac17a32bf1fefbc0bd412545a907979fda26b6)
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 
16ed66bfb4SGeliang Tang #include <trace/events/mptcp.h>
17ed66bfb4SGeliang Tang 
1865492c5aSPaolo Abeni static bool mptcp_cap_flag_sha256(u8 flags)
1965492c5aSPaolo Abeni {
2065492c5aSPaolo Abeni 	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
2165492c5aSPaolo Abeni }
2265492c5aSPaolo Abeni 
23cfde141eSPaolo Abeni static void mptcp_parse_option(const struct sk_buff *skb,
24cfde141eSPaolo Abeni 			       const unsigned char *ptr, int opsize,
25cfde141eSPaolo Abeni 			       struct mptcp_options_received *mp_opt)
26eda7acddSPeter Krystad {
27eda7acddSPeter Krystad 	u8 subtype = *ptr >> 4;
28648ef4b8SMat Martineau 	int expected_opsize;
29eda7acddSPeter Krystad 	u8 version;
30eda7acddSPeter Krystad 	u8 flags;
315c4a824dSGeliang Tang 	u8 i;
32eda7acddSPeter Krystad 
33eda7acddSPeter Krystad 	switch (subtype) {
34eda7acddSPeter Krystad 	case MPTCPOPT_MP_CAPABLE:
35cc7972eaSChristoph Paasch 		/* strict size checking */
36cc7972eaSChristoph Paasch 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
37cc7972eaSChristoph Paasch 			if (skb->len > tcp_hdr(skb)->doff << 2)
38cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
39cc7972eaSChristoph Paasch 			else
40cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
41cc7972eaSChristoph Paasch 		} else {
42cc7972eaSChristoph Paasch 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
43cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
44cc7972eaSChristoph Paasch 			else
45cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
46cc7972eaSChristoph Paasch 		}
47208e8f66SGeliang Tang 
48208e8f66SGeliang Tang 		/* Cfr RFC 8684 Section 3.3.0:
49208e8f66SGeliang Tang 		 * If a checksum is present but its use had
50208e8f66SGeliang Tang 		 * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
51208e8f66SGeliang Tang 		 * close the subflow with a RST, as it is not behaving as negotiated.
52208e8f66SGeliang Tang 		 * If a checksum is not present when its use has been negotiated, the
53208e8f66SGeliang Tang 		 * receiver MUST close the subflow with a RST, as it is considered
54208e8f66SGeliang Tang 		 * broken
55208e8f66SGeliang Tang 		 * We parse even option with mismatching csum presence, so that
56208e8f66SGeliang Tang 		 * later in subflow_data_ready we can trigger the reset.
57208e8f66SGeliang Tang 		 */
58208e8f66SGeliang Tang 		if (opsize != expected_opsize &&
59208e8f66SGeliang Tang 		    (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
60208e8f66SGeliang Tang 		     opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
61eda7acddSPeter Krystad 			break;
62eda7acddSPeter Krystad 
63cc7972eaSChristoph Paasch 		/* try to be gentle vs future versions on the initial syn */
64eda7acddSPeter Krystad 		version = *ptr++ & MPTCP_VERSION_MASK;
65cc7972eaSChristoph Paasch 		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
66eda7acddSPeter Krystad 			if (version != MPTCP_SUPPORTED_VERSION)
67eda7acddSPeter Krystad 				break;
68cc7972eaSChristoph Paasch 		} else if (version < MPTCP_SUPPORTED_VERSION) {
69cc7972eaSChristoph Paasch 			break;
70cc7972eaSChristoph Paasch 		}
71eda7acddSPeter Krystad 
72eda7acddSPeter Krystad 		flags = *ptr++;
7365492c5aSPaolo Abeni 		if (!mptcp_cap_flag_sha256(flags) ||
74eda7acddSPeter Krystad 		    (flags & MPTCP_CAP_EXTENSIBILITY))
75eda7acddSPeter Krystad 			break;
76eda7acddSPeter Krystad 
77eda7acddSPeter Krystad 		/* RFC 6824, Section 3.1:
78eda7acddSPeter Krystad 		 * "For the Checksum Required bit (labeled "A"), if either
79eda7acddSPeter Krystad 		 * host requires the use of checksums, checksums MUST be used.
80eda7acddSPeter Krystad 		 * In other words, the only way for checksums not to be used
81eda7acddSPeter Krystad 		 * is if both hosts in their SYNs set A=0."
82eda7acddSPeter Krystad 		 */
83eda7acddSPeter Krystad 		if (flags & MPTCP_CAP_CHECKSUM_REQD)
8474c7dfbeSPaolo Abeni 			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
85eda7acddSPeter Krystad 
86a086aebaSPaolo Abeni 		mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
87df377be3SGeliang Tang 
8874c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTIONS_MPTCP_MPC;
89cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
90eda7acddSPeter Krystad 			mp_opt->sndr_key = get_unaligned_be64(ptr);
91eda7acddSPeter Krystad 			ptr += 8;
92cc7972eaSChristoph Paasch 		}
93cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
94eda7acddSPeter Krystad 			mp_opt->rcvr_key = get_unaligned_be64(ptr);
95eda7acddSPeter Krystad 			ptr += 8;
96eda7acddSPeter Krystad 		}
97208e8f66SGeliang Tang 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
98cc7972eaSChristoph Paasch 			/* Section 3.1.:
99cc7972eaSChristoph Paasch 			 * "the data parameters in a MP_CAPABLE are semantically
100cc7972eaSChristoph Paasch 			 * equivalent to those in a DSS option and can be used
101cc7972eaSChristoph Paasch 			 * interchangeably."
102cc7972eaSChristoph Paasch 			 */
10374c7dfbeSPaolo Abeni 			mp_opt->suboptions |= OPTION_MPTCP_DSS;
104cc7972eaSChristoph Paasch 			mp_opt->use_map = 1;
105cc7972eaSChristoph Paasch 			mp_opt->mpc_map = 1;
106cc7972eaSChristoph Paasch 			mp_opt->data_len = get_unaligned_be16(ptr);
107cc7972eaSChristoph Paasch 			ptr += 2;
108cc7972eaSChristoph Paasch 		}
109208e8f66SGeliang Tang 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
110208e8f66SGeliang Tang 			mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
11174c7dfbeSPaolo Abeni 			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
112208e8f66SGeliang Tang 			ptr += 2;
113208e8f66SGeliang Tang 		}
114208e8f66SGeliang Tang 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
115cc7972eaSChristoph Paasch 			 version, flags, opsize, mp_opt->sndr_key,
116208e8f66SGeliang Tang 			 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
117eda7acddSPeter Krystad 		break;
118eda7acddSPeter Krystad 
119f296234cSPeter Krystad 	case MPTCPOPT_MP_JOIN:
12074c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTIONS_MPTCP_MPJ;
121f296234cSPeter Krystad 		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
122f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
123f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
124f296234cSPeter Krystad 			mp_opt->token = get_unaligned_be32(ptr);
125f296234cSPeter Krystad 			ptr += 4;
126f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
127f296234cSPeter Krystad 			ptr += 4;
128f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
129f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
130f296234cSPeter Krystad 				 mp_opt->token, mp_opt->nonce);
131f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
132f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
133f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
134f296234cSPeter Krystad 			mp_opt->thmac = get_unaligned_be64(ptr);
135f296234cSPeter Krystad 			ptr += 8;
136f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
137f296234cSPeter Krystad 			ptr += 4;
138f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
139f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
140f296234cSPeter Krystad 				 mp_opt->thmac, mp_opt->nonce);
141f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
142f296234cSPeter Krystad 			ptr += 2;
143f296234cSPeter Krystad 			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
144f296234cSPeter Krystad 			pr_debug("MP_JOIN hmac");
145f296234cSPeter Krystad 		} else {
14674c7dfbeSPaolo Abeni 			mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ;
147f296234cSPeter Krystad 		}
148f296234cSPeter Krystad 		break;
149f296234cSPeter Krystad 
150eda7acddSPeter Krystad 	case MPTCPOPT_DSS:
151eda7acddSPeter Krystad 		pr_debug("DSS");
152648ef4b8SMat Martineau 		ptr++;
153648ef4b8SMat Martineau 
154cc7972eaSChristoph Paasch 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
155cc7972eaSChristoph Paasch 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
156cc7972eaSChristoph Paasch 		 * map info accordingly
157cc7972eaSChristoph Paasch 		 */
158cc7972eaSChristoph Paasch 		mp_opt->mpc_map = 0;
159648ef4b8SMat Martineau 		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
160648ef4b8SMat Martineau 		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
161648ef4b8SMat Martineau 		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
162648ef4b8SMat Martineau 		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
163648ef4b8SMat Martineau 		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
164648ef4b8SMat Martineau 		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
165648ef4b8SMat Martineau 
166648ef4b8SMat Martineau 		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
167648ef4b8SMat Martineau 			 mp_opt->data_fin, mp_opt->dsn64,
168648ef4b8SMat Martineau 			 mp_opt->use_map, mp_opt->ack64,
169648ef4b8SMat Martineau 			 mp_opt->use_ack);
170648ef4b8SMat Martineau 
171648ef4b8SMat Martineau 		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
172648ef4b8SMat Martineau 
173648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
174648ef4b8SMat Martineau 			if (mp_opt->ack64)
175648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
176648ef4b8SMat Martineau 			else
177648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
178648ef4b8SMat Martineau 		}
179648ef4b8SMat Martineau 
180648ef4b8SMat Martineau 		if (mp_opt->use_map) {
181648ef4b8SMat Martineau 			if (mp_opt->dsn64)
182648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
183648ef4b8SMat Martineau 			else
184648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
185648ef4b8SMat Martineau 		}
186648ef4b8SMat Martineau 
187390b95a5SGeliang Tang 		/* Always parse any csum presence combination, we will enforce
188390b95a5SGeliang Tang 		 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
189648ef4b8SMat Martineau 		 */
190648ef4b8SMat Martineau 		if (opsize != expected_opsize &&
191648ef4b8SMat Martineau 		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
192648ef4b8SMat Martineau 			break;
193648ef4b8SMat Martineau 
19474c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_DSS;
195648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
196648ef4b8SMat Martineau 			if (mp_opt->ack64) {
197648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be64(ptr);
198648ef4b8SMat Martineau 				ptr += 8;
199648ef4b8SMat Martineau 			} else {
200648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be32(ptr);
201648ef4b8SMat Martineau 				ptr += 4;
202648ef4b8SMat Martineau 			}
203648ef4b8SMat Martineau 
204648ef4b8SMat Martineau 			pr_debug("data_ack=%llu", mp_opt->data_ack);
205648ef4b8SMat Martineau 		}
206648ef4b8SMat Martineau 
207648ef4b8SMat Martineau 		if (mp_opt->use_map) {
208648ef4b8SMat Martineau 			if (mp_opt->dsn64) {
209648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be64(ptr);
210648ef4b8SMat Martineau 				ptr += 8;
211648ef4b8SMat Martineau 			} else {
212648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be32(ptr);
213648ef4b8SMat Martineau 				ptr += 4;
214648ef4b8SMat Martineau 			}
215648ef4b8SMat Martineau 
216648ef4b8SMat Martineau 			mp_opt->subflow_seq = get_unaligned_be32(ptr);
217648ef4b8SMat Martineau 			ptr += 4;
218648ef4b8SMat Martineau 
219648ef4b8SMat Martineau 			mp_opt->data_len = get_unaligned_be16(ptr);
220648ef4b8SMat Martineau 			ptr += 2;
221648ef4b8SMat Martineau 
222390b95a5SGeliang Tang 			if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
22374c7dfbeSPaolo Abeni 				mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
224390b95a5SGeliang Tang 				mp_opt->csum = (__force __sum16)get_unaligned_be16(ptr);
225390b95a5SGeliang Tang 				ptr += 2;
226390b95a5SGeliang Tang 			}
227390b95a5SGeliang Tang 
228390b95a5SGeliang Tang 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
229648ef4b8SMat Martineau 				 mp_opt->data_seq, mp_opt->subflow_seq,
23074c7dfbeSPaolo Abeni 				 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
23174c7dfbeSPaolo Abeni 				 mp_opt->csum);
232648ef4b8SMat Martineau 		}
233648ef4b8SMat Martineau 
234eda7acddSPeter Krystad 		break;
235eda7acddSPeter Krystad 
2363df523abSPeter Krystad 	case MPTCPOPT_ADD_ADDR:
2373df523abSPeter Krystad 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
2383df523abSPeter Krystad 		if (!mp_opt->echo) {
2393df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
2403df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
2411b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET;
2423df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2433df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
2443df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
2451b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET6;
2463df523abSPeter Krystad #endif
2473df523abSPeter Krystad 			else
2483df523abSPeter Krystad 				break;
2493df523abSPeter Krystad 		} else {
2503df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
2513df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
2521b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET;
2533df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2543df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
2553df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
2561b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET6;
2573df523abSPeter Krystad #endif
2583df523abSPeter Krystad 			else
2593df523abSPeter Krystad 				break;
2603df523abSPeter Krystad 		}
2613df523abSPeter Krystad 
26274c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
263f7dafee1SGeliang Tang 		mp_opt->addr.id = *ptr++;
264a086aebaSPaolo Abeni 		mp_opt->addr.port = 0;
265a086aebaSPaolo Abeni 		mp_opt->ahmac = 0;
2661b1a6ef5SGeliang Tang 		if (mp_opt->addr.family == AF_INET) {
267f7dafee1SGeliang Tang 			memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
2683df523abSPeter Krystad 			ptr += 4;
2693df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
2703df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
271f7dafee1SGeliang Tang 				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
2723df523abSPeter Krystad 				ptr += 2;
2733df523abSPeter Krystad 			}
2743df523abSPeter Krystad 		}
2753df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2763df523abSPeter Krystad 		else {
277f7dafee1SGeliang Tang 			memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
2783df523abSPeter Krystad 			ptr += 16;
2793df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
2803df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
281f7dafee1SGeliang Tang 				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
2823df523abSPeter Krystad 				ptr += 2;
2833df523abSPeter Krystad 			}
2843df523abSPeter Krystad 		}
2853df523abSPeter Krystad #endif
2863df523abSPeter Krystad 		if (!mp_opt->echo) {
2873df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
2883df523abSPeter Krystad 			ptr += 8;
2893df523abSPeter Krystad 		}
29090a4aea8SGeliang Tang 		pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
2911b1a6ef5SGeliang Tang 			 (mp_opt->addr.family == AF_INET6) ? "6" : "",
292f7dafee1SGeliang Tang 			 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
2933df523abSPeter Krystad 		break;
2943df523abSPeter Krystad 
2953df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
2965c4a824dSGeliang Tang 		if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
2975c4a824dSGeliang Tang 		    opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
2983df523abSPeter Krystad 			break;
2993df523abSPeter Krystad 
3008e60eed6SGeliang Tang 		ptr++;
3018e60eed6SGeliang Tang 
30274c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
3035c4a824dSGeliang Tang 		mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
3045c4a824dSGeliang Tang 		for (i = 0; i < mp_opt->rm_list.nr; i++)
3055c4a824dSGeliang Tang 			mp_opt->rm_list.ids[i] = *ptr++;
3065c4a824dSGeliang Tang 		pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
3073df523abSPeter Krystad 		break;
3083df523abSPeter Krystad 
30940453a5cSGeliang Tang 	case MPTCPOPT_MP_PRIO:
31040453a5cSGeliang Tang 		if (opsize != TCPOLEN_MPTCP_PRIO)
31140453a5cSGeliang Tang 			break;
31240453a5cSGeliang Tang 
31374c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_PRIO;
31440453a5cSGeliang Tang 		mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
31540453a5cSGeliang Tang 		pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
31640453a5cSGeliang Tang 		break;
31740453a5cSGeliang Tang 
31850c504a2SFlorian Westphal 	case MPTCPOPT_MP_FASTCLOSE:
31950c504a2SFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
32050c504a2SFlorian Westphal 			break;
32150c504a2SFlorian Westphal 
32250c504a2SFlorian Westphal 		ptr += 2;
32350c504a2SFlorian Westphal 		mp_opt->rcvr_key = get_unaligned_be64(ptr);
32450c504a2SFlorian Westphal 		ptr += 8;
32574c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
32650c504a2SFlorian Westphal 		break;
32750c504a2SFlorian Westphal 
328dc87efdbSFlorian Westphal 	case MPTCPOPT_RST:
329dc87efdbSFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_RST)
330dc87efdbSFlorian Westphal 			break;
331dc87efdbSFlorian Westphal 
332dc87efdbSFlorian Westphal 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
333dc87efdbSFlorian Westphal 			break;
33474c7dfbeSPaolo Abeni 
33574c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_RST;
336dc87efdbSFlorian Westphal 		flags = *ptr++;
337dc87efdbSFlorian Westphal 		mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
338dc87efdbSFlorian Westphal 		mp_opt->reset_reason = *ptr;
339dc87efdbSFlorian Westphal 		break;
340dc87efdbSFlorian Westphal 
3415580d41bSGeliang Tang 	case MPTCPOPT_MP_FAIL:
3425580d41bSGeliang Tang 		if (opsize != TCPOLEN_MPTCP_FAIL)
3435580d41bSGeliang Tang 			break;
3445580d41bSGeliang Tang 
3455580d41bSGeliang Tang 		ptr += 2;
34674c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_FAIL;
3475580d41bSGeliang Tang 		mp_opt->fail_seq = get_unaligned_be64(ptr);
3485580d41bSGeliang Tang 		pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
3495580d41bSGeliang Tang 		break;
3505580d41bSGeliang Tang 
351eda7acddSPeter Krystad 	default:
352eda7acddSPeter Krystad 		break;
353eda7acddSPeter Krystad 	}
354eda7acddSPeter Krystad }
355eda7acddSPeter Krystad 
356c863225bSGeliang Tang void mptcp_get_options(const struct sock *sk,
357c863225bSGeliang Tang 		       const struct sk_buff *skb,
358cfde141eSPaolo Abeni 		       struct mptcp_options_received *mp_opt)
359cec37a6eSPeter Krystad {
360cec37a6eSPeter Krystad 	const struct tcphdr *th = tcp_hdr(skb);
361cfde141eSPaolo Abeni 	const unsigned char *ptr;
362cfde141eSPaolo Abeni 	int length;
363cec37a6eSPeter Krystad 
364cfde141eSPaolo Abeni 	/* initialize option status */
36574c7dfbeSPaolo Abeni 	mp_opt->suboptions = 0;
366cfde141eSPaolo Abeni 
367cfde141eSPaolo Abeni 	length = (th->doff * 4) - sizeof(struct tcphdr);
368cec37a6eSPeter Krystad 	ptr = (const unsigned char *)(th + 1);
369cec37a6eSPeter Krystad 
370cec37a6eSPeter Krystad 	while (length > 0) {
371cec37a6eSPeter Krystad 		int opcode = *ptr++;
372cec37a6eSPeter Krystad 		int opsize;
373cec37a6eSPeter Krystad 
374cec37a6eSPeter Krystad 		switch (opcode) {
375cec37a6eSPeter Krystad 		case TCPOPT_EOL:
376cec37a6eSPeter Krystad 			return;
377cec37a6eSPeter Krystad 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
378cec37a6eSPeter Krystad 			length--;
379cec37a6eSPeter Krystad 			continue;
380cec37a6eSPeter Krystad 		default:
38107718be2SMaxim Mikityanskiy 			if (length < 2)
38207718be2SMaxim Mikityanskiy 				return;
383cec37a6eSPeter Krystad 			opsize = *ptr++;
384cec37a6eSPeter Krystad 			if (opsize < 2) /* "silly options" */
385cec37a6eSPeter Krystad 				return;
386cec37a6eSPeter Krystad 			if (opsize > length)
387cec37a6eSPeter Krystad 				return;	/* don't parse partial options */
388cec37a6eSPeter Krystad 			if (opcode == TCPOPT_MPTCP)
389cfde141eSPaolo Abeni 				mptcp_parse_option(skb, ptr, opsize, mp_opt);
390cec37a6eSPeter Krystad 			ptr += opsize - 2;
391cec37a6eSPeter Krystad 			length -= opsize;
392cec37a6eSPeter Krystad 		}
393cec37a6eSPeter Krystad 	}
394cec37a6eSPeter Krystad }
395cec37a6eSPeter Krystad 
396cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
397cc7972eaSChristoph Paasch 		       unsigned int *size, struct mptcp_out_options *opts)
398cec37a6eSPeter Krystad {
399cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
400cec37a6eSPeter Krystad 
401cc7972eaSChristoph Paasch 	/* we will use snd_isn to detect first pkt [re]transmission
402cc7972eaSChristoph Paasch 	 * in mptcp_established_options_mp()
403cc7972eaSChristoph Paasch 	 */
404cc7972eaSChristoph Paasch 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
405cec37a6eSPeter Krystad 	if (subflow->request_mptcp) {
406cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
40706fe1719SGeliang Tang 		opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
408bab6b88eSGeliang Tang 		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
409cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYN;
410cec37a6eSPeter Krystad 		return true;
411ec3edaa7SPeter Krystad 	} else if (subflow->request_join) {
412ec3edaa7SPeter Krystad 		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
413ec3edaa7SPeter Krystad 			 subflow->local_nonce);
414ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
415ec3edaa7SPeter Krystad 		opts->join_id = subflow->local_id;
416ec3edaa7SPeter Krystad 		opts->token = subflow->remote_token;
417ec3edaa7SPeter Krystad 		opts->nonce = subflow->local_nonce;
418ec3edaa7SPeter Krystad 		opts->backup = subflow->request_bkup;
419ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYN;
420ec3edaa7SPeter Krystad 		return true;
421cec37a6eSPeter Krystad 	}
422cec37a6eSPeter Krystad 	return false;
423cec37a6eSPeter Krystad }
424cec37a6eSPeter Krystad 
425ec3edaa7SPeter Krystad /* MP_JOIN client subflow must wait for 4th ack before sending any data:
426ec3edaa7SPeter Krystad  * TCP can't schedule delack timer before the subflow is fully established.
427ec3edaa7SPeter Krystad  * MPTCP uses the delack timer to do 3rd ack retransmissions
428ec3edaa7SPeter Krystad  */
429ec3edaa7SPeter Krystad static void schedule_3rdack_retransmission(struct sock *sk)
430ec3edaa7SPeter Krystad {
431ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
432ec3edaa7SPeter Krystad 	struct tcp_sock *tp = tcp_sk(sk);
433ec3edaa7SPeter Krystad 	unsigned long timeout;
434ec3edaa7SPeter Krystad 
435ec3edaa7SPeter Krystad 	/* reschedule with a timeout above RTT, as we must look only for drop */
436ec3edaa7SPeter Krystad 	if (tp->srtt_us)
437ec3edaa7SPeter Krystad 		timeout = tp->srtt_us << 1;
438ec3edaa7SPeter Krystad 	else
439ec3edaa7SPeter Krystad 		timeout = TCP_TIMEOUT_INIT;
440ec3edaa7SPeter Krystad 
441ec3edaa7SPeter Krystad 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
442ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
443ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = timeout;
444ec3edaa7SPeter Krystad 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
445ec3edaa7SPeter Krystad }
446ec3edaa7SPeter Krystad 
447ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk)
448ec3edaa7SPeter Krystad {
449ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
450ec3edaa7SPeter Krystad 
451ec3edaa7SPeter Krystad 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
452ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = 0;
453ec3edaa7SPeter Krystad 	icsk->icsk_ack.ato = 0;
454ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
455ec3edaa7SPeter Krystad }
456ec3edaa7SPeter Krystad 
457cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
458d87903b6SPaolo Abeni 					 bool snd_data_fin_enable,
459cc7972eaSChristoph Paasch 					 unsigned int *size,
4606d0060f6SMat Martineau 					 unsigned int remaining,
461cec37a6eSPeter Krystad 					 struct mptcp_out_options *opts)
462cec37a6eSPeter Krystad {
463cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
46406fe1719SGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
465cc7972eaSChristoph Paasch 	struct mptcp_ext *mpext;
466cc7972eaSChristoph Paasch 	unsigned int data_len;
467c94b1f96SGeliang Tang 	u8 len;
468cec37a6eSPeter Krystad 
469ec3edaa7SPeter Krystad 	/* When skb is not available, we better over-estimate the emitted
470ec3edaa7SPeter Krystad 	 * options len. A full DSS option (28 bytes) is longer than
471ec3edaa7SPeter Krystad 	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
472ec3edaa7SPeter Krystad 	 * tell the caller to defer the estimate to
473ec3edaa7SPeter Krystad 	 * mptcp_established_options_dss(), which will reserve enough space.
474cc7972eaSChristoph Paasch 	 */
475ec3edaa7SPeter Krystad 	if (!skb)
476ec3edaa7SPeter Krystad 		return false;
477ec3edaa7SPeter Krystad 
478d87903b6SPaolo Abeni 	/* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
479d87903b6SPaolo Abeni 	if (subflow->fully_established || snd_data_fin_enable ||
480d87903b6SPaolo Abeni 	    subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
481d87903b6SPaolo Abeni 	    sk->sk_state != TCP_ESTABLISHED)
482ec3edaa7SPeter Krystad 		return false;
483ec3edaa7SPeter Krystad 
484ec3edaa7SPeter Krystad 	if (subflow->mp_capable) {
485cc7972eaSChristoph Paasch 		mpext = mptcp_get_ext(skb);
486cc7972eaSChristoph Paasch 		data_len = mpext ? mpext->data_len : 0;
487cc7972eaSChristoph Paasch 
488cc7972eaSChristoph Paasch 		/* we will check ext_copy.data_len in mptcp_write_options() to
489cc7972eaSChristoph Paasch 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
490cc7972eaSChristoph Paasch 		 * TCPOLEN_MPTCP_MPC_ACK
491cc7972eaSChristoph Paasch 		 */
492cc7972eaSChristoph Paasch 		opts->ext_copy.data_len = data_len;
493cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
494cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
495cec37a6eSPeter Krystad 		opts->rcvr_key = subflow->remote_key;
49606fe1719SGeliang Tang 		opts->csum_reqd = READ_ONCE(msk->csum_enabled);
497bab6b88eSGeliang Tang 		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
498cc7972eaSChristoph Paasch 
499cc7972eaSChristoph Paasch 		/* Section 3.1.
500cc7972eaSChristoph Paasch 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
501cc7972eaSChristoph Paasch 		 * packets that start the first subflow of an MPTCP connection,
502cc7972eaSChristoph Paasch 		 * as well as the first packet that carries data
503cc7972eaSChristoph Paasch 		 */
504c94b1f96SGeliang Tang 		if (data_len > 0) {
505c94b1f96SGeliang Tang 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
506c94b1f96SGeliang Tang 			if (opts->csum_reqd) {
507c5b39e26SGeliang Tang 				/* we need to propagate more info to csum the pseudo hdr */
508c5b39e26SGeliang Tang 				opts->ext_copy.data_seq = mpext->data_seq;
509c5b39e26SGeliang Tang 				opts->ext_copy.subflow_seq = mpext->subflow_seq;
510c94b1f96SGeliang Tang 				opts->ext_copy.csum = mpext->csum;
511c94b1f96SGeliang Tang 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
512c94b1f96SGeliang Tang 			}
513c94b1f96SGeliang Tang 			*size = ALIGN(len, 4);
514c94b1f96SGeliang Tang 		} else {
515cec37a6eSPeter Krystad 			*size = TCPOLEN_MPTCP_MPC_ACK;
516c94b1f96SGeliang Tang 		}
517cc7972eaSChristoph Paasch 
518cc7972eaSChristoph Paasch 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
519cc7972eaSChristoph Paasch 			 subflow, subflow->local_key, subflow->remote_key,
520cc7972eaSChristoph Paasch 			 data_len);
521cc7972eaSChristoph Paasch 
522cec37a6eSPeter Krystad 		return true;
523ec3edaa7SPeter Krystad 	} else if (subflow->mp_join) {
524ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
525ec3edaa7SPeter Krystad 		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
526ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_ACK;
527ec3edaa7SPeter Krystad 		pr_debug("subflow=%p", subflow);
528ec3edaa7SPeter Krystad 
529ec3edaa7SPeter Krystad 		schedule_3rdack_retransmission(sk);
530ec3edaa7SPeter Krystad 		return true;
531cec37a6eSPeter Krystad 	}
532cec37a6eSPeter Krystad 	return false;
533cec37a6eSPeter Krystad }
534cec37a6eSPeter Krystad 
5356d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
5369c29e361SPaolo Abeni 				 struct sk_buff *skb, struct mptcp_ext *ext)
5376d0060f6SMat Martineau {
538017512a0SPaolo Abeni 	/* The write_seq value has already been incremented, so the actual
539017512a0SPaolo Abeni 	 * sequence number for the DATA_FIN is one less.
540017512a0SPaolo Abeni 	 */
541017512a0SPaolo Abeni 	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
5427279da61SMat Martineau 
5439c29e361SPaolo Abeni 	if (!ext->use_map || !skb->len) {
5446d0060f6SMat Martineau 		/* RFC6824 requires a DSS mapping with specific values
5456d0060f6SMat Martineau 		 * if DATA_FIN is set but no data payload is mapped
5466d0060f6SMat Martineau 		 */
5476d37a0b8SMat Martineau 		ext->data_fin = 1;
5486d0060f6SMat Martineau 		ext->use_map = 1;
5496d0060f6SMat Martineau 		ext->dsn64 = 1;
550017512a0SPaolo Abeni 		ext->data_seq = data_fin_tx_seq;
5516d0060f6SMat Martineau 		ext->subflow_seq = 0;
5526d0060f6SMat Martineau 		ext->data_len = 1;
5537279da61SMat Martineau 	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
5546d37a0b8SMat Martineau 		/* If there's an existing DSS mapping and it is the
5556d37a0b8SMat Martineau 		 * final mapping, DATA_FIN consumes 1 additional byte of
5566d37a0b8SMat Martineau 		 * mapping space.
5576d0060f6SMat Martineau 		 */
5586d37a0b8SMat Martineau 		ext->data_fin = 1;
5596d0060f6SMat Martineau 		ext->data_len++;
5606d0060f6SMat Martineau 	}
5616d0060f6SMat Martineau }
5626d0060f6SMat Martineau 
5636d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
564d87903b6SPaolo Abeni 					  bool snd_data_fin_enable,
5656d0060f6SMat Martineau 					  unsigned int *size,
5666d0060f6SMat Martineau 					  unsigned int remaining,
5676d0060f6SMat Martineau 					  struct mptcp_out_options *opts)
5686d0060f6SMat Martineau {
5696d0060f6SMat Martineau 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
5707279da61SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
5716d0060f6SMat Martineau 	unsigned int dss_size = 0;
5726d0060f6SMat Martineau 	struct mptcp_ext *mpext;
5736d0060f6SMat Martineau 	unsigned int ack_size;
574d22f4988SChristoph Paasch 	bool ret = false;
575d87903b6SPaolo Abeni 	u64 ack_seq;
5766d0060f6SMat Martineau 
577c5b39e26SGeliang Tang 	opts->csum_reqd = READ_ONCE(msk->csum_enabled);
5780bac966aSMat Martineau 	mpext = skb ? mptcp_get_ext(skb) : NULL;
5796d0060f6SMat Martineau 
5807279da61SMat Martineau 	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
581c5b39e26SGeliang Tang 		unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
5826d0060f6SMat Martineau 
583c5b39e26SGeliang Tang 		if (mpext) {
584c5b39e26SGeliang Tang 			if (opts->csum_reqd)
585c5b39e26SGeliang Tang 				map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
586c5b39e26SGeliang Tang 
587c5b39e26SGeliang Tang 			opts->ext_copy = *mpext;
588c5b39e26SGeliang Tang 		}
5896d0060f6SMat Martineau 
5906d0060f6SMat Martineau 		remaining -= map_size;
5916d0060f6SMat Martineau 		dss_size = map_size;
5927279da61SMat Martineau 		if (skb && snd_data_fin_enable)
5939c29e361SPaolo Abeni 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
5941bff1e43SPaolo Abeni 		opts->suboptions = OPTION_MPTCP_DSS;
595d22f4988SChristoph Paasch 		ret = true;
596d22f4988SChristoph Paasch 	}
597d22f4988SChristoph Paasch 
5982398e399SPaolo Abeni 	/* passive sockets msk will set the 'can_ack' after accept(), even
5992398e399SPaolo Abeni 	 * if the first subflow may have the already the remote key handy
6002398e399SPaolo Abeni 	 */
601d22f4988SChristoph Paasch 	opts->ext_copy.use_ack = 0;
602dc093db5SPaolo Abeni 	if (!READ_ONCE(msk->can_ack)) {
603d22f4988SChristoph Paasch 		*size = ALIGN(dss_size, 4);
604d22f4988SChristoph Paasch 		return ret;
6056d0060f6SMat Martineau 	}
6066d0060f6SMat Martineau 
607e3859603SPaolo Abeni 	ack_seq = READ_ONCE(msk->ack_seq);
60837198e93SDavide Caratti 	if (READ_ONCE(msk->use_64bit_ack)) {
6096d0060f6SMat Martineau 		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
610e3859603SPaolo Abeni 		opts->ext_copy.data_ack = ack_seq;
611a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 1;
612a0c1d0eaSChristoph Paasch 	} else {
613a0c1d0eaSChristoph Paasch 		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
614e3859603SPaolo Abeni 		opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
615a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 0;
616a0c1d0eaSChristoph Paasch 	}
617a0c1d0eaSChristoph Paasch 	opts->ext_copy.use_ack = 1;
6181bff1e43SPaolo Abeni 	opts->suboptions = OPTION_MPTCP_DSS;
619ea4ca586SPaolo Abeni 	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
6206d0060f6SMat Martineau 
6216d0060f6SMat Martineau 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
6226d0060f6SMat Martineau 	if (dss_size == 0)
6236d0060f6SMat Martineau 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
6246d0060f6SMat Martineau 
6256d0060f6SMat Martineau 	dss_size += ack_size;
6266d0060f6SMat Martineau 
6276d0060f6SMat Martineau 	*size = ALIGN(dss_size, 4);
6286d0060f6SMat Martineau 	return true;
6296d0060f6SMat Martineau }
6306d0060f6SMat Martineau 
631761c124eSGeliang Tang static u64 add_addr_generate_hmac(u64 key1, u64 key2,
632761c124eSGeliang Tang 				  struct mptcp_addr_info *addr)
6333df523abSPeter Krystad {
634761c124eSGeliang Tang 	u16 port = ntohs(addr->port);
635bd697222STodd Malsbary 	u8 hmac[SHA256_DIGEST_SIZE];
6363df523abSPeter Krystad 	u8 msg[19];
637761c124eSGeliang Tang 	int i = 0;
6383df523abSPeter Krystad 
639761c124eSGeliang Tang 	msg[i++] = addr->id;
640761c124eSGeliang Tang 	if (addr->family == AF_INET) {
641761c124eSGeliang Tang 		memcpy(&msg[i], &addr->addr.s_addr, 4);
642761c124eSGeliang Tang 		i += 4;
643761c124eSGeliang Tang 	}
644761c124eSGeliang Tang #if IS_ENABLED(CONFIG_MPTCP_IPV6)
645761c124eSGeliang Tang 	else if (addr->family == AF_INET6) {
646761c124eSGeliang Tang 		memcpy(&msg[i], &addr->addr6.s6_addr, 16);
647761c124eSGeliang Tang 		i += 16;
648761c124eSGeliang Tang 	}
649761c124eSGeliang Tang #endif
650761c124eSGeliang Tang 	msg[i++] = port >> 8;
651761c124eSGeliang Tang 	msg[i++] = port & 0xFF;
6523df523abSPeter Krystad 
653761c124eSGeliang Tang 	mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
6543df523abSPeter Krystad 
655bd697222STodd Malsbary 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
6563df523abSPeter Krystad }
6573df523abSPeter Krystad 
65884dfe367SGeliang Tang static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
6593df523abSPeter Krystad 					       unsigned int *size,
6603df523abSPeter Krystad 					       unsigned int remaining,
6613df523abSPeter Krystad 					       struct mptcp_out_options *opts)
6623df523abSPeter Krystad {
6633df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
6643df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
66584dfe367SGeliang Tang 	bool drop_other_suboptions = false;
66684dfe367SGeliang Tang 	unsigned int opt_size = *size;
6676a6c05a8SGeliang Tang 	bool echo;
6684a2777a8SGeliang Tang 	bool port;
6691b1c7a0eSPeter Krystad 	int len;
6703df523abSPeter Krystad 
6711f5e9e2fSYonglong Li 	/* add addr will strip the existing options, be sure to avoid breaking
6721f5e9e2fSYonglong Li 	 * MPC/MPJ handshakes
6731f5e9e2fSYonglong Li 	 */
674f643b803SGeliang Tang 	if (!mptcp_pm_should_add_signal(msk) ||
6751f5e9e2fSYonglong Li 	    (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
6761f5e9e2fSYonglong Li 	    !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
6771f5e9e2fSYonglong Li 		    &echo, &port, &drop_other_suboptions))
6783df523abSPeter Krystad 		return false;
6791b1c7a0eSPeter Krystad 
6801f5e9e2fSYonglong Li 	if (drop_other_suboptions)
6811f5e9e2fSYonglong Li 		remaining += opt_size;
68230f60baeSGeliang Tang 	len = mptcp_add_addr_len(opts->addr.family, echo, port);
6831b1c7a0eSPeter Krystad 	if (remaining < len)
6841b1c7a0eSPeter Krystad 		return false;
6851b1c7a0eSPeter Krystad 
6861b1c7a0eSPeter Krystad 	*size = len;
6871f5e9e2fSYonglong Li 	if (drop_other_suboptions) {
6881f5e9e2fSYonglong Li 		pr_debug("drop other suboptions");
6891f5e9e2fSYonglong Li 		opts->suboptions = 0;
6901bff1e43SPaolo Abeni 
6911bff1e43SPaolo Abeni 		/* note that e.g. DSS could have written into the memory
6921bff1e43SPaolo Abeni 		 * aliased by ahmac, we must reset the field here
6931bff1e43SPaolo Abeni 		 * to avoid appending the hmac even for ADD_ADDR echo
6941bff1e43SPaolo Abeni 		 * options
6951bff1e43SPaolo Abeni 		 */
6961bff1e43SPaolo Abeni 		opts->ahmac = 0;
69784dfe367SGeliang Tang 		*size -= opt_size;
6981f5e9e2fSYonglong Li 	}
6993df523abSPeter Krystad 	opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
7006a6c05a8SGeliang Tang 	if (!echo) {
7013df523abSPeter Krystad 		opts->ahmac = add_addr_generate_hmac(msk->local_key,
7023df523abSPeter Krystad 						     msk->remote_key,
703761c124eSGeliang Tang 						     &opts->addr);
7043df523abSPeter Krystad 	}
7054a2777a8SGeliang Tang 	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
70630f60baeSGeliang Tang 		 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
7073df523abSPeter Krystad 
7083df523abSPeter Krystad 	return true;
7093df523abSPeter Krystad }
7103df523abSPeter Krystad 
7115cb104aeSGeliang Tang static bool mptcp_established_options_rm_addr(struct sock *sk,
7125cb104aeSGeliang Tang 					      unsigned int *size,
7135cb104aeSGeliang Tang 					      unsigned int remaining,
7145cb104aeSGeliang Tang 					      struct mptcp_out_options *opts)
7155cb104aeSGeliang Tang {
7165cb104aeSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
7175cb104aeSGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
7186445e17aSGeliang Tang 	struct mptcp_rm_list rm_list;
7196445e17aSGeliang Tang 	int i, len;
7205cb104aeSGeliang Tang 
7215cb104aeSGeliang Tang 	if (!mptcp_pm_should_rm_signal(msk) ||
7226445e17aSGeliang Tang 	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
7235cb104aeSGeliang Tang 		return false;
7245cb104aeSGeliang Tang 
7256445e17aSGeliang Tang 	len = mptcp_rm_addr_len(&rm_list);
7266445e17aSGeliang Tang 	if (len < 0)
7276445e17aSGeliang Tang 		return false;
7286445e17aSGeliang Tang 	if (remaining < len)
7295cb104aeSGeliang Tang 		return false;
7305cb104aeSGeliang Tang 
7316445e17aSGeliang Tang 	*size = len;
7325cb104aeSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
7336445e17aSGeliang Tang 	opts->rm_list = rm_list;
7345cb104aeSGeliang Tang 
7356445e17aSGeliang Tang 	for (i = 0; i < opts->rm_list.nr; i++)
7366445e17aSGeliang Tang 		pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
7375cb104aeSGeliang Tang 
7385cb104aeSGeliang Tang 	return true;
7395cb104aeSGeliang Tang }
7405cb104aeSGeliang Tang 
74106706542SGeliang Tang static bool mptcp_established_options_mp_prio(struct sock *sk,
74206706542SGeliang Tang 					      unsigned int *size,
74306706542SGeliang Tang 					      unsigned int remaining,
74406706542SGeliang Tang 					      struct mptcp_out_options *opts)
74506706542SGeliang Tang {
74606706542SGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
74706706542SGeliang Tang 
7481bff1e43SPaolo Abeni 	/* can't send MP_PRIO with MPC, as they share the same option space:
7491bff1e43SPaolo Abeni 	 * 'backup'. Also it makes no sense at all
7501bff1e43SPaolo Abeni 	 */
751*13ac17a3SGeliang Tang 	if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
75206706542SGeliang Tang 		return false;
75306706542SGeliang Tang 
754ec99a470SDavide Caratti 	/* account for the trailing 'nop' option */
755ec99a470SDavide Caratti 	if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
75606706542SGeliang Tang 		return false;
75706706542SGeliang Tang 
758ec99a470SDavide Caratti 	*size = TCPOLEN_MPTCP_PRIO_ALIGN;
75906706542SGeliang Tang 	opts->suboptions |= OPTION_MPTCP_PRIO;
76006706542SGeliang Tang 	opts->backup = subflow->request_bkup;
76106706542SGeliang Tang 
76206706542SGeliang Tang 	pr_debug("prio=%d", opts->backup);
76306706542SGeliang Tang 
76406706542SGeliang Tang 	return true;
76506706542SGeliang Tang }
76606706542SGeliang Tang 
767c25aeb4eSGeliang Tang static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
768dc87efdbSFlorian Westphal 						   unsigned int *size,
769dc87efdbSFlorian Westphal 						   unsigned int remaining,
770dc87efdbSFlorian Westphal 						   struct mptcp_out_options *opts)
771dc87efdbSFlorian Westphal {
772dc87efdbSFlorian Westphal 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
773dc87efdbSFlorian Westphal 
774dc87efdbSFlorian Westphal 	if (remaining < TCPOLEN_MPTCP_RST)
775c25aeb4eSGeliang Tang 		return false;
776dc87efdbSFlorian Westphal 
777dc87efdbSFlorian Westphal 	*size = TCPOLEN_MPTCP_RST;
778dc87efdbSFlorian Westphal 	opts->suboptions |= OPTION_MPTCP_RST;
779dc87efdbSFlorian Westphal 	opts->reset_transient = subflow->reset_transient;
780dc87efdbSFlorian Westphal 	opts->reset_reason = subflow->reset_reason;
781c25aeb4eSGeliang Tang 
782c25aeb4eSGeliang Tang 	return true;
783c25aeb4eSGeliang Tang }
784c25aeb4eSGeliang Tang 
785c25aeb4eSGeliang Tang static bool mptcp_established_options_mp_fail(struct sock *sk,
786c25aeb4eSGeliang Tang 					      unsigned int *size,
787c25aeb4eSGeliang Tang 					      unsigned int remaining,
788c25aeb4eSGeliang Tang 					      struct mptcp_out_options *opts)
789c25aeb4eSGeliang Tang {
790c25aeb4eSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
791c25aeb4eSGeliang Tang 
792c25aeb4eSGeliang Tang 	if (likely(!subflow->send_mp_fail))
793c25aeb4eSGeliang Tang 		return false;
794c25aeb4eSGeliang Tang 
795c25aeb4eSGeliang Tang 	if (remaining < TCPOLEN_MPTCP_FAIL)
796c25aeb4eSGeliang Tang 		return false;
797c25aeb4eSGeliang Tang 
798c25aeb4eSGeliang Tang 	*size = TCPOLEN_MPTCP_FAIL;
799c25aeb4eSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_FAIL;
800c25aeb4eSGeliang Tang 	opts->fail_seq = subflow->map_seq;
801c25aeb4eSGeliang Tang 
802c25aeb4eSGeliang Tang 	pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
803c25aeb4eSGeliang Tang 
804c25aeb4eSGeliang Tang 	return true;
805dc87efdbSFlorian Westphal }
806dc87efdbSFlorian Westphal 
8076d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
8086d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
8096d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
8106d0060f6SMat Martineau {
811d87903b6SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
812d87903b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
8136d0060f6SMat Martineau 	unsigned int opt_size = 0;
814d87903b6SPaolo Abeni 	bool snd_data_fin;
8156d0060f6SMat Martineau 	bool ret = false;
8166d0060f6SMat Martineau 
8173df523abSPeter Krystad 	opts->suboptions = 0;
8183df523abSPeter Krystad 
819d87903b6SPaolo Abeni 	if (unlikely(__mptcp_check_fallback(msk)))
820e1ff9e82SDavide Caratti 		return false;
821e1ff9e82SDavide Caratti 
822dc87efdbSFlorian Westphal 	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
823c25aeb4eSGeliang Tang 		if (mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
824c25aeb4eSGeliang Tang 			*size += opt_size;
825c25aeb4eSGeliang Tang 			remaining -= opt_size;
826c25aeb4eSGeliang Tang 		}
827c25aeb4eSGeliang Tang 		if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
828c25aeb4eSGeliang Tang 			*size += opt_size;
829c25aeb4eSGeliang Tang 			remaining -= opt_size;
830c25aeb4eSGeliang Tang 		}
831dc87efdbSFlorian Westphal 		return true;
832dc87efdbSFlorian Westphal 	}
833d5824847SPaolo Abeni 
834d87903b6SPaolo Abeni 	snd_data_fin = mptcp_data_fin_enabled(msk);
835d87903b6SPaolo Abeni 	if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
8366d0060f6SMat Martineau 		ret = true;
837c25aeb4eSGeliang Tang 	else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) {
8386d0060f6SMat Martineau 		ret = true;
839c25aeb4eSGeliang Tang 		if (mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
840c25aeb4eSGeliang Tang 			*size += opt_size;
841c25aeb4eSGeliang Tang 			remaining -= opt_size;
842c25aeb4eSGeliang Tang 			return true;
843c25aeb4eSGeliang Tang 		}
844c25aeb4eSGeliang Tang 	}
8456d0060f6SMat Martineau 
8466d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
8476d0060f6SMat Martineau 	 * TCP option space would be fatal
8486d0060f6SMat Martineau 	 */
8496d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
8506d0060f6SMat Martineau 		return false;
8516d0060f6SMat Martineau 
8526d0060f6SMat Martineau 	*size += opt_size;
8536d0060f6SMat Martineau 	remaining -= opt_size;
85484dfe367SGeliang Tang 	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
8553df523abSPeter Krystad 		*size += opt_size;
8563df523abSPeter Krystad 		remaining -= opt_size;
8573df523abSPeter Krystad 		ret = true;
8585cb104aeSGeliang Tang 	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
8595cb104aeSGeliang Tang 		*size += opt_size;
8605cb104aeSGeliang Tang 		remaining -= opt_size;
8615cb104aeSGeliang Tang 		ret = true;
8623df523abSPeter Krystad 	}
8636d0060f6SMat Martineau 
86406706542SGeliang Tang 	if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
86506706542SGeliang Tang 		*size += opt_size;
86606706542SGeliang Tang 		remaining -= opt_size;
86706706542SGeliang Tang 		ret = true;
86806706542SGeliang Tang 	}
86906706542SGeliang Tang 
8706d0060f6SMat Martineau 	return ret;
8716d0060f6SMat Martineau }
8726d0060f6SMat Martineau 
873cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
874cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
875cec37a6eSPeter Krystad {
876cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
877cec37a6eSPeter Krystad 
878cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
879cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
880cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
88106fe1719SGeliang Tang 		opts->csum_reqd = subflow_req->csum_reqd;
882bab6b88eSGeliang Tang 		opts->allow_join_id0 = subflow_req->allow_join_id0;
883cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
884cec37a6eSPeter Krystad 		pr_debug("subflow_req=%p, local_key=%llu",
885cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
886cec37a6eSPeter Krystad 		return true;
887f296234cSPeter Krystad 	} else if (subflow_req->mp_join) {
888f296234cSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
889f296234cSPeter Krystad 		opts->backup = subflow_req->backup;
890f296234cSPeter Krystad 		opts->join_id = subflow_req->local_id;
891f296234cSPeter Krystad 		opts->thmac = subflow_req->thmac;
892f296234cSPeter Krystad 		opts->nonce = subflow_req->local_nonce;
893f296234cSPeter Krystad 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
894f296234cSPeter Krystad 			 subflow_req, opts->backup, opts->join_id,
895f296234cSPeter Krystad 			 opts->thmac, opts->nonce);
896f296234cSPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
897f296234cSPeter Krystad 		return true;
898cec37a6eSPeter Krystad 	}
899cec37a6eSPeter Krystad 	return false;
900cec37a6eSPeter Krystad }
901cec37a6eSPeter Krystad 
902d5824847SPaolo Abeni static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
903f296234cSPeter Krystad 				    struct mptcp_subflow_context *subflow,
904d22f4988SChristoph Paasch 				    struct sk_buff *skb,
905d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
906d22f4988SChristoph Paasch {
907d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
908f296234cSPeter Krystad 	 * will make the subflow fully established
909d22f4988SChristoph Paasch 	 */
910f296234cSPeter Krystad 	if (likely(subflow->fully_established)) {
911f296234cSPeter Krystad 		/* on passive sockets, check for 3rd ack retransmission
912f296234cSPeter Krystad 		 * note that msk is always set by subflow_syn_recv_sock()
913f296234cSPeter Krystad 		 * for mp_join subflows
914f296234cSPeter Krystad 		 */
915f296234cSPeter Krystad 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
916f296234cSPeter Krystad 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
91774c7dfbeSPaolo Abeni 		    subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
918f296234cSPeter Krystad 		    READ_ONCE(msk->pm.server_side))
919d5824847SPaolo Abeni 			tcp_send_ack(ssk);
920f296234cSPeter Krystad 		goto fully_established;
921f296234cSPeter Krystad 	}
922d22f4988SChristoph Paasch 
923d5824847SPaolo Abeni 	/* we must process OoO packets before the first subflow is fully
924d5824847SPaolo Abeni 	 * established. OoO packets are instead a protocol violation
925d5824847SPaolo Abeni 	 * for MP_JOIN subflows as the peer must not send any data
926d5824847SPaolo Abeni 	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
927f296234cSPeter Krystad 	 */
928d5824847SPaolo Abeni 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
929d5824847SPaolo Abeni 		if (subflow->mp_join)
930d5824847SPaolo Abeni 			goto reset;
931f296234cSPeter Krystad 		return subflow->mp_capable;
932d5824847SPaolo Abeni 	}
933f296234cSPeter Krystad 
93474c7dfbeSPaolo Abeni 	if (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
93574c7dfbeSPaolo Abeni 	    ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo)) {
936f296234cSPeter Krystad 		/* subflows are fully established as soon as we get any
93767b12f79SMatthieu Baerts 		 * additional ack, including ADD_ADDR.
938f296234cSPeter Krystad 		 */
9390be534f5SPaolo Abeni 		subflow->fully_established = 1;
940b93df08cSPaolo Abeni 		WRITE_ONCE(msk->fully_established, true);
941f296234cSPeter Krystad 		goto fully_established;
942f296234cSPeter Krystad 	}
943d22f4988SChristoph Paasch 
944d22f4988SChristoph Paasch 	/* If the first established packet does not contain MP_CAPABLE + data
945d5824847SPaolo Abeni 	 * then fallback to TCP. Fallback scenarios requires a reset for
946d5824847SPaolo Abeni 	 * MP_JOIN subflows.
947d22f4988SChristoph Paasch 	 */
94874c7dfbeSPaolo Abeni 	if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
949d5824847SPaolo Abeni 		if (subflow->mp_join)
950d5824847SPaolo Abeni 			goto reset;
951d22f4988SChristoph Paasch 		subflow->mp_capable = 0;
952e1ff9e82SDavide Caratti 		pr_fallback(msk);
953e1ff9e82SDavide Caratti 		__mptcp_do_fallback(msk);
954d22f4988SChristoph Paasch 		return false;
955d22f4988SChristoph Paasch 	}
956f296234cSPeter Krystad 
957df377be3SGeliang Tang 	if (mp_opt->deny_join_id0)
958df377be3SGeliang Tang 		WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
959df377be3SGeliang Tang 
960d6085fe1SPaolo Abeni 	if (unlikely(!READ_ONCE(msk->pm.server_side)))
961d6085fe1SPaolo Abeni 		pr_warn_once("bogus mpc option on established client sk");
962b93df08cSPaolo Abeni 	mptcp_subflow_fully_established(subflow, mp_opt);
963f296234cSPeter Krystad 
964f296234cSPeter Krystad fully_established:
9655b950ff4SPaolo Abeni 	/* if the subflow is not already linked into the conn_list, we can't
9665b950ff4SPaolo Abeni 	 * notify the PM: this subflow is still on the listener queue
9675b950ff4SPaolo Abeni 	 * and the PM possibly acquiring the subflow lock could race with
9685b950ff4SPaolo Abeni 	 * the listener close
9695b950ff4SPaolo Abeni 	 */
9705b950ff4SPaolo Abeni 	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
971f296234cSPeter Krystad 		return true;
972f296234cSPeter Krystad 
973f296234cSPeter Krystad 	subflow->pm_notified = 1;
974ec3edaa7SPeter Krystad 	if (subflow->mp_join) {
975d5824847SPaolo Abeni 		clear_3rdack_retransmission(ssk);
97662535200SGeliang Tang 		mptcp_pm_subflow_established(msk);
977ec3edaa7SPeter Krystad 	} else {
9786c714f1bSFlorian Westphal 		mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
979ec3edaa7SPeter Krystad 	}
980d22f4988SChristoph Paasch 	return true;
981d5824847SPaolo Abeni 
982d5824847SPaolo Abeni reset:
983d5824847SPaolo Abeni 	mptcp_subflow_reset(ssk);
984d5824847SPaolo Abeni 	return false;
985d22f4988SChristoph Paasch }
986d22f4988SChristoph Paasch 
9871502328fSPaolo Abeni u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
988cc9d2566SPaolo Abeni {
9891502328fSPaolo Abeni 	u32 old_seq32, cur_seq32;
990cc9d2566SPaolo Abeni 
9911502328fSPaolo Abeni 	old_seq32 = (u32)old_seq;
9921502328fSPaolo Abeni 	cur_seq32 = (u32)cur_seq;
9931502328fSPaolo Abeni 	cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
9941502328fSPaolo Abeni 	if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
9951502328fSPaolo Abeni 		return cur_seq + (1LL << 32);
996cc9d2566SPaolo Abeni 
9971502328fSPaolo Abeni 	/* reverse wrap could happen, too */
9981502328fSPaolo Abeni 	if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
9991502328fSPaolo Abeni 		return cur_seq - (1LL << 32);
10001502328fSPaolo Abeni 	return cur_seq;
1001cc9d2566SPaolo Abeni }
1002cc9d2566SPaolo Abeni 
10036f8a612aSFlorian Westphal static void ack_update_msk(struct mptcp_sock *msk,
10046e628cd3SPaolo Abeni 			   struct sock *ssk,
1005cc9d2566SPaolo Abeni 			   struct mptcp_options_received *mp_opt)
1006cc9d2566SPaolo Abeni {
10077439d687SPaolo Abeni 	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
10086f8a612aSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
10097439d687SPaolo Abeni 	u64 old_snd_una;
10107439d687SPaolo Abeni 
10117439d687SPaolo Abeni 	mptcp_data_lock(sk);
1012cc9d2566SPaolo Abeni 
1013cc9d2566SPaolo Abeni 	/* avoid ack expansion on update conflict, to reduce the risk of
1014cc9d2566SPaolo Abeni 	 * wrongly expanding to a future ack sequence number, which is way
1015cc9d2566SPaolo Abeni 	 * more dangerous than missing an ack
1016cc9d2566SPaolo Abeni 	 */
10177439d687SPaolo Abeni 	old_snd_una = msk->snd_una;
10181502328fSPaolo Abeni 	new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1019cc9d2566SPaolo Abeni 
10200d199e43SFlorian Westphal 	/* ACK for data not even sent yet? Ignore.*/
10210d199e43SFlorian Westphal 	if (unlikely(after64(new_snd_una, snd_nxt)))
1022cc9d2566SPaolo Abeni 		new_snd_una = old_snd_una;
1023cc9d2566SPaolo Abeni 
10246f8a612aSFlorian Westphal 	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
10256f8a612aSFlorian Westphal 
1026219d0499SPaolo Abeni 	if (after64(new_wnd_end, msk->wnd_end))
10277439d687SPaolo Abeni 		msk->wnd_end = new_wnd_end;
1028219d0499SPaolo Abeni 
1029219d0499SPaolo Abeni 	/* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1030d09d818eSPaolo Abeni 	if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1031219d0499SPaolo Abeni 		__mptcp_check_push(sk, ssk);
10326f8a612aSFlorian Westphal 
10337439d687SPaolo Abeni 	if (after64(new_snd_una, old_snd_una)) {
10347439d687SPaolo Abeni 		msk->snd_una = new_snd_una;
10357439d687SPaolo Abeni 		__mptcp_data_acked(sk);
1036cc9d2566SPaolo Abeni 	}
10377439d687SPaolo Abeni 	mptcp_data_unlock(sk);
1038ed66bfb4SGeliang Tang 
1039ed66bfb4SGeliang Tang 	trace_ack_update_msk(mp_opt->data_ack,
1040ed66bfb4SGeliang Tang 			     old_snd_una, new_snd_una,
1041ed66bfb4SGeliang Tang 			     new_wnd_end, msk->wnd_end);
1042b51f9b80SPaolo Abeni }
1043cc9d2566SPaolo Abeni 
10441a49b2c2SMat Martineau bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
10453721b9b6SMat Martineau {
10463721b9b6SMat Martineau 	/* Skip if DATA_FIN was already received.
10473721b9b6SMat Martineau 	 * If updating simultaneously with the recvmsg loop, values
10483721b9b6SMat Martineau 	 * should match. If they mismatch, the peer is misbehaving and
10493721b9b6SMat Martineau 	 * we will prefer the most recent information.
10503721b9b6SMat Martineau 	 */
1051781bf13dSPaolo Abeni 	if (READ_ONCE(msk->rcv_data_fin))
10523721b9b6SMat Martineau 		return false;
10533721b9b6SMat Martineau 
10541a49b2c2SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin_seq,
10551502328fSPaolo Abeni 		   mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
10563721b9b6SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin, 1);
10573721b9b6SMat Martineau 
10583721b9b6SMat Martineau 	return true;
10593721b9b6SMat Martineau }
10603721b9b6SMat Martineau 
10611b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk,
10621b1c7a0eSPeter Krystad 				struct mptcp_options_received *mp_opt)
10631b1c7a0eSPeter Krystad {
10641b1c7a0eSPeter Krystad 	u64 hmac = 0;
10651b1c7a0eSPeter Krystad 
10661b1c7a0eSPeter Krystad 	if (mp_opt->echo)
10671b1c7a0eSPeter Krystad 		return true;
10681b1c7a0eSPeter Krystad 
10691b1c7a0eSPeter Krystad 	hmac = add_addr_generate_hmac(msk->remote_key,
10701b1c7a0eSPeter Krystad 				      msk->local_key,
1071761c124eSGeliang Tang 				      &mp_opt->addr);
10721b1c7a0eSPeter Krystad 
10731b1c7a0eSPeter Krystad 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
10741b1c7a0eSPeter Krystad 		 msk, (unsigned long long)hmac,
10751b1c7a0eSPeter Krystad 		 (unsigned long long)mp_opt->ahmac);
10761b1c7a0eSPeter Krystad 
10771b1c7a0eSPeter Krystad 	return hmac == mp_opt->ahmac;
10781b1c7a0eSPeter Krystad }
10791b1c7a0eSPeter Krystad 
10806787b7e3SJianguo Wu /* Return false if a subflow has been reset, else return true */
10816787b7e3SJianguo Wu bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1082648ef4b8SMat Martineau {
1083d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
10841b1c7a0eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1085cfde141eSPaolo Abeni 	struct mptcp_options_received mp_opt;
1086648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
1087648ef4b8SMat Martineau 
10886e628cd3SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
10896e628cd3SPaolo Abeni 		/* Keep it simple and unconditionally trigger send data cleanup and
10906e628cd3SPaolo Abeni 		 * pending queue spooling. We will need to acquire the data lock
10916e628cd3SPaolo Abeni 		 * for more accurate checks, and once the lock is acquired, such
10926e628cd3SPaolo Abeni 		 * helpers are cheap.
10936e628cd3SPaolo Abeni 		 */
10946e628cd3SPaolo Abeni 		mptcp_data_lock(subflow->conn);
1095219d0499SPaolo Abeni 		if (sk_stream_memory_free(sk))
1096219d0499SPaolo Abeni 			__mptcp_check_push(subflow->conn, sk);
10976e628cd3SPaolo Abeni 		__mptcp_data_acked(subflow->conn);
10986e628cd3SPaolo Abeni 		mptcp_data_unlock(subflow->conn);
10996787b7e3SJianguo Wu 		return true;
11006e628cd3SPaolo Abeni 	}
1101e1ff9e82SDavide Caratti 
1102c863225bSGeliang Tang 	mptcp_get_options(sk, skb, &mp_opt);
11036787b7e3SJianguo Wu 
11046787b7e3SJianguo Wu 	/* The subflow can be in close state only if check_fully_established()
11056787b7e3SJianguo Wu 	 * just sent a reset. If so, tell the caller to ignore the current packet.
11066787b7e3SJianguo Wu 	 */
1107cfde141eSPaolo Abeni 	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
11086787b7e3SJianguo Wu 		return sk->sk_state != TCP_CLOSE;
1109648ef4b8SMat Martineau 
1110f6c2ef59SPaolo Abeni 	if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
111174c7dfbeSPaolo Abeni 		if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
111250c504a2SFlorian Westphal 		    msk->local_key == mp_opt.rcvr_key) {
111350c504a2SFlorian Westphal 			WRITE_ONCE(msk->rcv_fastclose, true);
111450c504a2SFlorian Westphal 			mptcp_schedule_work((struct sock *)msk);
111550c504a2SFlorian Westphal 		}
111650c504a2SFlorian Westphal 
1117f6c2ef59SPaolo Abeni 		if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1118f6c2ef59SPaolo Abeni 		    add_addr_hmac_valid(msk, &mp_opt)) {
1119a877de06SGeliang Tang 			if (!mp_opt.echo) {
1120f7dafee1SGeliang Tang 				mptcp_pm_add_addr_received(msk, &mp_opt.addr);
1121a877de06SGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1122a877de06SGeliang Tang 			} else {
1123f7dafee1SGeliang Tang 				mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1124d58300c3SDavide Caratti 				mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1125a877de06SGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1126a877de06SGeliang Tang 			}
11272fbdd9eaSGeliang Tang 
1128f7dafee1SGeliang Tang 			if (mp_opt.addr.port)
11292fbdd9eaSGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
11301b1c7a0eSPeter Krystad 		}
11311b1c7a0eSPeter Krystad 
113274c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
11335c4a824dSGeliang Tang 			mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1134d0876b22SGeliang Tang 
113574c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
113640453a5cSGeliang Tang 			mptcp_pm_mp_prio_received(sk, mp_opt.backup);
11370be2ac28SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
113840453a5cSGeliang Tang 		}
113940453a5cSGeliang Tang 
114074c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
11415580d41bSGeliang Tang 			mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1142eb7f3365SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
11435580d41bSGeliang Tang 		}
11445580d41bSGeliang Tang 
114574c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1146dc87efdbSFlorian Westphal 			subflow->reset_seen = 1;
1147dc87efdbSFlorian Westphal 			subflow->reset_reason = mp_opt.reset_reason;
1148dc87efdbSFlorian Westphal 			subflow->reset_transient = mp_opt.reset_transient;
1149dc87efdbSFlorian Westphal 		}
1150dc87efdbSFlorian Westphal 
115174c7dfbeSPaolo Abeni 		if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
11526787b7e3SJianguo Wu 			return true;
1153f6c2ef59SPaolo Abeni 	}
1154648ef4b8SMat Martineau 
1155cc9d2566SPaolo Abeni 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
1156cc9d2566SPaolo Abeni 	 * monodirectional flows will stuck
1157cc9d2566SPaolo Abeni 	 */
1158cfde141eSPaolo Abeni 	if (mp_opt.use_ack)
11596f8a612aSFlorian Westphal 		ack_update_msk(msk, sk, &mp_opt);
1160cc9d2566SPaolo Abeni 
116106827b34SMat Martineau 	/* Zero-data-length packets are dropped by the caller and not
116206827b34SMat Martineau 	 * propagated to the MPTCP layer, so the skb extension does not
116306827b34SMat Martineau 	 * need to be allocated or populated. DATA_FIN information, if
116406827b34SMat Martineau 	 * present, needs to be updated here before the skb is freed.
116543b54c6eSMat Martineau 	 */
116643b54c6eSMat Martineau 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
116743b54c6eSMat Martineau 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
11681a49b2c2SMat Martineau 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
116943b54c6eSMat Martineau 		    schedule_work(&msk->work))
117043b54c6eSMat Martineau 			sock_hold(subflow->conn);
117106827b34SMat Martineau 
11726787b7e3SJianguo Wu 		return true;
117343b54c6eSMat Martineau 	}
117443b54c6eSMat Martineau 
1175648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1176648ef4b8SMat Martineau 	if (!mpext)
11776787b7e3SJianguo Wu 		return true;
1178648ef4b8SMat Martineau 
1179648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
1180648ef4b8SMat Martineau 
1181f6c2ef59SPaolo Abeni 	if (likely(mp_opt.use_map)) {
1182cfde141eSPaolo Abeni 		if (mp_opt.mpc_map) {
1183cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
1184cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
1185cc7972eaSChristoph Paasch 			 */
1186cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
1187cc7972eaSChristoph Paasch 					     &mpext->data_seq);
1188cc7972eaSChristoph Paasch 			mpext->data_seq++;
1189cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
1190cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
1191cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
1192a77895dbSPaolo Abeni 			mpext->data_fin = 0;
1193cc7972eaSChristoph Paasch 		} else {
1194cfde141eSPaolo Abeni 			mpext->data_seq = mp_opt.data_seq;
1195cfde141eSPaolo Abeni 			mpext->subflow_seq = mp_opt.subflow_seq;
1196cfde141eSPaolo Abeni 			mpext->dsn64 = mp_opt.dsn64;
1197cfde141eSPaolo Abeni 			mpext->data_fin = mp_opt.data_fin;
1198cc7972eaSChristoph Paasch 		}
1199cfde141eSPaolo Abeni 		mpext->data_len = mp_opt.data_len;
1200648ef4b8SMat Martineau 		mpext->use_map = 1;
120174c7dfbeSPaolo Abeni 		mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1202208e8f66SGeliang Tang 
1203208e8f66SGeliang Tang 		if (mpext->csum_reqd)
1204208e8f66SGeliang Tang 			mpext->csum = mp_opt.csum;
1205648ef4b8SMat Martineau 	}
12066787b7e3SJianguo Wu 
12076787b7e3SJianguo Wu 	return true;
1208648ef4b8SMat Martineau }
1209648ef4b8SMat Martineau 
1210fa3fe2b1SFlorian Westphal static void mptcp_set_rwin(const struct tcp_sock *tp)
1211fa3fe2b1SFlorian Westphal {
1212fa3fe2b1SFlorian Westphal 	const struct sock *ssk = (const struct sock *)tp;
1213fa3fe2b1SFlorian Westphal 	const struct mptcp_subflow_context *subflow;
1214fa3fe2b1SFlorian Westphal 	struct mptcp_sock *msk;
1215fa3fe2b1SFlorian Westphal 	u64 ack_seq;
1216fa3fe2b1SFlorian Westphal 
1217fa3fe2b1SFlorian Westphal 	subflow = mptcp_subflow_ctx(ssk);
1218fa3fe2b1SFlorian Westphal 	msk = mptcp_sk(subflow->conn);
1219fa3fe2b1SFlorian Westphal 
1220fa3fe2b1SFlorian Westphal 	ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1221fa3fe2b1SFlorian Westphal 
1222fa3fe2b1SFlorian Westphal 	if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1223fa3fe2b1SFlorian Westphal 		WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1224fa3fe2b1SFlorian Westphal }
1225fa3fe2b1SFlorian Westphal 
1226c94b1f96SGeliang Tang static u16 mptcp_make_csum(const struct mptcp_ext *mpext)
1227c94b1f96SGeliang Tang {
1228c94b1f96SGeliang Tang 	struct csum_pseudo_header header;
1229c94b1f96SGeliang Tang 	__wsum csum;
1230c94b1f96SGeliang Tang 
1231c94b1f96SGeliang Tang 	/* cfr RFC 8684 3.3.1.:
1232c94b1f96SGeliang Tang 	 * the data sequence number used in the pseudo-header is
1233c94b1f96SGeliang Tang 	 * always the 64-bit value, irrespective of what length is used in the
1234c94b1f96SGeliang Tang 	 * DSS option itself.
1235c94b1f96SGeliang Tang 	 */
1236c94b1f96SGeliang Tang 	header.data_seq = cpu_to_be64(mpext->data_seq);
1237c94b1f96SGeliang Tang 	header.subflow_seq = htonl(mpext->subflow_seq);
1238c94b1f96SGeliang Tang 	header.data_len = htons(mpext->data_len);
1239c94b1f96SGeliang Tang 	header.csum = 0;
1240c94b1f96SGeliang Tang 
1241c94b1f96SGeliang Tang 	csum = csum_partial(&header, sizeof(header), ~csum_unfold(mpext->csum));
1242c94b1f96SGeliang Tang 	return (__force u16)csum_fold(csum);
1243c94b1f96SGeliang Tang }
1244c94b1f96SGeliang Tang 
1245fa3fe2b1SFlorian Westphal void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1246fa3fe2b1SFlorian Westphal 			 struct mptcp_out_options *opts)
1247eda7acddSPeter Krystad {
1248c25aeb4eSGeliang Tang 	if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1249c25aeb4eSGeliang Tang 		const struct sock *ssk = (const struct sock *)tp;
1250c25aeb4eSGeliang Tang 		struct mptcp_subflow_context *subflow;
1251c25aeb4eSGeliang Tang 
1252c25aeb4eSGeliang Tang 		subflow = mptcp_subflow_ctx(ssk);
1253c25aeb4eSGeliang Tang 		subflow->send_mp_fail = 0;
1254c25aeb4eSGeliang Tang 
1255c25aeb4eSGeliang Tang 		*ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1256c25aeb4eSGeliang Tang 				      TCPOLEN_MPTCP_FAIL,
1257c25aeb4eSGeliang Tang 				      0, 0);
1258c25aeb4eSGeliang Tang 		put_unaligned_be64(opts->fail_seq, ptr);
1259c25aeb4eSGeliang Tang 		ptr += 2;
1260c25aeb4eSGeliang Tang 	}
1261c25aeb4eSGeliang Tang 
12621bff1e43SPaolo Abeni 	/* RST is mutually exclusive with everything else */
12631bff1e43SPaolo Abeni 	if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
12641bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_RST,
12651bff1e43SPaolo Abeni 				      TCPOLEN_MPTCP_RST,
12661bff1e43SPaolo Abeni 				      opts->reset_transient,
12671bff1e43SPaolo Abeni 				      opts->reset_reason);
12681bff1e43SPaolo Abeni 		return;
12691bff1e43SPaolo Abeni 	}
12701bff1e43SPaolo Abeni 
12711bff1e43SPaolo Abeni 	/* DSS, MPC, MPJ and ADD_ADDR are mutually exclusive, see
12721bff1e43SPaolo Abeni 	 * mptcp_established_options*()
12731bff1e43SPaolo Abeni 	 */
12741bff1e43SPaolo Abeni 	if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
12751bff1e43SPaolo Abeni 		struct mptcp_ext *mpext = &opts->ext_copy;
12761bff1e43SPaolo Abeni 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
12771bff1e43SPaolo Abeni 		u8 flags = 0;
12781bff1e43SPaolo Abeni 
12791bff1e43SPaolo Abeni 		if (mpext->use_ack) {
12801bff1e43SPaolo Abeni 			flags = MPTCP_DSS_HAS_ACK;
12811bff1e43SPaolo Abeni 			if (mpext->ack64) {
12821bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_ACK64;
12831bff1e43SPaolo Abeni 				flags |= MPTCP_DSS_ACK64;
12841bff1e43SPaolo Abeni 			} else {
12851bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_ACK32;
12861bff1e43SPaolo Abeni 			}
12871bff1e43SPaolo Abeni 		}
12881bff1e43SPaolo Abeni 
12891bff1e43SPaolo Abeni 		if (mpext->use_map) {
12901bff1e43SPaolo Abeni 			len += TCPOLEN_MPTCP_DSS_MAP64;
12911bff1e43SPaolo Abeni 
12921bff1e43SPaolo Abeni 			/* Use only 64-bit mapping flags for now, add
12931bff1e43SPaolo Abeni 			 * support for optional 32-bit mappings later.
12941bff1e43SPaolo Abeni 			 */
12951bff1e43SPaolo Abeni 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
12961bff1e43SPaolo Abeni 			if (mpext->data_fin)
12971bff1e43SPaolo Abeni 				flags |= MPTCP_DSS_DATA_FIN;
12981bff1e43SPaolo Abeni 
12991bff1e43SPaolo Abeni 			if (opts->csum_reqd)
13001bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
13011bff1e43SPaolo Abeni 		}
13021bff1e43SPaolo Abeni 
13031bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
13041bff1e43SPaolo Abeni 
13051bff1e43SPaolo Abeni 		if (mpext->use_ack) {
13061bff1e43SPaolo Abeni 			if (mpext->ack64) {
13071bff1e43SPaolo Abeni 				put_unaligned_be64(mpext->data_ack, ptr);
13081bff1e43SPaolo Abeni 				ptr += 2;
13091bff1e43SPaolo Abeni 			} else {
13101bff1e43SPaolo Abeni 				put_unaligned_be32(mpext->data_ack32, ptr);
13111bff1e43SPaolo Abeni 				ptr += 1;
13121bff1e43SPaolo Abeni 			}
13131bff1e43SPaolo Abeni 		}
13141bff1e43SPaolo Abeni 
13151bff1e43SPaolo Abeni 		if (mpext->use_map) {
13161bff1e43SPaolo Abeni 			put_unaligned_be64(mpext->data_seq, ptr);
13171bff1e43SPaolo Abeni 			ptr += 2;
13181bff1e43SPaolo Abeni 			put_unaligned_be32(mpext->subflow_seq, ptr);
13191bff1e43SPaolo Abeni 			ptr += 1;
13201bff1e43SPaolo Abeni 			if (opts->csum_reqd) {
13211bff1e43SPaolo Abeni 				put_unaligned_be32(mpext->data_len << 16 |
13221bff1e43SPaolo Abeni 						   mptcp_make_csum(mpext), ptr);
13231bff1e43SPaolo Abeni 			} else {
13241bff1e43SPaolo Abeni 				put_unaligned_be32(mpext->data_len << 16 |
13251bff1e43SPaolo Abeni 						   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
13261bff1e43SPaolo Abeni 			}
13271bff1e43SPaolo Abeni 		}
1328*13ac17a3SGeliang Tang 	} else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
132906fe1719SGeliang Tang 		u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1330eda7acddSPeter Krystad 
1331c94b1f96SGeliang Tang 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1332eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
1333c94b1f96SGeliang Tang 		} else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1334cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
1335c94b1f96SGeliang Tang 		} else if (opts->ext_copy.data_len) {
1336cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1337c94b1f96SGeliang Tang 			if (opts->csum_reqd)
1338c94b1f96SGeliang Tang 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1339c94b1f96SGeliang Tang 		} else {
1340eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
1341c94b1f96SGeliang Tang 		}
1342eda7acddSPeter Krystad 
134306fe1719SGeliang Tang 		if (opts->csum_reqd)
134406fe1719SGeliang Tang 			flag |= MPTCP_CAP_CHECKSUM_REQD;
134506fe1719SGeliang Tang 
1346bab6b88eSGeliang Tang 		if (!opts->allow_join_id0)
1347bab6b88eSGeliang Tang 			flag |= MPTCP_CAP_DENY_JOIN_ID0;
1348bab6b88eSGeliang Tang 
13493df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
13503df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
135106fe1719SGeliang Tang 				      flag);
1352cc7972eaSChristoph Paasch 
1353cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1354cc7972eaSChristoph Paasch 		    opts->suboptions))
1355cc7972eaSChristoph Paasch 			goto mp_capable_done;
1356cc7972eaSChristoph Paasch 
1357eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
1358eda7acddSPeter Krystad 		ptr += 2;
1359cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1360cc7972eaSChristoph Paasch 			goto mp_capable_done;
1361cc7972eaSChristoph Paasch 
1362eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
1363eda7acddSPeter Krystad 		ptr += 2;
1364cc7972eaSChristoph Paasch 		if (!opts->ext_copy.data_len)
1365cc7972eaSChristoph Paasch 			goto mp_capable_done;
1366cc7972eaSChristoph Paasch 
1367c94b1f96SGeliang Tang 		if (opts->csum_reqd) {
1368c94b1f96SGeliang Tang 			put_unaligned_be32(opts->ext_copy.data_len << 16 |
1369c94b1f96SGeliang Tang 					   mptcp_make_csum(&opts->ext_copy), ptr);
1370c94b1f96SGeliang Tang 		} else {
1371cc7972eaSChristoph Paasch 			put_unaligned_be32(opts->ext_copy.data_len << 16 |
1372cc7972eaSChristoph Paasch 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1373c94b1f96SGeliang Tang 		}
1374cc7972eaSChristoph Paasch 		ptr += 1;
13756d0060f6SMat Martineau 
13761bff1e43SPaolo Abeni 		/* MPC is additionally mutually exclusive with MP_PRIO */
13771bff1e43SPaolo Abeni 		goto mp_capable_done;
13781bff1e43SPaolo Abeni 	} else if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
13791bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
13801bff1e43SPaolo Abeni 				      TCPOLEN_MPTCP_MPJ_SYN,
13811bff1e43SPaolo Abeni 				      opts->backup, opts->join_id);
13821bff1e43SPaolo Abeni 		put_unaligned_be32(opts->token, ptr);
13831bff1e43SPaolo Abeni 		ptr += 1;
13841bff1e43SPaolo Abeni 		put_unaligned_be32(opts->nonce, ptr);
13851bff1e43SPaolo Abeni 		ptr += 1;
13861bff1e43SPaolo Abeni 	} else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
13871bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
13881bff1e43SPaolo Abeni 				      TCPOLEN_MPTCP_MPJ_SYNACK,
13891bff1e43SPaolo Abeni 				      opts->backup, opts->join_id);
13901bff1e43SPaolo Abeni 		put_unaligned_be64(opts->thmac, ptr);
13911bff1e43SPaolo Abeni 		ptr += 2;
13921bff1e43SPaolo Abeni 		put_unaligned_be32(opts->nonce, ptr);
13931bff1e43SPaolo Abeni 		ptr += 1;
13941bff1e43SPaolo Abeni 	} else if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
13951bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
13961bff1e43SPaolo Abeni 				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
13971bff1e43SPaolo Abeni 		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
13981bff1e43SPaolo Abeni 		ptr += 5;
13991bff1e43SPaolo Abeni 	} else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
14006eb3d1e3SGeliang Tang 		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
14016eb3d1e3SGeliang Tang 		u8 echo = MPTCP_ADDR_ECHO;
14023df523abSPeter Krystad 
14033df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1404fef6b7ecSGeliang Tang 		if (opts->addr.family == AF_INET6)
1405e1ef6832SGeliang Tang 			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1406e1ef6832SGeliang Tang #endif
1407e1ef6832SGeliang Tang 
140830f60baeSGeliang Tang 		if (opts->addr.port)
140922fb85ffSGeliang Tang 			len += TCPOLEN_MPTCP_PORT_LEN;
141022fb85ffSGeliang Tang 
14116eb3d1e3SGeliang Tang 		if (opts->ahmac) {
14126eb3d1e3SGeliang Tang 			len += sizeof(opts->ahmac);
14136eb3d1e3SGeliang Tang 			echo = 0;
14146eb3d1e3SGeliang Tang 		}
14156eb3d1e3SGeliang Tang 
14163df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
141730f60baeSGeliang Tang 				      len, echo, opts->addr.id);
1418fef6b7ecSGeliang Tang 		if (opts->addr.family == AF_INET) {
141930f60baeSGeliang Tang 			memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
14203df523abSPeter Krystad 			ptr += 1;
14213df523abSPeter Krystad 		}
14223df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1423fef6b7ecSGeliang Tang 		else if (opts->addr.family == AF_INET6) {
142430f60baeSGeliang Tang 			memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
14253df523abSPeter Krystad 			ptr += 4;
1426e1ef6832SGeliang Tang 		}
1427e1ef6832SGeliang Tang #endif
142822fb85ffSGeliang Tang 
142930f60baeSGeliang Tang 		if (!opts->addr.port) {
14303df523abSPeter Krystad 			if (opts->ahmac) {
14313df523abSPeter Krystad 				put_unaligned_be64(opts->ahmac, ptr);
14323df523abSPeter Krystad 				ptr += 2;
14333df523abSPeter Krystad 			}
143422fb85ffSGeliang Tang 		} else {
143530f60baeSGeliang Tang 			u16 port = ntohs(opts->addr.port);
143630f60baeSGeliang Tang 
143722fb85ffSGeliang Tang 			if (opts->ahmac) {
143822fb85ffSGeliang Tang 				u8 *bptr = (u8 *)ptr;
143922fb85ffSGeliang Tang 
144030f60baeSGeliang Tang 				put_unaligned_be16(port, bptr);
144122fb85ffSGeliang Tang 				bptr += 2;
144222fb85ffSGeliang Tang 				put_unaligned_be64(opts->ahmac, bptr);
144322fb85ffSGeliang Tang 				bptr += 8;
144422fb85ffSGeliang Tang 				put_unaligned_be16(TCPOPT_NOP << 8 |
144522fb85ffSGeliang Tang 						   TCPOPT_NOP, bptr);
144622fb85ffSGeliang Tang 
144722fb85ffSGeliang Tang 				ptr += 3;
144822fb85ffSGeliang Tang 			} else {
144930f60baeSGeliang Tang 				put_unaligned_be32(port << 16 |
145022fb85ffSGeliang Tang 						   TCPOPT_NOP << 8 |
145122fb85ffSGeliang Tang 						   TCPOPT_NOP, ptr);
145222fb85ffSGeliang Tang 				ptr += 1;
14533df523abSPeter Krystad 			}
145422fb85ffSGeliang Tang 		}
14553df523abSPeter Krystad 	}
14563df523abSPeter Krystad 
14571bff1e43SPaolo Abeni 	if (OPTION_MPTCP_PRIO & opts->suboptions) {
14581bff1e43SPaolo Abeni 		const struct sock *ssk = (const struct sock *)tp;
14591bff1e43SPaolo Abeni 		struct mptcp_subflow_context *subflow;
14601bff1e43SPaolo Abeni 
14611bff1e43SPaolo Abeni 		subflow = mptcp_subflow_ctx(ssk);
14621bff1e43SPaolo Abeni 		subflow->send_mp_prio = 0;
14631bff1e43SPaolo Abeni 
14641bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
14651bff1e43SPaolo Abeni 				      TCPOLEN_MPTCP_PRIO,
14661bff1e43SPaolo Abeni 				      opts->backup, TCPOPT_NOP);
14671bff1e43SPaolo Abeni 	}
14681bff1e43SPaolo Abeni 
14691bff1e43SPaolo Abeni mp_capable_done:
14703df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
14716445e17aSGeliang Tang 		u8 i = 1;
14726445e17aSGeliang Tang 
14733df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
14746445e17aSGeliang Tang 				      TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
14756445e17aSGeliang Tang 				      0, opts->rm_list.ids[0]);
14766445e17aSGeliang Tang 
14776445e17aSGeliang Tang 		while (i < opts->rm_list.nr) {
14786445e17aSGeliang Tang 			u8 id1, id2, id3, id4;
14796445e17aSGeliang Tang 
14806445e17aSGeliang Tang 			id1 = opts->rm_list.ids[i];
14816445e17aSGeliang Tang 			id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
14826445e17aSGeliang Tang 			id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
14836445e17aSGeliang Tang 			id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
14846445e17aSGeliang Tang 			put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
14856445e17aSGeliang Tang 			ptr += 1;
14866445e17aSGeliang Tang 			i += 4;
14876445e17aSGeliang Tang 		}
14883df523abSPeter Krystad 	}
14893df523abSPeter Krystad 
1490fa3fe2b1SFlorian Westphal 	if (tp)
1491fa3fe2b1SFlorian Westphal 		mptcp_set_rwin(tp);
1492eda7acddSPeter Krystad }
1493dc87efdbSFlorian Westphal 
1494dc87efdbSFlorian Westphal __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1495dc87efdbSFlorian Westphal {
1496dc87efdbSFlorian Westphal 	const struct mptcp_ext *ext = mptcp_get_ext(skb);
1497dc87efdbSFlorian Westphal 	u8 flags, reason;
1498dc87efdbSFlorian Westphal 
1499dc87efdbSFlorian Westphal 	if (ext) {
1500dc87efdbSFlorian Westphal 		flags = ext->reset_transient;
1501dc87efdbSFlorian Westphal 		reason = ext->reset_reason;
1502dc87efdbSFlorian Westphal 
1503dc87efdbSFlorian Westphal 		return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1504dc87efdbSFlorian Westphal 				    flags, reason);
1505dc87efdbSFlorian Westphal 	}
1506dc87efdbSFlorian Westphal 
1507dc87efdbSFlorian Westphal 	return htonl(0u);
1508dc87efdbSFlorian Westphal }
1509dc87efdbSFlorian Westphal EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
1510