]>
Commit | Line | Data |
---|---|---|
39236c6e | 1 | /* |
5ba3f43e | 2 | * Copyright (c) 2012-2017 Apple Inc. All rights reserved. |
39236c6e A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | #include <sys/param.h> | |
29 | #include <sys/systm.h> | |
30 | #include <netinet/in_systm.h> | |
31 | #include <sys/socket.h> | |
32 | #include <sys/socketvar.h> | |
3e170ce0 | 33 | #include <sys/syslog.h> |
39236c6e A |
34 | #include <net/route.h> |
35 | #include <netinet/in.h> | |
36 | #include <net/if.h> | |
37 | ||
38 | #include <netinet/ip.h> | |
39 | #include <netinet/ip_var.h> | |
40 | #include <netinet/in_var.h> | |
41 | #include <netinet/tcp.h> | |
39037602 | 42 | #include <netinet/tcp_cache.h> |
39236c6e A |
43 | #include <netinet/tcp_seq.h> |
44 | #include <netinet/tcpip.h> | |
45 | #include <netinet/tcp_fsm.h> | |
46 | #include <netinet/mptcp_var.h> | |
47 | #include <netinet/mptcp.h> | |
48 | #include <netinet/mptcp_opt.h> | |
49 | #include <netinet/mptcp_seq.h> | |
50 | ||
51 | #include <libkern/crypto/sha1.h> | |
52 | #include <netinet/mptcp_timer.h> | |
53 | ||
54 | #include <mach/sdt.h> | |
55 | ||
56 | static int mptcp_validate_join_hmac(struct tcpcb *, u_char*, int); | |
57 | static int mptcp_snd_mpprio(struct tcpcb *tp, u_char *cp, int optlen); | |
5ba3f43e | 58 | static void mptcp_send_remaddr_opt(struct tcpcb *, struct mptcp_remaddr_opt *); |
39236c6e A |
59 | |
60 | /* | |
61 | * MPTCP Options Output Processing | |
62 | */ | |
63 | ||
64 | static unsigned | |
5ba3f43e | 65 | mptcp_setup_first_subflow_syn_opts(struct socket *so, u_char *opt, unsigned optlen) |
39236c6e | 66 | { |
5ba3f43e | 67 | struct mptcp_mpcapable_opt_common mptcp_opt; |
39236c6e | 68 | struct tcpcb *tp = sototcpcb(so); |
5ba3f43e A |
69 | struct mptcb *mp_tp = tptomptp(tp); |
70 | ||
71 | mpte_lock_assert_held(mp_tp->mpt_mpte); | |
39236c6e | 72 | |
39236c6e A |
73 | /* |
74 | * Avoid retransmitting the MP_CAPABLE option. | |
75 | */ | |
39037602 A |
76 | if (tp->t_rxtshift > mptcp_mpcap_retries) { |
77 | if (!(mp_tp->mpt_flags & (MPTCPF_FALLBACK_HEURISTIC | MPTCPF_HEURISTIC_TRAC))) { | |
78 | mp_tp->mpt_flags |= MPTCPF_HEURISTIC_TRAC; | |
79 | tcp_heuristic_mptcp_loss(tp); | |
80 | } | |
39236c6e | 81 | return (optlen); |
39037602 A |
82 | } |
83 | ||
84 | if (!tcp_heuristic_do_mptcp(tp)) { | |
85 | mp_tp->mpt_flags |= MPTCPF_FALLBACK_HEURISTIC; | |
86 | return (optlen); | |
87 | } | |
39236c6e | 88 | |
5ba3f43e | 89 | bzero(&mptcp_opt, sizeof (struct mptcp_mpcapable_opt_common)); |
39236c6e | 90 | |
5ba3f43e A |
91 | mptcp_opt.mmco_kind = TCPOPT_MULTIPATH; |
92 | mptcp_opt.mmco_len = | |
93 | sizeof (struct mptcp_mpcapable_opt_common) + | |
94 | sizeof (mptcp_key_t); | |
95 | mptcp_opt.mmco_subtype = MPO_CAPABLE; | |
96 | mptcp_opt.mmco_version = mp_tp->mpt_version; | |
97 | mptcp_opt.mmco_flags |= MPCAP_PROPOSAL_SBIT; | |
98 | if (mp_tp->mpt_flags & MPTCPF_CHECKSUM) | |
99 | mptcp_opt.mmco_flags |= MPCAP_CHECKSUM_CBIT; | |
100 | memcpy(opt + optlen, &mptcp_opt, sizeof (struct mptcp_mpcapable_opt_common)); | |
101 | optlen += sizeof (struct mptcp_mpcapable_opt_common); | |
102 | memcpy(opt + optlen, &mp_tp->mpt_localkey, sizeof (mptcp_key_t)); | |
103 | optlen += sizeof (mptcp_key_t); | |
39236c6e A |
104 | |
105 | return (optlen); | |
106 | } | |
107 | ||
108 | static unsigned | |
5ba3f43e | 109 | mptcp_setup_join_subflow_syn_opts(struct socket *so, u_char *opt, unsigned optlen) |
39236c6e | 110 | { |
5ba3f43e | 111 | struct mptcp_mpjoin_opt_req mpjoin_req; |
39236c6e A |
112 | struct inpcb *inp = sotoinpcb(so); |
113 | struct tcpcb *tp = NULL; | |
5ba3f43e | 114 | struct mptsub *mpts; |
39236c6e A |
115 | |
116 | if (!inp) | |
117 | return (optlen); | |
118 | ||
119 | tp = intotcpcb(inp); | |
120 | if (!tp) | |
121 | return (optlen); | |
122 | ||
5ba3f43e | 123 | mpts = tp->t_mpsub; |
39236c6e | 124 | |
5ba3f43e A |
125 | VERIFY(tptomptp(tp)); |
126 | mpte_lock_assert_held(tptomptp(tp)->mpt_mpte); | |
fe8ab488 | 127 | |
5ba3f43e A |
128 | bzero(&mpjoin_req, sizeof (mpjoin_req)); |
129 | mpjoin_req.mmjo_kind = TCPOPT_MULTIPATH; | |
130 | mpjoin_req.mmjo_len = sizeof (mpjoin_req); | |
131 | mpjoin_req.mmjo_subtype_bkp = MPO_JOIN << 4; | |
fe8ab488 | 132 | |
5ba3f43e A |
133 | if (tp->t_mpflags & TMPF_BACKUP_PATH) { |
134 | mpjoin_req.mmjo_subtype_bkp |= MPTCP_BACKUP; | |
135 | } else if (inp->inp_boundifp && IFNET_IS_CELLULAR(inp->inp_boundifp) && | |
136 | mpts->mpts_mpte->mpte_svctype != MPTCP_SVCTYPE_AGGREGATE) { | |
137 | mpjoin_req.mmjo_subtype_bkp |= MPTCP_BACKUP; | |
138 | tp->t_mpflags |= TMPF_BACKUP_PATH; | |
39236c6e | 139 | } else { |
5ba3f43e A |
140 | mpts->mpts_flags |= MPTSF_PREFERRED; |
141 | } | |
142 | ||
143 | mpjoin_req.mmjo_addr_id = tp->t_local_aid; | |
144 | mpjoin_req.mmjo_peer_token = tptomptp(tp)->mpt_remotetoken; | |
145 | if (mpjoin_req.mmjo_peer_token == 0) { | |
146 | mptcplog((LOG_DEBUG, "%s: peer token 0", __func__), | |
147 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); | |
39236c6e | 148 | } |
5ba3f43e A |
149 | mptcp_get_rands(tp->t_local_aid, tptomptp(tp), |
150 | &mpjoin_req.mmjo_rand, NULL); | |
151 | memcpy(opt + optlen, &mpjoin_req, mpjoin_req.mmjo_len); | |
152 | optlen += mpjoin_req.mmjo_len; | |
153 | ||
39236c6e A |
154 | return (optlen); |
155 | } | |
156 | ||
157 | unsigned | |
158 | mptcp_setup_join_ack_opts(struct tcpcb *tp, u_char *opt, unsigned optlen) | |
159 | { | |
160 | unsigned new_optlen; | |
161 | struct mptcp_mpjoin_opt_rsp2 join_rsp2; | |
162 | ||
163 | if ((MAX_TCPOPTLEN - optlen) < sizeof (struct mptcp_mpjoin_opt_rsp2)) { | |
164 | printf("%s: no space left %d \n", __func__, optlen); | |
165 | return (optlen); | |
166 | } | |
167 | ||
168 | bzero(&join_rsp2, sizeof (struct mptcp_mpjoin_opt_rsp2)); | |
169 | join_rsp2.mmjo_kind = TCPOPT_MULTIPATH; | |
170 | join_rsp2.mmjo_len = sizeof (struct mptcp_mpjoin_opt_rsp2); | |
171 | join_rsp2.mmjo_subtype = MPO_JOIN; | |
172 | mptcp_get_hmac(tp->t_local_aid, tptomptp(tp), | |
5ba3f43e | 173 | (u_char*)&join_rsp2.mmjo_mac); |
39236c6e A |
174 | memcpy(opt + optlen, &join_rsp2, join_rsp2.mmjo_len); |
175 | new_optlen = optlen + join_rsp2.mmjo_len; | |
176 | return (new_optlen); | |
177 | } | |
178 | ||
179 | unsigned | |
5ba3f43e | 180 | mptcp_setup_syn_opts(struct socket *so, u_char *opt, unsigned optlen) |
39236c6e A |
181 | { |
182 | unsigned new_optlen; | |
183 | ||
5ba3f43e A |
184 | if (!(so->so_flags & SOF_MP_SEC_SUBFLOW)) |
185 | new_optlen = mptcp_setup_first_subflow_syn_opts(so, opt, optlen); | |
186 | else | |
187 | new_optlen = mptcp_setup_join_subflow_syn_opts(so, opt, optlen); | |
188 | ||
39236c6e A |
189 | return (new_optlen); |
190 | } | |
191 | ||
192 | static int | |
193 | mptcp_send_mpfail(struct tcpcb *tp, u_char *opt, unsigned int optlen) | |
194 | { | |
195 | #pragma unused(tp, opt, optlen) | |
196 | ||
197 | struct mptcb *mp_tp = NULL; | |
198 | struct mptcp_mpfail_opt fail_opt; | |
199 | uint64_t dsn; | |
200 | int len = sizeof (struct mptcp_mpfail_opt); | |
201 | ||
202 | mp_tp = tptomptp(tp); | |
203 | if (mp_tp == NULL) { | |
204 | tp->t_mpflags &= ~TMPF_SND_MPFAIL; | |
205 | return (optlen); | |
206 | } | |
207 | ||
5ba3f43e A |
208 | mpte_lock_assert_held(mp_tp->mpt_mpte); |
209 | ||
39236c6e A |
210 | /* if option space low give up */ |
211 | if ((MAX_TCPOPTLEN - optlen) < sizeof (struct mptcp_mpfail_opt)) { | |
212 | tp->t_mpflags &= ~TMPF_SND_MPFAIL; | |
213 | return (optlen); | |
39037602 | 214 | } |
39236c6e | 215 | |
39236c6e | 216 | dsn = mp_tp->mpt_rcvnxt; |
39236c6e A |
217 | |
218 | bzero(&fail_opt, sizeof (fail_opt)); | |
219 | fail_opt.mfail_kind = TCPOPT_MULTIPATH; | |
220 | fail_opt.mfail_len = len; | |
221 | fail_opt.mfail_subtype = MPO_FAIL; | |
222 | fail_opt.mfail_dsn = mptcp_hton64(dsn); | |
223 | memcpy(opt + optlen, &fail_opt, len); | |
224 | optlen += len; | |
225 | tp->t_mpflags &= ~TMPF_SND_MPFAIL; | |
5ba3f43e | 226 | mptcplog((LOG_DEBUG, "%s: %d \n", __func__, |
39037602 | 227 | tp->t_local_aid), (MPTCP_SOCKET_DBG | MPTCP_SENDER_DBG), |
3e170ce0 | 228 | MPTCP_LOGLVL_LOG); |
39236c6e A |
229 | return (optlen); |
230 | } | |
231 | ||
232 | static int | |
233 | mptcp_send_infinite_mapping(struct tcpcb *tp, u_char *opt, unsigned int optlen) | |
234 | { | |
235 | struct mptcp_dsn_opt infin_opt; | |
236 | struct mptcb *mp_tp = NULL; | |
237 | size_t len = sizeof (struct mptcp_dsn_opt); | |
238 | struct socket *so = tp->t_inpcb->inp_socket; | |
39236c6e A |
239 | int csum_len = 0; |
240 | ||
241 | if (!so) | |
242 | return (optlen); | |
243 | ||
244 | mp_tp = tptomptp(tp); | |
245 | if (mp_tp == NULL) | |
246 | return (optlen); | |
247 | ||
5ba3f43e A |
248 | mpte_lock_assert_held(mp_tp->mpt_mpte); |
249 | ||
39236c6e A |
250 | if (mp_tp->mpt_flags & MPTCPF_CHECKSUM) |
251 | csum_len = 2; | |
252 | ||
253 | /* try later */ | |
5ba3f43e | 254 | if ((MAX_TCPOPTLEN - optlen) < (len + csum_len)) |
39236c6e | 255 | return (optlen); |
5ba3f43e | 256 | |
39236c6e A |
257 | bzero(&infin_opt, sizeof (infin_opt)); |
258 | infin_opt.mdss_copt.mdss_kind = TCPOPT_MULTIPATH; | |
259 | infin_opt.mdss_copt.mdss_len = len + csum_len; | |
260 | infin_opt.mdss_copt.mdss_subtype = MPO_DSS; | |
261 | infin_opt.mdss_copt.mdss_flags |= MDSS_M; | |
262 | if (mp_tp->mpt_flags & MPTCPF_RECVD_MPFAIL) { | |
263 | infin_opt.mdss_dsn = (u_int32_t) | |
264 | MPTCP_DATASEQ_LOW32(mp_tp->mpt_dsn_at_csum_fail); | |
fe8ab488 | 265 | infin_opt.mdss_subflow_seqn = mp_tp->mpt_ssn_at_csum_fail; |
39236c6e | 266 | } else { |
490019cf A |
267 | /* |
268 | * If MPTCP fallback happens, but TFO succeeds, the data on the | |
269 | * SYN does not belong to the MPTCP data sequence space. | |
270 | */ | |
271 | if ((tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED) && | |
272 | ((mp_tp->mpt_local_idsn + 1) == mp_tp->mpt_snduna)) { | |
273 | infin_opt.mdss_subflow_seqn = 1; | |
274 | ||
5ba3f43e A |
275 | mptcplog((LOG_DEBUG, "%s: idsn %llu snduna %llu \n", |
276 | __func__, mp_tp->mpt_local_idsn, | |
490019cf A |
277 | mp_tp->mpt_snduna), |
278 | (MPTCP_SOCKET_DBG | MPTCP_SENDER_DBG), | |
279 | MPTCP_LOGLVL_LOG); | |
280 | } else { | |
5ba3f43e | 281 | infin_opt.mdss_subflow_seqn = tp->snd_una - tp->t_mpsub->mpts_iss; |
490019cf | 282 | } |
39236c6e A |
283 | infin_opt.mdss_dsn = (u_int32_t) |
284 | MPTCP_DATASEQ_LOW32(mp_tp->mpt_snduna); | |
39236c6e | 285 | } |
5ba3f43e | 286 | |
39236c6e A |
287 | if ((infin_opt.mdss_dsn == 0) || (infin_opt.mdss_subflow_seqn == 0)) { |
288 | return (optlen); | |
289 | } | |
290 | infin_opt.mdss_dsn = htonl(infin_opt.mdss_dsn); | |
291 | infin_opt.mdss_subflow_seqn = htonl(infin_opt.mdss_subflow_seqn); | |
292 | infin_opt.mdss_data_len = 0; | |
293 | ||
294 | memcpy(opt + optlen, &infin_opt, len); | |
295 | optlen += len; | |
296 | if (csum_len != 0) { | |
297 | /* The checksum field is set to 0 for infinite mapping */ | |
298 | uint16_t csum = 0; | |
299 | memcpy(opt + optlen, &csum, csum_len); | |
300 | optlen += csum_len; | |
301 | } | |
302 | ||
5ba3f43e | 303 | mptcplog((LOG_DEBUG, "%s: dsn = %x, seq = %x len = %x\n", __func__, |
3e170ce0 A |
304 | ntohl(infin_opt.mdss_dsn), |
305 | ntohl(infin_opt.mdss_subflow_seqn), | |
306 | ntohs(infin_opt.mdss_data_len)), | |
307 | (MPTCP_SOCKET_DBG | MPTCP_SENDER_DBG), | |
308 | MPTCP_LOGLVL_LOG); | |
39236c6e | 309 | |
39236c6e A |
310 | tp->t_mpflags |= TMPF_INFIN_SENT; |
311 | tcpstat.tcps_estab_fallback++; | |
312 | return (optlen); | |
313 | } | |
314 | ||
315 | ||
316 | static int | |
317 | mptcp_ok_to_fin(struct tcpcb *tp, u_int64_t dsn, u_int32_t datalen) | |
318 | { | |
5ba3f43e A |
319 | struct mptcb *mp_tp = tptomptp(tp); |
320 | ||
321 | mpte_lock_assert_held(mp_tp->mpt_mpte); | |
39236c6e | 322 | |
39236c6e | 323 | dsn = (mp_tp->mpt_sndmax & MPTCP_DATASEQ_LOW32_MASK) | dsn; |
5ba3f43e | 324 | if ((dsn + datalen) == mp_tp->mpt_sndmax) |
39236c6e | 325 | return (1); |
5ba3f43e | 326 | |
39236c6e A |
327 | return (0); |
328 | } | |
329 | ||
39236c6e A |
330 | unsigned int |
331 | mptcp_setup_opts(struct tcpcb *tp, int32_t off, u_char *opt, | |
5ba3f43e A |
332 | unsigned int optlen, int flags, int len, |
333 | boolean_t *p_mptcp_acknow) | |
39236c6e A |
334 | { |
335 | struct inpcb *inp = (struct inpcb *)tp->t_inpcb; | |
336 | struct socket *so = inp->inp_socket; | |
337 | struct mptcb *mp_tp = tptomptp(tp); | |
338 | boolean_t do_csum = FALSE; | |
339 | boolean_t send_64bit_dsn = FALSE; | |
340 | boolean_t send_64bit_ack = FALSE; | |
5ba3f43e | 341 | u_int32_t old_mpt_flags = tp->t_mpflags & TMPF_MPTCP_SIGNALS; |
39236c6e | 342 | |
5ba3f43e | 343 | if (mptcp_enable == 0 || mp_tp == NULL || tp->t_state == TCPS_CLOSED) { |
39236c6e | 344 | /* do nothing */ |
fe8ab488 | 345 | goto ret_optlen; |
39236c6e A |
346 | } |
347 | ||
5ba3f43e A |
348 | mpte_lock_assert_held(mp_tp->mpt_mpte); |
349 | ||
fe8ab488 | 350 | if (mp_tp->mpt_flags & MPTCPF_CHECKSUM) { |
39236c6e | 351 | do_csum = TRUE; |
fe8ab488 | 352 | } |
39236c6e A |
353 | |
354 | /* tcp_output handles the SYN path separately */ | |
fe8ab488 A |
355 | if (flags & TH_SYN) { |
356 | goto ret_optlen; | |
357 | } | |
39236c6e A |
358 | |
359 | if ((MAX_TCPOPTLEN - optlen) < | |
360 | sizeof (struct mptcp_mpcapable_opt_common)) { | |
5ba3f43e A |
361 | mptcplog((LOG_ERR, "%s: no space left %d flags %x tp->t_mpflags %x len %d\n", |
362 | __func__, optlen, flags, tp->t_mpflags, len), | |
363 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); | |
fe8ab488 | 364 | goto ret_optlen; |
39236c6e A |
365 | } |
366 | ||
39236c6e A |
367 | if (tp->t_mpflags & TMPF_TCP_FALLBACK) { |
368 | if (tp->t_mpflags & TMPF_SND_MPFAIL) | |
369 | optlen = mptcp_send_mpfail(tp, opt, optlen); | |
370 | else if (!(tp->t_mpflags & TMPF_INFIN_SENT)) | |
371 | optlen = mptcp_send_infinite_mapping(tp, opt, optlen); | |
fe8ab488 | 372 | goto ret_optlen; |
39236c6e A |
373 | } |
374 | ||
5ba3f43e | 375 | if (tp->t_mpflags & TMPF_SND_KEYS) { |
39236c6e A |
376 | struct mptcp_mpcapable_opt_rsp1 mptcp_opt; |
377 | if ((MAX_TCPOPTLEN - optlen) < | |
378 | sizeof (struct mptcp_mpcapable_opt_rsp1)) | |
fe8ab488 | 379 | goto ret_optlen; |
39236c6e A |
380 | bzero(&mptcp_opt, sizeof (struct mptcp_mpcapable_opt_rsp1)); |
381 | mptcp_opt.mmc_common.mmco_kind = TCPOPT_MULTIPATH; | |
382 | mptcp_opt.mmc_common.mmco_len = | |
383 | sizeof (struct mptcp_mpcapable_opt_rsp1); | |
384 | mptcp_opt.mmc_common.mmco_subtype = MPO_CAPABLE; | |
3e170ce0 | 385 | mptcp_opt.mmc_common.mmco_version = mp_tp->mpt_version; |
39236c6e A |
386 | /* HMAC-SHA1 is the proposal */ |
387 | mptcp_opt.mmc_common.mmco_flags |= MPCAP_PROPOSAL_SBIT; | |
39236c6e A |
388 | if (mp_tp->mpt_flags & MPTCPF_CHECKSUM) |
389 | mptcp_opt.mmc_common.mmco_flags |= MPCAP_CHECKSUM_CBIT; | |
5ba3f43e A |
390 | mptcp_opt.mmc_localkey = mp_tp->mpt_localkey; |
391 | mptcp_opt.mmc_remotekey = mp_tp->mpt_remotekey; | |
39236c6e A |
392 | memcpy(opt + optlen, &mptcp_opt, mptcp_opt.mmc_common.mmco_len); |
393 | optlen += mptcp_opt.mmc_common.mmco_len; | |
5ba3f43e | 394 | tp->t_mpflags &= ~TMPF_SND_KEYS; |
39236c6e A |
395 | |
396 | if (!tp->t_mpuna) { | |
397 | tp->t_mpuna = tp->snd_una; | |
398 | } else { | |
399 | /* its a retransmission of the MP_CAPABLE ACK */ | |
400 | } | |
fe8ab488 | 401 | goto ret_optlen; |
39236c6e A |
402 | } |
403 | ||
5ba3f43e | 404 | if (tp->t_mpflags & TMPF_SND_JACK) { |
39236c6e A |
405 | /* Do the ACK part */ |
406 | optlen = mptcp_setup_join_ack_opts(tp, opt, optlen); | |
407 | if (!tp->t_mpuna) { | |
408 | tp->t_mpuna = tp->snd_una; | |
409 | } | |
410 | /* Start a timer to retransmit the ACK */ | |
411 | tp->t_timer[TCPT_JACK_RXMT] = | |
412 | OFFSET_FROM_START(tp, tcp_jack_rxmt); | |
5ba3f43e A |
413 | |
414 | tp->t_mpflags &= ~TMPF_SND_JACK; | |
fe8ab488 | 415 | goto ret_optlen; |
39236c6e A |
416 | } |
417 | ||
418 | if (!(tp->t_mpflags & TMPF_MPTCP_TRUE)) | |
fe8ab488 | 419 | goto ret_optlen; |
39037602 A |
420 | /* |
421 | * From here on, all options are sent only if MPTCP_TRUE | |
fe8ab488 A |
422 | * or when data is sent early on as in Fast Join |
423 | */ | |
39236c6e | 424 | |
39037602 A |
425 | if ((tp->t_mpflags & TMPF_MPTCP_TRUE) && |
426 | (tp->t_mpflags & TMPF_SND_REM_ADDR)) { | |
427 | int rem_opt_len = sizeof (struct mptcp_remaddr_opt); | |
428 | if ((optlen + rem_opt_len) <= MAX_TCPOPTLEN) { | |
429 | mptcp_send_remaddr_opt(tp, | |
430 | (struct mptcp_remaddr_opt *)(opt + optlen)); | |
431 | optlen += rem_opt_len; | |
432 | } else { | |
433 | tp->t_mpflags &= ~TMPF_SND_REM_ADDR; | |
434 | } | |
435 | } | |
436 | ||
437 | if (tp->t_mpflags & TMPF_SND_MPPRIO) { | |
438 | optlen = mptcp_snd_mpprio(tp, opt, optlen); | |
439 | } | |
440 | ||
5ba3f43e | 441 | if (mp_tp->mpt_flags & MPTCPF_SND_64BITDSN) { |
39236c6e A |
442 | send_64bit_dsn = TRUE; |
443 | } | |
39037602 | 444 | if (mp_tp->mpt_flags & MPTCPF_SND_64BITACK) |
39236c6e | 445 | send_64bit_ack = TRUE; |
39037602 | 446 | |
5ba3f43e A |
447 | #define CHECK_OPTLEN { \ |
448 | if ((MAX_TCPOPTLEN - optlen) < dssoptlen) { \ | |
449 | mptcplog((LOG_ERR, "%s: dssoptlen %d optlen %d \n", __func__, \ | |
450 | dssoptlen, optlen), \ | |
451 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); \ | |
452 | goto ret_optlen; \ | |
453 | } \ | |
39236c6e A |
454 | } |
455 | ||
456 | #define DO_FIN(dsn_opt) { \ | |
457 | int sndfin = 0; \ | |
5ba3f43e | 458 | sndfin = mptcp_ok_to_fin(tp, dsn_opt.mdss_dsn, len); \ |
39236c6e A |
459 | if (sndfin) { \ |
460 | dsn_opt.mdss_copt.mdss_flags |= MDSS_F; \ | |
39037602 | 461 | dsn_opt.mdss_data_len += 1; \ |
a39ff7e2 A |
462 | if (do_csum) \ |
463 | dss_csum = in_addword(dss_csum, 1); \ | |
39236c6e A |
464 | } \ |
465 | } | |
466 | ||
467 | #define CHECK_DATALEN { \ | |
468 | /* MPTCP socket does not support IP options */ \ | |
5ba3f43e A |
469 | if ((len + optlen + dssoptlen) > tp->t_maxopd) { \ |
470 | mptcplog((LOG_ERR, "%s: nosp %d len %d opt %d %d %d\n", \ | |
471 | __func__, len, dssoptlen, optlen, \ | |
3e170ce0 A |
472 | tp->t_maxseg, tp->t_maxopd), \ |
473 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); \ | |
39236c6e | 474 | /* remove option length from payload len */ \ |
5ba3f43e | 475 | len = tp->t_maxopd - optlen - dssoptlen; \ |
39236c6e A |
476 | } \ |
477 | } | |
478 | ||
479 | if ((tp->t_mpflags & TMPF_SEND_DSN) && | |
480 | (send_64bit_dsn)) { | |
481 | /* | |
482 | * If there was the need to send 64-bit Data ACK along | |
483 | * with 64-bit DSN, then 26 or 28 bytes would be used. | |
484 | * With timestamps and NOOP padding that will cause | |
485 | * overflow. Hence, in the rare event that both 64-bit | |
486 | * DSN and 64-bit ACK have to be sent, delay the send of | |
487 | * 64-bit ACK until our 64-bit DSN is acked with a 64-bit ack. | |
488 | * XXX If this delay causes issue, remove the 2-byte padding. | |
489 | */ | |
490 | struct mptcp_dss64_ack32_opt dsn_ack_opt; | |
5ba3f43e A |
491 | unsigned int dssoptlen = sizeof (dsn_ack_opt); |
492 | uint16_t dss_csum; | |
39236c6e A |
493 | |
494 | if (do_csum) { | |
5ba3f43e | 495 | dssoptlen += 2; |
39236c6e A |
496 | } |
497 | ||
498 | CHECK_OPTLEN; | |
499 | ||
500 | bzero(&dsn_ack_opt, sizeof (dsn_ack_opt)); | |
501 | dsn_ack_opt.mdss_copt.mdss_kind = TCPOPT_MULTIPATH; | |
502 | dsn_ack_opt.mdss_copt.mdss_subtype = MPO_DSS; | |
5ba3f43e | 503 | dsn_ack_opt.mdss_copt.mdss_len = dssoptlen; |
39236c6e A |
504 | dsn_ack_opt.mdss_copt.mdss_flags |= |
505 | MDSS_M | MDSS_m | MDSS_A; | |
506 | ||
507 | CHECK_DATALEN; | |
508 | ||
5ba3f43e A |
509 | mptcp_output_getm_dsnmap64(so, off, |
510 | &dsn_ack_opt.mdss_dsn, | |
511 | &dsn_ack_opt.mdss_subflow_seqn, | |
512 | &dsn_ack_opt.mdss_data_len, | |
513 | &dss_csum); | |
39236c6e A |
514 | |
515 | if ((dsn_ack_opt.mdss_data_len == 0) || | |
516 | (dsn_ack_opt.mdss_dsn == 0)) { | |
fe8ab488 | 517 | goto ret_optlen; |
39236c6e A |
518 | } |
519 | ||
520 | if (tp->t_mpflags & TMPF_SEND_DFIN) { | |
521 | DO_FIN(dsn_ack_opt); | |
522 | } | |
523 | ||
39236c6e A |
524 | dsn_ack_opt.mdss_ack = |
525 | htonl(MPTCP_DATAACK_LOW32(mp_tp->mpt_rcvnxt)); | |
39236c6e A |
526 | |
527 | dsn_ack_opt.mdss_dsn = mptcp_hton64(dsn_ack_opt.mdss_dsn); | |
528 | dsn_ack_opt.mdss_subflow_seqn = htonl( | |
529 | dsn_ack_opt.mdss_subflow_seqn); | |
530 | dsn_ack_opt.mdss_data_len = htons( | |
531 | dsn_ack_opt.mdss_data_len); | |
39236c6e A |
532 | |
533 | memcpy(opt + optlen, &dsn_ack_opt, sizeof (dsn_ack_opt)); | |
5ba3f43e A |
534 | if (do_csum) |
535 | *((uint16_t *)(void *)(opt + optlen + sizeof (dsn_ack_opt))) = dss_csum; | |
39236c6e | 536 | |
5ba3f43e A |
537 | optlen += dssoptlen; |
538 | mptcplog((LOG_DEBUG,"%s: long DSS = %llx ACK = %llx \n", __func__, | |
3e170ce0 A |
539 | mptcp_ntoh64(dsn_ack_opt.mdss_dsn), |
540 | mptcp_ntoh64(dsn_ack_opt.mdss_ack)), | |
541 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_LOG); | |
39037602 | 542 | |
39236c6e | 543 | tp->t_mpflags &= ~TMPF_MPTCP_ACKNOW; |
fe8ab488 | 544 | goto ret_optlen; |
39236c6e A |
545 | } |
546 | ||
547 | if ((tp->t_mpflags & TMPF_SEND_DSN) && | |
548 | (!send_64bit_dsn) && | |
549 | !(tp->t_mpflags & TMPF_MPTCP_ACKNOW)) { | |
550 | struct mptcp_dsn_opt dsn_opt; | |
5ba3f43e A |
551 | unsigned int dssoptlen = sizeof (struct mptcp_dsn_opt); |
552 | uint16_t dss_csum; | |
39236c6e A |
553 | |
554 | if (do_csum) { | |
5ba3f43e | 555 | dssoptlen += 2; |
39236c6e A |
556 | } |
557 | ||
558 | CHECK_OPTLEN; | |
559 | ||
560 | bzero(&dsn_opt, sizeof (dsn_opt)); | |
561 | dsn_opt.mdss_copt.mdss_kind = TCPOPT_MULTIPATH; | |
562 | dsn_opt.mdss_copt.mdss_subtype = MPO_DSS; | |
5ba3f43e | 563 | dsn_opt.mdss_copt.mdss_len = dssoptlen; |
39236c6e A |
564 | dsn_opt.mdss_copt.mdss_flags |= MDSS_M; |
565 | ||
566 | CHECK_DATALEN; | |
567 | ||
5ba3f43e A |
568 | mptcp_output_getm_dsnmap32(so, off, &dsn_opt.mdss_dsn, |
569 | &dsn_opt.mdss_subflow_seqn, | |
570 | &dsn_opt.mdss_data_len, | |
571 | &dss_csum); | |
39236c6e A |
572 | |
573 | if ((dsn_opt.mdss_data_len == 0) || | |
574 | (dsn_opt.mdss_dsn == 0)) { | |
fe8ab488 | 575 | goto ret_optlen; |
39236c6e A |
576 | } |
577 | ||
578 | if (tp->t_mpflags & TMPF_SEND_DFIN) { | |
579 | DO_FIN(dsn_opt); | |
580 | } | |
581 | ||
582 | dsn_opt.mdss_dsn = htonl(dsn_opt.mdss_dsn); | |
583 | dsn_opt.mdss_subflow_seqn = htonl(dsn_opt.mdss_subflow_seqn); | |
584 | dsn_opt.mdss_data_len = htons(dsn_opt.mdss_data_len); | |
39236c6e | 585 | memcpy(opt + optlen, &dsn_opt, sizeof (dsn_opt)); |
5ba3f43e A |
586 | if (do_csum) |
587 | *((uint16_t *)(void *)(opt + optlen + sizeof (dsn_opt))) = dss_csum; | |
588 | ||
589 | optlen += dssoptlen; | |
39236c6e | 590 | tp->t_mpflags &= ~TMPF_MPTCP_ACKNOW; |
fe8ab488 | 591 | goto ret_optlen; |
39236c6e A |
592 | } |
593 | ||
594 | /* 32-bit Data ACK option */ | |
595 | if ((tp->t_mpflags & TMPF_MPTCP_ACKNOW) && | |
596 | (!send_64bit_ack) && | |
597 | !(tp->t_mpflags & TMPF_SEND_DSN) && | |
598 | !(tp->t_mpflags & TMPF_SEND_DFIN)) { | |
599 | ||
600 | struct mptcp_data_ack_opt dack_opt; | |
5ba3f43e | 601 | unsigned int dssoptlen = 0; |
39236c6e | 602 | do_ack32_only: |
5ba3f43e | 603 | dssoptlen = sizeof (dack_opt); |
39236c6e A |
604 | |
605 | CHECK_OPTLEN; | |
606 | ||
5ba3f43e | 607 | bzero(&dack_opt, dssoptlen); |
39236c6e | 608 | dack_opt.mdss_copt.mdss_kind = TCPOPT_MULTIPATH; |
5ba3f43e | 609 | dack_opt.mdss_copt.mdss_len = dssoptlen; |
39236c6e A |
610 | dack_opt.mdss_copt.mdss_subtype = MPO_DSS; |
611 | dack_opt.mdss_copt.mdss_flags |= MDSS_A; | |
39236c6e A |
612 | dack_opt.mdss_ack = |
613 | htonl(MPTCP_DATAACK_LOW32(mp_tp->mpt_rcvnxt)); | |
5ba3f43e A |
614 | memcpy(opt + optlen, &dack_opt, dssoptlen); |
615 | optlen += dssoptlen; | |
39236c6e A |
616 | VERIFY(optlen <= MAX_TCPOPTLEN); |
617 | tp->t_mpflags &= ~TMPF_MPTCP_ACKNOW; | |
fe8ab488 | 618 | goto ret_optlen; |
39236c6e A |
619 | } |
620 | ||
621 | /* 64-bit Data ACK option */ | |
622 | if ((tp->t_mpflags & TMPF_MPTCP_ACKNOW) && | |
623 | (send_64bit_ack) && | |
624 | !(tp->t_mpflags & TMPF_SEND_DSN) && | |
625 | !(tp->t_mpflags & TMPF_SEND_DFIN)) { | |
626 | struct mptcp_data_ack64_opt dack_opt; | |
5ba3f43e | 627 | unsigned int dssoptlen = 0; |
39236c6e | 628 | do_ack64_only: |
5ba3f43e | 629 | dssoptlen = sizeof (dack_opt); |
39236c6e A |
630 | |
631 | CHECK_OPTLEN; | |
632 | ||
5ba3f43e | 633 | bzero(&dack_opt, dssoptlen); |
39236c6e | 634 | dack_opt.mdss_copt.mdss_kind = TCPOPT_MULTIPATH; |
5ba3f43e | 635 | dack_opt.mdss_copt.mdss_len = dssoptlen; |
39236c6e A |
636 | dack_opt.mdss_copt.mdss_subtype = MPO_DSS; |
637 | dack_opt.mdss_copt.mdss_flags |= (MDSS_A | MDSS_a); | |
39236c6e A |
638 | dack_opt.mdss_ack = mptcp_hton64(mp_tp->mpt_rcvnxt); |
639 | /* | |
640 | * The other end should retransmit 64-bit DSN until it | |
641 | * receives a 64-bit ACK. | |
642 | */ | |
643 | mp_tp->mpt_flags &= ~MPTCPF_SND_64BITACK; | |
5ba3f43e A |
644 | memcpy(opt + optlen, &dack_opt, dssoptlen); |
645 | optlen += dssoptlen; | |
39236c6e A |
646 | VERIFY(optlen <= MAX_TCPOPTLEN); |
647 | tp->t_mpflags &= ~TMPF_MPTCP_ACKNOW; | |
fe8ab488 | 648 | goto ret_optlen; |
39236c6e A |
649 | } |
650 | ||
651 | /* 32-bit DSS+Data ACK option */ | |
652 | if ((tp->t_mpflags & TMPF_SEND_DSN) && | |
653 | (!send_64bit_dsn) && | |
654 | (!send_64bit_ack) && | |
655 | (tp->t_mpflags & TMPF_MPTCP_ACKNOW)) { | |
656 | struct mptcp_dss_ack_opt dss_ack_opt; | |
5ba3f43e A |
657 | unsigned int dssoptlen = sizeof (dss_ack_opt); |
658 | uint16_t dss_csum; | |
39236c6e A |
659 | |
660 | if (do_csum) | |
5ba3f43e | 661 | dssoptlen += 2; |
39236c6e A |
662 | |
663 | CHECK_OPTLEN; | |
664 | ||
665 | bzero(&dss_ack_opt, sizeof (dss_ack_opt)); | |
666 | dss_ack_opt.mdss_copt.mdss_kind = TCPOPT_MULTIPATH; | |
5ba3f43e | 667 | dss_ack_opt.mdss_copt.mdss_len = dssoptlen; |
39236c6e A |
668 | dss_ack_opt.mdss_copt.mdss_subtype = MPO_DSS; |
669 | dss_ack_opt.mdss_copt.mdss_flags |= MDSS_A | MDSS_M; | |
39236c6e A |
670 | dss_ack_opt.mdss_ack = |
671 | htonl(MPTCP_DATAACK_LOW32(mp_tp->mpt_rcvnxt)); | |
39236c6e A |
672 | |
673 | CHECK_DATALEN; | |
674 | ||
5ba3f43e A |
675 | mptcp_output_getm_dsnmap32(so, off, &dss_ack_opt.mdss_dsn, |
676 | &dss_ack_opt.mdss_subflow_seqn, | |
677 | &dss_ack_opt.mdss_data_len, | |
678 | &dss_csum); | |
39236c6e A |
679 | |
680 | if ((dss_ack_opt.mdss_data_len == 0) || | |
681 | (dss_ack_opt.mdss_dsn == 0)) { | |
682 | goto do_ack32_only; | |
683 | } | |
684 | ||
685 | if (tp->t_mpflags & TMPF_SEND_DFIN) { | |
686 | DO_FIN(dss_ack_opt); | |
687 | } | |
688 | ||
689 | dss_ack_opt.mdss_dsn = htonl(dss_ack_opt.mdss_dsn); | |
690 | dss_ack_opt.mdss_subflow_seqn = | |
691 | htonl(dss_ack_opt.mdss_subflow_seqn); | |
692 | dss_ack_opt.mdss_data_len = htons(dss_ack_opt.mdss_data_len); | |
39236c6e | 693 | memcpy(opt + optlen, &dss_ack_opt, sizeof (dss_ack_opt)); |
5ba3f43e A |
694 | if (do_csum) |
695 | *((uint16_t *)(void *)(opt + optlen + sizeof (dss_ack_opt))) = dss_csum; | |
39236c6e | 696 | |
5ba3f43e | 697 | optlen += dssoptlen; |
39236c6e A |
698 | |
699 | if (optlen > MAX_TCPOPTLEN) | |
700 | panic("optlen too large"); | |
701 | tp->t_mpflags &= ~TMPF_MPTCP_ACKNOW; | |
fe8ab488 | 702 | goto ret_optlen; |
39236c6e A |
703 | } |
704 | ||
705 | /* 32-bit DSS + 64-bit DACK option */ | |
706 | if ((tp->t_mpflags & TMPF_SEND_DSN) && | |
707 | (!send_64bit_dsn) && | |
708 | (send_64bit_ack) && | |
709 | (tp->t_mpflags & TMPF_MPTCP_ACKNOW)) { | |
710 | struct mptcp_dss32_ack64_opt dss_ack_opt; | |
5ba3f43e A |
711 | unsigned int dssoptlen = sizeof (dss_ack_opt); |
712 | uint16_t dss_csum; | |
39236c6e A |
713 | |
714 | if (do_csum) | |
5ba3f43e | 715 | dssoptlen += 2; |
39236c6e A |
716 | |
717 | CHECK_OPTLEN; | |
718 | ||
719 | bzero(&dss_ack_opt, sizeof (dss_ack_opt)); | |
720 | dss_ack_opt.mdss_copt.mdss_kind = TCPOPT_MULTIPATH; | |
5ba3f43e | 721 | dss_ack_opt.mdss_copt.mdss_len = dssoptlen; |
39236c6e A |
722 | dss_ack_opt.mdss_copt.mdss_subtype = MPO_DSS; |
723 | dss_ack_opt.mdss_copt.mdss_flags |= MDSS_M | MDSS_A | MDSS_a; | |
39236c6e A |
724 | dss_ack_opt.mdss_ack = |
725 | mptcp_hton64(mp_tp->mpt_rcvnxt); | |
39236c6e A |
726 | |
727 | CHECK_DATALEN; | |
728 | ||
5ba3f43e A |
729 | mptcp_output_getm_dsnmap32(so, off, &dss_ack_opt.mdss_dsn, |
730 | &dss_ack_opt.mdss_subflow_seqn, | |
731 | &dss_ack_opt.mdss_data_len, | |
732 | &dss_csum); | |
39236c6e A |
733 | |
734 | if ((dss_ack_opt.mdss_data_len == 0) || | |
735 | (dss_ack_opt.mdss_dsn == 0)) { | |
736 | goto do_ack64_only; | |
737 | } | |
738 | ||
739 | if (tp->t_mpflags & TMPF_SEND_DFIN) { | |
740 | DO_FIN(dss_ack_opt); | |
741 | } | |
742 | ||
743 | dss_ack_opt.mdss_dsn = htonl(dss_ack_opt.mdss_dsn); | |
744 | dss_ack_opt.mdss_subflow_seqn = | |
745 | htonl(dss_ack_opt.mdss_subflow_seqn); | |
746 | dss_ack_opt.mdss_data_len = htons(dss_ack_opt.mdss_data_len); | |
39236c6e | 747 | memcpy(opt + optlen, &dss_ack_opt, sizeof (dss_ack_opt)); |
5ba3f43e A |
748 | if (do_csum) |
749 | *((uint16_t *)(void *)(opt + optlen + sizeof (dss_ack_opt))) = dss_csum; | |
39236c6e | 750 | |
5ba3f43e | 751 | optlen += dssoptlen; |
39236c6e A |
752 | |
753 | if (optlen > MAX_TCPOPTLEN) | |
754 | panic("optlen too large"); | |
755 | tp->t_mpflags &= ~TMPF_MPTCP_ACKNOW; | |
fe8ab488 | 756 | goto ret_optlen; |
39236c6e A |
757 | } |
758 | ||
759 | if (tp->t_mpflags & TMPF_SEND_DFIN) { | |
a39ff7e2 | 760 | unsigned int dssoptlen = sizeof(struct mptcp_dss_ack_opt); |
39236c6e | 761 | struct mptcp_dss_ack_opt dss_ack_opt; |
a39ff7e2 A |
762 | uint16_t dss_csum; |
763 | ||
764 | if (do_csum) { | |
765 | uint64_t dss_val = mptcp_hton64(mp_tp->mpt_sndmax - 1); | |
766 | uint16_t dlen = htons(1); | |
767 | uint32_t sseq = 0; | |
768 | uint32_t sum; | |
769 | ||
39236c6e | 770 | |
5ba3f43e | 771 | dssoptlen += 2; |
39236c6e | 772 | |
a39ff7e2 A |
773 | sum = in_pseudo64(dss_val, sseq, dlen); |
774 | ADDCARRY(sum); | |
775 | dss_csum = ~sum & 0xffff; | |
776 | } | |
777 | ||
39236c6e A |
778 | CHECK_OPTLEN; |
779 | ||
780 | bzero(&dss_ack_opt, sizeof (dss_ack_opt)); | |
781 | ||
fe8ab488 A |
782 | /* |
783 | * Data FIN occupies one sequence space. | |
784 | * Don't send it if it has been Acked. | |
785 | */ | |
a39ff7e2 | 786 | if ((mp_tp->mpt_sndnxt + 1 != mp_tp->mpt_sndmax) || |
5ba3f43e | 787 | (mp_tp->mpt_snduna == mp_tp->mpt_sndmax)) |
fe8ab488 | 788 | goto ret_optlen; |
39236c6e A |
789 | |
790 | dss_ack_opt.mdss_copt.mdss_kind = TCPOPT_MULTIPATH; | |
5ba3f43e | 791 | dss_ack_opt.mdss_copt.mdss_len = dssoptlen; |
39236c6e A |
792 | dss_ack_opt.mdss_copt.mdss_subtype = MPO_DSS; |
793 | dss_ack_opt.mdss_copt.mdss_flags |= MDSS_A | MDSS_M | MDSS_F; | |
794 | dss_ack_opt.mdss_ack = | |
795 | htonl(MPTCP_DATAACK_LOW32(mp_tp->mpt_rcvnxt)); | |
796 | dss_ack_opt.mdss_dsn = | |
a39ff7e2 | 797 | htonl(MPTCP_DATASEQ_LOW32(mp_tp->mpt_sndmax - 1)); |
39236c6e A |
798 | dss_ack_opt.mdss_subflow_seqn = 0; |
799 | dss_ack_opt.mdss_data_len = 1; | |
800 | dss_ack_opt.mdss_data_len = htons(dss_ack_opt.mdss_data_len); | |
801 | memcpy(opt + optlen, &dss_ack_opt, sizeof (dss_ack_opt)); | |
a39ff7e2 A |
802 | if (do_csum) |
803 | *((uint16_t *)(void *)(opt + optlen + sizeof (dss_ack_opt))) = dss_csum; | |
804 | ||
5ba3f43e | 805 | optlen += dssoptlen; |
39236c6e A |
806 | } |
807 | ||
fe8ab488 A |
808 | ret_optlen: |
809 | if (TRUE == *p_mptcp_acknow ) { | |
810 | VERIFY(old_mpt_flags != 0); | |
5ba3f43e | 811 | u_int32_t new_mpt_flags = tp->t_mpflags & TMPF_MPTCP_SIGNALS; |
fe8ab488 A |
812 | |
813 | /* | |
814 | * If none of the above mpflags were acted on by | |
815 | * this routine, reset these flags and set p_mptcp_acknow | |
816 | * to false. | |
5ba3f43e | 817 | * |
39037602 | 818 | * XXX The reset value of p_mptcp_acknow can be used |
fe8ab488 A |
819 | * to communicate tcp_output to NOT send a pure ack without any |
820 | * MPTCP options as it will be treated as a dup ack. | |
821 | * Since the instances of mptcp_setup_opts not acting on | |
822 | * these options are mostly corner cases and sending a dup | |
823 | * ack here would only have an impact if the system | |
824 | * has sent consecutive dup acks before this false one, | |
825 | * we haven't modified the logic in tcp_output to avoid | |
826 | * that. | |
827 | */ | |
5ba3f43e A |
828 | if (old_mpt_flags == new_mpt_flags) { |
829 | tp->t_mpflags &= ~TMPF_MPTCP_SIGNALS; | |
fe8ab488 | 830 | *p_mptcp_acknow = FALSE; |
5ba3f43e A |
831 | mptcplog((LOG_DEBUG, "%s: no action \n", __func__), |
832 | MPTCP_SENDER_DBG, MPTCP_LOGLVL_LOG); | |
3e170ce0 | 833 | } else { |
5ba3f43e A |
834 | mptcplog((LOG_DEBUG, "%s: acknow set, old flags %x new flags %x \n", |
835 | __func__, old_mpt_flags, new_mpt_flags), | |
3e170ce0 | 836 | MPTCP_SENDER_DBG, MPTCP_LOGLVL_LOG); |
fe8ab488 A |
837 | } |
838 | } | |
839 | ||
840 | return optlen; | |
39236c6e A |
841 | } |
842 | ||
843 | /* | |
844 | * MPTCP Options Input Processing | |
845 | */ | |
846 | ||
3e170ce0 A |
847 | static int |
848 | mptcp_sanitize_option(struct tcpcb *tp, int mptcp_subtype) | |
849 | { | |
850 | struct mptcb *mp_tp = tptomptp(tp); | |
851 | int ret = 1; | |
852 | ||
853 | if (mp_tp == NULL) { | |
5ba3f43e A |
854 | mptcplog((LOG_ERR, "%s: NULL mpsocket \n", __func__), |
855 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); | |
3e170ce0 A |
856 | return (0); |
857 | } | |
858 | ||
859 | switch (mptcp_subtype) { | |
860 | case MPO_CAPABLE: | |
861 | break; | |
862 | case MPO_JOIN: /* fall through */ | |
863 | case MPO_DSS: /* fall through */ | |
864 | case MPO_FASTCLOSE: /* fall through */ | |
865 | case MPO_FAIL: /* fall through */ | |
866 | case MPO_REMOVE_ADDR: /* fall through */ | |
867 | case MPO_ADD_ADDR: /* fall through */ | |
868 | case MPO_PRIO: /* fall through */ | |
869 | if (mp_tp->mpt_state < MPTCPS_ESTABLISHED) | |
870 | ret = 0; | |
871 | break; | |
872 | default: | |
873 | ret = 0; | |
5ba3f43e | 874 | mptcplog((LOG_ERR, "%s: type = %d \n", __func__, |
3e170ce0 A |
875 | mptcp_subtype), |
876 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); | |
877 | break; | |
878 | } | |
879 | return (ret); | |
880 | } | |
39236c6e A |
881 | |
882 | static int | |
3e170ce0 | 883 | mptcp_valid_mpcapable_common_opt(u_char *cp) |
39236c6e A |
884 | { |
885 | struct mptcp_mpcapable_opt_common *rsp = | |
886 | (struct mptcp_mpcapable_opt_common *)cp; | |
887 | ||
888 | /* mmco_kind, mmco_len and mmco_subtype are validated before */ | |
889 | ||
39236c6e A |
890 | if (!(rsp->mmco_flags & MPCAP_PROPOSAL_SBIT)) |
891 | return (0); | |
892 | ||
893 | if (rsp->mmco_flags & (MPCAP_BBIT | MPCAP_CBIT | MPCAP_DBIT | | |
894 | MPCAP_EBIT | MPCAP_FBIT | MPCAP_GBIT)) | |
895 | return (0); | |
896 | ||
897 | return (1); | |
898 | } | |
899 | ||
900 | ||
901 | static void | |
902 | mptcp_do_mpcapable_opt(struct tcpcb *tp, u_char *cp, struct tcphdr *th, | |
903 | int optlen) | |
904 | { | |
39236c6e A |
905 | struct mptcp_mpcapable_opt_rsp *rsp = NULL; |
906 | struct mptcb *mp_tp = tptomptp(tp); | |
907 | ||
5ba3f43e A |
908 | mpte_lock_assert_held(mp_tp->mpt_mpte); |
909 | ||
39037602 A |
910 | /* Only valid on SYN/ACK */ |
911 | if ((th->th_flags & (TH_SYN | TH_ACK)) != (TH_SYN | TH_ACK)) | |
912 | return; | |
39236c6e | 913 | |
39236c6e | 914 | /* Validate the kind, len, flags */ |
3e170ce0 | 915 | if (mptcp_valid_mpcapable_common_opt(cp) != 1) { |
39236c6e A |
916 | tcpstat.tcps_invalid_mpcap++; |
917 | return; | |
918 | } | |
919 | ||
39037602 | 920 | /* handle SYN/ACK retransmission by acknowledging with ACK */ |
d190cdc3 | 921 | if (mp_tp->mpt_state >= MPTCPS_ESTABLISHED) |
39037602 | 922 | return; |
3e170ce0 | 923 | |
39037602 A |
924 | /* A SYN/ACK contains peer's key and flags */ |
925 | if (optlen != sizeof (struct mptcp_mpcapable_opt_rsp)) { | |
926 | /* complain */ | |
5ba3f43e | 927 | mptcplog((LOG_ERR, "%s: SYN_ACK optlen = %d, sizeof mp opt = %lu \n", |
39037602 A |
928 | __func__, optlen, |
929 | sizeof (struct mptcp_mpcapable_opt_rsp)), | |
930 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); | |
931 | tcpstat.tcps_invalid_mpcap++; | |
932 | return; | |
933 | } | |
39236c6e | 934 | |
39037602 A |
935 | /* |
936 | * If checksum flag is set, enable MPTCP checksum, even if | |
937 | * it was not negotiated on the first SYN. | |
938 | */ | |
939 | if (((struct mptcp_mpcapable_opt_common *)cp)->mmco_flags & | |
940 | MPCAP_CHECKSUM_CBIT) | |
941 | mp_tp->mpt_flags |= MPTCPF_CHECKSUM; | |
39236c6e | 942 | |
39037602 | 943 | rsp = (struct mptcp_mpcapable_opt_rsp *)cp; |
39037602 A |
944 | mp_tp->mpt_remotekey = rsp->mmc_localkey; |
945 | /* For now just downgrade to the peer's version */ | |
946 | mp_tp->mpt_peer_version = rsp->mmc_common.mmco_version; | |
947 | if (rsp->mmc_common.mmco_version < mp_tp->mpt_version) { | |
948 | mp_tp->mpt_version = rsp->mmc_common.mmco_version; | |
949 | tcpstat.tcps_mp_verdowngrade++; | |
950 | } | |
951 | if (mptcp_init_remote_parms(mp_tp) != 0) { | |
952 | tcpstat.tcps_invalid_mpcap++; | |
39037602 | 953 | return; |
39236c6e | 954 | } |
39037602 | 955 | tcp_heuristic_mptcp_success(tp); |
5ba3f43e | 956 | tp->t_mpflags |= (TMPF_SND_KEYS | TMPF_MPTCP_TRUE); |
39236c6e A |
957 | } |
958 | ||
959 | ||
960 | static void | |
961 | mptcp_do_mpjoin_opt(struct tcpcb *tp, u_char *cp, struct tcphdr *th, int optlen) | |
962 | { | |
963 | #define MPTCP_JOPT_ERROR_PATH(tp) { \ | |
964 | tp->t_mpflags |= TMPF_RESET; \ | |
965 | tcpstat.tcps_invalid_joins++; \ | |
966 | if (tp->t_inpcb->inp_socket != NULL) { \ | |
967 | soevent(tp->t_inpcb->inp_socket, \ | |
968 | SO_FILT_HINT_LOCKED | SO_FILT_HINT_MUSTRST); \ | |
969 | } \ | |
970 | } | |
971 | int error = 0; | |
39037602 A |
972 | struct mptcp_mpjoin_opt_rsp *join_rsp = |
973 | (struct mptcp_mpjoin_opt_rsp *)cp; | |
39236c6e | 974 | |
39037602 A |
975 | /* Only valid on SYN/ACK */ |
976 | if ((th->th_flags & (TH_SYN | TH_ACK)) != (TH_SYN | TH_ACK)) | |
39236c6e | 977 | return; |
39236c6e | 978 | |
39037602 | 979 | if (optlen != sizeof (struct mptcp_mpjoin_opt_rsp)) { |
5ba3f43e A |
980 | mptcplog((LOG_ERR, "%s: SYN_ACK: unexpected optlen = %d mp " |
981 | "option = %lu\n", __func__, optlen, | |
39037602 A |
982 | sizeof (struct mptcp_mpjoin_opt_rsp)), |
983 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); | |
984 | tp->t_mpflags &= ~TMPF_PREESTABLISHED; | |
985 | /* send RST and close */ | |
986 | MPTCP_JOPT_ERROR_PATH(tp); | |
987 | return; | |
988 | } | |
39236c6e | 989 | |
39037602 A |
990 | mptcp_set_raddr_rand(tp->t_local_aid, tptomptp(tp), |
991 | join_rsp->mmjo_addr_id, join_rsp->mmjo_rand); | |
992 | error = mptcp_validate_join_hmac(tp, | |
993 | (u_char*)&join_rsp->mmjo_mac, SHA1_TRUNCATED); | |
994 | if (error) { | |
5ba3f43e | 995 | mptcplog((LOG_ERR, "%s: SYN_ACK error = %d \n", __func__, error), |
39037602 | 996 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_ERR); |
39236c6e | 997 | tp->t_mpflags &= ~TMPF_PREESTABLISHED; |
39037602 A |
998 | /* send RST and close */ |
999 | MPTCP_JOPT_ERROR_PATH(tp); | |
1000 | return; | |
39236c6e | 1001 | } |
5ba3f43e | 1002 | tp->t_mpflags |= (TMPF_SENT_JOIN | TMPF_SND_JACK); |
39236c6e A |
1003 | } |
1004 | ||
1005 | static int | |
1006 | mptcp_validate_join_hmac(struct tcpcb *tp, u_char* hmac, int mac_len) | |
1007 | { | |
1008 | u_char digest[SHA1_RESULTLEN] = {0}; | |
5ba3f43e | 1009 | struct mptcb *mp_tp = tptomptp(tp); |
39236c6e A |
1010 | u_int32_t rem_rand, loc_rand; |
1011 | ||
5ba3f43e | 1012 | mpte_lock_assert_held(mp_tp->mpt_mpte); |
39236c6e A |
1013 | |
1014 | rem_rand = loc_rand = 0; | |
1015 | ||
39236c6e A |
1016 | mptcp_get_rands(tp->t_local_aid, mp_tp, &loc_rand, &rem_rand); |
1017 | if ((rem_rand == 0) || (loc_rand == 0)) | |
1018 | return (-1); | |
1019 | ||
5ba3f43e A |
1020 | mptcp_hmac_sha1(mp_tp->mpt_remotekey, mp_tp->mpt_localkey, rem_rand, loc_rand, |
1021 | digest); | |
39236c6e A |
1022 | |
1023 | if (bcmp(digest, hmac, mac_len) == 0) | |
1024 | return (0); /* matches */ | |
1025 | else { | |
1026 | printf("%s: remote key %llx local key %llx remote rand %x " | |
5ba3f43e | 1027 | "local rand %x \n", __func__, mp_tp->mpt_remotekey, mp_tp->mpt_localkey, |
39236c6e A |
1028 | rem_rand, loc_rand); |
1029 | return (-1); | |
1030 | } | |
1031 | } | |
1032 | ||
5ba3f43e A |
1033 | /* |
1034 | * Update the mptcb send state variables, but the actual sbdrop occurs | |
1035 | * in MPTCP layer | |
1036 | */ | |
1037 | void | |
1038 | mptcp_data_ack_rcvd(struct mptcb *mp_tp, struct tcpcb *tp, u_int64_t full_dack) | |
1039 | { | |
a39ff7e2 | 1040 | u_int64_t acked = full_dack - mp_tp->mpt_snduna; |
5ba3f43e A |
1041 | |
1042 | if (acked) { | |
1043 | struct socket *mp_so = mptetoso(mp_tp->mpt_mpte); | |
1044 | ||
1045 | if (acked > mp_so->so_snd.sb_cc) { | |
1046 | if (acked > mp_so->so_snd.sb_cc + 1 || | |
1047 | mp_tp->mpt_state < MPTCPS_FIN_WAIT_1) | |
1048 | mptcplog((LOG_ERR, "%s: acked %u, sb_cc %u full %u suna %u state %u\n", | |
1049 | __func__, (uint32_t)acked, mp_so->so_snd.sb_cc, | |
1050 | (uint32_t)full_dack, (uint32_t)mp_tp->mpt_snduna, | |
1051 | mp_tp->mpt_state), | |
1052 | MPTCP_RECEIVER_DBG, MPTCP_LOGLVL_ERR); | |
1053 | ||
1054 | sbdrop(&mp_so->so_snd, (int)mp_so->so_snd.sb_cc); | |
1055 | } else { | |
1056 | sbdrop(&mp_so->so_snd, acked); | |
1057 | } | |
1058 | ||
1059 | mp_tp->mpt_snduna += acked; | |
1060 | /* In degraded mode, we may get some Data ACKs */ | |
1061 | if ((tp->t_mpflags & TMPF_TCP_FALLBACK) && | |
1062 | !(mp_tp->mpt_flags & MPTCPF_POST_FALLBACK_SYNC) && | |
1063 | MPTCP_SEQ_GT(mp_tp->mpt_sndnxt, mp_tp->mpt_snduna)) { | |
1064 | /* bring back sndnxt to retransmit MPTCP data */ | |
1065 | mp_tp->mpt_sndnxt = mp_tp->mpt_dsn_at_csum_fail; | |
1066 | mp_tp->mpt_flags |= MPTCPF_POST_FALLBACK_SYNC; | |
1067 | tp->t_inpcb->inp_socket->so_flags1 |= | |
1068 | SOF1_POST_FALLBACK_SYNC; | |
1069 | } | |
1070 | ||
1071 | mptcp_clean_reinjectq(mp_tp->mpt_mpte); | |
1072 | ||
1073 | sowwakeup(mp_so); | |
1074 | } | |
1075 | if (full_dack == mp_tp->mpt_sndmax && | |
1076 | mp_tp->mpt_state >= MPTCPS_FIN_WAIT_1) { | |
1077 | mptcp_close_fsm(mp_tp, MPCE_RECV_DATA_ACK); | |
1078 | tp->t_mpflags &= ~TMPF_SEND_DFIN; | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | void | |
1083 | mptcp_update_window_fallback(struct tcpcb *tp) | |
1084 | { | |
1085 | struct mptcb *mp_tp = tptomptp(tp); | |
1086 | ||
1087 | mpte_lock_assert_held(mp_tp->mpt_mpte); | |
1088 | ||
1089 | if (!(mp_tp->mpt_flags & MPTCPF_FALLBACK_TO_TCP)) | |
1090 | return; | |
1091 | ||
1092 | mptcplog((LOG_DEBUG, "%s: update window to %u\n", __func__, tp->snd_wnd), | |
1093 | MPTCP_SOCKET_DBG, MPTCP_LOGLVL_VERBOSE); | |
1094 | ||
1095 | mp_tp->mpt_sndwnd = tp->snd_wnd; | |
1096 | mp_tp->mpt_sndwl1 = mp_tp->mpt_rcvnxt; | |
1097 | mp_tp->mpt_sndwl2 = mp_tp->mpt_snduna; | |
1098 | ||
1099 | sowwakeup(tp->t_inpcb->inp_socket); | |
1100 | } | |
1101 | ||
1102 | static void | |
1103 | mptcp_update_window(struct mptcb *mp_tp, u_int64_t ack, u_int64_t seq, | |
1104 | u_int32_t tiwin) | |
1105 | { | |
1106 | /* Don't look at the window if there is no ACK flag */ | |
1107 | if ((SEQ_LT(mp_tp->mpt_sndwl1, seq) || | |
1108 | (mp_tp->mpt_sndwl1 == seq && (SEQ_LT(mp_tp->mpt_sndwl2, ack) || | |
1109 | (mp_tp->mpt_sndwl2 == ack && tiwin > mp_tp->mpt_sndwnd))))) { | |
1110 | mp_tp->mpt_sndwnd = tiwin; | |
1111 | mp_tp->mpt_sndwl1 = seq; | |
1112 | mp_tp->mpt_sndwl2 = ack; | |
1113 | ||
1114 | mptcplog((LOG_DEBUG, "%s: Updating window to %u\n", __func__, | |
1115 | mp_tp->mpt_sndwnd), MPTCP_RECEIVER_DBG, MPTCP_LOGLVL_VERBOSE); | |
1116 | } | |
1117 | } | |
1118 | ||
39236c6e | 1119 | static void |
5ba3f43e A |
1120 | mptcp_do_dss_opt_ack_meat(u_int64_t full_dack, u_int64_t full_dsn, |
1121 | struct tcpcb *tp, u_int32_t tiwin) | |
39236c6e A |
1122 | { |
1123 | struct mptcb *mp_tp = tptomptp(tp); | |
1124 | int close_notify = 0; | |
1125 | ||
39037602 A |
1126 | tp->t_mpflags |= TMPF_RCVD_DACK; |
1127 | ||
39236c6e A |
1128 | if (MPTCP_SEQ_LEQ(full_dack, mp_tp->mpt_sndmax) && |
1129 | MPTCP_SEQ_GEQ(full_dack, mp_tp->mpt_snduna)) { | |
1130 | mptcp_data_ack_rcvd(mp_tp, tp, full_dack); | |
fe8ab488 | 1131 | if (mp_tp->mpt_state > MPTCPS_FIN_WAIT_2) |
39236c6e | 1132 | close_notify = 1; |
39236c6e A |
1133 | if (mp_tp->mpt_flags & MPTCPF_RCVD_64BITACK) { |
1134 | mp_tp->mpt_flags &= ~MPTCPF_RCVD_64BITACK; | |
1135 | mp_tp->mpt_flags &= ~MPTCPF_SND_64BITDSN; | |
1136 | } | |
490019cf A |
1137 | mptcp_notify_mpready(tp->t_inpcb->inp_socket); |
1138 | if (close_notify) | |
1139 | mptcp_notify_close(tp->t_inpcb->inp_socket); | |
39236c6e | 1140 | } else { |
5ba3f43e A |
1141 | mptcplog((LOG_ERR,"%s: unexpected dack %u snduna %u sndmax %u\n", __func__, |
1142 | (u_int32_t)full_dack, (u_int32_t)mp_tp->mpt_snduna, | |
1143 | (u_int32_t)mp_tp->mpt_sndmax), | |
3e170ce0 A |
1144 | (MPTCP_SOCKET_DBG|MPTCP_RECEIVER_DBG), |
1145 | MPTCP_LOGLVL_LOG); | |
39236c6e | 1146 | } |
5ba3f43e A |
1147 | |
1148 | mptcp_update_window(mp_tp, full_dack, full_dsn, tiwin); | |
39236c6e A |
1149 | } |
1150 | ||
1151 | static void | |
5ba3f43e | 1152 | mptcp_do_dss_opt_meat(u_char *cp, struct tcpcb *tp, struct tcphdr *th) |
39236c6e A |
1153 | { |
1154 | struct mptcp_dss_copt *dss_rsp = (struct mptcp_dss_copt *)cp; | |
1155 | u_int64_t full_dack = 0; | |
5ba3f43e | 1156 | u_int32_t tiwin = th->th_win << tp->snd_scale; |
39236c6e A |
1157 | struct mptcb *mp_tp = tptomptp(tp); |
1158 | int csum_len = 0; | |
1159 | ||
5ba3f43e A |
1160 | #define MPTCP_DSS_OPT_SZ_CHK(len, expected_len) { \ |
1161 | if (len != expected_len) { \ | |
1162 | mptcplog((LOG_ERR, "%s: bad len = %d dss: %x \n", __func__, \ | |
1163 | len, dss_rsp->mdss_flags), \ | |
1164 | (MPTCP_SOCKET_DBG|MPTCP_RECEIVER_DBG), \ | |
1165 | MPTCP_LOGLVL_LOG); \ | |
1166 | return; \ | |
1167 | } \ | |
39236c6e | 1168 | } |
39236c6e A |
1169 | |
1170 | if (mp_tp->mpt_flags & MPTCPF_CHECKSUM) | |
1171 | csum_len = 2; | |
1172 | ||
1173 | dss_rsp->mdss_flags &= (MDSS_A|MDSS_a|MDSS_M|MDSS_m); | |
1174 | switch (dss_rsp->mdss_flags) { | |
1175 | case (MDSS_M): | |
1176 | { | |
1177 | /* 32-bit DSS, No Data ACK */ | |
1178 | struct mptcp_dsn_opt *dss_rsp1; | |
1179 | dss_rsp1 = (struct mptcp_dsn_opt *)cp; | |
1180 | ||
1181 | MPTCP_DSS_OPT_SZ_CHK(dss_rsp1->mdss_copt.mdss_len, | |
1182 | sizeof (struct mptcp_dsn_opt) + csum_len); | |
1183 | if (csum_len == 0) | |
1184 | mptcp_update_dss_rcv_state(dss_rsp1, tp, 0); | |
1185 | else | |
1186 | mptcp_update_dss_rcv_state(dss_rsp1, tp, | |
1187 | *(uint16_t *)(void *)(cp + | |
1188 | (dss_rsp1->mdss_copt.mdss_len - csum_len))); | |
1189 | break; | |
1190 | } | |
1191 | case (MDSS_A): | |
1192 | { | |
1193 | /* 32-bit Data ACK, no DSS */ | |
1194 | struct mptcp_data_ack_opt *dack_opt; | |
1195 | dack_opt = (struct mptcp_data_ack_opt *)cp; | |
1196 | ||
1197 | MPTCP_DSS_OPT_SZ_CHK(dack_opt->mdss_copt.mdss_len, | |
1198 | sizeof (struct mptcp_data_ack_opt)); | |
1199 | ||
1200 | u_int32_t dack = dack_opt->mdss_ack; | |
1201 | NTOHL(dack); | |
39236c6e | 1202 | MPTCP_EXTEND_DSN(mp_tp->mpt_snduna, dack, full_dack); |
5ba3f43e | 1203 | mptcp_do_dss_opt_ack_meat(full_dack, mp_tp->mpt_sndwl1, tp, tiwin); |
39236c6e A |
1204 | break; |
1205 | } | |
1206 | case (MDSS_M | MDSS_A): | |
1207 | { | |
1208 | /* 32-bit Data ACK + 32-bit DSS */ | |
1209 | struct mptcp_dss_ack_opt *dss_ack_rsp; | |
1210 | dss_ack_rsp = (struct mptcp_dss_ack_opt *)cp; | |
5ba3f43e A |
1211 | u_int64_t full_dsn; |
1212 | uint16_t csum = 0; | |
39236c6e A |
1213 | |
1214 | MPTCP_DSS_OPT_SZ_CHK(dss_ack_rsp->mdss_copt.mdss_len, | |
1215 | sizeof (struct mptcp_dss_ack_opt) + csum_len); | |
1216 | ||
1217 | u_int32_t dack = dss_ack_rsp->mdss_ack; | |
1218 | NTOHL(dack); | |
39236c6e | 1219 | MPTCP_EXTEND_DSN(mp_tp->mpt_snduna, dack, full_dack); |
5ba3f43e A |
1220 | |
1221 | NTOHL(dss_ack_rsp->mdss_dsn); | |
1222 | NTOHL(dss_ack_rsp->mdss_subflow_seqn); | |
1223 | NTOHS(dss_ack_rsp->mdss_data_len); | |
1224 | MPTCP_EXTEND_DSN(mp_tp->mpt_rcvnxt, dss_ack_rsp->mdss_dsn, full_dsn); | |
1225 | ||
1226 | mptcp_do_dss_opt_ack_meat(full_dack, full_dsn, tp, tiwin); | |
1227 | ||
1228 | if (csum_len != 0) | |
1229 | csum = *(uint16_t *)(void *)(cp + (dss_ack_rsp->mdss_copt.mdss_len - csum_len)); | |
1230 | ||
1231 | mptcp_update_rcv_state_meat(mp_tp, tp, | |
1232 | full_dsn, | |
1233 | dss_ack_rsp->mdss_subflow_seqn, | |
1234 | dss_ack_rsp->mdss_data_len, | |
1235 | csum); | |
39236c6e A |
1236 | break; |
1237 | } | |
1238 | case (MDSS_M | MDSS_m): | |
1239 | { | |
1240 | /* 64-bit DSS , No Data ACK */ | |
1241 | struct mptcp_dsn64_opt *dsn64; | |
1242 | dsn64 = (struct mptcp_dsn64_opt *)cp; | |
1243 | u_int64_t full_dsn; | |
5ba3f43e | 1244 | uint16_t csum = 0; |
39236c6e A |
1245 | |
1246 | MPTCP_DSS_OPT_SZ_CHK(dsn64->mdss_copt.mdss_len, | |
1247 | sizeof (struct mptcp_dsn64_opt) + csum_len); | |
1248 | ||
39236c6e | 1249 | mp_tp->mpt_flags |= MPTCPF_SND_64BITACK; |
39236c6e A |
1250 | |
1251 | full_dsn = mptcp_ntoh64(dsn64->mdss_dsn); | |
1252 | NTOHL(dsn64->mdss_subflow_seqn); | |
1253 | NTOHS(dsn64->mdss_data_len); | |
5ba3f43e A |
1254 | |
1255 | if (csum_len != 0) | |
1256 | csum = *(uint16_t *)(void *)(cp + dsn64->mdss_copt.mdss_len - csum_len); | |
1257 | ||
1258 | mptcp_update_rcv_state_meat(mp_tp, tp, full_dsn, | |
1259 | dsn64->mdss_subflow_seqn, | |
1260 | dsn64->mdss_data_len, | |
1261 | csum); | |
39236c6e A |
1262 | break; |
1263 | } | |
1264 | case (MDSS_A | MDSS_a): | |
1265 | { | |
1266 | /* 64-bit Data ACK, no DSS */ | |
1267 | struct mptcp_data_ack64_opt *dack64; | |
1268 | dack64 = (struct mptcp_data_ack64_opt *)cp; | |
1269 | ||
1270 | MPTCP_DSS_OPT_SZ_CHK(dack64->mdss_copt.mdss_len, | |
1271 | sizeof (struct mptcp_data_ack64_opt)); | |
1272 | ||
39236c6e | 1273 | mp_tp->mpt_flags |= MPTCPF_RCVD_64BITACK; |
39236c6e A |
1274 | |
1275 | full_dack = mptcp_ntoh64(dack64->mdss_ack); | |
5ba3f43e | 1276 | mptcp_do_dss_opt_ack_meat(full_dack, mp_tp->mpt_sndwl1, tp, tiwin); |
39236c6e A |
1277 | break; |
1278 | } | |
1279 | case (MDSS_M | MDSS_m | MDSS_A): | |
1280 | { | |
1281 | /* 64-bit DSS + 32-bit Data ACK */ | |
1282 | struct mptcp_dss64_ack32_opt *dss_ack_rsp; | |
1283 | dss_ack_rsp = (struct mptcp_dss64_ack32_opt *)cp; | |
5ba3f43e A |
1284 | u_int64_t full_dsn; |
1285 | uint16_t csum = 0; | |
39236c6e A |
1286 | |
1287 | MPTCP_DSS_OPT_SZ_CHK(dss_ack_rsp->mdss_copt.mdss_len, | |
1288 | sizeof (struct mptcp_dss64_ack32_opt) + csum_len); | |
1289 | ||
39236c6e A |
1290 | u_int32_t dack = dss_ack_rsp->mdss_ack; |
1291 | NTOHL(dack); | |
39236c6e A |
1292 | mp_tp->mpt_flags |= MPTCPF_SND_64BITACK; |
1293 | MPTCP_EXTEND_DSN(mp_tp->mpt_snduna, dack, full_dack); | |
5ba3f43e A |
1294 | |
1295 | full_dsn = mptcp_ntoh64(dss_ack_rsp->mdss_dsn); | |
1296 | NTOHL(dss_ack_rsp->mdss_subflow_seqn); | |
1297 | NTOHS(dss_ack_rsp->mdss_data_len); | |
1298 | ||
1299 | mptcp_do_dss_opt_ack_meat(full_dack, full_dsn, tp, tiwin); | |
1300 | ||
1301 | if (csum_len != 0) | |
1302 | csum = *(uint16_t *)(void *)(cp + dss_ack_rsp->mdss_copt.mdss_len - csum_len); | |
1303 | ||
1304 | mptcp_update_rcv_state_meat(mp_tp, tp, full_dsn, | |
1305 | dss_ack_rsp->mdss_subflow_seqn, | |
1306 | dss_ack_rsp->mdss_data_len, | |
1307 | csum); | |
1308 | ||
39236c6e A |
1309 | break; |
1310 | } | |
1311 | case (MDSS_M | MDSS_A | MDSS_a): | |
1312 | { | |
1313 | /* 32-bit DSS + 64-bit Data ACK */ | |
1314 | struct mptcp_dss32_ack64_opt *dss32_ack64_opt; | |
1315 | dss32_ack64_opt = (struct mptcp_dss32_ack64_opt *)cp; | |
1316 | u_int64_t full_dsn; | |
1317 | ||
1318 | MPTCP_DSS_OPT_SZ_CHK( | |
1319 | dss32_ack64_opt->mdss_copt.mdss_len, | |
1320 | sizeof (struct mptcp_dss32_ack64_opt) + csum_len); | |
1321 | ||
39236c6e | 1322 | full_dack = mptcp_ntoh64(dss32_ack64_opt->mdss_ack); |
39236c6e | 1323 | NTOHL(dss32_ack64_opt->mdss_dsn); |
39236c6e A |
1324 | mp_tp->mpt_flags |= MPTCPF_RCVD_64BITACK; |
1325 | MPTCP_EXTEND_DSN(mp_tp->mpt_rcvnxt, | |
1326 | dss32_ack64_opt->mdss_dsn, full_dsn); | |
39236c6e A |
1327 | NTOHL(dss32_ack64_opt->mdss_subflow_seqn); |
1328 | NTOHS(dss32_ack64_opt->mdss_data_len); | |
5ba3f43e A |
1329 | |
1330 | mptcp_do_dss_opt_ack_meat(full_dack, full_dsn, tp, tiwin); | |
39236c6e A |
1331 | if (csum_len == 0) |
1332 | mptcp_update_rcv_state_meat(mp_tp, tp, full_dsn, | |
1333 | dss32_ack64_opt->mdss_subflow_seqn, | |
1334 | dss32_ack64_opt->mdss_data_len, 0); | |
1335 | else | |
1336 | mptcp_update_rcv_state_meat(mp_tp, tp, full_dsn, | |
1337 | dss32_ack64_opt->mdss_subflow_seqn, | |
1338 | dss32_ack64_opt->mdss_data_len, | |
1339 | *(uint16_t *)(void *)(cp + | |
1340 | dss32_ack64_opt->mdss_copt.mdss_len - | |
1341 | csum_len)); | |
1342 | break; | |
1343 | } | |
1344 | case (MDSS_M | MDSS_m | MDSS_A | MDSS_a): | |
1345 | { | |
1346 | /* 64-bit DSS + 64-bit Data ACK */ | |
1347 | struct mptcp_dss64_ack64_opt *dss64_ack64; | |
1348 | dss64_ack64 = (struct mptcp_dss64_ack64_opt *)cp; | |
1349 | u_int64_t full_dsn; | |
1350 | ||
1351 | MPTCP_DSS_OPT_SZ_CHK(dss64_ack64->mdss_copt.mdss_len, | |
1352 | sizeof (struct mptcp_dss64_ack64_opt) + csum_len); | |
1353 | ||
39236c6e A |
1354 | mp_tp->mpt_flags |= MPTCPF_RCVD_64BITACK; |
1355 | mp_tp->mpt_flags |= MPTCPF_SND_64BITACK; | |
39236c6e A |
1356 | full_dsn = mptcp_ntoh64(dss64_ack64->mdss_dsn); |
1357 | full_dack = mptcp_ntoh64(dss64_ack64->mdss_dsn); | |
5ba3f43e | 1358 | mptcp_do_dss_opt_ack_meat(full_dack, full_dsn, tp, tiwin); |
39236c6e A |
1359 | NTOHL(dss64_ack64->mdss_subflow_seqn); |
1360 | NTOHS(dss64_ack64->mdss_data_len); | |
1361 | if (csum_len == 0) | |
1362 | mptcp_update_rcv_state_meat(mp_tp, tp, full_dsn, | |
1363 | dss64_ack64->mdss_subflow_seqn, | |
1364 | dss64_ack64->mdss_data_len, 0); | |
1365 | else | |
1366 | mptcp_update_rcv_state_meat(mp_tp, tp, full_dsn, | |
1367 | dss64_ack64->mdss_subflow_seqn, | |
1368 | dss64_ack64->mdss_data_len, | |
1369 | *(uint16_t *)(void *)(cp + | |
1370 | dss64_ack64->mdss_copt.mdss_len - | |
1371 | csum_len)); | |
1372 | break; | |
1373 | } | |
1374 | default: | |
5ba3f43e A |
1375 | mptcplog((LOG_DEBUG,"%s: File bug, DSS flags = %x\n", |
1376 | __func__, dss_rsp->mdss_flags), | |
3e170ce0 A |
1377 | (MPTCP_SOCKET_DBG|MPTCP_RECEIVER_DBG), |
1378 | MPTCP_LOGLVL_LOG); | |
39236c6e A |
1379 | break; |
1380 | } | |
1381 | } | |
1382 | ||
39236c6e A |
1383 | static void |
1384 | mptcp_do_dss_opt(struct tcpcb *tp, u_char *cp, struct tcphdr *th, int optlen) | |
1385 | { | |
5ba3f43e A |
1386 | #pragma unused(optlen) |
1387 | struct mptcb *mp_tp = tptomptp(tp); | |
39236c6e A |
1388 | |
1389 | if (!mp_tp) | |
1390 | return; | |
1391 | ||
fe8ab488 A |
1392 | /* We may get Data ACKs just during fallback, so don't ignore those */ |
1393 | if ((tp->t_mpflags & TMPF_MPTCP_TRUE) || | |
1394 | (tp->t_mpflags & TMPF_TCP_FALLBACK)) { | |
39236c6e A |
1395 | struct mptcp_dss_copt *dss_rsp = (struct mptcp_dss_copt *)cp; |
1396 | ||
1397 | if (dss_rsp->mdss_subtype == MPO_DSS) { | |
5c9f4661 A |
1398 | if (dss_rsp->mdss_flags & MDSS_F) |
1399 | tp->t_rcv_map.mpt_dfin = 1; | |
39236c6e | 1400 | |
5ba3f43e | 1401 | mptcp_do_dss_opt_meat(cp, tp, th); |
39236c6e A |
1402 | } |
1403 | } | |
1404 | } | |
1405 | ||
1406 | static void | |
1407 | mptcp_do_fastclose_opt(struct tcpcb *tp, u_char *cp, struct tcphdr *th) | |
1408 | { | |
1409 | struct mptcb *mp_tp = NULL; | |
1410 | struct mptcp_fastclose_opt *fc_opt = (struct mptcp_fastclose_opt *)cp; | |
1411 | ||
1412 | if (th->th_flags != TH_ACK) | |
1413 | return; | |
1414 | ||
39236c6e A |
1415 | if (fc_opt->mfast_len != sizeof (struct mptcp_fastclose_opt)) { |
1416 | tcpstat.tcps_invalid_opt++; | |
1417 | return; | |
1418 | } | |
1419 | ||
5ba3f43e | 1420 | mp_tp = tptomptp(tp); |
39236c6e A |
1421 | if (!mp_tp) |
1422 | return; | |
1423 | ||
5ba3f43e | 1424 | if (fc_opt->mfast_key != mp_tp->mpt_localkey) { |
39236c6e A |
1425 | tcpstat.tcps_invalid_opt++; |
1426 | return; | |
1427 | } | |
1428 | ||
1429 | /* | |
1430 | * fastclose could make us more vulnerable to attacks, hence | |
1431 | * accept only those that are at the next expected sequence number. | |
1432 | */ | |
1433 | if (th->th_seq != tp->rcv_nxt) { | |
1434 | tcpstat.tcps_invalid_opt++; | |
1435 | return; | |
1436 | } | |
1437 | ||
39236c6e | 1438 | /* Reset this flow */ |
39037602 | 1439 | tp->t_mpflags |= (TMPF_RESET | TMPF_FASTCLOSERCV); |
39236c6e A |
1440 | |
1441 | if (tp->t_inpcb->inp_socket != NULL) { | |
1442 | soevent(tp->t_inpcb->inp_socket, | |
1443 | SO_FILT_HINT_LOCKED | SO_FILT_HINT_MUSTRST); | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | ||
1448 | static void | |
1449 | mptcp_do_mpfail_opt(struct tcpcb *tp, u_char *cp, struct tcphdr *th) | |
1450 | { | |
1451 | struct mptcb *mp_tp = NULL; | |
1452 | struct mptcp_mpfail_opt *fail_opt = (struct mptcp_mpfail_opt *)cp; | |
fe8ab488 A |
1453 | u_int32_t mdss_subflow_seqn = 0; |
1454 | int error = 0; | |
39236c6e | 1455 | |
fe8ab488 A |
1456 | /* |
1457 | * mpfail could make us more vulnerable to attacks. Hence accept | |
1458 | * only those that are the next expected sequence number. | |
1459 | */ | |
1460 | if (th->th_seq != tp->rcv_nxt) { | |
1461 | tcpstat.tcps_invalid_opt++; | |
1462 | return; | |
1463 | } | |
1464 | ||
1465 | /* A packet without RST, must atleast have the ACK bit set */ | |
1466 | if ((th->th_flags != TH_ACK) && (th->th_flags != TH_RST)) | |
39236c6e A |
1467 | return; |
1468 | ||
1469 | if (fail_opt->mfail_len != sizeof (struct mptcp_mpfail_opt)) | |
1470 | return; | |
1471 | ||
5ba3f43e A |
1472 | mp_tp = tptomptp(tp); |
1473 | ||
39236c6e A |
1474 | mp_tp->mpt_flags |= MPTCPF_RECVD_MPFAIL; |
1475 | mp_tp->mpt_dsn_at_csum_fail = mptcp_hton64(fail_opt->mfail_dsn); | |
5ba3f43e | 1476 | error = mptcp_get_map_for_dsn(tp->t_inpcb->inp_socket, |
fe8ab488 A |
1477 | mp_tp->mpt_dsn_at_csum_fail, &mdss_subflow_seqn); |
1478 | if (error == 0) { | |
1479 | mp_tp->mpt_ssn_at_csum_fail = mdss_subflow_seqn; | |
1480 | } | |
39236c6e A |
1481 | |
1482 | mptcp_notify_mpfail(tp->t_inpcb->inp_socket); | |
1483 | } | |
1484 | ||
3e170ce0 | 1485 | void |
39236c6e A |
1486 | tcp_do_mptcp_options(struct tcpcb *tp, u_char *cp, struct tcphdr *th, |
1487 | struct tcpopt *to, int optlen) | |
1488 | { | |
1489 | int mptcp_subtype; | |
5ba3f43e A |
1490 | struct mptcb *mp_tp = tptomptp(tp); |
1491 | ||
1492 | if (mp_tp == NULL) | |
1493 | return; | |
1494 | ||
1495 | mpte_lock_assert_held(mp_tp->mpt_mpte); | |
39236c6e A |
1496 | |
1497 | /* All MPTCP options have atleast 4 bytes */ | |
1498 | if (optlen < 4) | |
3e170ce0 | 1499 | return; |
39236c6e A |
1500 | |
1501 | mptcp_subtype = (cp[2] >> 4); | |
1502 | ||
3e170ce0 A |
1503 | if (mptcp_sanitize_option(tp, mptcp_subtype) == 0) |
1504 | return; | |
1505 | ||
39236c6e A |
1506 | switch (mptcp_subtype) { |
1507 | case MPO_CAPABLE: | |
1508 | mptcp_do_mpcapable_opt(tp, cp, th, optlen); | |
1509 | break; | |
1510 | case MPO_JOIN: | |
1511 | mptcp_do_mpjoin_opt(tp, cp, th, optlen); | |
1512 | break; | |
1513 | case MPO_DSS: | |
1514 | mptcp_do_dss_opt(tp, cp, th, optlen); | |
1515 | break; | |
1516 | case MPO_FASTCLOSE: | |
1517 | mptcp_do_fastclose_opt(tp, cp, th); | |
1518 | break; | |
1519 | case MPO_FAIL: | |
1520 | mptcp_do_mpfail_opt(tp, cp, th); | |
1521 | break; | |
1522 | case MPO_ADD_ADDR: /* fall through */ | |
1523 | case MPO_REMOVE_ADDR: /* fall through */ | |
1524 | case MPO_PRIO: | |
1525 | to->to_flags |= TOF_MPTCP; | |
1526 | break; | |
1527 | default: | |
39236c6e A |
1528 | break; |
1529 | } | |
3e170ce0 | 1530 | return; |
39236c6e A |
1531 | } |
1532 | ||
39236c6e | 1533 | /* REMOVE_ADDR option is sent when a source address goes away */ |
5ba3f43e | 1534 | static void |
39236c6e A |
1535 | mptcp_send_remaddr_opt(struct tcpcb *tp, struct mptcp_remaddr_opt *opt) |
1536 | { | |
5ba3f43e | 1537 | mptcplog((LOG_DEBUG,"%s: local id %d remove id %d \n", |
3e170ce0 A |
1538 | __func__, tp->t_local_aid, tp->t_rem_aid), |
1539 | (MPTCP_SOCKET_DBG|MPTCP_SENDER_DBG), MPTCP_LOGLVL_LOG); | |
39236c6e | 1540 | |
fe8ab488 | 1541 | bzero(opt, sizeof (*opt)); |
39236c6e | 1542 | opt->mr_kind = TCPOPT_MULTIPATH; |
fe8ab488 | 1543 | opt->mr_len = sizeof (*opt); |
39236c6e A |
1544 | opt->mr_subtype = MPO_REMOVE_ADDR; |
1545 | opt->mr_addr_id = tp->t_rem_aid; | |
1546 | tp->t_mpflags &= ~TMPF_SND_REM_ADDR; | |
1547 | } | |
1548 | ||
39236c6e A |
1549 | /* We send MP_PRIO option based on the values set by the SIOCSCONNORDER ioctl */ |
1550 | static int | |
1551 | mptcp_snd_mpprio(struct tcpcb *tp, u_char *cp, int optlen) | |
1552 | { | |
1553 | struct mptcp_mpprio_addr_opt mpprio; | |
1554 | ||
1555 | if (tp->t_state != TCPS_ESTABLISHED) { | |
1556 | tp->t_mpflags &= ~TMPF_SND_MPPRIO; | |
1557 | return (optlen); | |
1558 | } | |
1559 | ||
39236c6e A |
1560 | if ((MAX_TCPOPTLEN - optlen) < |
1561 | (int)sizeof (mpprio)) | |
1562 | return (optlen); | |
1563 | ||
1564 | bzero(&mpprio, sizeof (mpprio)); | |
1565 | mpprio.mpprio_kind = TCPOPT_MULTIPATH; | |
1566 | mpprio.mpprio_len = sizeof (mpprio); | |
1567 | mpprio.mpprio_subtype = MPO_PRIO; | |
1568 | if (tp->t_mpflags & TMPF_BACKUP_PATH) | |
1569 | mpprio.mpprio_flags |= MPTCP_MPPRIO_BKP; | |
1570 | mpprio.mpprio_addrid = tp->t_local_aid; | |
1571 | memcpy(cp + optlen, &mpprio, sizeof (mpprio)); | |
1572 | optlen += sizeof (mpprio); | |
1573 | tp->t_mpflags &= ~TMPF_SND_MPPRIO; | |
5ba3f43e | 1574 | mptcplog((LOG_DEBUG, "%s: aid = %d \n", __func__, |
3e170ce0 A |
1575 | tp->t_local_aid), |
1576 | (MPTCP_SOCKET_DBG|MPTCP_SENDER_DBG), MPTCP_LOGLVL_LOG); | |
39236c6e A |
1577 | return (optlen); |
1578 | } | |
5ba3f43e | 1579 |