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 : 1794614 : 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 : 1794614 : u8 subtype = *ptr >> 4;
28 : 1794614 : int expected_opsize;
29 : 1794614 : u16 subopt;
30 : 1794614 : u8 version;
31 : 1794614 : u8 flags;
32 : 1794614 : u8 i;
33 : :
34 [ + + + + : 1794614 : switch (subtype) {
+ + + + +
- ]
35 : 7410 : case MPTCPOPT_MP_CAPABLE:
36 : : /* strict size checking */
37 [ + + ]: 7410 : if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
38 [ + + ]: 4232 : if (skb->len > tcp_hdr(skb)->doff << 2)
39 : : expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
40 : : else
41 : 3196 : 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 [ + + ]: 7410 : if (opsize != expected_opsize &&
64 : 101 : (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
65 [ + + ]: 101 : opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
66 : : break;
67 : :
68 : : /* try to be gentle vs future versions on the initial syn */
69 : 7398 : version = *ptr++ & MPTCP_VERSION_MASK;
70 [ + + ]: 7398 : if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
71 [ + + ]: 5710 : if (version != MPTCP_SUPPORTED_VERSION)
72 : : break;
73 [ + - ]: 1688 : } else if (version < MPTCP_SUPPORTED_VERSION) {
74 : : break;
75 : : }
76 : :
77 : 7392 : flags = *ptr++;
78 [ + + + + ]: 7392 : 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 [ + + ]: 7356 : if (flags & MPTCP_CAP_CHECKSUM_REQD)
89 : 565 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
90 : :
91 : 7356 : mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
92 : :
93 : 7356 : mp_opt->suboptions |= subopt;
94 [ + + ]: 7356 : if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
95 [ + + ]: 5680 : mp_opt->sndr_key = get_unaligned_be64(ptr);
96 : 5680 : ptr += 8;
97 : : }
98 [ + + ]: 5680 : if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
99 : 4214 : mp_opt->rcvr_key = get_unaligned_be64(ptr);
100 : 4214 : ptr += 8;
101 : : }
102 [ + + ]: 5890 : 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 : 1036 : mp_opt->suboptions |= OPTION_MPTCP_DSS;
109 : 1036 : mp_opt->use_map = 1;
110 : 1036 : mp_opt->mpc_map = 1;
111 [ + + ]: 1036 : mp_opt->data_len = get_unaligned_be16(ptr);
112 : 1036 : ptr += 2;
113 : : }
114 [ + + ]: 1036 : if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
115 : 89 : mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
116 : 89 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
117 : 89 : ptr += 2;
118 : : }
119 [ - + - - ]: 7356 : 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 [ - + - - ]: 1783739 : pr_debug("DSS\n");
157 : 1783739 : ptr++;
158 : :
159 : 1783739 : flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
160 : 1783739 : mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
161 : 1783739 : mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
162 : 1783739 : mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
163 : 1783739 : mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
164 : 1783739 : mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
165 : :
166 [ - + - - ]: 1783739 : 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 : 1783739 : expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
172 : :
173 [ + - ]: 1783739 : if (mp_opt->use_ack) {
174 [ + + ]: 1783739 : if (mp_opt->ack64)
175 : : expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
176 : : else
177 : 301895 : expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
178 : : }
179 : :
180 [ + + ]: 1783739 : if (mp_opt->use_map) {
181 [ + + ]: 1083446 : if (mp_opt->dsn64)
182 : 1083404 : 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 [ + + ]: 1783739 : if (opsize != expected_opsize &&
191 [ + - ]: 190224 : opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
192 : : break;
193 : :
194 : 1783739 : mp_opt->suboptions |= OPTION_MPTCP_DSS;
195 [ + - ]: 1783739 : if (mp_opt->use_ack) {
196 [ + + ]: 1783739 : if (mp_opt->ack64) {
197 : 1481844 : mp_opt->data_ack = get_unaligned_be64(ptr);
198 : 1481844 : ptr += 8;
199 : : } else {
200 : 301895 : mp_opt->data_ack = get_unaligned_be32(ptr);
201 : 301895 : ptr += 4;
202 : : }
203 : :
204 [ - + - - ]: 1783739 : pr_debug("data_ack=%llu\n", mp_opt->data_ack);
205 : : }
206 : :
207 [ + + ]: 1783739 : if (mp_opt->use_map) {
208 [ + + ]: 1083446 : if (mp_opt->dsn64) {
209 : 1083404 : mp_opt->data_seq = get_unaligned_be64(ptr);
210 : 1083404 : ptr += 8;
211 : : } else {
212 : 42 : mp_opt->data_seq = get_unaligned_be32(ptr);
213 : 42 : ptr += 4;
214 : : }
215 : :
216 [ + + ]: 1083446 : mp_opt->subflow_seq = get_unaligned_be32(ptr);
217 : 1083446 : ptr += 4;
218 : :
219 [ + + ]: 1083446 : mp_opt->data_len = get_unaligned_be16(ptr);
220 : 1083446 : ptr += 2;
221 : :
222 [ + + ]: 1083446 : if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
223 : 190224 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
224 : 190224 : mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
225 : 190224 : ptr += 2;
226 : : }
227 : :
228 [ - + - - ]: 1083446 : 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 : 794 : case MPTCPOPT_ADD_ADDR:
237 : 794 : mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
238 [ + + ]: 794 : if (!mp_opt->echo) {
239 : 402 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
240 [ + + ]: 402 : opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
241 : 310 : 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 : 794 : mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
263 : 794 : mp_opt->addr.id = *ptr++;
264 : 794 : mp_opt->addr.port = 0;
265 : 794 : mp_opt->ahmac = 0;
266 [ + + ]: 794 : if (mp_opt->addr.family == AF_INET) {
267 : 612 : memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
268 : 612 : ptr += 4;
269 : 612 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
270 [ + + ]: 612 : 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 [ + + ]: 794 : if (!mp_opt->echo) {
287 : 402 : mp_opt->ahmac = get_unaligned_be64(ptr);
288 : 402 : ptr += 8;
289 : : }
290 [ - + - - : 794 : 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 : 274 : case MPTCPOPT_MP_FASTCLOSE:
319 [ + - ]: 274 : if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
320 : : break;
321 : :
322 : 274 : ptr += 2;
323 [ - + ]: 274 : mp_opt->rcvr_key = get_unaligned_be64(ptr);
324 : 274 : ptr += 8;
325 : 274 : mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
326 [ - + - - ]: 274 : pr_debug("MP_FASTCLOSE: recv_key=%llu\n", mp_opt->rcvr_key);
327 : : break;
328 : :
329 : 281 : case MPTCPOPT_RST:
330 [ + - ]: 281 : if (opsize != TCPOLEN_MPTCP_RST)
331 : : break;
332 : :
333 [ + + ]: 281 : if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
334 : : break;
335 : :
336 : 269 : mp_opt->suboptions |= OPTION_MPTCP_RST;
337 : 269 : flags = *ptr++;
338 : 269 : mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
339 : 269 : mp_opt->reset_reason = *ptr;
340 [ - + - - ]: 269 : 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 : 1794614 : }
358 : :
359 : 1794772 : void mptcp_get_options(const struct sk_buff *skb,
360 : : struct mptcp_options_received *mp_opt)
361 : : {
362 : 1794772 : const struct tcphdr *th = tcp_hdr(skb);
363 : 1794772 : const unsigned char *ptr;
364 : 1794772 : int length;
365 : :
366 : : /* Ensure that casting the whole status to u32 is efficient and safe */
367 : 1794772 : BUILD_BUG_ON(sizeof_field(struct mptcp_options_received, status) != sizeof(u32));
368 : 1794772 : BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_options_received, status),
369 : : sizeof(u32)));
370 : 1794772 : *(u32 *)&mp_opt->status = 0;
371 : :
372 : 1794772 : length = (th->doff * 4) - sizeof(struct tcphdr);
373 : 1794772 : ptr = (const unsigned char *)(th + 1);
374 : :
375 [ + + ]: 10791542 : while (length > 0) {
376 : 8996770 : int opcode = *ptr++;
377 : 8996770 : int opsize;
378 : :
379 [ + + - ]: 8996770 : switch (opcode) {
380 : : case TCPOPT_EOL:
381 : : return;
382 : 5387686 : case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
383 : 5387686 : length--;
384 : 5387686 : continue;
385 : 3609084 : default:
386 [ + - ]: 3609084 : if (length < 2)
387 : : return;
388 : 3609084 : opsize = *ptr++;
389 [ + - ]: 3609084 : if (opsize < 2) /* "silly options" */
390 : : return;
391 [ + - ]: 3609084 : if (opsize > length)
392 : : return; /* don't parse partial options */
393 [ + + ]: 3609084 : if (opcode == TCPOPT_MPTCP)
394 : 1794614 : mptcp_parse_option(skb, ptr, opsize, mp_opt);
395 : 3609084 : ptr += opsize - 2;
396 : 3609084 : length -= opsize;
397 : : }
398 : : }
399 : : }
400 : :
401 : 2336 : bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
402 : : unsigned int *size, struct mptcp_out_options *opts)
403 : : {
404 [ + + ]: 2336 : 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 : 2336 : subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
410 [ + + ]: 2336 : if (subflow->request_mptcp) {
411 [ + + ]: 1738 : 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 : 1732 : opts->suboptions = OPTION_MPTCP_MPC_SYN;
422 : 1732 : opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
423 : 1732 : opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
424 : 1732 : *size = TCPOLEN_MPTCP_MPC_SYN;
425 : 1732 : return true;
426 [ + + ]: 598 : } else if (subflow->request_join) {
427 [ - + - - ]: 568 : pr_debug("remote_token=%u, nonce=%u\n", subflow->remote_token,
428 : : subflow->local_nonce);
429 : 568 : opts->suboptions = OPTION_MPTCP_MPJ_SYN;
430 : 568 : opts->join_id = subflow->local_id;
431 : 568 : opts->token = subflow->remote_token;
432 : 568 : opts->nonce = subflow->local_nonce;
433 : 568 : opts->backup = subflow->request_bkup;
434 : 568 : *size = TCPOLEN_MPTCP_MPJ_SYN;
435 : 568 : 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 : 3024091 : 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 [ - + ]: 3024091 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
455 [ - + ]: 3024091 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
456 : 3024091 : struct mptcp_ext *mpext;
457 : 3024091 : unsigned int data_len;
458 : 3024091 : 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 [ + + ]: 3024091 : if (!skb)
467 : : return false;
468 : :
469 : : /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
470 [ - + + + : 1418077 : if (READ_ONCE(subflow->fully_established) || snd_data_fin_enable ||
+ + ]
[ + + + + ]
471 [ + + ]: 4012 : subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
472 [ + + ]: 2809 : sk->sk_state != TCP_ESTABLISHED)
473 : : return false;
474 : :
475 [ + + ]: 2809 : if (subflow->mp_capable) {
476 [ + + ]: 2337 : mpext = mptcp_get_ext(skb);
477 [ + + ]: 1604 : 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 : 2337 : opts->data_len = data_len;
484 : 2337 : opts->suboptions = OPTION_MPTCP_MPC_ACK;
485 : 2337 : opts->sndr_key = subflow->local_key;
486 : 2337 : opts->rcvr_key = subflow->remote_key;
487 [ - + ]: 2337 : opts->csum_reqd = READ_ONCE(msk->csum_enabled);
488 : 2337 : 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 [ + + ]: 2337 : if (data_len > 0) {
496 : 871 : len = TCPOLEN_MPTCP_MPC_ACK_DATA;
497 [ + + ]: 871 : if (opts->csum_reqd) {
498 : : /* we need to propagate more info to csum the pseudo hdr */
499 : 77 : opts->data_seq = mpext->data_seq;
500 : 77 : opts->subflow_seq = mpext->subflow_seq;
501 : 77 : opts->csum = mpext->csum;
502 : 77 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
503 : : }
504 : 871 : *size = ALIGN(len, 4);
505 : : } else {
506 : 1466 : *size = TCPOLEN_MPTCP_MPC_ACK;
507 : : }
508 : :
509 [ - + - - ]: 2337 : 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 : 2337 : 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 : 47630 : 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 [ - + ]: 47630 : u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
541 : :
542 [ + + - + ]: 47630 : 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 : 24060 : ext->data_fin = 1;
547 : 24060 : ext->use_map = 1;
548 : 24060 : ext->dsn64 = 1;
549 : 24060 : ext->data_seq = data_fin_tx_seq;
550 : 24060 : ext->subflow_seq = 0;
551 : 24060 : ext->data_len = 1;
552 [ + + ]: 23570 : } 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 : 1838 : ext->data_fin = 1;
558 : 1838 : ext->data_len++;
559 : : }
560 : 47630 : }
561 : :
562 : 3021282 : 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 [ - + ]: 3021282 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
568 [ - + ]: 3021282 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
569 [ - + ]: 3021282 : struct tcp_sock *tp = tcp_sk(sk);
570 : 3021282 : unsigned int dss_size = 0;
571 : 3021282 : struct mptcp_ext *mpext;
572 : 3021282 : unsigned int ack_size;
573 : 3021282 : bool ret = false;
574 : :
575 : : /* Zero `use_ack` and `use_map` flags with one shot. */
576 : 3021282 : BUILD_BUG_ON(sizeof_field(struct mptcp_ext, flags) != sizeof(u16));
577 : 3021282 : BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_ext, flags),
578 : : sizeof(u16)));
579 : 3021282 : *(u16 *)&opts->ext_copy.flags = 0;
580 [ - + ]: 3021282 : opts->csum_reqd = READ_ONCE(msk->csum_enabled);
581 [ + + ]: 3021282 : mpext = skb ? mptcp_get_ext(skb) : NULL;
582 : :
583 [ + + - + : 1415268 : if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
+ + ]
584 : 2290132 : unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
585 : :
586 [ + + ]: 2290132 : if (mpext) {
587 [ + + ]: 660058 : if (opts->csum_reqd)
588 : 127598 : map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
589 : :
590 : 660058 : opts->ext_copy = *mpext;
591 : : }
592 : :
593 : 2290132 : dss_size = map_size;
594 [ + + ]: 2290132 : if (skb && snd_data_fin_enable)
595 : 47630 : mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
596 : 2290132 : opts->suboptions = OPTION_MPTCP_DSS;
597 : 2290132 : ret = true;
598 : : }
599 : :
600 : : /* passive sockets msk will set the 'can_ack' after accept(), even
601 : : * if the first subflow may have the already the remote key handy
602 : : */
603 [ - + + + ]: 3021282 : if (!READ_ONCE(msk->can_ack)) {
[ + + ]
604 : 30 : *size = ALIGN(dss_size, 4);
605 : 30 : return ret;
606 : : }
607 : :
608 [ - + + + ]: 3021252 : if (READ_ONCE(msk->use_64bit_ack)) {
[ + + ]
609 : 2585267 : ack_size = TCPOLEN_MPTCP_DSS_ACK64;
610 : 2585267 : opts->ext_copy.ack64 = 1;
611 : : } else {
612 : 435985 : ack_size = TCPOLEN_MPTCP_DSS_ACK32;
613 : 435985 : opts->ext_copy.ack64 = 0;
614 : : }
615 : 3021252 : opts->ext_copy.use_ack = 1;
616 : 3021252 : opts->suboptions = OPTION_MPTCP_DSS;
617 : :
618 : : /* Add kind/length/subtype/flag overhead if mapping is not populated */
619 [ + + ]: 3021252 : if (dss_size == 0)
620 : 731150 : ack_size += TCPOLEN_MPTCP_DSS_BASE;
621 : :
622 : : /* The caller is __tcp_transmit_skb(), and will compute the new rcv
623 : : * wnd soon: ensure that the window can shrink.
624 : : */
625 [ + + ]: 3021252 : if (skb)
626 : 1415268 : tp->rcv_wnd = tp->rcv_nxt - tp->rcv_wup;
627 : :
628 : 3021252 : dss_size += ack_size;
629 : :
630 : 3021252 : *size = ALIGN(dss_size, 4);
631 : 3021252 : return true;
632 : : }
633 : :
634 : 860 : static u64 add_addr_generate_hmac(u64 key1, u64 key2,
635 : : struct mptcp_addr_info *addr)
636 : : {
637 : 860 : u16 port = ntohs(addr->port);
638 : 860 : u8 hmac[SHA256_DIGEST_SIZE];
639 : 860 : u8 msg[19];
640 : 860 : int i = 0;
641 : :
642 : 860 : msg[i++] = addr->id;
643 [ + + ]: 860 : if (addr->family == AF_INET) {
644 : 664 : memcpy(&msg[i], &addr->addr.s_addr, 4);
645 : 664 : i += 4;
646 : : }
647 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
648 [ + - ]: 196 : else if (addr->family == AF_INET6) {
649 : 196 : memcpy(&msg[i], &addr->addr6.s6_addr, 16);
650 : 196 : i += 16;
651 : : }
652 : : #endif
653 : 860 : msg[i++] = port >> 8;
654 : 860 : msg[i++] = port & 0xFF;
655 : :
656 : 860 : mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
657 : :
658 : 860 : return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
659 : : }
660 : :
661 : 3024087 : static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
662 : : unsigned int *size,
663 : : unsigned int remaining,
664 : : struct mptcp_out_options *opts)
665 : : {
666 [ - + ]: 3024087 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
667 [ - + ]: 3024087 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
668 : 3024087 : unsigned int opt_size = *size;
669 : 3024087 : struct mptcp_addr_info addr;
670 : 3024087 : bool echo;
671 : 3024087 : int len;
672 : :
673 : : /* add addr will strip the existing options, be sure to avoid breaking
674 : : * MPC/MPJ handshakes
675 : : */
676 [ + + ]: 3024087 : if (!mptcp_pm_should_add_signal(msk) ||
677 [ + - + - ]: 860 : (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
678 [ + - + + ]: 1720 : !skb || !skb_is_tcp_pure_ack(skb) ||
679 : 860 : !mptcp_pm_add_addr_signal(msk, opt_size, remaining, &addr, &echo))
680 : 3023235 : return false;
681 : :
682 : 852 : remaining += opt_size;
683 : :
684 [ - + + + ]: 852 : len = mptcp_add_addr_len(addr.family, echo, !!addr.port);
[ + + ]
685 [ - + ]: 852 : if (remaining < len)
686 : : return false;
687 : :
688 : 852 : *size = len;
689 [ - + - - ]: 852 : pr_debug("drop other suboptions\n");
690 : 852 : opts->suboptions = 0;
691 : 852 : *size -= opt_size;
692 : 852 : opts->addr = addr;
693 : 852 : opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
694 [ - + + + ]: 852 : if (!echo) {
[ + + ]
695 [ + - ]: 458 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
696 : 458 : opts->ahmac = add_addr_generate_hmac(READ_ONCE(msk->local_key),
697 : 458 : READ_ONCE(msk->remote_key),
698 : : &opts->addr);
699 : : } else {
700 [ + - ]: 394 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
701 : 394 : opts->ahmac = 0;
702 : : }
703 [ - + - - : 852 : pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d\n",
- - ]
[ - + - - ]
704 : : opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
705 : :
706 : : return true;
707 : : }
708 : :
709 : 3023235 : static bool mptcp_established_options_rm_addr(struct sock *sk,
710 : : unsigned int *size,
711 : : unsigned int remaining,
712 : : struct mptcp_out_options *opts)
713 : : {
714 [ - + ]: 3023235 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
715 [ - + ]: 3023235 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
716 : 3023235 : struct mptcp_rm_list rm_list;
717 : 3023235 : int i, len;
718 : :
719 [ + + - + ]: 3023343 : if (!mptcp_pm_should_rm_signal(msk) ||
720 : 108 : !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
721 : 3023127 : return false;
722 : :
723 [ - + ]: 284 : len = mptcp_rm_addr_len(&rm_list);
724 : 108 : if (len < 0)
725 : : return false;
726 [ - + ]: 108 : if (remaining < len)
727 : : return false;
728 : :
729 : 108 : *size = len;
730 : 108 : opts->suboptions |= OPTION_MPTCP_RM_ADDR;
731 : 108 : opts->rm_list = rm_list;
732 : :
733 [ + + ]: 224 : for (i = 0; i < opts->rm_list.nr; i++)
734 [ - + - - ]: 116 : pr_debug("rm_list_ids[%d]=%d\n", i, opts->rm_list.ids[i]);
735 : 108 : MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
736 : 108 : return true;
737 : : }
738 : :
739 : 3024087 : static bool mptcp_established_options_mp_prio(struct sock *sk,
740 : : unsigned int *size,
741 : : unsigned int remaining,
742 : : struct mptcp_out_options *opts)
743 : : {
744 [ + + ]: 3024087 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
745 : :
746 : : /* can't send MP_PRIO with MPC, as they share the same option space:
747 : : * 'backup'. Also it makes no sense at all
748 : : */
749 [ + + + - ]: 3024087 : if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
750 : : return false;
751 : :
752 : : /* account for the trailing 'nop' option */
753 [ + - ]: 28 : if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
754 : : return false;
755 : :
756 : 28 : *size = TCPOLEN_MPTCP_PRIO_ALIGN;
757 : 28 : opts->suboptions |= OPTION_MPTCP_PRIO;
758 : 28 : opts->backup = subflow->request_bkup;
759 : :
760 [ - + - - ]: 28 : pr_debug("prio=%d\n", opts->backup);
761 : :
762 : : return true;
763 : : }
764 : :
765 : 509 : static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
766 : : unsigned int *size,
767 : : unsigned int remaining,
768 : : struct mptcp_out_options *opts)
769 : : {
770 [ + - ]: 509 : const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
771 : :
772 [ + - ]: 509 : if (remaining < TCPOLEN_MPTCP_RST)
773 : : return false;
774 : :
775 : 509 : *size = TCPOLEN_MPTCP_RST;
776 : 509 : opts->suboptions |= OPTION_MPTCP_RST;
777 : 509 : opts->reset_transient = subflow->reset_transient;
778 : 509 : opts->reset_reason = subflow->reset_reason;
779 [ + - ]: 509 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
780 : :
781 : : return true;
782 : : }
783 : :
784 : 509 : static bool mptcp_established_options_fastclose(struct sock *sk,
785 : : unsigned int *size,
786 : : unsigned int remaining,
787 : : struct mptcp_out_options *opts)
788 : : {
789 [ - + ]: 509 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
790 [ - + ]: 509 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
791 : :
792 [ + + ]: 509 : if (likely(!subflow->send_fastclose))
793 : : return false;
794 : :
795 [ + - ]: 454 : if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
796 : : return false;
797 : :
798 : 454 : *size = TCPOLEN_MPTCP_FASTCLOSE;
799 : 454 : opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
800 : 454 : opts->rcvr_key = READ_ONCE(msk->remote_key);
801 : :
802 [ - + - - ]: 454 : pr_debug("FASTCLOSE key=%llu\n", opts->rcvr_key);
803 [ + - ]: 454 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
804 : : return true;
805 : : }
806 : :
807 : 3021337 : static bool mptcp_established_options_mp_fail(struct sock *sk,
808 : : unsigned int *size,
809 : : unsigned int remaining,
810 : : struct mptcp_out_options *opts)
811 : : {
812 [ + + ]: 3021337 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
813 : :
814 [ + + ]: 3021337 : if (likely(!subflow->send_mp_fail))
815 : : return false;
816 : :
817 [ + - ]: 6 : if (remaining < TCPOLEN_MPTCP_FAIL)
818 : : return false;
819 : :
820 : 6 : *size = TCPOLEN_MPTCP_FAIL;
821 : 6 : opts->suboptions |= OPTION_MPTCP_FAIL;
822 : 6 : opts->fail_seq = subflow->map_seq;
823 : :
824 [ - + - - ]: 6 : pr_debug("MP_FAIL fail_seq=%llu\n", opts->fail_seq);
825 [ + - ]: 6 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
826 : :
827 : : return true;
828 : : }
829 : :
830 : 3108414 : bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
831 : : unsigned int *size, unsigned int remaining,
832 : : struct mptcp_out_options *opts)
833 : : {
834 [ - + ]: 3108414 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
835 [ - + ]: 3108414 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
836 : 3108414 : unsigned int opt_size = 0;
837 : 3108414 : bool snd_data_fin;
838 : 3108414 : bool ret = false;
839 : :
840 : 3108414 : opts->suboptions = 0;
841 : :
842 : : /* Force later mptcp_write_options(), but do not use any actual
843 : : * option space.
844 : : */
845 [ + + + + ]: 3108414 : if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
846 : : return true;
847 : :
848 [ + + + + ]: 3024600 : if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
849 [ + + + + ]: 564 : if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
850 : 55 : mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
851 : 456 : *size += opt_size;
852 : 456 : remaining -= opt_size;
853 : : }
854 : : /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
855 [ + - ]: 509 : if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
856 : 509 : *size += opt_size;
857 : 509 : remaining -= opt_size;
858 : : }
859 : 509 : return true;
860 : : }
861 : :
862 [ + + ]: 3024091 : snd_data_fin = mptcp_data_fin_enabled(msk);
863 [ + + ]: 3024091 : if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
864 : : ret = true;
865 [ + - ]: 3021282 : else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
866 : 3021282 : unsigned int mp_fail_size;
867 : :
868 : 3021282 : ret = true;
869 [ + + ]: 3021282 : if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
870 : : remaining - opt_size, opts)) {
871 : 4 : *size += opt_size + mp_fail_size;
872 : 4 : remaining -= opt_size - mp_fail_size;
873 : 4 : return true;
874 : : }
875 : : }
876 : :
877 : : /* we reserved enough space for the above options, and exceeding the
878 : : * TCP option space would be fatal
879 : : */
880 [ - + + - ]: 3024087 : if (WARN_ON_ONCE(opt_size > remaining))
881 : : return false;
882 : :
883 : 3024087 : *size += opt_size;
884 : 3024087 : remaining -= opt_size;
885 [ + + ]: 3024087 : if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
886 : 852 : *size += opt_size;
887 : 852 : remaining -= opt_size;
888 : 852 : ret = true;
889 [ + + ]: 3023235 : } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
890 : 108 : *size += opt_size;
891 : 108 : remaining -= opt_size;
892 : 108 : ret = true;
893 : : }
894 : :
895 [ + + ]: 3024087 : if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
896 : 28 : *size += opt_size;
897 : 28 : remaining -= opt_size;
898 : 28 : ret = true;
899 : : }
900 : :
901 : : return ret;
902 : : }
903 : :
904 : 2276 : bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
905 : : struct mptcp_out_options *opts)
906 : : {
907 : 2276 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
908 : :
909 [ + + ]: 2276 : if (subflow_req->mp_capable) {
910 : 1624 : opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
911 : 1624 : opts->sndr_key = subflow_req->local_key;
912 : 1624 : opts->csum_reqd = subflow_req->csum_reqd;
913 : 1624 : opts->allow_join_id0 = subflow_req->allow_join_id0;
914 : 1624 : *size = TCPOLEN_MPTCP_MPC_SYNACK;
915 [ - + - - ]: 1624 : pr_debug("subflow_req=%p, local_key=%llu\n",
916 : : subflow_req, subflow_req->local_key);
917 : 1624 : return true;
918 [ + + ]: 652 : } else if (subflow_req->mp_join) {
919 : 492 : opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
920 : 492 : opts->backup = subflow_req->request_bkup;
921 : 492 : opts->join_id = subflow_req->local_id;
922 : 492 : opts->thmac = subflow_req->thmac;
923 : 492 : opts->nonce = subflow_req->local_nonce;
924 [ - + - - ]: 492 : pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
925 : : subflow_req, opts->backup, opts->join_id,
926 : : opts->thmac, opts->nonce);
927 : 492 : *size = TCPOLEN_MPTCP_MPJ_SYNACK;
928 : 492 : return true;
929 : : }
930 : : return false;
931 : : }
932 : :
933 : 1788204 : static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
934 : : struct mptcp_subflow_context *subflow,
935 : : struct sk_buff *skb,
936 : : struct mptcp_options_received *mp_opt)
937 : : {
938 : : /* here we can process OoO, in-window pkts, only in-sequence 4th ack
939 : : * will make the subflow fully established
940 : : */
941 [ - + + + ]: 1788204 : if (likely(READ_ONCE(subflow->fully_established))) {
[ + + ]
942 : : /* on passive sockets, check for 3rd ack retransmission
943 : : * note that msk is always set by subflow_syn_recv_sock()
944 : : * for mp_join subflows
945 : : */
946 [ + + ]: 1786245 : if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
947 [ + + + + ]: 258910 : TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
948 [ + + ]: 53601 : subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
949 [ + - ]: 484 : !subflow->request_join)
950 : 484 : tcp_send_ack(ssk);
951 : 1786245 : goto check_notify;
952 : : }
953 : :
954 : : /* we must process OoO packets before the first subflow is fully
955 : : * established. OoO packets are instead a protocol violation
956 : : * for MP_JOIN subflows as the peer must not send any data
957 : : * before receiving the forth ack - cfr. RFC 8684 section 3.2.
958 : : */
959 [ + + ]: 1959 : if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
960 [ - + ]: 55 : if (subflow->mp_join)
961 : 0 : goto reset;
962 [ - + - - ]: 55 : if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
963 : 0 : goto set_fully_established;
964 : 55 : return subflow->mp_capable;
965 : : }
966 : :
967 [ + + ]: 1904 : if (subflow->remote_key_valid &&
968 [ + + - + : 1866 : (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
+ + ]
969 : 154 : ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) &&
970 [ + + + - ]: 154 : (!mp_opt->echo || subflow->mp_join)))) {
971 : : /* subflows are fully established as soon as we get any
972 : : * additional ack, including ADD_ADDR.
973 : : */
974 : 1852 : goto set_fully_established;
975 : : }
976 : :
977 : : /* If the first established packet does not contain MP_CAPABLE + data
978 : : * then fallback to TCP. Fallback scenarios requires a reset for
979 : : * MP_JOIN subflows.
980 : : */
981 [ + + ]: 52 : if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
982 [ + + ]: 14 : if (subflow->mp_join)
983 : 8 : goto reset;
984 : 6 : subflow->mp_capable = 0;
985 [ - + ]: 6 : if (!mptcp_try_fallback(ssk, MPTCP_MIB_MPCAPABLEDATAFALLBACK)) {
986 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_FALLBACKFAILED);
987 : 0 : goto reset;
988 : : }
989 : : return false;
990 : : }
991 : :
992 [ - + + - ]: 38 : if (unlikely(!READ_ONCE(msk->pm.server_side)))
[ + - ]
993 : : /* DO-NOT-MERGE: use WARN i/o pr_warn: only for MPTCP export */
994 : 0 : WARN_ONCE(1, "bogus mpc option on established client sk");
995 : :
996 : 38 : set_fully_established:
997 [ - + ]: 1890 : if (mp_opt->deny_join_id0)
998 : 0 : WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
999 : :
1000 : 1890 : mptcp_data_lock((struct sock *)msk);
1001 : 1890 : __mptcp_subflow_fully_established(msk, subflow, mp_opt);
1002 : 1890 : mptcp_data_unlock((struct sock *)msk);
1003 : :
1004 : 1788135 : check_notify:
1005 : : /* if the subflow is not already linked into the conn_list, we can't
1006 : : * notify the PM: this subflow is still on the listener queue
1007 : : * and the PM possibly acquiring the subflow lock could race with
1008 : : * the listener close
1009 : : */
1010 [ + + + - ]: 1788135 : if (likely(subflow->pm_notified) || list_empty(&subflow->node))
1011 : : return true;
1012 : :
1013 : 2374 : subflow->pm_notified = 1;
1014 [ + + ]: 2374 : if (subflow->mp_join) {
1015 : 936 : clear_3rdack_retransmission(ssk);
1016 : 936 : mptcp_pm_subflow_established(msk);
1017 : : } else {
1018 : 1438 : mptcp_pm_fully_established(msk, ssk);
1019 : : }
1020 : : return true;
1021 : :
1022 : 8 : reset:
1023 : 8 : mptcp_subflow_reset(ssk);
1024 : 8 : return false;
1025 : : }
1026 : :
1027 : 313364 : u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1028 : : {
1029 : 313364 : u32 old_seq32, cur_seq32;
1030 : :
1031 : 313364 : old_seq32 = (u32)old_seq;
1032 : 313364 : cur_seq32 = (u32)cur_seq;
1033 : 313364 : cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
1034 [ - + - - ]: 313364 : if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
1035 : 0 : return cur_seq + (1LL << 32);
1036 : :
1037 : : /* reverse wrap could happen, too */
1038 [ + + - + ]: 313364 : if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
1039 : 0 : return cur_seq - (1LL << 32);
1040 : : return cur_seq;
1041 : : }
1042 : :
1043 : 0 : static void __mptcp_snd_una_update(struct mptcp_sock *msk, u64 new_snd_una)
1044 : : {
1045 : 562800 : msk->bytes_acked += new_snd_una - msk->snd_una;
1046 : 562800 : WRITE_ONCE(msk->snd_una, new_snd_una);
1047 : : }
1048 : :
1049 : 1784769 : static void rwin_update(struct mptcp_sock *msk, struct sock *ssk,
1050 : : struct sk_buff *skb)
1051 : : {
1052 [ - + ]: 1784769 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1053 [ - + ]: 1784769 : struct tcp_sock *tp = tcp_sk(ssk);
1054 : 1784769 : u64 mptcp_rcv_wnd;
1055 : :
1056 : : /* Avoid touching extra cachelines if TCP is going to accept this
1057 : : * skb without filling the TCP-level window even with a possibly
1058 : : * outdated mptcp-level rwin.
1059 : : */
1060 [ + - + + ]: 1784769 : if (!skb->len || skb->len < tcp_receive_window(tp))
1061 : : return;
1062 : :
1063 [ - + ]: 8860 : mptcp_rcv_wnd = atomic64_read(&msk->rcv_wnd_sent);
1064 [ - + ]: 8860 : if (!after64(mptcp_rcv_wnd, subflow->rcv_wnd_sent))
1065 : : return;
1066 : :
1067 : : /* Some other subflow grew the mptcp-level rwin since rcv_wup,
1068 : : * resync.
1069 : : */
1070 : 0 : tp->rcv_wnd += mptcp_rcv_wnd - subflow->rcv_wnd_sent;
1071 [ # # ]: 0 : tcp_update_max_rcv_wnd_seq(tp);
1072 : 0 : subflow->rcv_wnd_sent = mptcp_rcv_wnd;
1073 : : }
1074 : :
1075 : 1783739 : static void ack_update_msk(struct mptcp_sock *msk,
1076 : : struct sock *ssk,
1077 : : struct mptcp_options_received *mp_opt)
1078 : : {
1079 : 1783739 : u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1080 : 1783739 : struct sock *sk = (struct sock *)msk;
1081 : 1783739 : u64 old_snd_una;
1082 : :
1083 : 1783739 : mptcp_data_lock(sk);
1084 : :
1085 : : /* avoid ack expansion on update conflict, to reduce the risk of
1086 : : * wrongly expanding to a future ack sequence number, which is way
1087 : : * more dangerous than missing an ack
1088 : : */
1089 : 1783739 : old_snd_una = msk->snd_una;
1090 [ + + ]: 1783739 : new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
1091 : :
1092 : : /* ACK for data not even sent yet? Ignore.*/
1093 [ + + ]: 1783739 : if (unlikely(after64(new_snd_una, snd_nxt)))
1094 : 72 : new_snd_una = old_snd_una;
1095 : :
1096 [ - + ]: 1783739 : new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1097 : :
1098 [ + + ]: 1783739 : if (after64(new_wnd_end, msk->wnd_end))
1099 : 558801 : WRITE_ONCE(msk->wnd_end, new_wnd_end);
1100 : :
1101 : : /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1102 [ + + ]: 1783739 : if (after64(msk->wnd_end, snd_nxt))
1103 : 1671506 : __mptcp_check_push(sk, ssk);
1104 : :
1105 [ + + ]: 1783739 : if (after64(new_snd_una, old_snd_una)) {
1106 : 542834 : __mptcp_snd_una_update(msk, new_snd_una);
1107 : 542834 : __mptcp_data_acked(sk);
1108 : : }
1109 : 1783739 : msk->last_ack_recv = tcp_jiffies32;
1110 : 1783739 : mptcp_data_unlock(sk);
1111 : :
1112 : 1783739 : trace_ack_update_msk(mp_opt->data_ack,
1113 : : old_snd_una, new_snd_una,
1114 : 1783739 : new_wnd_end, READ_ONCE(msk->wnd_end));
1115 : 1783739 : }
1116 : :
1117 : 23805 : bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
1118 : : {
1119 : : /* Skip if DATA_FIN was already received.
1120 : : * If updating simultaneously with the recvmsg loop, values
1121 : : * should match. If they mismatch, the peer is misbehaving and
1122 : : * we will prefer the most recent information.
1123 : : */
1124 [ - + + + ]: 23805 : if (READ_ONCE(msk->rcv_data_fin))
[ + + ]
1125 : : return false;
1126 : :
1127 [ + + ]: 3216 : WRITE_ONCE(msk->rcv_data_fin_seq,
1128 : : mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1129 : 3216 : WRITE_ONCE(msk->rcv_data_fin, 1);
1130 : :
1131 : 3216 : return true;
1132 : : }
1133 : :
1134 : 794 : static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1135 : : struct mptcp_options_received *mp_opt)
1136 : : {
1137 : 794 : u64 hmac = 0;
1138 : :
1139 [ + + ]: 794 : if (mp_opt->echo)
1140 : : return true;
1141 : :
1142 : 402 : hmac = add_addr_generate_hmac(READ_ONCE(msk->remote_key),
1143 : 402 : READ_ONCE(msk->local_key),
1144 : : &mp_opt->addr);
1145 : :
1146 [ - + - - ]: 402 : pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1147 : : msk, hmac, mp_opt->ahmac);
1148 : :
1149 : 402 : return hmac == mp_opt->ahmac;
1150 : : }
1151 : :
1152 : : /* Return false in case of error (or subflow has been reset),
1153 : : * else return true.
1154 : : */
1155 : 1808205 : bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1156 : : {
1157 [ - + ]: 1808205 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1158 [ - + ]: 1808205 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1159 : 1808205 : struct mptcp_options_received mp_opt;
1160 : 1808205 : struct mptcp_ext *mpext;
1161 : :
1162 [ + + ]: 1808205 : if (__mptcp_check_fallback(msk)) {
1163 : : /* Keep it simple and unconditionally trigger send data cleanup and
1164 : : * pending queue spooling. We will need to acquire the data lock
1165 : : * for more accurate checks, and once the lock is acquired, such
1166 : : * helpers are cheap.
1167 : : */
1168 : 20001 : mptcp_data_lock(subflow->conn);
1169 [ + + ]: 20001 : if (sk_stream_memory_free(sk))
1170 : 19998 : __mptcp_check_push(subflow->conn, sk);
1171 : :
1172 : : /* on fallback we just need to ignore the msk-level snd_una, as
1173 : : * this is really plain TCP
1174 : : */
1175 : 20001 : __mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
1176 : :
1177 : 20001 : __mptcp_data_acked(subflow->conn);
1178 : 20001 : mptcp_data_unlock(subflow->conn);
1179 : 20001 : return true;
1180 : : }
1181 : :
1182 : 1788204 : mptcp_get_options(skb, &mp_opt);
1183 : :
1184 : : /* The subflow can be in close state only if check_fully_established()
1185 : : * just sent a reset. If so, tell the caller to ignore the current packet.
1186 : : */
1187 [ + + ]: 1788204 : if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1188 : 62 : return sk->sk_state != TCP_CLOSE;
1189 : :
1190 [ + + ]: 1788142 : if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1191 [ + + ]: 194801 : if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1192 [ + + ]: 274 : READ_ONCE(msk->local_key) == mp_opt.rcvr_key) {
1193 : 264 : WRITE_ONCE(msk->rcv_fastclose, true);
1194 : 264 : mptcp_schedule_work((struct sock *)msk);
1195 [ + - ]: 264 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
1196 : : }
1197 : :
1198 [ + + + - ]: 195595 : if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1199 : 794 : add_addr_hmac_valid(msk, &mp_opt)) {
1200 [ + + ]: 794 : if (!mp_opt.echo) {
1201 : 402 : mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1202 [ + - ]: 402 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1203 : : } else {
1204 : 392 : mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1205 : 392 : mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
1206 [ + - ]: 392 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1207 : : }
1208 : :
1209 [ + + ]: 794 : if (mp_opt.addr.port)
1210 [ + - ]: 86 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1211 : : }
1212 : :
1213 [ + + ]: 194801 : if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1214 : 108 : mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1215 : :
1216 [ + + ]: 194801 : if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1217 : 40 : mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1218 [ + - ]: 40 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1219 : : }
1220 : :
1221 [ + + ]: 194801 : if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1222 : 6 : mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1223 [ + - ]: 6 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1224 : : }
1225 : :
1226 [ + + ]: 194801 : if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1227 : 259 : subflow->reset_seen = 1;
1228 : 259 : subflow->reset_reason = mp_opt.reset_reason;
1229 : 259 : subflow->reset_transient = mp_opt.reset_transient;
1230 [ + - ]: 259 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1231 : : }
1232 : :
1233 [ + + ]: 194801 : if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1234 : : return true;
1235 : : }
1236 : :
1237 : : /* we can't wait for recvmsg() to update the ack_seq, otherwise
1238 : : * monodirectional flows will stuck
1239 : : */
1240 [ + + ]: 1784769 : if (mp_opt.use_ack)
1241 : 1783739 : ack_update_msk(msk, sk, &mp_opt);
1242 : 1784769 : rwin_update(msk, sk, skb);
1243 : :
1244 : : /* Zero-data-length packets are dropped by the caller and not
1245 : : * propagated to the MPTCP layer, so the skb extension does not
1246 : : * need to be allocated or populated. DATA_FIN information, if
1247 : : * present, needs to be updated here before the skb is freed.
1248 : : */
1249 [ + + ]: 1784769 : if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1250 [ + + + - : 738322 : if (mp_opt.data_fin && mp_opt.data_len == 1 &&
+ + ]
1251 : 20369 : mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
1252 : 3001 : mptcp_schedule_work((struct sock *)msk);
1253 : :
1254 : 717953 : return true;
1255 : : }
1256 : :
1257 : 1066816 : mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1258 [ + - ]: 1066816 : if (!mpext)
1259 : : return false;
1260 : :
1261 : 1066816 : memset(mpext, 0, sizeof(*mpext));
1262 : :
1263 [ + + ]: 1066816 : if (likely(mp_opt.use_map)) {
1264 [ + + ]: 1064071 : if (mp_opt.mpc_map) {
1265 : : /* this is an MP_CAPABLE carrying MPTCP data
1266 : : * we know this map the first chunk of data
1267 : : */
1268 : 1030 : mptcp_crypto_key_sha(subflow->remote_key, NULL,
1269 : : &mpext->data_seq);
1270 : 1030 : mpext->data_seq++;
1271 : 1030 : mpext->subflow_seq = 1;
1272 : 1030 : mpext->dsn64 = 1;
1273 : 1030 : mpext->mpc_map = 1;
1274 : 1030 : mpext->data_fin = 0;
1275 : : } else {
1276 : 1063041 : mpext->data_seq = mp_opt.data_seq;
1277 : 1063041 : mpext->subflow_seq = mp_opt.subflow_seq;
1278 : 1063041 : mpext->dsn64 = mp_opt.dsn64;
1279 : 1063041 : mpext->data_fin = mp_opt.data_fin;
1280 : : }
1281 : 1064071 : mpext->data_len = mp_opt.data_len;
1282 : 1064071 : mpext->use_map = 1;
1283 : 1064071 : mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1284 : :
1285 [ + + ]: 1064071 : if (mpext->csum_reqd)
1286 : 184558 : mpext->csum = mp_opt.csum;
1287 : : }
1288 : :
1289 : : return true;
1290 : : }
1291 : :
1292 : 1414416 : static u64 mptcp_set_rwin(struct mptcp_sock *msk, struct tcp_sock *tp,
1293 : : struct tcphdr *th, u64 ack_seq)
1294 : : {
1295 : 1414416 : const struct sock *ssk = (const struct sock *)tp;
1296 : 1414416 : u64 rcv_wnd_old, rcv_wnd_new;
1297 : 1414416 : u32 new_win;
1298 : 1414416 : u64 win;
1299 : :
1300 : 1414416 : rcv_wnd_new = ack_seq + tp->rcv_wnd;
1301 : :
1302 [ + + ]: 1414416 : rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1303 [ + + ]: 1414416 : if (after64(rcv_wnd_new, rcv_wnd_old)) {
1304 : 65 : u64 rcv_wnd;
1305 : :
1306 : 613651 : for (;;) {
1307 : 613716 : rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1308 : :
1309 [ + + ]: 613651 : if (rcv_wnd == rcv_wnd_old)
1310 : : break;
1311 : :
1312 : 6 : rcv_wnd_old = rcv_wnd;
1313 [ - + ]: 6 : if (before64(rcv_wnd_new, rcv_wnd_old)) {
1314 [ # # ]: 0 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1315 : 0 : goto raise_win;
1316 : : }
1317 [ - - ]: 71 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1318 : : }
1319 : 613645 : goto update_wspace;
1320 : : }
1321 : :
1322 [ + + ]: 800771 : if (rcv_wnd_new != rcv_wnd_old) {
1323 : 251825 : raise_win:
1324 : : /* The msk-level rcv wnd is after the tcp level one,
1325 : : * sync the latter.
1326 : : */
1327 : 251825 : rcv_wnd_new = rcv_wnd_old;
1328 : 251825 : win = rcv_wnd_old - ack_seq;
1329 : 251825 : new_win = min_t(u64, win, U32_MAX);
1330 : 251825 : tp->rcv_wnd = new_win;
1331 [ + + ]: 251825 : tcp_update_max_rcv_wnd_seq(tp);
1332 : :
1333 : : /* Make sure we do not exceed the maximum possible
1334 : : * scaled window.
1335 : : */
1336 [ - + ]: 251825 : if (unlikely(th->syn))
1337 : 0 : new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1338 [ - + ]: 251825 : if (!tp->rx_opt.rcv_wscale &&
[ - + - - ]
1339 [ # # ]: 0 : READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1340 : 0 : new_win = min(new_win, MAX_TCP_WINDOW);
1341 : : else
1342 : 251825 : new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1343 : :
1344 : : /* RFC1323 scaling applied */
1345 : 251825 : new_win >>= tp->rx_opt.rcv_wscale;
1346 : 251825 : th->window = htons(new_win);
1347 [ + - ]: 251825 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1348 : : }
1349 : :
1350 : 548946 : update_wspace:
1351 : 1414416 : WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
1352 : 1414416 : return rcv_wnd_new;
1353 : : }
1354 : :
1355 : 22993 : static void mptcp_track_rwin(struct tcp_sock *tp)
1356 : : {
1357 : 22993 : const struct sock *ssk = (const struct sock *)tp;
1358 : 22993 : struct mptcp_subflow_context *subflow;
1359 : 22993 : struct mptcp_sock *msk;
1360 : :
1361 [ + - ]: 22993 : if (!ssk)
1362 : : return;
1363 : :
1364 [ - + ]: 22993 : subflow = mptcp_subflow_ctx(ssk);
1365 [ - + ]: 22993 : msk = mptcp_sk(subflow->conn);
1366 : 22993 : WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
1367 : : }
1368 : :
1369 : 199759 : __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1370 : : {
1371 : 199759 : struct csum_pseudo_header header;
1372 : 199759 : __wsum csum;
1373 : :
1374 : : /* cfr RFC 8684 3.3.1.:
1375 : : * the data sequence number used in the pseudo-header is
1376 : : * always the 64-bit value, irrespective of what length is used in the
1377 : : * DSS option itself.
1378 : : */
1379 : 199759 : header.data_seq = cpu_to_be64(data_seq);
1380 : 199759 : header.subflow_seq = htonl(subflow_seq);
1381 : 199759 : header.data_len = htons(data_len);
1382 : 199759 : header.csum = 0;
1383 : :
1384 : 199759 : csum = csum_partial(&header, sizeof(header), sum);
1385 : 199759 : return csum_fold(csum);
1386 : : }
1387 : :
1388 : 135177 : static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1389 : : {
1390 : 270354 : return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1391 : 135177 : ~csum_unfold(mpext->csum));
1392 : : }
1393 : :
1394 : 0 : static void put_len_csum(u16 len, __sum16 csum, void *data)
1395 : : {
1396 : 135256 : __sum16 *sumptr = data + 2;
1397 : 135256 : __be16 *ptr = data;
1398 : :
1399 : 135256 : put_unaligned_be16(len, ptr);
1400 : :
1401 : 135256 : put_unaligned(csum, sumptr);
1402 : 135256 : }
1403 : :
1404 : 1445995 : void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1405 : : struct mptcp_out_options *opts)
1406 : : {
1407 : 1445995 : const struct sock *ssk = (const struct sock *)tp;
1408 : 1445995 : struct mptcp_subflow_context *subflow;
1409 : :
1410 : : /* Which options can be used together?
1411 : : *
1412 : : * X: mutually exclusive
1413 : : * O: often used together
1414 : : * C: can be used together in some cases
1415 : : * P: could be used together but we prefer not to (optimisations)
1416 : : *
1417 : : * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
1418 : : * ------|------|------|------|------|------|------|------|------|
1419 : : * MPC |------|------|------|------|------|------|------|------|
1420 : : * MPJ | X |------|------|------|------|------|------|------|
1421 : : * DSS | X | X |------|------|------|------|------|------|
1422 : : * ADD | X | X | P |------|------|------|------|------|
1423 : : * RM | C | C | C | P |------|------|------|------|
1424 : : * PRIO | X | C | C | C | C |------|------|------|
1425 : : * FAIL | X | X | C | X | X | X |------|------|
1426 : : * FC | X | X | X | X | X | X | X |------|
1427 : : * RST | X | X | X | X | X | X | O | O |
1428 : : * ------|------|------|------|------|------|------|------|------|
1429 : : *
1430 : : * The same applies in mptcp_established_options() function.
1431 : : */
1432 [ + + ]: 1445995 : if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1433 : 1414416 : struct mptcp_ext *mpext = &opts->ext_copy;
1434 : 1414416 : u8 len = TCPOLEN_MPTCP_DSS_BASE;
1435 : 1414416 : u8 flags = 0;
1436 : :
1437 [ + - ]: 1414416 : if (mpext->use_ack) {
1438 : 1414416 : flags = MPTCP_DSS_HAS_ACK;
1439 [ + + ]: 1414416 : if (mpext->ack64) {
1440 : : len += TCPOLEN_MPTCP_DSS_ACK64;
1441 : : flags |= MPTCP_DSS_ACK64;
1442 : : } else {
1443 : 103430 : len += TCPOLEN_MPTCP_DSS_ACK32;
1444 : : }
1445 : : }
1446 : :
1447 [ + + ]: 1414416 : if (mpext->use_map) {
1448 : 684118 : len += TCPOLEN_MPTCP_DSS_MAP64;
1449 : :
1450 : : /* Use only 64-bit mapping flags for now, add
1451 : : * support for optional 32-bit mappings later.
1452 : : */
1453 : 684118 : flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1454 [ + + ]: 684118 : if (mpext->data_fin)
1455 : 25898 : flags |= MPTCP_DSS_DATA_FIN;
1456 : :
1457 [ + + ]: 684118 : if (opts->csum_reqd)
1458 : 135179 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1459 : : }
1460 : :
1461 [ + - ]: 1414416 : *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1462 : :
1463 [ + - ]: 1414416 : if (mpext->use_ack) {
1464 : 1414416 : struct mptcp_sock *msk;
1465 : 1414416 : u64 ack_seq;
1466 : :
1467 : : /* DSS option is set only by mptcp_established_option,
1468 : : * the caller is __tcp_transmit_skb() and ssk is always
1469 : : * not NULL.
1470 : : */
1471 [ - + ]: 1414416 : subflow = mptcp_subflow_ctx(ssk);
1472 [ - + ]: 1414416 : msk = mptcp_sk(subflow->conn);
1473 : 1414416 : ack_seq = READ_ONCE(msk->ack_seq);
1474 [ + + ]: 1414416 : if (mpext->ack64) {
1475 : 1310986 : put_unaligned_be64(ack_seq, ptr);
1476 : 1310986 : ptr += 2;
1477 : : } else {
1478 : 103430 : put_unaligned_be32(ack_seq, ptr);
1479 : 103430 : ptr += 1;
1480 : : }
1481 : 1414416 : subflow->rcv_wnd_sent = mptcp_set_rwin(msk, tp, th,
1482 : : ack_seq);
1483 : : }
1484 : :
1485 [ + + ]: 1414416 : if (mpext->use_map) {
1486 [ + + ]: 684118 : put_unaligned_be64(mpext->data_seq, ptr);
1487 : 684118 : ptr += 2;
1488 [ + + ]: 684118 : put_unaligned_be32(mpext->subflow_seq, ptr);
1489 : 684118 : ptr += 1;
1490 [ + + ]: 684118 : if (opts->csum_reqd) {
1491 : : /* data_len == 0 is reserved for the infinite mapping,
1492 : : * the checksum will also be set to 0.
1493 : : */
1494 : 135179 : put_len_csum(mpext->data_len,
1495 [ + + ]: 135179 : (mpext->data_len ? mptcp_make_csum(mpext) : 0),
1496 : : ptr);
1497 : : } else {
1498 : 548939 : put_unaligned_be32(mpext->data_len << 16 |
1499 : 548939 : TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1500 : : }
1501 : 684118 : ptr += 1;
1502 : : }
1503 : :
1504 : : /* We might need to add MP_FAIL options in rare cases */
1505 [ + + ]: 1414416 : if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1506 : 4 : goto mp_fail;
1507 [ + + ]: 31579 : } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1508 : 5693 : u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1509 : :
1510 [ + + ]: 5693 : if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1511 : : len = TCPOLEN_MPTCP_MPC_SYN;
1512 [ + + ]: 3961 : } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1513 : : len = TCPOLEN_MPTCP_MPC_SYNACK;
1514 [ + + ]: 2337 : } else if (opts->data_len) {
1515 : 871 : len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1516 [ + + ]: 871 : if (opts->csum_reqd)
1517 : 77 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1518 : : } else {
1519 : : len = TCPOLEN_MPTCP_MPC_ACK;
1520 : : }
1521 : :
1522 [ + + ]: 5693 : if (opts->csum_reqd)
1523 : 451 : flag |= MPTCP_CAP_CHECKSUM_REQD;
1524 : :
1525 [ + + ]: 5693 : if (!opts->allow_join_id0)
1526 : 33 : flag |= MPTCP_CAP_DENY_JOIN_ID0;
1527 : :
1528 [ + + ]: 5693 : *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1529 : : MPTCP_SUPPORTED_VERSION,
1530 : : flag);
1531 : :
1532 : 5693 : if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1533 [ + + ]: 5693 : opts->suboptions))
1534 : 1732 : goto mp_capable_done;
1535 : :
1536 [ + + ]: 3961 : put_unaligned_be64(opts->sndr_key, ptr);
1537 : 3961 : ptr += 2;
1538 [ + + ]: 3961 : if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1539 : 1624 : goto mp_capable_done;
1540 : :
1541 [ + + ]: 2337 : put_unaligned_be64(opts->rcvr_key, ptr);
1542 : 2337 : ptr += 2;
1543 [ + + ]: 2337 : if (!opts->data_len)
1544 : 1466 : goto mp_capable_done;
1545 : :
1546 [ + + ]: 871 : if (opts->csum_reqd) {
1547 : 77 : put_len_csum(opts->data_len,
1548 : 77 : __mptcp_make_csum(opts->data_seq,
1549 : : opts->subflow_seq,
1550 : : opts->data_len,
1551 : 77 : ~csum_unfold(opts->csum)),
1552 : : ptr);
1553 : : } else {
1554 : 794 : put_unaligned_be32(opts->data_len << 16 |
1555 : 794 : TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1556 : : }
1557 : 871 : ptr += 1;
1558 : :
1559 : : /* MPC is additionally mutually exclusive with MP_PRIO */
1560 : 871 : goto mp_capable_done;
1561 [ + + ]: 25886 : } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
1562 [ + + ]: 1532 : if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1563 : 568 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1564 : : TCPOLEN_MPTCP_MPJ_SYN,
1565 : 568 : opts->backup, opts->join_id);
1566 : 568 : put_unaligned_be32(opts->token, ptr);
1567 : 568 : ptr += 1;
1568 : 568 : put_unaligned_be32(opts->nonce, ptr);
1569 : 568 : ptr += 1;
1570 [ + + ]: 964 : } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1571 : 492 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1572 : : TCPOLEN_MPTCP_MPJ_SYNACK,
1573 : 492 : opts->backup, opts->join_id);
1574 : 492 : put_unaligned_be64(opts->thmac, ptr);
1575 : 492 : ptr += 2;
1576 : 492 : put_unaligned_be32(opts->nonce, ptr);
1577 : 492 : ptr += 1;
1578 : : } else {
1579 : 472 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1580 : : TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1581 : 472 : memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1582 : 472 : ptr += 5;
1583 : : }
1584 [ + + ]: 24354 : } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1585 : 852 : u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1586 : 852 : u8 echo = MPTCP_ADDR_ECHO;
1587 : :
1588 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1589 [ + + ]: 852 : if (opts->addr.family == AF_INET6)
1590 : 196 : len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1591 : : #endif
1592 : :
1593 [ + + ]: 852 : if (opts->addr.port)
1594 : 86 : len += TCPOLEN_MPTCP_PORT_LEN;
1595 : :
1596 [ + + ]: 852 : if (opts->ahmac) {
1597 : 458 : len += sizeof(opts->ahmac);
1598 : 458 : echo = 0;
1599 : : }
1600 : :
1601 : 852 : *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1602 [ + + ]: 852 : len, echo, opts->addr.id);
1603 [ + + ]: 852 : if (opts->addr.family == AF_INET) {
1604 : 656 : memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1605 : 656 : ptr += 1;
1606 : : }
1607 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1608 [ + - ]: 196 : else if (opts->addr.family == AF_INET6) {
1609 : 196 : memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1610 : 196 : ptr += 4;
1611 : : }
1612 : : #endif
1613 : :
1614 [ + + ]: 852 : if (!opts->addr.port) {
1615 [ + + ]: 766 : if (opts->ahmac) {
1616 : 412 : put_unaligned_be64(opts->ahmac, ptr);
1617 : 412 : ptr += 2;
1618 : : }
1619 : : } else {
1620 : 86 : u16 port = ntohs(opts->addr.port);
1621 : :
1622 [ + + ]: 86 : if (opts->ahmac) {
1623 : 46 : u8 *bptr = (u8 *)ptr;
1624 : :
1625 : 46 : put_unaligned_be16(port, bptr);
1626 : 46 : bptr += 2;
1627 : 46 : put_unaligned_be64(opts->ahmac, bptr);
1628 : 46 : bptr += 8;
1629 : 46 : put_unaligned_be16(TCPOPT_NOP << 8 |
1630 : : TCPOPT_NOP, bptr);
1631 : :
1632 : 46 : ptr += 3;
1633 : : } else {
1634 : 40 : put_unaligned_be32(port << 16 |
1635 : 40 : TCPOPT_NOP << 8 |
1636 : : TCPOPT_NOP, ptr);
1637 : 40 : ptr += 1;
1638 : : }
1639 : : }
1640 [ + + ]: 23502 : } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1641 : : /* FASTCLOSE is mutually exclusive with others except RST */
1642 : 454 : *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1643 : : TCPOLEN_MPTCP_FASTCLOSE,
1644 : : 0, 0);
1645 [ + - ]: 454 : put_unaligned_be64(opts->rcvr_key, ptr);
1646 : 454 : ptr += 2;
1647 : :
1648 [ + - ]: 454 : if (OPTION_MPTCP_RST & opts->suboptions)
1649 : 454 : goto mp_rst;
1650 : : return;
1651 [ + + ]: 23048 : } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1652 : 2 : mp_fail:
1653 : : /* MP_FAIL is mutually exclusive with others except RST */
1654 [ + + ]: 6 : subflow = mptcp_subflow_ctx(ssk);
1655 : 6 : subflow->send_mp_fail = 0;
1656 : :
1657 : 6 : *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1658 : : TCPOLEN_MPTCP_FAIL,
1659 : : 0, 0);
1660 [ + + ]: 6 : put_unaligned_be64(opts->fail_seq, ptr);
1661 : 6 : ptr += 2;
1662 : :
1663 [ + + ]: 6 : if (OPTION_MPTCP_RST & opts->suboptions)
1664 : 2 : goto mp_rst;
1665 : : return;
1666 [ + + ]: 23046 : } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1667 : 53 : mp_rst:
1668 : 509 : *ptr++ = mptcp_option(MPTCPOPT_RST,
1669 : : TCPOLEN_MPTCP_RST,
1670 : 509 : opts->reset_transient,
1671 : 509 : opts->reset_reason);
1672 : 509 : return;
1673 [ + - ]: 22993 : } else if (unlikely(!opts->suboptions)) {
1674 : : /* Fallback to TCP */
1675 : 22993 : mptcp_track_rwin(tp);
1676 : 22993 : return;
1677 : : }
1678 : :
1679 [ + + ]: 1416796 : if (OPTION_MPTCP_PRIO & opts->suboptions) {
1680 [ + - ]: 28 : subflow = mptcp_subflow_ctx(ssk);
1681 : 28 : subflow->send_mp_prio = 0;
1682 : :
1683 : 28 : *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1684 : : TCPOLEN_MPTCP_PRIO,
1685 [ + - ]: 28 : opts->backup, TCPOPT_NOP);
1686 : :
1687 [ + - ]: 28 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
1688 : : }
1689 : :
1690 : 1416768 : mp_capable_done:
1691 [ + + ]: 1422489 : if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1692 : 108 : u8 i = 1;
1693 : :
1694 : 108 : *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1695 : 108 : TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1696 : 108 : 0, opts->rm_list.ids[0]);
1697 : :
1698 [ + + ]: 112 : while (i < opts->rm_list.nr) {
1699 : 4 : u8 id1, id2, id3, id4;
1700 : :
1701 : 4 : id1 = opts->rm_list.ids[i];
1702 [ + - ]: 4 : id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1703 [ - + ]: 4 : id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1704 [ - + ]: 4 : id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1705 : 4 : put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1706 : 4 : ptr += 1;
1707 : 4 : i += 4;
1708 : : }
1709 : : }
1710 : : }
1711 : :
1712 : 16 : __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1713 : : {
1714 [ + - ]: 16 : const struct mptcp_ext *ext = mptcp_get_ext(skb);
1715 : 16 : u8 flags, reason;
1716 : :
1717 [ + - ]: 16 : if (ext) {
1718 : 16 : flags = ext->reset_transient;
1719 : 16 : reason = ext->reset_reason;
1720 : :
1721 : 16 : return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1722 : : flags, reason);
1723 : : }
1724 : :
1725 : : return htonl(0u);
1726 : : }
1727 : : EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
|