xref: /openbmc/linux/net/mptcp/options.c (revision c863225b79426459feca2ef5b0cc2f07e8e68771)
1eda7acddSPeter Krystad // SPDX-License-Identifier: GPL-2.0
2eda7acddSPeter Krystad /* Multipath TCP
3eda7acddSPeter Krystad  *
4eda7acddSPeter Krystad  * Copyright (c) 2017 - 2019, Intel Corporation.
5eda7acddSPeter Krystad  */
6eda7acddSPeter Krystad 
7c85adcedSGeliang Tang #define pr_fmt(fmt) "MPTCP: " fmt
8c85adcedSGeliang Tang 
9eda7acddSPeter Krystad #include <linux/kernel.h>
10a24d22b2SEric Biggers #include <crypto/sha2.h>
11eda7acddSPeter Krystad #include <net/tcp.h>
12eda7acddSPeter Krystad #include <net/mptcp.h>
13eda7acddSPeter Krystad #include "protocol.h"
14a877de06SGeliang Tang #include "mib.h"
15eda7acddSPeter Krystad 
16ed66bfb4SGeliang Tang #include <trace/events/mptcp.h>
17ed66bfb4SGeliang Tang 
1865492c5aSPaolo Abeni static bool mptcp_cap_flag_sha256(u8 flags)
1965492c5aSPaolo Abeni {
2065492c5aSPaolo Abeni 	return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
2165492c5aSPaolo Abeni }
2265492c5aSPaolo Abeni 
23cfde141eSPaolo Abeni static void mptcp_parse_option(const struct sk_buff *skb,
24cfde141eSPaolo Abeni 			       const unsigned char *ptr, int opsize,
25cfde141eSPaolo Abeni 			       struct mptcp_options_received *mp_opt)
26eda7acddSPeter Krystad {
27eda7acddSPeter Krystad 	u8 subtype = *ptr >> 4;
28648ef4b8SMat Martineau 	int expected_opsize;
29eda7acddSPeter Krystad 	u8 version;
30eda7acddSPeter Krystad 	u8 flags;
315c4a824dSGeliang Tang 	u8 i;
32eda7acddSPeter Krystad 
33eda7acddSPeter Krystad 	switch (subtype) {
34eda7acddSPeter Krystad 	case MPTCPOPT_MP_CAPABLE:
35cc7972eaSChristoph Paasch 		/* strict size checking */
36cc7972eaSChristoph Paasch 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
37cc7972eaSChristoph Paasch 			if (skb->len > tcp_hdr(skb)->doff << 2)
38cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
39cc7972eaSChristoph Paasch 			else
40cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
41cc7972eaSChristoph Paasch 		} else {
42cc7972eaSChristoph Paasch 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)
43cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
44cc7972eaSChristoph Paasch 			else
45cc7972eaSChristoph Paasch 				expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
46cc7972eaSChristoph Paasch 		}
47cc7972eaSChristoph Paasch 		if (opsize != expected_opsize)
48eda7acddSPeter Krystad 			break;
49eda7acddSPeter Krystad 
50cc7972eaSChristoph Paasch 		/* try to be gentle vs future versions on the initial syn */
51eda7acddSPeter Krystad 		version = *ptr++ & MPTCP_VERSION_MASK;
52cc7972eaSChristoph Paasch 		if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
53eda7acddSPeter Krystad 			if (version != MPTCP_SUPPORTED_VERSION)
54eda7acddSPeter Krystad 				break;
55cc7972eaSChristoph Paasch 		} else if (version < MPTCP_SUPPORTED_VERSION) {
56cc7972eaSChristoph Paasch 			break;
57cc7972eaSChristoph Paasch 		}
58eda7acddSPeter Krystad 
59eda7acddSPeter Krystad 		flags = *ptr++;
6065492c5aSPaolo Abeni 		if (!mptcp_cap_flag_sha256(flags) ||
61eda7acddSPeter Krystad 		    (flags & MPTCP_CAP_EXTENSIBILITY))
62eda7acddSPeter Krystad 			break;
63eda7acddSPeter Krystad 
64eda7acddSPeter Krystad 		/* RFC 6824, Section 3.1:
65eda7acddSPeter Krystad 		 * "For the Checksum Required bit (labeled "A"), if either
66eda7acddSPeter Krystad 		 * host requires the use of checksums, checksums MUST be used.
67eda7acddSPeter Krystad 		 * In other words, the only way for checksums not to be used
68eda7acddSPeter Krystad 		 * is if both hosts in their SYNs set A=0."
69eda7acddSPeter Krystad 		 *
70eda7acddSPeter Krystad 		 * Section 3.3.0:
71eda7acddSPeter Krystad 		 * "If a checksum is not present when its use has been
72eda7acddSPeter Krystad 		 * negotiated, the receiver MUST close the subflow with a RST as
73eda7acddSPeter Krystad 		 * it is considered broken."
74eda7acddSPeter Krystad 		 *
75eda7acddSPeter Krystad 		 * We don't implement DSS checksum - fall back to TCP.
76eda7acddSPeter Krystad 		 */
77eda7acddSPeter Krystad 		if (flags & MPTCP_CAP_CHECKSUM_REQD)
78eda7acddSPeter Krystad 			break;
79eda7acddSPeter Krystad 
80eda7acddSPeter Krystad 		mp_opt->mp_capable = 1;
81cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
82eda7acddSPeter Krystad 			mp_opt->sndr_key = get_unaligned_be64(ptr);
83eda7acddSPeter Krystad 			ptr += 8;
84cc7972eaSChristoph Paasch 		}
85cc7972eaSChristoph Paasch 		if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
86eda7acddSPeter Krystad 			mp_opt->rcvr_key = get_unaligned_be64(ptr);
87eda7acddSPeter Krystad 			ptr += 8;
88eda7acddSPeter Krystad 		}
89cc7972eaSChristoph Paasch 		if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA) {
90cc7972eaSChristoph Paasch 			/* Section 3.1.:
91cc7972eaSChristoph Paasch 			 * "the data parameters in a MP_CAPABLE are semantically
92cc7972eaSChristoph Paasch 			 * equivalent to those in a DSS option and can be used
93cc7972eaSChristoph Paasch 			 * interchangeably."
94cc7972eaSChristoph Paasch 			 */
95cc7972eaSChristoph Paasch 			mp_opt->dss = 1;
96cc7972eaSChristoph Paasch 			mp_opt->use_map = 1;
97cc7972eaSChristoph Paasch 			mp_opt->mpc_map = 1;
98cc7972eaSChristoph Paasch 			mp_opt->data_len = get_unaligned_be16(ptr);
99cc7972eaSChristoph Paasch 			ptr += 2;
100cc7972eaSChristoph Paasch 		}
101cc7972eaSChristoph Paasch 		pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d",
102cc7972eaSChristoph Paasch 			 version, flags, opsize, mp_opt->sndr_key,
103cc7972eaSChristoph Paasch 			 mp_opt->rcvr_key, mp_opt->data_len);
104eda7acddSPeter Krystad 		break;
105eda7acddSPeter Krystad 
106f296234cSPeter Krystad 	case MPTCPOPT_MP_JOIN:
107f296234cSPeter Krystad 		mp_opt->mp_join = 1;
108f296234cSPeter Krystad 		if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
109f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
110f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
111f296234cSPeter Krystad 			mp_opt->token = get_unaligned_be32(ptr);
112f296234cSPeter Krystad 			ptr += 4;
113f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
114f296234cSPeter Krystad 			ptr += 4;
115f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u",
116f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
117f296234cSPeter Krystad 				 mp_opt->token, mp_opt->nonce);
118f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
119f296234cSPeter Krystad 			mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
120f296234cSPeter Krystad 			mp_opt->join_id = *ptr++;
121f296234cSPeter Krystad 			mp_opt->thmac = get_unaligned_be64(ptr);
122f296234cSPeter Krystad 			ptr += 8;
123f296234cSPeter Krystad 			mp_opt->nonce = get_unaligned_be32(ptr);
124f296234cSPeter Krystad 			ptr += 4;
125f296234cSPeter Krystad 			pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u",
126f296234cSPeter Krystad 				 mp_opt->backup, mp_opt->join_id,
127f296234cSPeter Krystad 				 mp_opt->thmac, mp_opt->nonce);
128f296234cSPeter Krystad 		} else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
129f296234cSPeter Krystad 			ptr += 2;
130f296234cSPeter Krystad 			memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
131f296234cSPeter Krystad 			pr_debug("MP_JOIN hmac");
132f296234cSPeter Krystad 		} else {
133f296234cSPeter Krystad 			mp_opt->mp_join = 0;
134f296234cSPeter Krystad 		}
135f296234cSPeter Krystad 		break;
136f296234cSPeter Krystad 
137eda7acddSPeter Krystad 	case MPTCPOPT_DSS:
138eda7acddSPeter Krystad 		pr_debug("DSS");
139648ef4b8SMat Martineau 		ptr++;
140648ef4b8SMat Martineau 
141cc7972eaSChristoph Paasch 		/* we must clear 'mpc_map' be able to detect MP_CAPABLE
142cc7972eaSChristoph Paasch 		 * map vs DSS map in mptcp_incoming_options(), and reconstruct
143cc7972eaSChristoph Paasch 		 * map info accordingly
144cc7972eaSChristoph Paasch 		 */
145cc7972eaSChristoph Paasch 		mp_opt->mpc_map = 0;
146648ef4b8SMat Martineau 		flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
147648ef4b8SMat Martineau 		mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
148648ef4b8SMat Martineau 		mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
149648ef4b8SMat Martineau 		mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
150648ef4b8SMat Martineau 		mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
151648ef4b8SMat Martineau 		mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
152648ef4b8SMat Martineau 
153648ef4b8SMat Martineau 		pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d",
154648ef4b8SMat Martineau 			 mp_opt->data_fin, mp_opt->dsn64,
155648ef4b8SMat Martineau 			 mp_opt->use_map, mp_opt->ack64,
156648ef4b8SMat Martineau 			 mp_opt->use_ack);
157648ef4b8SMat Martineau 
158648ef4b8SMat Martineau 		expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
159648ef4b8SMat Martineau 
160648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
161648ef4b8SMat Martineau 			if (mp_opt->ack64)
162648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
163648ef4b8SMat Martineau 			else
164648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
165648ef4b8SMat Martineau 		}
166648ef4b8SMat Martineau 
167648ef4b8SMat Martineau 		if (mp_opt->use_map) {
168648ef4b8SMat Martineau 			if (mp_opt->dsn64)
169648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
170648ef4b8SMat Martineau 			else
171648ef4b8SMat Martineau 				expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
172648ef4b8SMat Martineau 		}
173648ef4b8SMat Martineau 
174648ef4b8SMat Martineau 		/* RFC 6824, Section 3.3:
175648ef4b8SMat Martineau 		 * If a checksum is present, but its use had
176648ef4b8SMat Martineau 		 * not been negotiated in the MP_CAPABLE handshake,
177648ef4b8SMat Martineau 		 * the checksum field MUST be ignored.
178648ef4b8SMat Martineau 		 */
179648ef4b8SMat Martineau 		if (opsize != expected_opsize &&
180648ef4b8SMat Martineau 		    opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
181648ef4b8SMat Martineau 			break;
182648ef4b8SMat Martineau 
183eda7acddSPeter Krystad 		mp_opt->dss = 1;
184648ef4b8SMat Martineau 
185648ef4b8SMat Martineau 		if (mp_opt->use_ack) {
186648ef4b8SMat Martineau 			if (mp_opt->ack64) {
187648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be64(ptr);
188648ef4b8SMat Martineau 				ptr += 8;
189648ef4b8SMat Martineau 			} else {
190648ef4b8SMat Martineau 				mp_opt->data_ack = get_unaligned_be32(ptr);
191648ef4b8SMat Martineau 				ptr += 4;
192648ef4b8SMat Martineau 			}
193648ef4b8SMat Martineau 
194648ef4b8SMat Martineau 			pr_debug("data_ack=%llu", mp_opt->data_ack);
195648ef4b8SMat Martineau 		}
196648ef4b8SMat Martineau 
197648ef4b8SMat Martineau 		if (mp_opt->use_map) {
198648ef4b8SMat Martineau 			if (mp_opt->dsn64) {
199648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be64(ptr);
200648ef4b8SMat Martineau 				ptr += 8;
201648ef4b8SMat Martineau 			} else {
202648ef4b8SMat Martineau 				mp_opt->data_seq = get_unaligned_be32(ptr);
203648ef4b8SMat Martineau 				ptr += 4;
204648ef4b8SMat Martineau 			}
205648ef4b8SMat Martineau 
206648ef4b8SMat Martineau 			mp_opt->subflow_seq = get_unaligned_be32(ptr);
207648ef4b8SMat Martineau 			ptr += 4;
208648ef4b8SMat Martineau 
209648ef4b8SMat Martineau 			mp_opt->data_len = get_unaligned_be16(ptr);
210648ef4b8SMat Martineau 			ptr += 2;
211648ef4b8SMat Martineau 
212648ef4b8SMat Martineau 			pr_debug("data_seq=%llu subflow_seq=%u data_len=%u",
213648ef4b8SMat Martineau 				 mp_opt->data_seq, mp_opt->subflow_seq,
214648ef4b8SMat Martineau 				 mp_opt->data_len);
215648ef4b8SMat Martineau 		}
216648ef4b8SMat Martineau 
217eda7acddSPeter Krystad 		break;
218eda7acddSPeter Krystad 
2193df523abSPeter Krystad 	case MPTCPOPT_ADD_ADDR:
2203df523abSPeter Krystad 		mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
2213df523abSPeter Krystad 		if (!mp_opt->echo) {
2223df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
2233df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
2241b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET;
2253df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2263df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
2273df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
2281b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET6;
2293df523abSPeter Krystad #endif
2303df523abSPeter Krystad 			else
2313df523abSPeter Krystad 				break;
2323df523abSPeter Krystad 		} else {
2333df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
2343df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
2351b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET;
2363df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2373df523abSPeter Krystad 			else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
2383df523abSPeter Krystad 				 opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
2391b1a6ef5SGeliang Tang 				mp_opt->addr.family = AF_INET6;
2403df523abSPeter Krystad #endif
2413df523abSPeter Krystad 			else
2423df523abSPeter Krystad 				break;
2433df523abSPeter Krystad 		}
2443df523abSPeter Krystad 
2453df523abSPeter Krystad 		mp_opt->add_addr = 1;
246f7dafee1SGeliang Tang 		mp_opt->addr.id = *ptr++;
2471b1a6ef5SGeliang Tang 		if (mp_opt->addr.family == AF_INET) {
248f7dafee1SGeliang Tang 			memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
2493df523abSPeter Krystad 			ptr += 4;
2503df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
2513df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
252f7dafee1SGeliang Tang 				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
2533df523abSPeter Krystad 				ptr += 2;
2543df523abSPeter Krystad 			}
2553df523abSPeter Krystad 		}
2563df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
2573df523abSPeter Krystad 		else {
258f7dafee1SGeliang Tang 			memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
2593df523abSPeter Krystad 			ptr += 16;
2603df523abSPeter Krystad 			if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
2613df523abSPeter Krystad 			    opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
262f7dafee1SGeliang Tang 				mp_opt->addr.port = htons(get_unaligned_be16(ptr));
2633df523abSPeter Krystad 				ptr += 2;
2643df523abSPeter Krystad 			}
2653df523abSPeter Krystad 		}
2663df523abSPeter Krystad #endif
2673df523abSPeter Krystad 		if (!mp_opt->echo) {
2683df523abSPeter Krystad 			mp_opt->ahmac = get_unaligned_be64(ptr);
2693df523abSPeter Krystad 			ptr += 8;
2703df523abSPeter Krystad 		}
27190a4aea8SGeliang Tang 		pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d",
2721b1a6ef5SGeliang Tang 			 (mp_opt->addr.family == AF_INET6) ? "6" : "",
273f7dafee1SGeliang Tang 			 mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
2743df523abSPeter Krystad 		break;
2753df523abSPeter Krystad 
2763df523abSPeter Krystad 	case MPTCPOPT_RM_ADDR:
2775c4a824dSGeliang Tang 		if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
2785c4a824dSGeliang Tang 		    opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
2793df523abSPeter Krystad 			break;
2803df523abSPeter Krystad 
2818e60eed6SGeliang Tang 		ptr++;
2828e60eed6SGeliang Tang 
2833df523abSPeter Krystad 		mp_opt->rm_addr = 1;
2845c4a824dSGeliang Tang 		mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
2855c4a824dSGeliang Tang 		for (i = 0; i < mp_opt->rm_list.nr; i++)
2865c4a824dSGeliang Tang 			mp_opt->rm_list.ids[i] = *ptr++;
2875c4a824dSGeliang Tang 		pr_debug("RM_ADDR: rm_list_nr=%d", mp_opt->rm_list.nr);
2883df523abSPeter Krystad 		break;
2893df523abSPeter Krystad 
29040453a5cSGeliang Tang 	case MPTCPOPT_MP_PRIO:
29140453a5cSGeliang Tang 		if (opsize != TCPOLEN_MPTCP_PRIO)
29240453a5cSGeliang Tang 			break;
29340453a5cSGeliang Tang 
29440453a5cSGeliang Tang 		mp_opt->mp_prio = 1;
29540453a5cSGeliang Tang 		mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
29640453a5cSGeliang Tang 		pr_debug("MP_PRIO: prio=%d", mp_opt->backup);
29740453a5cSGeliang Tang 		break;
29840453a5cSGeliang Tang 
29950c504a2SFlorian Westphal 	case MPTCPOPT_MP_FASTCLOSE:
30050c504a2SFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
30150c504a2SFlorian Westphal 			break;
30250c504a2SFlorian Westphal 
30350c504a2SFlorian Westphal 		ptr += 2;
30450c504a2SFlorian Westphal 		mp_opt->rcvr_key = get_unaligned_be64(ptr);
30550c504a2SFlorian Westphal 		ptr += 8;
30650c504a2SFlorian Westphal 		mp_opt->fastclose = 1;
30750c504a2SFlorian Westphal 		break;
30850c504a2SFlorian Westphal 
309dc87efdbSFlorian Westphal 	case MPTCPOPT_RST:
310dc87efdbSFlorian Westphal 		if (opsize != TCPOLEN_MPTCP_RST)
311dc87efdbSFlorian Westphal 			break;
312dc87efdbSFlorian Westphal 
313dc87efdbSFlorian Westphal 		if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
314dc87efdbSFlorian Westphal 			break;
315dc87efdbSFlorian Westphal 		mp_opt->reset = 1;
316dc87efdbSFlorian Westphal 		flags = *ptr++;
317dc87efdbSFlorian Westphal 		mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
318dc87efdbSFlorian Westphal 		mp_opt->reset_reason = *ptr;
319dc87efdbSFlorian Westphal 		break;
320dc87efdbSFlorian Westphal 
321eda7acddSPeter Krystad 	default:
322eda7acddSPeter Krystad 		break;
323eda7acddSPeter Krystad 	}
324eda7acddSPeter Krystad }
325eda7acddSPeter Krystad 
326*c863225bSGeliang Tang void mptcp_get_options(const struct sock *sk,
327*c863225bSGeliang Tang 		       const struct sk_buff *skb,
328cfde141eSPaolo Abeni 		       struct mptcp_options_received *mp_opt)
329cec37a6eSPeter Krystad {
330cec37a6eSPeter Krystad 	const struct tcphdr *th = tcp_hdr(skb);
331cfde141eSPaolo Abeni 	const unsigned char *ptr;
332cfde141eSPaolo Abeni 	int length;
333cec37a6eSPeter Krystad 
334cfde141eSPaolo Abeni 	/* initialize option status */
335cfde141eSPaolo Abeni 	mp_opt->mp_capable = 0;
336cfde141eSPaolo Abeni 	mp_opt->mp_join = 0;
337cfde141eSPaolo Abeni 	mp_opt->add_addr = 0;
338fe2d9b1aSGeliang Tang 	mp_opt->ahmac = 0;
33950c504a2SFlorian Westphal 	mp_opt->fastclose = 0;
340f7dafee1SGeliang Tang 	mp_opt->addr.port = 0;
341cfde141eSPaolo Abeni 	mp_opt->rm_addr = 0;
342cfde141eSPaolo Abeni 	mp_opt->dss = 0;
34340453a5cSGeliang Tang 	mp_opt->mp_prio = 0;
344dc87efdbSFlorian Westphal 	mp_opt->reset = 0;
345cfde141eSPaolo Abeni 
346cfde141eSPaolo Abeni 	length = (th->doff * 4) - sizeof(struct tcphdr);
347cec37a6eSPeter Krystad 	ptr = (const unsigned char *)(th + 1);
348cec37a6eSPeter Krystad 
349cec37a6eSPeter Krystad 	while (length > 0) {
350cec37a6eSPeter Krystad 		int opcode = *ptr++;
351cec37a6eSPeter Krystad 		int opsize;
352cec37a6eSPeter Krystad 
353cec37a6eSPeter Krystad 		switch (opcode) {
354cec37a6eSPeter Krystad 		case TCPOPT_EOL:
355cec37a6eSPeter Krystad 			return;
356cec37a6eSPeter Krystad 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
357cec37a6eSPeter Krystad 			length--;
358cec37a6eSPeter Krystad 			continue;
359cec37a6eSPeter Krystad 		default:
360cec37a6eSPeter Krystad 			opsize = *ptr++;
361cec37a6eSPeter Krystad 			if (opsize < 2) /* "silly options" */
362cec37a6eSPeter Krystad 				return;
363cec37a6eSPeter Krystad 			if (opsize > length)
364cec37a6eSPeter Krystad 				return;	/* don't parse partial options */
365cec37a6eSPeter Krystad 			if (opcode == TCPOPT_MPTCP)
366cfde141eSPaolo Abeni 				mptcp_parse_option(skb, ptr, opsize, mp_opt);
367cec37a6eSPeter Krystad 			ptr += opsize - 2;
368cec37a6eSPeter Krystad 			length -= opsize;
369cec37a6eSPeter Krystad 		}
370cec37a6eSPeter Krystad 	}
371cec37a6eSPeter Krystad }
372cec37a6eSPeter Krystad 
373cc7972eaSChristoph Paasch bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
374cc7972eaSChristoph Paasch 		       unsigned int *size, struct mptcp_out_options *opts)
375cec37a6eSPeter Krystad {
376cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
377cec37a6eSPeter Krystad 
378cc7972eaSChristoph Paasch 	/* we will use snd_isn to detect first pkt [re]transmission
379cc7972eaSChristoph Paasch 	 * in mptcp_established_options_mp()
380cc7972eaSChristoph Paasch 	 */
381cc7972eaSChristoph Paasch 	subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
382cec37a6eSPeter Krystad 	if (subflow->request_mptcp) {
383cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYN;
38406fe1719SGeliang Tang 		opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
385cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYN;
386cec37a6eSPeter Krystad 		return true;
387ec3edaa7SPeter Krystad 	} else if (subflow->request_join) {
388ec3edaa7SPeter Krystad 		pr_debug("remote_token=%u, nonce=%u", subflow->remote_token,
389ec3edaa7SPeter Krystad 			 subflow->local_nonce);
390ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYN;
391ec3edaa7SPeter Krystad 		opts->join_id = subflow->local_id;
392ec3edaa7SPeter Krystad 		opts->token = subflow->remote_token;
393ec3edaa7SPeter Krystad 		opts->nonce = subflow->local_nonce;
394ec3edaa7SPeter Krystad 		opts->backup = subflow->request_bkup;
395ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYN;
396ec3edaa7SPeter Krystad 		return true;
397cec37a6eSPeter Krystad 	}
398cec37a6eSPeter Krystad 	return false;
399cec37a6eSPeter Krystad }
400cec37a6eSPeter Krystad 
401ec3edaa7SPeter Krystad /* MP_JOIN client subflow must wait for 4th ack before sending any data:
402ec3edaa7SPeter Krystad  * TCP can't schedule delack timer before the subflow is fully established.
403ec3edaa7SPeter Krystad  * MPTCP uses the delack timer to do 3rd ack retransmissions
404ec3edaa7SPeter Krystad  */
405ec3edaa7SPeter Krystad static void schedule_3rdack_retransmission(struct sock *sk)
406ec3edaa7SPeter Krystad {
407ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
408ec3edaa7SPeter Krystad 	struct tcp_sock *tp = tcp_sk(sk);
409ec3edaa7SPeter Krystad 	unsigned long timeout;
410ec3edaa7SPeter Krystad 
411ec3edaa7SPeter Krystad 	/* reschedule with a timeout above RTT, as we must look only for drop */
412ec3edaa7SPeter Krystad 	if (tp->srtt_us)
413ec3edaa7SPeter Krystad 		timeout = tp->srtt_us << 1;
414ec3edaa7SPeter Krystad 	else
415ec3edaa7SPeter Krystad 		timeout = TCP_TIMEOUT_INIT;
416ec3edaa7SPeter Krystad 
417ec3edaa7SPeter Krystad 	WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER);
418ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
419ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = timeout;
420ec3edaa7SPeter Krystad 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
421ec3edaa7SPeter Krystad }
422ec3edaa7SPeter Krystad 
423ec3edaa7SPeter Krystad static void clear_3rdack_retransmission(struct sock *sk)
424ec3edaa7SPeter Krystad {
425ec3edaa7SPeter Krystad 	struct inet_connection_sock *icsk = inet_csk(sk);
426ec3edaa7SPeter Krystad 
427ec3edaa7SPeter Krystad 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
428ec3edaa7SPeter Krystad 	icsk->icsk_ack.timeout = 0;
429ec3edaa7SPeter Krystad 	icsk->icsk_ack.ato = 0;
430ec3edaa7SPeter Krystad 	icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
431ec3edaa7SPeter Krystad }
432ec3edaa7SPeter Krystad 
433cc7972eaSChristoph Paasch static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
434d87903b6SPaolo Abeni 					 bool snd_data_fin_enable,
435cc7972eaSChristoph Paasch 					 unsigned int *size,
4366d0060f6SMat Martineau 					 unsigned int remaining,
437cec37a6eSPeter Krystad 					 struct mptcp_out_options *opts)
438cec37a6eSPeter Krystad {
439cec37a6eSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
44006fe1719SGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
441cc7972eaSChristoph Paasch 	struct mptcp_ext *mpext;
442cc7972eaSChristoph Paasch 	unsigned int data_len;
443c94b1f96SGeliang Tang 	u8 len;
444cec37a6eSPeter Krystad 
445ec3edaa7SPeter Krystad 	/* When skb is not available, we better over-estimate the emitted
446ec3edaa7SPeter Krystad 	 * options len. A full DSS option (28 bytes) is longer than
447ec3edaa7SPeter Krystad 	 * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
448ec3edaa7SPeter Krystad 	 * tell the caller to defer the estimate to
449ec3edaa7SPeter Krystad 	 * mptcp_established_options_dss(), which will reserve enough space.
450cc7972eaSChristoph Paasch 	 */
451ec3edaa7SPeter Krystad 	if (!skb)
452ec3edaa7SPeter Krystad 		return false;
453ec3edaa7SPeter Krystad 
454d87903b6SPaolo Abeni 	/* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
455d87903b6SPaolo Abeni 	if (subflow->fully_established || snd_data_fin_enable ||
456d87903b6SPaolo Abeni 	    subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
457d87903b6SPaolo Abeni 	    sk->sk_state != TCP_ESTABLISHED)
458ec3edaa7SPeter Krystad 		return false;
459ec3edaa7SPeter Krystad 
460ec3edaa7SPeter Krystad 	if (subflow->mp_capable) {
461cc7972eaSChristoph Paasch 		mpext = mptcp_get_ext(skb);
462cc7972eaSChristoph Paasch 		data_len = mpext ? mpext->data_len : 0;
463cc7972eaSChristoph Paasch 
464cc7972eaSChristoph Paasch 		/* we will check ext_copy.data_len in mptcp_write_options() to
465cc7972eaSChristoph Paasch 		 * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
466cc7972eaSChristoph Paasch 		 * TCPOLEN_MPTCP_MPC_ACK
467cc7972eaSChristoph Paasch 		 */
468cc7972eaSChristoph Paasch 		opts->ext_copy.data_len = data_len;
469cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_ACK;
470cec37a6eSPeter Krystad 		opts->sndr_key = subflow->local_key;
471cec37a6eSPeter Krystad 		opts->rcvr_key = subflow->remote_key;
47206fe1719SGeliang Tang 		opts->csum_reqd = READ_ONCE(msk->csum_enabled);
473cc7972eaSChristoph Paasch 
474cc7972eaSChristoph Paasch 		/* Section 3.1.
475cc7972eaSChristoph Paasch 		 * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
476cc7972eaSChristoph Paasch 		 * packets that start the first subflow of an MPTCP connection,
477cc7972eaSChristoph Paasch 		 * as well as the first packet that carries data
478cc7972eaSChristoph Paasch 		 */
479c94b1f96SGeliang Tang 		if (data_len > 0) {
480c94b1f96SGeliang Tang 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
481c94b1f96SGeliang Tang 			if (opts->csum_reqd) {
482c5b39e26SGeliang Tang 				/* we need to propagate more info to csum the pseudo hdr */
483c5b39e26SGeliang Tang 				opts->ext_copy.data_seq = mpext->data_seq;
484c5b39e26SGeliang Tang 				opts->ext_copy.subflow_seq = mpext->subflow_seq;
485c94b1f96SGeliang Tang 				opts->ext_copy.csum = mpext->csum;
486c94b1f96SGeliang Tang 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
487c94b1f96SGeliang Tang 			}
488c94b1f96SGeliang Tang 			*size = ALIGN(len, 4);
489c94b1f96SGeliang Tang 		} else {
490cec37a6eSPeter Krystad 			*size = TCPOLEN_MPTCP_MPC_ACK;
491c94b1f96SGeliang Tang 		}
492cc7972eaSChristoph Paasch 
493cc7972eaSChristoph Paasch 		pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d",
494cc7972eaSChristoph Paasch 			 subflow, subflow->local_key, subflow->remote_key,
495cc7972eaSChristoph Paasch 			 data_len);
496cc7972eaSChristoph Paasch 
497cec37a6eSPeter Krystad 		return true;
498ec3edaa7SPeter Krystad 	} else if (subflow->mp_join) {
499ec3edaa7SPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_ACK;
500ec3edaa7SPeter Krystad 		memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
501ec3edaa7SPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_ACK;
502ec3edaa7SPeter Krystad 		pr_debug("subflow=%p", subflow);
503ec3edaa7SPeter Krystad 
504ec3edaa7SPeter Krystad 		schedule_3rdack_retransmission(sk);
505ec3edaa7SPeter Krystad 		return true;
506cec37a6eSPeter Krystad 	}
507cec37a6eSPeter Krystad 	return false;
508cec37a6eSPeter Krystad }
509cec37a6eSPeter Krystad 
5106d0060f6SMat Martineau static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
5119c29e361SPaolo Abeni 				 struct sk_buff *skb, struct mptcp_ext *ext)
5126d0060f6SMat Martineau {
513017512a0SPaolo Abeni 	/* The write_seq value has already been incremented, so the actual
514017512a0SPaolo Abeni 	 * sequence number for the DATA_FIN is one less.
515017512a0SPaolo Abeni 	 */
516017512a0SPaolo Abeni 	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
5177279da61SMat Martineau 
5189c29e361SPaolo Abeni 	if (!ext->use_map || !skb->len) {
5196d0060f6SMat Martineau 		/* RFC6824 requires a DSS mapping with specific values
5206d0060f6SMat Martineau 		 * if DATA_FIN is set but no data payload is mapped
5216d0060f6SMat Martineau 		 */
5226d37a0b8SMat Martineau 		ext->data_fin = 1;
5236d0060f6SMat Martineau 		ext->use_map = 1;
5246d0060f6SMat Martineau 		ext->dsn64 = 1;
525017512a0SPaolo Abeni 		ext->data_seq = data_fin_tx_seq;
5266d0060f6SMat Martineau 		ext->subflow_seq = 0;
5276d0060f6SMat Martineau 		ext->data_len = 1;
5287279da61SMat Martineau 	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
5296d37a0b8SMat Martineau 		/* If there's an existing DSS mapping and it is the
5306d37a0b8SMat Martineau 		 * final mapping, DATA_FIN consumes 1 additional byte of
5316d37a0b8SMat Martineau 		 * mapping space.
5326d0060f6SMat Martineau 		 */
5336d37a0b8SMat Martineau 		ext->data_fin = 1;
5346d0060f6SMat Martineau 		ext->data_len++;
5356d0060f6SMat Martineau 	}
5366d0060f6SMat Martineau }
5376d0060f6SMat Martineau 
5386d0060f6SMat Martineau static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
539d87903b6SPaolo Abeni 					  bool snd_data_fin_enable,
5406d0060f6SMat Martineau 					  unsigned int *size,
5416d0060f6SMat Martineau 					  unsigned int remaining,
5426d0060f6SMat Martineau 					  struct mptcp_out_options *opts)
5436d0060f6SMat Martineau {
5446d0060f6SMat Martineau 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
5457279da61SMat Martineau 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
5466d0060f6SMat Martineau 	unsigned int dss_size = 0;
5476d0060f6SMat Martineau 	struct mptcp_ext *mpext;
5486d0060f6SMat Martineau 	unsigned int ack_size;
549d22f4988SChristoph Paasch 	bool ret = false;
550d87903b6SPaolo Abeni 	u64 ack_seq;
5516d0060f6SMat Martineau 
552c5b39e26SGeliang Tang 	opts->csum_reqd = READ_ONCE(msk->csum_enabled);
5530bac966aSMat Martineau 	mpext = skb ? mptcp_get_ext(skb) : NULL;
5546d0060f6SMat Martineau 
5557279da61SMat Martineau 	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
556c5b39e26SGeliang Tang 		unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
5576d0060f6SMat Martineau 
558c5b39e26SGeliang Tang 		if (mpext) {
559c5b39e26SGeliang Tang 			if (opts->csum_reqd)
560c5b39e26SGeliang Tang 				map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
561c5b39e26SGeliang Tang 
562c5b39e26SGeliang Tang 			opts->ext_copy = *mpext;
563c5b39e26SGeliang Tang 		}
5646d0060f6SMat Martineau 
5656d0060f6SMat Martineau 		remaining -= map_size;
5666d0060f6SMat Martineau 		dss_size = map_size;
5677279da61SMat Martineau 		if (skb && snd_data_fin_enable)
5689c29e361SPaolo Abeni 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
569d22f4988SChristoph Paasch 		ret = true;
570d22f4988SChristoph Paasch 	}
571d22f4988SChristoph Paasch 
5722398e399SPaolo Abeni 	/* passive sockets msk will set the 'can_ack' after accept(), even
5732398e399SPaolo Abeni 	 * if the first subflow may have the already the remote key handy
5742398e399SPaolo Abeni 	 */
575d22f4988SChristoph Paasch 	opts->ext_copy.use_ack = 0;
576dc093db5SPaolo Abeni 	if (!READ_ONCE(msk->can_ack)) {
577d22f4988SChristoph Paasch 		*size = ALIGN(dss_size, 4);
578d22f4988SChristoph Paasch 		return ret;
5796d0060f6SMat Martineau 	}
5806d0060f6SMat Martineau 
581e3859603SPaolo Abeni 	ack_seq = READ_ONCE(msk->ack_seq);
58237198e93SDavide Caratti 	if (READ_ONCE(msk->use_64bit_ack)) {
5836d0060f6SMat Martineau 		ack_size = TCPOLEN_MPTCP_DSS_ACK64;
584e3859603SPaolo Abeni 		opts->ext_copy.data_ack = ack_seq;
585a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 1;
586a0c1d0eaSChristoph Paasch 	} else {
587a0c1d0eaSChristoph Paasch 		ack_size = TCPOLEN_MPTCP_DSS_ACK32;
588e3859603SPaolo Abeni 		opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
589a0c1d0eaSChristoph Paasch 		opts->ext_copy.ack64 = 0;
590a0c1d0eaSChristoph Paasch 	}
591a0c1d0eaSChristoph Paasch 	opts->ext_copy.use_ack = 1;
592ea4ca586SPaolo Abeni 	WRITE_ONCE(msk->old_wspace, __mptcp_space((struct sock *)msk));
5936d0060f6SMat Martineau 
5946d0060f6SMat Martineau 	/* Add kind/length/subtype/flag overhead if mapping is not populated */
5956d0060f6SMat Martineau 	if (dss_size == 0)
5966d0060f6SMat Martineau 		ack_size += TCPOLEN_MPTCP_DSS_BASE;
5976d0060f6SMat Martineau 
5986d0060f6SMat Martineau 	dss_size += ack_size;
5996d0060f6SMat Martineau 
6006d0060f6SMat Martineau 	*size = ALIGN(dss_size, 4);
6016d0060f6SMat Martineau 	return true;
6026d0060f6SMat Martineau }
6036d0060f6SMat Martineau 
604761c124eSGeliang Tang static u64 add_addr_generate_hmac(u64 key1, u64 key2,
605761c124eSGeliang Tang 				  struct mptcp_addr_info *addr)
6063df523abSPeter Krystad {
607761c124eSGeliang Tang 	u16 port = ntohs(addr->port);
608bd697222STodd Malsbary 	u8 hmac[SHA256_DIGEST_SIZE];
6093df523abSPeter Krystad 	u8 msg[19];
610761c124eSGeliang Tang 	int i = 0;
6113df523abSPeter Krystad 
612761c124eSGeliang Tang 	msg[i++] = addr->id;
613761c124eSGeliang Tang 	if (addr->family == AF_INET) {
614761c124eSGeliang Tang 		memcpy(&msg[i], &addr->addr.s_addr, 4);
615761c124eSGeliang Tang 		i += 4;
616761c124eSGeliang Tang 	}
617761c124eSGeliang Tang #if IS_ENABLED(CONFIG_MPTCP_IPV6)
618761c124eSGeliang Tang 	else if (addr->family == AF_INET6) {
619761c124eSGeliang Tang 		memcpy(&msg[i], &addr->addr6.s6_addr, 16);
620761c124eSGeliang Tang 		i += 16;
621761c124eSGeliang Tang 	}
622761c124eSGeliang Tang #endif
623761c124eSGeliang Tang 	msg[i++] = port >> 8;
624761c124eSGeliang Tang 	msg[i++] = port & 0xFF;
6253df523abSPeter Krystad 
626761c124eSGeliang Tang 	mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
6273df523abSPeter Krystad 
628bd697222STodd Malsbary 	return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
6293df523abSPeter Krystad }
6303df523abSPeter Krystad 
63184dfe367SGeliang Tang static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
6323df523abSPeter Krystad 					       unsigned int *size,
6333df523abSPeter Krystad 					       unsigned int remaining,
6343df523abSPeter Krystad 					       struct mptcp_out_options *opts)
6353df523abSPeter Krystad {
6363df523abSPeter Krystad 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
6373df523abSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
63884dfe367SGeliang Tang 	bool drop_other_suboptions = false;
63984dfe367SGeliang Tang 	unsigned int opt_size = *size;
6406a6c05a8SGeliang Tang 	bool echo;
6414a2777a8SGeliang Tang 	bool port;
6421b1c7a0eSPeter Krystad 	int len;
6433df523abSPeter Krystad 
644fbe0f87aSGeliang Tang 	if ((mptcp_pm_should_add_signal_ipv6(msk) ||
64507f8252fSDavide Caratti 	     mptcp_pm_should_add_signal_port(msk) ||
64607f8252fSDavide Caratti 	     mptcp_pm_should_add_signal_echo(msk)) &&
64784dfe367SGeliang Tang 	    skb && skb_is_tcp_pure_ack(skb)) {
64884dfe367SGeliang Tang 		pr_debug("drop other suboptions");
64984dfe367SGeliang Tang 		opts->suboptions = 0;
6503ae32c07SGeliang Tang 		opts->ext_copy.use_ack = 0;
6513ae32c07SGeliang Tang 		opts->ext_copy.use_map = 0;
65284dfe367SGeliang Tang 		remaining += opt_size;
65384dfe367SGeliang Tang 		drop_other_suboptions = true;
65484dfe367SGeliang Tang 	}
65584dfe367SGeliang Tang 
656f643b803SGeliang Tang 	if (!mptcp_pm_should_add_signal(msk) ||
65730f60baeSGeliang Tang 	    !(mptcp_pm_add_addr_signal(msk, remaining, &opts->addr, &echo, &port)))
6583df523abSPeter Krystad 		return false;
6591b1c7a0eSPeter Krystad 
66030f60baeSGeliang Tang 	len = mptcp_add_addr_len(opts->addr.family, echo, port);
6611b1c7a0eSPeter Krystad 	if (remaining < len)
6621b1c7a0eSPeter Krystad 		return false;
6631b1c7a0eSPeter Krystad 
6641b1c7a0eSPeter Krystad 	*size = len;
66584dfe367SGeliang Tang 	if (drop_other_suboptions)
66684dfe367SGeliang Tang 		*size -= opt_size;
6673df523abSPeter Krystad 	opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
6686a6c05a8SGeliang Tang 	if (!echo) {
6693df523abSPeter Krystad 		opts->ahmac = add_addr_generate_hmac(msk->local_key,
6703df523abSPeter Krystad 						     msk->remote_key,
671761c124eSGeliang Tang 						     &opts->addr);
6723df523abSPeter Krystad 	}
6734a2777a8SGeliang Tang 	pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d",
67430f60baeSGeliang Tang 		 opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
6753df523abSPeter Krystad 
6763df523abSPeter Krystad 	return true;
6773df523abSPeter Krystad }
6783df523abSPeter Krystad 
6795cb104aeSGeliang Tang static bool mptcp_established_options_rm_addr(struct sock *sk,
6805cb104aeSGeliang Tang 					      unsigned int *size,
6815cb104aeSGeliang Tang 					      unsigned int remaining,
6825cb104aeSGeliang Tang 					      struct mptcp_out_options *opts)
6835cb104aeSGeliang Tang {
6845cb104aeSGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
6855cb104aeSGeliang Tang 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
6866445e17aSGeliang Tang 	struct mptcp_rm_list rm_list;
6876445e17aSGeliang Tang 	int i, len;
6885cb104aeSGeliang Tang 
6895cb104aeSGeliang Tang 	if (!mptcp_pm_should_rm_signal(msk) ||
6906445e17aSGeliang Tang 	    !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
6915cb104aeSGeliang Tang 		return false;
6925cb104aeSGeliang Tang 
6936445e17aSGeliang Tang 	len = mptcp_rm_addr_len(&rm_list);
6946445e17aSGeliang Tang 	if (len < 0)
6956445e17aSGeliang Tang 		return false;
6966445e17aSGeliang Tang 	if (remaining < len)
6975cb104aeSGeliang Tang 		return false;
6985cb104aeSGeliang Tang 
6996445e17aSGeliang Tang 	*size = len;
7005cb104aeSGeliang Tang 	opts->suboptions |= OPTION_MPTCP_RM_ADDR;
7016445e17aSGeliang Tang 	opts->rm_list = rm_list;
7025cb104aeSGeliang Tang 
7036445e17aSGeliang Tang 	for (i = 0; i < opts->rm_list.nr; i++)
7046445e17aSGeliang Tang 		pr_debug("rm_list_ids[%d]=%d", i, opts->rm_list.ids[i]);
7055cb104aeSGeliang Tang 
7065cb104aeSGeliang Tang 	return true;
7075cb104aeSGeliang Tang }
7085cb104aeSGeliang Tang 
70906706542SGeliang Tang static bool mptcp_established_options_mp_prio(struct sock *sk,
71006706542SGeliang Tang 					      unsigned int *size,
71106706542SGeliang Tang 					      unsigned int remaining,
71206706542SGeliang Tang 					      struct mptcp_out_options *opts)
71306706542SGeliang Tang {
71406706542SGeliang Tang 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
71506706542SGeliang Tang 
71606706542SGeliang Tang 	if (!subflow->send_mp_prio)
71706706542SGeliang Tang 		return false;
71806706542SGeliang Tang 
719ec99a470SDavide Caratti 	/* account for the trailing 'nop' option */
720ec99a470SDavide Caratti 	if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
72106706542SGeliang Tang 		return false;
72206706542SGeliang Tang 
723ec99a470SDavide Caratti 	*size = TCPOLEN_MPTCP_PRIO_ALIGN;
72406706542SGeliang Tang 	opts->suboptions |= OPTION_MPTCP_PRIO;
72506706542SGeliang Tang 	opts->backup = subflow->request_bkup;
72606706542SGeliang Tang 
72706706542SGeliang Tang 	pr_debug("prio=%d", opts->backup);
72806706542SGeliang Tang 
72906706542SGeliang Tang 	return true;
73006706542SGeliang Tang }
73106706542SGeliang Tang 
732dc87efdbSFlorian Westphal static noinline void mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
733dc87efdbSFlorian Westphal 						   unsigned int *size,
734dc87efdbSFlorian Westphal 						   unsigned int remaining,
735dc87efdbSFlorian Westphal 						   struct mptcp_out_options *opts)
736dc87efdbSFlorian Westphal {
737dc87efdbSFlorian Westphal 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
738dc87efdbSFlorian Westphal 
739dc87efdbSFlorian Westphal 	if (remaining < TCPOLEN_MPTCP_RST)
740dc87efdbSFlorian Westphal 		return;
741dc87efdbSFlorian Westphal 
742dc87efdbSFlorian Westphal 	*size = TCPOLEN_MPTCP_RST;
743dc87efdbSFlorian Westphal 	opts->suboptions |= OPTION_MPTCP_RST;
744dc87efdbSFlorian Westphal 	opts->reset_transient = subflow->reset_transient;
745dc87efdbSFlorian Westphal 	opts->reset_reason = subflow->reset_reason;
746dc87efdbSFlorian Westphal }
747dc87efdbSFlorian Westphal 
7486d0060f6SMat Martineau bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
7496d0060f6SMat Martineau 			       unsigned int *size, unsigned int remaining,
7506d0060f6SMat Martineau 			       struct mptcp_out_options *opts)
7516d0060f6SMat Martineau {
752d87903b6SPaolo Abeni 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
753d87903b6SPaolo Abeni 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
7546d0060f6SMat Martineau 	unsigned int opt_size = 0;
755d87903b6SPaolo Abeni 	bool snd_data_fin;
7566d0060f6SMat Martineau 	bool ret = false;
7576d0060f6SMat Martineau 
7583df523abSPeter Krystad 	opts->suboptions = 0;
7593df523abSPeter Krystad 
760d87903b6SPaolo Abeni 	if (unlikely(__mptcp_check_fallback(msk)))
761e1ff9e82SDavide Caratti 		return false;
762e1ff9e82SDavide Caratti 
763dc87efdbSFlorian Westphal 	if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
764dc87efdbSFlorian Westphal 		mptcp_established_options_rst(sk, skb, size, remaining, opts);
765dc87efdbSFlorian Westphal 		return true;
766dc87efdbSFlorian Westphal 	}
767d5824847SPaolo Abeni 
768d87903b6SPaolo Abeni 	snd_data_fin = mptcp_data_fin_enabled(msk);
769d87903b6SPaolo Abeni 	if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, remaining, opts))
7706d0060f6SMat Martineau 		ret = true;
771d87903b6SPaolo Abeni 	else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, remaining, opts))
7726d0060f6SMat Martineau 		ret = true;
7736d0060f6SMat Martineau 
7746d0060f6SMat Martineau 	/* we reserved enough space for the above options, and exceeding the
7756d0060f6SMat Martineau 	 * TCP option space would be fatal
7766d0060f6SMat Martineau 	 */
7776d0060f6SMat Martineau 	if (WARN_ON_ONCE(opt_size > remaining))
7786d0060f6SMat Martineau 		return false;
7796d0060f6SMat Martineau 
7806d0060f6SMat Martineau 	*size += opt_size;
7816d0060f6SMat Martineau 	remaining -= opt_size;
78284dfe367SGeliang Tang 	if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
7833df523abSPeter Krystad 		*size += opt_size;
7843df523abSPeter Krystad 		remaining -= opt_size;
7853df523abSPeter Krystad 		ret = true;
7865cb104aeSGeliang Tang 	} else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
7875cb104aeSGeliang Tang 		*size += opt_size;
7885cb104aeSGeliang Tang 		remaining -= opt_size;
7895cb104aeSGeliang Tang 		ret = true;
7903df523abSPeter Krystad 	}
7916d0060f6SMat Martineau 
79206706542SGeliang Tang 	if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
79306706542SGeliang Tang 		*size += opt_size;
79406706542SGeliang Tang 		remaining -= opt_size;
79506706542SGeliang Tang 		ret = true;
79606706542SGeliang Tang 	}
79706706542SGeliang Tang 
7986d0060f6SMat Martineau 	return ret;
7996d0060f6SMat Martineau }
8006d0060f6SMat Martineau 
801cec37a6eSPeter Krystad bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
802cec37a6eSPeter Krystad 			  struct mptcp_out_options *opts)
803cec37a6eSPeter Krystad {
804cec37a6eSPeter Krystad 	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
805cec37a6eSPeter Krystad 
806cec37a6eSPeter Krystad 	if (subflow_req->mp_capable) {
807cec37a6eSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
808cec37a6eSPeter Krystad 		opts->sndr_key = subflow_req->local_key;
80906fe1719SGeliang Tang 		opts->csum_reqd = subflow_req->csum_reqd;
810cec37a6eSPeter Krystad 		*size = TCPOLEN_MPTCP_MPC_SYNACK;
811cec37a6eSPeter Krystad 		pr_debug("subflow_req=%p, local_key=%llu",
812cec37a6eSPeter Krystad 			 subflow_req, subflow_req->local_key);
813cec37a6eSPeter Krystad 		return true;
814f296234cSPeter Krystad 	} else if (subflow_req->mp_join) {
815f296234cSPeter Krystad 		opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
816f296234cSPeter Krystad 		opts->backup = subflow_req->backup;
817f296234cSPeter Krystad 		opts->join_id = subflow_req->local_id;
818f296234cSPeter Krystad 		opts->thmac = subflow_req->thmac;
819f296234cSPeter Krystad 		opts->nonce = subflow_req->local_nonce;
820f296234cSPeter Krystad 		pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u",
821f296234cSPeter Krystad 			 subflow_req, opts->backup, opts->join_id,
822f296234cSPeter Krystad 			 opts->thmac, opts->nonce);
823f296234cSPeter Krystad 		*size = TCPOLEN_MPTCP_MPJ_SYNACK;
824f296234cSPeter Krystad 		return true;
825cec37a6eSPeter Krystad 	}
826cec37a6eSPeter Krystad 	return false;
827cec37a6eSPeter Krystad }
828cec37a6eSPeter Krystad 
829d5824847SPaolo Abeni static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
830f296234cSPeter Krystad 				    struct mptcp_subflow_context *subflow,
831d22f4988SChristoph Paasch 				    struct sk_buff *skb,
832d22f4988SChristoph Paasch 				    struct mptcp_options_received *mp_opt)
833d22f4988SChristoph Paasch {
834d22f4988SChristoph Paasch 	/* here we can process OoO, in-window pkts, only in-sequence 4th ack
835f296234cSPeter Krystad 	 * will make the subflow fully established
836d22f4988SChristoph Paasch 	 */
837f296234cSPeter Krystad 	if (likely(subflow->fully_established)) {
838f296234cSPeter Krystad 		/* on passive sockets, check for 3rd ack retransmission
839f296234cSPeter Krystad 		 * note that msk is always set by subflow_syn_recv_sock()
840f296234cSPeter Krystad 		 * for mp_join subflows
841f296234cSPeter Krystad 		 */
842f296234cSPeter Krystad 		if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
843f296234cSPeter Krystad 		    TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
844f296234cSPeter Krystad 		    subflow->mp_join && mp_opt->mp_join &&
845f296234cSPeter Krystad 		    READ_ONCE(msk->pm.server_side))
846d5824847SPaolo Abeni 			tcp_send_ack(ssk);
847f296234cSPeter Krystad 		goto fully_established;
848f296234cSPeter Krystad 	}
849d22f4988SChristoph Paasch 
850d5824847SPaolo Abeni 	/* we must process OoO packets before the first subflow is fully
851d5824847SPaolo Abeni 	 * established. OoO packets are instead a protocol violation
852d5824847SPaolo Abeni 	 * for MP_JOIN subflows as the peer must not send any data
853d5824847SPaolo Abeni 	 * before receiving the forth ack - cfr. RFC 8684 section 3.2.
854f296234cSPeter Krystad 	 */
855d5824847SPaolo Abeni 	if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
856d5824847SPaolo Abeni 		if (subflow->mp_join)
857d5824847SPaolo Abeni 			goto reset;
858f296234cSPeter Krystad 		return subflow->mp_capable;
859d5824847SPaolo Abeni 	}
860f296234cSPeter Krystad 
8615a91e32bSPaolo Abeni 	if (mp_opt->dss && mp_opt->use_ack) {
862f296234cSPeter Krystad 		/* subflows are fully established as soon as we get any
863f296234cSPeter Krystad 		 * additional ack.
864f296234cSPeter Krystad 		 */
8650be534f5SPaolo Abeni 		subflow->fully_established = 1;
866b93df08cSPaolo Abeni 		WRITE_ONCE(msk->fully_established, true);
867f296234cSPeter Krystad 		goto fully_established;
868f296234cSPeter Krystad 	}
869d22f4988SChristoph Paasch 
87084dfe367SGeliang Tang 	if (mp_opt->add_addr) {
87184dfe367SGeliang Tang 		WRITE_ONCE(msk->fully_established, true);
87284dfe367SGeliang Tang 		return true;
87384dfe367SGeliang Tang 	}
87484dfe367SGeliang Tang 
875d22f4988SChristoph Paasch 	/* If the first established packet does not contain MP_CAPABLE + data
876d5824847SPaolo Abeni 	 * then fallback to TCP. Fallback scenarios requires a reset for
877d5824847SPaolo Abeni 	 * MP_JOIN subflows.
878d22f4988SChristoph Paasch 	 */
879d22f4988SChristoph Paasch 	if (!mp_opt->mp_capable) {
880d5824847SPaolo Abeni 		if (subflow->mp_join)
881d5824847SPaolo Abeni 			goto reset;
882d22f4988SChristoph Paasch 		subflow->mp_capable = 0;
883e1ff9e82SDavide Caratti 		pr_fallback(msk);
884e1ff9e82SDavide Caratti 		__mptcp_do_fallback(msk);
885d22f4988SChristoph Paasch 		return false;
886d22f4988SChristoph Paasch 	}
887f296234cSPeter Krystad 
888d6085fe1SPaolo Abeni 	if (unlikely(!READ_ONCE(msk->pm.server_side)))
889d6085fe1SPaolo Abeni 		pr_warn_once("bogus mpc option on established client sk");
890b93df08cSPaolo Abeni 	mptcp_subflow_fully_established(subflow, mp_opt);
891f296234cSPeter Krystad 
892f296234cSPeter Krystad fully_established:
8935b950ff4SPaolo Abeni 	/* if the subflow is not already linked into the conn_list, we can't
8945b950ff4SPaolo Abeni 	 * notify the PM: this subflow is still on the listener queue
8955b950ff4SPaolo Abeni 	 * and the PM possibly acquiring the subflow lock could race with
8965b950ff4SPaolo Abeni 	 * the listener close
8975b950ff4SPaolo Abeni 	 */
8985b950ff4SPaolo Abeni 	if (likely(subflow->pm_notified) || list_empty(&subflow->node))
899f296234cSPeter Krystad 		return true;
900f296234cSPeter Krystad 
901f296234cSPeter Krystad 	subflow->pm_notified = 1;
902ec3edaa7SPeter Krystad 	if (subflow->mp_join) {
903d5824847SPaolo Abeni 		clear_3rdack_retransmission(ssk);
90462535200SGeliang Tang 		mptcp_pm_subflow_established(msk);
905ec3edaa7SPeter Krystad 	} else {
9066c714f1bSFlorian Westphal 		mptcp_pm_fully_established(msk, ssk, GFP_ATOMIC);
907ec3edaa7SPeter Krystad 	}
908d22f4988SChristoph Paasch 	return true;
909d5824847SPaolo Abeni 
910d5824847SPaolo Abeni reset:
911d5824847SPaolo Abeni 	mptcp_subflow_reset(ssk);
912d5824847SPaolo Abeni 	return false;
913d22f4988SChristoph Paasch }
914d22f4988SChristoph Paasch 
915cc9d2566SPaolo Abeni static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
916cc9d2566SPaolo Abeni {
917cc9d2566SPaolo Abeni 	u32 old_ack32, cur_ack32;
918cc9d2566SPaolo Abeni 
919cc9d2566SPaolo Abeni 	if (use_64bit)
920cc9d2566SPaolo Abeni 		return cur_ack;
921cc9d2566SPaolo Abeni 
922cc9d2566SPaolo Abeni 	old_ack32 = (u32)old_ack;
923cc9d2566SPaolo Abeni 	cur_ack32 = (u32)cur_ack;
924cc9d2566SPaolo Abeni 	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
925cc9d2566SPaolo Abeni 	if (unlikely(before(cur_ack32, old_ack32)))
926cc9d2566SPaolo Abeni 		return cur_ack + (1LL << 32);
927cc9d2566SPaolo Abeni 	return cur_ack;
928cc9d2566SPaolo Abeni }
929cc9d2566SPaolo Abeni 
9306f8a612aSFlorian Westphal static void ack_update_msk(struct mptcp_sock *msk,
9316e628cd3SPaolo Abeni 			   struct sock *ssk,
932cc9d2566SPaolo Abeni 			   struct mptcp_options_received *mp_opt)
933cc9d2566SPaolo Abeni {
9347439d687SPaolo Abeni 	u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
9356f8a612aSFlorian Westphal 	struct sock *sk = (struct sock *)msk;
9367439d687SPaolo Abeni 	u64 old_snd_una;
9377439d687SPaolo Abeni 
9387439d687SPaolo Abeni 	mptcp_data_lock(sk);
939cc9d2566SPaolo Abeni 
940cc9d2566SPaolo Abeni 	/* avoid ack expansion on update conflict, to reduce the risk of
941cc9d2566SPaolo Abeni 	 * wrongly expanding to a future ack sequence number, which is way
942cc9d2566SPaolo Abeni 	 * more dangerous than missing an ack
943cc9d2566SPaolo Abeni 	 */
9447439d687SPaolo Abeni 	old_snd_una = msk->snd_una;
945cc9d2566SPaolo Abeni 	new_snd_una = expand_ack(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
946cc9d2566SPaolo Abeni 
947cc9d2566SPaolo Abeni 	/* ACK for data not even sent yet? Ignore. */
948eaa2ffabSPaolo Abeni 	if (after64(new_snd_una, snd_nxt))
949cc9d2566SPaolo Abeni 		new_snd_una = old_snd_una;
950cc9d2566SPaolo Abeni 
9516f8a612aSFlorian Westphal 	new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
9526f8a612aSFlorian Westphal 
953219d0499SPaolo Abeni 	if (after64(new_wnd_end, msk->wnd_end))
9547439d687SPaolo Abeni 		msk->wnd_end = new_wnd_end;
955219d0499SPaolo Abeni 
956219d0499SPaolo Abeni 	/* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
957d09d818eSPaolo Abeni 	if (after64(msk->wnd_end, READ_ONCE(msk->snd_nxt)))
958219d0499SPaolo Abeni 		__mptcp_check_push(sk, ssk);
9596f8a612aSFlorian Westphal 
9607439d687SPaolo Abeni 	if (after64(new_snd_una, old_snd_una)) {
9617439d687SPaolo Abeni 		msk->snd_una = new_snd_una;
9627439d687SPaolo Abeni 		__mptcp_data_acked(sk);
963cc9d2566SPaolo Abeni 	}
9647439d687SPaolo Abeni 	mptcp_data_unlock(sk);
965ed66bfb4SGeliang Tang 
966ed66bfb4SGeliang Tang 	trace_ack_update_msk(mp_opt->data_ack,
967ed66bfb4SGeliang Tang 			     old_snd_una, new_snd_una,
968ed66bfb4SGeliang Tang 			     new_wnd_end, msk->wnd_end);
969b51f9b80SPaolo Abeni }
970cc9d2566SPaolo Abeni 
9711a49b2c2SMat Martineau bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
9723721b9b6SMat Martineau {
9733721b9b6SMat Martineau 	/* Skip if DATA_FIN was already received.
9743721b9b6SMat Martineau 	 * If updating simultaneously with the recvmsg loop, values
9753721b9b6SMat Martineau 	 * should match. If they mismatch, the peer is misbehaving and
9763721b9b6SMat Martineau 	 * we will prefer the most recent information.
9773721b9b6SMat Martineau 	 */
978781bf13dSPaolo Abeni 	if (READ_ONCE(msk->rcv_data_fin))
9793721b9b6SMat Martineau 		return false;
9803721b9b6SMat Martineau 
9811a49b2c2SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin_seq,
9821a49b2c2SMat Martineau 		   expand_ack(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
9833721b9b6SMat Martineau 	WRITE_ONCE(msk->rcv_data_fin, 1);
9843721b9b6SMat Martineau 
9853721b9b6SMat Martineau 	return true;
9863721b9b6SMat Martineau }
9873721b9b6SMat Martineau 
9881b1c7a0eSPeter Krystad static bool add_addr_hmac_valid(struct mptcp_sock *msk,
9891b1c7a0eSPeter Krystad 				struct mptcp_options_received *mp_opt)
9901b1c7a0eSPeter Krystad {
9911b1c7a0eSPeter Krystad 	u64 hmac = 0;
9921b1c7a0eSPeter Krystad 
9931b1c7a0eSPeter Krystad 	if (mp_opt->echo)
9941b1c7a0eSPeter Krystad 		return true;
9951b1c7a0eSPeter Krystad 
9961b1c7a0eSPeter Krystad 	hmac = add_addr_generate_hmac(msk->remote_key,
9971b1c7a0eSPeter Krystad 				      msk->local_key,
998761c124eSGeliang Tang 				      &mp_opt->addr);
9991b1c7a0eSPeter Krystad 
10001b1c7a0eSPeter Krystad 	pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
10011b1c7a0eSPeter Krystad 		 msk, (unsigned long long)hmac,
10021b1c7a0eSPeter Krystad 		 (unsigned long long)mp_opt->ahmac);
10031b1c7a0eSPeter Krystad 
10041b1c7a0eSPeter Krystad 	return hmac == mp_opt->ahmac;
10051b1c7a0eSPeter Krystad }
10061b1c7a0eSPeter Krystad 
100777d0cab9SFlorian Westphal void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1008648ef4b8SMat Martineau {
1009d22f4988SChristoph Paasch 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
10101b1c7a0eSPeter Krystad 	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1011cfde141eSPaolo Abeni 	struct mptcp_options_received mp_opt;
1012648ef4b8SMat Martineau 	struct mptcp_ext *mpext;
1013648ef4b8SMat Martineau 
10146e628cd3SPaolo Abeni 	if (__mptcp_check_fallback(msk)) {
10156e628cd3SPaolo Abeni 		/* Keep it simple and unconditionally trigger send data cleanup and
10166e628cd3SPaolo Abeni 		 * pending queue spooling. We will need to acquire the data lock
10176e628cd3SPaolo Abeni 		 * for more accurate checks, and once the lock is acquired, such
10186e628cd3SPaolo Abeni 		 * helpers are cheap.
10196e628cd3SPaolo Abeni 		 */
10206e628cd3SPaolo Abeni 		mptcp_data_lock(subflow->conn);
1021219d0499SPaolo Abeni 		if (sk_stream_memory_free(sk))
1022219d0499SPaolo Abeni 			__mptcp_check_push(subflow->conn, sk);
10236e628cd3SPaolo Abeni 		__mptcp_data_acked(subflow->conn);
10246e628cd3SPaolo Abeni 		mptcp_data_unlock(subflow->conn);
1025e1ff9e82SDavide Caratti 		return;
10266e628cd3SPaolo Abeni 	}
1027e1ff9e82SDavide Caratti 
1028*c863225bSGeliang Tang 	mptcp_get_options(sk, skb, &mp_opt);
1029cfde141eSPaolo Abeni 	if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1030d22f4988SChristoph Paasch 		return;
1031648ef4b8SMat Martineau 
103250c504a2SFlorian Westphal 	if (mp_opt.fastclose &&
103350c504a2SFlorian Westphal 	    msk->local_key == mp_opt.rcvr_key) {
103450c504a2SFlorian Westphal 		WRITE_ONCE(msk->rcv_fastclose, true);
103550c504a2SFlorian Westphal 		mptcp_schedule_work((struct sock *)msk);
103650c504a2SFlorian Westphal 	}
103750c504a2SFlorian Westphal 
1038cfde141eSPaolo Abeni 	if (mp_opt.add_addr && add_addr_hmac_valid(msk, &mp_opt)) {
1039a877de06SGeliang Tang 		if (!mp_opt.echo) {
1040f7dafee1SGeliang Tang 			mptcp_pm_add_addr_received(msk, &mp_opt.addr);
1041a877de06SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1042a877de06SGeliang Tang 		} else {
1043f7dafee1SGeliang Tang 			mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1044d58300c3SDavide Caratti 			mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1045a877de06SGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1046a877de06SGeliang Tang 		}
10472fbdd9eaSGeliang Tang 
1048f7dafee1SGeliang Tang 		if (mp_opt.addr.port)
10492fbdd9eaSGeliang Tang 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
10502fbdd9eaSGeliang Tang 
1051cfde141eSPaolo Abeni 		mp_opt.add_addr = 0;
10521b1c7a0eSPeter Krystad 	}
10531b1c7a0eSPeter Krystad 
1054d0876b22SGeliang Tang 	if (mp_opt.rm_addr) {
10555c4a824dSGeliang Tang 		mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1056d0876b22SGeliang Tang 		mp_opt.rm_addr = 0;
1057d0876b22SGeliang Tang 	}
1058d0876b22SGeliang Tang 
105940453a5cSGeliang Tang 	if (mp_opt.mp_prio) {
106040453a5cSGeliang Tang 		mptcp_pm_mp_prio_received(sk, mp_opt.backup);
10610be2ac28SGeliang Tang 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
106240453a5cSGeliang Tang 		mp_opt.mp_prio = 0;
106340453a5cSGeliang Tang 	}
106440453a5cSGeliang Tang 
1065dc87efdbSFlorian Westphal 	if (mp_opt.reset) {
1066dc87efdbSFlorian Westphal 		subflow->reset_seen = 1;
1067dc87efdbSFlorian Westphal 		subflow->reset_reason = mp_opt.reset_reason;
1068dc87efdbSFlorian Westphal 		subflow->reset_transient = mp_opt.reset_transient;
1069dc87efdbSFlorian Westphal 	}
1070dc87efdbSFlorian Westphal 
1071cfde141eSPaolo Abeni 	if (!mp_opt.dss)
1072648ef4b8SMat Martineau 		return;
1073648ef4b8SMat Martineau 
1074cc9d2566SPaolo Abeni 	/* we can't wait for recvmsg() to update the ack_seq, otherwise
1075cc9d2566SPaolo Abeni 	 * monodirectional flows will stuck
1076cc9d2566SPaolo Abeni 	 */
1077cfde141eSPaolo Abeni 	if (mp_opt.use_ack)
10786f8a612aSFlorian Westphal 		ack_update_msk(msk, sk, &mp_opt);
1079cc9d2566SPaolo Abeni 
108006827b34SMat Martineau 	/* Zero-data-length packets are dropped by the caller and not
108106827b34SMat Martineau 	 * propagated to the MPTCP layer, so the skb extension does not
108206827b34SMat Martineau 	 * need to be allocated or populated. DATA_FIN information, if
108306827b34SMat Martineau 	 * present, needs to be updated here before the skb is freed.
108443b54c6eSMat Martineau 	 */
108543b54c6eSMat Martineau 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
108643b54c6eSMat Martineau 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
10871a49b2c2SMat Martineau 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64) &&
108843b54c6eSMat Martineau 		    schedule_work(&msk->work))
108943b54c6eSMat Martineau 			sock_hold(subflow->conn);
109006827b34SMat Martineau 
109106827b34SMat Martineau 		return;
109243b54c6eSMat Martineau 	}
109343b54c6eSMat Martineau 
1094648ef4b8SMat Martineau 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1095648ef4b8SMat Martineau 	if (!mpext)
1096648ef4b8SMat Martineau 		return;
1097648ef4b8SMat Martineau 
1098648ef4b8SMat Martineau 	memset(mpext, 0, sizeof(*mpext));
1099648ef4b8SMat Martineau 
1100cfde141eSPaolo Abeni 	if (mp_opt.use_map) {
1101cfde141eSPaolo Abeni 		if (mp_opt.mpc_map) {
1102cc7972eaSChristoph Paasch 			/* this is an MP_CAPABLE carrying MPTCP data
1103cc7972eaSChristoph Paasch 			 * we know this map the first chunk of data
1104cc7972eaSChristoph Paasch 			 */
1105cc7972eaSChristoph Paasch 			mptcp_crypto_key_sha(subflow->remote_key, NULL,
1106cc7972eaSChristoph Paasch 					     &mpext->data_seq);
1107cc7972eaSChristoph Paasch 			mpext->data_seq++;
1108cc7972eaSChristoph Paasch 			mpext->subflow_seq = 1;
1109cc7972eaSChristoph Paasch 			mpext->dsn64 = 1;
1110cc7972eaSChristoph Paasch 			mpext->mpc_map = 1;
1111a77895dbSPaolo Abeni 			mpext->data_fin = 0;
1112cc7972eaSChristoph Paasch 		} else {
1113cfde141eSPaolo Abeni 			mpext->data_seq = mp_opt.data_seq;
1114cfde141eSPaolo Abeni 			mpext->subflow_seq = mp_opt.subflow_seq;
1115cfde141eSPaolo Abeni 			mpext->dsn64 = mp_opt.dsn64;
1116cfde141eSPaolo Abeni 			mpext->data_fin = mp_opt.data_fin;
1117cc7972eaSChristoph Paasch 		}
1118cfde141eSPaolo Abeni 		mpext->data_len = mp_opt.data_len;
1119648ef4b8SMat Martineau 		mpext->use_map = 1;
1120648ef4b8SMat Martineau 	}
1121648ef4b8SMat Martineau }
1122648ef4b8SMat Martineau 
1123fa3fe2b1SFlorian Westphal static void mptcp_set_rwin(const struct tcp_sock *tp)
1124fa3fe2b1SFlorian Westphal {
1125fa3fe2b1SFlorian Westphal 	const struct sock *ssk = (const struct sock *)tp;
1126fa3fe2b1SFlorian Westphal 	const struct mptcp_subflow_context *subflow;
1127fa3fe2b1SFlorian Westphal 	struct mptcp_sock *msk;
1128fa3fe2b1SFlorian Westphal 	u64 ack_seq;
1129fa3fe2b1SFlorian Westphal 
1130fa3fe2b1SFlorian Westphal 	subflow = mptcp_subflow_ctx(ssk);
1131fa3fe2b1SFlorian Westphal 	msk = mptcp_sk(subflow->conn);
1132fa3fe2b1SFlorian Westphal 
1133fa3fe2b1SFlorian Westphal 	ack_seq = READ_ONCE(msk->ack_seq) + tp->rcv_wnd;
1134fa3fe2b1SFlorian Westphal 
1135fa3fe2b1SFlorian Westphal 	if (after64(ack_seq, READ_ONCE(msk->rcv_wnd_sent)))
1136fa3fe2b1SFlorian Westphal 		WRITE_ONCE(msk->rcv_wnd_sent, ack_seq);
1137fa3fe2b1SFlorian Westphal }
1138fa3fe2b1SFlorian Westphal 
1139c94b1f96SGeliang Tang static u16 mptcp_make_csum(const struct mptcp_ext *mpext)
1140c94b1f96SGeliang Tang {
1141c94b1f96SGeliang Tang 	struct csum_pseudo_header header;
1142c94b1f96SGeliang Tang 	__wsum csum;
1143c94b1f96SGeliang Tang 
1144c94b1f96SGeliang Tang 	/* cfr RFC 8684 3.3.1.:
1145c94b1f96SGeliang Tang 	 * the data sequence number used in the pseudo-header is
1146c94b1f96SGeliang Tang 	 * always the 64-bit value, irrespective of what length is used in the
1147c94b1f96SGeliang Tang 	 * DSS option itself.
1148c94b1f96SGeliang Tang 	 */
1149c94b1f96SGeliang Tang 	header.data_seq = cpu_to_be64(mpext->data_seq);
1150c94b1f96SGeliang Tang 	header.subflow_seq = htonl(mpext->subflow_seq);
1151c94b1f96SGeliang Tang 	header.data_len = htons(mpext->data_len);
1152c94b1f96SGeliang Tang 	header.csum = 0;
1153c94b1f96SGeliang Tang 
1154c94b1f96SGeliang Tang 	csum = csum_partial(&header, sizeof(header), ~csum_unfold(mpext->csum));
1155c94b1f96SGeliang Tang 	return (__force u16)csum_fold(csum);
1156c94b1f96SGeliang Tang }
1157c94b1f96SGeliang Tang 
1158fa3fe2b1SFlorian Westphal void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
1159fa3fe2b1SFlorian Westphal 			 struct mptcp_out_options *opts)
1160eda7acddSPeter Krystad {
1161cc7972eaSChristoph Paasch 	if ((OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK |
1162eda7acddSPeter Krystad 	     OPTION_MPTCP_MPC_ACK) & opts->suboptions) {
116306fe1719SGeliang Tang 		u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1164eda7acddSPeter Krystad 
1165c94b1f96SGeliang Tang 		if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1166eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYN;
1167c94b1f96SGeliang Tang 		} else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1168cec37a6eSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_SYNACK;
1169c94b1f96SGeliang Tang 		} else if (opts->ext_copy.data_len) {
1170cc7972eaSChristoph Paasch 			len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1171c94b1f96SGeliang Tang 			if (opts->csum_reqd)
1172c94b1f96SGeliang Tang 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1173c94b1f96SGeliang Tang 		} else {
1174eda7acddSPeter Krystad 			len = TCPOLEN_MPTCP_MPC_ACK;
1175c94b1f96SGeliang Tang 		}
1176eda7acddSPeter Krystad 
117706fe1719SGeliang Tang 		if (opts->csum_reqd)
117806fe1719SGeliang Tang 			flag |= MPTCP_CAP_CHECKSUM_REQD;
117906fe1719SGeliang Tang 
11803df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
11813df523abSPeter Krystad 				      MPTCP_SUPPORTED_VERSION,
118206fe1719SGeliang Tang 				      flag);
1183cc7972eaSChristoph Paasch 
1184cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1185cc7972eaSChristoph Paasch 		    opts->suboptions))
1186cc7972eaSChristoph Paasch 			goto mp_capable_done;
1187cc7972eaSChristoph Paasch 
1188eda7acddSPeter Krystad 		put_unaligned_be64(opts->sndr_key, ptr);
1189eda7acddSPeter Krystad 		ptr += 2;
1190cc7972eaSChristoph Paasch 		if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1191cc7972eaSChristoph Paasch 			goto mp_capable_done;
1192cc7972eaSChristoph Paasch 
1193eda7acddSPeter Krystad 		put_unaligned_be64(opts->rcvr_key, ptr);
1194eda7acddSPeter Krystad 		ptr += 2;
1195cc7972eaSChristoph Paasch 		if (!opts->ext_copy.data_len)
1196cc7972eaSChristoph Paasch 			goto mp_capable_done;
1197cc7972eaSChristoph Paasch 
1198c94b1f96SGeliang Tang 		if (opts->csum_reqd) {
1199c94b1f96SGeliang Tang 			put_unaligned_be32(opts->ext_copy.data_len << 16 |
1200c94b1f96SGeliang Tang 					   mptcp_make_csum(&opts->ext_copy), ptr);
1201c94b1f96SGeliang Tang 		} else {
1202cc7972eaSChristoph Paasch 			put_unaligned_be32(opts->ext_copy.data_len << 16 |
1203cc7972eaSChristoph Paasch 					   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1204c94b1f96SGeliang Tang 		}
1205cc7972eaSChristoph Paasch 		ptr += 1;
1206eda7acddSPeter Krystad 	}
12076d0060f6SMat Martineau 
1208cc7972eaSChristoph Paasch mp_capable_done:
1209fef6b7ecSGeliang Tang 	if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
12106eb3d1e3SGeliang Tang 		u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
12116eb3d1e3SGeliang Tang 		u8 echo = MPTCP_ADDR_ECHO;
12123df523abSPeter Krystad 
12133df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1214fef6b7ecSGeliang Tang 		if (opts->addr.family == AF_INET6)
1215e1ef6832SGeliang Tang 			len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1216e1ef6832SGeliang Tang #endif
1217e1ef6832SGeliang Tang 
121830f60baeSGeliang Tang 		if (opts->addr.port)
121922fb85ffSGeliang Tang 			len += TCPOLEN_MPTCP_PORT_LEN;
122022fb85ffSGeliang Tang 
12216eb3d1e3SGeliang Tang 		if (opts->ahmac) {
12226eb3d1e3SGeliang Tang 			len += sizeof(opts->ahmac);
12236eb3d1e3SGeliang Tang 			echo = 0;
12246eb3d1e3SGeliang Tang 		}
12256eb3d1e3SGeliang Tang 
12263df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
122730f60baeSGeliang Tang 				      len, echo, opts->addr.id);
1228fef6b7ecSGeliang Tang 		if (opts->addr.family == AF_INET) {
122930f60baeSGeliang Tang 			memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
12303df523abSPeter Krystad 			ptr += 1;
12313df523abSPeter Krystad 		}
12323df523abSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1233fef6b7ecSGeliang Tang 		else if (opts->addr.family == AF_INET6) {
123430f60baeSGeliang Tang 			memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
12353df523abSPeter Krystad 			ptr += 4;
1236e1ef6832SGeliang Tang 		}
1237e1ef6832SGeliang Tang #endif
123822fb85ffSGeliang Tang 
123930f60baeSGeliang Tang 		if (!opts->addr.port) {
12403df523abSPeter Krystad 			if (opts->ahmac) {
12413df523abSPeter Krystad 				put_unaligned_be64(opts->ahmac, ptr);
12423df523abSPeter Krystad 				ptr += 2;
12433df523abSPeter Krystad 			}
124422fb85ffSGeliang Tang 		} else {
124530f60baeSGeliang Tang 			u16 port = ntohs(opts->addr.port);
124630f60baeSGeliang Tang 
124722fb85ffSGeliang Tang 			if (opts->ahmac) {
124822fb85ffSGeliang Tang 				u8 *bptr = (u8 *)ptr;
124922fb85ffSGeliang Tang 
125030f60baeSGeliang Tang 				put_unaligned_be16(port, bptr);
125122fb85ffSGeliang Tang 				bptr += 2;
125222fb85ffSGeliang Tang 				put_unaligned_be64(opts->ahmac, bptr);
125322fb85ffSGeliang Tang 				bptr += 8;
125422fb85ffSGeliang Tang 				put_unaligned_be16(TCPOPT_NOP << 8 |
125522fb85ffSGeliang Tang 						   TCPOPT_NOP, bptr);
125622fb85ffSGeliang Tang 
125722fb85ffSGeliang Tang 				ptr += 3;
125822fb85ffSGeliang Tang 			} else {
125930f60baeSGeliang Tang 				put_unaligned_be32(port << 16 |
126022fb85ffSGeliang Tang 						   TCPOPT_NOP << 8 |
126122fb85ffSGeliang Tang 						   TCPOPT_NOP, ptr);
126222fb85ffSGeliang Tang 				ptr += 1;
12633df523abSPeter Krystad 			}
126422fb85ffSGeliang Tang 		}
12653df523abSPeter Krystad 	}
12663df523abSPeter Krystad 
12673df523abSPeter Krystad 	if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
12686445e17aSGeliang Tang 		u8 i = 1;
12696445e17aSGeliang Tang 
12703df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
12716445e17aSGeliang Tang 				      TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
12726445e17aSGeliang Tang 				      0, opts->rm_list.ids[0]);
12736445e17aSGeliang Tang 
12746445e17aSGeliang Tang 		while (i < opts->rm_list.nr) {
12756445e17aSGeliang Tang 			u8 id1, id2, id3, id4;
12766445e17aSGeliang Tang 
12776445e17aSGeliang Tang 			id1 = opts->rm_list.ids[i];
12786445e17aSGeliang Tang 			id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
12796445e17aSGeliang Tang 			id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
12806445e17aSGeliang Tang 			id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
12816445e17aSGeliang Tang 			put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
12826445e17aSGeliang Tang 			ptr += 1;
12836445e17aSGeliang Tang 			i += 4;
12846445e17aSGeliang Tang 		}
12853df523abSPeter Krystad 	}
12863df523abSPeter Krystad 
128706706542SGeliang Tang 	if (OPTION_MPTCP_PRIO & opts->suboptions) {
128806706542SGeliang Tang 		const struct sock *ssk = (const struct sock *)tp;
128906706542SGeliang Tang 		struct mptcp_subflow_context *subflow;
129006706542SGeliang Tang 
129106706542SGeliang Tang 		subflow = mptcp_subflow_ctx(ssk);
129206706542SGeliang Tang 		subflow->send_mp_prio = 0;
129306706542SGeliang Tang 
129406706542SGeliang Tang 		*ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
129506706542SGeliang Tang 				      TCPOLEN_MPTCP_PRIO,
129606706542SGeliang Tang 				      opts->backup, TCPOPT_NOP);
129706706542SGeliang Tang 	}
129806706542SGeliang Tang 
1299ec3edaa7SPeter Krystad 	if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1300ec3edaa7SPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1301ec3edaa7SPeter Krystad 				      TCPOLEN_MPTCP_MPJ_SYN,
1302ec3edaa7SPeter Krystad 				      opts->backup, opts->join_id);
1303ec3edaa7SPeter Krystad 		put_unaligned_be32(opts->token, ptr);
1304ec3edaa7SPeter Krystad 		ptr += 1;
1305ec3edaa7SPeter Krystad 		put_unaligned_be32(opts->nonce, ptr);
1306ec3edaa7SPeter Krystad 		ptr += 1;
1307ec3edaa7SPeter Krystad 	}
1308ec3edaa7SPeter Krystad 
1309f296234cSPeter Krystad 	if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1310f296234cSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1311f296234cSPeter Krystad 				      TCPOLEN_MPTCP_MPJ_SYNACK,
1312f296234cSPeter Krystad 				      opts->backup, opts->join_id);
1313f296234cSPeter Krystad 		put_unaligned_be64(opts->thmac, ptr);
1314f296234cSPeter Krystad 		ptr += 2;
1315f296234cSPeter Krystad 		put_unaligned_be32(opts->nonce, ptr);
1316f296234cSPeter Krystad 		ptr += 1;
1317f296234cSPeter Krystad 	}
1318f296234cSPeter Krystad 
1319ec3edaa7SPeter Krystad 	if (OPTION_MPTCP_MPJ_ACK & opts->suboptions) {
1320ec3edaa7SPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1321ec3edaa7SPeter Krystad 				      TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1322ec3edaa7SPeter Krystad 		memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1323ec3edaa7SPeter Krystad 		ptr += 5;
1324ec3edaa7SPeter Krystad 	}
1325ec3edaa7SPeter Krystad 
1326dc87efdbSFlorian Westphal 	if (OPTION_MPTCP_RST & opts->suboptions)
1327dc87efdbSFlorian Westphal 		*ptr++ = mptcp_option(MPTCPOPT_RST,
1328dc87efdbSFlorian Westphal 				      TCPOLEN_MPTCP_RST,
1329dc87efdbSFlorian Westphal 				      opts->reset_transient,
1330dc87efdbSFlorian Westphal 				      opts->reset_reason);
1331dc87efdbSFlorian Westphal 
13326d0060f6SMat Martineau 	if (opts->ext_copy.use_ack || opts->ext_copy.use_map) {
13336d0060f6SMat Martineau 		struct mptcp_ext *mpext = &opts->ext_copy;
13346d0060f6SMat Martineau 		u8 len = TCPOLEN_MPTCP_DSS_BASE;
13356d0060f6SMat Martineau 		u8 flags = 0;
13366d0060f6SMat Martineau 
13376d0060f6SMat Martineau 		if (mpext->use_ack) {
1338a0c1d0eaSChristoph Paasch 			flags = MPTCP_DSS_HAS_ACK;
1339a0c1d0eaSChristoph Paasch 			if (mpext->ack64) {
13406d0060f6SMat Martineau 				len += TCPOLEN_MPTCP_DSS_ACK64;
1341a0c1d0eaSChristoph Paasch 				flags |= MPTCP_DSS_ACK64;
1342a0c1d0eaSChristoph Paasch 			} else {
1343a0c1d0eaSChristoph Paasch 				len += TCPOLEN_MPTCP_DSS_ACK32;
1344a0c1d0eaSChristoph Paasch 			}
13456d0060f6SMat Martineau 		}
13466d0060f6SMat Martineau 
13476d0060f6SMat Martineau 		if (mpext->use_map) {
13486d0060f6SMat Martineau 			len += TCPOLEN_MPTCP_DSS_MAP64;
13496d0060f6SMat Martineau 
13506d0060f6SMat Martineau 			/* Use only 64-bit mapping flags for now, add
13516d0060f6SMat Martineau 			 * support for optional 32-bit mappings later.
13526d0060f6SMat Martineau 			 */
13536d0060f6SMat Martineau 			flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
13546d0060f6SMat Martineau 			if (mpext->data_fin)
13556d0060f6SMat Martineau 				flags |= MPTCP_DSS_DATA_FIN;
1356c5b39e26SGeliang Tang 
1357c5b39e26SGeliang Tang 			if (opts->csum_reqd)
1358c5b39e26SGeliang Tang 				len += TCPOLEN_MPTCP_DSS_CHECKSUM;
13596d0060f6SMat Martineau 		}
13606d0060f6SMat Martineau 
13613df523abSPeter Krystad 		*ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
13626d0060f6SMat Martineau 
13636d0060f6SMat Martineau 		if (mpext->use_ack) {
1364a0c1d0eaSChristoph Paasch 			if (mpext->ack64) {
13656d0060f6SMat Martineau 				put_unaligned_be64(mpext->data_ack, ptr);
13666d0060f6SMat Martineau 				ptr += 2;
1367a0c1d0eaSChristoph Paasch 			} else {
1368a0c1d0eaSChristoph Paasch 				put_unaligned_be32(mpext->data_ack32, ptr);
1369a0c1d0eaSChristoph Paasch 				ptr += 1;
1370a0c1d0eaSChristoph Paasch 			}
13716d0060f6SMat Martineau 		}
13726d0060f6SMat Martineau 
13736d0060f6SMat Martineau 		if (mpext->use_map) {
13746d0060f6SMat Martineau 			put_unaligned_be64(mpext->data_seq, ptr);
13756d0060f6SMat Martineau 			ptr += 2;
13766d0060f6SMat Martineau 			put_unaligned_be32(mpext->subflow_seq, ptr);
13776d0060f6SMat Martineau 			ptr += 1;
1378c5b39e26SGeliang Tang 			if (opts->csum_reqd) {
1379c5b39e26SGeliang Tang 				put_unaligned_be32(mpext->data_len << 16 |
1380c5b39e26SGeliang Tang 						   mptcp_make_csum(mpext), ptr);
1381c5b39e26SGeliang Tang 			} else {
13826d0060f6SMat Martineau 				put_unaligned_be32(mpext->data_len << 16 |
13836d0060f6SMat Martineau 						   TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
13846d0060f6SMat Martineau 			}
13856d0060f6SMat Martineau 		}
1386c5b39e26SGeliang Tang 	}
1387fa3fe2b1SFlorian Westphal 
1388fa3fe2b1SFlorian Westphal 	if (tp)
1389fa3fe2b1SFlorian Westphal 		mptcp_set_rwin(tp);
1390eda7acddSPeter Krystad }
1391dc87efdbSFlorian Westphal 
1392dc87efdbSFlorian Westphal __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1393dc87efdbSFlorian Westphal {
1394dc87efdbSFlorian Westphal 	const struct mptcp_ext *ext = mptcp_get_ext(skb);
1395dc87efdbSFlorian Westphal 	u8 flags, reason;
1396dc87efdbSFlorian Westphal 
1397dc87efdbSFlorian Westphal 	if (ext) {
1398dc87efdbSFlorian Westphal 		flags = ext->reset_transient;
1399dc87efdbSFlorian Westphal 		reason = ext->reset_reason;
1400dc87efdbSFlorian Westphal 
1401dc87efdbSFlorian Westphal 		return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1402dc87efdbSFlorian Westphal 				    flags, reason);
1403dc87efdbSFlorian Westphal 	}
1404dc87efdbSFlorian Westphal 
1405dc87efdbSFlorian Westphal 	return htonl(0u);
1406dc87efdbSFlorian Westphal }
1407dc87efdbSFlorian Westphal EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
1408