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 : 2006070 : 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 : 2006070 : u8 subtype = *ptr >> 4;
28 : 2006070 : int expected_opsize;
29 : 2006070 : u16 subopt;
30 : 2006070 : u8 version;
31 : 2006070 : u8 flags;
32 : 2006070 : u8 i;
33 : :
34 [ + + + + : 2006070 : switch (subtype) {
+ + + + +
- ]
35 : 7556 : case MPTCPOPT_MP_CAPABLE:
36 : : /* strict size checking */
37 [ + + ]: 7556 : if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
38 [ + + ]: 4292 : if (skb->len > tcp_hdr(skb)->doff << 2)
39 : : expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
40 : : else
41 : 3286 : expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
42 : : subopt = OPTION_MPTCP_MPC_ACK;
43 : : } else {
44 [ + + ]: 3264 : 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 : 1740 : expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
49 : 1740 : 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 [ + + ]: 7556 : if (opsize != expected_opsize &&
64 : 96 : (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
65 [ + + ]: 96 : opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
66 : : break;
67 : :
68 : : /* try to be gentle vs future versions on the initial syn */
69 : 7544 : version = *ptr++ & MPTCP_VERSION_MASK;
70 [ + + ]: 7544 : if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
71 [ + + ]: 5810 : if (version != MPTCP_SUPPORTED_VERSION)
72 : : break;
73 [ + - ]: 1734 : } else if (version < MPTCP_SUPPORTED_VERSION) {
74 : : break;
75 : : }
76 : :
77 : 7538 : flags = *ptr++;
78 [ + + + + ]: 7538 : 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 [ + + ]: 7502 : if (flags & MPTCP_CAP_CHECKSUM_REQD)
89 : 560 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
90 : :
91 : 7502 : mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
92 : :
93 : 7502 : mp_opt->suboptions |= subopt;
94 [ + + ]: 7502 : if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
95 [ + + ]: 5780 : mp_opt->sndr_key = get_unaligned_be64(ptr);
96 : 5780 : ptr += 8;
97 : : }
98 [ + + ]: 5780 : if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
99 : 4274 : mp_opt->rcvr_key = get_unaligned_be64(ptr);
100 : 4274 : ptr += 8;
101 : : }
102 [ + + ]: 5996 : 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 : 1006 : mp_opt->suboptions |= OPTION_MPTCP_DSS;
109 : 1006 : mp_opt->use_map = 1;
110 : 1006 : mp_opt->mpc_map = 1;
111 [ + + ]: 1006 : mp_opt->data_len = get_unaligned_be16(ptr);
112 : 1006 : ptr += 2;
113 : : }
114 [ + + ]: 1006 : if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
115 : 84 : mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
116 : 84 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
117 : 84 : ptr += 2;
118 : : }
119 [ - + - - ]: 7502 : 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 : 2613 : case MPTCPOPT_MP_JOIN:
125 [ + + ]: 2613 : if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
126 : 664 : mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYN;
127 : 664 : mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
128 : 664 : mp_opt->join_id = *ptr++;
129 [ - + ]: 664 : mp_opt->token = get_unaligned_be32(ptr);
130 : 664 : ptr += 4;
131 : 664 : mp_opt->nonce = get_unaligned_be32(ptr);
132 : 664 : ptr += 4;
133 [ - + - - ]: 664 : 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 [ + + ]: 1949 : } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
137 : 624 : mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYNACK;
138 : 624 : mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
139 : 624 : mp_opt->join_id = *ptr++;
140 [ - + ]: 624 : mp_opt->thmac = get_unaligned_be64(ptr);
141 : 624 : ptr += 8;
142 [ - + ]: 624 : mp_opt->nonce = get_unaligned_be32(ptr);
143 : 624 : ptr += 4;
144 [ - + - - ]: 624 : 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 [ + - ]: 1325 : } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
148 : 1325 : mp_opt->suboptions |= OPTION_MPTCP_MPJ_ACK;
149 : 1325 : ptr += 2;
150 : 1325 : memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
151 [ - + - - ]: 1325 : pr_debug("MP_JOIN hmac\n");
152 : : }
153 : : break;
154 : :
155 : : case MPTCPOPT_DSS:
156 [ - + - - ]: 1994382 : pr_debug("DSS\n");
157 : 1994382 : ptr++;
158 : :
159 : 1994382 : flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
160 : 1994382 : mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
161 : 1994382 : mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
162 : 1994382 : mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
163 : 1994382 : mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
164 : 1994382 : mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
165 : :
166 [ - + - - ]: 1994382 : 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 : 1994382 : expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
172 : :
173 [ + - ]: 1994382 : if (mp_opt->use_ack) {
174 [ + + ]: 1994382 : if (mp_opt->ack64)
175 : : expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
176 : : else
177 : 229251 : expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
178 : : }
179 : :
180 [ + + ]: 1994382 : if (mp_opt->use_map) {
181 [ + + ]: 1254087 : if (mp_opt->dsn64)
182 : 1254045 : 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 [ + + ]: 1994382 : if (opsize != expected_opsize &&
191 [ + - ]: 135347 : opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
192 : : break;
193 : :
194 : 1994382 : mp_opt->suboptions |= OPTION_MPTCP_DSS;
195 [ + - ]: 1994382 : if (mp_opt->use_ack) {
196 [ + + ]: 1994382 : if (mp_opt->ack64) {
197 : 1765131 : mp_opt->data_ack = get_unaligned_be64(ptr);
198 : 1765131 : ptr += 8;
199 : : } else {
200 : 229251 : mp_opt->data_ack = get_unaligned_be32(ptr);
201 : 229251 : ptr += 4;
202 : : }
203 : :
204 [ - + - - ]: 1994382 : pr_debug("data_ack=%llu\n", mp_opt->data_ack);
205 : : }
206 : :
207 [ + + ]: 1994382 : if (mp_opt->use_map) {
208 [ + + ]: 1254087 : if (mp_opt->dsn64) {
209 : 1254045 : mp_opt->data_seq = get_unaligned_be64(ptr);
210 : 1254045 : ptr += 8;
211 : : } else {
212 : 42 : mp_opt->data_seq = get_unaligned_be32(ptr);
213 : 42 : ptr += 4;
214 : : }
215 : :
216 [ + + ]: 1254087 : mp_opt->subflow_seq = get_unaligned_be32(ptr);
217 : 1254087 : ptr += 4;
218 : :
219 [ + + ]: 1254087 : mp_opt->data_len = get_unaligned_be16(ptr);
220 : 1254087 : ptr += 2;
221 : :
222 [ + + ]: 1254087 : if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
223 : 135347 : mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
224 : 135347 : mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
225 : 135347 : ptr += 2;
226 : : }
227 : :
228 [ - + - - ]: 1254087 : 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 : 840 : case MPTCPOPT_ADD_ADDR:
237 : 840 : mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
238 [ + + ]: 840 : if (!mp_opt->echo) {
239 : 426 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
240 [ + + ]: 426 : opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
241 : 330 : mp_opt->addr.family = AF_INET;
242 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
243 : 96 : else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
244 [ + - ]: 96 : opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
245 : 96 : 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 : 840 : mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
263 : 840 : mp_opt->addr.id = *ptr++;
264 : 840 : mp_opt->addr.port = 0;
265 : 840 : mp_opt->ahmac = 0;
266 [ + + ]: 840 : if (mp_opt->addr.family == AF_INET) {
267 : 646 : memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
268 : 646 : ptr += 4;
269 : 646 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
270 [ + + ]: 646 : 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 : 194 : memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
278 : 194 : ptr += 16;
279 : 194 : if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
280 [ + + ]: 194 : 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 [ + + ]: 840 : if (!mp_opt->echo) {
287 : 426 : mp_opt->ahmac = get_unaligned_be64(ptr);
288 : 426 : ptr += 8;
289 : : }
290 [ - + - - : 840 : 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 : 254 : case MPTCPOPT_MP_FASTCLOSE:
319 [ + - ]: 254 : if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
320 : : break;
321 : :
322 : 254 : ptr += 2;
323 [ - + ]: 254 : mp_opt->rcvr_key = get_unaligned_be64(ptr);
324 : 254 : ptr += 8;
325 : 254 : mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
326 [ - + - - ]: 254 : pr_debug("MP_FASTCLOSE: recv_key=%llu\n", mp_opt->rcvr_key);
327 : : break;
328 : :
329 : 265 : case MPTCPOPT_RST:
330 [ + - ]: 265 : if (opsize != TCPOLEN_MPTCP_RST)
331 : : break;
332 : :
333 [ + + ]: 265 : if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
334 : : break;
335 : :
336 : 253 : mp_opt->suboptions |= OPTION_MPTCP_RST;
337 : 253 : flags = *ptr++;
338 : 253 : mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
339 : 253 : mp_opt->reset_reason = *ptr;
340 [ - + - - ]: 253 : 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 : 2006070 : }
358 : :
359 : 2006253 : void mptcp_get_options(const struct sk_buff *skb,
360 : : struct mptcp_options_received *mp_opt)
361 : : {
362 : 2006253 : const struct tcphdr *th = tcp_hdr(skb);
363 : 2006253 : const unsigned char *ptr;
364 : 2006253 : int length;
365 : :
366 : : /* Ensure that casting the whole status to u32 is efficient and safe */
367 : 2006253 : BUILD_BUG_ON(sizeof_field(struct mptcp_options_received, status) != sizeof(u32));
368 : 2006253 : BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_options_received, status),
369 : : sizeof(u32)));
370 : 2006253 : *(u32 *)&mp_opt->status = 0;
371 : :
372 : 2006253 : length = (th->doff * 4) - sizeof(struct tcphdr);
373 : 2006253 : ptr = (const unsigned char *)(th + 1);
374 : :
375 [ + + ]: 12285522 : while (length > 0) {
376 : 10279269 : int opcode = *ptr++;
377 : 10279269 : int opsize;
378 : :
379 [ + + - ]: 10279269 : switch (opcode) {
380 : : case TCPOPT_EOL:
381 : : return;
382 : 6251076 : case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
383 : 6251076 : length--;
384 : 6251076 : continue;
385 : 4028193 : default:
386 [ + - ]: 4028193 : if (length < 2)
387 : : return;
388 : 4028193 : opsize = *ptr++;
389 [ + - ]: 4028193 : if (opsize < 2) /* "silly options" */
390 : : return;
391 [ + - ]: 4028193 : if (opsize > length)
392 : : return; /* don't parse partial options */
393 [ + + ]: 4028193 : if (opcode == TCPOPT_MPTCP)
394 : 2006070 : mptcp_parse_option(skb, ptr, opsize, mp_opt);
395 : 4028193 : ptr += opsize - 2;
396 : 4028193 : length -= opsize;
397 : : }
398 : : }
399 : : }
400 : :
401 : 2539 : bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
402 : : unsigned int *size, struct mptcp_out_options *opts)
403 : : {
404 [ + + ]: 2539 : 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 : 2539 : subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
410 [ + + ]: 2539 : if (subflow->request_mptcp) {
411 [ + + ]: 1779 : 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 : 1773 : opts->suboptions = OPTION_MPTCP_MPC_SYN;
422 : 1773 : opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
423 : 1773 : opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
424 : 1773 : *size = TCPOLEN_MPTCP_MPC_SYN;
425 : 1773 : return true;
426 [ + + ]: 760 : } else if (subflow->request_join) {
427 [ - + - - ]: 730 : pr_debug("remote_token=%u, nonce=%u\n", subflow->remote_token,
428 : : subflow->local_nonce);
429 : 730 : opts->suboptions = OPTION_MPTCP_MPJ_SYN;
430 : 730 : opts->join_id = subflow->local_id;
431 : 730 : opts->token = subflow->remote_token;
432 : 730 : opts->nonce = subflow->local_nonce;
433 : 730 : opts->backup = subflow->request_bkup;
434 : 730 : *size = TCPOLEN_MPTCP_MPJ_SYN;
435 : 730 : return true;
436 : : }
437 : : return false;
438 : : }
439 : :
440 : 1264 : static void clear_3rdack_retransmission(struct sock *sk)
441 : : {
442 : 1264 : struct inet_connection_sock *icsk = inet_csk(sk);
443 : :
444 : 1264 : sk_stop_timer(sk, &icsk->icsk_delack_timer);
445 : 1264 : icsk->icsk_ack.ato = 0;
446 : 1264 : icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
447 : 1264 : }
448 : :
449 : 3828452 : 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 [ - + ]: 3828452 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
454 [ - + ]: 3828452 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
455 : 3828452 : struct mptcp_ext *mpext;
456 : 3828452 : unsigned int data_len;
457 : 3828452 : 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 [ + + ]: 3828452 : if (!skb)
466 : : return false;
467 : :
468 : : /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
469 [ - + + + : 1675375 : if (READ_ONCE(subflow->fully_established) || snd_data_fin_enable ||
+ + ]
[ + + + + ]
470 [ + + ]: 4165 : subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
471 [ + + ]: 2989 : sk->sk_state != TCP_ESTABLISHED)
472 : : return false;
473 : :
474 [ + + ]: 2989 : if (subflow->mp_capable) {
475 [ + + ]: 2352 : mpext = mptcp_get_ext(skb);
476 [ + + ]: 1599 : 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 : 2352 : opts->data_len = data_len;
483 : 2352 : opts->suboptions = OPTION_MPTCP_MPC_ACK;
484 : 2352 : opts->sndr_key = subflow->local_key;
485 : 2352 : opts->rcvr_key = subflow->remote_key;
486 [ - + ]: 2352 : opts->csum_reqd = READ_ONCE(msk->csum_enabled);
487 : 2352 : 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 [ + + ]: 2352 : if (data_len > 0) {
495 : 846 : len = TCPOLEN_MPTCP_MPC_ACK_DATA;
496 [ + + ]: 846 : if (opts->csum_reqd) {
497 : : /* we need to propagate more info to csum the pseudo hdr */
498 : 84 : opts->data_seq = mpext->data_seq;
499 : 84 : opts->subflow_seq = mpext->subflow_seq;
500 : 84 : opts->csum = mpext->csum;
501 : 84 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
502 : : }
503 : 846 : *size = ALIGN(len, 4);
504 : : } else {
505 : 1506 : *size = TCPOLEN_MPTCP_MPC_ACK;
506 : : }
507 : :
508 [ - + - - ]: 2352 : 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 : 2352 : return true;
513 [ + + ]: 637 : } else if (subflow->mp_join) {
514 : 637 : opts->suboptions = OPTION_MPTCP_MPJ_ACK;
515 : 637 : memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
516 : 637 : *size = TCPOLEN_MPTCP_MPJ_ACK;
517 [ - + - - ]: 637 : 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 [ + + ]: 637 : if (sock_owned_by_user(sk))
525 : 549 : set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
526 : : else
527 : 88 : mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
528 : 637 : return true;
529 : : }
530 : : return false;
531 : : }
532 : :
533 : 45216 : 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 [ - + ]: 45216 : u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
540 : :
541 [ + + - + ]: 45216 : 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 : 25420 : ext->data_fin = 1;
546 : 25420 : ext->use_map = 1;
547 : 25420 : ext->dsn64 = 1;
548 : 25420 : ext->data_seq = data_fin_tx_seq;
549 : 25420 : ext->subflow_seq = 0;
550 : 25420 : ext->data_len = 1;
551 [ + + ]: 19796 : } 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 : 1830 : ext->data_fin = 1;
557 : 1830 : ext->data_len++;
558 : : }
559 : 45216 : }
560 : :
561 : 3825463 : 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 [ - + ]: 3825463 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
566 [ - + ]: 3825463 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
567 [ - + ]: 3825463 : struct tcp_sock *tp = tcp_sk(sk);
568 : 3825463 : unsigned int dss_size = 0;
569 : 3825463 : struct mptcp_ext *mpext;
570 : 3825463 : unsigned int ack_size;
571 : 3825463 : bool ret = false;
572 : :
573 : : /* Zero `use_ack` and `use_map` flags with one shot. */
574 : 3825463 : BUILD_BUG_ON(sizeof_field(struct mptcp_ext, flags) != sizeof(u16));
575 : 3825463 : BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_ext, flags),
576 : : sizeof(u16)));
577 : 3825463 : *(u16 *)&opts->ext_copy.flags = 0;
578 [ - + ]: 3825463 : opts->csum_reqd = READ_ONCE(msk->csum_enabled);
579 [ + + ]: 3825463 : mpext = skb ? mptcp_get_ext(skb) : NULL;
580 : :
581 [ + + - + : 1672386 : if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
+ + ]
582 : 3065911 : unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
583 : :
584 [ + + ]: 3065911 : if (mpext) {
585 [ + + ]: 887414 : if (opts->csum_reqd)
586 : 134908 : map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
587 : :
588 : 887414 : opts->ext_copy = *mpext;
589 : : }
590 : :
591 : 3065911 : dss_size = map_size;
592 [ + + ]: 3065911 : if (skb && snd_data_fin_enable)
593 : 45216 : mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
594 : 3065911 : opts->suboptions = OPTION_MPTCP_DSS;
595 : 3065911 : 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 [ - + + + ]: 3825463 : if (!READ_ONCE(msk->can_ack)) {
[ + + ]
602 : 31 : *size = ALIGN(dss_size, 4);
603 : 31 : return ret;
604 : : }
605 : :
606 [ - + + + ]: 3825432 : if (READ_ONCE(msk->use_64bit_ack)) {
[ + + ]
607 : 3190450 : ack_size = TCPOLEN_MPTCP_DSS_ACK64;
608 : 3190450 : opts->ext_copy.ack64 = 1;
609 : : } else {
610 : 634982 : ack_size = TCPOLEN_MPTCP_DSS_ACK32;
611 : 634982 : opts->ext_copy.ack64 = 0;
612 : : }
613 : 3825432 : opts->ext_copy.use_ack = 1;
614 : 3825432 : opts->suboptions = OPTION_MPTCP_DSS;
615 : :
616 : : /* Add kind/length/subtype/flag overhead if mapping is not populated */
617 [ + + ]: 3825432 : if (dss_size == 0)
618 : 759552 : 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 [ + + ]: 3825432 : if (skb)
624 : 1672386 : tp->rcv_wnd = tp->rcv_nxt - tp->rcv_wup;
625 : :
626 : 3825432 : dss_size += ack_size;
627 : :
628 : 3825432 : *size = ALIGN(dss_size, 4);
629 : 3825432 : return true;
630 : : }
631 : :
632 : 914 : static u64 add_addr_generate_hmac(u64 key1, u64 key2,
633 : : struct mptcp_addr_info *addr)
634 : : {
635 : 914 : u16 port = ntohs(addr->port);
636 : 914 : u8 hmac[SHA256_DIGEST_SIZE];
637 : 914 : u8 msg[19];
638 : 914 : int i = 0;
639 : :
640 : 914 : msg[i++] = addr->id;
641 [ + + ]: 914 : if (addr->family == AF_INET) {
642 : 704 : memcpy(&msg[i], &addr->addr.s_addr, 4);
643 : 704 : i += 4;
644 : : }
645 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
646 [ + - ]: 210 : else if (addr->family == AF_INET6) {
647 : 210 : memcpy(&msg[i], &addr->addr6.s6_addr, 16);
648 : 210 : i += 16;
649 : : }
650 : : #endif
651 : 914 : msg[i++] = port >> 8;
652 : 914 : msg[i++] = port & 0xFF;
653 : :
654 : 914 : mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
655 : :
656 : 914 : return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
657 : : }
658 : :
659 : 3828448 : 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 [ - + ]: 3828448 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
666 [ - + ]: 3828448 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
667 : 3828448 : struct mptcp_addr_info addr;
668 : 3828448 : bool drop_ts = has_ts;
669 : 3828448 : bool echo;
670 : :
671 : : /* add addr will strip the existing options, be sure to avoid breaking
672 : : * MPC/MPJ handshakes
673 : : */
674 [ + + ]: 3828448 : if (!mptcp_pm_should_add_signal(msk) ||
675 [ + - + + ]: 908 : (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
676 [ + + + + ]: 1813 : !skb || !skb_is_tcp_pure_ack(skb) ||
677 : 906 : !mptcp_pm_add_addr_signal(msk, size, remaining, &addr, &echo,
678 : : &drop_ts))
679 : 3827550 : return false;
680 : :
681 [ - + - - ]: 898 : pr_debug("drop other suboptions\n");
682 : 898 : opts->suboptions = OPTION_MPTCP_ADD_ADDR;
683 [ - + ]: 898 : opts->drop_ts = drop_ts;
684 : 898 : opts->addr = addr;
685 [ - + + + ]: 898 : if (!echo) {
[ + + ]
686 [ + - ]: 488 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
687 : 488 : opts->ahmac = add_addr_generate_hmac(READ_ONCE(msk->local_key),
688 : 488 : 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 [ - + - - : 898 : 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 : 3827550 : static bool mptcp_established_options_rm_addr(struct sock *sk, int *size,
701 : : unsigned int remaining,
702 : : struct mptcp_out_options *opts)
703 : : {
704 [ - + ]: 3827550 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
705 [ - + ]: 3827550 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
706 : 3827550 : struct mptcp_rm_list rm_list;
707 : 3827550 : int i;
708 : :
709 [ + + - + ]: 3827658 : if (!mptcp_pm_should_rm_signal(msk) ||
710 : 108 : !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list, size)))
711 : 3827442 : 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 : 3828448 : static bool mptcp_established_options_mp_prio(struct sock *sk, int *size,
723 : : unsigned int remaining,
724 : : struct mptcp_out_options *opts)
725 : : {
726 [ + + ]: 3828448 : 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 [ + + + - ]: 3828448 : 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 : 495 : 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 [ + - ]: 495 : const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
753 : :
754 [ + - ]: 495 : if (remaining < TCPOLEN_MPTCP_RST)
755 : : return false;
756 : :
757 : 495 : *size = TCPOLEN_MPTCP_RST;
758 : 495 : opts->suboptions |= OPTION_MPTCP_RST;
759 : 495 : opts->reset_transient = subflow->reset_transient;
760 : 495 : opts->reset_reason = subflow->reset_reason;
761 [ + - ]: 495 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
762 : :
763 : : return true;
764 : : }
765 : :
766 : 495 : static bool mptcp_established_options_fastclose(struct sock *sk, int *size,
767 : : unsigned int remaining,
768 : : struct mptcp_out_options *opts)
769 : : {
770 [ - + ]: 495 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
771 [ - + ]: 495 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
772 : :
773 [ + + ]: 495 : if (likely(!subflow->send_fastclose))
774 : : return false;
775 : :
776 [ + - ]: 435 : if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
777 : : return false;
778 : :
779 : 435 : *size = TCPOLEN_MPTCP_FASTCLOSE;
780 : 435 : opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
781 : 435 : opts->rcvr_key = READ_ONCE(msk->remote_key);
782 : :
783 [ - + - - ]: 435 : pr_debug("FASTCLOSE key=%llu\n", opts->rcvr_key);
784 [ + - ]: 435 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
785 : : return true;
786 : : }
787 : :
788 : 3825523 : static bool mptcp_established_options_mp_fail(struct sock *sk, int *size,
789 : : unsigned int remaining,
790 : : struct mptcp_out_options *opts)
791 : : {
792 [ + + ]: 3825523 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
793 : :
794 [ + + ]: 3825523 : 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 : 3921720 : 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 [ - + ]: 3921720 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
815 [ - + ]: 3921720 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
816 : 3921720 : int total_size = 0;
817 : 3921720 : bool snd_data_fin;
818 : 3921720 : bool ret = false;
819 : 3921720 : int opt_size = 0;
820 : :
821 : 3921720 : opts->suboptions = 0;
822 : :
823 : : /* Force later mptcp_write_options(), but do not use any actual
824 : : * option space.
825 : : */
826 [ + + + + ]: 3921720 : if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
827 : : return 0;
828 : :
829 [ + + + + ]: 3828947 : if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
830 [ + + + + ]: 555 : if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
831 : 60 : mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
832 : 437 : total_size += opt_size;
833 : 437 : remaining -= opt_size;
834 : : }
835 : : /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
836 [ + - ]: 495 : if (mptcp_established_options_rst(sk, &opt_size, remaining, opts)) {
837 : 495 : total_size += opt_size;
838 : 495 : remaining -= opt_size;
839 : : }
840 : 495 : return total_size;
841 : : }
842 : :
843 [ + + ]: 3828452 : snd_data_fin = mptcp_data_fin_enabled(msk);
844 [ + + ]: 3828452 : if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
845 : : ret = true;
846 [ + - ]: 3825463 : else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
847 : 3825463 : int mp_fail_size;
848 : :
849 : 3825463 : ret = true;
850 [ + + ]: 3825463 : 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 [ - + - + ]: 3828448 : if (WARN_ON_ONCE(opt_size > remaining))
862 : : return -1;
863 : :
864 : 3828448 : total_size += opt_size;
865 : 3828448 : remaining -= opt_size;
866 [ + + ]: 3828448 : if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining,
867 : : has_ts, opts)) {
868 : 898 : total_size += opt_size;
869 : 898 : remaining -= opt_size;
870 : 898 : ret = true;
871 [ + + ]: 3827550 : } 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 [ + + ]: 3828448 : 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 [ - + ]: 3828448 : return ret ? total_size : -1;
884 : : }
885 : :
886 : 2488 : bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
887 : : struct mptcp_out_options *opts)
888 : : {
889 : 2488 : struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
890 : :
891 [ + + ]: 2488 : if (subflow_req->mp_capable) {
892 : 1670 : opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
893 : 1670 : opts->sndr_key = subflow_req->local_key;
894 : 1670 : opts->csum_reqd = subflow_req->csum_reqd;
895 : 1670 : opts->allow_join_id0 = subflow_req->allow_join_id0;
896 : 1670 : *size = TCPOLEN_MPTCP_MPC_SYNACK;
897 [ - + - - ]: 1670 : pr_debug("subflow_req=%p, local_key=%llu\n",
898 : : subflow_req, subflow_req->local_key);
899 : 1670 : return true;
900 [ + + ]: 818 : } else if (subflow_req->mp_join) {
901 : 658 : opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
902 : 658 : opts->backup = subflow_req->request_bkup;
903 : 658 : opts->join_id = subflow_req->local_id;
904 : 658 : opts->thmac = subflow_req->thmac;
905 : 658 : opts->nonce = subflow_req->local_nonce;
906 [ - + - - ]: 658 : 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 : 658 : *size = TCPOLEN_MPTCP_MPJ_SYNACK;
910 : 658 : return true;
911 : : }
912 : : return false;
913 : : }
914 : :
915 : 1999061 : 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 [ - + + + ]: 1999061 : 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 [ + + ]: 1996902 : if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
929 [ + + + + ]: 215920 : TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
930 [ + + ]: 68897 : subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
931 [ + - ]: 649 : !subflow->request_join)
932 : 649 : tcp_send_ack(ssk);
933 : 1996902 : 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 [ + + ]: 2159 : if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
942 [ - + ]: 51 : if (subflow->mp_join)
943 : 0 : goto reset;
944 [ - + - - ]: 51 : if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
945 : 0 : goto set_fully_established;
946 : 51 : return subflow->mp_capable;
947 : : }
948 : :
949 [ + + ]: 2108 : if (subflow->remote_key_valid &&
950 [ + + - + : 2070 : (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
+ + ]
951 : 164 : ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) &&
952 [ + + + - ]: 164 : (!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 : 2056 : 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 [ - + ]: 2094 : if (mp_opt->deny_join_id0)
980 : 0 : WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
981 : :
982 : 2094 : mptcp_data_lock((struct sock *)msk);
983 : 2094 : __mptcp_subflow_fully_established(msk, subflow, mp_opt);
984 : 2094 : mptcp_data_unlock((struct sock *)msk);
985 : :
986 : 1998996 : 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 [ + + + - ]: 1998996 : if (likely(subflow->pm_notified) || list_empty(&subflow->node))
993 : : return true;
994 : :
995 : 2742 : subflow->pm_notified = 1;
996 [ + + ]: 2742 : if (subflow->mp_join) {
997 : 1264 : clear_3rdack_retransmission(ssk);
998 : 1264 : mptcp_pm_subflow_established(msk);
999 : : } else {
1000 : 1478 : 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 : 241121 : u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
1010 : : {
1011 : 241121 : u32 old_seq32, cur_seq32;
1012 : :
1013 : 241121 : old_seq32 = (u32)old_seq;
1014 : 241121 : cur_seq32 = (u32)cur_seq;
1015 : 241121 : cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
1016 [ - + - - ]: 241121 : 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 [ + + - + ]: 241121 : 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 : 547792 : msk->bytes_acked += new_snd_una - msk->snd_una;
1028 : 547792 : WRITE_ONCE(msk->snd_una, new_snd_una);
1029 : : }
1030 : :
1031 : 1995381 : static void rwin_update(struct mptcp_sock *msk, struct sock *ssk,
1032 : : struct sk_buff *skb)
1033 : : {
1034 [ - + ]: 1995381 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1035 [ - + ]: 1995381 : struct tcp_sock *tp = tcp_sk(ssk);
1036 : 1995381 : 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 [ + - + + ]: 1995381 : if (!skb->len || skb->len < tcp_receive_window(tp))
1043 : : return;
1044 : :
1045 [ + + ]: 14798 : mptcp_rcv_wnd = atomic64_read(&msk->rcv_wnd_sent);
1046 [ + + ]: 14798 : 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 : 27 : tp->rcv_wnd += mptcp_rcv_wnd - subflow->rcv_wnd_sent;
1053 [ + + ]: 27 : tcp_update_max_rcv_wnd_seq(tp);
1054 : 27 : subflow->rcv_wnd_sent = mptcp_rcv_wnd;
1055 : : }
1056 : :
1057 : 1994382 : static void ack_update_msk(struct mptcp_sock *msk,
1058 : : struct sock *ssk,
1059 : : struct mptcp_options_received *mp_opt)
1060 : : {
1061 : 1994382 : u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
1062 : 1994382 : struct sock *sk = (struct sock *)msk;
1063 : 1994382 : u64 old_snd_una;
1064 : :
1065 : 1994382 : 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 : 1994382 : old_snd_una = msk->snd_una;
1072 [ + + ]: 1994382 : 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 [ + + ]: 1994382 : if (unlikely(after64(new_snd_una, snd_nxt)))
1076 : 74 : new_snd_una = old_snd_una;
1077 : :
1078 [ - + ]: 1994382 : new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
1079 : :
1080 [ + + ]: 1994382 : if (after64(new_wnd_end, msk->wnd_end))
1081 : 573232 : WRITE_ONCE(msk->wnd_end, new_wnd_end);
1082 : :
1083 : : /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
1084 [ + + ]: 1994382 : if (after64(msk->wnd_end, snd_nxt))
1085 : 1865670 : __mptcp_check_push(sk, ssk);
1086 : :
1087 [ + + ]: 1994382 : if (after64(new_snd_una, old_snd_una)) {
1088 : 580795 : __mptcp_snd_una_update(msk, new_snd_una);
1089 : 580795 : __mptcp_data_acked(sk);
1090 : : }
1091 : 1994382 : msk->last_ack_recv = tcp_jiffies32;
1092 : 1994382 : mptcp_data_unlock(sk);
1093 : :
1094 : 1994382 : trace_ack_update_msk(mp_opt->data_ack,
1095 : : old_snd_una, new_snd_una,
1096 : 1994382 : new_wnd_end, READ_ONCE(msk->wnd_end));
1097 : 1994382 : }
1098 : :
1099 : 25924 : 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 [ - + + + ]: 25924 : if (READ_ONCE(msk->rcv_data_fin))
[ + + ]
1107 : : return false;
1108 : :
1109 [ + + ]: 3315 : WRITE_ONCE(msk->rcv_data_fin_seq,
1110 : : mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
1111 : 3315 : WRITE_ONCE(msk->rcv_data_fin, 1);
1112 : :
1113 : 3315 : return true;
1114 : : }
1115 : :
1116 : 840 : static bool add_addr_hmac_valid(struct mptcp_sock *msk,
1117 : : struct mptcp_options_received *mp_opt)
1118 : : {
1119 : 840 : u64 hmac = 0;
1120 : :
1121 [ + + ]: 840 : if (mp_opt->echo)
1122 : : return true;
1123 : :
1124 : 426 : hmac = add_addr_generate_hmac(READ_ONCE(msk->remote_key),
1125 : 426 : READ_ONCE(msk->local_key),
1126 : : &mp_opt->addr);
1127 : :
1128 [ - + - - ]: 426 : pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
1129 : : msk, hmac, mp_opt->ahmac);
1130 : :
1131 : 426 : return hmac == mp_opt->ahmac;
1132 : : }
1133 : :
1134 : 19015 : static bool mptcp_over_limit(struct sock *sk, struct sock *ssk,
1135 : : const struct sk_buff *skb)
1136 : : {
1137 [ - + ]: 19015 : struct mptcp_sock *msk = mptcp_sk(sk);
1138 [ + + ]: 19015 : u64 mem = sk_rmem_alloc_get(sk);
1139 : :
1140 : 19015 : mem += READ_ONCE(msk->backlog_len);
1141 [ + + ]: 19015 : if (likely(mem <= READ_ONCE(sk->sk_rcvbuf)))
1142 : : return false;
1143 : :
1144 : : /* Avoid silently dropping pure acks, fin or zero win probes. */
1145 [ + + ]: 45 : if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq ||
1146 [ + - + - ]: 44 : TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN ||
1147 [ - + + - ]: 22 : !after(TCP_SKB_CB(skb)->end_seq, tcp_sk(ssk)->rcv_nxt))
1148 : : return false;
1149 : :
1150 : : /* Dropped due to memory constraints, schedule an ack. */
1151 : 22 : inet_csk(ssk)->icsk_ack.pending |= ICSK_ACK_NOMEM | ICSK_ACK_NOW;
1152 : 22 : inet_csk_schedule_ack(ssk);
1153 : 22 : return true;
1154 : : }
1155 : :
1156 : : /* Return false when the caller must drop the packet, i.e. in case of error,
1157 : : * subflow has been reset, or over memory limits.
1158 : : */
1159 : 2018076 : bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
1160 : : {
1161 [ - + ]: 2018076 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1162 [ - + ]: 2018076 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1163 : 2018076 : struct mptcp_options_received mp_opt;
1164 : 2018076 : struct mptcp_ext *mpext;
1165 : :
1166 [ + + ]: 2018076 : if (__mptcp_check_fallback(msk)) {
1167 : : /* Keep it simple and unconditionally trigger send data cleanup and
1168 : : * pending queue spooling. We will need to acquire the data lock
1169 : : * for more accurate checks, and once the lock is acquired, such
1170 : : * helpers are cheap.
1171 : : */
1172 : 19015 : mptcp_data_lock(subflow->conn);
1173 [ + + ]: 19015 : if (sk_stream_memory_free(sk))
1174 : 19013 : __mptcp_check_push(subflow->conn, sk);
1175 : :
1176 : : /* on fallback we just need to ignore the msk-level snd_una, as
1177 : : * this is really plain TCP
1178 : : */
1179 : 19015 : __mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
1180 : :
1181 : 19015 : __mptcp_data_acked(subflow->conn);
1182 : 19015 : mptcp_data_unlock(subflow->conn);
1183 : 19015 : return !mptcp_over_limit(subflow->conn, sk, skb);
1184 : : }
1185 : :
1186 : 1999061 : mptcp_get_options(skb, &mp_opt);
1187 : :
1188 : : /* The subflow can be in close state only if check_fully_established()
1189 : : * just sent a reset. If so, tell the caller to ignore the current packet.
1190 : : */
1191 [ + + ]: 1999061 : if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
1192 : 59 : return sk->sk_state != TCP_CLOSE;
1193 : :
1194 [ + + ]: 1999002 : if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
1195 [ + + ]: 140141 : if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
1196 [ + + ]: 254 : READ_ONCE(msk->local_key) == mp_opt.rcvr_key) {
1197 : 244 : WRITE_ONCE(msk->rcv_fastclose, true);
1198 : 244 : mptcp_schedule_work((struct sock *)msk);
1199 [ + - ]: 244 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
1200 : : }
1201 : :
1202 [ + + + - ]: 140981 : if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
1203 : 840 : add_addr_hmac_valid(msk, &mp_opt)) {
1204 [ + + ]: 840 : if (!mp_opt.echo) {
1205 : 426 : mptcp_pm_add_addr_received(sk, &mp_opt.addr);
1206 [ + - ]: 426 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
1207 : : } else {
1208 : 414 : mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
1209 [ + - ]: 414 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
1210 : : }
1211 : :
1212 [ + + ]: 840 : if (mp_opt.addr.port)
1213 [ + - ]: 96 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
1214 : : }
1215 : :
1216 [ + + ]: 140141 : if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
1217 : 108 : mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
1218 : :
1219 [ + + ]: 140141 : if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
1220 : 40 : mptcp_pm_mp_prio_received(sk, mp_opt.backup);
1221 [ + - ]: 40 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
1222 : : }
1223 : :
1224 [ + + ]: 140141 : if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
1225 : 6 : mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
1226 [ + - ]: 6 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
1227 : : }
1228 : :
1229 [ + + ]: 140141 : if (mp_opt.suboptions & OPTION_MPTCP_RST) {
1230 : 243 : subflow->reset_seen = 1;
1231 : 243 : subflow->reset_reason = mp_opt.reset_reason;
1232 : 243 : subflow->reset_transient = mp_opt.reset_transient;
1233 [ + - ]: 243 : MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
1234 : : }
1235 : :
1236 [ + + ]: 140141 : if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
1237 : : return true;
1238 : : }
1239 : :
1240 : : /* we can't wait for recvmsg() to update the ack_seq, otherwise
1241 : : * monodirectional flows will stuck
1242 : : */
1243 [ + + ]: 1995381 : if (mp_opt.use_ack)
1244 : 1994382 : ack_update_msk(msk, sk, &mp_opt);
1245 : 1995381 : rwin_update(msk, sk, skb);
1246 : :
1247 : : /* Zero-data-length packets are dropped by the caller and not
1248 : : * propagated to the MPTCP layer, so the skb extension does not
1249 : : * need to be allocated or populated. DATA_FIN information, if
1250 : : * present, needs to be updated here before the skb is freed.
1251 : : */
1252 [ + + ]: 1995381 : if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
1253 [ + + + - : 782910 : if (mp_opt.data_fin && mp_opt.data_len == 1 &&
+ + ]
1254 : 22869 : mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
1255 : 3097 : mptcp_schedule_work((struct sock *)msk);
1256 : :
1257 : 760041 : return true;
1258 : : }
1259 : :
1260 : 1235340 : mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
1261 [ + - ]: 1235340 : if (!mpext)
1262 : : return false;
1263 : :
1264 : 1235340 : memset(mpext, 0, sizeof(*mpext));
1265 : :
1266 [ + + ]: 1235340 : if (likely(mp_opt.use_map)) {
1267 [ + + ]: 1232181 : if (mp_opt.mpc_map) {
1268 : : /* this is an MP_CAPABLE carrying MPTCP data
1269 : : * we know this map the first chunk of data
1270 : : */
1271 : 999 : mptcp_crypto_key_sha(subflow->remote_key, NULL,
1272 : : &mpext->data_seq);
1273 : 999 : mpext->data_seq++;
1274 : 999 : mpext->subflow_seq = 1;
1275 : 999 : mpext->dsn64 = 1;
1276 : 999 : mpext->mpc_map = 1;
1277 : 999 : mpext->data_fin = 0;
1278 : : } else {
1279 : 1231182 : mpext->data_seq = mp_opt.data_seq;
1280 : 1231182 : mpext->subflow_seq = mp_opt.subflow_seq;
1281 : 1231182 : mpext->dsn64 = mp_opt.dsn64;
1282 : 1231182 : mpext->data_fin = mp_opt.data_fin;
1283 : : }
1284 : 1232181 : mpext->data_len = mp_opt.data_len;
1285 : 1232181 : mpext->use_map = 1;
1286 : 1232181 : mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
1287 : :
1288 [ + + ]: 1232181 : if (mpext->csum_reqd)
1289 : 131956 : mpext->csum = mp_opt.csum;
1290 : : }
1291 : :
1292 : : return true;
1293 : : }
1294 : :
1295 : 1671488 : static u64 mptcp_set_rwin(struct mptcp_sock *msk, struct tcp_sock *tp,
1296 : : struct tcphdr *th, u64 ack_seq)
1297 : : {
1298 : 1671488 : const struct sock *ssk = (const struct sock *)tp;
1299 : 1671488 : u64 rcv_wnd_old, rcv_wnd_new;
1300 : 1671488 : u32 new_win;
1301 : 1671488 : u64 win;
1302 : :
1303 : 1671488 : rcv_wnd_new = ack_seq + tp->rcv_wnd;
1304 : :
1305 [ + + ]: 1671488 : rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
1306 [ + + ]: 1671488 : if (after64(rcv_wnd_new, rcv_wnd_old)) {
1307 : 45359 : u64 rcv_wnd;
1308 : :
1309 : 613598 : for (;;) {
1310 : 658957 : rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
1311 : :
1312 [ + + ]: 613598 : if (rcv_wnd == rcv_wnd_old)
1313 : : break;
1314 : :
1315 : 5 : rcv_wnd_old = rcv_wnd;
1316 [ + + ]: 5 : if (before64(rcv_wnd_new, rcv_wnd_old)) {
1317 [ # # ]: 1 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
1318 : 1 : goto raise_win;
1319 : : }
1320 [ - + ]: 45363 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
1321 : : }
1322 : 613593 : goto update_wspace;
1323 : : }
1324 : :
1325 [ + + ]: 1057894 : if (rcv_wnd_new != rcv_wnd_old) {
1326 : 327216 : raise_win:
1327 : : /* The msk-level rcv wnd is after the tcp level one,
1328 : : * sync the latter.
1329 : : */
1330 : 327217 : rcv_wnd_new = rcv_wnd_old;
1331 : 327217 : win = rcv_wnd_old - ack_seq;
1332 : 327217 : new_win = min_t(u64, win, U32_MAX);
1333 : 327217 : tp->rcv_wnd = new_win;
1334 [ + + ]: 327217 : tcp_update_max_rcv_wnd_seq(tp);
1335 : :
1336 : : /* Make sure we do not exceed the maximum possible
1337 : : * scaled window.
1338 : : */
1339 [ - + ]: 327217 : if (unlikely(th->syn))
1340 : 0 : new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
1341 [ - + ]: 327217 : if (!tp->rx_opt.rcv_wscale &&
[ - + - - ]
1342 [ # # ]: 0 : READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
1343 : 0 : new_win = min(new_win, MAX_TCP_WINDOW);
1344 : : else
1345 : 327217 : new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
1346 : :
1347 : : /* RFC1323 scaling applied */
1348 : 327217 : new_win >>= tp->rx_opt.rcv_wscale;
1349 : 327217 : th->window = htons(new_win);
1350 [ + - ]: 327217 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
1351 : : }
1352 : :
1353 : 730678 : update_wspace:
1354 : 1671488 : WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
1355 : 1671488 : return rcv_wnd_new;
1356 : : }
1357 : :
1358 : 21500 : static void mptcp_track_rwin(struct tcp_sock *tp)
1359 : : {
1360 : 21500 : const struct sock *ssk = (const struct sock *)tp;
1361 : 21500 : struct mptcp_subflow_context *subflow;
1362 : 21500 : struct mptcp_sock *msk;
1363 : :
1364 [ + - ]: 21500 : if (!ssk)
1365 : : return;
1366 : :
1367 [ - + ]: 21500 : subflow = mptcp_subflow_ctx(ssk);
1368 [ - + ]: 21500 : msk = mptcp_sk(subflow->conn);
1369 : 21500 : WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
1370 : : }
1371 : :
1372 : 201682 : __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
1373 : : {
1374 : 201682 : struct csum_pseudo_header header;
1375 : 201682 : __wsum csum;
1376 : :
1377 : : /* cfr RFC 8684 3.3.1.:
1378 : : * the data sequence number used in the pseudo-header is
1379 : : * always the 64-bit value, irrespective of what length is used in the
1380 : : * DSS option itself.
1381 : : */
1382 : 201682 : header.data_seq = cpu_to_be64(data_seq);
1383 : 201682 : header.subflow_seq = htonl(subflow_seq);
1384 : 201682 : header.data_len = htons(data_len);
1385 : 201682 : header.csum = 0;
1386 : :
1387 : 201682 : csum = csum_partial(&header, sizeof(header), sum);
1388 : 201682 : return csum_fold(csum);
1389 : : }
1390 : :
1391 : 138468 : static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
1392 : : {
1393 : 276936 : return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
1394 : 138468 : ~csum_unfold(mpext->csum));
1395 : : }
1396 : :
1397 : 0 : static void put_len_csum(u16 len, __sum16 csum, void *data)
1398 : : {
1399 : 138554 : __sum16 *sumptr = data + 2;
1400 : 138554 : __be16 *ptr = data;
1401 : :
1402 : 138554 : put_unaligned_be16(len, ptr);
1403 : :
1404 : 138554 : put_unaligned(csum, sumptr);
1405 : 138554 : }
1406 : :
1407 : 1702201 : void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
1408 : : struct mptcp_out_options *opts)
1409 : : {
1410 : 1702201 : const struct sock *ssk = (const struct sock *)tp;
1411 : 1702201 : struct mptcp_subflow_context *subflow;
1412 : :
1413 : : /* Which options can be used together?
1414 : : *
1415 : : * X: mutually exclusive
1416 : : * O: often used together
1417 : : * C: can be used together in some cases
1418 : : * P: could be used together but we prefer not to (optimisations)
1419 : : *
1420 : : * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
1421 : : * ------|------|------|------|------|------|------|------|------|
1422 : : * MPC |------|------|------|------|------|------|------|------|
1423 : : * MPJ | X |------|------|------|------|------|------|------|
1424 : : * DSS | X | X |------|------|------|------|------|------|
1425 : : * ADD | X | X | P |------|------|------|------|------|
1426 : : * RM | C | C | C | P |------|------|------|------|
1427 : : * PRIO | X | C | C | C | C |------|------|------|
1428 : : * FAIL | X | X | C | X | X | X |------|------|
1429 : : * FC | X | X | X | X | X | X | X |------|
1430 : : * RST | X | X | X | X | X | X | O | O |
1431 : : * ------|------|------|------|------|------|------|------|------|
1432 : : *
1433 : : * The same applies in mptcp_established_options() function.
1434 : : */
1435 [ + + ]: 1702201 : if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
1436 : 1671488 : struct mptcp_ext *mpext = &opts->ext_copy;
1437 : 1671488 : u8 len = TCPOLEN_MPTCP_DSS_BASE;
1438 : 1671488 : u8 flags = 0;
1439 : :
1440 [ + - ]: 1671488 : if (mpext->use_ack) {
1441 : 1671488 : flags = MPTCP_DSS_HAS_ACK;
1442 [ + + ]: 1671488 : if (mpext->ack64) {
1443 : : len += TCPOLEN_MPTCP_DSS_ACK64;
1444 : : flags |= MPTCP_DSS_ACK64;
1445 : : } else {
1446 : 143660 : len += TCPOLEN_MPTCP_DSS_ACK32;
1447 : : }
1448 : : }
1449 : :
1450 [ + + ]: 1671488 : if (mpext->use_map) {
1451 : 912834 : len += TCPOLEN_MPTCP_DSS_MAP64;
1452 : :
1453 : : /* Use only 64-bit mapping flags for now, add
1454 : : * support for optional 32-bit mappings later.
1455 : : */
1456 : 912834 : flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
1457 [ + + ]: 912834 : if (mpext->data_fin)
1458 : 27250 : flags |= MPTCP_DSS_DATA_FIN;
1459 : :
1460 [ + + ]: 912834 : if (opts->csum_reqd)
1461 : 138470 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1462 : : }
1463 : :
1464 [ + - ]: 1671488 : *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
1465 : :
1466 [ + - ]: 1671488 : if (mpext->use_ack) {
1467 : 1671488 : struct mptcp_sock *msk;
1468 : 1671488 : u64 ack_seq;
1469 : :
1470 : : /* DSS option is set only by mptcp_established_options,
1471 : : * the caller is __tcp_transmit_skb() and ssk is always
1472 : : * not NULL.
1473 : : */
1474 [ - + ]: 1671488 : subflow = mptcp_subflow_ctx(ssk);
1475 [ - + ]: 1671488 : msk = mptcp_sk(subflow->conn);
1476 : 1671488 : ack_seq = READ_ONCE(msk->ack_seq);
1477 [ + + ]: 1671488 : if (mpext->ack64) {
1478 : 1527828 : put_unaligned_be64(ack_seq, ptr);
1479 : 1527828 : ptr += 2;
1480 : : } else {
1481 : 143660 : put_unaligned_be32(ack_seq, ptr);
1482 : 143660 : ptr += 1;
1483 : : }
1484 : 1671488 : subflow->rcv_wnd_sent = mptcp_set_rwin(msk, tp, th,
1485 : : ack_seq);
1486 : : }
1487 : :
1488 [ + + ]: 1671488 : if (mpext->use_map) {
1489 [ + + ]: 912834 : put_unaligned_be64(mpext->data_seq, ptr);
1490 : 912834 : ptr += 2;
1491 [ + + ]: 912834 : put_unaligned_be32(mpext->subflow_seq, ptr);
1492 : 912834 : ptr += 1;
1493 [ + + ]: 912834 : if (opts->csum_reqd) {
1494 : : /* data_len == 0 is reserved for the infinite mapping,
1495 : : * the checksum will also be set to 0.
1496 : : */
1497 : 138470 : put_len_csum(mpext->data_len,
1498 [ + + ]: 138470 : (mpext->data_len ? mptcp_make_csum(mpext) : 0),
1499 : : ptr);
1500 : : } else {
1501 : 774364 : put_unaligned_be32(mpext->data_len << 16 |
1502 : 774364 : TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1503 : : }
1504 : 912834 : ptr += 1;
1505 : : }
1506 : :
1507 : : /* We might need to add MP_FAIL options in rare cases */
1508 [ + + ]: 1671488 : if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
1509 : 4 : goto mp_fail;
1510 [ + + ]: 30713 : } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
1511 : 5795 : u8 len, flag = MPTCP_CAP_HMAC_SHA256;
1512 : :
1513 [ + + ]: 5795 : if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
1514 : : len = TCPOLEN_MPTCP_MPC_SYN;
1515 [ + + ]: 4022 : } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
1516 : : len = TCPOLEN_MPTCP_MPC_SYNACK;
1517 [ + + ]: 2352 : } else if (opts->data_len) {
1518 : 846 : len = TCPOLEN_MPTCP_MPC_ACK_DATA;
1519 [ + + ]: 846 : if (opts->csum_reqd)
1520 : 84 : len += TCPOLEN_MPTCP_DSS_CHECKSUM;
1521 : : } else {
1522 : : len = TCPOLEN_MPTCP_MPC_ACK;
1523 : : }
1524 : :
1525 [ + + ]: 5795 : if (opts->csum_reqd)
1526 : 459 : flag |= MPTCP_CAP_CHECKSUM_REQD;
1527 : :
1528 [ + + ]: 5795 : if (!opts->allow_join_id0)
1529 : 33 : flag |= MPTCP_CAP_DENY_JOIN_ID0;
1530 : :
1531 [ + + ]: 5795 : *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
1532 : : MPTCP_SUPPORTED_VERSION,
1533 : : flag);
1534 : :
1535 : 5795 : if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
1536 [ + + ]: 5795 : opts->suboptions))
1537 : 1773 : goto mp_capable_done;
1538 : :
1539 [ + + ]: 4022 : put_unaligned_be64(opts->sndr_key, ptr);
1540 : 4022 : ptr += 2;
1541 [ + + ]: 4022 : if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
1542 : 1670 : goto mp_capable_done;
1543 : :
1544 [ + + ]: 2352 : put_unaligned_be64(opts->rcvr_key, ptr);
1545 : 2352 : ptr += 2;
1546 [ + + ]: 2352 : if (!opts->data_len)
1547 : 1506 : goto mp_capable_done;
1548 : :
1549 [ + + ]: 846 : if (opts->csum_reqd) {
1550 : 84 : put_len_csum(opts->data_len,
1551 : 84 : __mptcp_make_csum(opts->data_seq,
1552 : : opts->subflow_seq,
1553 : : opts->data_len,
1554 : 84 : ~csum_unfold(opts->csum)),
1555 : : ptr);
1556 : : } else {
1557 : 762 : put_unaligned_be32(opts->data_len << 16 |
1558 : 762 : TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
1559 : : }
1560 : 846 : ptr += 1;
1561 : :
1562 : : /* MPC is additionally mutually exclusive with MP_PRIO */
1563 : 846 : goto mp_capable_done;
1564 [ + + ]: 24918 : } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
1565 [ + + ]: 2025 : if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
1566 : 730 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1567 : : TCPOLEN_MPTCP_MPJ_SYN,
1568 : 730 : opts->backup, opts->join_id);
1569 : 730 : put_unaligned_be32(opts->token, ptr);
1570 : 730 : ptr += 1;
1571 : 730 : put_unaligned_be32(opts->nonce, ptr);
1572 : 730 : ptr += 1;
1573 [ + + ]: 1295 : } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
1574 : 658 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1575 : : TCPOLEN_MPTCP_MPJ_SYNACK,
1576 : 658 : opts->backup, opts->join_id);
1577 : 658 : put_unaligned_be64(opts->thmac, ptr);
1578 : 658 : ptr += 2;
1579 : 658 : put_unaligned_be32(opts->nonce, ptr);
1580 : 658 : ptr += 1;
1581 : : } else {
1582 : 637 : *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
1583 : : TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
1584 : 637 : memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
1585 : 637 : ptr += 5;
1586 : : }
1587 [ + + ]: 22893 : } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
1588 : 898 : u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
1589 : 898 : u8 echo = MPTCP_ADDR_ECHO;
1590 : :
1591 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1592 [ + + ]: 898 : if (opts->addr.family == AF_INET6)
1593 : 208 : len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
1594 : : #endif
1595 : :
1596 [ + + ]: 898 : if (opts->addr.port)
1597 : 96 : len += TCPOLEN_MPTCP_PORT_LEN;
1598 : :
1599 [ + + ]: 898 : if (opts->ahmac) {
1600 : 488 : len += sizeof(opts->ahmac);
1601 : 488 : echo = 0;
1602 : : }
1603 : :
1604 : 898 : *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
1605 [ + + ]: 898 : len, echo, opts->addr.id);
1606 [ + + ]: 898 : if (opts->addr.family == AF_INET) {
1607 : 690 : memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
1608 : 690 : ptr += 1;
1609 : : }
1610 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
1611 [ + - ]: 208 : else if (opts->addr.family == AF_INET6) {
1612 : 208 : memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
1613 : 208 : ptr += 4;
1614 : : }
1615 : : #endif
1616 : :
1617 [ + + ]: 898 : if (!opts->addr.port) {
1618 [ + + ]: 802 : if (opts->ahmac) {
1619 : 434 : put_unaligned_be64(opts->ahmac, ptr);
1620 : 434 : ptr += 2;
1621 : : }
1622 : : } else {
1623 : 96 : u16 port = ntohs(opts->addr.port);
1624 : :
1625 [ + + ]: 96 : if (opts->ahmac) {
1626 : 54 : u8 *bptr = (u8 *)ptr;
1627 : :
1628 : 54 : put_unaligned_be16(port, bptr);
1629 : 54 : bptr += 2;
1630 : 54 : put_unaligned_be64(opts->ahmac, bptr);
1631 : 54 : bptr += 8;
1632 : 54 : put_unaligned_be16(TCPOPT_NOP << 8 |
1633 : : TCPOPT_NOP, bptr);
1634 : :
1635 : 54 : ptr += 3;
1636 : : } else {
1637 : 42 : put_unaligned_be32(port << 16 |
1638 : 42 : TCPOPT_NOP << 8 |
1639 : : TCPOPT_NOP, ptr);
1640 : 42 : ptr += 1;
1641 : : }
1642 : : }
1643 [ + + ]: 21995 : } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
1644 : : /* FASTCLOSE is mutually exclusive with others except RST */
1645 : 435 : *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
1646 : : TCPOLEN_MPTCP_FASTCLOSE,
1647 : : 0, 0);
1648 [ + - ]: 435 : put_unaligned_be64(opts->rcvr_key, ptr);
1649 : 435 : ptr += 2;
1650 : :
1651 [ + - ]: 435 : if (OPTION_MPTCP_RST & opts->suboptions)
1652 : 435 : goto mp_rst;
1653 : : return;
1654 [ + + ]: 21560 : } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
1655 : 2 : mp_fail:
1656 : : /* MP_FAIL is mutually exclusive with others except RST */
1657 [ + + ]: 6 : subflow = mptcp_subflow_ctx(ssk);
1658 : 6 : subflow->send_mp_fail = 0;
1659 : :
1660 : 6 : *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
1661 : : TCPOLEN_MPTCP_FAIL,
1662 : : 0, 0);
1663 [ + + ]: 6 : put_unaligned_be64(opts->fail_seq, ptr);
1664 : 6 : ptr += 2;
1665 : :
1666 [ + + ]: 6 : if (OPTION_MPTCP_RST & opts->suboptions)
1667 : 2 : goto mp_rst;
1668 : : return;
1669 [ + + ]: 21558 : } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
1670 : 58 : mp_rst:
1671 : 495 : *ptr++ = mptcp_option(MPTCPOPT_RST,
1672 : : TCPOLEN_MPTCP_RST,
1673 : 495 : opts->reset_transient,
1674 : 495 : opts->reset_reason);
1675 : 495 : return;
1676 [ + - ]: 21500 : } else if (unlikely(!opts->suboptions)) {
1677 : : /* Fallback to TCP */
1678 : 21500 : mptcp_track_rwin(tp);
1679 : 21500 : return;
1680 : : }
1681 : :
1682 [ + + ]: 1674407 : if (OPTION_MPTCP_PRIO & opts->suboptions) {
1683 [ + - ]: 28 : subflow = mptcp_subflow_ctx(ssk);
1684 : 28 : subflow->send_mp_prio = 0;
1685 : :
1686 : 28 : *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
1687 : : TCPOLEN_MPTCP_PRIO,
1688 [ + - ]: 28 : opts->backup, TCPOPT_NOP);
1689 : :
1690 [ + - ]: 28 : MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
1691 : : }
1692 : :
1693 : 1674379 : mp_capable_done:
1694 [ + + ]: 1680202 : if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
1695 : 108 : u8 i = 1;
1696 : :
1697 : 108 : *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
1698 : 108 : TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
1699 : 108 : 0, opts->rm_list.ids[0]);
1700 : :
1701 [ + + ]: 112 : while (i < opts->rm_list.nr) {
1702 : 4 : u8 id1, id2, id3, id4;
1703 : :
1704 : 4 : id1 = opts->rm_list.ids[i];
1705 [ + - ]: 4 : id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
1706 [ - + ]: 4 : id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
1707 [ - + ]: 4 : id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
1708 : 4 : put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
1709 : 4 : ptr += 1;
1710 : 4 : i += 4;
1711 : : }
1712 : : }
1713 : : }
1714 : :
1715 : 16 : __be32 mptcp_get_reset_option(const struct sk_buff *skb)
1716 : : {
1717 [ + - ]: 16 : const struct mptcp_ext *ext = mptcp_get_ext(skb);
1718 : 16 : u8 flags, reason;
1719 : :
1720 [ + - ]: 16 : if (ext) {
1721 : 16 : flags = ext->reset_transient;
1722 : 16 : reason = ext->reset_reason;
1723 : :
1724 : 16 : return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
1725 : : flags, reason);
1726 : : }
1727 : :
1728 : : return htonl(0u);
1729 : : }
1730 : : EXPORT_SYMBOL_GPL(mptcp_get_reset_option);
|