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