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