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