]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/mptcp_var.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / netinet / mptcp_var.h
CommitLineData
39236c6e 1/*
f427ee49 2 * Copyright (c) 2012-2020 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
29#ifndef _NETINET_MPTCP_VAR_H_
0a7de745 30#define _NETINET_MPTCP_VAR_H_
39236c6e
A
31
32#ifdef PRIVATE
33#include <netinet/in.h>
34#include <netinet/tcp.h>
35#endif
36
37#ifdef BSD_KERNEL_PRIVATE
38#include <sys/queue.h>
39#include <sys/protosw.h>
40#include <kern/locks.h>
41#include <mach/boolean.h>
42#include <netinet/mp_pcb.h>
5ba3f43e 43#include <netinet/tcp_var.h>
a39ff7e2 44#include <os/log.h>
5ba3f43e
A
45
46struct mpt_itf_info {
47 uint32_t ifindex;
48 uint32_t has_v4_conn:1,
0a7de745
A
49 has_v6_conn:1,
50 has_nat64_conn:1,
51 no_mptcp_support:1;
5ba3f43e 52};
39236c6e
A
53
54/*
55 * MPTCP Session
56 *
57 * This is an extension to the multipath PCB specific for MPTCP, protected by
5ba3f43e 58 * the per-PCB mpp_lock (also the socket's lock);
39236c6e
A
59 */
60struct mptses {
0a7de745
A
61 struct mppcb *mpte_mppcb; /* back ptr to multipath PCB */
62 struct mptcb *mpte_mptcb; /* ptr to MPTCP PCB */
63 TAILQ_HEAD(, mptopt) mpte_sopts; /* list of socket options */
64 TAILQ_HEAD(, mptsub) mpte_subflows; /* list of subflows */
bca245ac 65#define MPTCP_MAX_NUM_SUBFLOWS 256
0a7de745
A
66 uint16_t mpte_numflows; /* # of subflows in list */
67 uint16_t mpte_nummpcapflows; /* # of MP_CAP subflows */
68 sae_associd_t mpte_associd; /* MPTCP association ID */
69 sae_connid_t mpte_connid_last; /* last used connection ID */
5ba3f43e 70
cb323159
A
71 uint64_t mpte_time_target;
72 thread_call_t mpte_time_thread;
73
74 uint32_t mpte_last_cellicon_set;
75 uint32_t mpte_cellicon_increments;
76
5ba3f43e
A
77 union {
78 /* Source address of initial subflow */
cb323159
A
79 struct sockaddr _mpte_src;
80 struct sockaddr_in _mpte_src_v4;
81 struct sockaddr_in6 _mpte_src_v6;
82 } mpte_u_src;
83#define mpte_src mpte_u_src._mpte_src
84#define __mpte_src_v4 mpte_u_src._mpte_src_v4
85#define __mpte_src_v6 mpte_u_src._mpte_src_v6
5ba3f43e
A
86 union {
87 /* Destination address of initial subflow */
cb323159
A
88 struct sockaddr _mpte_dst;
89 struct sockaddr_in _mpte_dst_v4;
90 struct sockaddr_in6 _mpte_dst_v6;
91 } mpte_u_dst;
92#define mpte_dst mpte_u_dst._mpte_dst
93#define __mpte_dst_v4 mpte_u_dst._mpte_dst_v4
94#define __mpte_dst_v6 mpte_u_dst._mpte_dst_v6
5ba3f43e 95
c3c9b80d
A
96 struct sockaddr_in mpte_sub_dst_v4;
97 struct sockaddr_in6 mpte_sub_dst_v6;
cb323159 98
0a7de745 99 uint16_t mpte_alternate_port; /* Alternate port for subflow establishment (network-byte-order) */
a39ff7e2 100
cb323159
A
101 int mpte_epid;
102 uuid_t mpte_euuid;
103
0a7de745 104 struct mptsub *mpte_active_sub; /* ptr to last active subf */
cb323159 105 uint16_t mpte_flags; /* per mptcp session flags */
0a7de745
A
106#define MPTE_SND_REM_ADDR 0x01 /* Send Remove_addr option */
107#define MPTE_SVCTYPE_CHECKED 0x02 /* Did entitlement-check for service-type */
108#define MPTE_FIRSTPARTY 0x04 /* First-party app used multipath_extended entitlement */
109#define MPTE_ACCESS_GRANTED 0x08 /* Access to cellular has been granted for this connection */
cb323159
A
110#define MPTE_FORCE_ENABLE 0x10 /* For MPTCP regardless of heuristics to detect middleboxes */
111#define MPTE_IN_WORKLOOP 0x20 /* Are we currently inside the workloop ? */
112#define MPTE_WORKLOOP_RELAUNCH 0x40 /* Another event got queued, we should restart the workloop */
113#define MPTE_UNICAST_IP 0x80 /* New subflows are only being established towards the unicast IP in the ADD_ADDR */
114#define MPTE_CELL_PROHIBITED 0x100 /* Cell access has been prohibited based on signal quality */
0a7de745
A
115 uint8_t mpte_svctype; /* MPTCP Service type */
116 uint8_t mpte_lost_aid; /* storing lost address id */
117 uint8_t mpte_addrid_last; /* storing address id parm */
5ba3f43e 118
0a7de745
A
119#define MPTE_ITFINFO_SIZE 4
120 uint32_t mpte_itfinfo_size;
121 struct mpt_itf_info _mpte_itfinfo[MPTE_ITFINFO_SIZE];
122 struct mpt_itf_info *mpte_itfinfo;
5ba3f43e 123
0a7de745 124 struct mbuf *mpte_reinjectq;
5ba3f43e
A
125
126 /* The below is used for stats */
0a7de745
A
127 uint32_t mpte_subflow_switches; /* Number of subflow-switches in sending */
128 uint32_t mpte_used_cell:1,
129 mpte_used_wifi:1,
130 mpte_initial_cell:1,
131 mpte_triggered_cell,
132 mpte_handshake_success:1;
133
134 struct mptcp_itf_stats mpte_itfstats[MPTCP_ITFSTATS_SIZE];
135 uint64_t mpte_init_txbytes __attribute__((aligned(8)));
136 uint64_t mpte_init_rxbytes __attribute__((aligned(8)));
39236c6e
A
137};
138
5ba3f43e
A
139static inline struct socket *
140mptetoso(struct mptses *mpte)
141{
0a7de745 142 return mpte->mpte_mppcb->mpp_socket;
5ba3f43e
A
143}
144
145static inline struct mptses *
146mptompte(struct mppcb *mp)
147{
0a7de745 148 return (struct mptses *)mp->mpp_pcbe;
5ba3f43e 149}
39236c6e 150
5ba3f43e
A
151static inline struct mptses *
152mpsotompte(struct socket *so)
153{
0a7de745 154 return mptompte(mpsotomppcb(so));
5ba3f43e 155}
39236c6e 156
5ba3f43e
A
157static inline boolean_t
158mpp_try_lock(struct mppcb *mp)
159{
0a7de745 160 if (!lck_mtx_try_lock(&mp->mpp_lock)) {
5ba3f43e 161 return false;
0a7de745 162 }
39236c6e 163
5ba3f43e
A
164 VERIFY(!(mp->mpp_flags & MPP_INSIDE_OUTPUT));
165 VERIFY(!(mp->mpp_flags & MPP_INSIDE_INPUT));
39236c6e 166
5ba3f43e
A
167 return true;
168}
169
170static inline void
171mpp_lock(struct mppcb *mp)
172{
173 lck_mtx_lock(&mp->mpp_lock);
174 VERIFY(!(mp->mpp_flags & MPP_INSIDE_OUTPUT));
175 VERIFY(!(mp->mpp_flags & MPP_INSIDE_INPUT));
176}
177
178static inline void
179mpp_unlock(struct mppcb *mp)
180{
181 VERIFY(!(mp->mpp_flags & MPP_INSIDE_OUTPUT));
182 VERIFY(!(mp->mpp_flags & MPP_INSIDE_INPUT));
183 lck_mtx_unlock(&mp->mpp_lock);
184}
185
186static inline lck_mtx_t *
187mpp_getlock(struct mppcb *mp, int flags)
188{
189 if (flags & PR_F_WILLUNLOCK) {
190 VERIFY(!(mp->mpp_flags & MPP_INSIDE_OUTPUT));
191 VERIFY(!(mp->mpp_flags & MPP_INSIDE_INPUT));
192 }
193
0a7de745 194 return &mp->mpp_lock;
5ba3f43e
A
195}
196
5ba3f43e
A
197static inline int
198mptcp_subflow_cwnd_space(struct socket *so)
199{
200 struct tcpcb *tp = sototcpcb(so);
f427ee49 201 int cwnd = (int)(MIN(tp->snd_wnd, tp->snd_cwnd) - (so->so_snd.sb_cc));
5ba3f43e 202
f427ee49 203 return MIN(cwnd, sbspace(&so->so_snd));
5ba3f43e 204}
39236c6e 205
c3c9b80d
A
206static inline bool
207mptcp_subflows_need_backup_flag(struct mptses *mpte)
208{
209 return mpte->mpte_svctype < MPTCP_SVCTYPE_AGGREGATE ||
210 mpte->mpte_svctype == MPTCP_SVCTYPE_PURE_HANDOVER;
211}
39236c6e
A
212
213/*
214 * MPTCP socket options
215 */
216struct mptopt {
0a7de745
A
217 TAILQ_ENTRY(mptopt) mpo_entry; /* glue to other options */
218 uint32_t mpo_flags; /* see flags below */
219 int mpo_level; /* sopt_level */
220 int mpo_name; /* sopt_name */
221 int mpo_intval; /* sopt_val */
39236c6e
A
222};
223
0a7de745
A
224#define MPOF_ATTACHED 0x1 /* attached to MP socket */
225#define MPOF_SUBFLOW_OK 0x2 /* can be issued on subflow socket */
226#define MPOF_INTERIM 0x4 /* has not been issued on any subflow */
39236c6e 227
39236c6e
A
228/*
229 * MPTCP subflow
230 *
5ba3f43e 231 * Note that mpts_flags and mpts_evctl are modified via atomic operations.
39236c6e
A
232 */
233struct mptsub {
f427ee49
A
234 TAILQ_ENTRY(mptsub) mpts_entry; /* glue to peer subflows */
235 uint32_t mpts_refcnt; /* reference count */
236 uint32_t mpts_flags; /* see flags below */
237 long mpts_evctl; /* subflow control events */
238 sae_connid_t mpts_connid; /* subflow connection ID */
239 int mpts_oldintval; /* sopt_val before sosetopt */
240 struct mptses *mpts_mpte; /* back ptr to MPTCP session */
241 struct socket *mpts_socket; /* subflow socket */
242 struct sockaddr *mpts_src; /* source address */
5ba3f43e
A
243
244 union {
245 /* destination address */
cb323159
A
246 struct sockaddr _mpts_dst;
247 struct sockaddr_in _mpts_dst_v4;
248 struct sockaddr_in6 _mpts_dst_v6;
249 } mpts_u_dst;
250#define mpts_dst mpts_u_dst._mpts_dst
251#define __mpts_dst_v4 mpts_u_dst._mpts_dst_v4
252#define __mpts_dst_v6 mpts_u_dst._mpts_dst_v6
0a7de745
A
253 u_int32_t mpts_rel_seq; /* running count of subflow # */
254 u_int32_t mpts_iss; /* Initial sequence number, taking TFO into account */
255 u_int32_t mpts_ifscope; /* scoped to the interface */
256 uint32_t mpts_probesoon; /* send probe after probeto */
257 uint32_t mpts_probecnt; /* number of probes sent */
258 uint32_t mpts_maxseg; /* cached value of t_maxseg */
39236c6e
A
259};
260
261/*
262 * Valid values for mpts_flags. In particular:
263 *
264 * - MP_CAPABLE means that the connection is successfully established as
265 * MPTCP and data transfer may occur, but is not yet ready for multipath-
266 * related semantics until MP_READY. I.e. if this is on the first subflow,
267 * it causes the MPTCP socket to transition to a connected state, except
268 * that additional subflows will not be established; they will be marked
269 * with PENDING and will be processed when the first subflow is marked
270 * with MP_READY.
271 *
272 * - MP_READY implies that an MP_CAPABLE connection has been confirmed as
273 * an MPTCP connection. See notes above.
274 *
275 * - MP_DEGRADED implies that the connection has lost its MPTCP capabilities
276 * but data transfer on the MPTCP socket is unaffected. Any existing
277 * PENDING subflows will be disconnected, and further attempts to connect
278 * additional subflows will be rejected.
279 *
280 * Note that these are per-subflow flags. The setting and clearing of MP_READY
281 * reflects the state of the MPTCP connection with regards to its multipath
282 * semantics, via the MPTCPF_JOIN_READY flag. Until that flag is set (meaning
283 * until at least a subflow is marked with MP_READY), further connectx(2)
284 * attempts to join will be queued. When the flag is cleared (after it has
285 * been set), further connectx(2) will fail (and existing queued ones will be
286 * aborted) and the MPTCP connection loses all of its multipath semantics.
287 *
288 * Keep in sync with bsd/dev/dtrace/scripts/mptcp.d.
289 */
0a7de745
A
290#define MPTSF_ATTACHED 0x00000001 /* attached to MPTCP PCB */
291#define MPTSF_CONNECTING 0x00000002 /* connection was attempted */
292#define MPTSF_CONNECT_PENDING 0x00000004 /* will connect when MPTCP is ready */
293#define MPTSF_CONNECTED 0x00000008 /* connection is established */
294#define MPTSF_DISCONNECTING 0x00000010 /* disconnection was attempted */
295#define MPTSF_DISCONNECTED 0x00000020 /* has been disconnected */
296#define MPTSF_MP_CAPABLE 0x00000040 /* connected as a MPTCP subflow */
297#define MPTSF_MP_READY 0x00000080 /* MPTCP has been confirmed */
298#define MPTSF_MP_DEGRADED 0x00000100 /* has lost its MPTCP capabilities */
299#define MPTSF_PREFERRED 0x00000200 /* primary/preferred subflow */
300#define MPTSF_SOPT_OLDVAL 0x00000400 /* old option value is valid */
301#define MPTSF_SOPT_INPROG 0x00000800 /* sosetopt in progress */
302#define MPTSF_FAILINGOVER 0x00001000 /* subflow not used for output */
303#define MPTSF_ACTIVE 0x00002000 /* subflow currently in use */
304#define MPTSF_MPCAP_CTRSET 0x00004000 /* mpcap counter */
305#define MPTSF_CLOSED 0x00008000 /* soclose_locked has been called on this subflow */
306#define MPTSF_TFO_REQD 0x00010000 /* TFO requested */
307#define MPTSF_CLOSE_REQD 0x00020000 /* A close has been requested from NECP */
308#define MPTSF_INITIAL_SUB 0x00040000 /* This is the initial subflow */
309#define MPTSF_READ_STALL 0x00080000 /* A read-stall has been detected */
310#define MPTSF_WRITE_STALL 0x00100000 /* A write-stall has been detected */
cb323159
A
311#define MPTSF_FULLY_ESTABLISHED 0x00200000 /* Subflow is fully established and it has been confirmed
312 * whether or not it supports MPTCP.
313 * No need for further middlebox-detection.
314 */
315#define MPTSF_CELLICON_SET 0x00400000 /* This subflow set the cellicon */
0a7de745
A
316
317#define MPTSF_BITS \
39236c6e 318 "\020\1ATTACHED\2CONNECTING\3PENDING\4CONNECTED\5DISCONNECTING" \
5ba3f43e
A
319 "\6DISCONNECTED\7MP_CAPABLE\10MP_READY\11MP_DEGRADED" \
320 "\12PREFERRED\13SOPT_OLDVAL" \
321 "\14SOPT_INPROG\15FAILINGOVER\16ACTIVE\17MPCAP_CTRSET" \
322 "\20CLOSED\21TFO_REQD\22CLOSEREQD\23INITIALSUB\24READ_STALL" \
323 "\25WRITE_STALL\26CONFIRMED"
39236c6e
A
324
325/*
326 * MPTCP states
327 * Keep in sync with bsd/dev/dtrace/mptcp.d
328 */
329typedef enum mptcp_state {
0a7de745
A
330 MPTCPS_CLOSED = 0, /* closed */
331 MPTCPS_LISTEN = 1, /* not yet implemented */
332 MPTCPS_ESTABLISHED = 2, /* MPTCP connection established */
333 MPTCPS_CLOSE_WAIT = 3, /* rcvd DFIN, waiting for close */
334 MPTCPS_FIN_WAIT_1 = 4, /* have closed, sent DFIN */
335 MPTCPS_CLOSING = 5, /* closed xchd DFIN, waiting DFIN ACK */
336 MPTCPS_LAST_ACK = 6, /* had DFIN and close; await DFIN ACK */
337 MPTCPS_FIN_WAIT_2 = 7, /* have closed, DFIN is acked */
338 MPTCPS_TIME_WAIT = 8, /* in 2*MSL quiet wait after close */
339 MPTCPS_TERMINATE = 9, /* terminal state */
39236c6e
A
340} mptcp_state_t;
341
0a7de745
A
342typedef u_int64_t mptcp_key_t;
343typedef u_int32_t mptcp_token_t;
344typedef u_int8_t mptcp_addr_id;
39236c6e
A
345
346
347/* Address ID list */
348struct mptcp_subf_auth_entry {
349 LIST_ENTRY(mptcp_subf_auth_entry) msae_next;
0a7de745
A
350 u_int32_t msae_laddr_rand; /* Local nonce */
351 u_int32_t msae_raddr_rand; /* Remote nonce */
352 mptcp_addr_id msae_laddr_id; /* Local addr ID */
353 mptcp_addr_id msae_raddr_id; /* Remote addr ID */
39236c6e
A
354};
355
356/*
357 * MPTCP Protocol Control Block
358 *
359 * Protected by per-MPTCP mpt_lock.
360 * Keep in sync with bsd/dev/dtrace/scripts/mptcp.d.
361 */
362struct mptcb {
f427ee49 363 struct mptses *mpt_mpte; /* back ptr to MPTCP session */
0a7de745 364 mptcp_state_t mpt_state; /* MPTCP state */
f427ee49
A
365 uint32_t mpt_flags; /* see flags below */
366 uint8_t mpt_version; /* MPTCP proto version */
367 uint8_t mpt_peer_version; /* Version from peer */
368 u_short mpt_softerror; /* error not yet reported */
39236c6e
A
369 /*
370 * Authentication and metadata invariants
371 */
0a7de745
A
372 mptcp_key_t mpt_localkey; /* in network byte order */
373 mptcp_key_t mpt_remotekey; /* in network byte order */
374 mptcp_token_t mpt_localtoken; /* HMAC SHA1 of local key */
375 mptcp_token_t mpt_remotetoken; /* HMAC SHA1 of remote key */
39236c6e
A
376
377 /*
378 * Timer vars for scenarios where subflow level acks arrive, but
379 * Data ACKs do not.
380 */
0a7de745 381 int mpt_rxtshift; /* num of consecutive retrans */
f427ee49 382 uint64_t mpt_rxtstart; /* time at which rxt started */
cb323159 383 uint64_t mpt_rtseq; /* seq # being tracked */
f427ee49 384 uint64_t mpt_timewait; /* timewait */
cb323159 385 uint32_t mpt_timer_vals; /* timer related values */
39236c6e
A
386 /*
387 * Sending side
388 */
cb323159
A
389 uint64_t mpt_snduna; /* DSN of last unacked byte */
390 uint64_t mpt_sndnxt; /* DSN of next byte to send */
391 uint64_t mpt_sndmax; /* DSN of max byte sent */
392 uint64_t mpt_local_idsn; /* First byte's DSN */
393 uint32_t mpt_sndwnd;
394 uint64_t mpt_sndwl1;
395 uint64_t mpt_sndwl2;
39236c6e
A
396 /*
397 * Receiving side
398 */
cb323159
A
399 uint64_t mpt_rcvnxt; /* Next expected DSN */
400 uint64_t mpt_remote_idsn; /* Peer's IDSN */
f427ee49 401 uint64_t mpt_rcvadv;
cb323159 402 uint32_t mpt_rcvwnd;
39236c6e
A
403 LIST_HEAD(, mptcp_subf_auth_entry) mpt_subauth_list; /* address IDs */
404 /*
405 * Fastclose
406 */
cb323159
A
407 uint64_t mpt_dsn_at_csum_fail; /* MPFail Opt DSN */
408 uint32_t mpt_ssn_at_csum_fail; /* MPFail Subflow Seq */
39236c6e
A
409 /*
410 * Zombie handling
411 */
0a7de745
A
412#define MPT_GC_TICKS (30)
413#define MPT_GC_TICKS_FAST (10)
414 int32_t mpt_gc_ticks; /* Used for zombie deletion */
fe8ab488 415
cb323159 416 uint32_t mpt_notsent_lowat; /* TCP_NOTSENT_LOWAT support */
5ba3f43e 417
0a7de745 418 struct tsegqe_head mpt_segq;
f427ee49 419 uint32_t mpt_reassqlen; /* length of reassembly queue */
39236c6e
A
420};
421
422/* valid values for mpt_flags (see also notes on mpts_flags above) */
0a7de745
A
423#define MPTCPF_CHECKSUM 0x001 /* checksum DSS option */
424#define MPTCPF_FALLBACK_TO_TCP 0x002 /* Fallback to TCP */
425#define MPTCPF_JOIN_READY 0x004 /* Ready to start 2 or more subflows */
426#define MPTCPF_RECVD_MPFAIL 0x008 /* Received MP_FAIL option */
427#define MPTCPF_SND_64BITDSN 0x010 /* Send full 64-bit DSN */
428#define MPTCPF_SND_64BITACK 0x020 /* Send 64-bit ACK response */
429#define MPTCPF_RCVD_64BITACK 0x040 /* Received 64-bit Data ACK */
430#define MPTCPF_POST_FALLBACK_SYNC 0x080 /* Post fallback resend data */
431#define MPTCPF_FALLBACK_HEURISTIC 0x100 /* Send SYN without MP_CAPABLE due to heuristic */
432#define MPTCPF_HEURISTIC_TRAC 0x200 /* Tracked this connection in the heuristics as a failure */
433#define MPTCPF_REASS_INPROG 0x400 /* Reassembly is in progress */
cb323159 434#define MPTCPF_UNICAST_IP 0x800
0a7de745
A
435
436#define MPTCPF_BITS \
5ba3f43e
A
437 "\020\1CHECKSUM\2FALLBACK_TO_TCP\3JOIN_READY\4RECVD_MPFAIL" \
438 "\5SND_64BITDSN\6SND_64BITACK\7RCVD_64BITACK\10POST_FALLBACK_SYNC" \
439 "\11FALLBACK_HEURISTIC\12HEURISTIC_TRAC\13REASS_INPROG"
39236c6e
A
440
441/* valid values for mpt_timer_vals */
0a7de745
A
442#define MPTT_REXMT 0x01 /* Starting Retransmit Timer */
443#define MPTT_TW 0x02 /* Starting Timewait Timer */
444#define MPTT_FASTCLOSE 0x04 /* Starting Fastclose wait timer */
39236c6e
A
445
446/* events for close FSM */
0a7de745
A
447#define MPCE_CLOSE 0x1
448#define MPCE_RECV_DATA_ACK 0x2
449#define MPCE_RECV_DATA_FIN 0x4
39236c6e
A
450
451/* mptcb manipulation */
0a7de745
A
452static inline struct mptcb *
453tptomptp(struct tcpcb *tp)
5ba3f43e 454{
0a7de745 455 return tp->t_mptcb;
5ba3f43e 456}
39236c6e
A
457
458/*
459 * MPTCP control block and state structures are allocated along with
460 * the MP protocol control block; the folllowing represents the layout.
461 */
462struct mpp_mtp {
0a7de745
A
463 struct mppcb mpp; /* Multipath PCB */
464 struct mptses mpp_ses; /* MPTCP session */
465 struct mptcb mtcb; /* MPTCP PCB */
39236c6e
A
466};
467
468#ifdef SYSCTL_DECL
469SYSCTL_DECL(_net_inet_mptcp);
470#endif /* SYSCTL_DECL */
471
472extern struct mppcbinfo mtcbinfo;
473extern struct pr_usrreqs mptcp_usrreqs;
a39ff7e2 474extern os_log_t mptcp_log_handle;
39236c6e
A
475
476/* Encryption algorithm related definitions */
0a7de745 477#define SHA1_TRUNCATED 8
39236c6e 478
39236c6e 479/* MPTCP Debugging Levels */
0a7de745
A
480#define MPTCP_LOGLVL_NONE 0x0 /* No debug logging */
481#define MPTCP_LOGLVL_ERR 0x1 /* Errors in execution are logged */
482#define MPTCP_LOGLVL_LOG 0x2 /* Important logs */
483#define MPTCP_LOGLVL_VERBOSE 0x4 /* Verbose logs */
3e170ce0
A
484
485/* MPTCP sub-components for debug logging */
0a7de745
A
486#define MPTCP_NO_DBG 0x00 /* No areas are logged */
487#define MPTCP_STATE_DBG 0x01 /* State machine logging */
488#define MPTCP_SOCKET_DBG 0x02 /* Socket call logging */
489#define MPTCP_SENDER_DBG 0x04 /* Sender side logging */
490#define MPTCP_RECEIVER_DBG 0x08 /* Receiver logging */
491#define MPTCP_EVENTS_DBG 0x10 /* Subflow events logging */
39236c6e
A
492
493/* Mask to obtain 32-bit portion of data sequence number */
0a7de745
A
494#define MPTCP_DATASEQ_LOW32_MASK (0xffffffff)
495#define MPTCP_DATASEQ_LOW32(seq) (seq & MPTCP_DATASEQ_LOW32_MASK)
39236c6e
A
496
497/* Mask to obtain upper 32-bit portion of data sequence number */
0a7de745
A
498#define MPTCP_DATASEQ_HIGH32_MASK (0xffffffff00000000)
499#define MPTCP_DATASEQ_HIGH32(seq) (seq & MPTCP_DATASEQ_HIGH32_MASK)
39236c6e
A
500
501/* Mask to obtain 32-bit portion of data ack */
0a7de745
A
502#define MPTCP_DATAACK_LOW32_MASK (0xffffffff)
503#define MPTCP_DATAACK_LOW32(ack) (ack & MPTCP_DATAACK_LOW32_MASK)
39236c6e
A
504
505/* Mask to obtain upper 32-bit portion of data ack */
0a7de745
A
506#define MPTCP_DATAACK_HIGH32_MASK (0xffffffff00000000)
507#define MPTCP_DATAACK_HIGH32(ack) (ack & MPTCP_DATAACK_HIGH32_MASK)
39236c6e
A
508
509/*
510 * x is the 64-bit data sequence number, y the 32-bit data seq number to be
511 * extended. z is y extended to the appropriate 64-bit value.
512 * This algorithm is based on the fact that subflow level window sizes are
513 * at the maximum 2**30 (in reality, they are a lot lesser). A high throughput
514 * application sending on a large number of subflows can in theory have very
515 * large MPTCP level send and receive windows. In which case, 64 bit DSNs
516 * must be sent in place of 32 bit DSNs on wire. For us, with 2 subflows at
517 * 512K each, sequence wraparound detection can be done by checking whether
518 * the 32-bit value obtained on wire is 2**31 bytes apart from the stored
519 * lower 32-bits of the Data Sequence Number. Bogus DSNs are dropped by
520 * comparing against rwnd. Bogus DSNs within rwnd cannot be protected against
521 * and are as weak as bogus TCP sequence numbers.
522 */
0a7de745
A
523#define MPTCP_EXTEND_DSN(x, y, z) { \
524 if ((MPTCP_DATASEQ_LOW32(x) > y) && \
525 ((((u_int32_t)MPTCP_DATASEQ_LOW32(x)) - (u_int32_t)y) >= \
cb323159 526 (u_int32_t)(1U << 31))) { \
0a7de745
A
527 /* \
528 * y wrapped around and x and y are 2**31 bytes apart \
529 */ \
530 z = MPTCP_DATASEQ_HIGH32(x) + 0x100000000; \
531 z |= y; \
532 } else if ((MPTCP_DATASEQ_LOW32(x) < y) && \
533 (((u_int32_t)y - \
534 ((u_int32_t)MPTCP_DATASEQ_LOW32(x))) >= \
cb323159 535 (u_int32_t)(1U << 31))) { \
0a7de745
A
536 /* \
537 * x wrapped around and x and y are 2**31 apart \
538 */ \
539 z = MPTCP_DATASEQ_HIGH32(x) - 0x100000000; \
540 z |= y; \
541 } else { \
542 z = MPTCP_DATASEQ_HIGH32(x) | y; \
543 } \
39236c6e
A
544}
545
0a7de745
A
546#define mptcplog(x, y, z) do { \
547 if ((mptcp_dbg_area & y) && (mptcp_dbg_level & z)) \
548 log x; \
3e170ce0 549} while (0)
39236c6e 550
0a7de745
A
551extern int mptcp_enable; /* Multipath TCP */
552extern int mptcp_mpcap_retries; /* Multipath TCP retries */
553extern int mptcp_join_retries; /* Multipath TCP Join retries */
554extern int mptcp_dss_csum; /* Multipath DSS Option checksum */
555extern int mptcp_fail_thresh; /* Multipath failover thresh of retransmits */
39236c6e 556extern int mptcp_subflow_keeptime; /* Multipath subflow TCP_KEEPALIVE opt */
0a7de745
A
557extern uint32_t mptcp_dbg_level; /* Multipath TCP debugging level */
558extern uint32_t mptcp_dbg_area; /* Multipath TCP debugging area */
559extern int mptcp_developer_mode; /* Allow aggregation mode */
cb323159 560extern uint32_t mptcp_cellicon_refcount;
3e170ce0 561
94ff46dc
A
562#define MPTCP_CELLICON_TOGGLE_RATE (5 * TCP_RETRANSHZ) /* Only toggle every 5 seconds */
563
0a7de745 564extern int tcp_jack_rxmt; /* Join ACK retransmission value in msecs */
39236c6e
A
565
566__BEGIN_DECLS
567extern void mptcp_init(struct protosw *, struct domain *);
568extern int mptcp_ctloutput(struct socket *, struct sockopt *);
cb323159
A
569extern int mptcp_session_create(struct mppcb *);
570extern boolean_t mptcp_ok_to_create_subflows(struct mptcb *mp_tp);
571extern void mptcp_check_subflows_and_add(struct mptses *mpte);
572extern void mptcp_check_subflows_and_remove(struct mptses *mpte);
573extern void mptcpstats_inc_switch(struct mptses *mpte, const struct mptsub *mpts);
574extern void mptcpstats_update(struct mptcp_itf_stats *stats, const struct mptsub *mpts);
f427ee49
A
575extern int mptcpstats_get_index_by_ifindex(struct mptcp_itf_stats *stats, u_short ifindex, boolean_t create);
576extern struct mptses *mptcp_drop(struct mptses *mpte, struct mptcb *mp_tp, u_short errno);
39236c6e
A
577extern struct mptses *mptcp_close(struct mptses *, struct mptcb *);
578extern int mptcp_lock(struct socket *, int, void *);
579extern int mptcp_unlock(struct socket *, int, void *);
580extern lck_mtx_t *mptcp_getlock(struct socket *, int);
5ba3f43e
A
581extern void mptcp_subflow_workloop(struct mptses *);
582
583extern void mptcp_sched_create_subflows(struct mptses *);
39236c6e 584
a39ff7e2 585extern void mptcp_finish_usrclosed(struct mptses *mpte);
f427ee49 586extern struct mptopt *mptcp_sopt_alloc(zalloc_flags_t);
5ba3f43e 587extern const char *mptcp_sopt2str(int, int);
39236c6e
A
588extern void mptcp_sopt_free(struct mptopt *);
589extern void mptcp_sopt_insert(struct mptses *, struct mptopt *);
590extern void mptcp_sopt_remove(struct mptses *, struct mptopt *);
591extern struct mptopt *mptcp_sopt_find(struct mptses *, struct sockopt *);
592
5ba3f43e
A
593extern int mptcp_subflow_add(struct mptses *, struct sockaddr *,
594 struct sockaddr *, uint32_t, sae_connid_t *);
5ba3f43e
A
595extern void mptcp_subflow_del(struct mptses *, struct mptsub *);
596
cb323159 597extern void mptcp_handle_input(struct socket *so);
0a7de745 598#define MPTCP_SUBOUT_PROBING 0x01
5ba3f43e
A
599extern int mptcp_subflow_output(struct mptses *mpte, struct mptsub *mpts, int flags);
600extern void mptcp_clean_reinjectq(struct mptses *mpte);
601extern void mptcp_subflow_shutdown(struct mptses *, struct mptsub *);
602extern void mptcp_subflow_disconnect(struct mptses *, struct mptsub *);
603extern int mptcp_subflow_sosetopt(struct mptses *, struct mptsub *,
39236c6e
A
604 struct mptopt *);
605extern int mptcp_subflow_sogetopt(struct mptses *, struct socket *,
606 struct mptopt *);
607
608extern void mptcp_input(struct mptses *, struct mbuf *);
a39ff7e2 609extern boolean_t mptcp_can_send_more(struct mptcb *mp_tp, boolean_t ignore_reinject);
39236c6e
A
610extern int mptcp_output(struct mptses *);
611extern void mptcp_close_fsm(struct mptcb *, uint32_t);
612
39236c6e 613extern void mptcp_hmac_sha1(mptcp_key_t, mptcp_key_t, u_int32_t, u_int32_t,
5ba3f43e
A
614 u_char*);
615extern void mptcp_get_hmac(mptcp_addr_id, struct mptcb *, u_char *);
39236c6e
A
616extern void mptcp_get_rands(mptcp_addr_id, struct mptcb *, u_int32_t *,
617 u_int32_t *);
618extern void mptcp_set_raddr_rand(mptcp_addr_id, struct mptcb *, mptcp_addr_id,
619 u_int32_t);
490019cf 620extern int mptcp_init_remote_parms(struct mptcb *);
39236c6e
A
621extern boolean_t mptcp_ok_to_keepalive(struct mptcb *);
622extern void mptcp_insert_dsn(struct mppcb *, struct mbuf *);
5ba3f43e 623extern void mptcp_output_getm_dsnmap32(struct socket *so, int off,
0a7de745
A
624 uint32_t *dsn, uint32_t *relseq,
625 uint16_t *data_len, uint16_t *dss_csum);
5ba3f43e 626extern void mptcp_output_getm_dsnmap64(struct socket *so, int off,
0a7de745
A
627 uint64_t *dsn, uint32_t *relseq,
628 uint16_t *data_len, uint16_t *dss_csum);
39236c6e 629extern void mptcp_act_on_txfail(struct socket *);
cb323159 630extern struct mptsub *mptcp_get_subflow(struct mptses *mpte, struct mptsub **preferred);
f427ee49 631extern int mptcp_get_map_for_dsn(struct socket *so, uint64_t dsn_fail, uint32_t *tcp_seq);
5ba3f43e
A
632extern int32_t mptcp_adj_sendlen(struct socket *so, int32_t off);
633extern void mptcp_sbrcv_grow(struct mptcb *mp_tp);
39236c6e
A
634extern int32_t mptcp_sbspace(struct mptcb *);
635extern void mptcp_notify_mpready(struct socket *);
636extern void mptcp_notify_mpfail(struct socket *);
637extern void mptcp_notify_close(struct socket *);
fe8ab488
A
638extern boolean_t mptcp_no_rto_spike(struct socket*);
639extern int mptcp_set_notsent_lowat(struct mptses *mpte, int optval);
640extern u_int32_t mptcp_get_notsent_lowat(struct mptses *mpte);
641extern int mptcp_notsent_lowat_check(struct socket *so);
5ba3f43e 642extern void mptcp_ask_symptoms(struct mptses *mpte);
3e170ce0 643extern void mptcp_control_register(void);
cb323159
A
644extern int mptcp_is_wifi_unusable_for_session(struct mptses *mpte);
645extern boolean_t symptoms_is_wifi_lossy(void);
d9a64523 646extern void mptcp_session_necp_cb(void *, int, uint32_t, uint32_t, bool *);
cb323159
A
647extern struct sockaddr *mptcp_get_session_dst(struct mptses *mpte,
648 boolean_t has_v6, boolean_t has_v4);
5ba3f43e 649extern void mptcp_set_restrictions(struct socket *mp_so);
cb323159 650extern void mptcp_clear_cellicon(void);
94ff46dc 651extern void mptcp_unset_cellicon(struct mptses *mpte, struct mptsub *mpts, uint32_t val);
5ba3f43e
A
652extern void mptcp_reset_rexmit_state(struct tcpcb *tp);
653extern void mptcp_reset_keepalive(struct tcpcb *tp);
654extern int mptcp_validate_csum(struct tcpcb *tp, struct mbuf *m, uint64_t dsn,
f427ee49 655 uint32_t sseq, uint16_t dlen, uint16_t csum, int dfin);
39236c6e
A
656__END_DECLS
657
658#endif /* BSD_KERNEL_PRIVATE */
659#ifdef PRIVATE
3e170ce0 660
39236c6e 661typedef struct mptcp_flow {
f427ee49
A
662 uint64_t flow_len;
663 uint64_t flow_tcpci_offset;
0a7de745
A
664 uint32_t flow_flags;
665 sae_connid_t flow_cid;
39236c6e
A
666 struct sockaddr_storage flow_src;
667 struct sockaddr_storage flow_dst;
0a7de745
A
668 uint32_t flow_relseq; /* last subflow rel seq# */
669 int32_t flow_soerror; /* subflow level error */
670 uint32_t flow_probecnt; /* number of probes sent */
671 conninfo_tcp_t flow_ci; /* must be the last field */
39236c6e
A
672} mptcp_flow_t;
673
674typedef struct conninfo_mptcp {
f427ee49
A
675 uint64_t mptcpci_len;
676 uint64_t mptcpci_flow_offset; /* offsetof first flow */
677 uint64_t mptcpci_nflows; /* number of subflows */
0a7de745
A
678 uint32_t mptcpci_state; /* MPTCP level state */
679 uint32_t mptcpci_mpte_flags; /* Session flags */
680 uint32_t mptcpci_flags; /* MPTCB flags */
681 uint32_t mptcpci_ltoken; /* local token */
682 uint32_t mptcpci_rtoken; /* remote token */
683 uint32_t mptcpci_notsent_lowat; /* NOTSENT_LOWAT */
3e170ce0
A
684
685 /* Send side */
0a7de745
A
686 uint64_t mptcpci_snduna; /* DSN of last unacked byte */
687 uint64_t mptcpci_sndnxt; /* DSN of next byte to send */
688 uint64_t mptcpci_sndmax; /* DSN of max byte sent */
689 uint64_t mptcpci_lidsn; /* Local IDSN */
690 uint32_t mptcpci_sndwnd; /* Send window snapshot */
3e170ce0
A
691
692 /* Receive side */
0a7de745
A
693 uint64_t mptcpci_rcvnxt; /* Next expected DSN */
694 uint64_t mptcpci_rcvatmark; /* Session level rcvnxt */
695 uint64_t mptcpci_ridsn; /* Peer's IDSN */
696 uint32_t mptcpci_rcvwnd; /* Receive window */
3e170ce0 697
0a7de745 698 uint8_t mptcpci_mpte_addrid; /* last addr id */
3e170ce0 699
0a7de745 700 mptcp_flow_t mptcpci_flows[1];
39236c6e
A
701} conninfo_mptcp_t;
702
3e170ce0
A
703/* Use SymptomsD notifications of wifi and cell status in subflow selection */
704#define MPTCP_KERN_CTL_NAME "com.apple.network.advisory"
705typedef struct symptoms_advisory {
706 union {
0a7de745 707 uint32_t sa_nwk_status_int;
3e170ce0
A
708 struct {
709 union {
0a7de745
A
710#define SYMPTOMS_ADVISORY_NOCOMMENT 0x0000
711#define SYMPTOMS_ADVISORY_USEAPP 0xFFFF /* Very ugly workaround to avoid breaking backwards compatibility - ToDo: Fix it in +1 */
712 uint16_t sa_nwk_status;
3e170ce0
A
713 struct {
714#define SYMPTOMS_ADVISORY_WIFI_BAD 0x01
715#define SYMPTOMS_ADVISORY_WIFI_OK 0x02
0a7de745 716 uint8_t sa_wifi_status;
3e170ce0
A
717#define SYMPTOMS_ADVISORY_CELL_BAD 0x01
718#define SYMPTOMS_ADVISORY_CELL_OK 0x02
0a7de745 719 uint8_t sa_cell_status;
3e170ce0
A
720 };
721 };
0a7de745 722 uint16_t sa_unused;
3e170ce0
A
723 };
724 };
725} symptoms_advisory_t;
726
cb323159
A
727#define MPTCP_TARGET_BASED_RSSI_THRESHOLD -75
728struct mptcp_symptoms_answer {
729 struct symptoms_advisory advisory;
730 uuid_t uuid;
731 int32_t rssi;
732};
733
5ba3f43e 734struct mptcp_symptoms_ask_uuid {
0a7de745
A
735 uint32_t cmd;
736#define MPTCP_SYMPTOMS_ASK_UUID 1
737 uuid_t uuid;
738 uint32_t priority;
739#define MPTCP_SYMPTOMS_UNKNOWN 0
740#define MPTCP_SYMPTOMS_BACKGROUND 1
741#define MPTCP_SYMPTOMS_FOREGROUND 2
5ba3f43e
A
742};
743
744struct kev_mptcp_data {
745 int value;
746};
3e170ce0 747
39236c6e
A
748#endif /* PRIVATE */
749#endif /* _NETINET_MPTCP_VAR_H_ */