Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-2.0
2 : : /* Multipath TCP
3 : : *
4 : : * Copyright (c) 2017 - 2019, Intel Corporation.
5 : : */
6 : :
7 : : #define pr_fmt(fmt) "MPTCP: " fmt
8 : :
9 : : #include <linux/kernel.h>
10 : : #include <crypto/sha2.h>
11 : : #include <net/tcp.h>
12 : : #include <net/mptcp.h>
13 : : #include "protocol.h"
14 : : #include "mib.h"
15 : :
16 : : #include <trace/events/mptcp.h>
17 : :
18 : : static bool mptcp_cap_flag_sha256(u8 flags)
19 : : {
20 : : return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
21 : : }
22 : :
23 : 2023602 : static void mptcp_parse_option(const struct sk_buff *skb,
24 : : const unsigned char *ptr, int opsize,
25 : : struct mptcp_options_received *mp_opt)
26 : : {
27 : 2023602 : u8 subtype = *ptr >> 4;
28 : 2023602 : int expected_opsize;
29 : 2023602 : u16 subopt;
30 : 2023602 : u8 version;
31 : 2023602 : u8 flags;
32 : 2023602 : u8 i;
33 : :
34 [ + + + + : 2023602 : switch (subtype) {
+ + + + +
- ]
35 : 7414 : case MPTCPOPT_MP_CAPABLE:
36 : : /* strict size checking */
37 [ + + ]: 7414 : if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
38 [ + + ]: 4236 : if (skb->len > tcp_hdr(skb)->doff << 2)
39 : : expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
40 : : else
41 : 3197 : expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
42 : : subopt = OPTION_MPTCP_MPC_ACK;
43 : : } else {
44 [ + + ]: 3178 : if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) {
45 : : expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
46 : : subopt = OPTION_MPTCP_MPC_SYNACK;
47 : : } else {
48 : 1694 : expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
49 : 1694 : subopt = OPTION_MPTCP_MPC_SYN;
50 : : }
51 : : }
52 : :
53 : : /* Cfr RFC 8684 Section 3.3.0:
54 : : * If a checksum is present but its use had
55 : : * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
56 : : * close the subflow with a RST, as it is not behaving as negotiated.
57 : : * If a checksum is not present when its use has been negotiated, the
58 : : * receiver MUST close the subflow with a RST, as it is considered
59 : : * broken
60 : : * We parse even option with mismatching csum presence, so that
61 : : * later in subflow_data_ready we can trigger the reset.
62 : : */
63 [ + + ]: 7414 : if (opsize != expected_opsize &&
64 : 105 : (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
65 [ + + ]: 105 : opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
66 : : break;
67 : :
68 : : /* try to be gentle vs future versions on the initial syn */
69 : 7402 : version = *ptr++ & MPTCP_VERSION_MASK;
70 [ + + ]: 7402 : if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
71 [ + + ]: 5714 : if (version != MPTCP_SUPPORTED_VERSION)
72 : : break;
73 [ + - ]: 1688 : } else if (version < MPTCP_SUPPORTED_VERSION) {
74 : : break;
75 : : }
76 : :
77 : 7396 : flags = *ptr++;
78 [ + + + + ]: 7396 : if (!mptcp_cap_flag_sha256(flags) ||
79 : : (flags & MPTCP_CAP_EXTENSIBILITY))
80 : : break;
81 : :
82 : : /* RFC 6824, Section 3.1:
83 : : * "For the Checksum Required bit (labeled "A"), if either
84 : : * host requires the use of checksums, checksums MUST be used.
85 : : * In other words, the only way for checksums not to be used
86 : : * is if both hosts in their SYNs set A=0."
87 : : */
88 [ + + ]: 7360 : if (flags & MPTCP_CAP_CHECKSUM_REQD)
89 : 569 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
90 : :
91 : 7360 : mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
92 : :
93 : 7360 : mp_opt->suboptions |= subopt;
94 [ + + ]: 7360 : if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
95 [ + + ]: 5684 : mp_opt->sndr_key = get_unaligned_be64(ptr);
96 : 5684 : ptr += 8;
97 : : }
98 [ + + ]: 5684 : if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
99 : 4218 : mp_opt->rcvr_key = get_unaligned_be64(ptr);
100 : 4218 : ptr += 8;
101 : : }
102 [ + + ]: 5894 : if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
103 : : /* Section 3.1.:
104 : : * "the data parameters in a MP_CAPABLE are semantically
105 : : * equivalent to those in a DSS option and can be used
106 : : * interchangeably."
107 : : */
108 : 1039 : mp_opt->suboptions |= OPTION_MPTCP_DSS;
109 : 1039 : mp_opt->use_map = 1;
110 : 1039 : mp_opt->mpc_map = 1;
111 [ + + ]: 1039 : mp_opt->data_len = get_unaligned_be16(ptr);
112 : 1039 : ptr += 2;
113 : : }
114 [ + + ]: 1039 : if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
115 : 93 : mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
116 : 93 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
117 : 93 : ptr += 2;
118 : : }
119 [ - + - - ]: 7360 : pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u\n",
120 : : version, flags, opsize, mp_opt->sndr_key,
121 : : mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
122 : : break;
123 : :
124 : 1956 : case MPTCPOPT_MP_JOIN:
125 [ + + ]: 1956 : if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
126 : 500 : mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYN;
127 : 500 : mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
128 : 500 : mp_opt->join_id = *ptr++;
129 [ - + ]: 500 : mp_opt->token = get_unaligned_be32(ptr);
130 : 500 : ptr += 4;
131 : 500 : mp_opt->nonce = get_unaligned_be32(ptr);
132 : 500 : ptr += 4;
133 [ - + - - ]: 500 : pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u\n",
134 : : mp_opt->backup, mp_opt->join_id,
135 : : mp_opt->token, mp_opt->nonce);
136 [ + + ]: 1456 : } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
137 : 460 : mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYNACK;
138 : 460 : mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
139 : 460 : mp_opt->join_id = *ptr++;
140 [ - + ]: 460 : mp_opt->thmac = get_unaligned_be64(ptr);
141 : 460 : ptr += 8;
142 [ - + ]: 460 : mp_opt->nonce = get_unaligned_be32(ptr);
143 : 460 : ptr += 4;
144 [ - + - - ]: 460 : pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
145 : : mp_opt->backup, mp_opt->join_id,
146 : : mp_opt->thmac, mp_opt->nonce);
147 [ + - ]: 996 : } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
148 : 996 : mp_opt->suboptions |= OPTION_MPTCP_MPJ_ACK;
149 : 996 : ptr += 2;
150 : 996 : memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
151 [ - + - - ]: 996 : pr_debug("MP_JOIN hmac\n");
152 : : }
153 : : break;
154 : :
155 : : case MPTCPOPT_DSS:
156 [ - + - - ]: 2012766 : pr_debug("DSS\n");
157 : 2012766 : ptr++;
158 : :
159 : 2012766 : flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
160 : 2012766 : mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
161 : 2012766 : mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
162 : 2012766 : mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
163 : 2012766 : mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
164 : 2012766 : mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
165 : :
166 [ - + - - ]: 2012766 : pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d\n",
167 : : mp_opt->data_fin, mp_opt->dsn64,
168 : : mp_opt->use_map, mp_opt->ack64,
169 : : mp_opt->use_ack);
170 : :
171 : 2012766 : expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
172 : :
173 [ + - ]: 2012766 : if (mp_opt->use_ack) {
174 [ + + ]: 2012766 : if (mp_opt->ack64)
175 : : expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
176 : : else
177 : 335254 : expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
178 : : }
179 : :
180 [ + + ]: 2012766 : if (mp_opt->use_map) {
181 [ + + ]: 1268982 : if (mp_opt->dsn64)
182 : 1268940 : expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
183 : : else
184 : 42 : expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
185 : : }
186 : :
187 : : /* Always parse any csum presence combination, we will enforce
188 : : * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
189 : : */
190 [ + + ]: 2012766 : if (opsize != expected_opsize &&
191 [ + - ]: 112033 : opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
192 : : break;
193 : :
194 : 2012766 : mp_opt->suboptions |= OPTION_MPTCP_DSS;
195 [ + - ]: 2012766 : if (mp_opt->use_ack) {
196 [ + + ]: 2012766 : if (mp_opt->ack64) {
197 : 1677512 : mp_opt->data_ack = get_unaligned_be64(ptr);
198 : 1677512 : ptr += 8;
199 : : } else {
200 : 335254 : mp_opt->data_ack = get_unaligned_be32(ptr);
201 : 335254 : ptr += 4;
202 : : }
203 : :
204 [ - + - - ]: 2012766 : pr_debug("data_ack=%llu\n", mp_opt->data_ack);
205 : : }
206 : :
207 [ + + ]: 2012766 : if (mp_opt->use_map) {
208 [ + + ]: 1268982 : if (mp_opt->dsn64) {
209 : 1268940 : mp_opt->data_seq = get_unaligned_be64(ptr);
210 : 1268940 : ptr += 8;
211 : : } else {
212 : 42 : mp_opt->data_seq = get_unaligned_be32(ptr);
213 : 42 : ptr += 4;
214 : : }
215 : :
216 [ + + ]: 1268982 : mp_opt->subflow_seq = get_unaligned_be32(ptr);
217 : 1268982 : ptr += 4;
218 : :
219 [ + + ]: 1268982 : mp_opt->data_len = get_unaligned_be16(ptr);
220 : 1268982 : ptr += 2;
221 : :
222 [ + + ]: 1268982 : if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
223 : 112033 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
224 : 112033 : mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
225 : 112033 : ptr += 2;
226 : : }
227 : :
228 [ - + - - ]: 1268982 : pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
229 : : mp_opt->data_seq, mp_opt->subflow_seq,
230 : : mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
231 : : mp_opt->csum);
232 : : }
233 : :
234 : : break;
235 : :
236 : 795 : case MPTCPOPT_ADD_ADDR:
237 : 795 : mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
238 [ + + ]: 795 : if (!mp_opt->echo) {
239 : 403 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
240 [ + + ]: 403 : opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
241 : 311 : mp_opt->addr.family = AF_INET;
242 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
243 : 92 : else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
244 [ + - ]: 92 : opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
245 : 92 : mp_opt->addr.family = AF_INET6;
246 : : #endif
247 : : else
248 : : break;
249 : : } else {
250 : 392 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
251 [ + + ]: 392 : opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
252 : 302 : mp_opt->addr.family = AF_INET;
253 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
254 : 90 : else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
255 [ + - ]: 90 : opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
256 : 90 : mp_opt->addr.family = AF_INET6;
257 : : #endif
258 : : else
259 : : break;
260 : : }
261 : :
262 : 795 : mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
263 : 795 : mp_opt->addr.id = *ptr++;
264 : 795 : mp_opt->addr.port = 0;
265 : 795 : mp_opt->ahmac = 0;
266 [ + + ]: 795 : if (mp_opt->addr.family == AF_INET) {
267 : 613 : memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
268 : 613 : ptr += 4;
269 : 613 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
270 [ + + ]: 613 : opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
271 : 74 : mp_opt->addr.port = htons(get_unaligned_be16(ptr));
272 : 74 : ptr += 2;
273 : : }
274 : : }
275 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
276 : : else {
277 : 182 : memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
278 : 182 : ptr += 16;
279 : 182 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
280 [ + + ]: 182 : opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
281 : 12 : mp_opt->addr.port = htons(get_unaligned_be16(ptr));
282 : 12 : ptr += 2;
283 : : }
284 : : }
285 : : #endif
286 [ + + ]: 795 : if (!mp_opt->echo) {
287 : 403 : mp_opt->ahmac = get_unaligned_be64(ptr);
288 : 403 : ptr += 8;
289 : : }
290 [ - + - - : 795 : pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d\n",
- - ]
291 : : (mp_opt->addr.family == AF_INET6) ? "6" : "",
292 : : mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
293 : : break;
294 : :
295 : 108 : case MPTCPOPT_RM_ADDR:
296 [ + - ]: 108 : if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
297 : : opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
298 : : break;
299 : :
300 : 108 : ptr++;
301 : :
302 : 108 : mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
303 : 108 : mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
304 [ + + ]: 224 : for (i = 0; i < mp_opt->rm_list.nr; i++)
305 : 116 : mp_opt->rm_list.ids[i] = *ptr++;
306 [ - + - - ]: 108 : pr_debug("RM_ADDR: rm_list_nr=%d\n", mp_opt->rm_list.nr);
307 : : break;
308 : :
309 : 46 : case MPTCPOPT_MP_PRIO:
310 [ + + ]: 46 : if (opsize != TCPOLEN_MPTCP_PRIO)
311 : : break;
312 : :
313 : 40 : mp_opt->suboptions |= OPTION_MPTCP_PRIO;
314 : 40 : mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
315 [ - + - - ]: 40 : pr_debug("MP_PRIO: prio=%d\n", mp_opt->backup);
316 : : break;
317 : :
318 : 250 : case MPTCPOPT_MP_FASTCLOSE:
319 [ + - ]: 250 : if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
320 : : break;
321 : :
322 : 250 : ptr += 2;
323 [ - + ]: 250 : mp_opt->rcvr_key = get_unaligned_be64(ptr);
324 : 250 : ptr += 8;
325 : 250 : mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
326 [ - + - - ]: 250 : pr_debug("MP_FASTCLOSE: recv_key=%llu\n", mp_opt->rcvr_key);
327 : : break;
328 : :
329 : 261 : case MPTCPOPT_RST:
330 [ + - ]: 261 : if (opsize != TCPOLEN_MPTCP_RST)
331 : : break;
332 : :
333 [ + + ]: 261 : if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
334 : : break;
335 : :
336 : 249 : mp_opt->suboptions |= OPTION_MPTCP_RST;
337 : 249 : flags = *ptr++;
338 : 249 : mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
339 : 249 : mp_opt->reset_reason = *ptr;
340 [ - + - - ]: 249 : pr_debug("MP_RST: transient=%u reason=%u\n",
341 : : mp_opt->reset_transient, mp_opt->reset_reason);
342 : : break;
343 : :
344 : 6 : case MPTCPOPT_MP_FAIL:
345 [ + - ]: 6 : if (opsize != TCPOLEN_MPTCP_FAIL)
346 : : break;
347 : :
348 : 6 : ptr += 2;
349 : 6 : mp_opt->suboptions |= OPTION_MPTCP_FAIL;
350 [ - + ]: 6 : mp_opt->fail_seq = get_unaligned_be64(ptr);
351 [ - + - - ]: 6 : pr_debug("MP_FAIL: data_seq=%llu\n", mp_opt->fail_seq);
352 : : break;
353 : :
354 : : default:
355 : : break;
356 : : }
357 : 2023602 : }
358 : :
359 : 2023786 : void mptcp_get_options(const struct sk_buff *skb,
360 : : struct mptcp_options_received *mp_opt)
361 : : {
362 : 2023786 : const struct tcphdr *th = tcp_hdr(skb);
363 : 2023786 : const unsigned char *ptr;
364 : 2023786 : int length;
365 : :
366 : : /* Ensure that casting the whole status to u32 is efficient and safe */
367 : 2023786 : BUILD_BUG_ON(sizeof_field(struct mptcp_options_received, status) != sizeof(u32));
368 : 2023786 : BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_options_received, status),
369 : : sizeof(u32)));
370 : 2023786 : *(u32 *)&mp_opt->status = 0;
371 : :
372 : 2023786 : length = (th->doff * 4) - sizeof(struct tcphdr);
373 : 2023786 : ptr = (const unsigned char *)(th + 1);
374 : :
375 [ + + ]: 12467338 : while (length > 0) {
376 : 10443552 : int opcode = *ptr++;
377 : 10443552 : int opsize;
378 : :
379 [ + + - ]: 10443552 : switch (opcode) {
380 : : case TCPOPT_EOL:
381 : : return;
382 : 6375366 : case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
383 : 6375366 : length--;
384 : 6375366 : continue;
385 : 4068186 : default:
386 [ + - ]: 4068186 : if (length < 2)
387 : : return;
388 : 4068186 : opsize = *ptr++;
389 [ + - ]: 4068186 : if (opsize < 2) /* "silly options" */
390 : : return;
391 [ + - ]: 4068186 : if (opsize > length)
392 : : return; /* don't parse partial options */
393 [ + + ]: 4068186 : if (opcode == TCPOPT_MPTCP)
394 : 2023602 : mptcp_parse_option(skb, ptr, opsize, mp_opt);
395 : 4068186 : ptr += opsize - 2;
396 : 4068186 : length -= opsize;
397 : : }
398 : : }
399 : : }
400 : :
401 : 2340 : bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
402 : : unsigned int *size, struct mptcp_out_options *opts)
403 : : {
404 [ + + ]: 2340 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
405 : :
406 : : /* we will use snd_isn to detect first pkt [re]transmission
407 : : * in mptcp_established_options_mp()
408 : : */
409 : 2340 : subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
410 [ + + ]: 2340 : if (subflow->request_mptcp) {
411 [ + + ]: 1740 : if (unlikely(subflow_simultaneous_connect(sk))) {
412 [ - + ]: 6 : WARN_ON_ONCE(!mptcp_try_fallback(sk, MPTCP_MIB_SIMULTCONNFALLBACK));
413 : :
414 : : /* Ensure mptcp_finish_connect() will not process the
415 : : * MPC handshake.
416 : : */
417 : 6 : subflow->request_mptcp = 0;
418 : 6 : return false;
419 : : }
420 : :
421 : 1734 : opts->suboptions = OPTION_MPTCP_MPC_SYN;
422 : 1734 : opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
423 : 1734 : opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
424 : 1734 : *size = TCPOLEN_MPTCP_MPC_SYN;
425 : 1734 : return true;
426 [ + + ]: 600 : } else if (subflow->request_join) {
427 [ - + - - ]: 570 : pr_debug("remote_token=%u, nonce=%u\n", subflow->remote_token,
428 : : subflow->local_nonce);
429 : 570 : opts->suboptions = OPTION_MPTCP_MPJ_SYN;
430 : 570 : opts->join_id = subflow->local_id;
431 : 570 : opts->token = subflow->remote_token;
432 : 570 : opts->nonce = subflow->local_nonce;
433 : 570 : opts->backup = subflow->request_bkup;
434 : 570 : *size = TCPOLEN_MPTCP_MPJ_SYN;
435 : 570 : return true;
436 : : }
437 : : return false;
438 : : }
439 : :
440 : 936 : static void clear_3rdack_retransmission(struct sock *sk)
441 : : {
442 : 936 : struct inet_connection_sock *icsk = inet_csk(sk);
443 : :
444 : 936 : sk_stop_timer(sk, &icsk->icsk_delack_timer);
445 : 936 : icsk->icsk_ack.ato = 0;
446 : 936 : icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
447 : 936 : }
448 : :
449 : 3429185 : static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
450 : : bool snd_data_fin_enable,
451 : : unsigned int *size,
452 : : struct mptcp_out_options *opts)
453 : : {
454 [ - + ]: 3429185 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
455 [ - + ]: 3429185 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
456 : 3429185 : struct mptcp_ext *mpext;
457 : 3429185 : unsigned int data_len;
458 : 3429185 : u8 len;
459 : :
460 : : /* When skb is not available, we better over-estimate the emitted
461 : : * options len. A full DSS option (28 bytes) is longer than
462 : : * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
463 : : * tell the caller to defer the estimate to
464 : : * mptcp_established_options_dss(), which will reserve enough space.
465 : : */
466 [ + + ]: 3429185 : if (!skb)
467 : : return false;
468 : :
469 : : /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
470 [ - + + + : 1576069 : if (READ_ONCE(subflow->fully_established) || snd_data_fin_enable ||
+ + ]
[ + + + + ]
471 [ + + ]: 4062 : subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
472 [ + + ]: 2796 : sk->sk_state != TCP_ESTABLISHED)
473 : : return false;
474 : :
475 [ + + ]: 2796 : if (subflow->mp_capable) {
476 [ + + ]: 2324 : mpext = mptcp_get_ext(skb);
477 [ + + ]: 1591 : data_len = mpext ? mpext->data_len : 0;
478 : :
479 : : /* we will check ops->data_len in mptcp_write_options() to
480 : : * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
481 : : * TCPOLEN_MPTCP_MPC_ACK
482 : : */
483 : 2324 : opts->data_len = data_len;
484 : 2324 : opts->suboptions = OPTION_MPTCP_MPC_ACK;
485 : 2324 : opts->sndr_key = subflow->local_key;
486 : 2324 : opts->rcvr_key = subflow->remote_key;
487 [ - + ]: 2324 : opts->csum_reqd = READ_ONCE(msk->csum_enabled);
488 : 2324 : opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
489 : :
490 : : /* Section 3.1.
491 : : * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
492 : : * packets that start the first subflow of an MPTCP connection,
493 : : * as well as the first packet that carries data
494 : : */
495 [ + + ]: 2324 : if (data_len > 0) {
496 : 857 : len = TCPOLEN_MPTCP_MPC_ACK_DATA;
497 [ + + ]: 857 : if (opts->csum_reqd) {
498 : : /* we need to propagate more info to csum the pseudo hdr */
499 : 87 : opts->data_seq = mpext->data_seq;
500 : 87 : opts->subflow_seq = mpext->subflow_seq;
501 : 87 : opts->csum = mpext->csum;
502 : 87 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
503 : : }
504 : 857 : *size = ALIGN(len, 4);
505 : : } else {
506 : 1467 : *size = TCPOLEN_MPTCP_MPC_ACK;
507 : : }
508 : :
509 [ - + - - ]: 2324 : pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d\n",
510 : : subflow, subflow->local_key, subflow->remote_key,
511 : : data_len);
512 : :
513 : 2324 : return true;
514 [ + + ]: 472 : } else if (subflow->mp_join) {
515 : 472 : opts->suboptions = OPTION_MPTCP_MPJ_ACK;
516 : 472 : memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
517 : 472 : *size = TCPOLEN_MPTCP_MPJ_ACK;
518 [ - + - - ]: 472 : pr_debug("subflow=%p\n", subflow);
519 : :
520 : : /* we can use the full delegate action helper only from BH context
521 : : * If we are in process context - sk is flushing the backlog at
522 : : * socket lock release time - just set the appropriate flag, will
523 : : * be handled by the release callback
524 : : */
525 [ + + ]: 472 : if (sock_owned_by_user(sk))
526 : 385 : set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
527 : : else
528 : 87 : mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
529 : 472 : return true;
530 : : }
531 : : return false;
532 : : }
533 : :
534 : 33981 : static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
535 : : struct sk_buff *skb, struct mptcp_ext *ext)
536 : : {
537 : : /* The write_seq value has already been incremented, so the actual
538 : : * sequence number for the DATA_FIN is one less.
539 : : */
540 [ - + ]: 33981 : u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
541 : :
542 [ + + - + ]: 33981 : if (!ext->use_map || !skb->len) {
543 : : /* RFC6824 requires a DSS mapping with specific values
544 : : * if DATA_FIN is set but no data payload is mapped
545 : : */
546 : 12467 : ext->data_fin = 1;
547 : 12467 : ext->use_map = 1;
548 : 12467 : ext->dsn64 = 1;
549 : 12467 : ext->data_seq = data_fin_tx_seq;
550 : 12467 : ext->subflow_seq = 0;
551 : 12467 : ext->data_len = 1;
552 [ + + ]: 21514 : } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
553 : : /* If there's an existing DSS mapping and it is the
554 : : * final mapping, DATA_FIN consumes 1 additional byte of
555 : : * mapping space.
556 : : */
557 : 2038 : ext->data_fin = 1;
558 : 2038 : ext->data_len++;
559 : : }
560 : 33981 : }
561 : :
562 : 3426389 : static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
563 : : bool snd_data_fin_enable,
564 : : unsigned int *size,
565 : : struct mptcp_out_options *opts)
566 : : {
567 [ - + ]: 3426389 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
568 [ - + ]: 3426389 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
569 [ - + ]: 3426389 : struct tcp_sock *tp = tcp_sk(sk);
570 : 3426389 : unsigned int dss_size = 0;
571 : 3426389 : struct mptcp_ext *mpext;
572 : 3426389 : unsigned int ack_size;
573 : 3426389 : bool ret = false;
574 : :
575 [ - + ]: 3426389 : opts->csum_reqd = READ_ONCE(msk->csum_enabled);
576 [ + + ]: 3426389 : mpext = skb ? mptcp_get_ext(skb) : NULL;
577 : :
578 [ + + - + : 1573273 : if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
+ + ]
579 : 2656571 : unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
580 : :
581 [ + + ]: 2656571 : if (mpext) {
582 [ + + ]: 790988 : if (opts->csum_reqd)
583 : 78227 : map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
584 : :
585 : 790988 : opts->ext_copy = *mpext;
586 : : }
587 : :
588 : 2656571 : dss_size = map_size;
589 [ + + ]: 2656571 : if (skb && snd_data_fin_enable)
590 : 33981 : mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
591 : 2656571 : opts->suboptions = OPTION_MPTCP_DSS;
592 : 2656571 : ret = true;
593 : : }
594 : :
595 : : /* passive sockets msk will set the 'can_ack' after accept(), even
596 : : * if the first subflow may have the already the remote key handy
597 : : */
598 : 3426389 : opts->ext_copy.use_ack = 0;
599 [ - + + + ]: 3426389 : if (!READ_ONCE(msk->can_ack)) {
[ + + ]
600 : 32 : *size = ALIGN(dss_size, 4);
601 : 32 : return ret;
602 : : }
603 : :
604 [ - + + + ]: 3426357 : if (READ_ONCE(msk->use_64bit_ack)) {
[ + + ]
605 : 3015565 : ack_size = TCPOLEN_MPTCP_DSS_ACK64;
606 : 3015565 : opts->ext_copy.ack64 = 1;
607 : : } else {
608 : 410792 : ack_size = TCPOLEN_MPTCP_DSS_ACK32;
609 : 410792 : opts->ext_copy.ack64 = 0;
610 : : }
611 : 3426357 : opts->ext_copy.use_ack = 1;
612 : 3426357 : opts->suboptions = OPTION_MPTCP_DSS;
613 : :
614 : : /* Add kind/length/subtype/flag overhead if mapping is not populated */
615 [ + + ]: 3426357 : if (dss_size == 0)
616 : 769818 : ack_size += TCPOLEN_MPTCP_DSS_BASE;
617 : :
618 : : /* The caller is __tcp_transmit_skb(), and will compute the new rcv
619 : : * wnd soon: ensure that the window can shrink.
620 : : */
621 [ + + ]: 3426357 : if (skb)
622 : 1573273 : tp->rcv_wnd = tp->rcv_nxt - tp->rcv_wup;
623 : :
624 : 3426357 : dss_size += ack_size;
625 : :
626 : 3426357 : *size = ALIGN(dss_size, 4);
627 : 3426357 : return true;
628 : : }
629 : :
630 : 862 : static u64 add_addr_generate_hmac(u64 key1, u64 key2,
631 : : struct mptcp_addr_info *addr)
632 : : {
633 : 862 : u16 port = ntohs(addr->port);
634 : 862 : u8 hmac[SHA256_DIGEST_SIZE];
635 : 862 : u8 msg[19];
636 : 862 : int i = 0;
637 : :
638 : 862 : msg[i++] = addr->id;
639 [ + + ]: 862 : if (addr->family == AF_INET) {
640 : 666 : memcpy(&msg[i], &addr->addr.s_addr, 4);
641 : 666 : i += 4;
642 : : }
643 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
644 [ + - ]: 196 : else if (addr->family == AF_INET6) {
645 : 196 : memcpy(&msg[i], &addr->addr6.s6_addr, 16);
646 : 196 : i += 16;
647 : : }
648 : : #endif
649 : 862 : msg[i++] = port >> 8;
650 : 862 : msg[i++] = port & 0xFF;
651 : :
652 : 862 : mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
653 : :
654 : 862 : return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
655 : : }
656 : :
657 : 3429181 : static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
658 : : unsigned int *size,
659 : : unsigned int remaining,
660 : : struct mptcp_out_options *opts)
661 : : {
662 [ - + ]: 3429181 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
663 [ - + ]: 3429181 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
664 : 3429181 : bool drop_other_suboptions = false;
665 : 3429181 : unsigned int opt_size = *size;
666 : 3429181 : struct mptcp_addr_info addr;
667 : 3429181 : bool echo;
668 : 3429181 : int len;
669 : :
670 : : /* add addr will strip the existing options, be sure to avoid breaking
671 : : * MPC/MPJ handshakes
672 : : */
673 [ + + ]: 3429181 : if (!mptcp_pm_should_add_signal(msk) ||
674 [ + - + + ]: 1722 : (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
675 : 861 : !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &addr,
676 : : &echo, &drop_other_suboptions))
677 : 3428328 : return false;
678 : :
679 : : /*
680 : : * Later on, mptcp_write_options() will enforce mutually exclusion with
681 : : * DSS, bail out if such option is set and we can't drop it.
682 : : */
683 [ - + + - ]: 853 : if (drop_other_suboptions)
[ + - ]
684 : 853 : remaining += opt_size;
685 [ # # ]: 0 : else if (opts->suboptions & OPTION_MPTCP_DSS)
686 : : return false;
687 : :
688 [ - + + + ]: 853 : len = mptcp_add_addr_len(addr.family, echo, !!addr.port);
[ + + ]
689 [ - + ]: 853 : if (remaining < len)
690 : : return false;
691 : :
692 : 853 : *size = len;
693 [ - + + - ]: 853 : if (drop_other_suboptions) {
[ + - ]
694 [ - + - - ]: 853 : pr_debug("drop other suboptions\n");
695 : 853 : opts->suboptions = 0;
696 : :
697 : : /* note that e.g. DSS could have written into the memory
698 : : * aliased by ahmac, we must reset the field here
699 : : * to avoid appending the hmac even for ADD_ADDR echo
700 : : * options
701 : : */
702 : 853 : opts->ahmac = 0;
703 : 853 : *size -= opt_size;
704 : : }
705 : 853 : opts->addr = addr;
706 : 853 : opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
707 [ - + + + ]: 853 : if (!echo) {
[ + + ]
708 [ + - ]: 459 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
709 : 459 : opts->ahmac = add_addr_generate_hmac(READ_ONCE(msk->local_key),
710 : 459 : READ_ONCE(msk->remote_key),
711 : : &opts->addr);
712 : : } else {
713 [ + - + - ]: 788 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
[ # # ]
714 : : }
715 [ - + - - : 853 : pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d\n",
- - ]
[ - + - - ]
716 : : opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
717 : :
718 : : return true;
719 : : }
720 : :
721 : 3428328 : static bool mptcp_established_options_rm_addr(struct sock *sk,
722 : : unsigned int *size,
723 : : unsigned int remaining,
724 : : struct mptcp_out_options *opts)
725 : : {
726 [ - + ]: 3428328 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
727 [ - + ]: 3428328 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
728 : 3428328 : struct mptcp_rm_list rm_list;
729 : 3428328 : int i, len;
730 : :
731 [ + + - + ]: 3428436 : if (!mptcp_pm_should_rm_signal(msk) ||
732 : 108 : !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
733 : 3428220 : return false;
734 : :
735 [ - + ]: 292 : len = mptcp_rm_addr_len(&rm_list);
736 : 108 : if (len < 0)
737 : : return false;
738 [ - + ]: 108 : if (remaining < len)
739 : : return false;
740 : :
741 : 108 : *size = len;
742 : 108 : opts->suboptions |= OPTION_MPTCP_RM_ADDR;
743 : 108 : opts->rm_list = rm_list;
744 : :
745 [ + + ]: 224 : for (i = 0; i < opts->rm_list.nr; i++)
746 [ - + - - ]: 116 : pr_debug("rm_list_ids[%d]=%d\n", i, opts->rm_list.ids[i]);
747 : 108 : MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
748 : 108 : return true;
749 : : }
750 : :
751 : 3429181 : static bool mptcp_established_options_mp_prio(struct sock *sk,
752 : : unsigned int *size,
753 : : unsigned int remaining,
754 : : struct mptcp_out_options *opts)
755 : : {
756 [ + + ]: 3429181 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
757 : :
758 : : /* can't send MP_PRIO with MPC, as they share the same option space:
759 : : * 'backup'. Also it makes no sense at all
760 : : */
761 [ + + + - ]: 3429181 : if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
762 : : return false;
763 : :
764 : : /* account for the trailing 'nop' option */
765 [ + - ]: 28 : if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
766 : : return false;
767 : :
768 : 28 : *size = TCPOLEN_MPTCP_PRIO_ALIGN;
769 : 28 : opts->suboptions |= OPTION_MPTCP_PRIO;
770 : 28 : opts->backup = subflow->request_bkup;
771 : :
772 [ - + - - ]: 28 : pr_debug("prio=%d\n", opts->backup);
773 : :
774 : : return true;
775 : : }
776 : :
777 : 489 : static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
778 : : unsigned int *size,
779 : : unsigned int remaining,
780 : : struct mptcp_out_options *opts)
781 : : {
782 [ + - ]: 489 : const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
783 : :
784 [ + - ]: 489 : if (remaining < TCPOLEN_MPTCP_RST)
785 : : return false;
786 : :
787 : 489 : *size = TCPOLEN_MPTCP_RST;
788 : 489 : opts->suboptions |= OPTION_MPTCP_RST;
789 : 489 : opts->reset_transient = subflow->reset_transient;
790 : 489 : opts->reset_reason = subflow->reset_reason;
791 [ + - ]: 489 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
792 : :
793 : : return true;
794 : : }
795 : :
796 : 489 : static bool mptcp_established_options_fastclose(struct sock *sk,
797 : : unsigned int *size,
798 : : unsigned int remaining,
799 : : struct mptcp_out_options *opts)
800 : : {
801 [ - + ]: 489 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
802 [ - + ]: 489 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
803 : :
804 [ + + ]: 489 : if (likely(!subflow->send_fastclose))
805 : : return false;
806 : :
807 [ + - ]: 430 : if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
808 : : return false;
809 : :
810 : 430 : *size = TCPOLEN_MPTCP_FASTCLOSE;
811 : 430 : opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
812 : 430 : opts->rcvr_key = READ_ONCE(msk->remote_key);
813 : :
814 [ - + - - ]: 430 : pr_debug("FASTCLOSE key=%llu\n", opts->rcvr_key);
815 [ + - ]: 430 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
816 : : return true;
817 : : }
818 : :
819 : 3426448 : static bool mptcp_established_options_mp_fail(struct sock *sk,
820 : : unsigned int *size,
821 : : unsigned int remaining,
822 : : struct mptcp_out_options *opts)
823 : : {
824 [ + + ]: 3426448 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
825 : :
826 [ + + ]: 3426448 : if (likely(!subflow->send_mp_fail))
827 : : return false;
828 : :
829 [ + - ]: 6 : if (remaining < TCPOLEN_MPTCP_FAIL)
830 : : return false;
831 : :
832 : 6 : *size = TCPOLEN_MPTCP_FAIL;
833 : 6 : opts->suboptions |= OPTION_MPTCP_FAIL;
834 : 6 : opts->fail_seq = subflow->map_seq;
835 : :
836 [ - + - - ]: 6 : pr_debug("MP_FAIL fail_seq=%llu\n", opts->fail_seq);
837 [ + - ]: 6 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
838 : :
839 : : return true;
840 : : }
841 : :
842 : 3522946 : bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
843 : : unsigned int *size, unsigned int remaining,
844 : : struct mptcp_out_options *opts)
845 : : {
846 [ - + ]: 3522946 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
847 [ - + ]: 3522946 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
848 : 3522946 : unsigned int opt_size = 0;
849 : 3522946 : bool snd_data_fin;
850 : 3522946 : bool ret = false;
851 : :
852 : 3522946 : opts->suboptions = 0;
853 : :
854 : : /* Force later mptcp_write_options(), but do not use any actual
855 : : * option space.
856 : : */
857 [ + + + + ]: 3522946 : if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
858 : : return true;
859 : :
860 [ + + + + ]: 3429674 : if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
861 [ + + + + ]: 548 : if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
862 : 59 : mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
863 : 432 : *size += opt_size;
864 : 432 : remaining -= opt_size;
865 : : }
866 : : /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
867 [ + - ]: 489 : if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
868 : 489 : *size += opt_size;
869 : 489 : remaining -= opt_size;
870 : : }
871 : 489 : return true;
872 : : }
873 : :
874 [ + + ]: 3429185 : snd_data_fin = mptcp_data_fin_enabled(msk);
875 [ + + ]: 3429185 : if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
876 : : ret = true;
877 [ + - ]: 3426389 : else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
878 : 3426389 : unsigned int mp_fail_size;
879 : :
880 : 3426389 : ret = true;
881 [ + + ]: 3426389 : if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
882 : : remaining - opt_size, opts)) {
883 : 4 : *size += opt_size + mp_fail_size;
884 : 4 : remaining -= opt_size - mp_fail_size;
885 : 4 : return true;
886 : : }
887 : : }
888 : :
889 : : /* we reserved enough space for the above options, and exceeding the
890 : : * TCP option space would be fatal
891 : : */
892 [ - + + - ]: 3429181 : if (WARN_ON_ONCE(opt_size > remaining))
893 : : return false;
894 : :
895 : 3429181 : *size += opt_size;
896 : 3429181 : remaining -= opt_size;
897 [ + + ]: 3429181 : if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
898 : 853 : *size += opt_size;
899 : 853 : remaining -= opt_size;
900 : 853 : ret = true;
901 [ + + ]: 3428328 : } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
902 : 108 : *size += opt_size;
903 : 108 : remaining -= opt_size;
904 : 108 : ret = true;
905 : : }
906 : :
907 [ + + ]: 3429181 : if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
908 : 28 : *size += opt_size;
909 : 28 : remaining -= opt_size;
910 : 28 : ret = true;
911 : : }
912 : :
913 : : return ret;
914 : : }
915 : :
916 : 2278 : bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
917 : : struct mptcp_out_options *opts)
918 : : {
919 : 2278 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
920 : :
921 [ + + ]: 2278 : if (subflow_req->mp_capable) {
922 : 1626 : opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
923 : 1626 : opts->sndr_key = subflow_req->local_key;
924 : 1626 : opts->csum_reqd = subflow_req->csum_reqd;
925 : 1626 : opts->allow_join_id0 = subflow_req->allow_join_id0;
926 : 1626 : *size = TCPOLEN_MPTCP_MPC_SYNACK;
927 [ - + - - ]: 1626 : pr_debug("subflow_req=%p, local_key=%llu\n",
928 : : subflow_req, subflow_req->local_key);
929 : 1626 : return true;
930 [ + + ]: 652 : } else if (subflow_req->mp_join) {
931 : 492 : opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
932 : 492 : opts->backup = subflow_req->request_bkup;
933 : 492 : opts->join_id = subflow_req->local_id;
934 : 492 : opts->thmac = subflow_req->thmac;
935 : 492 : opts->nonce = subflow_req->local_nonce;
936 [ - + - - ]: 492 : pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
937 : : subflow_req, opts->backup, opts->join_id,
938 : : opts->thmac, opts->nonce);
939 : 492 : *size = TCPOLEN_MPTCP_MPJ_SYNACK;
940 : 492 : return true;
941 : : }
942 : : return false;
943 : : }
944 : :
945 : 2017218 : static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
946 : : struct mptcp_subflow_context *subflow,
947 : : struct sk_buff *skb,
948 : : struct mptcp_options_received *mp_opt)
949 : : {
950 : : /* here we can process OoO, in-window pkts, only in-sequence 4th ack
951 : : * will make the subflow fully established
952 : : */
953 [ - + + + ]: 2017218 : if (likely(READ_ONCE(subflow->fully_established))) {
[ + + ]
954 : : /* on passive sockets, check for 3rd ack retransmission
955 : : * note that msk is always set by subflow_syn_recv_sock()
956 : : * for mp_join subflows
957 : : */
958 [ + + ]: 2015258 : if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
959 [ + + + + ]: 261318 : TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
960 [ + + ]: 57962 : subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
961 [ + - ]: 484 : !subflow->request_join)
962 : 484 : tcp_send_ack(ssk);
963 : 2015258 : goto check_notify;
964 : : }
965 : :
966 : : /* we must process OoO packets before the first subflow is fully
967 : : * established. OoO packets are instead a protocol violation
968 : : * for MP_JOIN subflows as the peer must not send any data
969 : : * before receiving the forth ack - cfr. RFC 8684 section 3.2.
970 : : */
971 [ + + ]: 1960 : if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
972 [ - + ]: 56 : if (subflow->mp_join)
973 : 0 : goto reset;
974 [ - + - - ]: 56 : if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
975 : 0 : goto set_fully_established;
976 : 56 : return subflow->mp_capable;
977 : : }
978 : :
979 [ + + ]: 1904 : if (subflow->remote_key_valid &&
980 [ + + - + : 1866 : (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
+ + ]
981 : 154 : ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) &&
982 [ + + + - ]: 154 : (!mp_opt->echo || subflow->mp_join)))) {
983 : : /* subflows are fully established as soon as we get any
984 : : * additional ack, including ADD_ADDR.
985 : : */
986 : 1852 : goto set_fully_established;
987 : : }
988 : :
989 : : /* If the first established packet does not contain MP_CAPABLE + data
990 : : * then fallback to TCP. Fallback scenarios requires a reset for
991 : : * MP_JOIN subflows.
992 : : */
993 [ + + ]: 52 : if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
994 [ + + ]: 14 : if (subflow->mp_join)
995 : 8 : goto reset;
996 : 6 : subflow->mp_capable = 0;
997 [ - + ]: 6 : if (!mptcp_try_fallback(ssk, MPTCP_MIB_MPCAPABLEDATAFALLBACK)) {
998 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_FALLBACKFAILED);
999 : 0 : goto reset;
1000 : : }
1001 : : return false;
1002 : : }
1003 : :
1004 [ - + + - ]: 38 : if (unlikely(!READ_ONCE(msk->pm.server_side)))
[ + - ]
1005 : : /* DO-NOT-MERGE: use WARN i/o pr_warn: only for MPTCP export */
1006 : 0 : WARN_ONCE(1, "bogus mpc option on established client sk");
1007 : :
1008 : 38 : set_fully_established:
1009 [ - + ]: 1890 : if (mp_opt->deny_join_id0)
1010 : 0 : WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
1011 : :
1012 : 1890 : mptcp_data_lock((struct sock *)msk);
1013 : 1890 : __mptcp_subflow_fully_established(msk, subflow, mp_opt);
1014 : 1890 : mptcp_data_unlock((struct sock *)msk);
1015 : :
1016 : 2017148 : check_notify:
1017 : : /* if the subflow is not already linked into the conn_list, we can't
1018 : : * notify the PM: this subflow is still on the listener queue
1019 : : * and the PM possibly acquiring the subflow lock could race with
1020 : : * the listener close
1021 : : */
1022 [ + + + - ]: 2017148 : if (likely(subflow->pm_notified) || list_empty(&subflow->node))
1023 : : return true;
1024 : :
1025 : 2374 : subflow->pm_notified = 1;
1026 [ + + ]: 2374 : if (subflow->mp_join) {
1027 : 936 : clear_3rdack_retransmission(ssk);
1028 : 936 : mptcp_pm_subflow_established(msk);
1029 : : } else {
1030 : 1438 : mptcp_pm_fully_established(msk, ssk);
1031 : : }
1032 : : return true;
1033 : :
1034 : 8 : reset:
1035 : 8 : mptcp_subflow_reset(ssk);
1036 : 8 : return false;
1037 : : }
1038 : :
1039 : 346412 : u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1040 : : {
1041 : 346412 : u32 old_seq32, cur_seq32;
1042 : :
1043 : 346412 : old_seq32 = (u32)old_seq;
1044 : 346412 : cur_seq32 = (u32)cur_seq;
1045 : 346412 : cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
1046 [ - + - - ]: 346412 : if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
1047 : 0 : return cur_seq + (1LL << 32);
1048 : :
1049 : : /* reverse wrap could happen, too */
1050 [ + + - + ]: 346412 : if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
1051 : 0 : return cur_seq - (1LL << 32);
1052 : : return cur_seq;
1053 : : }
1054 : :
1055 : 0 : static void __mptcp_snd_una_update(struct mptcp_sock *msk, u64 new_snd_una)
1056 : : {
1057 : 599063 : msk->bytes_acked += new_snd_una - msk->snd_una;
1058 : 599063 : WRITE_ONCE(msk->snd_una, new_snd_una);
1059 : : }
1060 : :
1061 : 2013799 : static void rwin_update(struct mptcp_sock *msk, struct sock *ssk,
1062 : : struct sk_buff *skb)
1063 : : {
1064 [ - + ]: 2013799 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1065 [ - + ]: 2013799 : struct tcp_sock *tp = tcp_sk(ssk);
1066 : 2013799 : u64 mptcp_rcv_wnd;
1067 : :
1068 : : /* Avoid touching extra cachelines if TCP is going to accept this
1069 : : * skb without filling the TCP-level window even with a possibly
1070 : : * outdated mptcp-level rwin.
1071 : : */
1072 [ + - + + ]: 2013799 : if (!skb->len || skb->len < tcp_receive_window(tp))
1073 : : return;
1074 : :
1075 [ - + ]: 10539 : mptcp_rcv_wnd = atomic64_read(&msk->rcv_wnd_sent);
1076 [ - + ]: 10539 : if (!after64(mptcp_rcv_wnd, subflow->rcv_wnd_sent))
1077 : : return;
1078 : :
1079 : : /* Some other subflow grew the mptcp-level rwin since rcv_wup,
1080 : : * resync.
1081 : : */
1082 : 0 : tp->rcv_wnd += mptcp_rcv_wnd - subflow->rcv_wnd_sent;
1083 [ # # ]: 0 : tcp_update_max_rcv_wnd_seq(tp);
1084 : 0 : subflow->rcv_wnd_sent = mptcp_rcv_wnd;
1085 : : }
1086 : :
1087 : 2012766 : static void ack_update_msk(struct mptcp_sock *msk,
1088 : : struct sock *ssk,
1089 : : struct mptcp_options_received *mp_opt)
1090 : : {
1091 : 2012766 : u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1092 : 2012766 : struct sock *sk = (struct sock *)msk;
1093 : 2012766 : u64 old_snd_una;
1094 : :
1095 : 2012766 : mptcp_data_lock(sk);
1096 : :
1097 : : /* avoid ack expansion on update conflict, to reduce the risk of
1098 : : * wrongly expanding to a future ack sequence number, which is way
1099 : : * more dangerous than missing an ack
1100 : : */
1101 : 2012766 : old_snd_una = msk->snd_una;
1102 [ + + ]: 2012766 : new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1103 : :
1104 : : /* ACK for data not even sent yet? Ignore.*/
1105 [ + + ]: 2012766 : if (unlikely(after64(new_snd_una, snd_nxt)))
1106 : 88 : new_snd_una = old_snd_una;
1107 : :
1108 [ - + ]: 2012766 : new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1109 : :
1110 [ + + ]: 2012766 : if (after64(new_wnd_end, msk->wnd_end))
1111 : 579431 : WRITE_ONCE(msk->wnd_end, new_wnd_end);
1112 : :
1113 : : /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1114 [ + + ]: 2012766 : if (after64(msk->wnd_end, snd_nxt))
1115 : 1875437 : __mptcp_check_push(sk, ssk);
1116 : :
1117 [ + + ]: 2012766 : if (after64(new_snd_una, old_snd_una)) {
1118 : 579171 : __mptcp_snd_una_update(msk, new_snd_una);
1119 : 579171 : __mptcp_data_acked(sk);
1120 : : }
1121 : 2012766 : msk->last_ack_recv = tcp_jiffies32;
1122 : 2012766 : mptcp_data_unlock(sk);
1123 : :
1124 : 2012766 : trace_ack_update_msk(mp_opt->data_ack,
1125 : : old_snd_una, new_snd_una,
1126 : 2012766 : new_wnd_end, READ_ONCE(msk->wnd_end));
1127 : 2012766 : }
1128 : :
1129 : 13477 : bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1130 : : {
1131 : : /* Skip if DATA_FIN was already received.
1132 : : * If updating simultaneously with the recvmsg loop, values
1133 : : * should match. If they mismatch, the peer is misbehaving and
1134 : : * we will prefer the most recent information.
1135 : : */
1136 [ - + + + ]: 13477 : if (READ_ONCE(msk->rcv_data_fin))
[ + + ]
1137 : : return false;
1138 : :
1139 [ + + ]: 3209 : WRITE_ONCE(msk->rcv_data_fin_seq,
1140 : : mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1141 : 3209 : WRITE_ONCE(msk->rcv_data_fin, 1);
1142 : :
1143 : 3209 : return true;
1144 : : }
1145 : :
1146 : 795 : static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1147 : : struct mptcp_options_received *mp_opt)
1148 : : {
1149 : 795 : u64 hmac = 0;
1150 : :
1151 [ + + ]: 795 : if (mp_opt->echo)
1152 : : return true;
1153 : :
1154 : 403 : hmac = add_addr_generate_hmac(READ_ONCE(msk->remote_key),
1155 : 403 : READ_ONCE(msk->local_key),
1156 : : &mp_opt->addr);
1157 : :
1158 [ - + - - ]: 403 : pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1159 : : msk, hmac, mp_opt->ahmac);
1160 : :
1161 : 403 : return hmac == mp_opt->ahmac;
1162 : : }
1163 : :
1164 : : /* Return false in case of error (or subflow has been reset),
1165 : : * else return true.
1166 : : */
1167 : 2037150 : bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1168 : : {
1169 [ - + ]: 2037150 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1170 [ - + ]: 2037150 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1171 : 2037150 : struct mptcp_options_received mp_opt;
1172 : 2037150 : struct mptcp_ext *mpext;
1173 : :
1174 [ + + ]: 2037150 : if (__mptcp_check_fallback(msk)) {
1175 : : /* Keep it simple and unconditionally trigger send data cleanup and
1176 : : * pending queue spooling. We will need to acquire the data lock
1177 : : * for more accurate checks, and once the lock is acquired, such
1178 : : * helpers are cheap.
1179 : : */
1180 : 19932 : mptcp_data_lock(subflow->conn);
1181 [ + + ]: 19932 : if (sk_stream_memory_free(sk))
1182 : 19924 : __mptcp_check_push(subflow->conn, sk);
1183 : :
1184 : : /* on fallback we just need to ignore the msk-level snd_una, as
1185 : : * this is really plain TCP
1186 : : */
1187 : 19932 : __mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
1188 : :
1189 : 19932 : __mptcp_data_acked(subflow->conn);
1190 : 19932 : mptcp_data_unlock(subflow->conn);
1191 : 19932 : return true;
1192 : : }
1193 : :
1194 : 2017218 : mptcp_get_options(skb, &mp_opt);
1195 : :
1196 : : /* The subflow can be in close state only if check_fully_established()
1197 : : * just sent a reset. If so, tell the caller to ignore the current packet.
1198 : : */
1199 [ + + ]: 2017218 : if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1200 : 64 : return sk->sk_state != TCP_CLOSE;
1201 : :
1202 [ + + ]: 2017154 : if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1203 [ + + ]: 116595 : if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1204 [ + + ]: 250 : READ_ONCE(msk->local_key) == mp_opt.rcvr_key) {
1205 : 240 : WRITE_ONCE(msk->rcv_fastclose, true);
1206 : 240 : mptcp_schedule_work((struct sock *)msk);
1207 [ + - ]: 240 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
1208 : : }
1209 : :
1210 [ + + + - ]: 117390 : if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1211 : 795 : add_addr_hmac_valid(msk, &mp_opt)) {
1212 [ + + ]: 795 : if (!mp_opt.echo) {
1213 : 403 : mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1214 [ + - ]: 403 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1215 : : } else {
1216 : 392 : mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1217 : 392 : mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1218 [ + - ]: 392 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1219 : : }
1220 : :
1221 [ + + ]: 795 : if (mp_opt.addr.port)
1222 [ + - ]: 86 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1223 : : }
1224 : :
1225 [ + + ]: 116595 : if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1226 : 108 : mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1227 : :
1228 [ + + ]: 116595 : if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1229 : 40 : mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1230 [ + - ]: 40 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1231 : : }
1232 : :
1233 [ + + ]: 116595 : if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1234 : 6 : mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1235 [ + - ]: 6 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1236 : : }
1237 : :
1238 [ + + ]: 116595 : if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1239 : 239 : subflow->reset_seen = 1;
1240 : 239 : subflow->reset_reason = mp_opt.reset_reason;
1241 : 239 : subflow->reset_transient = mp_opt.reset_transient;
1242 [ + - ]: 239 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1243 : : }
1244 : :
1245 [ + + ]: 116595 : if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1246 : : return true;
1247 : : }
1248 : :
1249 : : /* we can't wait for recvmsg() to update the ack_seq, otherwise
1250 : : * monodirectional flows will stuck
1251 : : */
1252 [ + + ]: 2013799 : if (mp_opt.use_ack)
1253 : 2012766 : ack_update_msk(msk, sk, &mp_opt);
1254 : 2013799 : rwin_update(msk, sk, skb);
1255 : :
1256 : : /* Zero-data-length packets are dropped by the caller and not
1257 : : * propagated to the MPTCP layer, so the skb extension does not
1258 : : * need to be allocated or populated. DATA_FIN information, if
1259 : : * present, needs to be updated here before the skb is freed.
1260 : : */
1261 [ + + ]: 2013799 : if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1262 [ + + + - : 762107 : if (mp_opt.data_fin && mp_opt.data_len == 1 &&
+ + ]
1263 : 10534 : mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
1264 : 2985 : mptcp_schedule_work((struct sock *)msk);
1265 : :
1266 : 751573 : return true;
1267 : : }
1268 : :
1269 : 1262226 : mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1270 [ + - ]: 1262226 : if (!mpext)
1271 : : return false;
1272 : :
1273 : 1262226 : memset(mpext, 0, sizeof(*mpext));
1274 : :
1275 [ + + ]: 1262226 : if (likely(mp_opt.use_map)) {
1276 [ + + ]: 1259445 : if (mp_opt.mpc_map) {
1277 : : /* this is an MP_CAPABLE carrying MPTCP data
1278 : : * we know this map the first chunk of data
1279 : : */
1280 : 1033 : mptcp_crypto_key_sha(subflow->remote_key, NULL,
1281 : : &mpext->data_seq);
1282 : 1033 : mpext->data_seq++;
1283 : 1033 : mpext->subflow_seq = 1;
1284 : 1033 : mpext->dsn64 = 1;
1285 : 1033 : mpext->mpc_map = 1;
1286 : 1033 : mpext->data_fin = 0;
1287 : : } else {
1288 : 1258412 : mpext->data_seq = mp_opt.data_seq;
1289 : 1258412 : mpext->subflow_seq = mp_opt.subflow_seq;
1290 : 1258412 : mpext->dsn64 = mp_opt.dsn64;
1291 : 1258412 : mpext->data_fin = mp_opt.data_fin;
1292 : : }
1293 : 1259445 : mpext->data_len = mp_opt.data_len;
1294 : 1259445 : mpext->use_map = 1;
1295 : 1259445 : mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1296 : :
1297 [ + + ]: 1259445 : if (mpext->csum_reqd)
1298 : 110088 : mpext->csum = mp_opt.csum;
1299 : : }
1300 : :
1301 : : return true;
1302 : : }
1303 : :
1304 : 1572420 : static u64 mptcp_set_rwin(struct mptcp_sock *msk, struct tcp_sock *tp,
1305 : : struct tcphdr *th, u64 ack_seq)
1306 : : {
1307 : 1572420 : const struct sock *ssk = (const struct sock *)tp;
1308 : 1572420 : u64 rcv_wnd_old, rcv_wnd_new;
1309 : 1572420 : u32 new_win;
1310 : 1572420 : u64 win;
1311 : :
1312 : 1572420 : rcv_wnd_new = ack_seq + tp->rcv_wnd;
1313 : :
1314 [ + + ]: 1572420 : rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1315 [ + + ]: 1572420 : if (after64(rcv_wnd_new, rcv_wnd_old)) {
1316 : 70 : u64 rcv_wnd;
1317 : :
1318 : 624225 : for (;;) {
1319 : 624295 : rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1320 : :
1321 [ + + ]: 624225 : if (rcv_wnd == rcv_wnd_old)
1322 : : break;
1323 : :
1324 : 6 : rcv_wnd_old = rcv_wnd;
1325 [ + + ]: 6 : if (before64(rcv_wnd_new, rcv_wnd_old)) {
1326 [ # # ]: 2 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1327 : 2 : goto raise_win;
1328 : : }
1329 [ - - ]: 74 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1330 : : }
1331 : 624219 : goto update_wspace;
1332 : : }
1333 : :
1334 [ + + ]: 948199 : if (rcv_wnd_new != rcv_wnd_old) {
1335 : 326043 : raise_win:
1336 : : /* The msk-level rcv wnd is after the tcp level one,
1337 : : * sync the latter.
1338 : : */
1339 : 326045 : rcv_wnd_new = rcv_wnd_old;
1340 : 326045 : win = rcv_wnd_old - ack_seq;
1341 : 326045 : new_win = min_t(u64, win, U32_MAX);
1342 : 326045 : tp->rcv_wnd = new_win;
1343 [ + + ]: 326045 : tcp_update_max_rcv_wnd_seq(tp);
1344 : :
1345 : : /* Make sure we do not exceed the maximum possible
1346 : : * scaled window.
1347 : : */
1348 [ - + ]: 326045 : if (unlikely(th->syn))
1349 : 0 : new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1350 [ - + ]: 326045 : if (!tp->rx_opt.rcv_wscale &&
[ - + - - ]
1351 [ # # ]: 0 : READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1352 : 0 : new_win = min(new_win, MAX_TCP_WINDOW);
1353 : : else
1354 : 326045 : new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1355 : :
1356 : : /* RFC1323 scaling applied */
1357 : 326045 : new_win >>= tp->rx_opt.rcv_wscale;
1358 : 326045 : th->window = htons(new_win);
1359 [ + - ]: 326045 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1360 : : }
1361 : :
1362 : 622156 : update_wspace:
1363 : 1572420 : WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
1364 : 1572420 : return rcv_wnd_new;
1365 : : }
1366 : :
1367 : 23420 : static void mptcp_track_rwin(struct tcp_sock *tp)
1368 : : {
1369 : 23420 : const struct sock *ssk = (const struct sock *)tp;
1370 : 23420 : struct mptcp_subflow_context *subflow;
1371 : 23420 : struct mptcp_sock *msk;
1372 : :
1373 [ + - ]: 23420 : if (!ssk)
1374 : : return;
1375 : :
1376 [ - + ]: 23420 : subflow = mptcp_subflow_ctx(ssk);
1377 [ - + ]: 23420 : msk = mptcp_sk(subflow->conn);
1378 : 23420 : WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
1379 : : }
1380 : :
1381 : 118228 : __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1382 : : {
1383 : 118228 : struct csum_pseudo_header header;
1384 : 118228 : __wsum csum;
1385 : :
1386 : : /* cfr RFC 8684 3.3.1.:
1387 : : * the data sequence number used in the pseudo-header is
1388 : : * always the 64-bit value, irrespective of what length is used in the
1389 : : * DSS option itself.
1390 : : */
1391 : 118228 : header.data_seq = cpu_to_be64(data_seq);
1392 : 118228 : header.subflow_seq = htonl(subflow_seq);
1393 : 118228 : header.data_len = htons(data_len);
1394 : 118228 : header.csum = 0;
1395 : :
1396 : 118228 : csum = csum_partial(&header, sizeof(header), sum);
1397 : 118228 : return csum_fold(csum);
1398 : : }
1399 : :
1400 : 80325 : static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1401 : : {
1402 : 160650 : return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1403 : 80325 : ~csum_unfold(mpext->csum));
1404 : : }
1405 : :
1406 : 0 : static void put_len_csum(u16 len, __sum16 csum, void *data)
1407 : : {
1408 : 80414 : __sum16 *sumptr = data + 2;
1409 : 80414 : __be16 *ptr = data;
1410 : :
1411 : 80414 : put_unaligned_be16(len, ptr);
1412 : :
1413 : 80414 : put_unaligned(csum, sumptr);
1414 : 80414 : }
1415 : :
1416 : 1604400 : void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1417 : : struct mptcp_out_options *opts)
1418 : : {
1419 : 1604400 : const struct sock *ssk = (const struct sock *)tp;
1420 : 1604400 : struct mptcp_subflow_context *subflow;
1421 : :
1422 : : /* Which options can be used together?
1423 : : *
1424 : : * X: mutually exclusive
1425 : : * O: often used together
1426 : : * C: can be used together in some cases
1427 : : * P: could be used together but we prefer not to (optimisations)
1428 : : *
1429 : : * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
1430 : : * ------|------|------|------|------|------|------|------|------|
1431 : : * MPC |------|------|------|------|------|------|------|------|
1432 : : * MPJ | X |------|------|------|------|------|------|------|
1433 : : * DSS | X | X |------|------|------|------|------|------|
1434 : : * ADD | X | X | P |------|------|------|------|------|
1435 : : * RM | C | C | C | P |------|------|------|------|
1436 : : * PRIO | X | C | C | C | C |------|------|------|
1437 : : * FAIL | X | X | C | X | X | X |------|------|
1438 : : * FC | X | X | X | X | X | X | X |------|
1439 : : * RST | X | X | X | X | X | X | O | O |
1440 : : * ------|------|------|------|------|------|------|------|------|
1441 : : *
1442 : : * The same applies in mptcp_established_options() function.
1443 : : */
1444 [ + + ]: 1604400 : if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1445 : 1572420 : struct mptcp_ext *mpext = &opts->ext_copy;
1446 : 1572420 : u8 len = TCPOLEN_MPTCP_DSS_BASE;
1447 : 1572420 : u8 flags = 0;
1448 : :
1449 [ + - ]: 1572420 : if (mpext->use_ack) {
1450 : 1572420 : flags = MPTCP_DSS_HAS_ACK;
1451 [ + + ]: 1572420 : if (mpext->ack64) {
1452 : : len += TCPOLEN_MPTCP_DSS_ACK64;
1453 : : flags |= MPTCP_DSS_ACK64;
1454 : : } else {
1455 : 110561 : len += TCPOLEN_MPTCP_DSS_ACK32;
1456 : : }
1457 : : }
1458 : :
1459 [ + + ]: 1572420 : if (mpext->use_map) {
1460 : 803454 : len += TCPOLEN_MPTCP_DSS_MAP64;
1461 : :
1462 : : /* Use only 64-bit mapping flags for now, add
1463 : : * support for optional 32-bit mappings later.
1464 : : */
1465 : 803454 : flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1466 [ + + ]: 803454 : if (mpext->data_fin)
1467 : 14504 : flags |= MPTCP_DSS_DATA_FIN;
1468 : :
1469 [ + + ]: 803454 : if (opts->csum_reqd)
1470 : 80327 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1471 : : }
1472 : :
1473 [ + - ]: 1572420 : *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1474 : :
1475 [ + - ]: 1572420 : if (mpext->use_ack) {
1476 : 1572420 : struct mptcp_sock *msk;
1477 : 1572420 : u64 ack_seq;
1478 : :
1479 : : /* DSS option is set only by mptcp_established_option,
1480 : : * the caller is __tcp_transmit_skb() and ssk is always
1481 : : * not NULL.
1482 : : */
1483 [ - + ]: 1572420 : subflow = mptcp_subflow_ctx(ssk);
1484 [ - + ]: 1572420 : msk = mptcp_sk(subflow->conn);
1485 : 1572420 : ack_seq = READ_ONCE(msk->ack_seq);
1486 [ + + ]: 1572420 : if (mpext->ack64) {
1487 : 1461859 : put_unaligned_be64(ack_seq, ptr);
1488 : 1461859 : ptr += 2;
1489 : : } else {
1490 : 110561 : put_unaligned_be32(ack_seq, ptr);
1491 : 110561 : ptr += 1;
1492 : : }
1493 : 1572420 : subflow->rcv_wnd_sent = mptcp_set_rwin(msk, tp, th,
1494 : : ack_seq);
1495 : : }
1496 : :
1497 [ + + ]: 1572420 : if (mpext->use_map) {
1498 [ + + ]: 803454 : put_unaligned_be64(mpext->data_seq, ptr);
1499 : 803454 : ptr += 2;
1500 [ + + ]: 803454 : put_unaligned_be32(mpext->subflow_seq, ptr);
1501 : 803454 : ptr += 1;
1502 [ + + ]: 803454 : if (opts->csum_reqd) {
1503 : : /* data_len == 0 is reserved for the infinite mapping,
1504 : : * the checksum will also be set to 0.
1505 : : */
1506 : 80327 : put_len_csum(mpext->data_len,
1507 [ + + ]: 80327 : (mpext->data_len ? mptcp_make_csum(mpext) : 0),
1508 : : ptr);
1509 : : } else {
1510 : 723127 : put_unaligned_be32(mpext->data_len << 16 |
1511 : 723127 : TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1512 : : }
1513 : 803454 : ptr += 1;
1514 : : }
1515 : :
1516 : : /* We might need to add MP_FAIL options in rare cases */
1517 [ + + ]: 1572420 : if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1518 : 4 : goto mp_fail;
1519 [ + + ]: 31980 : } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1520 : 5684 : u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1521 : :
1522 [ + + ]: 5684 : if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1523 : : len = TCPOLEN_MPTCP_MPC_SYN;
1524 [ + + ]: 3950 : } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1525 : : len = TCPOLEN_MPTCP_MPC_SYNACK;
1526 [ + + ]: 2324 : } else if (opts->data_len) {
1527 : 857 : len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1528 [ + + ]: 857 : if (opts->csum_reqd)
1529 : 87 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1530 : : } else {
1531 : : len = TCPOLEN_MPTCP_MPC_ACK;
1532 : : }
1533 : :
1534 [ + + ]: 5684 : if (opts->csum_reqd)
1535 : 461 : flag |= MPTCP_CAP_CHECKSUM_REQD;
1536 : :
1537 [ + + ]: 5684 : if (!opts->allow_join_id0)
1538 : 33 : flag |= MPTCP_CAP_DENY_JOIN_ID0;
1539 : :
1540 [ + + ]: 5684 : *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1541 : : MPTCP_SUPPORTED_VERSION,
1542 : : flag);
1543 : :
1544 : 5684 : if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1545 [ + + ]: 5684 : opts->suboptions))
1546 : 1734 : goto mp_capable_done;
1547 : :
1548 [ + + ]: 3950 : put_unaligned_be64(opts->sndr_key, ptr);
1549 : 3950 : ptr += 2;
1550 [ + + ]: 3950 : if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1551 : 1626 : goto mp_capable_done;
1552 : :
1553 [ + + ]: 2324 : put_unaligned_be64(opts->rcvr_key, ptr);
1554 : 2324 : ptr += 2;
1555 [ + + ]: 2324 : if (!opts->data_len)
1556 : 1467 : goto mp_capable_done;
1557 : :
1558 [ + + ]: 857 : if (opts->csum_reqd) {
1559 : 87 : put_len_csum(opts->data_len,
1560 : 87 : __mptcp_make_csum(opts->data_seq,
1561 : : opts->subflow_seq,
1562 : : opts->data_len,
1563 : 87 : ~csum_unfold(opts->csum)),
1564 : : ptr);
1565 : : } else {
1566 : 770 : put_unaligned_be32(opts->data_len << 16 |
1567 : 770 : TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1568 : : }
1569 : 857 : ptr += 1;
1570 : :
1571 : : /* MPC is additionally mutually exclusive with MP_PRIO */
1572 : 857 : goto mp_capable_done;
1573 [ + + ]: 26296 : } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
1574 [ + + ]: 1534 : if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1575 : 570 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1576 : : TCPOLEN_MPTCP_MPJ_SYN,
1577 : 570 : opts->backup, opts->join_id);
1578 : 570 : put_unaligned_be32(opts->token, ptr);
1579 : 570 : ptr += 1;
1580 : 570 : put_unaligned_be32(opts->nonce, ptr);
1581 : 570 : ptr += 1;
1582 [ + + ]: 964 : } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1583 : 492 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1584 : : TCPOLEN_MPTCP_MPJ_SYNACK,
1585 : 492 : opts->backup, opts->join_id);
1586 : 492 : put_unaligned_be64(opts->thmac, ptr);
1587 : 492 : ptr += 2;
1588 : 492 : put_unaligned_be32(opts->nonce, ptr);
1589 : 492 : ptr += 1;
1590 : : } else {
1591 : 472 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1592 : : TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1593 : 472 : memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1594 : 472 : ptr += 5;
1595 : : }
1596 [ + + ]: 24762 : } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1597 : 853 : u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1598 : 853 : u8 echo = MPTCP_ADDR_ECHO;
1599 : :
1600 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1601 [ + + ]: 853 : if (opts->addr.family == AF_INET6)
1602 : 196 : len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1603 : : #endif
1604 : :
1605 [ + + ]: 853 : if (opts->addr.port)
1606 : 86 : len += TCPOLEN_MPTCP_PORT_LEN;
1607 : :
1608 [ + + ]: 853 : if (opts->ahmac) {
1609 : 459 : len += sizeof(opts->ahmac);
1610 : 459 : echo = 0;
1611 : : }
1612 : :
1613 : 853 : *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1614 [ + + ]: 853 : len, echo, opts->addr.id);
1615 [ + + ]: 853 : if (opts->addr.family == AF_INET) {
1616 : 657 : memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1617 : 657 : ptr += 1;
1618 : : }
1619 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1620 [ + - ]: 196 : else if (opts->addr.family == AF_INET6) {
1621 : 196 : memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1622 : 196 : ptr += 4;
1623 : : }
1624 : : #endif
1625 : :
1626 [ + + ]: 853 : if (!opts->addr.port) {
1627 [ + + ]: 767 : if (opts->ahmac) {
1628 : 413 : put_unaligned_be64(opts->ahmac, ptr);
1629 : 413 : ptr += 2;
1630 : : }
1631 : : } else {
1632 : 86 : u16 port = ntohs(opts->addr.port);
1633 : :
1634 [ + + ]: 86 : if (opts->ahmac) {
1635 : 46 : u8 *bptr = (u8 *)ptr;
1636 : :
1637 : 46 : put_unaligned_be16(port, bptr);
1638 : 46 : bptr += 2;
1639 : 46 : put_unaligned_be64(opts->ahmac, bptr);
1640 : 46 : bptr += 8;
1641 : 46 : put_unaligned_be16(TCPOPT_NOP << 8 |
1642 : : TCPOPT_NOP, bptr);
1643 : :
1644 : 46 : ptr += 3;
1645 : : } else {
1646 : 40 : put_unaligned_be32(port << 16 |
1647 : 40 : TCPOPT_NOP << 8 |
1648 : : TCPOPT_NOP, ptr);
1649 : 40 : ptr += 1;
1650 : : }
1651 : : }
1652 [ + + ]: 23909 : } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1653 : : /* FASTCLOSE is mutually exclusive with others except RST */
1654 : 430 : *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1655 : : TCPOLEN_MPTCP_FASTCLOSE,
1656 : : 0, 0);
1657 [ + - ]: 430 : put_unaligned_be64(opts->rcvr_key, ptr);
1658 : 430 : ptr += 2;
1659 : :
1660 [ + - ]: 430 : if (OPTION_MPTCP_RST & opts->suboptions)
1661 : 430 : goto mp_rst;
1662 : : return;
1663 [ + + ]: 23479 : } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1664 : 2 : mp_fail:
1665 : : /* MP_FAIL is mutually exclusive with others except RST */
1666 [ + + ]: 6 : subflow = mptcp_subflow_ctx(ssk);
1667 : 6 : subflow->send_mp_fail = 0;
1668 : :
1669 : 6 : *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1670 : : TCPOLEN_MPTCP_FAIL,
1671 : : 0, 0);
1672 [ + + ]: 6 : put_unaligned_be64(opts->fail_seq, ptr);
1673 : 6 : ptr += 2;
1674 : :
1675 [ + + ]: 6 : if (OPTION_MPTCP_RST & opts->suboptions)
1676 : 2 : goto mp_rst;
1677 : : return;
1678 [ + + ]: 23477 : } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1679 : 57 : mp_rst:
1680 : 489 : *ptr++ = mptcp_option(MPTCPOPT_RST,
1681 : : TCPOLEN_MPTCP_RST,
1682 : 489 : opts->reset_transient,
1683 : 489 : opts->reset_reason);
1684 : 489 : return;
1685 [ + - ]: 23420 : } else if (unlikely(!opts->suboptions)) {
1686 : : /* Fallback to TCP */
1687 : 23420 : mptcp_track_rwin(tp);
1688 : 23420 : return;
1689 : : }
1690 : :
1691 [ + + ]: 1574803 : if (OPTION_MPTCP_PRIO & opts->suboptions) {
1692 [ + - ]: 28 : subflow = mptcp_subflow_ctx(ssk);
1693 : 28 : subflow->send_mp_prio = 0;
1694 : :
1695 : 28 : *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1696 : : TCPOLEN_MPTCP_PRIO,
1697 [ + - ]: 28 : opts->backup, TCPOPT_NOP);
1698 : :
1699 [ + - ]: 28 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
1700 : : }
1701 : :
1702 : 1574775 : mp_capable_done:
1703 [ + + ]: 1580487 : if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1704 : 108 : u8 i = 1;
1705 : :
1706 : 108 : *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1707 : 108 : TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1708 : 108 : 0, opts->rm_list.ids[0]);
1709 : :
1710 [ + + ]: 112 : while (i < opts->rm_list.nr) {
1711 : 4 : u8 id1, id2, id3, id4;
1712 : :
1713 : 4 : id1 = opts->rm_list.ids[i];
1714 [ + - ]: 4 : id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1715 [ - + ]: 4 : id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1716 [ - + ]: 4 : id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1717 : 4 : put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1718 : 4 : ptr += 1;
1719 : 4 : i += 4;
1720 : : }
1721 : : }
1722 : : }
1723 : :
1724 : 16 : __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1725 : : {
1726 [ + - ]: 16 : const struct mptcp_ext *ext = mptcp_get_ext(skb);
1727 : 16 : u8 flags, reason;
1728 : :
1729 [ + - ]: 16 : if (ext) {
1730 : 16 : flags = ext->reset_transient;
1731 : 16 : reason = ext->reset_reason;
1732 : :
1733 : 16 : return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1734 : : flags, reason);
1735 : : }
1736 : :
1737 : : return htonl(0u);
1738 : : }
1739 : : EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
|