xref: /openbmc/linux/net/mptcp/options.c (revision 3c976f4c99237bcdab918ad18e7bee3b77e6d316)
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;
29fe33d386SPaolo Abeni 	u16 subopt;
30eda7acddSPeter Krystad 	u8 version;
31eda7acddSPeter Krystad 	u8 flags;
325c4a824dSGeliang Tang 	u8 i;
33eda7acddSPeter Krystad 
34eda7acddSPeter Krystad 	switch (subtype) {
35eda7acddSPeter Krystad 	case MPTCPOPT_MP_CAPABLE:
36cc7972eaSChristoph Paasch 		/* strict size checking */
37cc7972eaSChristoph Paasch 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
38cc7972eaSChristoph Paasch 			if (skb->len > tcp_hdr(skb)->doff << 2)
39cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
40cc7972eaSChristoph Paasch 			else
41cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
42fe33d386SPaolo Abeni 			subopt = OPTION_MPTCP_MPC_ACK;
43cc7972eaSChristoph Paasch 		} else {
44fe33d386SPaolo Abeni 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) {
45cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
46fe33d386SPaolo Abeni 				subopt = OPTION_MPTCP_MPC_SYNACK;
47fe33d386SPaolo Abeni 			} else {
48cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
49fe33d386SPaolo Abeni 				subopt = OPTION_MPTCP_MPC_SYN;
50fe33d386SPaolo Abeni 			}
51cc7972eaSChristoph Paasch 		}
52208e8f66SGeliang Tang 
53208e8f66SGeliang Tang 		/* Cfr RFC 8684 Section 3.3.0:
54208e8f66SGeliang Tang 		 * If a checksum is present but its use had
55208e8f66SGeliang Tang 		 * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
56208e8f66SGeliang Tang 		 * close the subflow with a RST, as it is not behaving as negotiated.
57208e8f66SGeliang Tang 		 * If a checksum is not present when its use has been negotiated, the
58208e8f66SGeliang Tang 		 * receiver MUST close the subflow with a RST, as it is considered
59208e8f66SGeliang Tang 		 * broken
60208e8f66SGeliang Tang 		 * We parse even option with mismatching csum presence, so that
61208e8f66SGeliang Tang 		 * later in subflow_data_ready we can trigger the reset.
62208e8f66SGeliang Tang 		 */
63208e8f66SGeliang Tang 		if (opsize != expected_opsize &&
64208e8f66SGeliang Tang 		    (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
65208e8f66SGeliang Tang 		     opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
66eda7acddSPeter Krystad 			break;
67eda7acddSPeter Krystad 
68cc7972eaSChristoph Paasch 		/* try to be gentle vs future versions on the initial syn */
69eda7acddSPeter Krystad 		version = *ptr++ & MPTCP_VERSION_MASK;
70cc7972eaSChristoph Paasch 		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
71eda7acddSPeter Krystad 			if (version != MPTCP_SUPPORTED_VERSION)
72eda7acddSPeter Krystad 				break;
73cc7972eaSChristoph Paasch 		} else if (version < MPTCP_SUPPORTED_VERSION) {
74cc7972eaSChristoph Paasch 			break;
75cc7972eaSChristoph Paasch 		}
76eda7acddSPeter Krystad 
77eda7acddSPeter Krystad 		flags = *ptr++;
7865492c5aSPaolo Abeni 		if (!mptcp_cap_flag_sha256(flags) ||
79eda7acddSPeter Krystad 		    (flags & MPTCP_CAP_EXTENSIBILITY))
80eda7acddSPeter Krystad 			break;
81eda7acddSPeter Krystad 
82eda7acddSPeter Krystad 		/* RFC 6824, Section 3.1:
83eda7acddSPeter Krystad 		 * "For the Checksum Required bit (labeled "A"), if either
84eda7acddSPeter Krystad 		 * host requires the use of checksums, checksums MUST be used.
85eda7acddSPeter Krystad 		 * In other words, the only way for checksums not to be used
86eda7acddSPeter Krystad 		 * is if both hosts in their SYNs set A=0."
87eda7acddSPeter Krystad 		 */
88eda7acddSPeter Krystad 		if (flags & MPTCP_CAP_CHECKSUM_REQD)
8974c7dfbeSPaolo Abeni 			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
90eda7acddSPeter Krystad 
91a086aebaSPaolo Abeni 		mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
92df377be3SGeliang Tang 
93fe33d386SPaolo Abeni 		mp_opt->suboptions |= subopt;
94cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
95eda7acddSPeter Krystad 			mp_opt->sndr_key = get_unaligned_be64(ptr);
96eda7acddSPeter Krystad 			ptr += 8;
97cc7972eaSChristoph Paasch 		}
98cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
99eda7acddSPeter Krystad 			mp_opt->rcvr_key = get_unaligned_be64(ptr);
100eda7acddSPeter Krystad 			ptr += 8;
101eda7acddSPeter Krystad 		}
102208e8f66SGeliang Tang 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
103cc7972eaSChristoph Paasch 			/* Section 3.1.:
104cc7972eaSChristoph Paasch 			 * "the data parameters in a MP_CAPABLE are semantically
105cc7972eaSChristoph Paasch 			 * equivalent to those in a DSS option and can be used
106cc7972eaSChristoph Paasch 			 * interchangeably."
107cc7972eaSChristoph Paasch 			 */
10874c7dfbeSPaolo Abeni 			mp_opt->suboptions |= OPTION_MPTCP_DSS;
109cc7972eaSChristoph Paasch 			mp_opt->use_map = 1;
110cc7972eaSChristoph Paasch 			mp_opt->mpc_map = 1;
111cc7972eaSChristoph Paasch 			mp_opt->data_len = get_unaligned_be16(ptr);
112cc7972eaSChristoph Paasch 			ptr += 2;
113cc7972eaSChristoph Paasch 		}
114208e8f66SGeliang Tang 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
115ba2c89e0SPaolo Abeni 			mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
11674c7dfbeSPaolo Abeni 			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
117208e8f66SGeliang Tang 			ptr += 2;
118208e8f66SGeliang Tang 		}
119208e8f66SGeliang Tang 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u",
120cc7972eaSChristoph Paasch 			 version, flags, opsize, mp_opt->sndr_key,
121208e8f66SGeliang Tang 			 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
122eda7acddSPeter Krystad 		break;
123eda7acddSPeter Krystad 
124f296234cSPeter Krystad 	case MPTCPOPT_MP_JOIN:
12574c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTIONS_MPTCP_MPJ;
126f296234cSPeter Krystad 		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
127f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
128f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
129f296234cSPeter Krystad 			mp_opt->token = get_unaligned_be32(ptr);
130f296234cSPeter Krystad 			ptr += 4;
131f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
132f296234cSPeter Krystad 			ptr += 4;
133f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
134f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
135f296234cSPeter Krystad 				 mp_opt->token, mp_opt->nonce);
136f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
137f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
138f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
139f296234cSPeter Krystad 			mp_opt->thmac = get_unaligned_be64(ptr);
140f296234cSPeter Krystad 			ptr += 8;
141f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
142f296234cSPeter Krystad 			ptr += 4;
143f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
144f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
145f296234cSPeter Krystad 				 mp_opt->thmac, mp_opt->nonce);
146f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
147f296234cSPeter Krystad 			ptr += 2;
148f296234cSPeter Krystad 			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
149f296234cSPeter Krystad 			pr_debug("MP_JOIN hmac");
150f296234cSPeter Krystad 		} else {
15174c7dfbeSPaolo Abeni 			mp_opt->suboptions &= ~OPTIONS_MPTCP_MPJ;
152f296234cSPeter Krystad 		}
153f296234cSPeter Krystad 		break;
154f296234cSPeter Krystad 
155eda7acddSPeter Krystad 	case MPTCPOPT_DSS:
156eda7acddSPeter Krystad 		pr_debug("DSS");
157648ef4b8SMat Martineau 		ptr++;
158648ef4b8SMat Martineau 
159cc7972eaSChristoph Paasch 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
160cc7972eaSChristoph Paasch 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
161cc7972eaSChristoph Paasch 		 * map info accordingly
162cc7972eaSChristoph Paasch 		 */
163cc7972eaSChristoph Paasch 		mp_opt->mpc_map = 0;
164648ef4b8SMat Martineau 		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
165648ef4b8SMat Martineau 		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
166648ef4b8SMat Martineau 		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
167648ef4b8SMat Martineau 		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
168648ef4b8SMat Martineau 		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
169648ef4b8SMat Martineau 		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
170648ef4b8SMat Martineau 
171648ef4b8SMat Martineau 		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
172648ef4b8SMat Martineau 			 mp_opt->data_fin, mp_opt->dsn64,
173648ef4b8SMat Martineau 			 mp_opt->use_map, mp_opt->ack64,
174648ef4b8SMat Martineau 			 mp_opt->use_ack);
175648ef4b8SMat Martineau 
176648ef4b8SMat Martineau 		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
177648ef4b8SMat Martineau 
178648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
179648ef4b8SMat Martineau 			if (mp_opt->ack64)
180648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
181648ef4b8SMat Martineau 			else
182648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
183648ef4b8SMat Martineau 		}
184648ef4b8SMat Martineau 
185648ef4b8SMat Martineau 		if (mp_opt->use_map) {
186648ef4b8SMat Martineau 			if (mp_opt->dsn64)
187648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
188648ef4b8SMat Martineau 			else
189648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
190648ef4b8SMat Martineau 		}
191648ef4b8SMat Martineau 
192390b95a5SGeliang Tang 		/* Always parse any csum presence combination, we will enforce
193390b95a5SGeliang Tang 		 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
194648ef4b8SMat Martineau 		 */
195648ef4b8SMat Martineau 		if (opsize != expected_opsize &&
196648ef4b8SMat Martineau 		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
197648ef4b8SMat Martineau 			break;
198648ef4b8SMat Martineau 
19974c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_DSS;
200648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
201648ef4b8SMat Martineau 			if (mp_opt->ack64) {
202648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be64(ptr);
203648ef4b8SMat Martineau 				ptr += 8;
204648ef4b8SMat Martineau 			} else {
205648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be32(ptr);
206648ef4b8SMat Martineau 				ptr += 4;
207648ef4b8SMat Martineau 			}
208648ef4b8SMat Martineau 
209648ef4b8SMat Martineau 			pr_debug("data_ack=%llu", mp_opt->data_ack);
210648ef4b8SMat Martineau 		}
211648ef4b8SMat Martineau 
212648ef4b8SMat Martineau 		if (mp_opt->use_map) {
213648ef4b8SMat Martineau 			if (mp_opt->dsn64) {
214648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be64(ptr);
215648ef4b8SMat Martineau 				ptr += 8;
216648ef4b8SMat Martineau 			} else {
217648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be32(ptr);
218648ef4b8SMat Martineau 				ptr += 4;
219648ef4b8SMat Martineau 			}
220648ef4b8SMat Martineau 
221648ef4b8SMat Martineau 			mp_opt->subflow_seq = get_unaligned_be32(ptr);
222648ef4b8SMat Martineau 			ptr += 4;
223648ef4b8SMat Martineau 
224648ef4b8SMat Martineau 			mp_opt->data_len = get_unaligned_be16(ptr);
225648ef4b8SMat Martineau 			ptr += 2;
226648ef4b8SMat Martineau 
227390b95a5SGeliang Tang 			if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
22874c7dfbeSPaolo Abeni 				mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
229ba2c89e0SPaolo Abeni 				mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
230390b95a5SGeliang Tang 				ptr += 2;
231390b95a5SGeliang Tang 			}
232390b95a5SGeliang Tang 
233390b95a5SGeliang Tang 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u",
234648ef4b8SMat Martineau 				 mp_opt->data_seq, mp_opt->subflow_seq,
23574c7dfbeSPaolo Abeni 				 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
23674c7dfbeSPaolo Abeni 				 mp_opt->csum);
237648ef4b8SMat Martineau 		}
238648ef4b8SMat Martineau 
239eda7acddSPeter Krystad 		break;
240eda7acddSPeter Krystad 
2413df523abSPeter Krystad 	case MPTCPOPT_ADD_ADDR:
2423df523abSPeter Krystad 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
2433df523abSPeter Krystad 		if (!mp_opt->echo) {
2443df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
2453df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
2461b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET;
2473df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2483df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
2493df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
2501b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET6;
2513df523abSPeter Krystad #endif
2523df523abSPeter Krystad 			else
2533df523abSPeter Krystad 				break;
2543df523abSPeter Krystad 		} else {
2553df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
2563df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
2571b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET;
2583df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2593df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
2603df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
2611b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET6;
2623df523abSPeter Krystad #endif
2633df523abSPeter Krystad 			else
2643df523abSPeter Krystad 				break;
2653df523abSPeter Krystad 		}
2663df523abSPeter Krystad 
26774c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
268f7dafee1SGeliang Tang 		mp_opt->addr.id = *ptr++;
269a086aebaSPaolo Abeni 		mp_opt->addr.port = 0;
270a086aebaSPaolo Abeni 		mp_opt->ahmac = 0;
2711b1a6ef5SGeliang Tang 		if (mp_opt->addr.family == AF_INET) {
272f7dafee1SGeliang Tang 			memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
2733df523abSPeter Krystad 			ptr += 4;
2743df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
2753df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
276f7dafee1SGeliang Tang 				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
2773df523abSPeter Krystad 				ptr += 2;
2783df523abSPeter Krystad 			}
2793df523abSPeter Krystad 		}
2803df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2813df523abSPeter Krystad 		else {
282f7dafee1SGeliang Tang 			memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
2833df523abSPeter Krystad 			ptr += 16;
2843df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
2853df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
286f7dafee1SGeliang Tang 				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
2873df523abSPeter Krystad 				ptr += 2;
2883df523abSPeter Krystad 			}
2893df523abSPeter Krystad 		}
2903df523abSPeter Krystad #endif
2913df523abSPeter Krystad 		if (!mp_opt->echo) {
2923df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
2933df523abSPeter Krystad 			ptr += 8;
2943df523abSPeter Krystad 		}
29590a4aea8SGeliang Tang 		pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
2961b1a6ef5SGeliang Tang 			 (mp_opt->addr.family == AF_INET6) ? "6" : "",
297f7dafee1SGeliang Tang 			 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
2983df523abSPeter Krystad 		break;
2993df523abSPeter Krystad 
3003df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
3015c4a824dSGeliang Tang 		if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
3025c4a824dSGeliang Tang 		    opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
3033df523abSPeter Krystad 			break;
3043df523abSPeter Krystad 
3058e60eed6SGeliang Tang 		ptr++;
3068e60eed6SGeliang Tang 
30774c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
3085c4a824dSGeliang Tang 		mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
3095c4a824dSGeliang Tang 		for (i = 0; i < mp_opt->rm_list.nr; i++)
3105c4a824dSGeliang Tang 			mp_opt->rm_list.ids[i] = *ptr++;
3115c4a824dSGeliang Tang 		pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
3123df523abSPeter Krystad 		break;
3133df523abSPeter Krystad 
31440453a5cSGeliang Tang 	case MPTCPOPT_MP_PRIO:
31540453a5cSGeliang Tang 		if (opsize != TCPOLEN_MPTCP_PRIO)
31640453a5cSGeliang Tang 			break;
31740453a5cSGeliang Tang 
31874c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_PRIO;
31940453a5cSGeliang Tang 		mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
32040453a5cSGeliang Tang 		pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
32140453a5cSGeliang Tang 		break;
32240453a5cSGeliang Tang 
32350c504a2SFlorian Westphal 	case MPTCPOPT_MP_FASTCLOSE:
32450c504a2SFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
32550c504a2SFlorian Westphal 			break;
32650c504a2SFlorian Westphal 
32750c504a2SFlorian Westphal 		ptr += 2;
32850c504a2SFlorian Westphal 		mp_opt->rcvr_key = get_unaligned_be64(ptr);
32950c504a2SFlorian Westphal 		ptr += 8;
33074c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
3311e75629cSGeliang Tang 		pr_debug("MP_FASTCLOSE: recv_key=%llu", mp_opt->rcvr_key);
33250c504a2SFlorian Westphal 		break;
33350c504a2SFlorian Westphal 
334dc87efdbSFlorian Westphal 	case MPTCPOPT_RST:
335dc87efdbSFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_RST)
336dc87efdbSFlorian Westphal 			break;
337dc87efdbSFlorian Westphal 
338dc87efdbSFlorian Westphal 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
339dc87efdbSFlorian Westphal 			break;
34074c7dfbeSPaolo Abeni 
34174c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_RST;
342dc87efdbSFlorian Westphal 		flags = *ptr++;
343dc87efdbSFlorian Westphal 		mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
344dc87efdbSFlorian Westphal 		mp_opt->reset_reason = *ptr;
3459ddd1cacSGeliang Tang 		pr_debug("MP_RST: transient=%u reason=%u",
3469ddd1cacSGeliang Tang 			 mp_opt->reset_transient, mp_opt->reset_reason);
347dc87efdbSFlorian Westphal 		break;
348dc87efdbSFlorian Westphal 
3495580d41bSGeliang Tang 	case MPTCPOPT_MP_FAIL:
3505580d41bSGeliang Tang 		if (opsize != TCPOLEN_MPTCP_FAIL)
3515580d41bSGeliang Tang 			break;
3525580d41bSGeliang Tang 
3535580d41bSGeliang Tang 		ptr += 2;
35474c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_FAIL;
3555580d41bSGeliang Tang 		mp_opt->fail_seq = get_unaligned_be64(ptr);
3565580d41bSGeliang Tang 		pr_debug("MP_FAIL: data_seq=%llu", mp_opt->fail_seq);
3575580d41bSGeliang Tang 		break;
3585580d41bSGeliang Tang 
359eda7acddSPeter Krystad 	default:
360eda7acddSPeter Krystad 		break;
361eda7acddSPeter Krystad 	}
362eda7acddSPeter Krystad }
363eda7acddSPeter Krystad 
3640799e21bSGeliang Tang void mptcp_get_options(const struct sk_buff *skb,
365cfde141eSPaolo Abeni 		       struct mptcp_options_received *mp_opt)
366cec37a6eSPeter Krystad {
367cec37a6eSPeter Krystad 	const struct tcphdr *th = tcp_hdr(skb);
368cfde141eSPaolo Abeni 	const unsigned char *ptr;
369cfde141eSPaolo Abeni 	int length;
370cec37a6eSPeter Krystad 
371cfde141eSPaolo Abeni 	/* initialize option status */
37274c7dfbeSPaolo Abeni 	mp_opt->suboptions = 0;
373cfde141eSPaolo Abeni 
374cfde141eSPaolo Abeni 	length = (th->doff * 4) - sizeof(struct tcphdr);
375cec37a6eSPeter Krystad 	ptr = (const unsigned char *)(th + 1);
376cec37a6eSPeter Krystad 
377cec37a6eSPeter Krystad 	while (length > 0) {
378cec37a6eSPeter Krystad 		int opcode = *ptr++;
379cec37a6eSPeter Krystad 		int opsize;
380cec37a6eSPeter Krystad 
381cec37a6eSPeter Krystad 		switch (opcode) {
382cec37a6eSPeter Krystad 		case TCPOPT_EOL:
383cec37a6eSPeter Krystad 			return;
384cec37a6eSPeter Krystad 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
385cec37a6eSPeter Krystad 			length--;
386cec37a6eSPeter Krystad 			continue;
387cec37a6eSPeter Krystad 		default:
38807718be2SMaxim Mikityanskiy 			if (length < 2)
38907718be2SMaxim Mikityanskiy 				return;
390cec37a6eSPeter Krystad 			opsize = *ptr++;
391cec37a6eSPeter Krystad 			if (opsize < 2) /* "silly options" */
392cec37a6eSPeter Krystad 				return;
393cec37a6eSPeter Krystad 			if (opsize > length)
394cec37a6eSPeter Krystad 				return;	/* don't parse partial options */
395cec37a6eSPeter Krystad 			if (opcode == TCPOPT_MPTCP)
396cfde141eSPaolo Abeni 				mptcp_parse_option(skb, ptr, opsize, mp_opt);
397cec37a6eSPeter Krystad 			ptr += opsize - 2;
398cec37a6eSPeter Krystad 			length -= opsize;
399cec37a6eSPeter Krystad 		}
400cec37a6eSPeter Krystad 	}
401cec37a6eSPeter Krystad }
402cec37a6eSPeter Krystad 
403cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
404cc7972eaSChristoph Paasch 		       unsigned int *size, struct mptcp_out_options *opts)
405cec37a6eSPeter Krystad {
406cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
407cec37a6eSPeter Krystad 
408cc7972eaSChristoph Paasch 	/* we will use snd_isn to detect first pkt [re]transmission
409cc7972eaSChristoph Paasch 	 * in mptcp_established_options_mp()
410cc7972eaSChristoph Paasch 	 */
411cc7972eaSChristoph Paasch 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
412cec37a6eSPeter Krystad 	if (subflow->request_mptcp) {
413cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
41406fe1719SGeliang Tang 		opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
415bab6b88eSGeliang Tang 		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
416cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYN;
417cec37a6eSPeter Krystad 		return true;
418ec3edaa7SPeter Krystad 	} else if (subflow->request_join) {
419ec3edaa7SPeter Krystad 		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
420ec3edaa7SPeter Krystad 			 subflow->local_nonce);
421ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
422ec3edaa7SPeter Krystad 		opts->join_id = subflow->local_id;
423ec3edaa7SPeter Krystad 		opts->token = subflow->remote_token;
424ec3edaa7SPeter Krystad 		opts->nonce = subflow->local_nonce;
425ec3edaa7SPeter Krystad 		opts->backup = subflow->request_bkup;
426ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYN;
427ec3edaa7SPeter Krystad 		return true;
428cec37a6eSPeter Krystad 	}
429cec37a6eSPeter Krystad 	return false;
430cec37a6eSPeter Krystad }
431cec37a6eSPeter Krystad 
432ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk)
433ec3edaa7SPeter Krystad {
434ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
435ec3edaa7SPeter Krystad 
436ec3edaa7SPeter Krystad 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
437ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = 0;
438ec3edaa7SPeter Krystad 	icsk->icsk_ack.ato = 0;
439ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
440ec3edaa7SPeter Krystad }
441ec3edaa7SPeter Krystad 
442cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
443d87903b6SPaolo Abeni 					 bool snd_data_fin_enable,
444cc7972eaSChristoph Paasch 					 unsigned int *size,
4456d0060f6SMat Martineau 					 unsigned int remaining,
446cec37a6eSPeter Krystad 					 struct mptcp_out_options *opts)
447cec37a6eSPeter Krystad {
448cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
44906fe1719SGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
450cc7972eaSChristoph Paasch 	struct mptcp_ext *mpext;
451cc7972eaSChristoph Paasch 	unsigned int data_len;
452c94b1f96SGeliang Tang 	u8 len;
453cec37a6eSPeter Krystad 
454ec3edaa7SPeter Krystad 	/* When skb is not available, we better over-estimate the emitted
455ec3edaa7SPeter Krystad 	 * options len. A full DSS option (28 bytes) is longer than
456ec3edaa7SPeter Krystad 	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
457ec3edaa7SPeter Krystad 	 * tell the caller to defer the estimate to
458ec3edaa7SPeter Krystad 	 * mptcp_established_options_dss(), which will reserve enough space.
459cc7972eaSChristoph Paasch 	 */
460ec3edaa7SPeter Krystad 	if (!skb)
461ec3edaa7SPeter Krystad 		return false;
462ec3edaa7SPeter Krystad 
463d87903b6SPaolo Abeni 	/* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
464d87903b6SPaolo Abeni 	if (subflow->fully_established || snd_data_fin_enable ||
465d87903b6SPaolo Abeni 	    subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
466d87903b6SPaolo Abeni 	    sk->sk_state != TCP_ESTABLISHED)
467ec3edaa7SPeter Krystad 		return false;
468ec3edaa7SPeter Krystad 
469ec3edaa7SPeter Krystad 	if (subflow->mp_capable) {
470cc7972eaSChristoph Paasch 		mpext = mptcp_get_ext(skb);
471cc7972eaSChristoph Paasch 		data_len = mpext ? mpext->data_len : 0;
472cc7972eaSChristoph Paasch 
473f7cc8890SDavide Caratti 		/* we will check ops->data_len in mptcp_write_options() to
474cc7972eaSChristoph Paasch 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
475cc7972eaSChristoph Paasch 		 * TCPOLEN_MPTCP_MPC_ACK
476cc7972eaSChristoph Paasch 		 */
477f7cc8890SDavide Caratti 		opts->data_len = data_len;
478cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
479cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
480cec37a6eSPeter Krystad 		opts->rcvr_key = subflow->remote_key;
48106fe1719SGeliang Tang 		opts->csum_reqd = READ_ONCE(msk->csum_enabled);
482bab6b88eSGeliang Tang 		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
483cc7972eaSChristoph Paasch 
484cc7972eaSChristoph Paasch 		/* Section 3.1.
485cc7972eaSChristoph Paasch 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
486cc7972eaSChristoph Paasch 		 * packets that start the first subflow of an MPTCP connection,
487cc7972eaSChristoph Paasch 		 * as well as the first packet that carries data
488cc7972eaSChristoph Paasch 		 */
489c94b1f96SGeliang Tang 		if (data_len > 0) {
490c94b1f96SGeliang Tang 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
491c94b1f96SGeliang Tang 			if (opts->csum_reqd) {
492c5b39e26SGeliang Tang 				/* we need to propagate more info to csum the pseudo hdr */
493f7cc8890SDavide Caratti 				opts->data_seq = mpext->data_seq;
494f7cc8890SDavide Caratti 				opts->subflow_seq = mpext->subflow_seq;
495f7cc8890SDavide Caratti 				opts->csum = mpext->csum;
496c94b1f96SGeliang Tang 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
497c94b1f96SGeliang Tang 			}
498c94b1f96SGeliang Tang 			*size = ALIGN(len, 4);
499c94b1f96SGeliang Tang 		} else {
500cec37a6eSPeter Krystad 			*size = TCPOLEN_MPTCP_MPC_ACK;
501c94b1f96SGeliang Tang 		}
502cc7972eaSChristoph Paasch 
503cc7972eaSChristoph Paasch 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
504cc7972eaSChristoph Paasch 			 subflow, subflow->local_key, subflow->remote_key,
505cc7972eaSChristoph Paasch 			 data_len);
506cc7972eaSChristoph Paasch 
507cec37a6eSPeter Krystad 		return true;
508ec3edaa7SPeter Krystad 	} else if (subflow->mp_join) {
509ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
510ec3edaa7SPeter Krystad 		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
511ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_ACK;
512ec3edaa7SPeter Krystad 		pr_debug("subflow=%p", subflow);
513ec3edaa7SPeter Krystad 
514bcd97734SPaolo Abeni 		/* we can use the full delegate action helper only from BH context
515bcd97734SPaolo Abeni 		 * If we are in process context - sk is flushing the backlog at
516bcd97734SPaolo Abeni 		 * socket lock release time - just set the appropriate flag, will
517bcd97734SPaolo Abeni 		 * be handled by the release callback
518bcd97734SPaolo Abeni 		 */
519bcd97734SPaolo Abeni 		if (sock_owned_by_user(sk))
520bcd97734SPaolo Abeni 			set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
521bcd97734SPaolo Abeni 		else
522bcd97734SPaolo Abeni 			mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
523ec3edaa7SPeter Krystad 		return true;
524cec37a6eSPeter Krystad 	}
525cec37a6eSPeter Krystad 	return false;
526cec37a6eSPeter Krystad }
527cec37a6eSPeter Krystad 
5286d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
5299c29e361SPaolo Abeni 				 struct sk_buff *skb, struct mptcp_ext *ext)
5306d0060f6SMat Martineau {
531017512a0SPaolo Abeni 	/* The write_seq value has already been incremented, so the actual
532017512a0SPaolo Abeni 	 * sequence number for the DATA_FIN is one less.
533017512a0SPaolo Abeni 	 */
534017512a0SPaolo Abeni 	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
5357279da61SMat Martineau 
5369c29e361SPaolo Abeni 	if (!ext->use_map || !skb->len) {
5376d0060f6SMat Martineau 		/* RFC6824 requires a DSS mapping with specific values
5386d0060f6SMat Martineau 		 * if DATA_FIN is set but no data payload is mapped
5396d0060f6SMat Martineau 		 */
5406d37a0b8SMat Martineau 		ext->data_fin = 1;
5416d0060f6SMat Martineau 		ext->use_map = 1;
5426d0060f6SMat Martineau 		ext->dsn64 = 1;
543017512a0SPaolo Abeni 		ext->data_seq = data_fin_tx_seq;
5446d0060f6SMat Martineau 		ext->subflow_seq = 0;
5456d0060f6SMat Martineau 		ext->data_len = 1;
5467279da61SMat Martineau 	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
5476d37a0b8SMat Martineau 		/* If there's an existing DSS mapping and it is the
5486d37a0b8SMat Martineau 		 * final mapping, DATA_FIN consumes 1 additional byte of
5496d37a0b8SMat Martineau 		 * mapping space.
5506d0060f6SMat Martineau 		 */
5516d37a0b8SMat Martineau 		ext->data_fin = 1;
5526d0060f6SMat Martineau 		ext->data_len++;
5536d0060f6SMat Martineau 	}
5546d0060f6SMat Martineau }
5556d0060f6SMat Martineau 
5566d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
557d87903b6SPaolo Abeni 					  bool snd_data_fin_enable,
5586d0060f6SMat Martineau 					  unsigned int *size,
5596d0060f6SMat Martineau 					  unsigned int remaining,
5606d0060f6SMat Martineau 					  struct mptcp_out_options *opts)
5616d0060f6SMat Martineau {
5626d0060f6SMat Martineau 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
5637279da61SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
5646d0060f6SMat Martineau 	unsigned int dss_size = 0;
5656d0060f6SMat Martineau 	struct mptcp_ext *mpext;
5666d0060f6SMat Martineau 	unsigned int ack_size;
567d22f4988SChristoph Paasch 	bool ret = false;
568d87903b6SPaolo Abeni 	u64 ack_seq;
5696d0060f6SMat Martineau 
570c5b39e26SGeliang Tang 	opts->csum_reqd = READ_ONCE(msk->csum_enabled);
5710bac966aSMat Martineau 	mpext = skb ? mptcp_get_ext(skb) : NULL;
5726d0060f6SMat Martineau 
5737279da61SMat Martineau 	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
574c5b39e26SGeliang Tang 		unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
5756d0060f6SMat Martineau 
576c5b39e26SGeliang Tang 		if (mpext) {
577c5b39e26SGeliang Tang 			if (opts->csum_reqd)
578c5b39e26SGeliang Tang 				map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
579c5b39e26SGeliang Tang 
580c5b39e26SGeliang Tang 			opts->ext_copy = *mpext;
581c5b39e26SGeliang Tang 		}
5826d0060f6SMat Martineau 
5836d0060f6SMat Martineau 		remaining -= map_size;
5846d0060f6SMat Martineau 		dss_size = map_size;
5857279da61SMat Martineau 		if (skb && snd_data_fin_enable)
5869c29e361SPaolo Abeni 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
5871bff1e43SPaolo Abeni 		opts->suboptions = OPTION_MPTCP_DSS;
588d22f4988SChristoph Paasch 		ret = true;
589d22f4988SChristoph Paasch 	}
590d22f4988SChristoph Paasch 
5912398e399SPaolo Abeni 	/* passive sockets msk will set the 'can_ack' after accept(), even
5922398e399SPaolo Abeni 	 * if the first subflow may have the already the remote key handy
5932398e399SPaolo Abeni 	 */
594d22f4988SChristoph Paasch 	opts->ext_copy.use_ack = 0;
595dc093db5SPaolo Abeni 	if (!READ_ONCE(msk->can_ack)) {
596d22f4988SChristoph Paasch 		*size = ALIGN(dss_size, 4);
597d22f4988SChristoph Paasch 		return ret;
5986d0060f6SMat Martineau 	}
5996d0060f6SMat Martineau 
600e3859603SPaolo Abeni 	ack_seq = READ_ONCE(msk->ack_seq);
60137198e93SDavide Caratti 	if (READ_ONCE(msk->use_64bit_ack)) {
6026d0060f6SMat Martineau 		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
603e3859603SPaolo Abeni 		opts->ext_copy.data_ack = ack_seq;
604a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 1;
605a0c1d0eaSChristoph Paasch 	} else {
606a0c1d0eaSChristoph Paasch 		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
607e3859603SPaolo Abeni 		opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
608a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 0;
609a0c1d0eaSChristoph Paasch 	}
610a0c1d0eaSChristoph Paasch 	opts->ext_copy.use_ack = 1;
6111bff1e43SPaolo Abeni 	opts->suboptions = OPTION_MPTCP_DSS;
612ea4ca586SPaolo Abeni 	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
6136d0060f6SMat Martineau 
6146d0060f6SMat Martineau 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
6156d0060f6SMat Martineau 	if (dss_size == 0)
6166d0060f6SMat Martineau 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
6176d0060f6SMat Martineau 
6186d0060f6SMat Martineau 	dss_size += ack_size;
6196d0060f6SMat Martineau 
6206d0060f6SMat Martineau 	*size = ALIGN(dss_size, 4);
6216d0060f6SMat Martineau 	return true;
6226d0060f6SMat Martineau }
6236d0060f6SMat Martineau 
624761c124eSGeliang Tang static u64 add_addr_generate_hmac(u64 key1, u64 key2,
625761c124eSGeliang Tang 				  struct mptcp_addr_info *addr)
6263df523abSPeter Krystad {
627761c124eSGeliang Tang 	u16 port = ntohs(addr->port);
628bd697222STodd Malsbary 	u8 hmac[SHA256_DIGEST_SIZE];
6293df523abSPeter Krystad 	u8 msg[19];
630761c124eSGeliang Tang 	int i = 0;
6313df523abSPeter Krystad 
632761c124eSGeliang Tang 	msg[i++] = addr->id;
633761c124eSGeliang Tang 	if (addr->family == AF_INET) {
634761c124eSGeliang Tang 		memcpy(&msg[i], &addr->addr.s_addr, 4);
635761c124eSGeliang Tang 		i += 4;
636761c124eSGeliang Tang 	}
637761c124eSGeliang Tang #if IS_ENABLED(CONFIG_MPTCP_IPV6)
638761c124eSGeliang Tang 	else if (addr->family == AF_INET6) {
639761c124eSGeliang Tang 		memcpy(&msg[i], &addr->addr6.s6_addr, 16);
640761c124eSGeliang Tang 		i += 16;
641761c124eSGeliang Tang 	}
642761c124eSGeliang Tang #endif
643761c124eSGeliang Tang 	msg[i++] = port >> 8;
644761c124eSGeliang Tang 	msg[i++] = port & 0xFF;
6453df523abSPeter Krystad 
646761c124eSGeliang Tang 	mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
6473df523abSPeter Krystad 
648bd697222STodd Malsbary 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
6493df523abSPeter Krystad }
6503df523abSPeter Krystad 
65184dfe367SGeliang Tang static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
6523df523abSPeter Krystad 					       unsigned int *size,
6533df523abSPeter Krystad 					       unsigned int remaining,
6543df523abSPeter Krystad 					       struct mptcp_out_options *opts)
6553df523abSPeter Krystad {
6563df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
6573df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
65884dfe367SGeliang Tang 	bool drop_other_suboptions = false;
65984dfe367SGeliang Tang 	unsigned int opt_size = *size;
6606a6c05a8SGeliang Tang 	bool echo;
6611b1c7a0eSPeter Krystad 	int len;
6623df523abSPeter Krystad 
6631f5e9e2fSYonglong Li 	/* add addr will strip the existing options, be sure to avoid breaking
6641f5e9e2fSYonglong Li 	 * MPC/MPJ handshakes
6651f5e9e2fSYonglong Li 	 */
666f643b803SGeliang Tang 	if (!mptcp_pm_should_add_signal(msk) ||
6671f5e9e2fSYonglong Li 	    (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
6681f5e9e2fSYonglong Li 	    !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
669af7939f3SGeliang Tang 		    &echo, &drop_other_suboptions))
6703df523abSPeter Krystad 		return false;
6711b1c7a0eSPeter Krystad 
6721f5e9e2fSYonglong Li 	if (drop_other_suboptions)
6731f5e9e2fSYonglong Li 		remaining += opt_size;
674af7939f3SGeliang Tang 	len = mptcp_add_addr_len(opts->addr.family, echo, !!opts->addr.port);
6751b1c7a0eSPeter Krystad 	if (remaining < len)
6761b1c7a0eSPeter Krystad 		return false;
6771b1c7a0eSPeter Krystad 
6781b1c7a0eSPeter Krystad 	*size = len;
6791f5e9e2fSYonglong Li 	if (drop_other_suboptions) {
6801f5e9e2fSYonglong Li 		pr_debug("drop other suboptions");
6811f5e9e2fSYonglong Li 		opts->suboptions = 0;
6821bff1e43SPaolo Abeni 
6831bff1e43SPaolo Abeni 		/* note that e.g. DSS could have written into the memory
6841bff1e43SPaolo Abeni 		 * aliased by ahmac, we must reset the field here
6851bff1e43SPaolo Abeni 		 * to avoid appending the hmac even for ADD_ADDR echo
6861bff1e43SPaolo Abeni 		 * options
6871bff1e43SPaolo Abeni 		 */
6881bff1e43SPaolo Abeni 		opts->ahmac = 0;
68984dfe367SGeliang Tang 		*size -= opt_size;
6901f5e9e2fSYonglong Li 	}
6913df523abSPeter Krystad 	opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
6926a6c05a8SGeliang Tang 	if (!echo) {
6933df523abSPeter Krystad 		opts->ahmac = add_addr_generate_hmac(msk->local_key,
6943df523abSPeter Krystad 						     msk->remote_key,
695761c124eSGeliang Tang 						     &opts->addr);
6963df523abSPeter Krystad 	}
6974a2777a8SGeliang Tang 	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
69830f60baeSGeliang Tang 		 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
6993df523abSPeter Krystad 
7003df523abSPeter Krystad 	return true;
7013df523abSPeter Krystad }
7023df523abSPeter Krystad 
7035cb104aeSGeliang Tang static bool mptcp_established_options_rm_addr(struct sock *sk,
7045cb104aeSGeliang Tang 					      unsigned int *size,
7055cb104aeSGeliang Tang 					      unsigned int remaining,
7065cb104aeSGeliang Tang 					      struct mptcp_out_options *opts)
7075cb104aeSGeliang Tang {
7085cb104aeSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
7095cb104aeSGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
7106445e17aSGeliang Tang 	struct mptcp_rm_list rm_list;
7116445e17aSGeliang Tang 	int i, len;
7125cb104aeSGeliang Tang 
7135cb104aeSGeliang Tang 	if (!mptcp_pm_should_rm_signal(msk) ||
7146445e17aSGeliang Tang 	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
7155cb104aeSGeliang Tang 		return false;
7165cb104aeSGeliang Tang 
7176445e17aSGeliang Tang 	len = mptcp_rm_addr_len(&rm_list);
7186445e17aSGeliang Tang 	if (len < 0)
7196445e17aSGeliang Tang 		return false;
7206445e17aSGeliang Tang 	if (remaining < len)
7215cb104aeSGeliang Tang 		return false;
7225cb104aeSGeliang Tang 
7236445e17aSGeliang Tang 	*size = len;
7245cb104aeSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
7256445e17aSGeliang Tang 	opts->rm_list = rm_list;
7265cb104aeSGeliang Tang 
7276445e17aSGeliang Tang 	for (i = 0; i < opts->rm_list.nr; i++)
7286445e17aSGeliang Tang 		pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
7295cb104aeSGeliang Tang 
7305cb104aeSGeliang Tang 	return true;
7315cb104aeSGeliang Tang }
7325cb104aeSGeliang Tang 
73306706542SGeliang Tang static bool mptcp_established_options_mp_prio(struct sock *sk,
73406706542SGeliang Tang 					      unsigned int *size,
73506706542SGeliang Tang 					      unsigned int remaining,
73606706542SGeliang Tang 					      struct mptcp_out_options *opts)
73706706542SGeliang Tang {
73806706542SGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
73906706542SGeliang Tang 
7401bff1e43SPaolo Abeni 	/* can't send MP_PRIO with MPC, as they share the same option space:
7411bff1e43SPaolo Abeni 	 * 'backup'. Also it makes no sense at all
7421bff1e43SPaolo Abeni 	 */
74313ac17a3SGeliang Tang 	if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
74406706542SGeliang Tang 		return false;
74506706542SGeliang Tang 
746ec99a470SDavide Caratti 	/* account for the trailing 'nop' option */
747ec99a470SDavide Caratti 	if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
74806706542SGeliang Tang 		return false;
74906706542SGeliang Tang 
750ec99a470SDavide Caratti 	*size = TCPOLEN_MPTCP_PRIO_ALIGN;
75106706542SGeliang Tang 	opts->suboptions |= OPTION_MPTCP_PRIO;
75206706542SGeliang Tang 	opts->backup = subflow->request_bkup;
75306706542SGeliang Tang 
75406706542SGeliang Tang 	pr_debug("prio=%d", opts->backup);
75506706542SGeliang Tang 
75606706542SGeliang Tang 	return true;
75706706542SGeliang Tang }
75806706542SGeliang Tang 
759c25aeb4eSGeliang Tang static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
760dc87efdbSFlorian Westphal 						   unsigned int *size,
761dc87efdbSFlorian Westphal 						   unsigned int remaining,
762dc87efdbSFlorian Westphal 						   struct mptcp_out_options *opts)
763dc87efdbSFlorian Westphal {
764dc87efdbSFlorian Westphal 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
765dc87efdbSFlorian Westphal 
766dc87efdbSFlorian Westphal 	if (remaining < TCPOLEN_MPTCP_RST)
767c25aeb4eSGeliang Tang 		return false;
768dc87efdbSFlorian Westphal 
769dc87efdbSFlorian Westphal 	*size = TCPOLEN_MPTCP_RST;
770dc87efdbSFlorian Westphal 	opts->suboptions |= OPTION_MPTCP_RST;
771dc87efdbSFlorian Westphal 	opts->reset_transient = subflow->reset_transient;
772dc87efdbSFlorian Westphal 	opts->reset_reason = subflow->reset_reason;
7730c1f78a4SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
774c25aeb4eSGeliang Tang 
775c25aeb4eSGeliang Tang 	return true;
776c25aeb4eSGeliang Tang }
777c25aeb4eSGeliang Tang 
778f284c0c7SPaolo Abeni static bool mptcp_established_options_fastclose(struct sock *sk,
779f284c0c7SPaolo Abeni 						unsigned int *size,
780f284c0c7SPaolo Abeni 						unsigned int remaining,
781f284c0c7SPaolo Abeni 						struct mptcp_out_options *opts)
782f284c0c7SPaolo Abeni {
783f284c0c7SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
784f284c0c7SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
785f284c0c7SPaolo Abeni 
786f284c0c7SPaolo Abeni 	if (likely(!subflow->send_fastclose))
787f284c0c7SPaolo Abeni 		return false;
788f284c0c7SPaolo Abeni 
789f284c0c7SPaolo Abeni 	if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
790f284c0c7SPaolo Abeni 		return false;
791f284c0c7SPaolo Abeni 
792f284c0c7SPaolo Abeni 	*size = TCPOLEN_MPTCP_FASTCLOSE;
793f284c0c7SPaolo Abeni 	opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
794f284c0c7SPaolo Abeni 	opts->rcvr_key = msk->remote_key;
795f284c0c7SPaolo Abeni 
796f284c0c7SPaolo Abeni 	pr_debug("FASTCLOSE key=%llu", opts->rcvr_key);
7970c1f78a4SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
798f284c0c7SPaolo Abeni 	return true;
799f284c0c7SPaolo Abeni }
800f284c0c7SPaolo Abeni 
801c25aeb4eSGeliang Tang static bool mptcp_established_options_mp_fail(struct sock *sk,
802c25aeb4eSGeliang Tang 					      unsigned int *size,
803c25aeb4eSGeliang Tang 					      unsigned int remaining,
804c25aeb4eSGeliang Tang 					      struct mptcp_out_options *opts)
805c25aeb4eSGeliang Tang {
806c25aeb4eSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
807c25aeb4eSGeliang Tang 
808c25aeb4eSGeliang Tang 	if (likely(!subflow->send_mp_fail))
809c25aeb4eSGeliang Tang 		return false;
810c25aeb4eSGeliang Tang 
811c25aeb4eSGeliang Tang 	if (remaining < TCPOLEN_MPTCP_FAIL)
812c25aeb4eSGeliang Tang 		return false;
813c25aeb4eSGeliang Tang 
814c25aeb4eSGeliang Tang 	*size = TCPOLEN_MPTCP_FAIL;
815c25aeb4eSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_FAIL;
816c25aeb4eSGeliang Tang 	opts->fail_seq = subflow->map_seq;
817c25aeb4eSGeliang Tang 
818c25aeb4eSGeliang Tang 	pr_debug("MP_FAIL fail_seq=%llu", opts->fail_seq);
8190c1f78a4SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
820c25aeb4eSGeliang Tang 
821c25aeb4eSGeliang Tang 	return true;
822dc87efdbSFlorian Westphal }
823dc87efdbSFlorian Westphal 
8246d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
8256d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
8266d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
8276d0060f6SMat Martineau {
828d87903b6SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
829d87903b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
8306d0060f6SMat Martineau 	unsigned int opt_size = 0;
831d87903b6SPaolo Abeni 	bool snd_data_fin;
8326d0060f6SMat Martineau 	bool ret = false;
8336d0060f6SMat Martineau 
8343df523abSPeter Krystad 	opts->suboptions = 0;
8353df523abSPeter Krystad 
8361e39e5a3SGeliang Tang 	if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
837e1ff9e82SDavide Caratti 		return false;
838e1ff9e82SDavide Caratti 
839dc87efdbSFlorian Westphal 	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
840f284c0c7SPaolo Abeni 		if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
841f284c0c7SPaolo Abeni 		    mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
842c25aeb4eSGeliang Tang 			*size += opt_size;
843c25aeb4eSGeliang Tang 			remaining -= opt_size;
844c25aeb4eSGeliang Tang 		}
845f284c0c7SPaolo Abeni 		/* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
846c25aeb4eSGeliang Tang 		if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
847c25aeb4eSGeliang Tang 			*size += opt_size;
848c25aeb4eSGeliang Tang 			remaining -= opt_size;
849c25aeb4eSGeliang Tang 		}
850dc87efdbSFlorian Westphal 		return true;
851dc87efdbSFlorian Westphal 	}
852d5824847SPaolo Abeni 
853d87903b6SPaolo Abeni 	snd_data_fin = mptcp_data_fin_enabled(msk);
854d87903b6SPaolo Abeni 	if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
8556d0060f6SMat Martineau 		ret = true;
856c25aeb4eSGeliang Tang 	else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts)) {
85704fac2caSMatthieu Baerts 		unsigned int mp_fail_size;
85804fac2caSMatthieu Baerts 
8596d0060f6SMat Martineau 		ret = true;
86004fac2caSMatthieu Baerts 		if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
86104fac2caSMatthieu Baerts 						      remaining - opt_size, opts)) {
86204fac2caSMatthieu Baerts 			*size += opt_size + mp_fail_size;
86304fac2caSMatthieu Baerts 			remaining -= opt_size - mp_fail_size;
864c25aeb4eSGeliang Tang 			return true;
865c25aeb4eSGeliang Tang 		}
866c25aeb4eSGeliang Tang 	}
8676d0060f6SMat Martineau 
8686d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
8696d0060f6SMat Martineau 	 * TCP option space would be fatal
8706d0060f6SMat Martineau 	 */
8716d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
8726d0060f6SMat Martineau 		return false;
8736d0060f6SMat Martineau 
8746d0060f6SMat Martineau 	*size += opt_size;
8756d0060f6SMat Martineau 	remaining -= opt_size;
87684dfe367SGeliang Tang 	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
8773df523abSPeter Krystad 		*size += opt_size;
8783df523abSPeter Krystad 		remaining -= opt_size;
8793df523abSPeter Krystad 		ret = true;
8805cb104aeSGeliang Tang 	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
8815cb104aeSGeliang Tang 		*size += opt_size;
8825cb104aeSGeliang Tang 		remaining -= opt_size;
8835cb104aeSGeliang Tang 		ret = true;
8843df523abSPeter Krystad 	}
8856d0060f6SMat Martineau 
88606706542SGeliang Tang 	if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
88706706542SGeliang Tang 		*size += opt_size;
88806706542SGeliang Tang 		remaining -= opt_size;
88906706542SGeliang Tang 		ret = true;
89006706542SGeliang Tang 	}
89106706542SGeliang Tang 
8926d0060f6SMat Martineau 	return ret;
8936d0060f6SMat Martineau }
8946d0060f6SMat Martineau 
895cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
896cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
897cec37a6eSPeter Krystad {
898cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
899cec37a6eSPeter Krystad 
900cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
901cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
902cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
90306fe1719SGeliang Tang 		opts->csum_reqd = subflow_req->csum_reqd;
904bab6b88eSGeliang Tang 		opts->allow_join_id0 = subflow_req->allow_join_id0;
905cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
906cec37a6eSPeter Krystad 		pr_debug("subflow_req=%p, local_key=%llu",
907cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
908cec37a6eSPeter Krystad 		return true;
909f296234cSPeter Krystad 	} else if (subflow_req->mp_join) {
910f296234cSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
911f296234cSPeter Krystad 		opts->backup = subflow_req->backup;
912f296234cSPeter Krystad 		opts->join_id = subflow_req->local_id;
913f296234cSPeter Krystad 		opts->thmac = subflow_req->thmac;
914f296234cSPeter Krystad 		opts->nonce = subflow_req->local_nonce;
915f296234cSPeter Krystad 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
916f296234cSPeter Krystad 			 subflow_req, opts->backup, opts->join_id,
917f296234cSPeter Krystad 			 opts->thmac, opts->nonce);
918f296234cSPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
919f296234cSPeter Krystad 		return true;
920cec37a6eSPeter Krystad 	}
921cec37a6eSPeter Krystad 	return false;
922cec37a6eSPeter Krystad }
923cec37a6eSPeter Krystad 
924d5824847SPaolo Abeni static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
925f296234cSPeter Krystad 				    struct mptcp_subflow_context *subflow,
926d22f4988SChristoph Paasch 				    struct sk_buff *skb,
927d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
928d22f4988SChristoph Paasch {
929d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
930f296234cSPeter Krystad 	 * will make the subflow fully established
931d22f4988SChristoph Paasch 	 */
932f296234cSPeter Krystad 	if (likely(subflow->fully_established)) {
933f296234cSPeter Krystad 		/* on passive sockets, check for 3rd ack retransmission
934f296234cSPeter Krystad 		 * note that msk is always set by subflow_syn_recv_sock()
935f296234cSPeter Krystad 		 * for mp_join subflows
936f296234cSPeter Krystad 		 */
937f296234cSPeter Krystad 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
938f296234cSPeter Krystad 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
93974c7dfbeSPaolo Abeni 		    subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
94070c708e8SKishen Maloor 		    !subflow->request_join)
941d5824847SPaolo Abeni 			tcp_send_ack(ssk);
942dfc8d060SDmytro Shytyi 		goto check_notify;
943f296234cSPeter Krystad 	}
944d22f4988SChristoph Paasch 
945d5824847SPaolo Abeni 	/* we must process OoO packets before the first subflow is fully
946d5824847SPaolo Abeni 	 * established. OoO packets are instead a protocol violation
947d5824847SPaolo Abeni 	 * for MP_JOIN subflows as the peer must not send any data
948d5824847SPaolo Abeni 	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
949f296234cSPeter Krystad 	 */
950d5824847SPaolo Abeni 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
951d5824847SPaolo Abeni 		if (subflow->mp_join)
952d5824847SPaolo Abeni 			goto reset;
953dfc8d060SDmytro Shytyi 		if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
954dfc8d060SDmytro Shytyi 			goto set_fully_established;
955f296234cSPeter Krystad 		return subflow->mp_capable;
956d5824847SPaolo Abeni 	}
957f296234cSPeter Krystad 
958b3ea6b27SPaolo Abeni 	if (subflow->remote_key_valid &&
959b3ea6b27SPaolo Abeni 	    (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
960b3ea6b27SPaolo Abeni 	     ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) && !mp_opt->echo))) {
961f296234cSPeter Krystad 		/* subflows are fully established as soon as we get any
96267b12f79SMatthieu Baerts 		 * additional ack, including ADD_ADDR.
963f296234cSPeter Krystad 		 */
9640be534f5SPaolo Abeni 		subflow->fully_established = 1;
965b93df08cSPaolo Abeni 		WRITE_ONCE(msk->fully_established, true);
966dfc8d060SDmytro Shytyi 		goto check_notify;
967f296234cSPeter Krystad 	}
968d22f4988SChristoph Paasch 
969d22f4988SChristoph Paasch 	/* If the first established packet does not contain MP_CAPABLE + data
970d5824847SPaolo Abeni 	 * then fallback to TCP. Fallback scenarios requires a reset for
971d5824847SPaolo Abeni 	 * MP_JOIN subflows.
972d22f4988SChristoph Paasch 	 */
97374c7dfbeSPaolo Abeni 	if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
974d5824847SPaolo Abeni 		if (subflow->mp_join)
975d5824847SPaolo Abeni 			goto reset;
976d22f4988SChristoph Paasch 		subflow->mp_capable = 0;
977e1ff9e82SDavide Caratti 		pr_fallback(msk);
978d51991e2SPaolo Abeni 		mptcp_do_fallback(ssk);
979d22f4988SChristoph Paasch 		return false;
980d22f4988SChristoph Paasch 	}
981f296234cSPeter Krystad 
982df377be3SGeliang Tang 	if (mp_opt->deny_join_id0)
983df377be3SGeliang Tang 		WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
984df377be3SGeliang Tang 
985dfc8d060SDmytro Shytyi set_fully_established:
986d6085fe1SPaolo Abeni 	if (unlikely(!READ_ONCE(msk->pm.server_side)))
987d6085fe1SPaolo Abeni 		pr_warn_once("bogus mpc option on established client sk");
988b93df08cSPaolo Abeni 	mptcp_subflow_fully_established(subflow, mp_opt);
989f296234cSPeter Krystad 
990dfc8d060SDmytro Shytyi check_notify:
9915b950ff4SPaolo Abeni 	/* if the subflow is not already linked into the conn_list, we can't
9925b950ff4SPaolo Abeni 	 * notify the PM: this subflow is still on the listener queue
9935b950ff4SPaolo Abeni 	 * and the PM possibly acquiring the subflow lock could race with
9945b950ff4SPaolo Abeni 	 * the listener close
9955b950ff4SPaolo Abeni 	 */
9965b950ff4SPaolo Abeni 	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
997f296234cSPeter Krystad 		return true;
998f296234cSPeter Krystad 
999f296234cSPeter Krystad 	subflow->pm_notified = 1;
1000ec3edaa7SPeter Krystad 	if (subflow->mp_join) {
1001d5824847SPaolo Abeni 		clear_3rdack_retransmission(ssk);
100262535200SGeliang Tang 		mptcp_pm_subflow_established(msk);
1003ec3edaa7SPeter Krystad 	} else {
10046c714f1bSFlorian Westphal 		mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
1005ec3edaa7SPeter Krystad 	}
1006d22f4988SChristoph Paasch 	return true;
1007d5824847SPaolo Abeni 
1008d5824847SPaolo Abeni reset:
1009d5824847SPaolo Abeni 	mptcp_subflow_reset(ssk);
1010d5824847SPaolo Abeni 	return false;
1011d22f4988SChristoph Paasch }
1012d22f4988SChristoph Paasch 
10131502328fSPaolo Abeni u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1014cc9d2566SPaolo Abeni {
10151502328fSPaolo Abeni 	u32 old_seq32, cur_seq32;
1016cc9d2566SPaolo Abeni 
10171502328fSPaolo Abeni 	old_seq32 = (u32)old_seq;
10181502328fSPaolo Abeni 	cur_seq32 = (u32)cur_seq;
10191502328fSPaolo Abeni 	cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
10201502328fSPaolo Abeni 	if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
10211502328fSPaolo Abeni 		return cur_seq + (1LL << 32);
1022cc9d2566SPaolo Abeni 
10231502328fSPaolo Abeni 	/* reverse wrap could happen, too */
10241502328fSPaolo Abeni 	if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
10251502328fSPaolo Abeni 		return cur_seq - (1LL << 32);
10261502328fSPaolo Abeni 	return cur_seq;
1027cc9d2566SPaolo Abeni }
1028cc9d2566SPaolo Abeni 
10296f8a612aSFlorian Westphal static void ack_update_msk(struct mptcp_sock *msk,
10306e628cd3SPaolo Abeni 			   struct sock *ssk,
1031cc9d2566SPaolo Abeni 			   struct mptcp_options_received *mp_opt)
1032cc9d2566SPaolo Abeni {
10337439d687SPaolo Abeni 	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
10346f8a612aSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
10357439d687SPaolo Abeni 	u64 old_snd_una;
10367439d687SPaolo Abeni 
10377439d687SPaolo Abeni 	mptcp_data_lock(sk);
1038cc9d2566SPaolo Abeni 
1039cc9d2566SPaolo Abeni 	/* avoid ack expansion on update conflict, to reduce the risk of
1040cc9d2566SPaolo Abeni 	 * wrongly expanding to a future ack sequence number, which is way
1041cc9d2566SPaolo Abeni 	 * more dangerous than missing an ack
1042cc9d2566SPaolo Abeni 	 */
10437439d687SPaolo Abeni 	old_snd_una = msk->snd_una;
10441502328fSPaolo Abeni 	new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1045cc9d2566SPaolo Abeni 
10460d199e43SFlorian Westphal 	/* ACK for data not even sent yet? Ignore.*/
10470d199e43SFlorian Westphal 	if (unlikely(after64(new_snd_una, snd_nxt)))
1048cc9d2566SPaolo Abeni 		new_snd_una = old_snd_una;
1049cc9d2566SPaolo Abeni 
10506f8a612aSFlorian Westphal 	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
10516f8a612aSFlorian Westphal 
1052219d0499SPaolo Abeni 	if (after64(new_wnd_end, msk->wnd_end))
10537439d687SPaolo Abeni 		msk->wnd_end = new_wnd_end;
1054219d0499SPaolo Abeni 
1055219d0499SPaolo Abeni 	/* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1056d09d818eSPaolo Abeni 	if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1057219d0499SPaolo Abeni 		__mptcp_check_push(sk, ssk);
10586f8a612aSFlorian Westphal 
10597439d687SPaolo Abeni 	if (after64(new_snd_una, old_snd_una)) {
10607439d687SPaolo Abeni 		msk->snd_una = new_snd_una;
10617439d687SPaolo Abeni 		__mptcp_data_acked(sk);
1062cc9d2566SPaolo Abeni 	}
10637439d687SPaolo Abeni 	mptcp_data_unlock(sk);
1064ed66bfb4SGeliang Tang 
1065ed66bfb4SGeliang Tang 	trace_ack_update_msk(mp_opt->data_ack,
1066ed66bfb4SGeliang Tang 			     old_snd_una, new_snd_una,
1067ed66bfb4SGeliang Tang 			     new_wnd_end, msk->wnd_end);
1068b51f9b80SPaolo Abeni }
1069cc9d2566SPaolo Abeni 
10701a49b2c2SMat Martineau bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
10713721b9b6SMat Martineau {
10723721b9b6SMat Martineau 	/* Skip if DATA_FIN was already received.
10733721b9b6SMat Martineau 	 * If updating simultaneously with the recvmsg loop, values
10743721b9b6SMat Martineau 	 * should match. If they mismatch, the peer is misbehaving and
10753721b9b6SMat Martineau 	 * we will prefer the most recent information.
10763721b9b6SMat Martineau 	 */
1077781bf13dSPaolo Abeni 	if (READ_ONCE(msk->rcv_data_fin))
10783721b9b6SMat Martineau 		return false;
10793721b9b6SMat Martineau 
10801a49b2c2SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin_seq,
10811502328fSPaolo Abeni 		   mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
10823721b9b6SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin, 1);
10833721b9b6SMat Martineau 
10843721b9b6SMat Martineau 	return true;
10853721b9b6SMat Martineau }
10863721b9b6SMat Martineau 
10871b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk,
10881b1c7a0eSPeter Krystad 				struct mptcp_options_received *mp_opt)
10891b1c7a0eSPeter Krystad {
10901b1c7a0eSPeter Krystad 	u64 hmac = 0;
10911b1c7a0eSPeter Krystad 
10921b1c7a0eSPeter Krystad 	if (mp_opt->echo)
10931b1c7a0eSPeter Krystad 		return true;
10941b1c7a0eSPeter Krystad 
10951b1c7a0eSPeter Krystad 	hmac = add_addr_generate_hmac(msk->remote_key,
10961b1c7a0eSPeter Krystad 				      msk->local_key,
1097761c124eSGeliang Tang 				      &mp_opt->addr);
10981b1c7a0eSPeter Krystad 
10991b1c7a0eSPeter Krystad 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1100742e2f36SGeliang Tang 		 msk, hmac, mp_opt->ahmac);
11011b1c7a0eSPeter Krystad 
11021b1c7a0eSPeter Krystad 	return hmac == mp_opt->ahmac;
11031b1c7a0eSPeter Krystad }
11041b1c7a0eSPeter Krystad 
11056787b7e3SJianguo Wu /* Return false if a subflow has been reset, else return true */
11066787b7e3SJianguo Wu bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1107648ef4b8SMat Martineau {
1108d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
11091b1c7a0eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1110cfde141eSPaolo Abeni 	struct mptcp_options_received mp_opt;
1111648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
1112648ef4b8SMat Martineau 
11136e628cd3SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
11146e628cd3SPaolo Abeni 		/* Keep it simple and unconditionally trigger send data cleanup and
11156e628cd3SPaolo Abeni 		 * pending queue spooling. We will need to acquire the data lock
11166e628cd3SPaolo Abeni 		 * for more accurate checks, and once the lock is acquired, such
11176e628cd3SPaolo Abeni 		 * helpers are cheap.
11186e628cd3SPaolo Abeni 		 */
11196e628cd3SPaolo Abeni 		mptcp_data_lock(subflow->conn);
1120219d0499SPaolo Abeni 		if (sk_stream_memory_free(sk))
1121219d0499SPaolo Abeni 			__mptcp_check_push(subflow->conn, sk);
11226e628cd3SPaolo Abeni 		__mptcp_data_acked(subflow->conn);
11236e628cd3SPaolo Abeni 		mptcp_data_unlock(subflow->conn);
11246787b7e3SJianguo Wu 		return true;
11256e628cd3SPaolo Abeni 	}
1126e1ff9e82SDavide Caratti 
11270799e21bSGeliang Tang 	mptcp_get_options(skb, &mp_opt);
11286787b7e3SJianguo Wu 
11296787b7e3SJianguo Wu 	/* The subflow can be in close state only if check_fully_established()
11306787b7e3SJianguo Wu 	 * just sent a reset. If so, tell the caller to ignore the current packet.
11316787b7e3SJianguo Wu 	 */
1132cfde141eSPaolo Abeni 	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
11336787b7e3SJianguo Wu 		return sk->sk_state != TCP_CLOSE;
1134648ef4b8SMat Martineau 
1135f6c2ef59SPaolo Abeni 	if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
113674c7dfbeSPaolo Abeni 		if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
113750c504a2SFlorian Westphal 		    msk->local_key == mp_opt.rcvr_key) {
113850c504a2SFlorian Westphal 			WRITE_ONCE(msk->rcv_fastclose, true);
113950c504a2SFlorian Westphal 			mptcp_schedule_work((struct sock *)msk);
11401e75629cSGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
114150c504a2SFlorian Westphal 		}
114250c504a2SFlorian Westphal 
1143f6c2ef59SPaolo Abeni 		if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1144f6c2ef59SPaolo Abeni 		    add_addr_hmac_valid(msk, &mp_opt)) {
1145a877de06SGeliang Tang 			if (!mp_opt.echo) {
1146d1ace2d9SKishen Maloor 				mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1147a877de06SGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1148a877de06SGeliang Tang 			} else {
1149f7dafee1SGeliang Tang 				mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1150d58300c3SDavide Caratti 				mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1151a877de06SGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1152a877de06SGeliang Tang 			}
11532fbdd9eaSGeliang Tang 
1154f7dafee1SGeliang Tang 			if (mp_opt.addr.port)
11552fbdd9eaSGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
11561b1c7a0eSPeter Krystad 		}
11571b1c7a0eSPeter Krystad 
115874c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
11595c4a824dSGeliang Tang 			mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1160d0876b22SGeliang Tang 
116174c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
116240453a5cSGeliang Tang 			mptcp_pm_mp_prio_received(sk, mp_opt.backup);
11630be2ac28SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
116440453a5cSGeliang Tang 		}
116540453a5cSGeliang Tang 
116674c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
11675580d41bSGeliang Tang 			mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1168eb7f3365SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
11695580d41bSGeliang Tang 		}
11705580d41bSGeliang Tang 
117174c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1172dc87efdbSFlorian Westphal 			subflow->reset_seen = 1;
1173dc87efdbSFlorian Westphal 			subflow->reset_reason = mp_opt.reset_reason;
1174dc87efdbSFlorian Westphal 			subflow->reset_transient = mp_opt.reset_transient;
1175e40dd439SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1176dc87efdbSFlorian Westphal 		}
1177dc87efdbSFlorian Westphal 
117874c7dfbeSPaolo Abeni 		if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
11796787b7e3SJianguo Wu 			return true;
1180f6c2ef59SPaolo Abeni 	}
1181648ef4b8SMat Martineau 
1182cc9d2566SPaolo Abeni 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
1183cc9d2566SPaolo Abeni 	 * monodirectional flows will stuck
1184cc9d2566SPaolo Abeni 	 */
1185cfde141eSPaolo Abeni 	if (mp_opt.use_ack)
11866f8a612aSFlorian Westphal 		ack_update_msk(msk, sk, &mp_opt);
1187cc9d2566SPaolo Abeni 
118806827b34SMat Martineau 	/* Zero-data-length packets are dropped by the caller and not
118906827b34SMat Martineau 	 * propagated to the MPTCP layer, so the skb extension does not
119006827b34SMat Martineau 	 * need to be allocated or populated. DATA_FIN information, if
119106827b34SMat Martineau 	 * present, needs to be updated here before the skb is freed.
119243b54c6eSMat Martineau 	 */
119343b54c6eSMat Martineau 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
119443b54c6eSMat Martineau 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
11951a49b2c2SMat Martineau 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
119643b54c6eSMat Martineau 		    schedule_work(&msk->work))
119743b54c6eSMat Martineau 			sock_hold(subflow->conn);
119806827b34SMat Martineau 
11996787b7e3SJianguo Wu 		return true;
120043b54c6eSMat Martineau 	}
120143b54c6eSMat Martineau 
1202648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1203648ef4b8SMat Martineau 	if (!mpext)
12046787b7e3SJianguo Wu 		return true;
1205648ef4b8SMat Martineau 
1206648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
1207648ef4b8SMat Martineau 
1208f6c2ef59SPaolo Abeni 	if (likely(mp_opt.use_map)) {
1209cfde141eSPaolo Abeni 		if (mp_opt.mpc_map) {
1210cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
1211cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
1212cc7972eaSChristoph Paasch 			 */
1213cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
1214cc7972eaSChristoph Paasch 					     &mpext->data_seq);
1215cc7972eaSChristoph Paasch 			mpext->data_seq++;
1216cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
1217cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
1218cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
1219a77895dbSPaolo Abeni 			mpext->data_fin = 0;
1220cc7972eaSChristoph Paasch 		} else {
1221cfde141eSPaolo Abeni 			mpext->data_seq = mp_opt.data_seq;
1222cfde141eSPaolo Abeni 			mpext->subflow_seq = mp_opt.subflow_seq;
1223cfde141eSPaolo Abeni 			mpext->dsn64 = mp_opt.dsn64;
1224cfde141eSPaolo Abeni 			mpext->data_fin = mp_opt.data_fin;
1225cc7972eaSChristoph Paasch 		}
1226cfde141eSPaolo Abeni 		mpext->data_len = mp_opt.data_len;
1227648ef4b8SMat Martineau 		mpext->use_map = 1;
122874c7dfbeSPaolo Abeni 		mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1229208e8f66SGeliang Tang 
1230208e8f66SGeliang Tang 		if (mpext->csum_reqd)
1231208e8f66SGeliang Tang 			mpext->csum = mp_opt.csum;
1232648ef4b8SMat Martineau 	}
12336787b7e3SJianguo Wu 
12346787b7e3SJianguo Wu 	return true;
1235648ef4b8SMat Martineau }
1236648ef4b8SMat Martineau 
1237f3589be0SPaolo Abeni static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
1238fa3fe2b1SFlorian Westphal {
1239fa3fe2b1SFlorian Westphal 	const struct sock *ssk = (const struct sock *)tp;
1240f3589be0SPaolo Abeni 	struct mptcp_subflow_context *subflow;
1241f3589be0SPaolo Abeni 	u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
1242fa3fe2b1SFlorian Westphal 	struct mptcp_sock *msk;
1243f3589be0SPaolo Abeni 	u32 new_win;
1244f3589be0SPaolo Abeni 	u64 win;
1245fa3fe2b1SFlorian Westphal 
1246fa3fe2b1SFlorian Westphal 	subflow = mptcp_subflow_ctx(ssk);
1247fa3fe2b1SFlorian Westphal 	msk = mptcp_sk(subflow->conn);
1248fa3fe2b1SFlorian Westphal 
1249f3589be0SPaolo Abeni 	ack_seq = READ_ONCE(msk->ack_seq);
1250f3589be0SPaolo Abeni 	rcv_wnd_new = ack_seq + tp->rcv_wnd;
1251fa3fe2b1SFlorian Westphal 
1252f3589be0SPaolo Abeni 	rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1253f3589be0SPaolo Abeni 	if (after64(rcv_wnd_new, rcv_wnd_old)) {
1254f3589be0SPaolo Abeni 		u64 rcv_wnd;
1255f3589be0SPaolo Abeni 
1256f3589be0SPaolo Abeni 		for (;;) {
1257f3589be0SPaolo Abeni 			rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1258f3589be0SPaolo Abeni 
1259f3589be0SPaolo Abeni 			if (rcv_wnd == rcv_wnd_old)
1260f3589be0SPaolo Abeni 				break;
126138acb626SPaolo Abeni 			if (before64(rcv_wnd_new, rcv_wnd)) {
126238acb626SPaolo Abeni 				MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1263f3589be0SPaolo Abeni 				goto raise_win;
126438acb626SPaolo Abeni 			}
126538acb626SPaolo Abeni 			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1266f3589be0SPaolo Abeni 			rcv_wnd_old = rcv_wnd;
1267f3589be0SPaolo Abeni 		}
1268f3589be0SPaolo Abeni 		return;
1269f3589be0SPaolo Abeni 	}
1270f3589be0SPaolo Abeni 
1271f3589be0SPaolo Abeni 	if (rcv_wnd_new != rcv_wnd_old) {
1272f3589be0SPaolo Abeni raise_win:
1273f3589be0SPaolo Abeni 		win = rcv_wnd_old - ack_seq;
1274f3589be0SPaolo Abeni 		tp->rcv_wnd = min_t(u64, win, U32_MAX);
1275f3589be0SPaolo Abeni 		new_win = tp->rcv_wnd;
1276f3589be0SPaolo Abeni 
1277f3589be0SPaolo Abeni 		/* Make sure we do not exceed the maximum possible
1278f3589be0SPaolo Abeni 		 * scaled window.
1279f3589be0SPaolo Abeni 		 */
1280f3589be0SPaolo Abeni 		if (unlikely(th->syn))
1281f3589be0SPaolo Abeni 			new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1282f3589be0SPaolo Abeni 		if (!tp->rx_opt.rcv_wscale &&
12830f1e4d06SKuniyuki Iwashima 		    READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1284f3589be0SPaolo Abeni 			new_win = min(new_win, MAX_TCP_WINDOW);
1285f3589be0SPaolo Abeni 		else
1286f3589be0SPaolo Abeni 			new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1287f3589be0SPaolo Abeni 
1288f3589be0SPaolo Abeni 		/* RFC1323 scaling applied */
1289f3589be0SPaolo Abeni 		new_win >>= tp->rx_opt.rcv_wscale;
1290f3589be0SPaolo Abeni 		th->window = htons(new_win);
129138acb626SPaolo Abeni 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1292f3589be0SPaolo Abeni 	}
1293fa3fe2b1SFlorian Westphal }
1294fa3fe2b1SFlorian Westphal 
1295ba2c89e0SPaolo Abeni __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1296c94b1f96SGeliang Tang {
1297c94b1f96SGeliang Tang 	struct csum_pseudo_header header;
1298c94b1f96SGeliang Tang 	__wsum csum;
1299c94b1f96SGeliang Tang 
1300c94b1f96SGeliang Tang 	/* cfr RFC 8684 3.3.1.:
1301c94b1f96SGeliang Tang 	 * the data sequence number used in the pseudo-header is
1302c94b1f96SGeliang Tang 	 * always the 64-bit value, irrespective of what length is used in the
1303c94b1f96SGeliang Tang 	 * DSS option itself.
1304c94b1f96SGeliang Tang 	 */
1305f7cc8890SDavide Caratti 	header.data_seq = cpu_to_be64(data_seq);
1306f7cc8890SDavide Caratti 	header.subflow_seq = htonl(subflow_seq);
1307f7cc8890SDavide Caratti 	header.data_len = htons(data_len);
1308c94b1f96SGeliang Tang 	header.csum = 0;
1309c94b1f96SGeliang Tang 
1310c312ee21SGeliang Tang 	csum = csum_partial(&header, sizeof(header), sum);
1311ba2c89e0SPaolo Abeni 	return csum_fold(csum);
1312c94b1f96SGeliang Tang }
1313c94b1f96SGeliang Tang 
1314ba2c89e0SPaolo Abeni static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1315f7cc8890SDavide Caratti {
1316f7cc8890SDavide Caratti 	return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1317c312ee21SGeliang Tang 				 ~csum_unfold(mpext->csum));
1318f7cc8890SDavide Caratti }
1319f7cc8890SDavide Caratti 
1320ba2c89e0SPaolo Abeni static void put_len_csum(u16 len, __sum16 csum, void *data)
1321ba2c89e0SPaolo Abeni {
1322ba2c89e0SPaolo Abeni 	__sum16 *sumptr = data + 2;
1323ba2c89e0SPaolo Abeni 	__be16 *ptr = data;
1324ba2c89e0SPaolo Abeni 
1325ba2c89e0SPaolo Abeni 	put_unaligned_be16(len, ptr);
1326ba2c89e0SPaolo Abeni 
1327ba2c89e0SPaolo Abeni 	put_unaligned(csum, sumptr);
1328ba2c89e0SPaolo Abeni }
1329ba2c89e0SPaolo Abeni 
1330ea66758cSPaolo Abeni void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1331fa3fe2b1SFlorian Westphal 			 struct mptcp_out_options *opts)
1332eda7acddSPeter Krystad {
1333c25aeb4eSGeliang Tang 	const struct sock *ssk = (const struct sock *)tp;
1334c25aeb4eSGeliang Tang 	struct mptcp_subflow_context *subflow;
1335c25aeb4eSGeliang Tang 
13368cca39e2SMatthieu Baerts 	/* Which options can be used together?
13378cca39e2SMatthieu Baerts 	 *
13388cca39e2SMatthieu Baerts 	 * X: mutually exclusive
13398cca39e2SMatthieu Baerts 	 * O: often used together
13408cca39e2SMatthieu Baerts 	 * C: can be used together in some cases
13418cca39e2SMatthieu Baerts 	 * P: could be used together but we prefer not to (optimisations)
13428cca39e2SMatthieu Baerts 	 *
13438cca39e2SMatthieu Baerts 	 *  Opt: | MPC  | MPJ  | DSS  | ADD  |  RM  | PRIO | FAIL |  FC  |
13448cca39e2SMatthieu Baerts 	 * ------|------|------|------|------|------|------|------|------|
13458cca39e2SMatthieu Baerts 	 *  MPC  |------|------|------|------|------|------|------|------|
13468cca39e2SMatthieu Baerts 	 *  MPJ  |  X   |------|------|------|------|------|------|------|
13478cca39e2SMatthieu Baerts 	 *  DSS  |  X   |  X   |------|------|------|------|------|------|
13488cca39e2SMatthieu Baerts 	 *  ADD  |  X   |  X   |  P   |------|------|------|------|------|
13498cca39e2SMatthieu Baerts 	 *  RM   |  C   |  C   |  C   |  P   |------|------|------|------|
13508cca39e2SMatthieu Baerts 	 *  PRIO |  X   |  C   |  C   |  C   |  C   |------|------|------|
13518cca39e2SMatthieu Baerts 	 *  FAIL |  X   |  X   |  C   |  X   |  X   |  X   |------|------|
13528cca39e2SMatthieu Baerts 	 *  FC   |  X   |  X   |  X   |  X   |  X   |  X   |  X   |------|
13538cca39e2SMatthieu Baerts 	 *  RST  |  X   |  X   |  X   |  X   |  X   |  X   |  O   |  O   |
13548cca39e2SMatthieu Baerts 	 * ------|------|------|------|------|------|------|------|------|
13558cca39e2SMatthieu Baerts 	 *
13568cca39e2SMatthieu Baerts 	 * The same applies in mptcp_established_options() function.
13571bff1e43SPaolo Abeni 	 */
13581bff1e43SPaolo Abeni 	if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
13591bff1e43SPaolo Abeni 		struct mptcp_ext *mpext = &opts->ext_copy;
13601bff1e43SPaolo Abeni 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
13611bff1e43SPaolo Abeni 		u8 flags = 0;
13621bff1e43SPaolo Abeni 
13631bff1e43SPaolo Abeni 		if (mpext->use_ack) {
13641bff1e43SPaolo Abeni 			flags = MPTCP_DSS_HAS_ACK;
13651bff1e43SPaolo Abeni 			if (mpext->ack64) {
13661bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_ACK64;
13671bff1e43SPaolo Abeni 				flags |= MPTCP_DSS_ACK64;
13681bff1e43SPaolo Abeni 			} else {
13691bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_ACK32;
13701bff1e43SPaolo Abeni 			}
13711bff1e43SPaolo Abeni 		}
13721bff1e43SPaolo Abeni 
13731bff1e43SPaolo Abeni 		if (mpext->use_map) {
13741bff1e43SPaolo Abeni 			len += TCPOLEN_MPTCP_DSS_MAP64;
13751bff1e43SPaolo Abeni 
13761bff1e43SPaolo Abeni 			/* Use only 64-bit mapping flags for now, add
13771bff1e43SPaolo Abeni 			 * support for optional 32-bit mappings later.
13781bff1e43SPaolo Abeni 			 */
13791bff1e43SPaolo Abeni 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
13801bff1e43SPaolo Abeni 			if (mpext->data_fin)
13811bff1e43SPaolo Abeni 				flags |= MPTCP_DSS_DATA_FIN;
13821bff1e43SPaolo Abeni 
13831bff1e43SPaolo Abeni 			if (opts->csum_reqd)
13841bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
13851bff1e43SPaolo Abeni 		}
13861bff1e43SPaolo Abeni 
13871bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
13881bff1e43SPaolo Abeni 
13891bff1e43SPaolo Abeni 		if (mpext->use_ack) {
13901bff1e43SPaolo Abeni 			if (mpext->ack64) {
13911bff1e43SPaolo Abeni 				put_unaligned_be64(mpext->data_ack, ptr);
13921bff1e43SPaolo Abeni 				ptr += 2;
13931bff1e43SPaolo Abeni 			} else {
13941bff1e43SPaolo Abeni 				put_unaligned_be32(mpext->data_ack32, ptr);
13951bff1e43SPaolo Abeni 				ptr += 1;
13961bff1e43SPaolo Abeni 			}
13971bff1e43SPaolo Abeni 		}
13981bff1e43SPaolo Abeni 
13991bff1e43SPaolo Abeni 		if (mpext->use_map) {
14001bff1e43SPaolo Abeni 			put_unaligned_be64(mpext->data_seq, ptr);
14011bff1e43SPaolo Abeni 			ptr += 2;
14021bff1e43SPaolo Abeni 			put_unaligned_be32(mpext->subflow_seq, ptr);
14031bff1e43SPaolo Abeni 			ptr += 1;
14041bff1e43SPaolo Abeni 			if (opts->csum_reqd) {
14051e39e5a3SGeliang Tang 				/* data_len == 0 is reserved for the infinite mapping,
14061e39e5a3SGeliang Tang 				 * the checksum will also be set to 0.
14071e39e5a3SGeliang Tang 				 */
1408ba2c89e0SPaolo Abeni 				put_len_csum(mpext->data_len,
14091e39e5a3SGeliang Tang 					     (mpext->data_len ? mptcp_make_csum(mpext) : 0),
14101e39e5a3SGeliang Tang 					     ptr);
14111bff1e43SPaolo Abeni 			} else {
14121bff1e43SPaolo Abeni 				put_unaligned_be32(mpext->data_len << 16 |
14131bff1e43SPaolo Abeni 						   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
14141bff1e43SPaolo Abeni 			}
1415110b6d1fSGeliang Tang 			ptr += 1;
14161bff1e43SPaolo Abeni 		}
1417902c8f86SMatthieu Baerts 
1418902c8f86SMatthieu Baerts 		/* We might need to add MP_FAIL options in rare cases */
1419902c8f86SMatthieu Baerts 		if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1420902c8f86SMatthieu Baerts 			goto mp_fail;
142113ac17a3SGeliang Tang 	} else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
142206fe1719SGeliang Tang 		u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1423eda7acddSPeter Krystad 
1424c94b1f96SGeliang Tang 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1425eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
1426c94b1f96SGeliang Tang 		} else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1427cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
1428f7cc8890SDavide Caratti 		} else if (opts->data_len) {
1429cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1430c94b1f96SGeliang Tang 			if (opts->csum_reqd)
1431c94b1f96SGeliang Tang 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1432c94b1f96SGeliang Tang 		} else {
1433eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
1434c94b1f96SGeliang Tang 		}
1435eda7acddSPeter Krystad 
143606fe1719SGeliang Tang 		if (opts->csum_reqd)
143706fe1719SGeliang Tang 			flag |= MPTCP_CAP_CHECKSUM_REQD;
143806fe1719SGeliang Tang 
1439bab6b88eSGeliang Tang 		if (!opts->allow_join_id0)
1440bab6b88eSGeliang Tang 			flag |= MPTCP_CAP_DENY_JOIN_ID0;
1441bab6b88eSGeliang Tang 
14423df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
14433df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
144406fe1719SGeliang Tang 				      flag);
1445cc7972eaSChristoph Paasch 
1446cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1447cc7972eaSChristoph Paasch 		    opts->suboptions))
1448cc7972eaSChristoph Paasch 			goto mp_capable_done;
1449cc7972eaSChristoph Paasch 
1450eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
1451eda7acddSPeter Krystad 		ptr += 2;
1452cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1453cc7972eaSChristoph Paasch 			goto mp_capable_done;
1454cc7972eaSChristoph Paasch 
1455eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
1456eda7acddSPeter Krystad 		ptr += 2;
1457f7cc8890SDavide Caratti 		if (!opts->data_len)
1458cc7972eaSChristoph Paasch 			goto mp_capable_done;
1459cc7972eaSChristoph Paasch 
1460c94b1f96SGeliang Tang 		if (opts->csum_reqd) {
1461ba2c89e0SPaolo Abeni 			put_len_csum(opts->data_len,
1462f7cc8890SDavide Caratti 				     __mptcp_make_csum(opts->data_seq,
1463f7cc8890SDavide Caratti 						       opts->subflow_seq,
1464f7cc8890SDavide Caratti 						       opts->data_len,
1465ba2c89e0SPaolo Abeni 						       ~csum_unfold(opts->csum)),
1466ba2c89e0SPaolo Abeni 				     ptr);
1467c94b1f96SGeliang Tang 		} else {
1468f7cc8890SDavide Caratti 			put_unaligned_be32(opts->data_len << 16 |
1469cc7972eaSChristoph Paasch 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1470c94b1f96SGeliang Tang 		}
1471cc7972eaSChristoph Paasch 		ptr += 1;
14726d0060f6SMat Martineau 
14731bff1e43SPaolo Abeni 		/* MPC is additionally mutually exclusive with MP_PRIO */
14741bff1e43SPaolo Abeni 		goto mp_capable_done;
147571b077e4SPaolo Abeni 	} else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
147671b077e4SPaolo Abeni 		if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
14771bff1e43SPaolo Abeni 			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
14781bff1e43SPaolo Abeni 					      TCPOLEN_MPTCP_MPJ_SYN,
14791bff1e43SPaolo Abeni 					      opts->backup, opts->join_id);
14801bff1e43SPaolo Abeni 			put_unaligned_be32(opts->token, ptr);
14811bff1e43SPaolo Abeni 			ptr += 1;
14821bff1e43SPaolo Abeni 			put_unaligned_be32(opts->nonce, ptr);
14831bff1e43SPaolo Abeni 			ptr += 1;
14841bff1e43SPaolo Abeni 		} else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
14851bff1e43SPaolo Abeni 			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
14861bff1e43SPaolo Abeni 					      TCPOLEN_MPTCP_MPJ_SYNACK,
14871bff1e43SPaolo Abeni 					      opts->backup, opts->join_id);
14881bff1e43SPaolo Abeni 			put_unaligned_be64(opts->thmac, ptr);
14891bff1e43SPaolo Abeni 			ptr += 2;
14901bff1e43SPaolo Abeni 			put_unaligned_be32(opts->nonce, ptr);
14911bff1e43SPaolo Abeni 			ptr += 1;
149271b077e4SPaolo Abeni 		} else {
14931bff1e43SPaolo Abeni 			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
14941bff1e43SPaolo Abeni 					      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
14951bff1e43SPaolo Abeni 			memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
14961bff1e43SPaolo Abeni 			ptr += 5;
149771b077e4SPaolo Abeni 		}
14981bff1e43SPaolo Abeni 	} else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
14996eb3d1e3SGeliang Tang 		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
15006eb3d1e3SGeliang Tang 		u8 echo = MPTCP_ADDR_ECHO;
15013df523abSPeter Krystad 
15023df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1503fef6b7ecSGeliang Tang 		if (opts->addr.family == AF_INET6)
1504e1ef6832SGeliang Tang 			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1505e1ef6832SGeliang Tang #endif
1506e1ef6832SGeliang Tang 
150730f60baeSGeliang Tang 		if (opts->addr.port)
150822fb85ffSGeliang Tang 			len += TCPOLEN_MPTCP_PORT_LEN;
150922fb85ffSGeliang Tang 
15106eb3d1e3SGeliang Tang 		if (opts->ahmac) {
15116eb3d1e3SGeliang Tang 			len += sizeof(opts->ahmac);
15126eb3d1e3SGeliang Tang 			echo = 0;
15136eb3d1e3SGeliang Tang 		}
15146eb3d1e3SGeliang Tang 
15153df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
151630f60baeSGeliang Tang 				      len, echo, opts->addr.id);
1517fef6b7ecSGeliang Tang 		if (opts->addr.family == AF_INET) {
151830f60baeSGeliang Tang 			memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
15193df523abSPeter Krystad 			ptr += 1;
15203df523abSPeter Krystad 		}
15213df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1522fef6b7ecSGeliang Tang 		else if (opts->addr.family == AF_INET6) {
152330f60baeSGeliang Tang 			memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
15243df523abSPeter Krystad 			ptr += 4;
1525e1ef6832SGeliang Tang 		}
1526e1ef6832SGeliang Tang #endif
152722fb85ffSGeliang Tang 
152830f60baeSGeliang Tang 		if (!opts->addr.port) {
15293df523abSPeter Krystad 			if (opts->ahmac) {
15303df523abSPeter Krystad 				put_unaligned_be64(opts->ahmac, ptr);
15313df523abSPeter Krystad 				ptr += 2;
15323df523abSPeter Krystad 			}
153322fb85ffSGeliang Tang 		} else {
153430f60baeSGeliang Tang 			u16 port = ntohs(opts->addr.port);
153530f60baeSGeliang Tang 
153622fb85ffSGeliang Tang 			if (opts->ahmac) {
153722fb85ffSGeliang Tang 				u8 *bptr = (u8 *)ptr;
153822fb85ffSGeliang Tang 
153930f60baeSGeliang Tang 				put_unaligned_be16(port, bptr);
154022fb85ffSGeliang Tang 				bptr += 2;
154122fb85ffSGeliang Tang 				put_unaligned_be64(opts->ahmac, bptr);
154222fb85ffSGeliang Tang 				bptr += 8;
154322fb85ffSGeliang Tang 				put_unaligned_be16(TCPOPT_NOP << 8 |
154422fb85ffSGeliang Tang 						   TCPOPT_NOP, bptr);
154522fb85ffSGeliang Tang 
154622fb85ffSGeliang Tang 				ptr += 3;
154722fb85ffSGeliang Tang 			} else {
154830f60baeSGeliang Tang 				put_unaligned_be32(port << 16 |
154922fb85ffSGeliang Tang 						   TCPOPT_NOP << 8 |
155022fb85ffSGeliang Tang 						   TCPOPT_NOP, ptr);
155122fb85ffSGeliang Tang 				ptr += 1;
15523df523abSPeter Krystad 			}
155322fb85ffSGeliang Tang 		}
1554f284c0c7SPaolo Abeni 	} else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1555f284c0c7SPaolo Abeni 		/* FASTCLOSE is mutually exclusive with others except RST */
1556f284c0c7SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1557f284c0c7SPaolo Abeni 				      TCPOLEN_MPTCP_FASTCLOSE,
1558f284c0c7SPaolo Abeni 				      0, 0);
1559f284c0c7SPaolo Abeni 		put_unaligned_be64(opts->rcvr_key, ptr);
1560f284c0c7SPaolo Abeni 		ptr += 2;
1561f284c0c7SPaolo Abeni 
1562f284c0c7SPaolo Abeni 		if (OPTION_MPTCP_RST & opts->suboptions)
1563f284c0c7SPaolo Abeni 			goto mp_rst;
1564f284c0c7SPaolo Abeni 		return;
1565902c8f86SMatthieu Baerts 	} else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1566902c8f86SMatthieu Baerts mp_fail:
1567902c8f86SMatthieu Baerts 		/* MP_FAIL is mutually exclusive with others except RST */
1568902c8f86SMatthieu Baerts 		subflow = mptcp_subflow_ctx(ssk);
1569902c8f86SMatthieu Baerts 		subflow->send_mp_fail = 0;
1570902c8f86SMatthieu Baerts 
1571902c8f86SMatthieu Baerts 		*ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1572902c8f86SMatthieu Baerts 				      TCPOLEN_MPTCP_FAIL,
1573902c8f86SMatthieu Baerts 				      0, 0);
1574902c8f86SMatthieu Baerts 		put_unaligned_be64(opts->fail_seq, ptr);
1575902c8f86SMatthieu Baerts 		ptr += 2;
1576902c8f86SMatthieu Baerts 
1577902c8f86SMatthieu Baerts 		if (OPTION_MPTCP_RST & opts->suboptions)
1578902c8f86SMatthieu Baerts 			goto mp_rst;
1579902c8f86SMatthieu Baerts 		return;
1580f284c0c7SPaolo Abeni 	} else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1581f284c0c7SPaolo Abeni mp_rst:
1582f284c0c7SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_RST,
1583f284c0c7SPaolo Abeni 				      TCPOLEN_MPTCP_RST,
1584f284c0c7SPaolo Abeni 				      opts->reset_transient,
1585f284c0c7SPaolo Abeni 				      opts->reset_reason);
1586f284c0c7SPaolo Abeni 		return;
15873df523abSPeter Krystad 	}
15883df523abSPeter Krystad 
15891bff1e43SPaolo Abeni 	if (OPTION_MPTCP_PRIO & opts->suboptions) {
15901bff1e43SPaolo Abeni 		subflow = mptcp_subflow_ctx(ssk);
15911bff1e43SPaolo Abeni 		subflow->send_mp_prio = 0;
15921bff1e43SPaolo Abeni 
15931bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
15941bff1e43SPaolo Abeni 				      TCPOLEN_MPTCP_PRIO,
15951bff1e43SPaolo Abeni 				      opts->backup, TCPOPT_NOP);
1596c21b50d5SMat Martineau 
1597*3c976f4cSGeliang Tang 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
15981bff1e43SPaolo Abeni 	}
15991bff1e43SPaolo Abeni 
16001bff1e43SPaolo Abeni mp_capable_done:
16013df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
16026445e17aSGeliang Tang 		u8 i = 1;
16036445e17aSGeliang Tang 
16043df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
16056445e17aSGeliang Tang 				      TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
16066445e17aSGeliang Tang 				      0, opts->rm_list.ids[0]);
16076445e17aSGeliang Tang 
16086445e17aSGeliang Tang 		while (i < opts->rm_list.nr) {
16096445e17aSGeliang Tang 			u8 id1, id2, id3, id4;
16106445e17aSGeliang Tang 
16116445e17aSGeliang Tang 			id1 = opts->rm_list.ids[i];
16126445e17aSGeliang Tang 			id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
16136445e17aSGeliang Tang 			id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
16146445e17aSGeliang Tang 			id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
16156445e17aSGeliang Tang 			put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
16166445e17aSGeliang Tang 			ptr += 1;
16176445e17aSGeliang Tang 			i += 4;
16186445e17aSGeliang Tang 		}
16193df523abSPeter Krystad 	}
16203df523abSPeter Krystad 
1621fa3fe2b1SFlorian Westphal 	if (tp)
1622f3589be0SPaolo Abeni 		mptcp_set_rwin(tp, th);
1623eda7acddSPeter Krystad }
1624dc87efdbSFlorian Westphal 
1625dc87efdbSFlorian Westphal __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1626dc87efdbSFlorian Westphal {
1627dc87efdbSFlorian Westphal 	const struct mptcp_ext *ext = mptcp_get_ext(skb);
1628dc87efdbSFlorian Westphal 	u8 flags, reason;
1629dc87efdbSFlorian Westphal 
1630dc87efdbSFlorian Westphal 	if (ext) {
1631dc87efdbSFlorian Westphal 		flags = ext->reset_transient;
1632dc87efdbSFlorian Westphal 		reason = ext->reset_reason;
1633dc87efdbSFlorian Westphal 
1634dc87efdbSFlorian Westphal 		return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1635dc87efdbSFlorian Westphal 				    flags, reason);
1636dc87efdbSFlorian Westphal 	}
1637dc87efdbSFlorian Westphal 
1638dc87efdbSFlorian Westphal 	return htonl(0u);
1639dc87efdbSFlorian Westphal }
1640dc87efdbSFlorian Westphal EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
1641