xref: /openbmc/linux/net/mptcp/options.c (revision ac8f933664c3a0e2d42f6ee9a2a6d25f87cb23f6)
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 
mptcp_cap_flag_sha256(u8 flags)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 
mptcp_parse_option(const struct sk_buff * skb,const unsigned char * ptr,int opsize,struct mptcp_options_received * mp_opt)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;
1115814deb5SEdward Adam Davis 			mp_opt->use_ack = 0;
112cc7972eaSChristoph Paasch 			mp_opt->data_len = get_unaligned_be16(ptr);
113cc7972eaSChristoph Paasch 			ptr += 2;
114cc7972eaSChristoph Paasch 		}
115208e8f66SGeliang Tang 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
116ba2c89e0SPaolo Abeni 			mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
11774c7dfbeSPaolo Abeni 			mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
118208e8f66SGeliang Tang 			ptr += 2;
119208e8f66SGeliang Tang 		}
120*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u\n",
121cc7972eaSChristoph Paasch 			 version, flags, opsize, mp_opt->sndr_key,
122208e8f66SGeliang Tang 			 mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
123eda7acddSPeter Krystad 		break;
124eda7acddSPeter Krystad 
125f296234cSPeter Krystad 	case MPTCPOPT_MP_JOIN:
126f296234cSPeter Krystad 		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
12740298206SEric Dumazet 			mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYN;
128f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
129f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
130f296234cSPeter Krystad 			mp_opt->token = get_unaligned_be32(ptr);
131f296234cSPeter Krystad 			ptr += 4;
132f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
133f296234cSPeter Krystad 			ptr += 4;
134*3d2e1b82SMatthieu Baerts (NGI0) 			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u\n",
135f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
136f296234cSPeter Krystad 				 mp_opt->token, mp_opt->nonce);
137f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
13840298206SEric Dumazet 			mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYNACK;
139f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
140f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
141f296234cSPeter Krystad 			mp_opt->thmac = get_unaligned_be64(ptr);
142f296234cSPeter Krystad 			ptr += 8;
143f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
144f296234cSPeter Krystad 			ptr += 4;
145*3d2e1b82SMatthieu Baerts (NGI0) 			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
146f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
147f296234cSPeter Krystad 				 mp_opt->thmac, mp_opt->nonce);
148f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
14940298206SEric Dumazet 			mp_opt->suboptions |= OPTION_MPTCP_MPJ_ACK;
150f296234cSPeter Krystad 			ptr += 2;
151f296234cSPeter Krystad 			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
152*3d2e1b82SMatthieu Baerts (NGI0) 			pr_debug("MP_JOIN hmac\n");
153f296234cSPeter Krystad 		}
154f296234cSPeter Krystad 		break;
155f296234cSPeter Krystad 
156eda7acddSPeter Krystad 	case MPTCPOPT_DSS:
157*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("DSS\n");
158648ef4b8SMat Martineau 		ptr++;
159648ef4b8SMat Martineau 
160cc7972eaSChristoph Paasch 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
161cc7972eaSChristoph Paasch 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
162cc7972eaSChristoph Paasch 		 * map info accordingly
163cc7972eaSChristoph Paasch 		 */
164cc7972eaSChristoph Paasch 		mp_opt->mpc_map = 0;
165648ef4b8SMat Martineau 		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
166648ef4b8SMat Martineau 		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
167648ef4b8SMat Martineau 		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
168648ef4b8SMat Martineau 		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
169648ef4b8SMat Martineau 		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
170648ef4b8SMat Martineau 		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
171648ef4b8SMat Martineau 
172*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d\n",
173648ef4b8SMat Martineau 			 mp_opt->data_fin, mp_opt->dsn64,
174648ef4b8SMat Martineau 			 mp_opt->use_map, mp_opt->ack64,
175648ef4b8SMat Martineau 			 mp_opt->use_ack);
176648ef4b8SMat Martineau 
177648ef4b8SMat Martineau 		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
178648ef4b8SMat Martineau 
179648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
180648ef4b8SMat Martineau 			if (mp_opt->ack64)
181648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
182648ef4b8SMat Martineau 			else
183648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
184648ef4b8SMat Martineau 		}
185648ef4b8SMat Martineau 
186648ef4b8SMat Martineau 		if (mp_opt->use_map) {
187648ef4b8SMat Martineau 			if (mp_opt->dsn64)
188648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
189648ef4b8SMat Martineau 			else
190648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
191648ef4b8SMat Martineau 		}
192648ef4b8SMat Martineau 
193390b95a5SGeliang Tang 		/* Always parse any csum presence combination, we will enforce
194390b95a5SGeliang Tang 		 * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
195648ef4b8SMat Martineau 		 */
196648ef4b8SMat Martineau 		if (opsize != expected_opsize &&
197648ef4b8SMat Martineau 		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
198648ef4b8SMat Martineau 			break;
199648ef4b8SMat Martineau 
20074c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_DSS;
201648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
202648ef4b8SMat Martineau 			if (mp_opt->ack64) {
203648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be64(ptr);
204648ef4b8SMat Martineau 				ptr += 8;
205648ef4b8SMat Martineau 			} else {
206648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be32(ptr);
207648ef4b8SMat Martineau 				ptr += 4;
208648ef4b8SMat Martineau 			}
209648ef4b8SMat Martineau 
210*3d2e1b82SMatthieu Baerts (NGI0) 			pr_debug("data_ack=%llu\n", mp_opt->data_ack);
211648ef4b8SMat Martineau 		}
212648ef4b8SMat Martineau 
213648ef4b8SMat Martineau 		if (mp_opt->use_map) {
214648ef4b8SMat Martineau 			if (mp_opt->dsn64) {
215648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be64(ptr);
216648ef4b8SMat Martineau 				ptr += 8;
217648ef4b8SMat Martineau 			} else {
218648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be32(ptr);
219648ef4b8SMat Martineau 				ptr += 4;
220648ef4b8SMat Martineau 			}
221648ef4b8SMat Martineau 
222648ef4b8SMat Martineau 			mp_opt->subflow_seq = get_unaligned_be32(ptr);
223648ef4b8SMat Martineau 			ptr += 4;
224648ef4b8SMat Martineau 
225648ef4b8SMat Martineau 			mp_opt->data_len = get_unaligned_be16(ptr);
226648ef4b8SMat Martineau 			ptr += 2;
227648ef4b8SMat Martineau 
228390b95a5SGeliang Tang 			if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
22974c7dfbeSPaolo Abeni 				mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
230ba2c89e0SPaolo Abeni 				mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
231390b95a5SGeliang Tang 				ptr += 2;
232390b95a5SGeliang Tang 			}
233390b95a5SGeliang Tang 
234*3d2e1b82SMatthieu Baerts (NGI0) 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
235648ef4b8SMat Martineau 				 mp_opt->data_seq, mp_opt->subflow_seq,
23674c7dfbeSPaolo Abeni 				 mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
23774c7dfbeSPaolo Abeni 				 mp_opt->csum);
238648ef4b8SMat Martineau 		}
239648ef4b8SMat Martineau 
240eda7acddSPeter Krystad 		break;
241eda7acddSPeter Krystad 
2423df523abSPeter Krystad 	case MPTCPOPT_ADD_ADDR:
2433df523abSPeter Krystad 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
2443df523abSPeter Krystad 		if (!mp_opt->echo) {
2453df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
2463df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
2471b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET;
2483df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2493df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
2503df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
2511b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET6;
2523df523abSPeter Krystad #endif
2533df523abSPeter Krystad 			else
2543df523abSPeter Krystad 				break;
2553df523abSPeter Krystad 		} else {
2563df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
2573df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
2581b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET;
2593df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2603df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
2613df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
2621b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET6;
2633df523abSPeter Krystad #endif
2643df523abSPeter Krystad 			else
2653df523abSPeter Krystad 				break;
2663df523abSPeter Krystad 		}
2673df523abSPeter Krystad 
26874c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
269f7dafee1SGeliang Tang 		mp_opt->addr.id = *ptr++;
270a086aebaSPaolo Abeni 		mp_opt->addr.port = 0;
271a086aebaSPaolo Abeni 		mp_opt->ahmac = 0;
2721b1a6ef5SGeliang Tang 		if (mp_opt->addr.family == AF_INET) {
273f7dafee1SGeliang Tang 			memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
2743df523abSPeter Krystad 			ptr += 4;
2753df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
2763df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
277f7dafee1SGeliang Tang 				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
2783df523abSPeter Krystad 				ptr += 2;
2793df523abSPeter Krystad 			}
2803df523abSPeter Krystad 		}
2813df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2823df523abSPeter Krystad 		else {
283f7dafee1SGeliang Tang 			memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
2843df523abSPeter Krystad 			ptr += 16;
2853df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
2863df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
287f7dafee1SGeliang Tang 				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
2883df523abSPeter Krystad 				ptr += 2;
2893df523abSPeter Krystad 			}
2903df523abSPeter Krystad 		}
2913df523abSPeter Krystad #endif
2923df523abSPeter Krystad 		if (!mp_opt->echo) {
2933df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
2943df523abSPeter Krystad 			ptr += 8;
2953df523abSPeter Krystad 		}
296*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d\n",
2971b1a6ef5SGeliang Tang 			 (mp_opt->addr.family == AF_INET6) ? "6" : "",
298f7dafee1SGeliang Tang 			 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
2993df523abSPeter Krystad 		break;
3003df523abSPeter Krystad 
3013df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
3025c4a824dSGeliang Tang 		if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
3035c4a824dSGeliang Tang 		    opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
3043df523abSPeter Krystad 			break;
3053df523abSPeter Krystad 
3068e60eed6SGeliang Tang 		ptr++;
3078e60eed6SGeliang Tang 
30874c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
3095c4a824dSGeliang Tang 		mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
3105c4a824dSGeliang Tang 		for (i = 0; i < mp_opt->rm_list.nr; i++)
3115c4a824dSGeliang Tang 			mp_opt->rm_list.ids[i] = *ptr++;
312*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("RM_ADDR: rm_list_nr=%d\n", mp_opt->rm_list.nr);
3133df523abSPeter Krystad 		break;
3143df523abSPeter Krystad 
31540453a5cSGeliang Tang 	case MPTCPOPT_MP_PRIO:
31640453a5cSGeliang Tang 		if (opsize != TCPOLEN_MPTCP_PRIO)
31740453a5cSGeliang Tang 			break;
31840453a5cSGeliang Tang 
31974c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_PRIO;
32040453a5cSGeliang Tang 		mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
321*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("MP_PRIO: prio=%d\n", mp_opt->backup);
32240453a5cSGeliang Tang 		break;
32340453a5cSGeliang Tang 
32450c504a2SFlorian Westphal 	case MPTCPOPT_MP_FASTCLOSE:
32550c504a2SFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
32650c504a2SFlorian Westphal 			break;
32750c504a2SFlorian Westphal 
32850c504a2SFlorian Westphal 		ptr += 2;
32950c504a2SFlorian Westphal 		mp_opt->rcvr_key = get_unaligned_be64(ptr);
33050c504a2SFlorian Westphal 		ptr += 8;
33174c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
332*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("MP_FASTCLOSE: recv_key=%llu\n", mp_opt->rcvr_key);
33350c504a2SFlorian Westphal 		break;
33450c504a2SFlorian Westphal 
335dc87efdbSFlorian Westphal 	case MPTCPOPT_RST:
336dc87efdbSFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_RST)
337dc87efdbSFlorian Westphal 			break;
338dc87efdbSFlorian Westphal 
339dc87efdbSFlorian Westphal 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
340dc87efdbSFlorian Westphal 			break;
34174c7dfbeSPaolo Abeni 
34274c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_RST;
343dc87efdbSFlorian Westphal 		flags = *ptr++;
344dc87efdbSFlorian Westphal 		mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
345dc87efdbSFlorian Westphal 		mp_opt->reset_reason = *ptr;
346*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("MP_RST: transient=%u reason=%u\n",
3479ddd1cacSGeliang Tang 			 mp_opt->reset_transient, mp_opt->reset_reason);
348dc87efdbSFlorian Westphal 		break;
349dc87efdbSFlorian Westphal 
3505580d41bSGeliang Tang 	case MPTCPOPT_MP_FAIL:
3515580d41bSGeliang Tang 		if (opsize != TCPOLEN_MPTCP_FAIL)
3525580d41bSGeliang Tang 			break;
3535580d41bSGeliang Tang 
3545580d41bSGeliang Tang 		ptr += 2;
35574c7dfbeSPaolo Abeni 		mp_opt->suboptions |= OPTION_MPTCP_FAIL;
3565580d41bSGeliang Tang 		mp_opt->fail_seq = get_unaligned_be64(ptr);
357*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("MP_FAIL: data_seq=%llu\n", mp_opt->fail_seq);
3585580d41bSGeliang Tang 		break;
3595580d41bSGeliang Tang 
360eda7acddSPeter Krystad 	default:
361eda7acddSPeter Krystad 		break;
362eda7acddSPeter Krystad 	}
363eda7acddSPeter Krystad }
364eda7acddSPeter Krystad 
mptcp_get_options(const struct sk_buff * skb,struct mptcp_options_received * mp_opt)3650799e21bSGeliang Tang void mptcp_get_options(const struct sk_buff *skb,
366cfde141eSPaolo Abeni 		       struct mptcp_options_received *mp_opt)
367cec37a6eSPeter Krystad {
368cec37a6eSPeter Krystad 	const struct tcphdr *th = tcp_hdr(skb);
369cfde141eSPaolo Abeni 	const unsigned char *ptr;
370cfde141eSPaolo Abeni 	int length;
371cec37a6eSPeter Krystad 
372cfde141eSPaolo Abeni 	/* initialize option status */
37374c7dfbeSPaolo Abeni 	mp_opt->suboptions = 0;
374cfde141eSPaolo Abeni 
375cfde141eSPaolo Abeni 	length = (th->doff * 4) - sizeof(struct tcphdr);
376cec37a6eSPeter Krystad 	ptr = (const unsigned char *)(th + 1);
377cec37a6eSPeter Krystad 
378cec37a6eSPeter Krystad 	while (length > 0) {
379cec37a6eSPeter Krystad 		int opcode = *ptr++;
380cec37a6eSPeter Krystad 		int opsize;
381cec37a6eSPeter Krystad 
382cec37a6eSPeter Krystad 		switch (opcode) {
383cec37a6eSPeter Krystad 		case TCPOPT_EOL:
384cec37a6eSPeter Krystad 			return;
385cec37a6eSPeter Krystad 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
386cec37a6eSPeter Krystad 			length--;
387cec37a6eSPeter Krystad 			continue;
388cec37a6eSPeter Krystad 		default:
38907718be2SMaxim Mikityanskiy 			if (length < 2)
39007718be2SMaxim Mikityanskiy 				return;
391cec37a6eSPeter Krystad 			opsize = *ptr++;
392cec37a6eSPeter Krystad 			if (opsize < 2) /* "silly options" */
393cec37a6eSPeter Krystad 				return;
394cec37a6eSPeter Krystad 			if (opsize > length)
395cec37a6eSPeter Krystad 				return;	/* don't parse partial options */
396cec37a6eSPeter Krystad 			if (opcode == TCPOPT_MPTCP)
397cfde141eSPaolo Abeni 				mptcp_parse_option(skb, ptr, opsize, mp_opt);
398cec37a6eSPeter Krystad 			ptr += opsize - 2;
399cec37a6eSPeter Krystad 			length -= opsize;
400cec37a6eSPeter Krystad 		}
401cec37a6eSPeter Krystad 	}
402cec37a6eSPeter Krystad }
403cec37a6eSPeter Krystad 
mptcp_syn_options(struct sock * sk,const struct sk_buff * skb,unsigned int * size,struct mptcp_out_options * opts)404cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
405cc7972eaSChristoph Paasch 		       unsigned int *size, struct mptcp_out_options *opts)
406cec37a6eSPeter Krystad {
407cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
408cec37a6eSPeter Krystad 
409cc7972eaSChristoph Paasch 	/* we will use snd_isn to detect first pkt [re]transmission
410cc7972eaSChristoph Paasch 	 * in mptcp_established_options_mp()
411cc7972eaSChristoph Paasch 	 */
412cc7972eaSChristoph Paasch 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
413cec37a6eSPeter Krystad 	if (subflow->request_mptcp) {
414cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
41506fe1719SGeliang Tang 		opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
416bab6b88eSGeliang Tang 		opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
417cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYN;
418cec37a6eSPeter Krystad 		return true;
419ec3edaa7SPeter Krystad 	} else if (subflow->request_join) {
420*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("remote_token=%u, nonce=%u\n", subflow->remote_token,
421ec3edaa7SPeter Krystad 			 subflow->local_nonce);
422ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
423ec3edaa7SPeter Krystad 		opts->join_id = subflow->local_id;
424ec3edaa7SPeter Krystad 		opts->token = subflow->remote_token;
425ec3edaa7SPeter Krystad 		opts->nonce = subflow->local_nonce;
426ec3edaa7SPeter Krystad 		opts->backup = subflow->request_bkup;
427ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYN;
428ec3edaa7SPeter Krystad 		return true;
429cec37a6eSPeter Krystad 	}
430cec37a6eSPeter Krystad 	return false;
431cec37a6eSPeter Krystad }
432cec37a6eSPeter Krystad 
clear_3rdack_retransmission(struct sock * sk)433ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk)
434ec3edaa7SPeter Krystad {
435ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
436ec3edaa7SPeter Krystad 
437ec3edaa7SPeter Krystad 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
438ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = 0;
439ec3edaa7SPeter Krystad 	icsk->icsk_ack.ato = 0;
440ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
441ec3edaa7SPeter Krystad }
442ec3edaa7SPeter Krystad 
mptcp_established_options_mp(struct sock * sk,struct sk_buff * skb,bool snd_data_fin_enable,unsigned int * size,struct mptcp_out_options * opts)443cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
444d87903b6SPaolo Abeni 					 bool snd_data_fin_enable,
445cc7972eaSChristoph Paasch 					 unsigned int *size,
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 
503*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d\n",
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;
512*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("subflow=%p\n", 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 
mptcp_write_data_fin(struct mptcp_subflow_context * subflow,struct sk_buff * skb,struct mptcp_ext * ext)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 
mptcp_established_options_dss(struct sock * sk,struct sk_buff * skb,bool snd_data_fin_enable,unsigned int * size,struct mptcp_out_options * opts)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 					  struct mptcp_out_options *opts)
5606d0060f6SMat Martineau {
5616d0060f6SMat Martineau 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
5627279da61SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
5636d0060f6SMat Martineau 	unsigned int dss_size = 0;
5646d0060f6SMat Martineau 	struct mptcp_ext *mpext;
5656d0060f6SMat Martineau 	unsigned int ack_size;
566d22f4988SChristoph Paasch 	bool ret = false;
567d87903b6SPaolo Abeni 	u64 ack_seq;
5686d0060f6SMat Martineau 
569c5b39e26SGeliang Tang 	opts->csum_reqd = READ_ONCE(msk->csum_enabled);
5700bac966aSMat Martineau 	mpext = skb ? mptcp_get_ext(skb) : NULL;
5716d0060f6SMat Martineau 
5727279da61SMat Martineau 	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
573c5b39e26SGeliang Tang 		unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
5746d0060f6SMat Martineau 
575c5b39e26SGeliang Tang 		if (mpext) {
576c5b39e26SGeliang Tang 			if (opts->csum_reqd)
577c5b39e26SGeliang Tang 				map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
578c5b39e26SGeliang Tang 
579c5b39e26SGeliang Tang 			opts->ext_copy = *mpext;
580c5b39e26SGeliang Tang 		}
5816d0060f6SMat Martineau 
5826d0060f6SMat Martineau 		dss_size = map_size;
5837279da61SMat Martineau 		if (skb && snd_data_fin_enable)
5849c29e361SPaolo Abeni 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
5851bff1e43SPaolo Abeni 		opts->suboptions = OPTION_MPTCP_DSS;
586d22f4988SChristoph Paasch 		ret = true;
587d22f4988SChristoph Paasch 	}
588d22f4988SChristoph Paasch 
5892398e399SPaolo Abeni 	/* passive sockets msk will set the 'can_ack' after accept(), even
5902398e399SPaolo Abeni 	 * if the first subflow may have the already the remote key handy
5912398e399SPaolo Abeni 	 */
592d22f4988SChristoph Paasch 	opts->ext_copy.use_ack = 0;
593dc093db5SPaolo Abeni 	if (!READ_ONCE(msk->can_ack)) {
594d22f4988SChristoph Paasch 		*size = ALIGN(dss_size, 4);
595d22f4988SChristoph Paasch 		return ret;
5966d0060f6SMat Martineau 	}
5976d0060f6SMat Martineau 
598e3859603SPaolo Abeni 	ack_seq = READ_ONCE(msk->ack_seq);
59937198e93SDavide Caratti 	if (READ_ONCE(msk->use_64bit_ack)) {
6006d0060f6SMat Martineau 		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
601e3859603SPaolo Abeni 		opts->ext_copy.data_ack = ack_seq;
602a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 1;
603a0c1d0eaSChristoph Paasch 	} else {
604a0c1d0eaSChristoph Paasch 		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
605e3859603SPaolo Abeni 		opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
606a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 0;
607a0c1d0eaSChristoph Paasch 	}
608a0c1d0eaSChristoph Paasch 	opts->ext_copy.use_ack = 1;
6091bff1e43SPaolo Abeni 	opts->suboptions = OPTION_MPTCP_DSS;
610ea4ca586SPaolo Abeni 	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
6116d0060f6SMat Martineau 
6126d0060f6SMat Martineau 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
6136d0060f6SMat Martineau 	if (dss_size == 0)
6146d0060f6SMat Martineau 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
6156d0060f6SMat Martineau 
6166d0060f6SMat Martineau 	dss_size += ack_size;
6176d0060f6SMat Martineau 
6186d0060f6SMat Martineau 	*size = ALIGN(dss_size, 4);
6196d0060f6SMat Martineau 	return true;
6206d0060f6SMat Martineau }
6216d0060f6SMat Martineau 
add_addr_generate_hmac(u64 key1,u64 key2,struct mptcp_addr_info * addr)622761c124eSGeliang Tang static u64 add_addr_generate_hmac(u64 key1, u64 key2,
623761c124eSGeliang Tang 				  struct mptcp_addr_info *addr)
6243df523abSPeter Krystad {
625761c124eSGeliang Tang 	u16 port = ntohs(addr->port);
626bd697222STodd Malsbary 	u8 hmac[SHA256_DIGEST_SIZE];
6273df523abSPeter Krystad 	u8 msg[19];
628761c124eSGeliang Tang 	int i = 0;
6293df523abSPeter Krystad 
630761c124eSGeliang Tang 	msg[i++] = addr->id;
631761c124eSGeliang Tang 	if (addr->family == AF_INET) {
632761c124eSGeliang Tang 		memcpy(&msg[i], &addr->addr.s_addr, 4);
633761c124eSGeliang Tang 		i += 4;
634761c124eSGeliang Tang 	}
635761c124eSGeliang Tang #if IS_ENABLED(CONFIG_MPTCP_IPV6)
636761c124eSGeliang Tang 	else if (addr->family == AF_INET6) {
637761c124eSGeliang Tang 		memcpy(&msg[i], &addr->addr6.s6_addr, 16);
638761c124eSGeliang Tang 		i += 16;
639761c124eSGeliang Tang 	}
640761c124eSGeliang Tang #endif
641761c124eSGeliang Tang 	msg[i++] = port >> 8;
642761c124eSGeliang Tang 	msg[i++] = port & 0xFF;
6433df523abSPeter Krystad 
644761c124eSGeliang Tang 	mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
6453df523abSPeter Krystad 
646bd697222STodd Malsbary 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
6473df523abSPeter Krystad }
6483df523abSPeter Krystad 
mptcp_established_options_add_addr(struct sock * sk,struct sk_buff * skb,unsigned int * size,unsigned int remaining,struct mptcp_out_options * opts)64984dfe367SGeliang Tang static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
6503df523abSPeter Krystad 					       unsigned int *size,
6513df523abSPeter Krystad 					       unsigned int remaining,
6523df523abSPeter Krystad 					       struct mptcp_out_options *opts)
6533df523abSPeter Krystad {
6543df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
6553df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
65684dfe367SGeliang Tang 	bool drop_other_suboptions = false;
65784dfe367SGeliang Tang 	unsigned int opt_size = *size;
6586a6c05a8SGeliang Tang 	bool echo;
6591b1c7a0eSPeter Krystad 	int len;
6603df523abSPeter Krystad 
6611f5e9e2fSYonglong Li 	/* add addr will strip the existing options, be sure to avoid breaking
6621f5e9e2fSYonglong Li 	 * MPC/MPJ handshakes
6631f5e9e2fSYonglong Li 	 */
664f643b803SGeliang Tang 	if (!mptcp_pm_should_add_signal(msk) ||
6651f5e9e2fSYonglong Li 	    (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
6661f5e9e2fSYonglong Li 	    !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
667af7939f3SGeliang Tang 		    &echo, &drop_other_suboptions))
6683df523abSPeter Krystad 		return false;
6691b1c7a0eSPeter Krystad 
6701f5e9e2fSYonglong Li 	if (drop_other_suboptions)
6711f5e9e2fSYonglong Li 		remaining += opt_size;
672af7939f3SGeliang Tang 	len = mptcp_add_addr_len(opts->addr.family, echo, !!opts->addr.port);
6731b1c7a0eSPeter Krystad 	if (remaining < len)
6741b1c7a0eSPeter Krystad 		return false;
6751b1c7a0eSPeter Krystad 
6761b1c7a0eSPeter Krystad 	*size = len;
6771f5e9e2fSYonglong Li 	if (drop_other_suboptions) {
678*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("drop other suboptions\n");
6791f5e9e2fSYonglong Li 		opts->suboptions = 0;
6801bff1e43SPaolo Abeni 
6811bff1e43SPaolo Abeni 		/* note that e.g. DSS could have written into the memory
6821bff1e43SPaolo Abeni 		 * aliased by ahmac, we must reset the field here
6831bff1e43SPaolo Abeni 		 * to avoid appending the hmac even for ADD_ADDR echo
6841bff1e43SPaolo Abeni 		 * options
6851bff1e43SPaolo Abeni 		 */
6861bff1e43SPaolo Abeni 		opts->ahmac = 0;
68784dfe367SGeliang Tang 		*size -= opt_size;
6881f5e9e2fSYonglong Li 	}
6893df523abSPeter Krystad 	opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
6906a6c05a8SGeliang Tang 	if (!echo) {
69145b1a122SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
6923df523abSPeter Krystad 		opts->ahmac = add_addr_generate_hmac(msk->local_key,
6933df523abSPeter Krystad 						     msk->remote_key,
694761c124eSGeliang Tang 						     &opts->addr);
69545b1a122SPaolo Abeni 	} else {
69645b1a122SPaolo Abeni 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
6973df523abSPeter Krystad 	}
698*3d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d\n",
69930f60baeSGeliang Tang 		 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
7003df523abSPeter Krystad 
7013df523abSPeter Krystad 	return true;
7023df523abSPeter Krystad }
7033df523abSPeter Krystad 
mptcp_established_options_rm_addr(struct sock * sk,unsigned int * size,unsigned int remaining,struct mptcp_out_options * opts)7045cb104aeSGeliang Tang static bool mptcp_established_options_rm_addr(struct sock *sk,
7055cb104aeSGeliang Tang 					      unsigned int *size,
7065cb104aeSGeliang Tang 					      unsigned int remaining,
7075cb104aeSGeliang Tang 					      struct mptcp_out_options *opts)
7085cb104aeSGeliang Tang {
7095cb104aeSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
7105cb104aeSGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
7116445e17aSGeliang Tang 	struct mptcp_rm_list rm_list;
7126445e17aSGeliang Tang 	int i, len;
7135cb104aeSGeliang Tang 
7145cb104aeSGeliang Tang 	if (!mptcp_pm_should_rm_signal(msk) ||
7156445e17aSGeliang Tang 	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
7165cb104aeSGeliang Tang 		return false;
7175cb104aeSGeliang Tang 
7186445e17aSGeliang Tang 	len = mptcp_rm_addr_len(&rm_list);
7196445e17aSGeliang Tang 	if (len < 0)
7206445e17aSGeliang Tang 		return false;
7216445e17aSGeliang Tang 	if (remaining < len)
7225cb104aeSGeliang Tang 		return false;
7235cb104aeSGeliang Tang 
7246445e17aSGeliang Tang 	*size = len;
7255cb104aeSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
7266445e17aSGeliang Tang 	opts->rm_list = rm_list;
7275cb104aeSGeliang Tang 
7286445e17aSGeliang Tang 	for (i = 0; i < opts->rm_list.nr; i++)
729*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("rm_list_ids[%d]=%d\n", i, opts->rm_list.ids[i]);
73045b1a122SPaolo Abeni 	MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
7315cb104aeSGeliang Tang 	return true;
7325cb104aeSGeliang Tang }
7335cb104aeSGeliang Tang 
mptcp_established_options_mp_prio(struct sock * sk,unsigned int * size,unsigned int remaining,struct mptcp_out_options * opts)73406706542SGeliang Tang static bool mptcp_established_options_mp_prio(struct sock *sk,
73506706542SGeliang Tang 					      unsigned int *size,
73606706542SGeliang Tang 					      unsigned int remaining,
73706706542SGeliang Tang 					      struct mptcp_out_options *opts)
73806706542SGeliang Tang {
73906706542SGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
74006706542SGeliang Tang 
7411bff1e43SPaolo Abeni 	/* can't send MP_PRIO with MPC, as they share the same option space:
7421bff1e43SPaolo Abeni 	 * 'backup'. Also it makes no sense at all
7431bff1e43SPaolo Abeni 	 */
74413ac17a3SGeliang Tang 	if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
74506706542SGeliang Tang 		return false;
74606706542SGeliang Tang 
747ec99a470SDavide Caratti 	/* account for the trailing 'nop' option */
748ec99a470SDavide Caratti 	if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
74906706542SGeliang Tang 		return false;
75006706542SGeliang Tang 
751ec99a470SDavide Caratti 	*size = TCPOLEN_MPTCP_PRIO_ALIGN;
75206706542SGeliang Tang 	opts->suboptions |= OPTION_MPTCP_PRIO;
75306706542SGeliang Tang 	opts->backup = subflow->request_bkup;
75406706542SGeliang Tang 
755*3d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("prio=%d\n", opts->backup);
75606706542SGeliang Tang 
75706706542SGeliang Tang 	return true;
75806706542SGeliang Tang }
75906706542SGeliang Tang 
mptcp_established_options_rst(struct sock * sk,struct sk_buff * skb,unsigned int * size,unsigned int remaining,struct mptcp_out_options * opts)760c25aeb4eSGeliang Tang static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
761dc87efdbSFlorian Westphal 						   unsigned int *size,
762dc87efdbSFlorian Westphal 						   unsigned int remaining,
763dc87efdbSFlorian Westphal 						   struct mptcp_out_options *opts)
764dc87efdbSFlorian Westphal {
765dc87efdbSFlorian Westphal 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
766dc87efdbSFlorian Westphal 
767dc87efdbSFlorian Westphal 	if (remaining < TCPOLEN_MPTCP_RST)
768c25aeb4eSGeliang Tang 		return false;
769dc87efdbSFlorian Westphal 
770dc87efdbSFlorian Westphal 	*size = TCPOLEN_MPTCP_RST;
771dc87efdbSFlorian Westphal 	opts->suboptions |= OPTION_MPTCP_RST;
772dc87efdbSFlorian Westphal 	opts->reset_transient = subflow->reset_transient;
773dc87efdbSFlorian Westphal 	opts->reset_reason = subflow->reset_reason;
7740c1f78a4SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
775c25aeb4eSGeliang Tang 
776c25aeb4eSGeliang Tang 	return true;
777c25aeb4eSGeliang Tang }
778c25aeb4eSGeliang Tang 
mptcp_established_options_fastclose(struct sock * sk,unsigned int * size,unsigned int remaining,struct mptcp_out_options * opts)779f284c0c7SPaolo Abeni static bool mptcp_established_options_fastclose(struct sock *sk,
780f284c0c7SPaolo Abeni 						unsigned int *size,
781f284c0c7SPaolo Abeni 						unsigned int remaining,
782f284c0c7SPaolo Abeni 						struct mptcp_out_options *opts)
783f284c0c7SPaolo Abeni {
784f284c0c7SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
785f284c0c7SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
786f284c0c7SPaolo Abeni 
787f284c0c7SPaolo Abeni 	if (likely(!subflow->send_fastclose))
788f284c0c7SPaolo Abeni 		return false;
789f284c0c7SPaolo Abeni 
790f284c0c7SPaolo Abeni 	if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
791f284c0c7SPaolo Abeni 		return false;
792f284c0c7SPaolo Abeni 
793f284c0c7SPaolo Abeni 	*size = TCPOLEN_MPTCP_FASTCLOSE;
794f284c0c7SPaolo Abeni 	opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
795f284c0c7SPaolo Abeni 	opts->rcvr_key = msk->remote_key;
796f284c0c7SPaolo Abeni 
797*3d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("FASTCLOSE key=%llu\n", opts->rcvr_key);
7980c1f78a4SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
799f284c0c7SPaolo Abeni 	return true;
800f284c0c7SPaolo Abeni }
801f284c0c7SPaolo Abeni 
mptcp_established_options_mp_fail(struct sock * sk,unsigned int * size,unsigned int remaining,struct mptcp_out_options * opts)802c25aeb4eSGeliang Tang static bool mptcp_established_options_mp_fail(struct sock *sk,
803c25aeb4eSGeliang Tang 					      unsigned int *size,
804c25aeb4eSGeliang Tang 					      unsigned int remaining,
805c25aeb4eSGeliang Tang 					      struct mptcp_out_options *opts)
806c25aeb4eSGeliang Tang {
807c25aeb4eSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
808c25aeb4eSGeliang Tang 
809c25aeb4eSGeliang Tang 	if (likely(!subflow->send_mp_fail))
810c25aeb4eSGeliang Tang 		return false;
811c25aeb4eSGeliang Tang 
812c25aeb4eSGeliang Tang 	if (remaining < TCPOLEN_MPTCP_FAIL)
813c25aeb4eSGeliang Tang 		return false;
814c25aeb4eSGeliang Tang 
815c25aeb4eSGeliang Tang 	*size = TCPOLEN_MPTCP_FAIL;
816c25aeb4eSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_FAIL;
817c25aeb4eSGeliang Tang 	opts->fail_seq = subflow->map_seq;
818c25aeb4eSGeliang Tang 
819*3d2e1b82SMatthieu Baerts (NGI0) 	pr_debug("MP_FAIL fail_seq=%llu\n", opts->fail_seq);
8200c1f78a4SPaolo Abeni 	MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
821c25aeb4eSGeliang Tang 
822c25aeb4eSGeliang Tang 	return true;
823dc87efdbSFlorian Westphal }
824dc87efdbSFlorian Westphal 
mptcp_established_options(struct sock * sk,struct sk_buff * skb,unsigned int * size,unsigned int remaining,struct mptcp_out_options * opts)8256d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
8266d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
8276d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
8286d0060f6SMat Martineau {
829d87903b6SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
830d87903b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
8316d0060f6SMat Martineau 	unsigned int opt_size = 0;
832d87903b6SPaolo Abeni 	bool snd_data_fin;
8336d0060f6SMat Martineau 	bool ret = false;
8346d0060f6SMat Martineau 
8353df523abSPeter Krystad 	opts->suboptions = 0;
8363df523abSPeter Krystad 
8371e39e5a3SGeliang Tang 	if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
838e1ff9e82SDavide Caratti 		return false;
839e1ff9e82SDavide Caratti 
840dc87efdbSFlorian Westphal 	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
841f284c0c7SPaolo Abeni 		if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
842f284c0c7SPaolo Abeni 		    mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
843c25aeb4eSGeliang Tang 			*size += opt_size;
844c25aeb4eSGeliang Tang 			remaining -= opt_size;
845c25aeb4eSGeliang Tang 		}
846f284c0c7SPaolo Abeni 		/* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
847c25aeb4eSGeliang Tang 		if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
848c25aeb4eSGeliang Tang 			*size += opt_size;
849c25aeb4eSGeliang Tang 			remaining -= opt_size;
850c25aeb4eSGeliang Tang 		}
851dc87efdbSFlorian Westphal 		return true;
852dc87efdbSFlorian Westphal 	}
853d5824847SPaolo Abeni 
854d87903b6SPaolo Abeni 	snd_data_fin = mptcp_data_fin_enabled(msk);
855ce395d0eSMatthieu Baerts 	if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
8566d0060f6SMat Martineau 		ret = true;
857ce395d0eSMatthieu Baerts 	else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
85804fac2caSMatthieu Baerts 		unsigned int mp_fail_size;
85904fac2caSMatthieu Baerts 
8606d0060f6SMat Martineau 		ret = true;
86104fac2caSMatthieu Baerts 		if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
86204fac2caSMatthieu Baerts 						      remaining - opt_size, opts)) {
86304fac2caSMatthieu Baerts 			*size += opt_size + mp_fail_size;
86404fac2caSMatthieu Baerts 			remaining -= opt_size - mp_fail_size;
865c25aeb4eSGeliang Tang 			return true;
866c25aeb4eSGeliang Tang 		}
867c25aeb4eSGeliang Tang 	}
8686d0060f6SMat Martineau 
8696d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
8706d0060f6SMat Martineau 	 * TCP option space would be fatal
8716d0060f6SMat Martineau 	 */
8726d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
8736d0060f6SMat Martineau 		return false;
8746d0060f6SMat Martineau 
8756d0060f6SMat Martineau 	*size += opt_size;
8766d0060f6SMat Martineau 	remaining -= opt_size;
87784dfe367SGeliang Tang 	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
8783df523abSPeter Krystad 		*size += opt_size;
8793df523abSPeter Krystad 		remaining -= opt_size;
8803df523abSPeter Krystad 		ret = true;
8815cb104aeSGeliang Tang 	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
8825cb104aeSGeliang Tang 		*size += opt_size;
8835cb104aeSGeliang Tang 		remaining -= opt_size;
8845cb104aeSGeliang Tang 		ret = true;
8853df523abSPeter Krystad 	}
8866d0060f6SMat Martineau 
88706706542SGeliang Tang 	if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
88806706542SGeliang Tang 		*size += opt_size;
88906706542SGeliang Tang 		remaining -= opt_size;
89006706542SGeliang Tang 		ret = true;
89106706542SGeliang Tang 	}
89206706542SGeliang Tang 
8936d0060f6SMat Martineau 	return ret;
8946d0060f6SMat Martineau }
8956d0060f6SMat Martineau 
mptcp_synack_options(const struct request_sock * req,unsigned int * size,struct mptcp_out_options * opts)896cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
897cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
898cec37a6eSPeter Krystad {
899cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
900cec37a6eSPeter Krystad 
901cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
902cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
903cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
90406fe1719SGeliang Tang 		opts->csum_reqd = subflow_req->csum_reqd;
905bab6b88eSGeliang Tang 		opts->allow_join_id0 = subflow_req->allow_join_id0;
906cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
907*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("subflow_req=%p, local_key=%llu\n",
908cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
909cec37a6eSPeter Krystad 		return true;
910f296234cSPeter Krystad 	} else if (subflow_req->mp_join) {
911f296234cSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
9128ed3e34cSMatthieu Baerts (NGI0) 		opts->backup = subflow_req->request_bkup;
913f296234cSPeter Krystad 		opts->join_id = subflow_req->local_id;
914f296234cSPeter Krystad 		opts->thmac = subflow_req->thmac;
915f296234cSPeter Krystad 		opts->nonce = subflow_req->local_nonce;
916*3d2e1b82SMatthieu Baerts (NGI0) 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
917f296234cSPeter Krystad 			 subflow_req, opts->backup, opts->join_id,
918f296234cSPeter Krystad 			 opts->thmac, opts->nonce);
919f296234cSPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
920f296234cSPeter Krystad 		return true;
921cec37a6eSPeter Krystad 	}
922cec37a6eSPeter Krystad 	return false;
923cec37a6eSPeter Krystad }
924cec37a6eSPeter Krystad 
check_fully_established(struct mptcp_sock * msk,struct sock * ssk,struct mptcp_subflow_context * subflow,struct sk_buff * skb,struct mptcp_options_received * mp_opt)925d5824847SPaolo Abeni static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
926f296234cSPeter Krystad 				    struct mptcp_subflow_context *subflow,
927d22f4988SChristoph Paasch 				    struct sk_buff *skb,
928d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
929d22f4988SChristoph Paasch {
930d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
931f296234cSPeter Krystad 	 * will make the subflow fully established
932d22f4988SChristoph Paasch 	 */
933f296234cSPeter Krystad 	if (likely(subflow->fully_established)) {
934f296234cSPeter Krystad 		/* on passive sockets, check for 3rd ack retransmission
935f296234cSPeter Krystad 		 * note that msk is always set by subflow_syn_recv_sock()
936f296234cSPeter Krystad 		 * for mp_join subflows
937f296234cSPeter Krystad 		 */
938f296234cSPeter Krystad 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
939f296234cSPeter Krystad 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
94074c7dfbeSPaolo Abeni 		    subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
94170c708e8SKishen Maloor 		    !subflow->request_join)
942d5824847SPaolo Abeni 			tcp_send_ack(ssk);
943dfc8d060SDmytro Shytyi 		goto check_notify;
944f296234cSPeter Krystad 	}
945d22f4988SChristoph Paasch 
946d5824847SPaolo Abeni 	/* we must process OoO packets before the first subflow is fully
947d5824847SPaolo Abeni 	 * established. OoO packets are instead a protocol violation
948d5824847SPaolo Abeni 	 * for MP_JOIN subflows as the peer must not send any data
949d5824847SPaolo Abeni 	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
950f296234cSPeter Krystad 	 */
951d5824847SPaolo Abeni 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
952d5824847SPaolo Abeni 		if (subflow->mp_join)
953d5824847SPaolo Abeni 			goto reset;
954dfc8d060SDmytro Shytyi 		if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
955dfc8d060SDmytro Shytyi 			goto set_fully_established;
956f296234cSPeter Krystad 		return subflow->mp_capable;
957d5824847SPaolo Abeni 	}
958f296234cSPeter Krystad 
959b3ea6b27SPaolo Abeni 	if (subflow->remote_key_valid &&
960b3ea6b27SPaolo Abeni 	    (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
96107bb418cSMatthieu Baerts (NGI0) 	     ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) &&
96207bb418cSMatthieu Baerts (NGI0) 	      (!mp_opt->echo || subflow->mp_join)))) {
963f296234cSPeter Krystad 		/* subflows are fully established as soon as we get any
96467b12f79SMatthieu Baerts 		 * additional ack, including ADD_ADDR.
965f296234cSPeter Krystad 		 */
966b45df837SPaolo Abeni 		goto set_fully_established;
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 
985d6085fe1SPaolo Abeni 	if (unlikely(!READ_ONCE(msk->pm.server_side)))
986d6085fe1SPaolo Abeni 		pr_warn_once("bogus mpc option on established client sk");
987b45df837SPaolo Abeni 
988fca4e6abSMatthieu Baerts (NGI0) set_fully_established:
989b45df837SPaolo Abeni 	mptcp_data_lock((struct sock *)msk);
990b45df837SPaolo Abeni 	__mptcp_subflow_fully_established(msk, subflow, mp_opt);
991b45df837SPaolo Abeni 	mptcp_data_unlock((struct sock *)msk);
992f296234cSPeter Krystad 
993dfc8d060SDmytro Shytyi check_notify:
9945b950ff4SPaolo Abeni 	/* if the subflow is not already linked into the conn_list, we can't
9955b950ff4SPaolo Abeni 	 * notify the PM: this subflow is still on the listener queue
9965b950ff4SPaolo Abeni 	 * and the PM possibly acquiring the subflow lock could race with
9975b950ff4SPaolo Abeni 	 * the listener close
9985b950ff4SPaolo Abeni 	 */
9995b950ff4SPaolo Abeni 	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
1000f296234cSPeter Krystad 		return true;
1001f296234cSPeter Krystad 
1002f296234cSPeter Krystad 	subflow->pm_notified = 1;
1003ec3edaa7SPeter Krystad 	if (subflow->mp_join) {
1004d5824847SPaolo Abeni 		clear_3rdack_retransmission(ssk);
100562535200SGeliang Tang 		mptcp_pm_subflow_established(msk);
1006ec3edaa7SPeter Krystad 	} else {
10077a486c44SPaolo Abeni 		mptcp_pm_fully_established(msk, ssk);
1008ec3edaa7SPeter Krystad 	}
1009d22f4988SChristoph Paasch 	return true;
1010d5824847SPaolo Abeni 
1011d5824847SPaolo Abeni reset:
1012d5824847SPaolo Abeni 	mptcp_subflow_reset(ssk);
1013d5824847SPaolo Abeni 	return false;
1014d22f4988SChristoph Paasch }
1015d22f4988SChristoph Paasch 
__mptcp_expand_seq(u64 old_seq,u64 cur_seq)10161502328fSPaolo Abeni u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1017cc9d2566SPaolo Abeni {
10181502328fSPaolo Abeni 	u32 old_seq32, cur_seq32;
1019cc9d2566SPaolo Abeni 
10201502328fSPaolo Abeni 	old_seq32 = (u32)old_seq;
10211502328fSPaolo Abeni 	cur_seq32 = (u32)cur_seq;
10221502328fSPaolo Abeni 	cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
10231502328fSPaolo Abeni 	if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
10241502328fSPaolo Abeni 		return cur_seq + (1LL << 32);
1025cc9d2566SPaolo Abeni 
10261502328fSPaolo Abeni 	/* reverse wrap could happen, too */
10271502328fSPaolo Abeni 	if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
10281502328fSPaolo Abeni 		return cur_seq - (1LL << 32);
10291502328fSPaolo Abeni 	return cur_seq;
1030cc9d2566SPaolo Abeni }
1031cc9d2566SPaolo Abeni 
__mptcp_snd_una_update(struct mptcp_sock * msk,u64 new_snd_una)103238967f42SPaolo Abeni static void __mptcp_snd_una_update(struct mptcp_sock *msk, u64 new_snd_una)
103338967f42SPaolo Abeni {
103438967f42SPaolo Abeni 	msk->bytes_acked += new_snd_una - msk->snd_una;
103538967f42SPaolo Abeni 	msk->snd_una = new_snd_una;
103638967f42SPaolo Abeni }
103738967f42SPaolo Abeni 
ack_update_msk(struct mptcp_sock * msk,struct sock * ssk,struct mptcp_options_received * mp_opt)10386f8a612aSFlorian Westphal static void ack_update_msk(struct mptcp_sock *msk,
10396e628cd3SPaolo Abeni 			   struct sock *ssk,
1040cc9d2566SPaolo Abeni 			   struct mptcp_options_received *mp_opt)
1041cc9d2566SPaolo Abeni {
10427439d687SPaolo Abeni 	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
10436f8a612aSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
10447439d687SPaolo Abeni 	u64 old_snd_una;
10457439d687SPaolo Abeni 
10467439d687SPaolo Abeni 	mptcp_data_lock(sk);
1047cc9d2566SPaolo Abeni 
1048cc9d2566SPaolo Abeni 	/* avoid ack expansion on update conflict, to reduce the risk of
1049cc9d2566SPaolo Abeni 	 * wrongly expanding to a future ack sequence number, which is way
1050cc9d2566SPaolo Abeni 	 * more dangerous than missing an ack
1051cc9d2566SPaolo Abeni 	 */
10527439d687SPaolo Abeni 	old_snd_una = msk->snd_una;
10531502328fSPaolo Abeni 	new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1054cc9d2566SPaolo Abeni 
10550d199e43SFlorian Westphal 	/* ACK for data not even sent yet? Ignore.*/
10560d199e43SFlorian Westphal 	if (unlikely(after64(new_snd_una, snd_nxt)))
1057cc9d2566SPaolo Abeni 		new_snd_una = old_snd_una;
1058cc9d2566SPaolo Abeni 
10596f8a612aSFlorian Westphal 	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
10606f8a612aSFlorian Westphal 
1061219d0499SPaolo Abeni 	if (after64(new_wnd_end, msk->wnd_end))
10627439d687SPaolo Abeni 		msk->wnd_end = new_wnd_end;
1063219d0499SPaolo Abeni 
1064219d0499SPaolo Abeni 	/* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1065d09d818eSPaolo Abeni 	if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
1066219d0499SPaolo Abeni 		__mptcp_check_push(sk, ssk);
10676f8a612aSFlorian Westphal 
10687439d687SPaolo Abeni 	if (after64(new_snd_una, old_snd_una)) {
106938967f42SPaolo Abeni 		__mptcp_snd_una_update(msk, new_snd_una);
10707439d687SPaolo Abeni 		__mptcp_data_acked(sk);
1071cc9d2566SPaolo Abeni 	}
10727439d687SPaolo Abeni 	mptcp_data_unlock(sk);
1073ed66bfb4SGeliang Tang 
1074ed66bfb4SGeliang Tang 	trace_ack_update_msk(mp_opt->data_ack,
1075ed66bfb4SGeliang Tang 			     old_snd_una, new_snd_una,
1076ed66bfb4SGeliang Tang 			     new_wnd_end, msk->wnd_end);
1077b51f9b80SPaolo Abeni }
1078cc9d2566SPaolo Abeni 
mptcp_update_rcv_data_fin(struct mptcp_sock * msk,u64 data_fin_seq,bool use_64bit)10791a49b2c2SMat Martineau bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
10803721b9b6SMat Martineau {
10813721b9b6SMat Martineau 	/* Skip if DATA_FIN was already received.
10823721b9b6SMat Martineau 	 * If updating simultaneously with the recvmsg loop, values
10833721b9b6SMat Martineau 	 * should match. If they mismatch, the peer is misbehaving and
10843721b9b6SMat Martineau 	 * we will prefer the most recent information.
10853721b9b6SMat Martineau 	 */
1086781bf13dSPaolo Abeni 	if (READ_ONCE(msk->rcv_data_fin))
10873721b9b6SMat Martineau 		return false;
10883721b9b6SMat Martineau 
10891a49b2c2SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin_seq,
10901502328fSPaolo Abeni 		   mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
10913721b9b6SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin, 1);
10923721b9b6SMat Martineau 
10933721b9b6SMat Martineau 	return true;
10943721b9b6SMat Martineau }
10953721b9b6SMat Martineau 
add_addr_hmac_valid(struct mptcp_sock * msk,struct mptcp_options_received * mp_opt)10961b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk,
10971b1c7a0eSPeter Krystad 				struct mptcp_options_received *mp_opt)
10981b1c7a0eSPeter Krystad {
10991b1c7a0eSPeter Krystad 	u64 hmac = 0;
11001b1c7a0eSPeter Krystad 
11011b1c7a0eSPeter Krystad 	if (mp_opt->echo)
11021b1c7a0eSPeter Krystad 		return true;
11031b1c7a0eSPeter Krystad 
11041b1c7a0eSPeter Krystad 	hmac = add_addr_generate_hmac(msk->remote_key,
11051b1c7a0eSPeter Krystad 				      msk->local_key,
1106761c124eSGeliang Tang 				      &mp_opt->addr);
11071b1c7a0eSPeter Krystad 
11081b1c7a0eSPeter Krystad 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1109742e2f36SGeliang Tang 		 msk, hmac, mp_opt->ahmac);
11101b1c7a0eSPeter Krystad 
11111b1c7a0eSPeter Krystad 	return hmac == mp_opt->ahmac;
11121b1c7a0eSPeter Krystad }
11131b1c7a0eSPeter Krystad 
11146787b7e3SJianguo Wu /* Return false if a subflow has been reset, else return true */
mptcp_incoming_options(struct sock * sk,struct sk_buff * skb)11156787b7e3SJianguo Wu bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1116648ef4b8SMat Martineau {
1117d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
11181b1c7a0eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1119cfde141eSPaolo Abeni 	struct mptcp_options_received mp_opt;
1120648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
1121648ef4b8SMat Martineau 
11226e628cd3SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
11236e628cd3SPaolo Abeni 		/* Keep it simple and unconditionally trigger send data cleanup and
11246e628cd3SPaolo Abeni 		 * pending queue spooling. We will need to acquire the data lock
11256e628cd3SPaolo Abeni 		 * for more accurate checks, and once the lock is acquired, such
11266e628cd3SPaolo Abeni 		 * helpers are cheap.
11276e628cd3SPaolo Abeni 		 */
11286e628cd3SPaolo Abeni 		mptcp_data_lock(subflow->conn);
1129219d0499SPaolo Abeni 		if (sk_stream_memory_free(sk))
1130219d0499SPaolo Abeni 			__mptcp_check_push(subflow->conn, sk);
1131c026d33bSPaolo Abeni 
1132c026d33bSPaolo Abeni 		/* on fallback we just need to ignore the msk-level snd_una, as
1133c026d33bSPaolo Abeni 		 * this is really plain TCP
1134c026d33bSPaolo Abeni 		 */
113538967f42SPaolo Abeni 		__mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
1136c026d33bSPaolo Abeni 
11376e628cd3SPaolo Abeni 		__mptcp_data_acked(subflow->conn);
11386e628cd3SPaolo Abeni 		mptcp_data_unlock(subflow->conn);
11396787b7e3SJianguo Wu 		return true;
11406e628cd3SPaolo Abeni 	}
1141e1ff9e82SDavide Caratti 
11420799e21bSGeliang Tang 	mptcp_get_options(skb, &mp_opt);
11436787b7e3SJianguo Wu 
11446787b7e3SJianguo Wu 	/* The subflow can be in close state only if check_fully_established()
11456787b7e3SJianguo Wu 	 * just sent a reset. If so, tell the caller to ignore the current packet.
11466787b7e3SJianguo Wu 	 */
1147cfde141eSPaolo Abeni 	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
11486787b7e3SJianguo Wu 		return sk->sk_state != TCP_CLOSE;
1149648ef4b8SMat Martineau 
1150f6c2ef59SPaolo Abeni 	if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
115174c7dfbeSPaolo Abeni 		if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
115250c504a2SFlorian Westphal 		    msk->local_key == mp_opt.rcvr_key) {
115350c504a2SFlorian Westphal 			WRITE_ONCE(msk->rcv_fastclose, true);
115450c504a2SFlorian Westphal 			mptcp_schedule_work((struct sock *)msk);
11551e75629cSGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
115650c504a2SFlorian Westphal 		}
115750c504a2SFlorian Westphal 
1158f6c2ef59SPaolo Abeni 		if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1159f6c2ef59SPaolo Abeni 		    add_addr_hmac_valid(msk, &mp_opt)) {
1160a877de06SGeliang Tang 			if (!mp_opt.echo) {
1161d1ace2d9SKishen Maloor 				mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1162a877de06SGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1163a877de06SGeliang Tang 			} else {
1164f7dafee1SGeliang Tang 				mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1165d58300c3SDavide Caratti 				mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1166a877de06SGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1167a877de06SGeliang Tang 			}
11682fbdd9eaSGeliang Tang 
1169f7dafee1SGeliang Tang 			if (mp_opt.addr.port)
11702fbdd9eaSGeliang Tang 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
11711b1c7a0eSPeter Krystad 		}
11721b1c7a0eSPeter Krystad 
117374c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
11745c4a824dSGeliang Tang 			mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1175d0876b22SGeliang Tang 
117674c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
117740453a5cSGeliang Tang 			mptcp_pm_mp_prio_received(sk, mp_opt.backup);
11780be2ac28SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
117940453a5cSGeliang Tang 		}
118040453a5cSGeliang Tang 
118174c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
11825580d41bSGeliang Tang 			mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1183eb7f3365SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
11845580d41bSGeliang Tang 		}
11855580d41bSGeliang Tang 
118674c7dfbeSPaolo Abeni 		if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1187dc87efdbSFlorian Westphal 			subflow->reset_seen = 1;
1188dc87efdbSFlorian Westphal 			subflow->reset_reason = mp_opt.reset_reason;
1189dc87efdbSFlorian Westphal 			subflow->reset_transient = mp_opt.reset_transient;
1190e40dd439SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1191dc87efdbSFlorian Westphal 		}
1192dc87efdbSFlorian Westphal 
119374c7dfbeSPaolo Abeni 		if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
11946787b7e3SJianguo Wu 			return true;
1195f6c2ef59SPaolo Abeni 	}
1196648ef4b8SMat Martineau 
1197cc9d2566SPaolo Abeni 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
1198cc9d2566SPaolo Abeni 	 * monodirectional flows will stuck
1199cc9d2566SPaolo Abeni 	 */
1200cfde141eSPaolo Abeni 	if (mp_opt.use_ack)
12016f8a612aSFlorian Westphal 		ack_update_msk(msk, sk, &mp_opt);
1202cc9d2566SPaolo Abeni 
120306827b34SMat Martineau 	/* Zero-data-length packets are dropped by the caller and not
120406827b34SMat Martineau 	 * propagated to the MPTCP layer, so the skb extension does not
120506827b34SMat Martineau 	 * need to be allocated or populated. DATA_FIN information, if
120606827b34SMat Martineau 	 * present, needs to be updated here before the skb is freed.
120743b54c6eSMat Martineau 	 */
120843b54c6eSMat Martineau 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
120943b54c6eSMat Martineau 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
1210a5cb752bSPaolo Abeni 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
1211a5cb752bSPaolo Abeni 			mptcp_schedule_work((struct sock *)msk);
121206827b34SMat Martineau 
12136787b7e3SJianguo Wu 		return true;
121443b54c6eSMat Martineau 	}
121543b54c6eSMat Martineau 
1216648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1217648ef4b8SMat Martineau 	if (!mpext)
12186787b7e3SJianguo Wu 		return true;
1219648ef4b8SMat Martineau 
1220648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
1221648ef4b8SMat Martineau 
1222f6c2ef59SPaolo Abeni 	if (likely(mp_opt.use_map)) {
1223cfde141eSPaolo Abeni 		if (mp_opt.mpc_map) {
1224cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
1225cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
1226cc7972eaSChristoph Paasch 			 */
1227cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
1228cc7972eaSChristoph Paasch 					     &mpext->data_seq);
1229cc7972eaSChristoph Paasch 			mpext->data_seq++;
1230cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
1231cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
1232cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
1233a77895dbSPaolo Abeni 			mpext->data_fin = 0;
1234cc7972eaSChristoph Paasch 		} else {
1235cfde141eSPaolo Abeni 			mpext->data_seq = mp_opt.data_seq;
1236cfde141eSPaolo Abeni 			mpext->subflow_seq = mp_opt.subflow_seq;
1237cfde141eSPaolo Abeni 			mpext->dsn64 = mp_opt.dsn64;
1238cfde141eSPaolo Abeni 			mpext->data_fin = mp_opt.data_fin;
1239cc7972eaSChristoph Paasch 		}
1240cfde141eSPaolo Abeni 		mpext->data_len = mp_opt.data_len;
1241648ef4b8SMat Martineau 		mpext->use_map = 1;
124274c7dfbeSPaolo Abeni 		mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1243208e8f66SGeliang Tang 
1244208e8f66SGeliang Tang 		if (mpext->csum_reqd)
1245208e8f66SGeliang Tang 			mpext->csum = mp_opt.csum;
1246648ef4b8SMat Martineau 	}
12476787b7e3SJianguo Wu 
12486787b7e3SJianguo Wu 	return true;
1249648ef4b8SMat Martineau }
1250648ef4b8SMat Martineau 
mptcp_set_rwin(struct tcp_sock * tp,struct tcphdr * th)1251f3589be0SPaolo Abeni static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
1252fa3fe2b1SFlorian Westphal {
1253fa3fe2b1SFlorian Westphal 	const struct sock *ssk = (const struct sock *)tp;
1254f3589be0SPaolo Abeni 	struct mptcp_subflow_context *subflow;
1255f3589be0SPaolo Abeni 	u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
1256fa3fe2b1SFlorian Westphal 	struct mptcp_sock *msk;
1257f3589be0SPaolo Abeni 	u32 new_win;
1258f3589be0SPaolo Abeni 	u64 win;
1259fa3fe2b1SFlorian Westphal 
1260fa3fe2b1SFlorian Westphal 	subflow = mptcp_subflow_ctx(ssk);
1261fa3fe2b1SFlorian Westphal 	msk = mptcp_sk(subflow->conn);
1262fa3fe2b1SFlorian Westphal 
1263f3589be0SPaolo Abeni 	ack_seq = READ_ONCE(msk->ack_seq);
1264f3589be0SPaolo Abeni 	rcv_wnd_new = ack_seq + tp->rcv_wnd;
1265fa3fe2b1SFlorian Westphal 
1266f3589be0SPaolo Abeni 	rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1267f3589be0SPaolo Abeni 	if (after64(rcv_wnd_new, rcv_wnd_old)) {
1268f3589be0SPaolo Abeni 		u64 rcv_wnd;
1269f3589be0SPaolo Abeni 
1270f3589be0SPaolo Abeni 		for (;;) {
1271f3589be0SPaolo Abeni 			rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1272f3589be0SPaolo Abeni 
1273f3589be0SPaolo Abeni 			if (rcv_wnd == rcv_wnd_old)
1274f3589be0SPaolo Abeni 				break;
12756bec0411SPaolo Abeni 
12766bec0411SPaolo Abeni 			rcv_wnd_old = rcv_wnd;
12776bec0411SPaolo Abeni 			if (before64(rcv_wnd_new, rcv_wnd_old)) {
127838acb626SPaolo Abeni 				MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1279f3589be0SPaolo Abeni 				goto raise_win;
128038acb626SPaolo Abeni 			}
128138acb626SPaolo Abeni 			MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1282f3589be0SPaolo Abeni 		}
1283f3589be0SPaolo Abeni 		return;
1284f3589be0SPaolo Abeni 	}
1285f3589be0SPaolo Abeni 
1286f3589be0SPaolo Abeni 	if (rcv_wnd_new != rcv_wnd_old) {
1287f3589be0SPaolo Abeni raise_win:
1288f3589be0SPaolo Abeni 		win = rcv_wnd_old - ack_seq;
1289f3589be0SPaolo Abeni 		tp->rcv_wnd = min_t(u64, win, U32_MAX);
1290f3589be0SPaolo Abeni 		new_win = tp->rcv_wnd;
1291f3589be0SPaolo Abeni 
1292f3589be0SPaolo Abeni 		/* Make sure we do not exceed the maximum possible
1293f3589be0SPaolo Abeni 		 * scaled window.
1294f3589be0SPaolo Abeni 		 */
1295f3589be0SPaolo Abeni 		if (unlikely(th->syn))
1296f3589be0SPaolo Abeni 			new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1297f3589be0SPaolo Abeni 		if (!tp->rx_opt.rcv_wscale &&
12980f1e4d06SKuniyuki Iwashima 		    READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1299f3589be0SPaolo Abeni 			new_win = min(new_win, MAX_TCP_WINDOW);
1300f3589be0SPaolo Abeni 		else
1301f3589be0SPaolo Abeni 			new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1302f3589be0SPaolo Abeni 
1303f3589be0SPaolo Abeni 		/* RFC1323 scaling applied */
1304f3589be0SPaolo Abeni 		new_win >>= tp->rx_opt.rcv_wscale;
1305f3589be0SPaolo Abeni 		th->window = htons(new_win);
130638acb626SPaolo Abeni 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1307f3589be0SPaolo Abeni 	}
1308fa3fe2b1SFlorian Westphal }
1309fa3fe2b1SFlorian Westphal 
__mptcp_make_csum(u64 data_seq,u32 subflow_seq,u16 data_len,__wsum sum)1310ba2c89e0SPaolo Abeni __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1311c94b1f96SGeliang Tang {
1312c94b1f96SGeliang Tang 	struct csum_pseudo_header header;
1313c94b1f96SGeliang Tang 	__wsum csum;
1314c94b1f96SGeliang Tang 
1315c94b1f96SGeliang Tang 	/* cfr RFC 8684 3.3.1.:
1316c94b1f96SGeliang Tang 	 * the data sequence number used in the pseudo-header is
1317c94b1f96SGeliang Tang 	 * always the 64-bit value, irrespective of what length is used in the
1318c94b1f96SGeliang Tang 	 * DSS option itself.
1319c94b1f96SGeliang Tang 	 */
1320f7cc8890SDavide Caratti 	header.data_seq = cpu_to_be64(data_seq);
1321f7cc8890SDavide Caratti 	header.subflow_seq = htonl(subflow_seq);
1322f7cc8890SDavide Caratti 	header.data_len = htons(data_len);
1323c94b1f96SGeliang Tang 	header.csum = 0;
1324c94b1f96SGeliang Tang 
1325c312ee21SGeliang Tang 	csum = csum_partial(&header, sizeof(header), sum);
1326ba2c89e0SPaolo Abeni 	return csum_fold(csum);
1327c94b1f96SGeliang Tang }
1328c94b1f96SGeliang Tang 
mptcp_make_csum(const struct mptcp_ext * mpext)1329ba2c89e0SPaolo Abeni static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1330f7cc8890SDavide Caratti {
1331f7cc8890SDavide Caratti 	return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1332c312ee21SGeliang Tang 				 ~csum_unfold(mpext->csum));
1333f7cc8890SDavide Caratti }
1334f7cc8890SDavide Caratti 
put_len_csum(u16 len,__sum16 csum,void * data)1335ba2c89e0SPaolo Abeni static void put_len_csum(u16 len, __sum16 csum, void *data)
1336ba2c89e0SPaolo Abeni {
1337ba2c89e0SPaolo Abeni 	__sum16 *sumptr = data + 2;
1338ba2c89e0SPaolo Abeni 	__be16 *ptr = data;
1339ba2c89e0SPaolo Abeni 
1340ba2c89e0SPaolo Abeni 	put_unaligned_be16(len, ptr);
1341ba2c89e0SPaolo Abeni 
1342ba2c89e0SPaolo Abeni 	put_unaligned(csum, sumptr);
1343ba2c89e0SPaolo Abeni }
1344ba2c89e0SPaolo Abeni 
mptcp_write_options(struct tcphdr * th,__be32 * ptr,struct tcp_sock * tp,struct mptcp_out_options * opts)1345ea66758cSPaolo Abeni void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1346fa3fe2b1SFlorian Westphal 			 struct mptcp_out_options *opts)
1347eda7acddSPeter Krystad {
1348c25aeb4eSGeliang Tang 	const struct sock *ssk = (const struct sock *)tp;
1349c25aeb4eSGeliang Tang 	struct mptcp_subflow_context *subflow;
1350c25aeb4eSGeliang Tang 
13518cca39e2SMatthieu Baerts 	/* Which options can be used together?
13528cca39e2SMatthieu Baerts 	 *
13538cca39e2SMatthieu Baerts 	 * X: mutually exclusive
13548cca39e2SMatthieu Baerts 	 * O: often used together
13558cca39e2SMatthieu Baerts 	 * C: can be used together in some cases
13568cca39e2SMatthieu Baerts 	 * P: could be used together but we prefer not to (optimisations)
13578cca39e2SMatthieu Baerts 	 *
13588cca39e2SMatthieu Baerts 	 *  Opt: | MPC  | MPJ  | DSS  | ADD  |  RM  | PRIO | FAIL |  FC  |
13598cca39e2SMatthieu Baerts 	 * ------|------|------|------|------|------|------|------|------|
13608cca39e2SMatthieu Baerts 	 *  MPC  |------|------|------|------|------|------|------|------|
13618cca39e2SMatthieu Baerts 	 *  MPJ  |  X   |------|------|------|------|------|------|------|
13628cca39e2SMatthieu Baerts 	 *  DSS  |  X   |  X   |------|------|------|------|------|------|
13638cca39e2SMatthieu Baerts 	 *  ADD  |  X   |  X   |  P   |------|------|------|------|------|
13648cca39e2SMatthieu Baerts 	 *  RM   |  C   |  C   |  C   |  P   |------|------|------|------|
13658cca39e2SMatthieu Baerts 	 *  PRIO |  X   |  C   |  C   |  C   |  C   |------|------|------|
13668cca39e2SMatthieu Baerts 	 *  FAIL |  X   |  X   |  C   |  X   |  X   |  X   |------|------|
13678cca39e2SMatthieu Baerts 	 *  FC   |  X   |  X   |  X   |  X   |  X   |  X   |  X   |------|
13688cca39e2SMatthieu Baerts 	 *  RST  |  X   |  X   |  X   |  X   |  X   |  X   |  O   |  O   |
13698cca39e2SMatthieu Baerts 	 * ------|------|------|------|------|------|------|------|------|
13708cca39e2SMatthieu Baerts 	 *
13718cca39e2SMatthieu Baerts 	 * The same applies in mptcp_established_options() function.
13721bff1e43SPaolo Abeni 	 */
13731bff1e43SPaolo Abeni 	if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
13741bff1e43SPaolo Abeni 		struct mptcp_ext *mpext = &opts->ext_copy;
13751bff1e43SPaolo Abeni 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
13761bff1e43SPaolo Abeni 		u8 flags = 0;
13771bff1e43SPaolo Abeni 
13781bff1e43SPaolo Abeni 		if (mpext->use_ack) {
13791bff1e43SPaolo Abeni 			flags = MPTCP_DSS_HAS_ACK;
13801bff1e43SPaolo Abeni 			if (mpext->ack64) {
13811bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_ACK64;
13821bff1e43SPaolo Abeni 				flags |= MPTCP_DSS_ACK64;
13831bff1e43SPaolo Abeni 			} else {
13841bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_ACK32;
13851bff1e43SPaolo Abeni 			}
13861bff1e43SPaolo Abeni 		}
13871bff1e43SPaolo Abeni 
13881bff1e43SPaolo Abeni 		if (mpext->use_map) {
13891bff1e43SPaolo Abeni 			len += TCPOLEN_MPTCP_DSS_MAP64;
13901bff1e43SPaolo Abeni 
13911bff1e43SPaolo Abeni 			/* Use only 64-bit mapping flags for now, add
13921bff1e43SPaolo Abeni 			 * support for optional 32-bit mappings later.
13931bff1e43SPaolo Abeni 			 */
13941bff1e43SPaolo Abeni 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
13951bff1e43SPaolo Abeni 			if (mpext->data_fin)
13961bff1e43SPaolo Abeni 				flags |= MPTCP_DSS_DATA_FIN;
13971bff1e43SPaolo Abeni 
13981bff1e43SPaolo Abeni 			if (opts->csum_reqd)
13991bff1e43SPaolo Abeni 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
14001bff1e43SPaolo Abeni 		}
14011bff1e43SPaolo Abeni 
14021bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
14031bff1e43SPaolo Abeni 
14041bff1e43SPaolo Abeni 		if (mpext->use_ack) {
14051bff1e43SPaolo Abeni 			if (mpext->ack64) {
14061bff1e43SPaolo Abeni 				put_unaligned_be64(mpext->data_ack, ptr);
14071bff1e43SPaolo Abeni 				ptr += 2;
14081bff1e43SPaolo Abeni 			} else {
14091bff1e43SPaolo Abeni 				put_unaligned_be32(mpext->data_ack32, ptr);
14101bff1e43SPaolo Abeni 				ptr += 1;
14111bff1e43SPaolo Abeni 			}
14121bff1e43SPaolo Abeni 		}
14131bff1e43SPaolo Abeni 
14141bff1e43SPaolo Abeni 		if (mpext->use_map) {
14151bff1e43SPaolo Abeni 			put_unaligned_be64(mpext->data_seq, ptr);
14161bff1e43SPaolo Abeni 			ptr += 2;
14171bff1e43SPaolo Abeni 			put_unaligned_be32(mpext->subflow_seq, ptr);
14181bff1e43SPaolo Abeni 			ptr += 1;
14191bff1e43SPaolo Abeni 			if (opts->csum_reqd) {
14201e39e5a3SGeliang Tang 				/* data_len == 0 is reserved for the infinite mapping,
14211e39e5a3SGeliang Tang 				 * the checksum will also be set to 0.
14221e39e5a3SGeliang Tang 				 */
1423ba2c89e0SPaolo Abeni 				put_len_csum(mpext->data_len,
14241e39e5a3SGeliang Tang 					     (mpext->data_len ? mptcp_make_csum(mpext) : 0),
14251e39e5a3SGeliang Tang 					     ptr);
14261bff1e43SPaolo Abeni 			} else {
14271bff1e43SPaolo Abeni 				put_unaligned_be32(mpext->data_len << 16 |
14281bff1e43SPaolo Abeni 						   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
14291bff1e43SPaolo Abeni 			}
1430110b6d1fSGeliang Tang 			ptr += 1;
14311bff1e43SPaolo Abeni 		}
1432902c8f86SMatthieu Baerts 
1433902c8f86SMatthieu Baerts 		/* We might need to add MP_FAIL options in rare cases */
1434902c8f86SMatthieu Baerts 		if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1435902c8f86SMatthieu Baerts 			goto mp_fail;
143613ac17a3SGeliang Tang 	} else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
143706fe1719SGeliang Tang 		u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1438eda7acddSPeter Krystad 
1439c94b1f96SGeliang Tang 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1440eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
1441c94b1f96SGeliang Tang 		} else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1442cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
1443f7cc8890SDavide Caratti 		} else if (opts->data_len) {
1444cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1445c94b1f96SGeliang Tang 			if (opts->csum_reqd)
1446c94b1f96SGeliang Tang 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1447c94b1f96SGeliang Tang 		} else {
1448eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
1449c94b1f96SGeliang Tang 		}
1450eda7acddSPeter Krystad 
145106fe1719SGeliang Tang 		if (opts->csum_reqd)
145206fe1719SGeliang Tang 			flag |= MPTCP_CAP_CHECKSUM_REQD;
145306fe1719SGeliang Tang 
1454bab6b88eSGeliang Tang 		if (!opts->allow_join_id0)
1455bab6b88eSGeliang Tang 			flag |= MPTCP_CAP_DENY_JOIN_ID0;
1456bab6b88eSGeliang Tang 
14573df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
14583df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
145906fe1719SGeliang Tang 				      flag);
1460cc7972eaSChristoph Paasch 
1461cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1462cc7972eaSChristoph Paasch 		    opts->suboptions))
1463cc7972eaSChristoph Paasch 			goto mp_capable_done;
1464cc7972eaSChristoph Paasch 
1465eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
1466eda7acddSPeter Krystad 		ptr += 2;
1467cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1468cc7972eaSChristoph Paasch 			goto mp_capable_done;
1469cc7972eaSChristoph Paasch 
1470eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
1471eda7acddSPeter Krystad 		ptr += 2;
1472f7cc8890SDavide Caratti 		if (!opts->data_len)
1473cc7972eaSChristoph Paasch 			goto mp_capable_done;
1474cc7972eaSChristoph Paasch 
1475c94b1f96SGeliang Tang 		if (opts->csum_reqd) {
1476ba2c89e0SPaolo Abeni 			put_len_csum(opts->data_len,
1477f7cc8890SDavide Caratti 				     __mptcp_make_csum(opts->data_seq,
1478f7cc8890SDavide Caratti 						       opts->subflow_seq,
1479f7cc8890SDavide Caratti 						       opts->data_len,
1480ba2c89e0SPaolo Abeni 						       ~csum_unfold(opts->csum)),
1481ba2c89e0SPaolo Abeni 				     ptr);
1482c94b1f96SGeliang Tang 		} else {
1483f7cc8890SDavide Caratti 			put_unaligned_be32(opts->data_len << 16 |
1484cc7972eaSChristoph Paasch 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1485c94b1f96SGeliang Tang 		}
1486cc7972eaSChristoph Paasch 		ptr += 1;
14876d0060f6SMat Martineau 
14881bff1e43SPaolo Abeni 		/* MPC is additionally mutually exclusive with MP_PRIO */
14891bff1e43SPaolo Abeni 		goto mp_capable_done;
149071b077e4SPaolo Abeni 	} else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
149171b077e4SPaolo Abeni 		if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
14921bff1e43SPaolo Abeni 			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
14931bff1e43SPaolo Abeni 					      TCPOLEN_MPTCP_MPJ_SYN,
14941bff1e43SPaolo Abeni 					      opts->backup, opts->join_id);
14951bff1e43SPaolo Abeni 			put_unaligned_be32(opts->token, ptr);
14961bff1e43SPaolo Abeni 			ptr += 1;
14971bff1e43SPaolo Abeni 			put_unaligned_be32(opts->nonce, ptr);
14981bff1e43SPaolo Abeni 			ptr += 1;
14991bff1e43SPaolo Abeni 		} else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
15001bff1e43SPaolo Abeni 			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
15011bff1e43SPaolo Abeni 					      TCPOLEN_MPTCP_MPJ_SYNACK,
15021bff1e43SPaolo Abeni 					      opts->backup, opts->join_id);
15031bff1e43SPaolo Abeni 			put_unaligned_be64(opts->thmac, ptr);
15041bff1e43SPaolo Abeni 			ptr += 2;
15051bff1e43SPaolo Abeni 			put_unaligned_be32(opts->nonce, ptr);
15061bff1e43SPaolo Abeni 			ptr += 1;
150771b077e4SPaolo Abeni 		} else {
15081bff1e43SPaolo Abeni 			*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
15091bff1e43SPaolo Abeni 					      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
15101bff1e43SPaolo Abeni 			memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
15111bff1e43SPaolo Abeni 			ptr += 5;
151271b077e4SPaolo Abeni 		}
15131bff1e43SPaolo Abeni 	} else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
15146eb3d1e3SGeliang Tang 		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
15156eb3d1e3SGeliang Tang 		u8 echo = MPTCP_ADDR_ECHO;
15163df523abSPeter Krystad 
15173df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1518fef6b7ecSGeliang Tang 		if (opts->addr.family == AF_INET6)
1519e1ef6832SGeliang Tang 			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1520e1ef6832SGeliang Tang #endif
1521e1ef6832SGeliang Tang 
152230f60baeSGeliang Tang 		if (opts->addr.port)
152322fb85ffSGeliang Tang 			len += TCPOLEN_MPTCP_PORT_LEN;
152422fb85ffSGeliang Tang 
15256eb3d1e3SGeliang Tang 		if (opts->ahmac) {
15266eb3d1e3SGeliang Tang 			len += sizeof(opts->ahmac);
15276eb3d1e3SGeliang Tang 			echo = 0;
15286eb3d1e3SGeliang Tang 		}
15296eb3d1e3SGeliang Tang 
15303df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
153130f60baeSGeliang Tang 				      len, echo, opts->addr.id);
1532fef6b7ecSGeliang Tang 		if (opts->addr.family == AF_INET) {
153330f60baeSGeliang Tang 			memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
15343df523abSPeter Krystad 			ptr += 1;
15353df523abSPeter Krystad 		}
15363df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1537fef6b7ecSGeliang Tang 		else if (opts->addr.family == AF_INET6) {
153830f60baeSGeliang Tang 			memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
15393df523abSPeter Krystad 			ptr += 4;
1540e1ef6832SGeliang Tang 		}
1541e1ef6832SGeliang Tang #endif
154222fb85ffSGeliang Tang 
154330f60baeSGeliang Tang 		if (!opts->addr.port) {
15443df523abSPeter Krystad 			if (opts->ahmac) {
15453df523abSPeter Krystad 				put_unaligned_be64(opts->ahmac, ptr);
15463df523abSPeter Krystad 				ptr += 2;
15473df523abSPeter Krystad 			}
154822fb85ffSGeliang Tang 		} else {
154930f60baeSGeliang Tang 			u16 port = ntohs(opts->addr.port);
155030f60baeSGeliang Tang 
155122fb85ffSGeliang Tang 			if (opts->ahmac) {
155222fb85ffSGeliang Tang 				u8 *bptr = (u8 *)ptr;
155322fb85ffSGeliang Tang 
155430f60baeSGeliang Tang 				put_unaligned_be16(port, bptr);
155522fb85ffSGeliang Tang 				bptr += 2;
155622fb85ffSGeliang Tang 				put_unaligned_be64(opts->ahmac, bptr);
155722fb85ffSGeliang Tang 				bptr += 8;
155822fb85ffSGeliang Tang 				put_unaligned_be16(TCPOPT_NOP << 8 |
155922fb85ffSGeliang Tang 						   TCPOPT_NOP, bptr);
156022fb85ffSGeliang Tang 
156122fb85ffSGeliang Tang 				ptr += 3;
156222fb85ffSGeliang Tang 			} else {
156330f60baeSGeliang Tang 				put_unaligned_be32(port << 16 |
156422fb85ffSGeliang Tang 						   TCPOPT_NOP << 8 |
156522fb85ffSGeliang Tang 						   TCPOPT_NOP, ptr);
156622fb85ffSGeliang Tang 				ptr += 1;
15673df523abSPeter Krystad 			}
156822fb85ffSGeliang Tang 		}
1569f284c0c7SPaolo Abeni 	} else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1570f284c0c7SPaolo Abeni 		/* FASTCLOSE is mutually exclusive with others except RST */
1571f284c0c7SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1572f284c0c7SPaolo Abeni 				      TCPOLEN_MPTCP_FASTCLOSE,
1573f284c0c7SPaolo Abeni 				      0, 0);
1574f284c0c7SPaolo Abeni 		put_unaligned_be64(opts->rcvr_key, ptr);
1575f284c0c7SPaolo Abeni 		ptr += 2;
1576f284c0c7SPaolo Abeni 
1577f284c0c7SPaolo Abeni 		if (OPTION_MPTCP_RST & opts->suboptions)
1578f284c0c7SPaolo Abeni 			goto mp_rst;
1579f284c0c7SPaolo Abeni 		return;
1580902c8f86SMatthieu Baerts 	} else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1581902c8f86SMatthieu Baerts mp_fail:
1582902c8f86SMatthieu Baerts 		/* MP_FAIL is mutually exclusive with others except RST */
1583902c8f86SMatthieu Baerts 		subflow = mptcp_subflow_ctx(ssk);
1584902c8f86SMatthieu Baerts 		subflow->send_mp_fail = 0;
1585902c8f86SMatthieu Baerts 
1586902c8f86SMatthieu Baerts 		*ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1587902c8f86SMatthieu Baerts 				      TCPOLEN_MPTCP_FAIL,
1588902c8f86SMatthieu Baerts 				      0, 0);
1589902c8f86SMatthieu Baerts 		put_unaligned_be64(opts->fail_seq, ptr);
1590902c8f86SMatthieu Baerts 		ptr += 2;
1591902c8f86SMatthieu Baerts 
1592902c8f86SMatthieu Baerts 		if (OPTION_MPTCP_RST & opts->suboptions)
1593902c8f86SMatthieu Baerts 			goto mp_rst;
1594902c8f86SMatthieu Baerts 		return;
1595f284c0c7SPaolo Abeni 	} else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1596f284c0c7SPaolo Abeni mp_rst:
1597f284c0c7SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_RST,
1598f284c0c7SPaolo Abeni 				      TCPOLEN_MPTCP_RST,
1599f284c0c7SPaolo Abeni 				      opts->reset_transient,
1600f284c0c7SPaolo Abeni 				      opts->reset_reason);
1601f284c0c7SPaolo Abeni 		return;
16023df523abSPeter Krystad 	}
16033df523abSPeter Krystad 
16041bff1e43SPaolo Abeni 	if (OPTION_MPTCP_PRIO & opts->suboptions) {
16051bff1e43SPaolo Abeni 		subflow = mptcp_subflow_ctx(ssk);
16061bff1e43SPaolo Abeni 		subflow->send_mp_prio = 0;
16071bff1e43SPaolo Abeni 
16081bff1e43SPaolo Abeni 		*ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
16091bff1e43SPaolo Abeni 				      TCPOLEN_MPTCP_PRIO,
16101bff1e43SPaolo Abeni 				      opts->backup, TCPOPT_NOP);
1611c21b50d5SMat Martineau 
16123c976f4cSGeliang Tang 		MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
16131bff1e43SPaolo Abeni 	}
16141bff1e43SPaolo Abeni 
16151bff1e43SPaolo Abeni mp_capable_done:
16163df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
16176445e17aSGeliang Tang 		u8 i = 1;
16186445e17aSGeliang Tang 
16193df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
16206445e17aSGeliang Tang 				      TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
16216445e17aSGeliang Tang 				      0, opts->rm_list.ids[0]);
16226445e17aSGeliang Tang 
16236445e17aSGeliang Tang 		while (i < opts->rm_list.nr) {
16246445e17aSGeliang Tang 			u8 id1, id2, id3, id4;
16256445e17aSGeliang Tang 
16266445e17aSGeliang Tang 			id1 = opts->rm_list.ids[i];
16276445e17aSGeliang Tang 			id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
16286445e17aSGeliang Tang 			id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
16296445e17aSGeliang Tang 			id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
16306445e17aSGeliang Tang 			put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
16316445e17aSGeliang Tang 			ptr += 1;
16326445e17aSGeliang Tang 			i += 4;
16336445e17aSGeliang Tang 		}
16343df523abSPeter Krystad 	}
16353df523abSPeter Krystad 
1636fa3fe2b1SFlorian Westphal 	if (tp)
1637f3589be0SPaolo Abeni 		mptcp_set_rwin(tp, th);
1638eda7acddSPeter Krystad }
1639dc87efdbSFlorian Westphal 
mptcp_get_reset_option(const struct sk_buff * skb)1640dc87efdbSFlorian Westphal __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1641dc87efdbSFlorian Westphal {
1642dc87efdbSFlorian Westphal 	const struct mptcp_ext *ext = mptcp_get_ext(skb);
1643dc87efdbSFlorian Westphal 	u8 flags, reason;
1644dc87efdbSFlorian Westphal 
1645dc87efdbSFlorian Westphal 	if (ext) {
1646dc87efdbSFlorian Westphal 		flags = ext->reset_transient;
1647dc87efdbSFlorian Westphal 		reason = ext->reset_reason;
1648dc87efdbSFlorian Westphal 
1649dc87efdbSFlorian Westphal 		return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1650dc87efdbSFlorian Westphal 				    flags, reason);
1651dc87efdbSFlorian Westphal 	}
1652dc87efdbSFlorian Westphal 
1653dc87efdbSFlorian Westphal 	return htonl(0u);
1654dc87efdbSFlorian Westphal }
1655dc87efdbSFlorian Westphal EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
1656