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