2 * Copyright (c) 2012-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <kern/locks.h>
30 #include <kern/policy_internal.h>
31 #include <kern/zalloc.h>
35 #include <sys/domain.h>
36 #include <sys/kdebug.h>
37 #include <sys/kern_control.h>
38 #include <sys/kernel.h>
40 #include <sys/mcache.h>
41 #include <sys/param.h>
43 #include <sys/protosw.h>
44 #include <sys/resourcevar.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/systm.h>
51 #include <net/content_filter.h>
53 #include <net/if_var.h>
54 #include <netinet/in.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/in_var.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcp_fsm.h>
59 #include <netinet/tcp_seq.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/mptcp_var.h>
62 #include <netinet/mptcp.h>
63 #include <netinet/mptcp_opt.h>
64 #include <netinet/mptcp_seq.h>
65 #include <netinet/mptcp_timer.h>
66 #include <libkern/crypto/sha1.h>
67 #include <netinet6/in6_pcb.h>
68 #include <netinet6/ip6protosw.h>
69 #include <dev/random/randomdev.h>
72 * Notes on MPTCP implementation.
74 * MPTCP is implemented as <SOCK_STREAM,IPPROTO_TCP> protocol in PF_MULTIPATH
75 * communication domain. The structure mtcbinfo describes the MPTCP instance
76 * of a Multipath protocol in that domain. It is used to keep track of all
77 * MPTCP PCB instances in the system, and is protected by the global lock
80 * An MPTCP socket is opened by calling socket(PF_MULTIPATH, SOCK_STREAM,
81 * IPPROTO_TCP). Upon success, a Multipath PCB gets allocated and along with
82 * it comes an MPTCP Session and an MPTCP PCB. All three structures are
83 * allocated from the same memory block, and each structure has a pointer
84 * to the adjacent ones. The layout is defined by the mpp_mtp structure.
85 * The socket lock (mpp_lock) is used to protect accesses to the Multipath
86 * PCB (mppcb) as well as the MPTCP Session (mptses).
88 * The MPTCP Session is an MPTCP-specific extension to the Multipath PCB;
90 * A functioning MPTCP Session consists of one or more subflow sockets. Each
91 * subflow socket is essentially a regular PF_INET/PF_INET6 TCP socket, and is
92 * represented by the mptsub structure. Because each subflow requires access
93 * to the MPTCP Session, the MPTCP socket's so_usecount is bumped up for each
94 * subflow. This gets decremented prior to the subflow's destruction.
96 * To handle events (read, write, control) from the subflows, we do direct
97 * upcalls into the specific function.
99 * The whole MPTCP connection is protected by a single lock, the MPTCP socket's
100 * lock. Incoming data on a subflow also ends up taking this single lock. To
101 * achieve the latter, tcp_lock/unlock has been changed to rather use the lock
102 * of the MPTCP-socket.
104 * An MPTCP socket will be destroyed when its so_usecount drops to zero; this
105 * work is done by the MPTCP garbage collector which is invoked on demand by
106 * the PF_MULTIPATH garbage collector. This process will take place once all
107 * of the subflows have been destroyed.
110 static void mptcp_attach_to_subf(struct socket
*, struct mptcb
*, uint8_t);
111 static void mptcp_detach_mptcb_from_subf(struct mptcb
*, struct socket
*);
113 static uint32_t mptcp_gc(struct mppcbinfo
*);
114 static int mptcp_subflow_soreceive(struct socket
*, struct sockaddr
**,
115 struct uio
*, struct mbuf
**, struct mbuf
**, int *);
116 static int mptcp_subflow_sosend(struct socket
*, struct sockaddr
*,
117 struct uio
*, struct mbuf
*, struct mbuf
*, int);
118 static void mptcp_subflow_wupcall(struct socket
*, void *, int);
119 static void mptcp_subflow_eupcall1(struct socket
*so
, void *arg
, long events
);
120 static void mptcp_update_last_owner(struct socket
*so
, struct socket
*mp_so
);
121 static void mptcp_drop_tfo_data(struct mptses
*, struct mptsub
*);
123 static void mptcp_subflow_abort(struct mptsub
*, int);
125 static void mptcp_send_dfin(struct socket
*so
);
126 static void mptcp_set_cellicon(struct mptses
*mpte
, struct mptsub
*mpts
);
127 static int mptcp_freeq(struct mptcb
*mp_tp
);
130 * Possible return values for subflow event handlers. Note that success
131 * values must be greater or equal than MPTS_EVRET_OK. Values less than that
132 * indicate errors or actions which require immediate attention; they will
133 * prevent the rest of the handlers from processing their respective events
134 * until the next round of events processing.
137 MPTS_EVRET_DELETE
= 1, /* delete this subflow */
138 MPTS_EVRET_OK
= 2, /* OK */
139 MPTS_EVRET_CONNECT_PENDING
= 3, /* resume pended connects */
140 MPTS_EVRET_DISCONNECT_FALLBACK
= 4, /* abort all but preferred */
143 static ev_ret_t
mptcp_subflow_propagate_ev(struct mptses
*, struct mptsub
*, long *, long);
144 static ev_ret_t
mptcp_subflow_nosrcaddr_ev(struct mptses
*, struct mptsub
*, long *, long);
145 static ev_ret_t
mptcp_subflow_failover_ev(struct mptses
*, struct mptsub
*, long *, long);
146 static ev_ret_t
mptcp_subflow_ifdenied_ev(struct mptses
*, struct mptsub
*, long *, long);
147 static ev_ret_t
mptcp_subflow_connected_ev(struct mptses
*, struct mptsub
*, long *, long);
148 static ev_ret_t
mptcp_subflow_disconnected_ev(struct mptses
*, struct mptsub
*, long *, long);
149 static ev_ret_t
mptcp_subflow_mpstatus_ev(struct mptses
*, struct mptsub
*, long *, long);
150 static ev_ret_t
mptcp_subflow_mustrst_ev(struct mptses
*, struct mptsub
*, long *, long);
151 static ev_ret_t
mptcp_subflow_mpcantrcvmore_ev(struct mptses
*, struct mptsub
*, long *, long);
152 static ev_ret_t
mptcp_subflow_mpsuberror_ev(struct mptses
*, struct mptsub
*, long *, long);
153 static ev_ret_t
mptcp_subflow_adaptive_rtimo_ev(struct mptses
*, struct mptsub
*, long *, long);
154 static ev_ret_t
mptcp_subflow_adaptive_wtimo_ev(struct mptses
*, struct mptsub
*, long *, long);
156 static void mptcp_do_sha1(mptcp_key_t
*, char *);
157 static void mptcp_init_local_parms(struct mptses
*);
159 static ZONE_DECLARE(mptsub_zone
, "mptsub", sizeof(struct mptsub
), ZC_ZFREE_CLEARMEM
);
160 static ZONE_DECLARE(mptopt_zone
, "mptopt", sizeof(struct mptopt
), ZC_ZFREE_CLEARMEM
);
161 static ZONE_DECLARE(mpt_subauth_zone
, "mptauth",
162 sizeof(struct mptcp_subf_auth_entry
), ZC_NONE
);
164 struct mppcbinfo mtcbinfo
;
166 SYSCTL_DECL(_net_inet
);
168 SYSCTL_NODE(_net_inet
, OID_AUTO
, mptcp
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "MPTCP");
170 uint32_t mptcp_dbg_area
= 31; /* more noise if greater than 1 */
171 SYSCTL_UINT(_net_inet_mptcp
, OID_AUTO
, dbg_area
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
172 &mptcp_dbg_area
, 0, "MPTCP debug area");
174 uint32_t mptcp_dbg_level
= 1;
175 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, dbg_level
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
176 &mptcp_dbg_level
, 0, "MPTCP debug level");
178 SYSCTL_UINT(_net_inet_mptcp
, OID_AUTO
, pcbcount
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
179 &mtcbinfo
.mppi_count
, 0, "Number of active PCBs");
182 static int mptcp_alternate_port
= 0;
183 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, alternate_port
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
184 &mptcp_alternate_port
, 0, "Set alternate port for MPTCP connections");
186 static struct protosw mptcp_subflow_protosw
;
187 static struct pr_usrreqs mptcp_subflow_usrreqs
;
188 static struct ip6protosw mptcp_subflow_protosw6
;
189 static struct pr_usrreqs mptcp_subflow_usrreqs6
;
191 static uint8_t mptcp_create_subflows_scheduled
;
193 typedef struct mptcp_subflow_event_entry
{
194 long sofilt_hint_mask
;
195 ev_ret_t (*sofilt_hint_ev_hdlr
)(
198 long *p_mpsofilt_hint
,
202 /* Using Symptoms Advisory to detect poor WiFi or poor Cell */
203 static kern_ctl_ref mptcp_kern_ctrl_ref
= NULL
;
204 static uint32_t mptcp_kern_skt_inuse
= 0;
205 static uint32_t mptcp_kern_skt_unit
;
206 static symptoms_advisory_t mptcp_advisory
;
208 uint32_t mptcp_cellicon_refcount
= 0;
211 * XXX The order of the event handlers below is really
212 * really important. Think twice before changing it.
214 static mptsub_ev_entry_t mpsub_ev_entry_tbl
[] = {
216 .sofilt_hint_mask
= SO_FILT_HINT_MP_SUB_ERROR
,
217 .sofilt_hint_ev_hdlr
= mptcp_subflow_mpsuberror_ev
,
220 .sofilt_hint_mask
= SO_FILT_HINT_MPCANTRCVMORE
,
221 .sofilt_hint_ev_hdlr
= mptcp_subflow_mpcantrcvmore_ev
,
224 .sofilt_hint_mask
= SO_FILT_HINT_MPFAILOVER
,
225 .sofilt_hint_ev_hdlr
= mptcp_subflow_failover_ev
,
228 .sofilt_hint_mask
= SO_FILT_HINT_CONNRESET
,
229 .sofilt_hint_ev_hdlr
= mptcp_subflow_propagate_ev
,
232 .sofilt_hint_mask
= SO_FILT_HINT_MUSTRST
,
233 .sofilt_hint_ev_hdlr
= mptcp_subflow_mustrst_ev
,
236 .sofilt_hint_mask
= SO_FILT_HINT_CANTRCVMORE
,
237 .sofilt_hint_ev_hdlr
= mptcp_subflow_propagate_ev
,
240 .sofilt_hint_mask
= SO_FILT_HINT_TIMEOUT
,
241 .sofilt_hint_ev_hdlr
= mptcp_subflow_propagate_ev
,
244 .sofilt_hint_mask
= SO_FILT_HINT_NOSRCADDR
,
245 .sofilt_hint_ev_hdlr
= mptcp_subflow_nosrcaddr_ev
,
248 .sofilt_hint_mask
= SO_FILT_HINT_IFDENIED
,
249 .sofilt_hint_ev_hdlr
= mptcp_subflow_ifdenied_ev
,
252 .sofilt_hint_mask
= SO_FILT_HINT_CONNECTED
,
253 .sofilt_hint_ev_hdlr
= mptcp_subflow_connected_ev
,
256 .sofilt_hint_mask
= SO_FILT_HINT_MPSTATUS
,
257 .sofilt_hint_ev_hdlr
= mptcp_subflow_mpstatus_ev
,
260 .sofilt_hint_mask
= SO_FILT_HINT_DISCONNECTED
,
261 .sofilt_hint_ev_hdlr
= mptcp_subflow_disconnected_ev
,
264 .sofilt_hint_mask
= SO_FILT_HINT_ADAPTIVE_RTIMO
,
265 .sofilt_hint_ev_hdlr
= mptcp_subflow_adaptive_rtimo_ev
,
268 .sofilt_hint_mask
= SO_FILT_HINT_ADAPTIVE_WTIMO
,
269 .sofilt_hint_ev_hdlr
= mptcp_subflow_adaptive_wtimo_ev
,
273 os_log_t mptcp_log_handle
;
276 * Protocol pr_init callback.
279 mptcp_init(struct protosw
*pp
, struct domain
*dp
)
282 static int mptcp_initialized
= 0;
284 struct ip6protosw
*prp6
;
286 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
| PR_ATTACHED
)) == PR_ATTACHED
);
288 /* do this only once */
289 if (mptcp_initialized
) {
292 mptcp_initialized
= 1;
294 mptcp_advisory
.sa_wifi_status
= SYMPTOMS_ADVISORY_WIFI_OK
;
297 * Since PF_MULTIPATH gets initialized after PF_INET/INET6,
298 * we must be able to find IPPROTO_TCP entries for both.
300 prp
= pffindproto_locked(PF_INET
, IPPROTO_TCP
, SOCK_STREAM
);
302 bcopy(prp
, &mptcp_subflow_protosw
, sizeof(*prp
));
303 bcopy(prp
->pr_usrreqs
, &mptcp_subflow_usrreqs
,
304 sizeof(mptcp_subflow_usrreqs
));
305 mptcp_subflow_protosw
.pr_entry
.tqe_next
= NULL
;
306 mptcp_subflow_protosw
.pr_entry
.tqe_prev
= NULL
;
307 mptcp_subflow_protosw
.pr_usrreqs
= &mptcp_subflow_usrreqs
;
308 mptcp_subflow_usrreqs
.pru_soreceive
= mptcp_subflow_soreceive
;
309 mptcp_subflow_usrreqs
.pru_sosend
= mptcp_subflow_sosend
;
310 mptcp_subflow_usrreqs
.pru_rcvoob
= pru_rcvoob_notsupp
;
312 * Socket filters shouldn't attach/detach to/from this protosw
313 * since pr_protosw is to be used instead, which points to the
314 * real protocol; if they do, it is a bug and we should panic.
316 mptcp_subflow_protosw
.pr_filter_head
.tqh_first
=
317 (struct socket_filter
*)(uintptr_t)0xdeadbeefdeadbeef;
318 mptcp_subflow_protosw
.pr_filter_head
.tqh_last
=
319 (struct socket_filter
**)(uintptr_t)0xdeadbeefdeadbeef;
321 prp6
= (struct ip6protosw
*)pffindproto_locked(PF_INET6
,
322 IPPROTO_TCP
, SOCK_STREAM
);
323 VERIFY(prp6
!= NULL
);
324 bcopy(prp6
, &mptcp_subflow_protosw6
, sizeof(*prp6
));
325 bcopy(prp6
->pr_usrreqs
, &mptcp_subflow_usrreqs6
,
326 sizeof(mptcp_subflow_usrreqs6
));
327 mptcp_subflow_protosw6
.pr_entry
.tqe_next
= NULL
;
328 mptcp_subflow_protosw6
.pr_entry
.tqe_prev
= NULL
;
329 mptcp_subflow_protosw6
.pr_usrreqs
= &mptcp_subflow_usrreqs6
;
330 mptcp_subflow_usrreqs6
.pru_soreceive
= mptcp_subflow_soreceive
;
331 mptcp_subflow_usrreqs6
.pru_sosend
= mptcp_subflow_sosend
;
332 mptcp_subflow_usrreqs6
.pru_rcvoob
= pru_rcvoob_notsupp
;
334 * Socket filters shouldn't attach/detach to/from this protosw
335 * since pr_protosw is to be used instead, which points to the
336 * real protocol; if they do, it is a bug and we should panic.
338 mptcp_subflow_protosw6
.pr_filter_head
.tqh_first
=
339 (struct socket_filter
*)(uintptr_t)0xdeadbeefdeadbeef;
340 mptcp_subflow_protosw6
.pr_filter_head
.tqh_last
=
341 (struct socket_filter
**)(uintptr_t)0xdeadbeefdeadbeef;
343 bzero(&mtcbinfo
, sizeof(mtcbinfo
));
344 TAILQ_INIT(&mtcbinfo
.mppi_pcbs
);
345 mtcbinfo
.mppi_size
= sizeof(struct mpp_mtp
);
346 mtcbinfo
.mppi_zone
= zone_create("mptc", mtcbinfo
.mppi_size
,
349 mtcbinfo
.mppi_lock_grp_attr
= lck_grp_attr_alloc_init();
350 mtcbinfo
.mppi_lock_grp
= lck_grp_alloc_init("mppcb",
351 mtcbinfo
.mppi_lock_grp_attr
);
352 mtcbinfo
.mppi_lock_attr
= lck_attr_alloc_init();
353 lck_mtx_init(&mtcbinfo
.mppi_lock
, mtcbinfo
.mppi_lock_grp
,
354 mtcbinfo
.mppi_lock_attr
);
356 mtcbinfo
.mppi_gc
= mptcp_gc
;
357 mtcbinfo
.mppi_timer
= mptcp_timer
;
359 /* attach to MP domain for garbage collection to take place */
360 mp_pcbinfo_attach(&mtcbinfo
);
362 mptcp_log_handle
= os_log_create("com.apple.xnu.net.mptcp", "mptcp");
366 mptcpstats_get_index_by_ifindex(struct mptcp_itf_stats
*stats
, u_short ifindex
, boolean_t create
)
370 for (i
= 0; i
< MPTCP_ITFSTATS_SIZE
; i
++) {
371 if (create
&& stats
[i
].ifindex
== IFSCOPE_NONE
) {
378 if (stats
[i
].ifindex
== ifindex
) {
385 stats
[index
].ifindex
= ifindex
;
392 mptcpstats_get_index(struct mptcp_itf_stats
*stats
, const struct mptsub
*mpts
)
394 const struct ifnet
*ifp
= sotoinpcb(mpts
->mpts_socket
)->inp_last_outifp
;
398 os_log_error(mptcp_log_handle
, "%s - %lx: no ifp on subflow, state %u flags %#x\n",
399 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpts
->mpts_mpte
),
400 sototcpcb(mpts
->mpts_socket
)->t_state
, mpts
->mpts_flags
);
404 index
= mptcpstats_get_index_by_ifindex(stats
, ifp
->if_index
, true);
407 if (stats
[index
].is_expensive
== 0) {
408 stats
[index
].is_expensive
= IFNET_IS_CELLULAR(ifp
);
416 mptcpstats_inc_switch(struct mptses
*mpte
, const struct mptsub
*mpts
)
420 tcpstat
.tcps_mp_switches
++;
421 mpte
->mpte_subflow_switches
++;
423 index
= mptcpstats_get_index(mpte
->mpte_itfstats
, mpts
);
426 mpte
->mpte_itfstats
[index
].switches
++;
431 * Flushes all recorded socket options from an MP socket.
434 mptcp_flush_sopts(struct mptses
*mpte
)
436 struct mptopt
*mpo
, *tmpo
;
438 TAILQ_FOREACH_SAFE(mpo
, &mpte
->mpte_sopts
, mpo_entry
, tmpo
) {
439 mptcp_sopt_remove(mpte
, mpo
);
440 mptcp_sopt_free(mpo
);
442 VERIFY(TAILQ_EMPTY(&mpte
->mpte_sopts
));
446 * Create an MPTCP session, called as a result of opening a MPTCP socket.
449 mptcp_session_create(struct mppcb
*mpp
)
451 struct mppcbinfo
*mppi
;
456 mppi
= mpp
->mpp_pcbinfo
;
457 VERIFY(mppi
!= NULL
);
459 __IGNORE_WCASTALIGN(mpte
= &((struct mpp_mtp
*)mpp
)->mpp_ses
);
460 __IGNORE_WCASTALIGN(mp_tp
= &((struct mpp_mtp
*)mpp
)->mtcb
);
462 /* MPTCP Multipath PCB Extension */
463 bzero(mpte
, sizeof(*mpte
));
464 VERIFY(mpp
->mpp_pcbe
== NULL
);
465 mpp
->mpp_pcbe
= mpte
;
466 mpte
->mpte_mppcb
= mpp
;
467 mpte
->mpte_mptcb
= mp_tp
;
469 TAILQ_INIT(&mpte
->mpte_sopts
);
470 TAILQ_INIT(&mpte
->mpte_subflows
);
471 mpte
->mpte_associd
= SAE_ASSOCID_ANY
;
472 mpte
->mpte_connid_last
= SAE_CONNID_ANY
;
474 mptcp_init_urgency_timer(mpte
);
476 mpte
->mpte_itfinfo
= &mpte
->_mpte_itfinfo
[0];
477 mpte
->mpte_itfinfo_size
= MPTE_ITFINFO_SIZE
;
479 if (mptcp_alternate_port
> 0 && mptcp_alternate_port
< UINT16_MAX
) {
480 mpte
->mpte_alternate_port
= htons((uint16_t)mptcp_alternate_port
);
483 mpte
->mpte_last_cellicon_set
= tcp_now
;
485 /* MPTCP Protocol Control Block */
486 bzero(mp_tp
, sizeof(*mp_tp
));
487 mp_tp
->mpt_mpte
= mpte
;
488 mp_tp
->mpt_state
= MPTCPS_CLOSED
;
490 DTRACE_MPTCP1(session__create
, struct mppcb
*, mpp
);
496 mptcp_get_session_dst(struct mptses
*mpte
, boolean_t ipv6
, boolean_t ipv4
)
498 if (!(mpte
->mpte_flags
& MPTE_UNICAST_IP
)) {
499 return &mpte
->mpte_dst
;
502 if (ipv6
&& mpte
->mpte_dst_unicast_v6
.sin6_family
== AF_INET6
) {
503 return (struct sockaddr
*)&mpte
->mpte_dst_unicast_v6
;
506 if (ipv4
&& mpte
->mpte_dst_unicast_v4
.sin_family
== AF_INET
) {
507 return (struct sockaddr
*)&mpte
->mpte_dst_unicast_v4
;
510 /* The interface has neither IPv4 nor IPv6 routes. Give our best guess,
511 * meaning we prefer IPv6 over IPv4.
513 if (mpte
->mpte_dst_unicast_v6
.sin6_family
== AF_INET6
) {
514 return (struct sockaddr
*)&mpte
->mpte_dst_unicast_v6
;
517 if (mpte
->mpte_dst_unicast_v4
.sin_family
== AF_INET
) {
518 return (struct sockaddr
*)&mpte
->mpte_dst_unicast_v4
;
521 /* We don't yet have a unicast IP */
526 mptcpstats_get_bytes(struct mptses
*mpte
, boolean_t initial_cell
,
527 uint64_t *cellbytes
, uint64_t *allbytes
)
529 int64_t mycellbytes
= 0;
530 uint64_t myallbytes
= 0;
533 for (i
= 0; i
< MPTCP_ITFSTATS_SIZE
; i
++) {
534 if (mpte
->mpte_itfstats
[i
].is_expensive
) {
535 mycellbytes
+= mpte
->mpte_itfstats
[i
].mpis_txbytes
;
536 mycellbytes
+= mpte
->mpte_itfstats
[i
].mpis_rxbytes
;
539 myallbytes
+= mpte
->mpte_itfstats
[i
].mpis_txbytes
;
540 myallbytes
+= mpte
->mpte_itfstats
[i
].mpis_rxbytes
;
544 mycellbytes
-= mpte
->mpte_init_txbytes
;
545 mycellbytes
-= mpte
->mpte_init_rxbytes
;
548 if (mycellbytes
< 0) {
549 os_log_error(mptcp_log_handle
, "%s - %lx: cellbytes is %lld\n",
550 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mycellbytes
);
554 *cellbytes
= mycellbytes
;
555 *allbytes
= myallbytes
;
560 mptcpstats_session_wrapup(struct mptses
*mpte
)
562 boolean_t cell
= mpte
->mpte_initial_cell
;
564 switch (mpte
->mpte_svctype
) {
565 case MPTCP_SVCTYPE_HANDOVER
:
566 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
567 tcpstat
.tcps_mptcp_fp_handover_attempt
++;
569 if (cell
&& mpte
->mpte_handshake_success
) {
570 tcpstat
.tcps_mptcp_fp_handover_success_cell
++;
572 if (mpte
->mpte_used_wifi
) {
573 tcpstat
.tcps_mptcp_handover_wifi_from_cell
++;
575 } else if (mpte
->mpte_handshake_success
) {
576 tcpstat
.tcps_mptcp_fp_handover_success_wifi
++;
578 if (mpte
->mpte_used_cell
) {
579 tcpstat
.tcps_mptcp_handover_cell_from_wifi
++;
583 tcpstat
.tcps_mptcp_handover_attempt
++;
585 if (cell
&& mpte
->mpte_handshake_success
) {
586 tcpstat
.tcps_mptcp_handover_success_cell
++;
588 if (mpte
->mpte_used_wifi
) {
589 tcpstat
.tcps_mptcp_handover_wifi_from_cell
++;
591 } else if (mpte
->mpte_handshake_success
) {
592 tcpstat
.tcps_mptcp_handover_success_wifi
++;
594 if (mpte
->mpte_used_cell
) {
595 tcpstat
.tcps_mptcp_handover_cell_from_wifi
++;
600 if (mpte
->mpte_handshake_success
) {
604 mptcpstats_get_bytes(mpte
, cell
, &cellbytes
, &allbytes
);
606 tcpstat
.tcps_mptcp_handover_cell_bytes
+= cellbytes
;
607 tcpstat
.tcps_mptcp_handover_all_bytes
+= allbytes
;
610 case MPTCP_SVCTYPE_INTERACTIVE
:
611 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
612 tcpstat
.tcps_mptcp_fp_interactive_attempt
++;
614 if (mpte
->mpte_handshake_success
) {
615 tcpstat
.tcps_mptcp_fp_interactive_success
++;
617 if (!cell
&& mpte
->mpte_used_cell
) {
618 tcpstat
.tcps_mptcp_interactive_cell_from_wifi
++;
622 tcpstat
.tcps_mptcp_interactive_attempt
++;
624 if (mpte
->mpte_handshake_success
) {
625 tcpstat
.tcps_mptcp_interactive_success
++;
627 if (!cell
&& mpte
->mpte_used_cell
) {
628 tcpstat
.tcps_mptcp_interactive_cell_from_wifi
++;
633 if (mpte
->mpte_handshake_success
) {
637 mptcpstats_get_bytes(mpte
, cell
, &cellbytes
, &allbytes
);
639 tcpstat
.tcps_mptcp_interactive_cell_bytes
+= cellbytes
;
640 tcpstat
.tcps_mptcp_interactive_all_bytes
+= allbytes
;
643 case MPTCP_SVCTYPE_AGGREGATE
:
644 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
645 tcpstat
.tcps_mptcp_fp_aggregate_attempt
++;
647 if (mpte
->mpte_handshake_success
) {
648 tcpstat
.tcps_mptcp_fp_aggregate_success
++;
651 tcpstat
.tcps_mptcp_aggregate_attempt
++;
653 if (mpte
->mpte_handshake_success
) {
654 tcpstat
.tcps_mptcp_aggregate_success
++;
658 if (mpte
->mpte_handshake_success
) {
662 mptcpstats_get_bytes(mpte
, cell
, &cellbytes
, &allbytes
);
664 tcpstat
.tcps_mptcp_aggregate_cell_bytes
+= cellbytes
;
665 tcpstat
.tcps_mptcp_aggregate_all_bytes
+= allbytes
;
670 if (cell
&& mpte
->mpte_handshake_success
&& mpte
->mpte_used_wifi
) {
671 tcpstat
.tcps_mptcp_back_to_wifi
++;
674 if (mpte
->mpte_triggered_cell
) {
675 tcpstat
.tcps_mptcp_triggered_cell
++;
680 * Destroy an MPTCP session.
683 mptcp_session_destroy(struct mptses
*mpte
)
685 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
687 VERIFY(mp_tp
!= NULL
);
688 VERIFY(TAILQ_EMPTY(&mpte
->mpte_subflows
) && mpte
->mpte_numflows
== 0);
690 mptcpstats_session_wrapup(mpte
);
691 mptcp_unset_cellicon(mpte
, NULL
, mpte
->mpte_cellicon_increments
);
692 mptcp_flush_sopts(mpte
);
694 if (mpte
->mpte_itfinfo_size
> MPTE_ITFINFO_SIZE
) {
695 _FREE(mpte
->mpte_itfinfo
, M_TEMP
);
697 mpte
->mpte_itfinfo
= NULL
;
699 m_freem_list(mpte
->mpte_reinjectq
);
701 os_log(mptcp_log_handle
, "%s - %lx: Destroying session\n",
702 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
706 mptcp_ok_to_create_subflows(struct mptcb
*mp_tp
)
708 return mp_tp
->mpt_state
>= MPTCPS_ESTABLISHED
&&
709 mp_tp
->mpt_state
< MPTCPS_FIN_WAIT_1
&&
710 !(mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
);
714 mptcp_synthesize_nat64(struct in6_addr
*addr
, uint32_t len
,
715 const struct in_addr
*addrv4
)
717 static const struct in6_addr well_known_prefix
= {
718 .__u6_addr
.__u6_addr8
= {0x00, 0x64, 0xff, 0x9b, 0x00, 0x00,
719 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
720 0x00, 0x00, 0x00, 0x00},
722 const char *ptrv4
= (const char *)addrv4
;
723 char *ptr
= (char *)addr
;
725 if (IN_ZERONET(ntohl(addrv4
->s_addr
)) || // 0.0.0.0/8 Source hosts on local network
726 IN_LOOPBACK(ntohl(addrv4
->s_addr
)) || // 127.0.0.0/8 Loopback
727 IN_LINKLOCAL(ntohl(addrv4
->s_addr
)) || // 169.254.0.0/16 Link Local
728 IN_DS_LITE(ntohl(addrv4
->s_addr
)) || // 192.0.0.0/29 DS-Lite
729 IN_6TO4_RELAY_ANYCAST(ntohl(addrv4
->s_addr
)) || // 192.88.99.0/24 6to4 Relay Anycast
730 IN_MULTICAST(ntohl(addrv4
->s_addr
)) || // 224.0.0.0/4 Multicast
731 INADDR_BROADCAST
== addrv4
->s_addr
) { // 255.255.255.255/32 Limited Broadcast
735 /* Check for the well-known prefix */
736 if (len
== NAT64_PREFIX_LEN_96
&&
737 IN6_ARE_ADDR_EQUAL(addr
, &well_known_prefix
)) {
738 if (IN_PRIVATE(ntohl(addrv4
->s_addr
)) || // 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 Private-Use
739 IN_SHARED_ADDRESS_SPACE(ntohl(addrv4
->s_addr
))) { // 100.64.0.0/10 Shared Address Space
745 case NAT64_PREFIX_LEN_96
:
746 memcpy(ptr
+ 12, ptrv4
, 4);
748 case NAT64_PREFIX_LEN_64
:
749 memcpy(ptr
+ 9, ptrv4
, 4);
751 case NAT64_PREFIX_LEN_56
:
752 memcpy(ptr
+ 7, ptrv4
, 1);
753 memcpy(ptr
+ 9, ptrv4
+ 1, 3);
755 case NAT64_PREFIX_LEN_48
:
756 memcpy(ptr
+ 6, ptrv4
, 2);
757 memcpy(ptr
+ 9, ptrv4
+ 2, 2);
759 case NAT64_PREFIX_LEN_40
:
760 memcpy(ptr
+ 5, ptrv4
, 3);
761 memcpy(ptr
+ 9, ptrv4
+ 3, 1);
763 case NAT64_PREFIX_LEN_32
:
764 memcpy(ptr
+ 4, ptrv4
, 4);
767 panic("NAT64-prefix len is wrong: %u\n", len
);
774 mptcp_trigger_cell_bringup(struct mptses
*mpte
)
776 struct socket
*mp_so
= mptetoso(mpte
);
778 if (!uuid_is_null(mpsotomppcb(mp_so
)->necp_client_uuid
)) {
779 uuid_string_t uuidstr
;
782 socket_unlock(mp_so
, 0);
783 err
= necp_client_assert_bb_radio_manager(mpsotomppcb(mp_so
)->necp_client_uuid
,
785 socket_lock(mp_so
, 0);
788 mpte
->mpte_triggered_cell
= 1;
791 uuid_unparse_upper(mpsotomppcb(mp_so
)->necp_client_uuid
, uuidstr
);
792 os_log_info(mptcp_log_handle
, "%s - %lx: asked irat to bringup cell for uuid %s, err %d\n",
793 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), uuidstr
, err
);
795 os_log_info(mptcp_log_handle
, "%s - %lx: UUID is already null\n",
796 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
801 mptcp_subflow_disconnecting(struct mptsub
*mpts
)
803 if (mpts
->mpts_socket
->so_state
& SS_ISDISCONNECTED
) {
807 if (mpts
->mpts_flags
& (MPTSF_DISCONNECTING
| MPTSF_DISCONNECTED
| MPTSF_CLOSE_REQD
)) {
811 if (sototcpcb(mpts
->mpts_socket
)->t_state
== TCPS_CLOSED
) {
819 * In Handover mode, only create cell subflow if
820 * - Symptoms marked WiFi as weak:
821 * Here, if we are sending data, then we can check the RTO-state. That is a
822 * stronger signal of WiFi quality than the Symptoms indicator.
823 * If however we are not sending any data, the only thing we can do is guess
824 * and thus bring up Cell.
826 * - Symptoms marked WiFi as unknown:
827 * In this state we don't know what the situation is and thus remain
828 * conservative, only bringing up cell if there are retransmissions going on.
831 mptcp_handover_use_cellular(struct mptses
*mpte
, struct tcpcb
*tp
)
833 int unusable_state
= mptcp_is_wifi_unusable_for_session(mpte
);
835 if (unusable_state
== 0) {
836 /* WiFi is good - don't use cell */
840 if (unusable_state
== -1) {
842 * We are in unknown state, only use Cell if we have confirmed
845 if (mptetoso(mpte
)->so_snd
.sb_cc
!= 0 && tp
->t_rxtshift
>= mptcp_fail_thresh
* 2) {
852 if (unusable_state
== 1) {
854 * WiFi is confirmed to be bad from Symptoms-Framework.
855 * If we are sending data, check the RTOs.
856 * Otherwise, be pessimistic and use Cell.
858 if (mptetoso(mpte
)->so_snd
.sb_cc
!= 0) {
859 if (tp
->t_rxtshift
>= mptcp_fail_thresh
* 2) {
873 mptcp_check_subflows_and_add(struct mptses
*mpte
)
875 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
876 boolean_t cellular_viable
= FALSE
;
877 boolean_t want_cellular
= TRUE
;
880 if (!mptcp_ok_to_create_subflows(mp_tp
)) {
881 os_log_debug(mptcp_log_handle
, "%s - %lx: not a good time for subflows, state %u flags %#x",
882 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mp_tp
->mpt_state
, mp_tp
->mpt_flags
);
886 if (mptcp_get_session_dst(mpte
, false, false) == NULL
) {
890 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
891 boolean_t need_to_ask_symptoms
= FALSE
, found
= FALSE
;
892 struct mpt_itf_info
*info
;
893 struct sockaddr_in6 nat64pre
;
894 struct sockaddr
*dst
;
899 info
= &mpte
->mpte_itfinfo
[i
];
901 ifindex
= info
->ifindex
;
902 if (ifindex
== IFSCOPE_NONE
) {
906 os_log(mptcp_log_handle
, "%s - %lx: itf %u no support %u hasv4 %u has v6 %u hasnat64 %u\n",
907 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), info
->ifindex
, info
->no_mptcp_support
,
908 info
->has_v4_conn
, info
->has_v6_conn
, info
->has_nat64_conn
);
910 if (info
->no_mptcp_support
) {
914 ifnet_head_lock_shared();
915 ifp
= ifindex2ifnet
[ifindex
];
922 if (IFNET_IS_CELLULAR(ifp
)) {
923 cellular_viable
= TRUE
;
926 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
927 const struct ifnet
*subifp
= sotoinpcb(mpts
->mpts_socket
)->inp_last_outifp
;
928 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
930 if (subifp
== NULL
) {
935 * If there is at least one functioning subflow on WiFi
936 * and we are checking for the cell interface, then
937 * we always need to ask symptoms for permission as
938 * cell is triggered even if WiFi is available.
940 if (!IFNET_IS_CELLULAR(subifp
) &&
941 !mptcp_subflow_disconnecting(mpts
) &&
942 IFNET_IS_CELLULAR(ifp
)) {
943 need_to_ask_symptoms
= TRUE
;
946 if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_HANDOVER
) {
947 os_log(mptcp_log_handle
,
948 "%s - %lx: handover: cell %u wifi-state %d flags %#x rxt %u first-party %u sb_cc %u ifindex %u this %u rtt %u rttvar %u rto %u\n",
949 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
950 IFNET_IS_CELLULAR(subifp
),
951 mptcp_is_wifi_unusable_for_session(mpte
),
954 !!(mpte
->mpte_flags
& MPTE_FIRSTPARTY
),
955 mptetoso(mpte
)->so_snd
.sb_cc
,
956 ifindex
, subifp
->if_index
,
957 tp
->t_srtt
>> TCP_RTT_SHIFT
,
958 tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
,
961 if (!IFNET_IS_CELLULAR(subifp
) &&
962 !mptcp_subflow_disconnecting(mpts
) &&
963 (mpts
->mpts_flags
& MPTSF_CONNECTED
) &&
964 !mptcp_handover_use_cellular(mpte
, tp
)) {
967 /* We found a proper subflow on WiFi - no need for cell */
968 want_cellular
= FALSE
;
971 } else if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_TARGET_BASED
) {
972 uint64_t time_now
= mach_continuous_time();
974 os_log(mptcp_log_handle
,
975 "%s - %lx: target-based: %llu now %llu unusable? %d cell %u sostat %#x mpts_flags %#x tcp-state %u\n",
976 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpte
->mpte_time_target
,
977 time_now
, mptcp_is_wifi_unusable_for_session(mpte
),
978 IFNET_IS_CELLULAR(subifp
), mpts
->mpts_socket
->so_state
,
979 mpts
->mpts_flags
, sototcpcb(mpts
->mpts_socket
)->t_state
);
981 if (!IFNET_IS_CELLULAR(subifp
) &&
982 !mptcp_subflow_disconnecting(mpts
) &&
983 (mpte
->mpte_time_target
== 0 ||
984 (int64_t)(mpte
->mpte_time_target
- time_now
) > 0 ||
985 !mptcp_is_wifi_unusable_for_session(mpte
))) {
988 want_cellular
= FALSE
;
993 if (subifp
->if_index
== ifindex
&&
994 !mptcp_subflow_disconnecting(mpts
)) {
996 * We found a subflow on this interface.
997 * No need to create a new one.
1008 if (need_to_ask_symptoms
&&
1009 !(mpte
->mpte_flags
& MPTE_FIRSTPARTY
) &&
1010 !(mpte
->mpte_flags
& MPTE_ACCESS_GRANTED
) &&
1011 mptcp_developer_mode
== 0) {
1012 mptcp_ask_symptoms(mpte
);
1016 dst
= mptcp_get_session_dst(mpte
, info
->has_v6_conn
, info
->has_v4_conn
);
1018 if (dst
->sa_family
== AF_INET
&&
1019 !info
->has_v4_conn
&& info
->has_nat64_conn
) {
1020 struct ipv6_prefix nat64prefixes
[NAT64_MAX_NUM_PREFIXES
];
1023 bzero(&nat64pre
, sizeof(struct sockaddr_in6
));
1025 error
= ifnet_get_nat64prefix(ifp
, nat64prefixes
);
1027 os_log_error(mptcp_log_handle
, "%s - %lx: no NAT64-prefix on itf %s, error %d\n",
1028 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), ifp
->if_name
, error
);
1032 for (j
= 0; j
< NAT64_MAX_NUM_PREFIXES
; j
++) {
1033 if (nat64prefixes
[j
].prefix_len
!= 0) {
1038 VERIFY(j
< NAT64_MAX_NUM_PREFIXES
);
1040 error
= mptcp_synthesize_nat64(&nat64prefixes
[j
].ipv6_prefix
,
1041 nat64prefixes
[j
].prefix_len
,
1042 &((struct sockaddr_in
*)(void *)dst
)->sin_addr
);
1044 os_log_error(mptcp_log_handle
, "%s - %lx: cannot synthesize this addr\n",
1045 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
1049 memcpy(&nat64pre
.sin6_addr
,
1050 &nat64prefixes
[j
].ipv6_prefix
,
1051 sizeof(nat64pre
.sin6_addr
));
1052 nat64pre
.sin6_len
= sizeof(struct sockaddr_in6
);
1053 nat64pre
.sin6_family
= AF_INET6
;
1054 nat64pre
.sin6_port
= ((struct sockaddr_in
*)(void *)dst
)->sin_port
;
1055 nat64pre
.sin6_flowinfo
= 0;
1056 nat64pre
.sin6_scope_id
= 0;
1058 dst
= (struct sockaddr
*)&nat64pre
;
1061 /* Initial subflow started on a NAT64'd address? */
1062 if (!(mpte
->mpte_flags
& MPTE_UNICAST_IP
) &&
1063 mpte
->mpte_dst
.sa_family
== AF_INET6
&&
1064 mpte
->mpte_dst_v4_nat64
.sin_family
== AF_INET
) {
1065 dst
= (struct sockaddr
*)&mpte
->mpte_dst_v4_nat64
;
1068 if (dst
->sa_family
== AF_INET
&& !info
->has_v4_conn
) {
1071 if (dst
->sa_family
== AF_INET6
&& !info
->has_v6_conn
) {
1075 mptcp_subflow_add(mpte
, NULL
, dst
, ifindex
, NULL
);
1078 if (!cellular_viable
&& want_cellular
) {
1079 /* Trigger Cell Bringup */
1080 mptcp_trigger_cell_bringup(mpte
);
1085 mptcp_remove_cell_subflows(struct mptses
*mpte
)
1087 struct mptsub
*mpts
, *tmpts
;
1088 boolean_t found
= false;
1090 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1091 const struct ifnet
*ifp
= sotoinpcb(mpts
->mpts_socket
)->inp_last_outifp
;
1093 if (ifp
== NULL
|| IFNET_IS_CELLULAR(ifp
)) {
1097 /* We have a functioning subflow on WiFi. No need for cell! */
1098 if (mpts
->mpts_flags
& MPTSF_CONNECTED
&&
1099 !mptcp_subflow_disconnecting(mpts
)) {
1104 /* Didn't found functional sub on WiFi - stay on cell */
1109 TAILQ_FOREACH_SAFE(mpts
, &mpte
->mpte_subflows
, mpts_entry
, tmpts
) {
1110 const struct ifnet
*ifp
= sotoinpcb(mpts
->mpts_socket
)->inp_last_outifp
;
1112 /* Only remove cellular subflows */
1113 if (ifp
== NULL
|| !IFNET_IS_CELLULAR(ifp
)) {
1117 os_log(mptcp_log_handle
, "%s - %lx: removing cell subflow\n",
1118 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
1120 soevent(mpts
->mpts_socket
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MUSTRST
);
1126 /* Returns true if it removed a subflow on cell */
1128 mptcp_handover_subflows_remove(struct mptses
*mpte
)
1130 int wifi_unusable
= mptcp_is_wifi_unusable_for_session(mpte
);
1131 boolean_t found_working_subflow
= false;
1132 struct mptsub
*mpts
;
1135 * Look for a subflow that is on a non-cellular interface
1136 * and actually works (aka, no retransmission timeout).
1138 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1139 const struct ifnet
*ifp
= sotoinpcb(mpts
->mpts_socket
)->inp_last_outifp
;
1143 if (ifp
== NULL
|| IFNET_IS_CELLULAR(ifp
)) {
1147 so
= mpts
->mpts_socket
;
1150 if (!(mpts
->mpts_flags
& MPTSF_CONNECTED
) ||
1151 tp
->t_state
!= TCPS_ESTABLISHED
) {
1155 os_log_debug(mptcp_log_handle
, "%s - %lx: rxt %u sb_cc %u unusable %d\n",
1156 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), tp
->t_rxtshift
, mptetoso(mpte
)->so_snd
.sb_cc
, wifi_unusable
);
1158 if (!mptcp_handover_use_cellular(mpte
, tp
)) {
1159 found_working_subflow
= true;
1165 * Couldn't find a working subflow, let's not remove those on a cellular
1168 if (!found_working_subflow
) {
1172 mptcp_remove_cell_subflows(mpte
);
1176 mptcp_targetbased_subflows_remove(struct mptses
*mpte
)
1178 uint64_t time_now
= mach_continuous_time();
1180 if (mpte
->mpte_time_target
!= 0 &&
1181 (int64_t)(mpte
->mpte_time_target
- time_now
) <= 0 &&
1182 mptcp_is_wifi_unusable_for_session(mpte
)) {
1183 /* WiFi is bad and we are below the target - don't remove any subflows */
1187 mptcp_remove_cell_subflows(mpte
);
1191 * Based on the MPTCP Service-type and the state of the subflows, we
1192 * will destroy subflows here.
1195 mptcp_check_subflows_and_remove(struct mptses
*mpte
)
1197 if (!mptcp_ok_to_create_subflows(mpte
->mpte_mptcb
)) {
1201 socket_lock_assert_owned(mptetoso(mpte
));
1203 if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_HANDOVER
) {
1204 mptcp_handover_subflows_remove(mpte
);
1207 if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_TARGET_BASED
) {
1208 mptcp_targetbased_subflows_remove(mpte
);
1213 mptcp_remove_subflows(struct mptses
*mpte
)
1215 struct mptsub
*mpts
, *tmpts
;
1217 if (!mptcp_ok_to_create_subflows(mpte
->mpte_mptcb
)) {
1221 TAILQ_FOREACH_SAFE(mpts
, &mpte
->mpte_subflows
, mpts_entry
, tmpts
) {
1222 const struct ifnet
*ifp
= sotoinpcb(mpts
->mpts_socket
)->inp_last_outifp
;
1223 boolean_t found
= false;
1227 if (mpts
->mpts_flags
& MPTSF_CLOSE_REQD
) {
1228 mpts
->mpts_flags
&= ~MPTSF_CLOSE_REQD
;
1230 os_log(mptcp_log_handle
, "%s - %lx: itf %u close_reqd last itf %d\n",
1231 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpts
->mpts_ifscope
,
1232 ifp
? ifp
->if_index
: -1);
1233 soevent(mpts
->mpts_socket
,
1234 SO_FILT_HINT_LOCKED
| SO_FILT_HINT_NOSRCADDR
);
1239 if (ifp
== NULL
&& mpts
->mpts_ifscope
== IFSCOPE_NONE
) {
1244 ifindex
= ifp
->if_index
;
1246 ifindex
= mpts
->mpts_ifscope
;
1249 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
1250 if (mpte
->mpte_itfinfo
[i
].ifindex
== IFSCOPE_NONE
) {
1254 if (mpte
->mpte_itfinfo
[i
].ifindex
== ifindex
) {
1255 if (mpts
->mpts_dst
.sa_family
== AF_INET6
&&
1256 (mpte
->mpte_itfinfo
[i
].has_v6_conn
|| mpte
->mpte_itfinfo
[i
].has_nat64_conn
)) {
1261 if (mpts
->mpts_dst
.sa_family
== AF_INET
&&
1262 mpte
->mpte_itfinfo
[i
].has_v4_conn
) {
1270 os_log(mptcp_log_handle
, "%s - %lx: itf %u killing %#x\n",
1271 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
1272 ifindex
, mpts
->mpts_flags
);
1274 soevent(mpts
->mpts_socket
,
1275 SO_FILT_HINT_LOCKED
| SO_FILT_HINT_NOSRCADDR
);
1281 mptcp_create_subflows(__unused
void *arg
)
1286 * Start with clearing, because we might be processing connections
1287 * while a new event comes in.
1289 if (OSTestAndClear(0x01, &mptcp_create_subflows_scheduled
)) {
1290 os_log_error(mptcp_log_handle
, "%s: bit was already cleared!\n", __func__
);
1293 /* Iterate over all MPTCP connections */
1295 lck_mtx_lock(&mtcbinfo
.mppi_lock
);
1297 TAILQ_FOREACH(mpp
, &mtcbinfo
.mppi_pcbs
, mpp_entry
) {
1298 struct socket
*mp_so
= mpp
->mpp_socket
;
1299 struct mptses
*mpte
= mpp
->mpp_pcbe
;
1301 if (!(mpp
->mpp_flags
& MPP_CREATE_SUBFLOWS
)) {
1305 socket_lock(mp_so
, 1);
1306 VERIFY(mp_so
->so_usecount
> 0);
1308 mpp
->mpp_flags
&= ~MPP_CREATE_SUBFLOWS
;
1310 mptcp_check_subflows_and_add(mpte
);
1311 mptcp_remove_subflows(mpte
);
1313 mp_so
->so_usecount
--; /* See mptcp_sched_create_subflows */
1314 socket_unlock(mp_so
, 1);
1317 lck_mtx_unlock(&mtcbinfo
.mppi_lock
);
1321 * We need this because we are coming from an NECP-event. This event gets posted
1322 * while holding NECP-locks. The creation of the subflow however leads us back
1323 * into NECP (e.g., to add the necp_cb and also from tcp_connect).
1324 * So, we would deadlock there as we already hold the NECP-lock.
1326 * So, let's schedule this separately. It also gives NECP the chance to make
1327 * progress, without having to wait for MPTCP to finish its subflow creation.
1330 mptcp_sched_create_subflows(struct mptses
*mpte
)
1332 struct mppcb
*mpp
= mpte
->mpte_mppcb
;
1333 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
1334 struct socket
*mp_so
= mpp
->mpp_socket
;
1336 if (!mptcp_ok_to_create_subflows(mp_tp
)) {
1337 os_log_debug(mptcp_log_handle
, "%s - %lx: not a good time for subflows, state %u flags %#x",
1338 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mp_tp
->mpt_state
, mp_tp
->mpt_flags
);
1342 if (!(mpp
->mpp_flags
& MPP_CREATE_SUBFLOWS
)) {
1343 mp_so
->so_usecount
++; /* To prevent it from being free'd in-between */
1344 mpp
->mpp_flags
|= MPP_CREATE_SUBFLOWS
;
1347 if (OSTestAndSet(0x01, &mptcp_create_subflows_scheduled
)) {
1351 /* Do the call in 100ms to allow NECP to schedule it on all sockets */
1352 timeout(mptcp_create_subflows
, NULL
, hz
/ 10);
1356 * Allocate an MPTCP socket option structure.
1359 mptcp_sopt_alloc(zalloc_flags_t how
)
1361 return zalloc_flags(mptopt_zone
, how
| Z_ZERO
);
1365 * Free an MPTCP socket option structure.
1368 mptcp_sopt_free(struct mptopt
*mpo
)
1370 VERIFY(!(mpo
->mpo_flags
& MPOF_ATTACHED
));
1372 zfree(mptopt_zone
, mpo
);
1376 * Add a socket option to the MPTCP socket option list.
1379 mptcp_sopt_insert(struct mptses
*mpte
, struct mptopt
*mpo
)
1381 socket_lock_assert_owned(mptetoso(mpte
));
1382 mpo
->mpo_flags
|= MPOF_ATTACHED
;
1383 TAILQ_INSERT_TAIL(&mpte
->mpte_sopts
, mpo
, mpo_entry
);
1387 * Remove a socket option from the MPTCP socket option list.
1390 mptcp_sopt_remove(struct mptses
*mpte
, struct mptopt
*mpo
)
1392 socket_lock_assert_owned(mptetoso(mpte
));
1393 VERIFY(mpo
->mpo_flags
& MPOF_ATTACHED
);
1394 mpo
->mpo_flags
&= ~MPOF_ATTACHED
;
1395 TAILQ_REMOVE(&mpte
->mpte_sopts
, mpo
, mpo_entry
);
1399 * Search for an existing <sopt_level,sopt_name> socket option.
1402 mptcp_sopt_find(struct mptses
*mpte
, struct sockopt
*sopt
)
1406 socket_lock_assert_owned(mptetoso(mpte
));
1408 TAILQ_FOREACH(mpo
, &mpte
->mpte_sopts
, mpo_entry
) {
1409 if (mpo
->mpo_level
== sopt
->sopt_level
&&
1410 mpo
->mpo_name
== sopt
->sopt_name
) {
1418 * Allocate a MPTCP subflow structure.
1420 static struct mptsub
*
1421 mptcp_subflow_alloc(void)
1423 return zalloc_flags(mptsub_zone
, Z_WAITOK
| Z_ZERO
);
1427 * Deallocate a subflow structure, called when all of the references held
1428 * on it have been released. This implies that the subflow has been deleted.
1431 mptcp_subflow_free(struct mptsub
*mpts
)
1433 VERIFY(mpts
->mpts_refcnt
== 0);
1434 VERIFY(!(mpts
->mpts_flags
& MPTSF_ATTACHED
));
1435 VERIFY(mpts
->mpts_mpte
== NULL
);
1436 VERIFY(mpts
->mpts_socket
== NULL
);
1438 if (mpts
->mpts_src
!= NULL
) {
1439 FREE(mpts
->mpts_src
, M_SONAME
);
1440 mpts
->mpts_src
= NULL
;
1443 zfree(mptsub_zone
, mpts
);
1447 mptcp_subflow_addref(struct mptsub
*mpts
)
1449 if (++mpts
->mpts_refcnt
== 0) {
1450 panic("%s: mpts %p wraparound refcnt\n", __func__
, mpts
);
1456 mptcp_subflow_remref(struct mptsub
*mpts
)
1458 if (mpts
->mpts_refcnt
== 0) {
1459 panic("%s: mpts %p negative refcnt\n", __func__
, mpts
);
1462 if (--mpts
->mpts_refcnt
> 0) {
1466 /* callee will unlock and destroy lock */
1467 mptcp_subflow_free(mpts
);
1471 mptcp_subflow_attach(struct mptses
*mpte
, struct mptsub
*mpts
, struct socket
*so
)
1473 struct socket
*mp_so
= mpte
->mpte_mppcb
->mpp_socket
;
1474 struct tcpcb
*tp
= sototcpcb(so
);
1477 * From this moment on, the subflow is linked to the MPTCP-connection.
1478 * Locking,... happens now at the MPTCP-layer
1480 tp
->t_mptcb
= mpte
->mpte_mptcb
;
1481 so
->so_flags
|= SOF_MP_SUBFLOW
;
1482 mp_so
->so_usecount
++;
1485 * Insert the subflow into the list, and associate the MPTCP PCB
1486 * as well as the the subflow socket. From this point on, removing
1487 * the subflow needs to be done via mptcp_subflow_del().
1489 TAILQ_INSERT_TAIL(&mpte
->mpte_subflows
, mpts
, mpts_entry
);
1490 mpte
->mpte_numflows
++;
1492 atomic_bitset_32(&mpts
->mpts_flags
, MPTSF_ATTACHED
);
1493 mpts
->mpts_mpte
= mpte
;
1494 mpts
->mpts_socket
= so
;
1496 mptcp_subflow_addref(mpts
); /* for being in MPTCP subflow list */
1497 mptcp_subflow_addref(mpts
); /* for subflow socket */
1501 mptcp_subflow_necp_cb(void *handle
, __unused
int action
,
1502 __unused
uint32_t interface_index
,
1503 uint32_t necp_flags
, bool *viable
)
1505 boolean_t low_power
= !!(necp_flags
& NECP_CLIENT_RESULT_FLAG_INTERFACE_LOW_POWER
);
1506 struct inpcb
*inp
= (struct inpcb
*)handle
;
1507 struct socket
*so
= inp
->inp_socket
;
1508 struct mptsub
*mpts
;
1509 struct mptses
*mpte
;
1512 action
= NECP_CLIENT_CBACTION_NONVIABLE
;
1515 if (action
!= NECP_CLIENT_CBACTION_NONVIABLE
) {
1520 * The socket is being garbage-collected. There is nothing to be done
1523 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
) {
1529 /* Check again after we acquired the lock. */
1530 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1534 mpte
= tptomptp(sototcpcb(so
))->mpt_mpte
;
1535 mpts
= sototcpcb(so
)->t_mpsub
;
1537 os_log_debug(mptcp_log_handle
, "%s - %lx: Subflow on itf %u became non-viable, power %u",
1538 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpts
->mpts_ifscope
, low_power
);
1540 mpts
->mpts_flags
|= MPTSF_CLOSE_REQD
;
1542 mptcp_sched_create_subflows(mpte
);
1544 if ((mpte
->mpte_svctype
== MPTCP_SVCTYPE_HANDOVER
||
1545 mpte
->mpte_svctype
== MPTCP_SVCTYPE_TARGET_BASED
) &&
1551 socket_unlock(so
, 1);
1555 * Create an MPTCP subflow socket.
1558 mptcp_subflow_socreate(struct mptses
*mpte
, struct mptsub
*mpts
, int dom
,
1561 lck_mtx_t
*subflow_mtx
;
1562 struct mptopt smpo
, *mpo
, *tmpo
;
1564 struct socket
*mp_so
;
1569 mp_so
= mptetoso(mpte
);
1571 p
= proc_find(mp_so
->last_pid
);
1572 if (p
== PROC_NULL
) {
1573 os_log_error(mptcp_log_handle
, "%s - %lx: Couldn't find proc for pid %u\n",
1574 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mp_so
->last_pid
);
1576 mptcp_subflow_free(mpts
);
1581 * Create the subflow socket (multipath subflow, non-blocking.)
1583 * This will cause SOF_MP_SUBFLOW socket flag to be set on the subflow
1584 * socket; it will be cleared when the socket is peeled off or closed.
1585 * It also indicates to the underlying TCP to handle MPTCP options.
1586 * A multipath subflow socket implies SS_NOFDREF state.
1590 * Unlock, because tcp_usr_attach ends up in in_pcballoc, which takes
1591 * the ipi-lock. We cannot hold the socket-lock at that point.
1593 socket_unlock(mp_so
, 0);
1594 error
= socreate_internal(dom
, so
, SOCK_STREAM
, IPPROTO_TCP
, p
,
1595 SOCF_MPTCP
, PROC_NULL
);
1596 socket_lock(mp_so
, 0);
1598 os_log_error(mptcp_log_handle
, "%s - %lx: unable to create subflow socket error %d\n",
1599 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), error
);
1603 mptcp_subflow_free(mpts
);
1608 * We need to protect the setting of SOF_MP_SUBFLOW with a lock, because
1609 * this marks the moment of lock-switch from the TCP-lock to the MPTCP-lock.
1610 * Which is why we also need to get the lock with pr_getlock, as after
1611 * setting the flag, socket_unlock will work on the MPTCP-level lock.
1613 subflow_mtx
= ((*so
)->so_proto
->pr_getlock
)(*so
, 0);
1614 lck_mtx_lock(subflow_mtx
);
1617 * Must be the first thing we do, to make sure all pointers for this
1620 mptcp_subflow_attach(mpte
, mpts
, *so
);
1623 * A multipath subflow socket is used internally in the kernel,
1624 * therefore it does not have a file desciptor associated by
1627 (*so
)->so_state
|= SS_NOFDREF
;
1629 lck_mtx_unlock(subflow_mtx
);
1631 /* prevent the socket buffers from being compressed */
1632 (*so
)->so_rcv
.sb_flags
|= SB_NOCOMPRESS
;
1633 (*so
)->so_snd
.sb_flags
|= SB_NOCOMPRESS
;
1635 /* Inherit preconnect and TFO data flags */
1636 if (mp_so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
1637 (*so
)->so_flags1
|= SOF1_PRECONNECT_DATA
;
1639 if (mp_so
->so_flags1
& SOF1_DATA_IDEMPOTENT
) {
1640 (*so
)->so_flags1
|= SOF1_DATA_IDEMPOTENT
;
1643 /* Inherit uuid and create the related flow. */
1644 if (!uuid_is_null(mpsotomppcb(mp_so
)->necp_client_uuid
)) {
1645 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
1647 sotoinpcb(*so
)->necp_cb
= mptcp_subflow_necp_cb
;
1650 * A note on the unlock: With MPTCP, we do multiple times a
1651 * necp_client_register_socket_flow. This is problematic,
1652 * because now the lock-ordering guarantee (first necp-locks,
1653 * then socket-locks) is no more respected. So, we need to
1656 socket_unlock(mp_so
, 0);
1657 error
= necp_client_register_socket_flow(mp_so
->last_pid
,
1658 mpsotomppcb(mp_so
)->necp_client_uuid
, sotoinpcb(*so
));
1659 socket_lock(mp_so
, 0);
1662 os_log_error(mptcp_log_handle
, "%s - %lx: necp_client_register_socket_flow failed with error %d\n",
1663 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), error
);
1668 /* Possible state-change during the unlock above */
1669 if (mp_tp
->mpt_state
>= MPTCPS_TIME_WAIT
||
1670 (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
)) {
1671 os_log_error(mptcp_log_handle
, "%s - %lx: state changed during unlock: %u flags %#x\n",
1672 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
1673 mp_tp
->mpt_state
, mp_tp
->mpt_flags
);
1679 uuid_copy(sotoinpcb(*so
)->necp_client_uuid
, mpsotomppcb(mp_so
)->necp_client_uuid
);
1682 /* Needs to happen prior to the delegation! */
1683 (*so
)->last_pid
= mp_so
->last_pid
;
1685 if (mp_so
->so_flags
& SOF_DELEGATED
) {
1686 if (mpte
->mpte_epid
) {
1687 error
= so_set_effective_pid(*so
, mpte
->mpte_epid
, p
, false);
1689 os_log_error(mptcp_log_handle
, "%s - %lx: so_set_effective_pid failed with error %d\n",
1690 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), error
);
1694 if (!uuid_is_null(mpte
->mpte_euuid
)) {
1695 error
= so_set_effective_uuid(*so
, mpte
->mpte_euuid
, p
, false);
1697 os_log_error(mptcp_log_handle
, "%s - %lx: so_set_effective_uuid failed with error %d\n",
1698 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), error
);
1704 /* inherit the other socket options */
1705 bzero(&smpo
, sizeof(smpo
));
1706 smpo
.mpo_flags
|= MPOF_SUBFLOW_OK
;
1707 smpo
.mpo_level
= SOL_SOCKET
;
1708 smpo
.mpo_intval
= 1;
1710 /* disable SIGPIPE */
1711 smpo
.mpo_name
= SO_NOSIGPIPE
;
1712 if ((error
= mptcp_subflow_sosetopt(mpte
, mpts
, &smpo
)) != 0) {
1716 /* find out if the subflow's source address goes away */
1717 smpo
.mpo_name
= SO_NOADDRERR
;
1718 if ((error
= mptcp_subflow_sosetopt(mpte
, mpts
, &smpo
)) != 0) {
1722 if (mpte
->mpte_mptcb
->mpt_state
>= MPTCPS_ESTABLISHED
) {
1724 * On secondary subflows we might need to set the cell-fallback
1725 * flag (see conditions in mptcp_subflow_sosetopt).
1727 smpo
.mpo_level
= SOL_SOCKET
;
1728 smpo
.mpo_name
= SO_MARK_CELLFALLBACK
;
1729 smpo
.mpo_intval
= 1;
1730 if ((error
= mptcp_subflow_sosetopt(mpte
, mpts
, &smpo
)) != 0) {
1735 /* replay setsockopt(2) on the subflow sockets for eligible options */
1736 TAILQ_FOREACH_SAFE(mpo
, &mpte
->mpte_sopts
, mpo_entry
, tmpo
) {
1739 if (!(mpo
->mpo_flags
& MPOF_SUBFLOW_OK
)) {
1744 * Skip those that are handled internally; these options
1745 * should not have been recorded and marked with the
1746 * MPOF_SUBFLOW_OK by mptcp_setopt(), but just in case.
1748 if (mpo
->mpo_level
== SOL_SOCKET
&&
1749 (mpo
->mpo_name
== SO_NOSIGPIPE
||
1750 mpo
->mpo_name
== SO_NOADDRERR
||
1751 mpo
->mpo_name
== SO_KEEPALIVE
)) {
1755 interim
= (mpo
->mpo_flags
& MPOF_INTERIM
);
1756 if (mptcp_subflow_sosetopt(mpte
, mpts
, mpo
) != 0 && interim
) {
1757 os_log_error(mptcp_log_handle
, "%s - %lx: sopt %s val %d interim record removed\n",
1758 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
1759 mptcp_sopt2str(mpo
->mpo_level
, mpo
->mpo_name
),
1761 mptcp_sopt_remove(mpte
, mpo
);
1762 mptcp_sopt_free(mpo
);
1768 * We need to receive everything that the subflow socket has,
1769 * so use a customized socket receive function. We will undo
1770 * this when the socket is peeled off or closed.
1774 (*so
)->so_proto
= &mptcp_subflow_protosw
;
1777 (*so
)->so_proto
= (struct protosw
*)&mptcp_subflow_protosw6
;
1786 DTRACE_MPTCP3(subflow__create
, struct mptses
*, mpte
,
1787 int, dom
, int, error
);
1792 mptcp_subflow_abort(mpts
, error
);
1800 * Close an MPTCP subflow socket.
1802 * Note that this may be called on an embryonic subflow, and the only
1803 * thing that is guaranteed valid is the protocol-user request.
1806 mptcp_subflow_soclose(struct mptsub
*mpts
)
1808 struct socket
*so
= mpts
->mpts_socket
;
1810 if (mpts
->mpts_flags
& MPTSF_CLOSED
) {
1815 VERIFY(so
->so_flags
& SOF_MP_SUBFLOW
);
1816 VERIFY((so
->so_state
& (SS_NBIO
| SS_NOFDREF
)) == (SS_NBIO
| SS_NOFDREF
));
1818 DTRACE_MPTCP5(subflow__close
, struct mptsub
*, mpts
,
1819 struct socket
*, so
,
1820 struct sockbuf
*, &so
->so_rcv
,
1821 struct sockbuf
*, &so
->so_snd
,
1822 struct mptses
*, mpts
->mpts_mpte
);
1824 mpts
->mpts_flags
|= MPTSF_CLOSED
;
1826 if (so
->so_retaincnt
== 0) {
1831 VERIFY(so
->so_usecount
> 0);
1839 * Connect an MPTCP subflow socket.
1841 * Note that in the pending connect case, the subflow socket may have been
1842 * bound to an interface and/or a source IP address which may no longer be
1843 * around by the time this routine is called; in that case the connect attempt
1844 * will most likely fail.
1847 mptcp_subflow_soconnectx(struct mptses
*mpte
, struct mptsub
*mpts
)
1849 char dbuf
[MAX_IPv6_STR_LEN
];
1850 struct socket
*mp_so
, *so
;
1851 struct mptcb
*mp_tp
;
1852 struct sockaddr
*dst
;
1854 int af
, error
, dport
;
1856 mp_so
= mptetoso(mpte
);
1857 mp_tp
= mpte
->mpte_mptcb
;
1858 so
= mpts
->mpts_socket
;
1859 af
= mpts
->mpts_dst
.sa_family
;
1860 dst
= &mpts
->mpts_dst
;
1862 VERIFY((mpts
->mpts_flags
& (MPTSF_CONNECTING
| MPTSF_CONNECTED
)) == MPTSF_CONNECTING
);
1863 VERIFY(mpts
->mpts_socket
!= NULL
);
1864 VERIFY(af
== AF_INET
|| af
== AF_INET6
);
1866 if (af
== AF_INET
) {
1867 inet_ntop(af
, &SIN(dst
)->sin_addr
.s_addr
, dbuf
, sizeof(dbuf
));
1868 dport
= ntohs(SIN(dst
)->sin_port
);
1870 inet_ntop(af
, &SIN6(dst
)->sin6_addr
, dbuf
, sizeof(dbuf
));
1871 dport
= ntohs(SIN6(dst
)->sin6_port
);
1874 os_log(mptcp_log_handle
,
1875 "%s - %lx: ifindex %u dst %s:%d pended %u\n", __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
1876 mpts
->mpts_ifscope
, dbuf
, dport
, !!(mpts
->mpts_flags
& MPTSF_CONNECT_PENDING
));
1878 p
= proc_find(mp_so
->last_pid
);
1879 if (p
== PROC_NULL
) {
1880 os_log_error(mptcp_log_handle
, "%s - %lx: Couldn't find proc for pid %u\n",
1881 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mp_so
->last_pid
);
1886 mpts
->mpts_flags
&= ~MPTSF_CONNECT_PENDING
;
1888 mptcp_attach_to_subf(so
, mpte
->mpte_mptcb
, mpte
->mpte_addrid_last
);
1890 /* connect the subflow socket */
1891 error
= soconnectxlocked(so
, mpts
->mpts_src
, &mpts
->mpts_dst
,
1892 p
, mpts
->mpts_ifscope
,
1893 mpte
->mpte_associd
, NULL
, 0, NULL
, 0, NULL
, NULL
);
1895 mpts
->mpts_iss
= sototcpcb(so
)->iss
;
1897 /* See tcp_connect_complete */
1898 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
&&
1899 (mp_so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
1900 mp_tp
->mpt_sndwnd
= sototcpcb(so
)->snd_wnd
;
1903 /* Allocate a unique address id per subflow */
1904 mpte
->mpte_addrid_last
++;
1905 if (mpte
->mpte_addrid_last
== 0) {
1906 mpte
->mpte_addrid_last
++;
1911 DTRACE_MPTCP3(subflow__connect
, struct mptses
*, mpte
,
1912 struct mptsub
*, mpts
, int, error
);
1914 os_log_error(mptcp_log_handle
, "%s - %lx: connectx failed with error %d ifscope %u\n",
1915 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), error
, mpts
->mpts_ifscope
);
1922 mptcp_adj_rmap(struct socket
*so
, struct mbuf
*m
, int off
, uint64_t dsn
,
1923 uint32_t rseq
, uint16_t dlen
)
1925 struct mptsub
*mpts
= sototcpcb(so
)->t_mpsub
;
1927 if (m_pktlen(m
) == 0) {
1931 if ((m
->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
)) {
1932 if (off
&& (dsn
!= m
->m_pkthdr
.mp_dsn
||
1933 rseq
!= m
->m_pkthdr
.mp_rseq
||
1934 dlen
!= m
->m_pkthdr
.mp_rlen
)) {
1935 os_log_error(mptcp_log_handle
, "%s - %lx: Received incorrect second mapping: %u - %u , %u - %u, %u - %u\n",
1936 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpts
->mpts_mpte
),
1937 (uint32_t)dsn
, (uint32_t)m
->m_pkthdr
.mp_dsn
,
1938 rseq
, m
->m_pkthdr
.mp_rseq
,
1939 dlen
, m
->m_pkthdr
.mp_rlen
);
1941 soevent(mpts
->mpts_socket
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MUSTRST
);
1944 m
->m_pkthdr
.mp_dsn
+= off
;
1945 m
->m_pkthdr
.mp_rseq
+= off
;
1947 VERIFY(m_pktlen(m
) < UINT16_MAX
);
1948 m
->m_pkthdr
.mp_rlen
= (uint16_t)m_pktlen(m
);
1950 if (!(mpts
->mpts_flags
& MPTSF_FULLY_ESTABLISHED
)) {
1951 /* data arrived without an DSS option mapping */
1953 /* initial subflow can fallback right after SYN handshake */
1954 if (mpts
->mpts_flags
& MPTSF_INITIAL_SUB
) {
1955 mptcp_notify_mpfail(so
);
1957 soevent(mpts
->mpts_socket
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MUSTRST
);
1961 } else if (m
->m_flags
& M_PKTHDR
) {
1962 /* We need to fake the DATA-mapping */
1963 m
->m_pkthdr
.pkt_flags
|= PKTF_MPTCP
;
1964 m
->m_pkthdr
.mp_dsn
= dsn
+ off
;
1965 m
->m_pkthdr
.mp_rseq
= rseq
+ off
;
1967 VERIFY(m_pktlen(m
) < UINT16_MAX
);
1968 m
->m_pkthdr
.mp_rlen
= (uint16_t)m_pktlen(m
);
1972 mpts
->mpts_flags
|= MPTSF_FULLY_ESTABLISHED
;
1978 * MPTCP subflow socket receive routine, derived from soreceive().
1981 mptcp_subflow_soreceive(struct socket
*so
, struct sockaddr
**psa
,
1982 struct uio
*uio
, struct mbuf
**mp0
, struct mbuf
**controlp
, int *flagsp
)
1985 struct socket
*mp_so
= mptetoso(tptomptp(sototcpcb(so
))->mpt_mpte
);
1986 int flags
, error
= 0;
1987 struct proc
*p
= current_proc();
1988 struct mbuf
*m
, **mp
= mp0
;
1989 boolean_t proc_held
= FALSE
;
1991 VERIFY(so
->so_proto
->pr_flags
& PR_CONNREQUIRED
);
1993 #ifdef MORE_LOCKING_DEBUG
1994 if (so
->so_usecount
== 1) {
1995 panic("%s: so=%x no other reference on socket\n", __func__
, so
);
2000 * We return all that is there in the subflow's socket receive buffer
2001 * to the MPTCP layer, so we require that the caller passes in the
2002 * expected parameters.
2004 if (mp
== NULL
|| controlp
!= NULL
) {
2012 if (flagsp
!= NULL
) {
2013 flags
= *flagsp
& ~MSG_EOR
;
2018 if (flags
& (MSG_PEEK
| MSG_OOB
| MSG_NEEDSA
| MSG_WAITALL
| MSG_WAITSTREAM
)) {
2022 flags
|= (MSG_DONTWAIT
| MSG_NBIO
);
2025 * If a recv attempt is made on a previously-accepted socket
2026 * that has been marked as inactive (disconnected), reject
2029 if (so
->so_flags
& SOF_DEFUNCT
) {
2030 struct sockbuf
*sb
= &so
->so_rcv
;
2034 * This socket should have been disconnected and flushed
2035 * prior to being returned from sodefunct(); there should
2036 * be no data on its receive list, so panic otherwise.
2038 if (so
->so_state
& SS_DEFUNCT
) {
2039 sb_empty_assert(sb
, __func__
);
2045 * See if the socket has been closed (SS_NOFDREF|SS_CANTRCVMORE)
2046 * and if so just return to the caller. This could happen when
2047 * soreceive() is called by a socket upcall function during the
2048 * time the socket is freed. The socket buffer would have been
2049 * locked across the upcall, therefore we cannot put this thread
2050 * to sleep (else we will deadlock) or return EWOULDBLOCK (else
2051 * we may livelock), because the lock on the socket buffer will
2052 * only be released when the upcall routine returns to its caller.
2053 * Because the socket has been officially closed, there can be
2054 * no further read on it.
2056 * A multipath subflow socket would have its SS_NOFDREF set by
2057 * default, so check for SOF_MP_SUBFLOW socket flag; when the
2058 * socket is closed for real, SOF_MP_SUBFLOW would be cleared.
2060 if ((so
->so_state
& (SS_NOFDREF
| SS_CANTRCVMORE
)) ==
2061 (SS_NOFDREF
| SS_CANTRCVMORE
) && !(so
->so_flags
& SOF_MP_SUBFLOW
)) {
2066 * For consistency with soreceive() semantics, we need to obey
2067 * SB_LOCK in case some other code path has locked the buffer.
2069 error
= sblock(&so
->so_rcv
, 0);
2074 m
= so
->so_rcv
.sb_mb
;
2077 * Panic if we notice inconsistencies in the socket's
2078 * receive list; both sb_mb and sb_cc should correctly
2079 * reflect the contents of the list, otherwise we may
2080 * end up with false positives during select() or poll()
2081 * which could put the application in a bad state.
2083 SB_MB_CHECK(&so
->so_rcv
);
2085 if (so
->so_error
!= 0) {
2086 error
= so
->so_error
;
2091 if (so
->so_state
& SS_CANTRCVMORE
) {
2095 if (!(so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
))) {
2101 * MSG_DONTWAIT is implicitly defined and this routine will
2102 * never block, so return EWOULDBLOCK when there is nothing.
2104 error
= EWOULDBLOCK
;
2108 mptcp_update_last_owner(so
, mp_so
);
2110 if (mp_so
->last_pid
!= proc_pid(p
)) {
2111 p
= proc_find(mp_so
->last_pid
);
2112 if (p
== PROC_NULL
) {
2119 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgrcv
);
2120 SBLASTRECORDCHK(&so
->so_rcv
, "mptcp_subflow_soreceive 1");
2121 SBLASTMBUFCHK(&so
->so_rcv
, "mptcp_subflow_soreceive 1");
2124 int dlen
= 0, dfin
= 0, error_out
= 0;
2125 struct mbuf
*start
= m
;
2131 VERIFY(m
->m_nextpkt
== NULL
);
2133 if ((m
->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
)) {
2134 orig_dlen
= dlen
= m
->m_pkthdr
.mp_rlen
;
2135 dsn
= m
->m_pkthdr
.mp_dsn
;
2136 sseq
= m
->m_pkthdr
.mp_rseq
;
2137 csum
= m
->m_pkthdr
.mp_csum
;
2139 /* We did fallback */
2140 if (mptcp_adj_rmap(so
, m
, 0, 0, 0, 0)) {
2146 sbfree(&so
->so_rcv
, m
);
2151 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
2156 so
->so_rcv
.sb_lastrecord
= m
;
2158 SB_EMPTY_FIXUP(&so
->so_rcv
);
2164 if (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP_DFIN
) {
2169 * Check if the full mapping is now present
2171 if ((int)so
->so_rcv
.sb_cc
< dlen
- dfin
) {
2172 mptcplog((LOG_INFO
, "%s not enough data (%u) need %u for dsn %u\n",
2173 __func__
, so
->so_rcv
.sb_cc
, dlen
, (uint32_t)dsn
),
2174 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_LOG
);
2177 error
= EWOULDBLOCK
;
2182 /* Now, get the full mapping */
2184 if (mptcp_adj_rmap(so
, m
, orig_dlen
- dlen
, dsn
, sseq
, orig_dlen
)) {
2193 sbfree(&so
->so_rcv
, m
);
2198 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
2202 if (dlen
- dfin
== 0) {
2206 VERIFY(dlen
<= 0 || m
);
2212 so
->so_rcv
.sb_lastrecord
= m
;
2214 SB_EMPTY_FIXUP(&so
->so_rcv
);
2221 if (mptcp_validate_csum(sototcpcb(so
), start
, dsn
, sseq
, orig_dlen
, csum
, dfin
)) {
2227 SBLASTRECORDCHK(&so
->so_rcv
, "mptcp_subflow_soreceive 2");
2228 SBLASTMBUFCHK(&so
->so_rcv
, "mptcp_subflow_soreceive 2");
2231 DTRACE_MPTCP3(subflow__receive
, struct socket
*, so
,
2232 struct sockbuf
*, &so
->so_rcv
, struct sockbuf
*, &so
->so_snd
);
2234 if (flagsp
!= NULL
) {
2239 sbunlock(&so
->so_rcv
, TRUE
);
2249 * MPTCP subflow socket send routine, derived from sosend().
2252 mptcp_subflow_sosend(struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
2253 struct mbuf
*top
, struct mbuf
*control
, int flags
)
2255 struct socket
*mp_so
= mptetoso(tptomptp(sototcpcb(so
))->mpt_mpte
);
2256 struct proc
*p
= current_proc();
2257 boolean_t en_tracing
= FALSE
, proc_held
= FALSE
;
2259 int sblocked
= 1; /* Pretend as if it is already locked, so we won't relock it */
2262 VERIFY(control
== NULL
);
2263 VERIFY(addr
== NULL
);
2264 VERIFY(uio
== NULL
);
2266 VERIFY((so
->so_flags
& SOF_CONTENT_FILTER
) == 0);
2268 VERIFY(top
->m_pkthdr
.len
> 0 && top
->m_pkthdr
.len
<= UINT16_MAX
);
2269 VERIFY(top
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
);
2272 * trace if tracing & network (vs. unix) sockets & and
2275 if (ENTR_SHOULDTRACE
&&
2276 (SOCK_CHECK_DOM(so
, AF_INET
) || SOCK_CHECK_DOM(so
, AF_INET6
))) {
2277 struct inpcb
*inp
= sotoinpcb(so
);
2278 if (inp
->inp_last_outifp
!= NULL
&&
2279 !(inp
->inp_last_outifp
->if_flags
& IFF_LOOPBACK
)) {
2281 en_tracing_val
= top
->m_pkthdr
.len
;
2282 KERNEL_ENERGYTRACE(kEnTrActKernSockWrite
, DBG_FUNC_START
,
2283 (unsigned long)VM_KERNEL_ADDRPERM(so
),
2284 ((so
->so_state
& SS_NBIO
) ? kEnTrFlagNonBlocking
: 0),
2285 (int64_t)en_tracing_val
);
2289 mptcp_update_last_owner(so
, mp_so
);
2291 if (mp_so
->last_pid
!= proc_pid(p
)) {
2292 p
= proc_find(mp_so
->last_pid
);
2293 if (p
== PROC_NULL
) {
2301 inp_update_necp_policy(sotoinpcb(so
), NULL
, NULL
, 0);
2304 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgsnd
);
2306 error
= sosendcheck(so
, NULL
, top
->m_pkthdr
.len
, 0, 1, 0, &sblocked
);
2311 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, top
, NULL
, NULL
, p
);
2323 soclearfastopen(so
);
2326 KERNEL_ENERGYTRACE(kEnTrActKernSockWrite
, DBG_FUNC_END
,
2327 (unsigned long)VM_KERNEL_ADDRPERM(so
),
2328 ((error
== EWOULDBLOCK
) ? kEnTrFlagNoWork
: 0),
2329 (int64_t)en_tracing_val
);
2336 * Establish an initial MPTCP connection (if first subflow and not yet
2337 * connected), or add a subflow to an existing MPTCP connection.
2340 mptcp_subflow_add(struct mptses
*mpte
, struct sockaddr
*src
,
2341 struct sockaddr
*dst
, uint32_t ifscope
, sae_connid_t
*pcid
)
2343 struct socket
*mp_so
, *so
= NULL
;
2344 struct mptcb
*mp_tp
;
2345 struct mptsub
*mpts
= NULL
;
2348 mp_so
= mptetoso(mpte
);
2349 mp_tp
= mpte
->mpte_mptcb
;
2351 socket_lock_assert_owned(mp_so
);
2353 if (mp_tp
->mpt_state
>= MPTCPS_CLOSE_WAIT
) {
2354 /* If the remote end sends Data FIN, refuse subflow adds */
2355 os_log_error(mptcp_log_handle
, "%s - %lx: state %u\n",
2356 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mp_tp
->mpt_state
);
2361 if (mpte
->mpte_numflows
> MPTCP_MAX_NUM_SUBFLOWS
) {
2366 mpts
= mptcp_subflow_alloc();
2368 os_log_error(mptcp_log_handle
, "%s - %lx: malloc subflow failed\n",
2369 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
2375 if (src
->sa_family
!= AF_INET
&& src
->sa_family
!= AF_INET6
) {
2376 error
= EAFNOSUPPORT
;
2380 if (src
->sa_family
== AF_INET
&&
2381 src
->sa_len
!= sizeof(struct sockaddr_in
)) {
2386 if (src
->sa_family
== AF_INET6
&&
2387 src
->sa_len
!= sizeof(struct sockaddr_in6
)) {
2392 MALLOC(mpts
->mpts_src
, struct sockaddr
*, src
->sa_len
, M_SONAME
,
2394 if (mpts
->mpts_src
== NULL
) {
2398 bcopy(src
, mpts
->mpts_src
, src
->sa_len
);
2401 if (dst
->sa_family
!= AF_INET
&& dst
->sa_family
!= AF_INET6
) {
2402 error
= EAFNOSUPPORT
;
2406 if (dst
->sa_family
== AF_INET
&&
2407 dst
->sa_len
!= sizeof(mpts
->__mpts_dst_v4
)) {
2412 if (dst
->sa_family
== AF_INET6
&&
2413 dst
->sa_len
!= sizeof(mpts
->__mpts_dst_v6
)) {
2418 memcpy(&mpts
->mpts_u_dst
, dst
, dst
->sa_len
);
2420 af
= mpts
->mpts_dst
.sa_family
;
2422 ifnet_head_lock_shared();
2423 if ((ifscope
> (unsigned)if_index
)) {
2430 mpts
->mpts_ifscope
= ifscope
;
2432 /* create the subflow socket */
2433 if ((error
= mptcp_subflow_socreate(mpte
, mpts
, af
, &so
)) != 0) {
2435 * Returning (error) and not cleaning up, because up to here
2436 * all we did is creating mpts.
2438 * And the contract is that the call to mptcp_subflow_socreate,
2439 * moves ownership of mpts to mptcp_subflow_socreate.
2445 * We may be called from within the kernel. Still need to account this
2446 * one to the real app.
2448 mptcp_update_last_owner(mpts
->mpts_socket
, mp_so
);
2451 * Increment the counter, while avoiding 0 (SAE_CONNID_ANY) and
2452 * -1 (SAE_CONNID_ALL).
2454 mpte
->mpte_connid_last
++;
2455 if (mpte
->mpte_connid_last
== SAE_CONNID_ALL
||
2456 mpte
->mpte_connid_last
== SAE_CONNID_ANY
) {
2457 mpte
->mpte_connid_last
++;
2460 mpts
->mpts_connid
= mpte
->mpte_connid_last
;
2462 mpts
->mpts_rel_seq
= 1;
2464 /* Allocate a unique address id per subflow */
2465 mpte
->mpte_addrid_last
++;
2466 if (mpte
->mpte_addrid_last
== 0) {
2467 mpte
->mpte_addrid_last
++;
2470 /* register for subflow socket read/write events */
2471 sock_setupcalls_locked(so
, NULL
, NULL
, mptcp_subflow_wupcall
, mpts
, 1);
2473 /* Register for subflow socket control events */
2474 sock_catchevents_locked(so
, mptcp_subflow_eupcall1
, mpts
,
2475 SO_FILT_HINT_CONNRESET
| SO_FILT_HINT_CANTRCVMORE
|
2476 SO_FILT_HINT_TIMEOUT
| SO_FILT_HINT_NOSRCADDR
|
2477 SO_FILT_HINT_IFDENIED
| SO_FILT_HINT_CONNECTED
|
2478 SO_FILT_HINT_DISCONNECTED
| SO_FILT_HINT_MPFAILOVER
|
2479 SO_FILT_HINT_MPSTATUS
| SO_FILT_HINT_MUSTRST
|
2480 SO_FILT_HINT_MPCANTRCVMORE
| SO_FILT_HINT_ADAPTIVE_RTIMO
|
2481 SO_FILT_HINT_ADAPTIVE_WTIMO
| SO_FILT_HINT_MP_SUB_ERROR
);
2484 VERIFY(!(mpts
->mpts_flags
&
2485 (MPTSF_CONNECTING
| MPTSF_CONNECTED
| MPTSF_CONNECT_PENDING
)));
2488 * Indicate to the TCP subflow whether or not it should establish
2489 * the initial MPTCP connection, or join an existing one. Fill
2490 * in the connection request structure with additional info needed
2491 * by the underlying TCP (to be used in the TCP options, etc.)
2493 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
&& mpte
->mpte_numflows
== 1) {
2494 mpts
->mpts_flags
|= MPTSF_INITIAL_SUB
;
2496 if (mp_tp
->mpt_state
== MPTCPS_CLOSED
) {
2497 mptcp_init_local_parms(mpte
);
2499 soisconnecting(mp_so
);
2501 /* If fastopen is requested, set state in mpts */
2502 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
2503 mpts
->mpts_flags
|= MPTSF_TFO_REQD
;
2506 if (!(mp_tp
->mpt_flags
& MPTCPF_JOIN_READY
)) {
2507 mpts
->mpts_flags
|= MPTSF_CONNECT_PENDING
;
2511 mpts
->mpts_flags
|= MPTSF_CONNECTING
;
2513 /* connect right away if first attempt, or if join can be done now */
2514 if (!(mpts
->mpts_flags
& MPTSF_CONNECT_PENDING
)) {
2515 error
= mptcp_subflow_soconnectx(mpte
, mpts
);
2523 *pcid
= mpts
->mpts_connid
;
2529 mptcp_subflow_abort(mpts
, error
);
2535 mptcp_subflow_free(mpts
);
2542 mptcpstats_update(struct mptcp_itf_stats
*stats
, const struct mptsub
*mpts
)
2544 int index
= mptcpstats_get_index(stats
, mpts
);
2547 struct inpcb
*inp
= sotoinpcb(mpts
->mpts_socket
);
2549 stats
[index
].mpis_txbytes
+= inp
->inp_stat
->txbytes
;
2550 stats
[index
].mpis_rxbytes
+= inp
->inp_stat
->rxbytes
;
2552 stats
[index
].mpis_wifi_txbytes
+= inp
->inp_wstat
->txbytes
;
2553 stats
[index
].mpis_wifi_rxbytes
+= inp
->inp_wstat
->rxbytes
;
2555 stats
[index
].mpis_wired_txbytes
+= inp
->inp_Wstat
->txbytes
;
2556 stats
[index
].mpis_wired_rxbytes
+= inp
->inp_Wstat
->rxbytes
;
2558 stats
[index
].mpis_cell_txbytes
+= inp
->inp_cstat
->txbytes
;
2559 stats
[index
].mpis_cell_rxbytes
+= inp
->inp_cstat
->rxbytes
;
2564 * Delete/remove a subflow from an MPTCP. The underlying subflow socket
2565 * will no longer be accessible after a subflow is deleted, thus this
2566 * should occur only after the subflow socket has been disconnected.
2569 mptcp_subflow_del(struct mptses
*mpte
, struct mptsub
*mpts
)
2571 struct socket
*mp_so
= mptetoso(mpte
);
2572 struct socket
*so
= mpts
->mpts_socket
;
2573 struct tcpcb
*tp
= sototcpcb(so
);
2575 socket_lock_assert_owned(mp_so
);
2576 VERIFY(mpts
->mpts_mpte
== mpte
);
2577 VERIFY(mpts
->mpts_flags
& MPTSF_ATTACHED
);
2578 VERIFY(mpte
->mpte_numflows
!= 0);
2579 VERIFY(mp_so
->so_usecount
> 0);
2581 mptcpstats_update(mpte
->mpte_itfstats
, mpts
);
2583 mptcp_unset_cellicon(mpte
, mpts
, 1);
2585 mpte
->mpte_init_rxbytes
= sotoinpcb(so
)->inp_stat
->rxbytes
;
2586 mpte
->mpte_init_txbytes
= sotoinpcb(so
)->inp_stat
->txbytes
;
2588 atomic_bitclear_32(&mpts
->mpts_flags
, MPTSF_ATTACHED
);
2589 TAILQ_REMOVE(&mpte
->mpte_subflows
, mpts
, mpts_entry
);
2590 mpte
->mpte_numflows
--;
2591 if (mpte
->mpte_active_sub
== mpts
) {
2592 mpte
->mpte_active_sub
= NULL
;
2596 * Drop references held by this subflow socket; there
2597 * will be no further upcalls made from this point.
2599 sock_setupcalls_locked(so
, NULL
, NULL
, NULL
, NULL
, 0);
2600 sock_catchevents_locked(so
, NULL
, NULL
, 0);
2602 mptcp_detach_mptcb_from_subf(mpte
->mpte_mptcb
, so
);
2604 mp_so
->so_usecount
--; /* for subflow socket */
2605 mpts
->mpts_mpte
= NULL
;
2606 mpts
->mpts_socket
= NULL
;
2608 mptcp_subflow_remref(mpts
); /* for MPTCP subflow list */
2609 mptcp_subflow_remref(mpts
); /* for subflow socket */
2611 so
->so_flags
&= ~SOF_MP_SUBFLOW
;
2617 mptcp_subflow_shutdown(struct mptses
*mpte
, struct mptsub
*mpts
)
2619 struct socket
*so
= mpts
->mpts_socket
;
2620 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
2623 if (mp_tp
->mpt_state
> MPTCPS_CLOSE_WAIT
) {
2627 if (!(so
->so_state
& (SS_ISDISCONNECTING
| SS_ISDISCONNECTED
)) &&
2628 (so
->so_state
& SS_ISCONNECTED
)) {
2629 mptcplog((LOG_DEBUG
, "MPTCP subflow shutdown %s: cid %d fin %d\n",
2630 __func__
, mpts
->mpts_connid
, send_dfin
),
2631 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
2634 mptcp_send_dfin(so
);
2636 soshutdownlock(so
, SHUT_WR
);
2641 mptcp_subflow_abort(struct mptsub
*mpts
, int error
)
2643 struct socket
*so
= mpts
->mpts_socket
;
2644 struct tcpcb
*tp
= sototcpcb(so
);
2646 if (mpts
->mpts_flags
& MPTSF_DISCONNECTED
) {
2650 mptcplog((LOG_DEBUG
, "%s aborting connection state %u\n", __func__
, tp
->t_state
),
2651 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
2653 if (tp
->t_state
!= TCPS_CLOSED
) {
2654 tcp_drop(tp
, error
);
2657 mptcp_subflow_eupcall1(so
, mpts
, SO_FILT_HINT_DISCONNECTED
);
2661 * Disconnect a subflow socket.
2664 mptcp_subflow_disconnect(struct mptses
*mpte
, struct mptsub
*mpts
)
2666 struct socket
*so
, *mp_so
;
2667 struct mptcb
*mp_tp
;
2670 so
= mpts
->mpts_socket
;
2671 mp_tp
= mpte
->mpte_mptcb
;
2672 mp_so
= mptetoso(mpte
);
2674 socket_lock_assert_owned(mp_so
);
2676 if (mpts
->mpts_flags
& (MPTSF_DISCONNECTING
| MPTSF_DISCONNECTED
)) {
2680 mptcp_unset_cellicon(mpte
, mpts
, 1);
2682 mpts
->mpts_flags
|= MPTSF_DISCONNECTING
;
2684 if (mp_tp
->mpt_state
> MPTCPS_CLOSE_WAIT
) {
2688 if (!(so
->so_state
& (SS_ISDISCONNECTING
| SS_ISDISCONNECTED
)) &&
2689 (so
->so_state
& SS_ISCONNECTED
)) {
2690 mptcplog((LOG_DEBUG
, "%s: cid %d fin %d\n",
2691 __func__
, mpts
->mpts_connid
, send_dfin
),
2692 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
2695 mptcp_send_dfin(so
);
2698 if (mp_so
->so_flags
& SOF_DEFUNCT
) {
2701 ret
= sosetdefunct(NULL
, so
, SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL
, TRUE
);
2703 ret
= sodefunct(NULL
, so
, SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL
);
2706 os_log_error(mptcp_log_handle
, "%s - %lx: sodefunct failed with %d\n",
2707 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), ret
);
2710 os_log_error(mptcp_log_handle
, "%s - %lx: sosetdefunct failed with %d\n",
2711 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), ret
);
2714 (void) soshutdownlock(so
, SHUT_RD
);
2715 (void) soshutdownlock(so
, SHUT_WR
);
2716 (void) sodisconnectlocked(so
);
2721 * Generate a disconnect event for this subflow socket, in case
2722 * the lower layer doesn't do it; this is needed because the
2723 * subflow socket deletion relies on it.
2725 mptcp_subflow_eupcall1(so
, mpts
, SO_FILT_HINT_DISCONNECTED
);
2729 * Subflow socket input.
2732 mptcp_subflow_input(struct mptses
*mpte
, struct mptsub
*mpts
)
2734 struct socket
*mp_so
= mptetoso(mpte
);
2735 struct mbuf
*m
= NULL
;
2737 int error
, wakeup
= 0;
2739 VERIFY(!(mpte
->mpte_mppcb
->mpp_flags
& MPP_INSIDE_INPUT
));
2740 mpte
->mpte_mppcb
->mpp_flags
|= MPP_INSIDE_INPUT
;
2742 DTRACE_MPTCP2(subflow__input
, struct mptses
*, mpte
,
2743 struct mptsub
*, mpts
);
2745 if (!(mpts
->mpts_flags
& MPTSF_CONNECTED
)) {
2749 so
= mpts
->mpts_socket
;
2751 error
= sock_receive_internal(so
, NULL
, &m
, 0, NULL
);
2752 if (error
!= 0 && error
!= EWOULDBLOCK
) {
2753 os_log_error(mptcp_log_handle
, "%s - %lx: cid %d error %d\n",
2754 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpts
->mpts_connid
, error
);
2755 if (error
== ENODATA
) {
2757 * Don't ignore ENODATA so as to discover
2758 * nasty middleboxes.
2760 mp_so
->so_error
= ENODATA
;
2765 } else if (error
== 0) {
2766 mptcplog((LOG_DEBUG
, "%s: cid %d \n", __func__
, mpts
->mpts_connid
),
2767 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_VERBOSE
);
2770 /* In fallback, make sure to accept data on all but one subflow */
2771 if (m
&& (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) &&
2772 !(mpts
->mpts_flags
& MPTSF_ACTIVE
)) {
2773 mptcplog((LOG_DEBUG
, "%s: degraded and got data on non-active flow\n",
2774 __func__
), MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_VERBOSE
);
2780 if (IFNET_IS_CELLULAR(sotoinpcb(so
)->inp_last_outifp
)) {
2781 mptcp_set_cellicon(mpte
, mpts
);
2783 mpte
->mpte_used_cell
= 1;
2786 * If during the past MPTCP_CELLICON_TOGGLE_RATE seconds we didn't
2787 * explicitly set the cellicon, then we unset it again.
2789 if (TSTMP_LT(mpte
->mpte_last_cellicon_set
+ MPTCP_CELLICON_TOGGLE_RATE
, tcp_now
)) {
2790 mptcp_unset_cellicon(mpte
, NULL
, 1);
2793 mpte
->mpte_used_wifi
= 1;
2796 mptcp_input(mpte
, m
);
2801 mpte
->mpte_mppcb
->mpp_flags
|= MPP_SHOULD_RWAKEUP
;
2804 mptcp_handle_deferred_upcalls(mpte
->mpte_mppcb
, MPP_INSIDE_INPUT
);
2808 mptcp_handle_input(struct socket
*so
)
2810 struct mptsub
*mpts
, *tmpts
;
2811 struct mptses
*mpte
;
2813 if (!(so
->so_flags
& SOF_MP_SUBFLOW
)) {
2817 mpts
= sototcpcb(so
)->t_mpsub
;
2818 mpte
= mpts
->mpts_mpte
;
2820 socket_lock_assert_owned(mptetoso(mpte
));
2822 if (mptcp_should_defer_upcall(mpte
->mpte_mppcb
)) {
2823 if (!(mpte
->mpte_mppcb
->mpp_flags
& MPP_INPUT_HANDLE
)) {
2824 mpte
->mpte_mppcb
->mpp_flags
|= MPP_SHOULD_RWAKEUP
;
2829 mpte
->mpte_mppcb
->mpp_flags
|= MPP_INPUT_HANDLE
;
2830 TAILQ_FOREACH_SAFE(mpts
, &mpte
->mpte_subflows
, mpts_entry
, tmpts
) {
2831 if (mpts
->mpts_socket
->so_usecount
== 0) {
2832 /* Will be removed soon by tcp_garbage_collect */
2836 mptcp_subflow_addref(mpts
);
2837 mpts
->mpts_socket
->so_usecount
++;
2839 mptcp_subflow_input(mpte
, mpts
);
2841 mptcp_subflow_remref(mpts
); /* ours */
2843 VERIFY(mpts
->mpts_socket
->so_usecount
!= 0);
2844 mpts
->mpts_socket
->so_usecount
--;
2847 mptcp_handle_deferred_upcalls(mpte
->mpte_mppcb
, MPP_INPUT_HANDLE
);
2851 * Subflow socket write upcall.
2853 * Called when the associated subflow socket posted a read event.
2856 mptcp_subflow_wupcall(struct socket
*so
, void *arg
, int waitf
)
2858 #pragma unused(so, waitf)
2859 struct mptsub
*mpts
= arg
;
2860 struct mptses
*mpte
= mpts
->mpts_mpte
;
2862 VERIFY(mpte
!= NULL
);
2864 if (mptcp_should_defer_upcall(mpte
->mpte_mppcb
)) {
2865 if (!(mpte
->mpte_mppcb
->mpp_flags
& MPP_WUPCALL
)) {
2866 mpte
->mpte_mppcb
->mpp_flags
|= MPP_SHOULD_WWAKEUP
;
2875 mptcp_search_seq_in_sub(struct mbuf
*m
, struct socket
*so
)
2877 struct mbuf
*so_m
= so
->so_snd
.sb_mb
;
2878 uint64_t dsn
= m
->m_pkthdr
.mp_dsn
;
2881 VERIFY(so_m
->m_flags
& M_PKTHDR
);
2882 VERIFY(so_m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
);
2884 /* Part of the segment is covered, don't reinject here */
2885 if (so_m
->m_pkthdr
.mp_dsn
<= dsn
&&
2886 so_m
->m_pkthdr
.mp_dsn
+ so_m
->m_pkthdr
.mp_rlen
> dsn
) {
2890 so_m
= so_m
->m_next
;
2897 * Subflow socket output.
2899 * Called for sending data from MPTCP to the underlying subflow socket.
2902 mptcp_subflow_output(struct mptses
*mpte
, struct mptsub
*mpts
, int flags
)
2904 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
2905 struct mbuf
*sb_mb
, *m
, *mpt_mbuf
= NULL
, *head
, *tail
;
2906 struct socket
*mp_so
, *so
;
2908 uint64_t mpt_dsn
= 0, off
= 0;
2909 int sb_cc
= 0, error
= 0, wakeup
= 0;
2911 uint16_t tot_sent
= 0;
2912 boolean_t reinjected
= FALSE
;
2914 mp_so
= mptetoso(mpte
);
2915 so
= mpts
->mpts_socket
;
2918 socket_lock_assert_owned(mp_so
);
2920 VERIFY(!(mpte
->mpte_mppcb
->mpp_flags
& MPP_INSIDE_OUTPUT
));
2921 mpte
->mpte_mppcb
->mpp_flags
|= MPP_INSIDE_OUTPUT
;
2923 VERIFY(!INP_WAIT_FOR_IF_FEEDBACK(sotoinpcb(so
)));
2924 VERIFY((mpts
->mpts_flags
& MPTSF_MP_CAPABLE
) ||
2925 (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) ||
2926 (mpts
->mpts_flags
& MPTSF_TFO_REQD
));
2927 VERIFY(mptcp_subflow_cwnd_space(mpts
->mpts_socket
) > 0);
2929 mptcplog((LOG_DEBUG
, "%s mpts_flags %#x, mpte_flags %#x cwnd_space %u\n",
2930 __func__
, mpts
->mpts_flags
, mpte
->mpte_flags
,
2931 mptcp_subflow_cwnd_space(so
)),
2932 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
2933 DTRACE_MPTCP2(subflow__output
, struct mptses
*, mpte
,
2934 struct mptsub
*, mpts
);
2936 /* Remove Addr Option is not sent reliably as per I-D */
2937 if (mpte
->mpte_flags
& MPTE_SND_REM_ADDR
) {
2938 tp
->t_rem_aid
= mpte
->mpte_lost_aid
;
2939 tp
->t_mpflags
|= TMPF_SND_REM_ADDR
;
2940 mpte
->mpte_flags
&= ~MPTE_SND_REM_ADDR
;
2944 * The mbuf chains containing the metadata (as well as pointing to
2945 * the user data sitting at the MPTCP output queue) would then be
2946 * sent down to the subflow socket.
2948 * Some notes on data sequencing:
2950 * a. Each mbuf must be a M_PKTHDR.
2951 * b. MPTCP metadata is stored in the mptcp_pktinfo structure
2952 * in the mbuf pkthdr structure.
2953 * c. Each mbuf containing the MPTCP metadata must have its
2954 * pkt_flags marked with the PKTF_MPTCP flag.
2957 if (mpte
->mpte_reinjectq
) {
2958 sb_mb
= mpte
->mpte_reinjectq
;
2960 sb_mb
= mp_so
->so_snd
.sb_mb
;
2963 if (sb_mb
== NULL
) {
2964 os_log_error(mptcp_log_handle
, "%s - %lx: No data in MPTCP-sendbuffer! smax %u snxt %u suna %u state %u flags %#x\n",
2965 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
2966 (uint32_t)mp_tp
->mpt_sndmax
, (uint32_t)mp_tp
->mpt_sndnxt
,
2967 (uint32_t)mp_tp
->mpt_snduna
, mp_tp
->mpt_state
, mp_so
->so_flags1
);
2969 /* Fix it to prevent looping */
2970 if (MPTCP_SEQ_LT(mp_tp
->mpt_sndnxt
, mp_tp
->mpt_snduna
)) {
2971 mp_tp
->mpt_sndnxt
= mp_tp
->mpt_snduna
;
2976 VERIFY(sb_mb
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
);
2978 if (sb_mb
->m_pkthdr
.mp_rlen
== 0 &&
2979 !(so
->so_state
& SS_ISCONNECTED
) &&
2980 (so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
2981 tp
->t_mpflags
|= TMPF_TFO_REQUEST
;
2982 goto zero_len_write
;
2985 mpt_dsn
= sb_mb
->m_pkthdr
.mp_dsn
;
2987 /* First, drop acknowledged data */
2988 if (MPTCP_SEQ_LT(mpt_dsn
, mp_tp
->mpt_snduna
)) {
2989 os_log_error(mptcp_log_handle
, "%s - %lx: dropping data, should have been done earlier "
2990 "dsn %u suna %u reinject? %u\n",
2991 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), (uint32_t)mpt_dsn
,
2992 (uint32_t)mp_tp
->mpt_snduna
, !!mpte
->mpte_reinjectq
);
2993 if (mpte
->mpte_reinjectq
) {
2994 mptcp_clean_reinjectq(mpte
);
2997 len
= mp_tp
->mpt_snduna
- mpt_dsn
;
2998 sbdrop(&mp_so
->so_snd
, (int)len
);
3003 /* Check again because of above sbdrop */
3004 if (mp_so
->so_snd
.sb_mb
== NULL
&& mpte
->mpte_reinjectq
== NULL
) {
3005 os_log_error(mptcp_log_handle
, "%s - $%lx: send-buffer is empty\n",
3006 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
3011 * In degraded mode, we don't receive data acks, so force free
3012 * mbufs less than snd_nxt
3014 if ((mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) &&
3015 (mp_tp
->mpt_flags
& MPTCPF_POST_FALLBACK_SYNC
) &&
3016 mp_so
->so_snd
.sb_mb
) {
3017 mpt_dsn
= mp_so
->so_snd
.sb_mb
->m_pkthdr
.mp_dsn
;
3018 if (MPTCP_SEQ_LT(mpt_dsn
, mp_tp
->mpt_snduna
)) {
3020 len
= mp_tp
->mpt_snduna
- mpt_dsn
;
3021 sbdrop(&mp_so
->so_snd
, (int)len
);
3024 os_log_error(mptcp_log_handle
, "%s - %lx: dropping data in degraded mode, should have been done earlier dsn %u sndnxt %u suna %u\n",
3025 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
3026 (uint32_t)mpt_dsn
, (uint32_t)mp_tp
->mpt_sndnxt
, (uint32_t)mp_tp
->mpt_snduna
);
3030 if ((mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) &&
3031 !(mp_tp
->mpt_flags
& MPTCPF_POST_FALLBACK_SYNC
)) {
3032 mp_tp
->mpt_flags
|= MPTCPF_POST_FALLBACK_SYNC
;
3033 so
->so_flags1
|= SOF1_POST_FALLBACK_SYNC
;
3037 * Adjust the top level notion of next byte used for retransmissions
3040 if (MPTCP_SEQ_LT(mp_tp
->mpt_sndnxt
, mp_tp
->mpt_snduna
)) {
3041 mp_tp
->mpt_sndnxt
= mp_tp
->mpt_snduna
;
3044 /* Now determine the offset from which to start transmitting data */
3045 if (mpte
->mpte_reinjectq
) {
3046 sb_mb
= mpte
->mpte_reinjectq
;
3049 sb_mb
= mp_so
->so_snd
.sb_mb
;
3051 if (sb_mb
== NULL
) {
3052 os_log_error(mptcp_log_handle
, "%s - %lx: send-buffer is still empty\n", __func__
,
3053 (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
3057 if (sb_mb
== mpte
->mpte_reinjectq
) {
3058 sb_cc
= sb_mb
->m_pkthdr
.mp_rlen
;
3061 if (mptcp_search_seq_in_sub(sb_mb
, so
)) {
3062 if (mptcp_can_send_more(mp_tp
, TRUE
)) {
3071 } else if (flags
& MPTCP_SUBOUT_PROBING
) {
3072 sb_cc
= sb_mb
->m_pkthdr
.mp_rlen
;
3075 sb_cc
= min(mp_so
->so_snd
.sb_cc
, mp_tp
->mpt_sndwnd
);
3078 * With TFO, there might be no data at all, thus still go into this
3081 if ((mp_so
->so_flags1
& SOF1_PRECONNECT_DATA
) ||
3082 MPTCP_SEQ_LT(mp_tp
->mpt_sndnxt
, mp_tp
->mpt_sndmax
)) {
3083 off
= mp_tp
->mpt_sndnxt
- mp_tp
->mpt_snduna
;
3086 os_log_error(mptcp_log_handle
, "%s - %lx: this should not happen: sndnxt %u sndmax %u\n",
3087 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), (uint32_t)mp_tp
->mpt_sndnxt
,
3088 (uint32_t)mp_tp
->mpt_sndmax
);
3094 sb_cc
= min(sb_cc
, mptcp_subflow_cwnd_space(so
));
3096 os_log_error(mptcp_log_handle
, "%s - %lx: sb_cc is %d, mp_so->sb_cc %u, sndwnd %u,sndnxt %u sndmax %u cwnd %u\n",
3097 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), sb_cc
, mp_so
->so_snd
.sb_cc
, mp_tp
->mpt_sndwnd
,
3098 (uint32_t)mp_tp
->mpt_sndnxt
, (uint32_t)mp_tp
->mpt_sndmax
,
3099 mptcp_subflow_cwnd_space(so
));
3102 sb_cc
= min(sb_cc
, UINT16_MAX
);
3105 * Create a DSN mapping for the data we are about to send. It all
3106 * has the same mapping.
3109 mpt_dsn
= sb_mb
->m_pkthdr
.mp_dsn
;
3111 mpt_dsn
= mp_tp
->mpt_snduna
+ off
;
3115 while (mpt_mbuf
&& reinjected
== FALSE
&&
3116 (mpt_mbuf
->m_pkthdr
.mp_rlen
== 0 ||
3117 mpt_mbuf
->m_pkthdr
.mp_rlen
<= (uint32_t)off
)) {
3118 off
-= mpt_mbuf
->m_pkthdr
.mp_rlen
;
3119 mpt_mbuf
= mpt_mbuf
->m_next
;
3121 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) {
3122 mptcplog((LOG_DEBUG
, "%s: %u snduna = %u sndnxt = %u probe %d\n",
3123 __func__
, mpts
->mpts_connid
, (uint32_t)mp_tp
->mpt_snduna
, (uint32_t)mp_tp
->mpt_sndnxt
,
3124 mpts
->mpts_probecnt
),
3125 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
3128 VERIFY((mpt_mbuf
== NULL
) || (mpt_mbuf
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
));
3132 while (tot_sent
< sb_cc
) {
3135 mlen
= mpt_mbuf
->m_len
;
3137 mlen
= MIN(mlen
, sb_cc
- tot_sent
);
3140 os_log_error(mptcp_log_handle
, "%s - %lx: mlen %d mp_rlen %u off %u sb_cc %u tot_sent %u\n",
3141 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mlen
, mpt_mbuf
->m_pkthdr
.mp_rlen
,
3142 (uint32_t)off
, sb_cc
, tot_sent
);
3150 m
= m_copym_mode(mpt_mbuf
, (int)off
, mlen
, M_DONTWAIT
,
3151 M_COPYM_MUST_COPY_HDR
);
3153 os_log_error(mptcp_log_handle
, "%s - %lx: m_copym_mode failed\n", __func__
,
3154 (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
3159 /* Create a DSN mapping for the data (m_copym does it) */
3160 VERIFY(m
->m_flags
& M_PKTHDR
);
3161 VERIFY(m
->m_next
== NULL
);
3163 m
->m_pkthdr
.pkt_flags
|= PKTF_MPTCP
;
3164 m
->m_pkthdr
.pkt_flags
&= ~PKTF_MPSO
;
3165 m
->m_pkthdr
.mp_dsn
= mpt_dsn
;
3166 m
->m_pkthdr
.mp_rseq
= mpts
->mpts_rel_seq
;
3167 m
->m_pkthdr
.len
= mlen
;
3179 mpt_mbuf
= mpt_mbuf
->m_next
;
3183 if (sb_cc
< sb_mb
->m_pkthdr
.mp_rlen
) {
3184 struct mbuf
*n
= sb_mb
;
3187 n
->m_pkthdr
.mp_dsn
+= sb_cc
;
3188 n
->m_pkthdr
.mp_rlen
-= sb_cc
;
3191 m_adj(sb_mb
, sb_cc
);
3193 mpte
->mpte_reinjectq
= sb_mb
->m_nextpkt
;
3198 mptcplog((LOG_DEBUG
, "%s: Queued dsn %u ssn %u len %u on sub %u\n",
3199 __func__
, (uint32_t)mpt_dsn
, mpts
->mpts_rel_seq
,
3200 tot_sent
, mpts
->mpts_connid
), MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
3202 if (head
&& (mp_tp
->mpt_flags
& MPTCPF_CHECKSUM
)) {
3203 dss_csum
= mptcp_output_csum(head
, mpt_dsn
, mpts
->mpts_rel_seq
,
3207 /* Now, let's update rel-seq and the data-level length */
3208 mpts
->mpts_rel_seq
+= tot_sent
;
3211 if (mp_tp
->mpt_flags
& MPTCPF_CHECKSUM
) {
3212 m
->m_pkthdr
.mp_csum
= dss_csum
;
3214 m
->m_pkthdr
.mp_rlen
= tot_sent
;
3219 if ((mpts
->mpts_flags
& MPTSF_TFO_REQD
) &&
3220 (tp
->t_tfo_stats
== 0)) {
3221 tp
->t_mpflags
|= TMPF_TFO_REQUEST
;
3224 error
= sock_sendmbuf(so
, NULL
, head
, 0, NULL
);
3226 DTRACE_MPTCP7(send
, struct mbuf
*, m
, struct socket
*, so
,
3227 struct sockbuf
*, &so
->so_rcv
,
3228 struct sockbuf
*, &so
->so_snd
,
3229 struct mptses
*, mpte
, struct mptsub
*, mpts
,
3235 (error
== EWOULDBLOCK
&& (tp
->t_mpflags
& TMPF_TFO_REQUEST
))) {
3236 uint64_t new_sndnxt
= mp_tp
->mpt_sndnxt
+ tot_sent
;
3238 if (mpts
->mpts_probesoon
&& mpts
->mpts_maxseg
&& tot_sent
) {
3239 tcpstat
.tcps_mp_num_probes
++;
3240 if ((uint32_t)tot_sent
< mpts
->mpts_maxseg
) {
3241 mpts
->mpts_probecnt
+= 1;
3243 mpts
->mpts_probecnt
+=
3244 tot_sent
/ mpts
->mpts_maxseg
;
3248 if (!reinjected
&& !(flags
& MPTCP_SUBOUT_PROBING
)) {
3249 if (MPTCP_DATASEQ_HIGH32(new_sndnxt
) >
3250 MPTCP_DATASEQ_HIGH32(mp_tp
->mpt_sndnxt
)) {
3251 mp_tp
->mpt_flags
|= MPTCPF_SND_64BITDSN
;
3253 mp_tp
->mpt_sndnxt
= new_sndnxt
;
3256 mptcp_cancel_timer(mp_tp
, MPTT_REXMT
);
3258 /* Must be here as mptcp_can_send_more() checks for this */
3259 soclearfastopen(mp_so
);
3261 if ((mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) ||
3262 (mpts
->mpts_probesoon
!= 0)) {
3263 mptcplog((LOG_DEBUG
, "%s %u degraded %u wrote %d %d probe %d probedelta %d\n",
3264 __func__
, mpts
->mpts_connid
,
3265 !!(mpts
->mpts_flags
& MPTSF_MP_DEGRADED
),
3266 tot_sent
, (int) sb_cc
, mpts
->mpts_probecnt
,
3267 (tcp_now
- mpts
->mpts_probesoon
)),
3268 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
3271 if (IFNET_IS_CELLULAR(sotoinpcb(so
)->inp_last_outifp
)) {
3272 mptcp_set_cellicon(mpte
, mpts
);
3274 mpte
->mpte_used_cell
= 1;
3277 * If during the past MPTCP_CELLICON_TOGGLE_RATE seconds we didn't
3278 * explicitly set the cellicon, then we unset it again.
3280 if (TSTMP_LT(mpte
->mpte_last_cellicon_set
+ MPTCP_CELLICON_TOGGLE_RATE
, tcp_now
)) {
3281 mptcp_unset_cellicon(mpte
, NULL
, 1);
3284 mpte
->mpte_used_wifi
= 1;
3288 * Don't propagate EWOULDBLOCK - it's already taken care of
3289 * in mptcp_usr_send for TFO.
3293 os_log_error(mptcp_log_handle
, "%s - %lx: %u error %d len %d subflags %#x sostate %#x soerror %u hiwat %u lowat %u\n",
3294 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpts
->mpts_connid
, error
, tot_sent
, so
->so_flags
, so
->so_state
, so
->so_error
, so
->so_snd
.sb_hiwat
, so
->so_snd
.sb_lowat
);
3299 mpte
->mpte_mppcb
->mpp_flags
|= MPP_SHOULD_WWAKEUP
;
3302 mptcp_handle_deferred_upcalls(mpte
->mpte_mppcb
, MPP_INSIDE_OUTPUT
);
3306 /* Opting to call pru_send as no mbuf at subflow level */
3307 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, NULL
, NULL
,
3308 NULL
, current_proc());
3314 mptcp_add_reinjectq(struct mptses
*mpte
, struct mbuf
*m
)
3316 struct mbuf
*n
, *prev
= NULL
;
3318 mptcplog((LOG_DEBUG
, "%s reinjecting dsn %u dlen %u rseq %u\n",
3319 __func__
, (uint32_t)m
->m_pkthdr
.mp_dsn
, m
->m_pkthdr
.mp_rlen
,
3320 m
->m_pkthdr
.mp_rseq
),
3321 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
3323 n
= mpte
->mpte_reinjectq
;
3325 /* First, look for an mbuf n, whose data-sequence-number is bigger or
3326 * equal than m's sequence number.
3329 if (MPTCP_SEQ_GEQ(n
->m_pkthdr
.mp_dsn
, m
->m_pkthdr
.mp_dsn
)) {
3339 /* m is already fully covered by the next mbuf in the queue */
3340 if (n
->m_pkthdr
.mp_dsn
== m
->m_pkthdr
.mp_dsn
&&
3341 n
->m_pkthdr
.mp_rlen
>= m
->m_pkthdr
.mp_rlen
) {
3342 mptcplog((LOG_DEBUG
, "%s fully covered with len %u\n",
3343 __func__
, n
->m_pkthdr
.mp_rlen
),
3344 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
3348 /* m is covering the next mbuf entirely, thus we remove this guy */
3349 if (m
->m_pkthdr
.mp_dsn
+ m
->m_pkthdr
.mp_rlen
>= n
->m_pkthdr
.mp_dsn
+ n
->m_pkthdr
.mp_rlen
) {
3350 struct mbuf
*tmp
= n
->m_nextpkt
;
3352 mptcplog((LOG_DEBUG
, "%s m is covering that guy dsn %u len %u dsn %u len %u\n",
3353 __func__
, (uint32_t)m
->m_pkthdr
.mp_dsn
, m
->m_pkthdr
.mp_rlen
,
3354 (uint32_t)n
->m_pkthdr
.mp_dsn
, n
->m_pkthdr
.mp_rlen
),
3355 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
3357 m
->m_nextpkt
= NULL
;
3359 mpte
->mpte_reinjectq
= tmp
;
3361 prev
->m_nextpkt
= tmp
;
3370 /* m is already fully covered by the previous mbuf in the queue */
3371 if (prev
->m_pkthdr
.mp_dsn
+ prev
->m_pkthdr
.mp_rlen
>= m
->m_pkthdr
.mp_dsn
+ m
->m_pkthdr
.len
) {
3372 mptcplog((LOG_DEBUG
, "%s prev covers us from %u with len %u\n",
3373 __func__
, (uint32_t)prev
->m_pkthdr
.mp_dsn
, prev
->m_pkthdr
.mp_rlen
),
3374 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
3380 mpte
->mpte_reinjectq
= m
;
3382 prev
->m_nextpkt
= m
;
3394 static struct mbuf
*
3395 mptcp_lookup_dsn(struct mptses
*mpte
, uint64_t dsn
)
3397 struct socket
*mp_so
= mptetoso(mpte
);
3400 m
= mp_so
->so_snd
.sb_mb
;
3403 /* If this segment covers what we are looking for, return it. */
3404 if (MPTCP_SEQ_LEQ(m
->m_pkthdr
.mp_dsn
, dsn
) &&
3405 MPTCP_SEQ_GT(m
->m_pkthdr
.mp_dsn
+ m
->m_pkthdr
.mp_rlen
, dsn
)) {
3410 /* Segment is no more in the queue */
3411 if (MPTCP_SEQ_GT(m
->m_pkthdr
.mp_dsn
, dsn
)) {
3421 static struct mbuf
*
3422 mptcp_copy_mbuf_list(struct mptses
*mpte
, struct mbuf
*m
, int len
)
3424 struct mbuf
*top
= NULL
, *tail
= NULL
;
3426 uint32_t dlen
, rseq
;
3428 dsn
= m
->m_pkthdr
.mp_dsn
;
3429 dlen
= m
->m_pkthdr
.mp_rlen
;
3430 rseq
= m
->m_pkthdr
.mp_rseq
;
3435 VERIFY((m
->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
));
3437 n
= m_copym_mode(m
, 0, m
->m_len
, M_DONTWAIT
, M_COPYM_MUST_COPY_HDR
);
3439 os_log_error(mptcp_log_handle
, "%s - %lx: m_copym_mode returned NULL\n",
3440 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
3444 VERIFY(n
->m_flags
& M_PKTHDR
);
3445 VERIFY(n
->m_next
== NULL
);
3446 VERIFY(n
->m_pkthdr
.mp_dsn
== dsn
);
3447 VERIFY(n
->m_pkthdr
.mp_rlen
== dlen
);
3448 VERIFY(n
->m_pkthdr
.mp_rseq
== rseq
);
3449 VERIFY(n
->m_len
== m
->m_len
);
3451 n
->m_pkthdr
.pkt_flags
|= (PKTF_MPSO
| PKTF_MPTCP
);
3478 mptcp_reinject_mbufs(struct socket
*so
)
3480 struct tcpcb
*tp
= sototcpcb(so
);
3481 struct mptsub
*mpts
= tp
->t_mpsub
;
3482 struct mptcb
*mp_tp
= tptomptp(tp
);
3483 struct mptses
*mpte
= mp_tp
->mpt_mpte
;;
3484 struct sockbuf
*sb
= &so
->so_snd
;
3489 struct mbuf
*n
= m
->m_next
, *orig
= m
;
3491 mptcplog((LOG_DEBUG
, "%s working on suna %u relseq %u iss %u len %u pktflags %#x\n",
3492 __func__
, tp
->snd_una
, m
->m_pkthdr
.mp_rseq
, mpts
->mpts_iss
,
3493 m
->m_pkthdr
.mp_rlen
, m
->m_pkthdr
.pkt_flags
),
3494 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
3496 VERIFY((m
->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
));
3498 if (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP_REINJ
) {
3502 /* Has it all already been acknowledged at the data-level? */
3503 if (MPTCP_SEQ_GEQ(mp_tp
->mpt_snduna
, m
->m_pkthdr
.mp_dsn
+ m
->m_pkthdr
.mp_rlen
)) {
3507 /* Part of this has already been acknowledged - lookup in the
3508 * MPTCP-socket for the segment.
3510 if (SEQ_GT(tp
->snd_una
- mpts
->mpts_iss
, m
->m_pkthdr
.mp_rseq
)) {
3511 m
= mptcp_lookup_dsn(mpte
, m
->m_pkthdr
.mp_dsn
);
3517 /* Copy the mbuf with headers (aka, DSN-numbers) */
3518 m
= mptcp_copy_mbuf_list(mpte
, m
, m
->m_pkthdr
.mp_rlen
);
3523 VERIFY(m
->m_nextpkt
== NULL
);
3525 /* Now, add to the reinject-queue, eliminating overlapping
3528 mptcp_add_reinjectq(mpte
, m
);
3530 orig
->m_pkthdr
.pkt_flags
|= PKTF_MPTCP_REINJ
;
3533 /* mp_rlen can cover multiple mbufs, so advance to the end of it. */
3535 VERIFY((n
->m_flags
& M_PKTHDR
) && (n
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
));
3537 if (n
->m_pkthdr
.mp_dsn
!= orig
->m_pkthdr
.mp_dsn
) {
3541 n
->m_pkthdr
.pkt_flags
|= PKTF_MPTCP_REINJ
;
3550 mptcp_clean_reinjectq(struct mptses
*mpte
)
3552 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
3554 socket_lock_assert_owned(mptetoso(mpte
));
3556 while (mpte
->mpte_reinjectq
) {
3557 struct mbuf
*m
= mpte
->mpte_reinjectq
;
3559 if (MPTCP_SEQ_GEQ(m
->m_pkthdr
.mp_dsn
, mp_tp
->mpt_snduna
) ||
3560 MPTCP_SEQ_GT(m
->m_pkthdr
.mp_dsn
+ m
->m_pkthdr
.mp_rlen
, mp_tp
->mpt_snduna
)) {
3564 mpte
->mpte_reinjectq
= m
->m_nextpkt
;
3565 m
->m_nextpkt
= NULL
;
3571 * Subflow socket control event upcall.
3574 mptcp_subflow_eupcall1(struct socket
*so
, void *arg
, long events
)
3577 struct mptsub
*mpts
= arg
;
3578 struct mptses
*mpte
= mpts
->mpts_mpte
;
3580 socket_lock_assert_owned(mptetoso(mpte
));
3582 if ((mpts
->mpts_evctl
& events
) == events
) {
3586 mpts
->mpts_evctl
|= events
;
3588 if (mptcp_should_defer_upcall(mpte
->mpte_mppcb
)) {
3589 mpte
->mpte_mppcb
->mpp_flags
|= MPP_SHOULD_WORKLOOP
;
3593 mptcp_subflow_workloop(mpte
);
3597 * Subflow socket control events.
3599 * Called for handling events related to the underlying subflow socket.
3602 mptcp_subflow_events(struct mptses
*mpte
, struct mptsub
*mpts
,
3603 long *p_mpsofilt_hint
)
3605 ev_ret_t ret
= MPTS_EVRET_OK
;
3606 int i
, mpsub_ev_entry_count
= sizeof(mpsub_ev_entry_tbl
) /
3607 sizeof(mpsub_ev_entry_tbl
[0]);
3609 /* bail if there's nothing to process */
3610 if (!mpts
->mpts_evctl
) {
3614 if (mpts
->mpts_evctl
& (SO_FILT_HINT_CONNRESET
| SO_FILT_HINT_MUSTRST
|
3615 SO_FILT_HINT_CANTSENDMORE
| SO_FILT_HINT_TIMEOUT
|
3616 SO_FILT_HINT_NOSRCADDR
| SO_FILT_HINT_IFDENIED
|
3617 SO_FILT_HINT_DISCONNECTED
)) {
3618 mpts
->mpts_evctl
|= SO_FILT_HINT_MPFAILOVER
;
3621 DTRACE_MPTCP3(subflow__events
, struct mptses
*, mpte
,
3622 struct mptsub
*, mpts
, uint32_t, mpts
->mpts_evctl
);
3625 * Process all the socket filter hints and reset the hint
3626 * once it is handled
3628 for (i
= 0; i
< mpsub_ev_entry_count
&& mpts
->mpts_evctl
; i
++) {
3630 * Always execute the DISCONNECTED event, because it will wakeup
3633 if ((mpts
->mpts_evctl
& mpsub_ev_entry_tbl
[i
].sofilt_hint_mask
) &&
3634 (ret
>= MPTS_EVRET_OK
||
3635 mpsub_ev_entry_tbl
[i
].sofilt_hint_mask
== SO_FILT_HINT_DISCONNECTED
)) {
3636 mpts
->mpts_evctl
&= ~mpsub_ev_entry_tbl
[i
].sofilt_hint_mask
;
3638 mpsub_ev_entry_tbl
[i
].sofilt_hint_ev_hdlr(mpte
, mpts
, p_mpsofilt_hint
, mpsub_ev_entry_tbl
[i
].sofilt_hint_mask
);
3639 ret
= ((error
>= MPTS_EVRET_OK
) ? MAX(error
, ret
) : error
);
3647 mptcp_subflow_propagate_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
3648 long *p_mpsofilt_hint
, long event
)
3650 struct socket
*mp_so
, *so
;
3651 struct mptcb
*mp_tp
;
3653 mp_so
= mptetoso(mpte
);
3654 mp_tp
= mpte
->mpte_mptcb
;
3655 so
= mpts
->mpts_socket
;
3658 * We got an event for this subflow that might need to be propagated,
3659 * based on the state of the MPTCP connection.
3661 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
||
3662 (!(mp_tp
->mpt_flags
& MPTCPF_JOIN_READY
) && !(mpts
->mpts_flags
& MPTSF_MP_READY
)) ||
3663 ((mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) && (mpts
->mpts_flags
& MPTSF_ACTIVE
))) {
3664 mp_so
->so_error
= so
->so_error
;
3665 *p_mpsofilt_hint
|= event
;
3668 return MPTS_EVRET_OK
;
3672 * Handle SO_FILT_HINT_NOSRCADDR subflow socket event.
3675 mptcp_subflow_nosrcaddr_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
3676 long *p_mpsofilt_hint
, long event
)
3678 #pragma unused(p_mpsofilt_hint, event)
3679 struct socket
*mp_so
;
3682 mp_so
= mptetoso(mpte
);
3683 tp
= intotcpcb(sotoinpcb(mpts
->mpts_socket
));
3686 * This overwrites any previous mpte_lost_aid to avoid storing
3687 * too much state when the typical case has only two subflows.
3689 mpte
->mpte_flags
|= MPTE_SND_REM_ADDR
;
3690 mpte
->mpte_lost_aid
= tp
->t_local_aid
;
3692 mptcplog((LOG_DEBUG
, "%s cid %d\n", __func__
, mpts
->mpts_connid
),
3693 MPTCP_EVENTS_DBG
, MPTCP_LOGLVL_LOG
);
3696 * The subflow connection has lost its source address.
3698 mptcp_subflow_abort(mpts
, EADDRNOTAVAIL
);
3700 if (mp_so
->so_flags
& SOF_NOADDRAVAIL
) {
3701 mptcp_subflow_propagate_ev(mpte
, mpts
, p_mpsofilt_hint
, event
);
3704 return MPTS_EVRET_DELETE
;
3708 mptcp_subflow_mpsuberror_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
3709 long *p_mpsofilt_hint
, long event
)
3711 #pragma unused(event, p_mpsofilt_hint)
3712 struct socket
*so
, *mp_so
;
3714 so
= mpts
->mpts_socket
;
3716 if (so
->so_error
!= ENODATA
) {
3717 return MPTS_EVRET_OK
;
3721 mp_so
= mptetoso(mpte
);
3723 mp_so
->so_error
= ENODATA
;
3728 return MPTS_EVRET_OK
;
3733 * Handle SO_FILT_HINT_MPCANTRCVMORE subflow socket event that
3734 * indicates that the remote side sent a Data FIN
3737 mptcp_subflow_mpcantrcvmore_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
3738 long *p_mpsofilt_hint
, long event
)
3740 #pragma unused(event)
3741 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
3743 mptcplog((LOG_DEBUG
, "%s: cid %d\n", __func__
, mpts
->mpts_connid
),
3744 MPTCP_EVENTS_DBG
, MPTCP_LOGLVL_LOG
);
3747 * We got a Data FIN for the MPTCP connection.
3748 * The FIN may arrive with data. The data is handed up to the
3749 * mptcp socket and the user is notified so that it may close
3750 * the socket if needed.
3752 if (mp_tp
->mpt_state
== MPTCPS_CLOSE_WAIT
) {
3753 *p_mpsofilt_hint
|= SO_FILT_HINT_CANTRCVMORE
;
3756 return MPTS_EVRET_OK
; /* keep the subflow socket around */
3760 * Handle SO_FILT_HINT_MPFAILOVER subflow socket event
3763 mptcp_subflow_failover_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
3764 long *p_mpsofilt_hint
, long event
)
3766 #pragma unused(event, p_mpsofilt_hint)
3767 struct mptsub
*mpts_alt
= NULL
;
3768 struct socket
*alt_so
= NULL
;
3769 struct socket
*mp_so
;
3770 int altpath_exists
= 0;
3772 mp_so
= mptetoso(mpte
);
3773 os_log_info(mptcp_log_handle
, "%s - %lx\n", __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
3775 mptcp_reinject_mbufs(mpts
->mpts_socket
);
3777 mpts_alt
= mptcp_get_subflow(mpte
, NULL
);
3779 /* If there is no alternate eligible subflow, ignore the failover hint. */
3780 if (mpts_alt
== NULL
|| mpts_alt
== mpts
) {
3781 os_log(mptcp_log_handle
, "%s - %lx no alternate path\n", __func__
,
3782 (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
3788 alt_so
= mpts_alt
->mpts_socket
;
3789 if (mpts_alt
->mpts_flags
& MPTSF_FAILINGOVER
) {
3790 /* All data acknowledged and no RTT spike */
3791 if (alt_so
->so_snd
.sb_cc
== 0 && mptcp_no_rto_spike(alt_so
)) {
3792 mpts_alt
->mpts_flags
&= ~MPTSF_FAILINGOVER
;
3794 /* no alternate path available */
3799 if (altpath_exists
) {
3800 mpts_alt
->mpts_flags
|= MPTSF_ACTIVE
;
3802 mpte
->mpte_active_sub
= mpts_alt
;
3803 mpts
->mpts_flags
|= MPTSF_FAILINGOVER
;
3804 mpts
->mpts_flags
&= ~MPTSF_ACTIVE
;
3806 os_log_info(mptcp_log_handle
, "%s - %lx: switched from %d to %d\n",
3807 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpts
->mpts_connid
, mpts_alt
->mpts_connid
);
3809 mptcpstats_inc_switch(mpte
, mpts
);
3813 mptcplog((LOG_DEBUG
, "%s: no alt cid = %d\n", __func__
,
3815 MPTCP_EVENTS_DBG
, MPTCP_LOGLVL_LOG
);
3817 mpts
->mpts_socket
->so_flags
&= ~SOF_MP_TRYFAILOVER
;
3820 return MPTS_EVRET_OK
;
3824 * Handle SO_FILT_HINT_IFDENIED subflow socket event.
3827 mptcp_subflow_ifdenied_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
3828 long *p_mpsofilt_hint
, long event
)
3830 mptcplog((LOG_DEBUG
, "%s: cid %d\n", __func__
,
3831 mpts
->mpts_connid
), MPTCP_EVENTS_DBG
, MPTCP_LOGLVL_LOG
);
3834 * The subflow connection cannot use the outgoing interface, let's
3835 * close this subflow.
3837 mptcp_subflow_abort(mpts
, EPERM
);
3839 mptcp_subflow_propagate_ev(mpte
, mpts
, p_mpsofilt_hint
, event
);
3841 return MPTS_EVRET_DELETE
;
3845 * https://tools.ietf.org/html/rfc6052#section-2
3846 * https://tools.ietf.org/html/rfc6147#section-5.2
3849 mptcp_desynthesize_ipv6_addr(const struct in6_addr
*addr
,
3850 const struct ipv6_prefix
*prefix
,
3851 struct in_addr
*addrv4
)
3853 char buf
[MAX_IPv4_STR_LEN
];
3854 char *ptrv4
= (char *)addrv4
;
3855 const char *ptr
= (const char *)addr
;
3857 if (memcmp(addr
, &prefix
->ipv6_prefix
, prefix
->prefix_len
) != 0) {
3861 switch (prefix
->prefix_len
) {
3862 case NAT64_PREFIX_LEN_96
:
3863 memcpy(ptrv4
, ptr
+ 12, 4);
3865 case NAT64_PREFIX_LEN_64
:
3866 memcpy(ptrv4
, ptr
+ 9, 4);
3868 case NAT64_PREFIX_LEN_56
:
3869 memcpy(ptrv4
, ptr
+ 7, 1);
3870 memcpy(ptrv4
+ 1, ptr
+ 9, 3);
3872 case NAT64_PREFIX_LEN_48
:
3873 memcpy(ptrv4
, ptr
+ 6, 2);
3874 memcpy(ptrv4
+ 2, ptr
+ 9, 2);
3876 case NAT64_PREFIX_LEN_40
:
3877 memcpy(ptrv4
, ptr
+ 5, 3);
3878 memcpy(ptrv4
+ 3, ptr
+ 9, 1);
3880 case NAT64_PREFIX_LEN_32
:
3881 memcpy(ptrv4
, ptr
+ 4, 4);
3884 panic("NAT64-prefix len is wrong: %u\n",
3885 prefix
->prefix_len
);
3888 os_log_info(mptcp_log_handle
, "%s desynthesized to %s\n", __func__
,
3889 inet_ntop(AF_INET
, (void *)addrv4
, buf
, sizeof(buf
)));
3895 mptcp_handle_ipv6_connection(struct mptses
*mpte
, const struct mptsub
*mpts
)
3897 struct ipv6_prefix nat64prefixes
[NAT64_MAX_NUM_PREFIXES
];
3898 struct socket
*so
= mpts
->mpts_socket
;
3902 /* Subflow IPs will be steered directly by the server - no need to
3905 if (mpte
->mpte_flags
& MPTE_UNICAST_IP
) {
3909 ifp
= sotoinpcb(so
)->inp_last_outifp
;
3911 if (ifnet_get_nat64prefix(ifp
, nat64prefixes
) == ENOENT
) {
3912 mptcp_ask_for_nat64(ifp
);
3917 for (j
= 0; j
< NAT64_MAX_NUM_PREFIXES
; j
++) {
3920 if (nat64prefixes
[j
].prefix_len
== 0) {
3924 success
= mptcp_desynthesize_ipv6_addr(&mpte
->__mpte_dst_v6
.sin6_addr
,
3926 &mpte
->mpte_dst_v4_nat64
.sin_addr
);
3928 mpte
->mpte_dst_v4_nat64
.sin_len
= sizeof(mpte
->mpte_dst_v4_nat64
);
3929 mpte
->mpte_dst_v4_nat64
.sin_family
= AF_INET
;
3930 mpte
->mpte_dst_v4_nat64
.sin_port
= mpte
->__mpte_dst_v6
.sin6_port
;
3937 mptcp_try_alternate_port(struct mptses
*mpte
, struct mptsub
*mpts
)
3941 if (!mptcp_ok_to_create_subflows(mpte
->mpte_mptcb
)) {
3945 inp
= sotoinpcb(mpts
->mpts_socket
);
3950 /* Should we try the alternate port? */
3951 if (mpte
->mpte_alternate_port
&&
3952 inp
->inp_fport
!= mpte
->mpte_alternate_port
) {
3953 union sockaddr_in_4_6 dst
;
3954 struct sockaddr_in
*dst_in
= (struct sockaddr_in
*)&dst
;
3956 memcpy(&dst
, &mpts
->mpts_dst
, mpts
->mpts_dst
.sa_len
);
3958 dst_in
->sin_port
= mpte
->mpte_alternate_port
;
3960 mptcp_subflow_add(mpte
, NULL
, (struct sockaddr
*)&dst
,
3961 mpts
->mpts_ifscope
, NULL
);
3962 } else { /* Else, we tried all we could, mark this interface as non-MPTCP */
3965 if (inp
->inp_last_outifp
== NULL
) {
3969 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
3970 struct mpt_itf_info
*info
= &mpte
->mpte_itfinfo
[i
];
3972 if (inp
->inp_last_outifp
->if_index
== info
->ifindex
) {
3973 info
->no_mptcp_support
= 1;
3981 * Handle SO_FILT_HINT_CONNECTED subflow socket event.
3984 mptcp_subflow_connected_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
3985 long *p_mpsofilt_hint
, long event
)
3987 #pragma unused(event, p_mpsofilt_hint)
3988 struct socket
*mp_so
, *so
;
3991 struct mptcb
*mp_tp
;
3993 boolean_t mpok
= FALSE
;
3995 mp_so
= mptetoso(mpte
);
3996 mp_tp
= mpte
->mpte_mptcb
;
3997 so
= mpts
->mpts_socket
;
3999 af
= mpts
->mpts_dst
.sa_family
;
4001 if (mpts
->mpts_flags
& MPTSF_CONNECTED
) {
4002 return MPTS_EVRET_OK
;
4005 if ((mpts
->mpts_flags
& MPTSF_DISCONNECTED
) ||
4006 (mpts
->mpts_flags
& MPTSF_DISCONNECTING
)) {
4007 if (!(so
->so_state
& (SS_ISDISCONNECTING
| SS_ISDISCONNECTED
)) &&
4008 (so
->so_state
& SS_ISCONNECTED
)) {
4009 mptcplog((LOG_DEBUG
, "%s: cid %d disconnect before tcp connect\n",
4010 __func__
, mpts
->mpts_connid
),
4011 MPTCP_EVENTS_DBG
, MPTCP_LOGLVL_LOG
);
4012 (void) soshutdownlock(so
, SHUT_RD
);
4013 (void) soshutdownlock(so
, SHUT_WR
);
4014 (void) sodisconnectlocked(so
);
4016 return MPTS_EVRET_OK
;
4020 * The subflow connection has been connected. Find out whether it
4021 * is connected as a regular TCP or as a MPTCP subflow. The idea is:
4023 * a. If MPTCP connection is not yet established, then this must be
4024 * the first subflow connection. If MPTCP failed to negotiate,
4025 * fallback to regular TCP by degrading this subflow.
4027 * b. If MPTCP connection has been established, then this must be
4028 * one of the subsequent subflow connections. If MPTCP failed
4029 * to negotiate, disconnect the connection.
4031 * Right now, we simply unblock any waiters at the MPTCP socket layer
4032 * if the MPTCP connection has not been established.
4035 if (so
->so_state
& SS_ISDISCONNECTED
) {
4037 * With MPTCP joins, a connection is connected at the subflow
4038 * level, but the 4th ACK from the server elevates the MPTCP
4039 * subflow to connected state. So there is a small window
4040 * where the subflow could get disconnected before the
4041 * connected event is processed.
4043 return MPTS_EVRET_OK
;
4046 if (mpts
->mpts_flags
& MPTSF_TFO_REQD
) {
4047 mptcp_drop_tfo_data(mpte
, mpts
);
4050 mpts
->mpts_flags
&= ~(MPTSF_CONNECTING
| MPTSF_TFO_REQD
);
4051 mpts
->mpts_flags
|= MPTSF_CONNECTED
;
4053 if (tp
->t_mpflags
& TMPF_MPTCP_TRUE
) {
4054 mpts
->mpts_flags
|= MPTSF_MP_CAPABLE
;
4057 tp
->t_mpflags
&= ~TMPF_TFO_REQUEST
;
4059 /* get/verify the outbound interface */
4060 inp
= sotoinpcb(so
);
4062 mpts
->mpts_maxseg
= tp
->t_maxseg
;
4064 mptcplog((LOG_DEBUG
, "%s: cid %d outif %s is %s\n", __func__
, mpts
->mpts_connid
,
4065 ((inp
->inp_last_outifp
!= NULL
) ? inp
->inp_last_outifp
->if_xname
: "NULL"),
4066 ((mpts
->mpts_flags
& MPTSF_MP_CAPABLE
) ? "MPTCP capable" : "a regular TCP")),
4067 (MPTCP_SOCKET_DBG
| MPTCP_EVENTS_DBG
), MPTCP_LOGLVL_LOG
);
4069 mpok
= (mpts
->mpts_flags
& MPTSF_MP_CAPABLE
);
4071 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
) {
4072 mp_tp
->mpt_state
= MPTCPS_ESTABLISHED
;
4073 mpte
->mpte_associd
= mpts
->mpts_connid
;
4074 DTRACE_MPTCP2(state__change
,
4075 struct mptcb
*, mp_tp
,
4076 uint32_t, 0 /* event */);
4078 if (SOCK_DOM(so
) == AF_INET
) {
4079 in_getsockaddr_s(so
, &mpte
->__mpte_src_v4
);
4081 in6_getsockaddr_s(so
, &mpte
->__mpte_src_v6
);
4084 mpts
->mpts_flags
|= MPTSF_ACTIVE
;
4086 /* case (a) above */
4088 tcpstat
.tcps_mpcap_fallback
++;
4090 tp
->t_mpflags
|= TMPF_INFIN_SENT
;
4091 mptcp_notify_mpfail(so
);
4093 if (IFNET_IS_CELLULAR(inp
->inp_last_outifp
) &&
4094 mpte
->mpte_svctype
< MPTCP_SVCTYPE_AGGREGATE
) {
4095 tp
->t_mpflags
|= (TMPF_BACKUP_PATH
| TMPF_SND_MPPRIO
);
4097 mpts
->mpts_flags
|= MPTSF_PREFERRED
;
4099 mpts
->mpts_flags
|= MPTSF_MPCAP_CTRSET
;
4100 mpte
->mpte_nummpcapflows
++;
4102 if (SOCK_DOM(so
) == AF_INET6
) {
4103 mptcp_handle_ipv6_connection(mpte
, mpts
);
4106 mptcp_check_subflows_and_add(mpte
);
4108 if (IFNET_IS_CELLULAR(inp
->inp_last_outifp
)) {
4109 mpte
->mpte_initial_cell
= 1;
4112 mpte
->mpte_handshake_success
= 1;
4115 mp_tp
->mpt_sndwnd
= tp
->snd_wnd
;
4116 mp_tp
->mpt_sndwl1
= mp_tp
->mpt_rcvnxt
;
4117 mp_tp
->mpt_sndwl2
= mp_tp
->mpt_snduna
;
4118 soisconnected(mp_so
);
4122 * In case of additional flows, the MPTCP socket is not
4123 * MPTSF_MP_CAPABLE until an ACK is received from server
4124 * for 3-way handshake. TCP would have guaranteed that this
4125 * is an MPTCP subflow.
4127 if (IFNET_IS_CELLULAR(inp
->inp_last_outifp
) &&
4128 !(tp
->t_mpflags
& TMPF_BACKUP_PATH
) &&
4129 mpte
->mpte_svctype
< MPTCP_SVCTYPE_AGGREGATE
) {
4130 tp
->t_mpflags
|= (TMPF_BACKUP_PATH
| TMPF_SND_MPPRIO
);
4131 mpts
->mpts_flags
&= ~MPTSF_PREFERRED
;
4133 mpts
->mpts_flags
|= MPTSF_PREFERRED
;
4136 mpts
->mpts_flags
|= MPTSF_MPCAP_CTRSET
;
4137 mpte
->mpte_nummpcapflows
++;
4139 mpts
->mpts_rel_seq
= 1;
4141 mptcp_check_subflows_and_remove(mpte
);
4143 mptcp_try_alternate_port(mpte
, mpts
);
4145 tcpstat
.tcps_join_fallback
++;
4146 if (IFNET_IS_CELLULAR(inp
->inp_last_outifp
)) {
4147 tcpstat
.tcps_mptcp_cell_proxy
++;
4149 tcpstat
.tcps_mptcp_wifi_proxy
++;
4152 soevent(mpts
->mpts_socket
, SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MUSTRST
);
4154 return MPTS_EVRET_OK
;
4157 /* This call, just to "book" an entry in the stats-table for this ifindex */
4158 mptcpstats_get_index(mpte
->mpte_itfstats
, mpts
);
4162 return MPTS_EVRET_OK
; /* keep the subflow socket around */
4166 * Handle SO_FILT_HINT_DISCONNECTED subflow socket event.
4169 mptcp_subflow_disconnected_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
4170 long *p_mpsofilt_hint
, long event
)
4172 #pragma unused(event, p_mpsofilt_hint)
4173 struct socket
*mp_so
, *so
;
4174 struct mptcb
*mp_tp
;
4176 mp_so
= mptetoso(mpte
);
4177 mp_tp
= mpte
->mpte_mptcb
;
4178 so
= mpts
->mpts_socket
;
4180 mptcplog((LOG_DEBUG
, "%s: cid %d, so_err %d, mpt_state %u fallback %u active %u flags %#x\n",
4181 __func__
, mpts
->mpts_connid
, so
->so_error
, mp_tp
->mpt_state
,
4182 !!(mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
),
4183 !!(mpts
->mpts_flags
& MPTSF_ACTIVE
), sototcpcb(so
)->t_mpflags
),
4184 MPTCP_EVENTS_DBG
, MPTCP_LOGLVL_LOG
);
4186 if (mpts
->mpts_flags
& MPTSF_DISCONNECTED
) {
4187 return MPTS_EVRET_DELETE
;
4190 mpts
->mpts_flags
|= MPTSF_DISCONNECTED
;
4192 /* The subflow connection has been disconnected. */
4194 if (mpts
->mpts_flags
& MPTSF_MPCAP_CTRSET
) {
4195 mpte
->mpte_nummpcapflows
--;
4196 if (mpte
->mpte_active_sub
== mpts
) {
4197 mpte
->mpte_active_sub
= NULL
;
4198 mptcplog((LOG_DEBUG
, "%s: resetting active subflow \n",
4199 __func__
), MPTCP_EVENTS_DBG
, MPTCP_LOGLVL_LOG
);
4201 mpts
->mpts_flags
&= ~MPTSF_MPCAP_CTRSET
;
4203 if (so
->so_flags
& SOF_MP_SEC_SUBFLOW
&&
4204 !(mpts
->mpts_flags
& MPTSF_CONNECTED
)) {
4205 mptcp_try_alternate_port(mpte
, mpts
);
4209 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
||
4210 ((mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) && (mpts
->mpts_flags
& MPTSF_ACTIVE
))) {
4211 mptcp_drop(mpte
, mp_tp
, so
->so_error
);
4215 * Clear flags that are used by getconninfo to return state.
4216 * Retain like MPTSF_DELETEOK for internal purposes.
4218 mpts
->mpts_flags
&= ~(MPTSF_CONNECTING
| MPTSF_CONNECT_PENDING
|
4219 MPTSF_CONNECTED
| MPTSF_DISCONNECTING
| MPTSF_PREFERRED
|
4220 MPTSF_MP_CAPABLE
| MPTSF_MP_READY
| MPTSF_MP_DEGRADED
| MPTSF_ACTIVE
);
4222 return MPTS_EVRET_DELETE
;
4226 * Handle SO_FILT_HINT_MPSTATUS subflow socket event
4229 mptcp_subflow_mpstatus_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
4230 long *p_mpsofilt_hint
, long event
)
4232 #pragma unused(event, p_mpsofilt_hint)
4233 ev_ret_t ret
= MPTS_EVRET_OK
;
4234 struct socket
*mp_so
, *so
;
4235 struct mptcb
*mp_tp
;
4237 mp_so
= mptetoso(mpte
);
4238 mp_tp
= mpte
->mpte_mptcb
;
4239 so
= mpts
->mpts_socket
;
4241 if (sototcpcb(so
)->t_mpflags
& TMPF_MPTCP_TRUE
) {
4242 mpts
->mpts_flags
|= MPTSF_MP_CAPABLE
;
4244 mpts
->mpts_flags
&= ~MPTSF_MP_CAPABLE
;
4247 if (sototcpcb(so
)->t_mpflags
& TMPF_TCP_FALLBACK
) {
4248 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) {
4251 mpts
->mpts_flags
|= MPTSF_MP_DEGRADED
;
4253 mpts
->mpts_flags
&= ~MPTSF_MP_DEGRADED
;
4256 if (sototcpcb(so
)->t_mpflags
& TMPF_MPTCP_READY
) {
4257 mpts
->mpts_flags
|= MPTSF_MP_READY
;
4259 mpts
->mpts_flags
&= ~MPTSF_MP_READY
;
4262 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) {
4263 mp_tp
->mpt_flags
|= MPTCPF_FALLBACK_TO_TCP
;
4264 mp_tp
->mpt_flags
&= ~MPTCPF_JOIN_READY
;
4267 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
4268 ret
= MPTS_EVRET_DISCONNECT_FALLBACK
;
4270 m_freem_list(mpte
->mpte_reinjectq
);
4271 mpte
->mpte_reinjectq
= NULL
;
4272 } else if (mpts
->mpts_flags
& MPTSF_MP_READY
) {
4273 mp_tp
->mpt_flags
|= MPTCPF_JOIN_READY
;
4274 ret
= MPTS_EVRET_CONNECT_PENDING
;
4282 * Handle SO_FILT_HINT_MUSTRST subflow socket event
4285 mptcp_subflow_mustrst_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
4286 long *p_mpsofilt_hint
, long event
)
4288 #pragma unused(event)
4289 struct socket
*mp_so
, *so
;
4290 struct mptcb
*mp_tp
;
4291 boolean_t is_fastclose
;
4293 mp_so
= mptetoso(mpte
);
4294 mp_tp
= mpte
->mpte_mptcb
;
4295 so
= mpts
->mpts_socket
;
4297 /* We got an invalid option or a fast close */
4298 struct inpcb
*inp
= sotoinpcb(so
);
4299 struct tcpcb
*tp
= NULL
;
4301 tp
= intotcpcb(inp
);
4302 so
->so_error
= ECONNABORTED
;
4304 is_fastclose
= !!(tp
->t_mpflags
& TMPF_FASTCLOSERCV
);
4306 tp
->t_mpflags
|= TMPF_RESET
;
4308 if (tp
->t_state
!= TCPS_CLOSED
) {
4309 struct tcptemp
*t_template
= tcp_maketemplate(tp
);
4312 struct tcp_respond_args tra
;
4314 bzero(&tra
, sizeof(tra
));
4315 if (inp
->inp_flags
& INP_BOUND_IF
) {
4316 tra
.ifscope
= inp
->inp_boundifp
->if_index
;
4318 tra
.ifscope
= IFSCOPE_NONE
;
4320 tra
.awdl_unrestricted
= 1;
4322 tcp_respond(tp
, t_template
->tt_ipgen
,
4323 &t_template
->tt_t
, (struct mbuf
*)NULL
,
4324 tp
->rcv_nxt
, tp
->snd_una
, TH_RST
, &tra
);
4325 (void) m_free(dtom(t_template
));
4329 if (!(mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) && is_fastclose
) {
4330 struct mptsub
*iter
, *tmp
;
4332 *p_mpsofilt_hint
|= SO_FILT_HINT_CONNRESET
;
4334 mp_so
->so_error
= ECONNRESET
;
4336 TAILQ_FOREACH_SAFE(iter
, &mpte
->mpte_subflows
, mpts_entry
, tmp
) {
4340 mptcp_subflow_abort(iter
, ECONNABORTED
);
4344 * mptcp_drop is being called after processing the events, to fully
4345 * close the MPTCP connection
4347 mptcp_drop(mpte
, mp_tp
, mp_so
->so_error
);
4350 mptcp_subflow_abort(mpts
, ECONNABORTED
);
4352 if (mp_tp
->mpt_gc_ticks
== MPT_GC_TICKS
) {
4353 mp_tp
->mpt_gc_ticks
= MPT_GC_TICKS_FAST
;
4356 return MPTS_EVRET_DELETE
;
4360 mptcp_subflow_adaptive_rtimo_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
4361 long *p_mpsofilt_hint
, long event
)
4363 #pragma unused(event)
4364 bool found_active
= false;
4366 mpts
->mpts_flags
|= MPTSF_READ_STALL
;
4368 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
4369 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
4371 if (!TCPS_HAVEESTABLISHED(tp
->t_state
) ||
4372 TCPS_HAVERCVDFIN2(tp
->t_state
)) {
4376 if (!(mpts
->mpts_flags
& MPTSF_READ_STALL
)) {
4377 found_active
= true;
4382 if (!found_active
) {
4383 *p_mpsofilt_hint
|= SO_FILT_HINT_ADAPTIVE_RTIMO
;
4386 return MPTS_EVRET_OK
;
4390 mptcp_subflow_adaptive_wtimo_ev(struct mptses
*mpte
, struct mptsub
*mpts
,
4391 long *p_mpsofilt_hint
, long event
)
4393 #pragma unused(event)
4394 bool found_active
= false;
4396 mpts
->mpts_flags
|= MPTSF_WRITE_STALL
;
4398 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
4399 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
4401 if (!TCPS_HAVEESTABLISHED(tp
->t_state
) ||
4402 tp
->t_state
> TCPS_CLOSE_WAIT
) {
4406 if (!(mpts
->mpts_flags
& MPTSF_WRITE_STALL
)) {
4407 found_active
= true;
4412 if (!found_active
) {
4413 *p_mpsofilt_hint
|= SO_FILT_HINT_ADAPTIVE_WTIMO
;
4416 return MPTS_EVRET_OK
;
4420 * Issues SOPT_SET on an MPTCP subflow socket; socket must already be locked,
4421 * caller must ensure that the option can be issued on subflow sockets, via
4422 * MPOF_SUBFLOW_OK flag.
4425 mptcp_subflow_sosetopt(struct mptses
*mpte
, struct mptsub
*mpts
, struct mptopt
*mpo
)
4427 struct socket
*mp_so
, *so
;
4428 struct sockopt sopt
;
4431 VERIFY(mpo
->mpo_flags
& MPOF_SUBFLOW_OK
);
4433 mp_so
= mptetoso(mpte
);
4434 so
= mpts
->mpts_socket
;
4436 socket_lock_assert_owned(mp_so
);
4438 if (mpte
->mpte_mptcb
->mpt_state
>= MPTCPS_ESTABLISHED
&&
4439 mpo
->mpo_level
== SOL_SOCKET
&&
4440 mpo
->mpo_name
== SO_MARK_CELLFALLBACK
) {
4441 struct ifnet
*ifp
= ifindex2ifnet
[mpts
->mpts_ifscope
];
4443 mptcplog((LOG_DEBUG
, "%s Setting CELL_FALLBACK, mpte_flags %#x, svctype %u wifi unusable %d lastcell? %d boundcell? %d\n",
4444 __func__
, mpte
->mpte_flags
, mpte
->mpte_svctype
, mptcp_is_wifi_unusable_for_session(mpte
),
4445 sotoinpcb(so
)->inp_last_outifp
? IFNET_IS_CELLULAR(sotoinpcb(so
)->inp_last_outifp
) : -1,
4446 mpts
->mpts_ifscope
!= IFSCOPE_NONE
&& ifp
? IFNET_IS_CELLULAR(ifp
) : -1),
4447 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
4450 * When we open a new subflow, mark it as cell fallback, if
4451 * this subflow goes over cell.
4453 * (except for first-party apps)
4456 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
4460 if (sotoinpcb(so
)->inp_last_outifp
&&
4461 !IFNET_IS_CELLULAR(sotoinpcb(so
)->inp_last_outifp
)) {
4466 * This here is an OR, because if the app is not binding to the
4467 * interface, then it definitely is not a cell-fallback
4470 if (mpts
->mpts_ifscope
== IFSCOPE_NONE
|| ifp
== NULL
||
4471 !IFNET_IS_CELLULAR(ifp
)) {
4476 mpo
->mpo_flags
&= ~MPOF_INTERIM
;
4478 bzero(&sopt
, sizeof(sopt
));
4479 sopt
.sopt_dir
= SOPT_SET
;
4480 sopt
.sopt_level
= mpo
->mpo_level
;
4481 sopt
.sopt_name
= mpo
->mpo_name
;
4482 sopt
.sopt_val
= CAST_USER_ADDR_T(&mpo
->mpo_intval
);
4483 sopt
.sopt_valsize
= sizeof(int);
4484 sopt
.sopt_p
= kernproc
;
4486 error
= sosetoptlock(so
, &sopt
, 0);
4488 os_log_error(mptcp_log_handle
, "%s - %lx: sopt %s "
4489 "val %d set error %d\n", __func__
,
4490 (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
4491 mptcp_sopt2str(mpo
->mpo_level
, mpo
->mpo_name
),
4492 mpo
->mpo_intval
, error
);
4498 * Issues SOPT_GET on an MPTCP subflow socket; socket must already be locked,
4499 * caller must ensure that the option can be issued on subflow sockets, via
4500 * MPOF_SUBFLOW_OK flag.
4503 mptcp_subflow_sogetopt(struct mptses
*mpte
, struct socket
*so
,
4506 struct socket
*mp_so
;
4507 struct sockopt sopt
;
4510 VERIFY(mpo
->mpo_flags
& MPOF_SUBFLOW_OK
);
4511 mp_so
= mptetoso(mpte
);
4513 socket_lock_assert_owned(mp_so
);
4515 bzero(&sopt
, sizeof(sopt
));
4516 sopt
.sopt_dir
= SOPT_GET
;
4517 sopt
.sopt_level
= mpo
->mpo_level
;
4518 sopt
.sopt_name
= mpo
->mpo_name
;
4519 sopt
.sopt_val
= CAST_USER_ADDR_T(&mpo
->mpo_intval
);
4520 sopt
.sopt_valsize
= sizeof(int);
4521 sopt
.sopt_p
= kernproc
;
4523 error
= sogetoptlock(so
, &sopt
, 0); /* already locked */
4525 os_log_error(mptcp_log_handle
,
4526 "%s - %lx: sopt %s get error %d\n",
4527 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
4528 mptcp_sopt2str(mpo
->mpo_level
, mpo
->mpo_name
), error
);
4535 * MPTCP garbage collector.
4537 * This routine is called by the MP domain on-demand, periodic callout,
4538 * which is triggered when a MPTCP socket is closed. The callout will
4539 * repeat as long as this routine returns a non-zero value.
4542 mptcp_gc(struct mppcbinfo
*mppi
)
4544 struct mppcb
*mpp
, *tmpp
;
4545 uint32_t active
= 0;
4547 LCK_MTX_ASSERT(&mppi
->mppi_lock
, LCK_MTX_ASSERT_OWNED
);
4549 TAILQ_FOREACH_SAFE(mpp
, &mppi
->mppi_pcbs
, mpp_entry
, tmpp
) {
4550 struct socket
*mp_so
;
4551 struct mptses
*mpte
;
4552 struct mptcb
*mp_tp
;
4554 mp_so
= mpp
->mpp_socket
;
4555 mpte
= mptompte(mpp
);
4556 mp_tp
= mpte
->mpte_mptcb
;
4558 if (!mpp_try_lock(mpp
)) {
4563 VERIFY(mpp
->mpp_flags
& MPP_ATTACHED
);
4565 /* check again under the lock */
4566 if (mp_so
->so_usecount
> 0) {
4567 boolean_t wakeup
= FALSE
;
4568 struct mptsub
*mpts
, *tmpts
;
4570 if (mp_tp
->mpt_state
>= MPTCPS_FIN_WAIT_1
) {
4571 if (mp_tp
->mpt_gc_ticks
> 0) {
4572 mp_tp
->mpt_gc_ticks
--;
4574 if (mp_tp
->mpt_gc_ticks
== 0) {
4579 TAILQ_FOREACH_SAFE(mpts
,
4580 &mpte
->mpte_subflows
, mpts_entry
, tmpts
) {
4581 mptcp_subflow_eupcall1(mpts
->mpts_socket
,
4582 mpts
, SO_FILT_HINT_DISCONNECTED
);
4585 socket_unlock(mp_so
, 0);
4590 if (mpp
->mpp_state
!= MPPCB_STATE_DEAD
) {
4591 panic("%s - %lx: skipped state "
4592 "[u=%d,r=%d,s=%d]\n", __func__
,
4593 (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
4594 mp_so
->so_usecount
, mp_so
->so_retaincnt
,
4598 if (mp_tp
->mpt_state
== MPTCPS_TIME_WAIT
) {
4599 mptcp_close(mpte
, mp_tp
);
4602 mptcp_session_destroy(mpte
);
4604 DTRACE_MPTCP4(dispose
, struct socket
*, mp_so
,
4605 struct sockbuf
*, &mp_so
->so_rcv
,
4606 struct sockbuf
*, &mp_so
->so_snd
,
4607 struct mppcb
*, mpp
);
4617 * Drop a MPTCP connection, reporting the specified error.
4620 mptcp_drop(struct mptses
*mpte
, struct mptcb
*mp_tp
, u_short errno
)
4622 struct socket
*mp_so
= mptetoso(mpte
);
4624 VERIFY(mpte
->mpte_mptcb
== mp_tp
);
4626 socket_lock_assert_owned(mp_so
);
4628 DTRACE_MPTCP2(state__change
, struct mptcb
*, mp_tp
,
4629 uint32_t, 0 /* event */);
4631 if (errno
== ETIMEDOUT
&& mp_tp
->mpt_softerror
!= 0) {
4632 errno
= mp_tp
->mpt_softerror
;
4634 mp_so
->so_error
= errno
;
4636 return mptcp_close(mpte
, mp_tp
);
4640 * Close a MPTCP control block.
4643 mptcp_close(struct mptses
*mpte
, struct mptcb
*mp_tp
)
4645 struct mptsub
*mpts
= NULL
, *tmpts
= NULL
;
4646 struct socket
*mp_so
= mptetoso(mpte
);
4648 socket_lock_assert_owned(mp_so
);
4649 VERIFY(mpte
->mpte_mptcb
== mp_tp
);
4651 mp_tp
->mpt_state
= MPTCPS_TERMINATE
;
4655 soisdisconnected(mp_so
);
4657 /* Clean up all subflows */
4658 TAILQ_FOREACH_SAFE(mpts
, &mpte
->mpte_subflows
, mpts_entry
, tmpts
) {
4659 mptcp_subflow_disconnect(mpte
, mpts
);
4666 mptcp_notify_close(struct socket
*so
)
4668 soevent(so
, (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_DISCONNECTED
));
4675 mptcp_subflow_workloop(struct mptses
*mpte
)
4677 boolean_t connect_pending
= FALSE
, disconnect_fallback
= FALSE
;
4678 long mpsofilt_hint_mask
= SO_FILT_HINT_LOCKED
;
4679 struct mptsub
*mpts
, *tmpts
;
4680 struct socket
*mp_so
;
4682 mp_so
= mptetoso(mpte
);
4684 socket_lock_assert_owned(mp_so
);
4686 if (mpte
->mpte_flags
& MPTE_IN_WORKLOOP
) {
4687 mpte
->mpte_flags
|= MPTE_WORKLOOP_RELAUNCH
;
4690 mpte
->mpte_flags
|= MPTE_IN_WORKLOOP
;
4693 mpte
->mpte_flags
&= ~MPTE_WORKLOOP_RELAUNCH
;
4695 TAILQ_FOREACH_SAFE(mpts
, &mpte
->mpte_subflows
, mpts_entry
, tmpts
) {
4698 if (mpts
->mpts_socket
->so_usecount
== 0) {
4699 /* Will be removed soon by tcp_garbage_collect */
4703 mptcp_subflow_addref(mpts
);
4704 mpts
->mpts_socket
->so_usecount
++;
4706 ret
= mptcp_subflow_events(mpte
, mpts
, &mpsofilt_hint_mask
);
4709 * If MPTCP socket is closed, disconnect all subflows.
4710 * This will generate a disconnect event which will
4711 * be handled during the next iteration, causing a
4712 * non-zero error to be returned above.
4714 if (mp_so
->so_flags
& SOF_PCBCLEARING
) {
4715 mptcp_subflow_disconnect(mpte
, mpts
);
4722 case MPTS_EVRET_DELETE
:
4723 mptcp_subflow_soclose(mpts
);
4725 case MPTS_EVRET_CONNECT_PENDING
:
4726 connect_pending
= TRUE
;
4728 case MPTS_EVRET_DISCONNECT_FALLBACK
:
4729 disconnect_fallback
= TRUE
;
4732 mptcplog((LOG_DEBUG
,
4733 "MPTCP Socket: %s: mptcp_subflow_events "
4734 "returned invalid value: %d\n", __func__
,
4736 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
4739 mptcp_subflow_remref(mpts
); /* ours */
4741 VERIFY(mpts
->mpts_socket
->so_usecount
!= 0);
4742 mpts
->mpts_socket
->so_usecount
--;
4745 if (mpsofilt_hint_mask
!= SO_FILT_HINT_LOCKED
) {
4746 VERIFY(mpsofilt_hint_mask
& SO_FILT_HINT_LOCKED
);
4748 if (mpsofilt_hint_mask
& SO_FILT_HINT_CANTRCVMORE
) {
4749 mp_so
->so_state
|= SS_CANTRCVMORE
;
4753 soevent(mp_so
, mpsofilt_hint_mask
);
4756 if (!connect_pending
&& !disconnect_fallback
) {
4760 TAILQ_FOREACH_SAFE(mpts
, &mpte
->mpte_subflows
, mpts_entry
, tmpts
) {
4761 if (disconnect_fallback
) {
4762 struct socket
*so
= NULL
;
4763 struct inpcb
*inp
= NULL
;
4764 struct tcpcb
*tp
= NULL
;
4766 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) {
4770 mpts
->mpts_flags
|= MPTSF_MP_DEGRADED
;
4772 if (mpts
->mpts_flags
& (MPTSF_DISCONNECTING
|
4773 MPTSF_DISCONNECTED
)) {
4777 so
= mpts
->mpts_socket
;
4780 * The MPTCP connection has degraded to a fallback
4781 * mode, so there is no point in keeping this subflow
4782 * regardless of its MPTCP-readiness state, unless it
4783 * is the primary one which we use for fallback. This
4784 * assumes that the subflow used for fallback is the
4788 inp
= sotoinpcb(so
);
4789 tp
= intotcpcb(inp
);
4791 ~(TMPF_MPTCP_READY
| TMPF_MPTCP_TRUE
);
4792 tp
->t_mpflags
|= TMPF_TCP_FALLBACK
;
4794 soevent(so
, SO_FILT_HINT_MUSTRST
);
4795 } else if (connect_pending
) {
4797 * The MPTCP connection has progressed to a state
4798 * where it supports full multipath semantics; allow
4799 * additional joins to be attempted for all subflows
4800 * that are in the PENDING state.
4802 if (mpts
->mpts_flags
& MPTSF_CONNECT_PENDING
) {
4803 int error
= mptcp_subflow_soconnectx(mpte
, mpts
);
4806 mptcp_subflow_abort(mpts
, error
);
4813 if (mpte
->mpte_flags
& MPTE_WORKLOOP_RELAUNCH
) {
4817 mpte
->mpte_flags
&= ~MPTE_IN_WORKLOOP
;
4821 * Protocol pr_lock callback.
4824 mptcp_lock(struct socket
*mp_so
, int refcount
, void *lr
)
4826 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
4830 lr_saved
= __builtin_return_address(0);
4836 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__
,
4837 mp_so
, lr_saved
, solockhistory_nr(mp_so
));
4842 if (mp_so
->so_usecount
< 0) {
4843 panic("%s: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n", __func__
,
4844 mp_so
, mp_so
->so_pcb
, lr_saved
, mp_so
->so_usecount
,
4845 solockhistory_nr(mp_so
));
4848 if (refcount
!= 0) {
4849 mp_so
->so_usecount
++;
4852 mp_so
->lock_lr
[mp_so
->next_lock_lr
] = lr_saved
;
4853 mp_so
->next_lock_lr
= (mp_so
->next_lock_lr
+ 1) % SO_LCKDBG_MAX
;
4859 * Protocol pr_unlock callback.
4862 mptcp_unlock(struct socket
*mp_so
, int refcount
, void *lr
)
4864 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
4868 lr_saved
= __builtin_return_address(0);
4874 panic("%s: so=%p NO PCB usecount=%x lr=%p lrh= %s\n", __func__
,
4875 mp_so
, mp_so
->so_usecount
, lr_saved
,
4876 solockhistory_nr(mp_so
));
4879 socket_lock_assert_owned(mp_so
);
4881 if (refcount
!= 0) {
4882 mp_so
->so_usecount
--;
4886 if (mp_so
->so_usecount
< 0) {
4887 panic("%s: so=%p usecount=%x lrh= %s\n", __func__
,
4888 mp_so
, mp_so
->so_usecount
, solockhistory_nr(mp_so
));
4891 if (mpp
->mpp_inside
< 0) {
4892 panic("%s: mpp=%p inside=%x lrh= %s\n", __func__
,
4893 mpp
, mpp
->mpp_inside
, solockhistory_nr(mp_so
));
4896 mp_so
->unlock_lr
[mp_so
->next_unlock_lr
] = lr_saved
;
4897 mp_so
->next_unlock_lr
= (mp_so
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
4904 * Protocol pr_getlock callback.
4907 mptcp_getlock(struct socket
*mp_so
, int flags
)
4909 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
4912 panic("%s: so=%p NULL so_pcb %s\n", __func__
, mp_so
,
4913 solockhistory_nr(mp_so
));
4916 if (mp_so
->so_usecount
< 0) {
4917 panic("%s: so=%p usecount=%x lrh= %s\n", __func__
,
4918 mp_so
, mp_so
->so_usecount
, solockhistory_nr(mp_so
));
4921 return mpp_getlock(mpp
, flags
);
4925 * MPTCP Join support
4929 mptcp_attach_to_subf(struct socket
*so
, struct mptcb
*mp_tp
, uint8_t addr_id
)
4931 struct tcpcb
*tp
= sototcpcb(so
);
4932 struct mptcp_subf_auth_entry
*sauth_entry
;
4935 * The address ID of the first flow is implicitly 0.
4937 if (mp_tp
->mpt_state
== MPTCPS_CLOSED
) {
4938 tp
->t_local_aid
= 0;
4940 tp
->t_local_aid
= addr_id
;
4941 tp
->t_mpflags
|= (TMPF_PREESTABLISHED
| TMPF_JOINED_FLOW
);
4942 so
->so_flags
|= SOF_MP_SEC_SUBFLOW
;
4944 sauth_entry
= zalloc(mpt_subauth_zone
);
4945 sauth_entry
->msae_laddr_id
= tp
->t_local_aid
;
4946 sauth_entry
->msae_raddr_id
= 0;
4947 sauth_entry
->msae_raddr_rand
= 0;
4949 sauth_entry
->msae_laddr_rand
= RandomULong();
4950 if (sauth_entry
->msae_laddr_rand
== 0) {
4953 LIST_INSERT_HEAD(&mp_tp
->mpt_subauth_list
, sauth_entry
, msae_next
);
4957 mptcp_detach_mptcb_from_subf(struct mptcb
*mp_tp
, struct socket
*so
)
4959 struct mptcp_subf_auth_entry
*sauth_entry
;
4960 struct tcpcb
*tp
= NULL
;
4968 LIST_FOREACH(sauth_entry
, &mp_tp
->mpt_subauth_list
, msae_next
) {
4969 if (sauth_entry
->msae_laddr_id
== tp
->t_local_aid
) {
4975 LIST_REMOVE(sauth_entry
, msae_next
);
4979 zfree(mpt_subauth_zone
, sauth_entry
);
4984 mptcp_get_rands(mptcp_addr_id addr_id
, struct mptcb
*mp_tp
, u_int32_t
*lrand
,
4987 struct mptcp_subf_auth_entry
*sauth_entry
;
4989 LIST_FOREACH(sauth_entry
, &mp_tp
->mpt_subauth_list
, msae_next
) {
4990 if (sauth_entry
->msae_laddr_id
== addr_id
) {
4992 *lrand
= sauth_entry
->msae_laddr_rand
;
4995 *rrand
= sauth_entry
->msae_raddr_rand
;
5003 mptcp_set_raddr_rand(mptcp_addr_id laddr_id
, struct mptcb
*mp_tp
,
5004 mptcp_addr_id raddr_id
, u_int32_t raddr_rand
)
5006 struct mptcp_subf_auth_entry
*sauth_entry
;
5008 LIST_FOREACH(sauth_entry
, &mp_tp
->mpt_subauth_list
, msae_next
) {
5009 if (sauth_entry
->msae_laddr_id
== laddr_id
) {
5010 if ((sauth_entry
->msae_raddr_id
!= 0) &&
5011 (sauth_entry
->msae_raddr_id
!= raddr_id
)) {
5012 os_log_error(mptcp_log_handle
, "%s - %lx: mismatched"
5013 " address ids %d %d \n", __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mp_tp
->mpt_mpte
),
5014 raddr_id
, sauth_entry
->msae_raddr_id
);
5017 sauth_entry
->msae_raddr_id
= raddr_id
;
5018 if ((sauth_entry
->msae_raddr_rand
!= 0) &&
5019 (sauth_entry
->msae_raddr_rand
!= raddr_rand
)) {
5020 os_log_error(mptcp_log_handle
, "%s - %lx: "
5021 "dup SYN_ACK %d %d \n",
5022 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mp_tp
->mpt_mpte
),
5023 raddr_rand
, sauth_entry
->msae_raddr_rand
);
5026 sauth_entry
->msae_raddr_rand
= raddr_rand
;
5033 * SHA1 support for MPTCP
5036 mptcp_do_sha1(mptcp_key_t
*key
, char *sha_digest
)
5039 const unsigned char *sha1_base
;
5042 sha1_base
= (const unsigned char *) key
;
5043 sha1_size
= sizeof(mptcp_key_t
);
5044 SHA1Init(&sha1ctxt
);
5045 SHA1Update(&sha1ctxt
, sha1_base
, sha1_size
);
5046 SHA1Final(sha_digest
, &sha1ctxt
);
5050 mptcp_hmac_sha1(mptcp_key_t key1
, mptcp_key_t key2
,
5051 u_int32_t rand1
, u_int32_t rand2
, u_char
*digest
)
5054 mptcp_key_t key_ipad
[8] = {0}; /* key XOR'd with inner pad */
5055 mptcp_key_t key_opad
[8] = {0}; /* key XOR'd with outer pad */
5059 bzero(digest
, SHA1_RESULTLEN
);
5061 /* Set up the Key for HMAC */
5068 /* Set up the message for HMAC */
5072 /* Key is 512 block length, so no need to compute hash */
5074 /* Compute SHA1(Key XOR opad, SHA1(Key XOR ipad, data)) */
5076 for (i
= 0; i
< 8; i
++) {
5077 key_ipad
[i
] ^= 0x3636363636363636;
5078 key_opad
[i
] ^= 0x5c5c5c5c5c5c5c5c;
5081 /* Perform inner SHA1 */
5082 SHA1Init(&sha1ctxt
);
5083 SHA1Update(&sha1ctxt
, (unsigned char *)key_ipad
, sizeof(key_ipad
));
5084 SHA1Update(&sha1ctxt
, (unsigned char *)data
, sizeof(data
));
5085 SHA1Final(digest
, &sha1ctxt
);
5087 /* Perform outer SHA1 */
5088 SHA1Init(&sha1ctxt
);
5089 SHA1Update(&sha1ctxt
, (unsigned char *)key_opad
, sizeof(key_opad
));
5090 SHA1Update(&sha1ctxt
, (unsigned char *)digest
, SHA1_RESULTLEN
);
5091 SHA1Final(digest
, &sha1ctxt
);
5095 * corresponds to MAC-B = MAC (Key=(Key-B+Key-A), Msg=(R-B+R-A))
5096 * corresponds to MAC-A = MAC (Key=(Key-A+Key-B), Msg=(R-A+R-B))
5099 mptcp_get_hmac(mptcp_addr_id aid
, struct mptcb
*mp_tp
, u_char
*digest
)
5101 uint32_t lrand
, rrand
;
5104 mptcp_get_rands(aid
, mp_tp
, &lrand
, &rrand
);
5105 mptcp_hmac_sha1(mp_tp
->mpt_localkey
, mp_tp
->mpt_remotekey
, lrand
, rrand
,
5110 * Authentication data generation
5113 mptcp_generate_token(char *sha_digest
, int sha_digest_len
, caddr_t token
,
5116 VERIFY(token_len
== sizeof(u_int32_t
));
5117 VERIFY(sha_digest_len
== SHA1_RESULTLEN
);
5119 /* Most significant 32 bits of the SHA1 hash */
5120 bcopy(sha_digest
, token
, sizeof(u_int32_t
));
5125 mptcp_generate_idsn(char *sha_digest
, int sha_digest_len
, caddr_t idsn
,
5128 VERIFY(idsn_len
== sizeof(u_int64_t
));
5129 VERIFY(sha_digest_len
== SHA1_RESULTLEN
);
5132 * Least significant 64 bits of the SHA1 hash
5135 idsn
[7] = sha_digest
[12];
5136 idsn
[6] = sha_digest
[13];
5137 idsn
[5] = sha_digest
[14];
5138 idsn
[4] = sha_digest
[15];
5139 idsn
[3] = sha_digest
[16];
5140 idsn
[2] = sha_digest
[17];
5141 idsn
[1] = sha_digest
[18];
5142 idsn
[0] = sha_digest
[19];
5147 mptcp_conn_properties(struct mptcb
*mp_tp
)
5149 /* There is only Version 0 at this time */
5150 mp_tp
->mpt_version
= MPTCP_STD_VERSION_0
;
5152 /* Set DSS checksum flag */
5153 if (mptcp_dss_csum
) {
5154 mp_tp
->mpt_flags
|= MPTCPF_CHECKSUM
;
5157 /* Set up receive window */
5158 mp_tp
->mpt_rcvwnd
= mptcp_sbspace(mp_tp
);
5160 /* Set up gc ticks */
5161 mp_tp
->mpt_gc_ticks
= MPT_GC_TICKS
;
5165 mptcp_init_local_parms(struct mptses
*mpte
)
5167 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
5168 char key_digest
[SHA1_RESULTLEN
];
5170 read_frandom(&mp_tp
->mpt_localkey
, sizeof(mp_tp
->mpt_localkey
));
5171 mptcp_do_sha1(&mp_tp
->mpt_localkey
, key_digest
);
5173 mptcp_generate_token(key_digest
, SHA1_RESULTLEN
,
5174 (caddr_t
)&mp_tp
->mpt_localtoken
, sizeof(mp_tp
->mpt_localtoken
));
5175 mptcp_generate_idsn(key_digest
, SHA1_RESULTLEN
,
5176 (caddr_t
)&mp_tp
->mpt_local_idsn
, sizeof(u_int64_t
));
5178 /* The subflow SYN is also first MPTCP byte */
5179 mp_tp
->mpt_snduna
= mp_tp
->mpt_sndmax
= mp_tp
->mpt_local_idsn
+ 1;
5180 mp_tp
->mpt_sndnxt
= mp_tp
->mpt_snduna
;
5182 mptcp_conn_properties(mp_tp
);
5186 mptcp_init_remote_parms(struct mptcb
*mp_tp
)
5188 char remote_digest
[SHA1_RESULTLEN
];
5190 /* Only Version 0 is supported for auth purposes */
5191 if (mp_tp
->mpt_version
!= MPTCP_STD_VERSION_0
) {
5195 /* Setup local and remote tokens and Initial DSNs */
5196 mptcp_do_sha1(&mp_tp
->mpt_remotekey
, remote_digest
);
5197 mptcp_generate_token(remote_digest
, SHA1_RESULTLEN
,
5198 (caddr_t
)&mp_tp
->mpt_remotetoken
, sizeof(mp_tp
->mpt_remotetoken
));
5199 mptcp_generate_idsn(remote_digest
, SHA1_RESULTLEN
,
5200 (caddr_t
)&mp_tp
->mpt_remote_idsn
, sizeof(u_int64_t
));
5201 mp_tp
->mpt_rcvnxt
= mp_tp
->mpt_remote_idsn
+ 1;
5202 mp_tp
->mpt_rcvadv
= mp_tp
->mpt_rcvnxt
+ mp_tp
->mpt_rcvwnd
;
5208 mptcp_send_dfin(struct socket
*so
)
5210 struct tcpcb
*tp
= NULL
;
5211 struct inpcb
*inp
= NULL
;
5213 inp
= sotoinpcb(so
);
5218 tp
= intotcpcb(inp
);
5223 if (!(tp
->t_mpflags
& TMPF_RESET
)) {
5224 tp
->t_mpflags
|= TMPF_SEND_DFIN
;
5229 * Data Sequence Mapping routines
5232 mptcp_insert_dsn(struct mppcb
*mpp
, struct mbuf
*m
)
5234 struct mptcb
*mp_tp
;
5240 __IGNORE_WCASTALIGN(mp_tp
= &((struct mpp_mtp
*)mpp
)->mtcb
);
5243 VERIFY(m
->m_flags
& M_PKTHDR
);
5244 m
->m_pkthdr
.pkt_flags
|= (PKTF_MPTCP
| PKTF_MPSO
);
5245 m
->m_pkthdr
.mp_dsn
= mp_tp
->mpt_sndmax
;
5246 VERIFY(m_pktlen(m
) >= 0 && m_pktlen(m
) < UINT16_MAX
);
5247 m
->m_pkthdr
.mp_rlen
= (uint16_t)m_pktlen(m
);
5248 mp_tp
->mpt_sndmax
+= m_pktlen(m
);
5254 mptcp_fallback_sbdrop(struct socket
*so
, struct mbuf
*m
, int len
)
5256 struct mptcb
*mp_tp
= tptomptp(sototcpcb(so
));
5262 if (!m
|| len
== 0) {
5266 while (m
&& len
> 0) {
5267 VERIFY(m
->m_flags
& M_PKTHDR
);
5268 VERIFY(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
);
5270 data_ack
= m
->m_pkthdr
.mp_dsn
+ m
->m_pkthdr
.mp_rlen
;
5271 dsn
= m
->m_pkthdr
.mp_dsn
;
5277 if (m
&& len
== 0) {
5279 * If there is one more mbuf in the chain, it automatically means
5280 * that up to m->mp_dsn has been ack'ed.
5282 * This means, we actually correct data_ack back down (compared
5283 * to what we set inside the loop - dsn + data_len). Because in
5284 * the loop we are "optimistic" and assume that the full mapping
5285 * will be acked. If that's not the case and we get out of the
5286 * loop with m != NULL, it means only up to m->mp_dsn has been
5289 data_ack
= m
->m_pkthdr
.mp_dsn
;
5294 * If len is negative, meaning we acked in the middle of an mbuf,
5295 * only up to this mbuf's data-sequence number has been acked
5296 * at the MPTCP-level.
5301 mptcplog((LOG_DEBUG
, "%s inferred ack up to %u\n", __func__
, (uint32_t)data_ack
),
5302 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
5304 /* We can have data in the subflow's send-queue that is being acked,
5305 * while the DATA_ACK has already advanced. Thus, we should check whether
5306 * or not the DATA_ACK is actually new here.
5308 if (MPTCP_SEQ_LEQ(data_ack
, mp_tp
->mpt_sndmax
) &&
5309 MPTCP_SEQ_GEQ(data_ack
, mp_tp
->mpt_snduna
)) {
5310 mptcp_data_ack_rcvd(mp_tp
, sototcpcb(so
), data_ack
);
5315 mptcp_preproc_sbdrop(struct socket
*so
, struct mbuf
*m
, unsigned int len
)
5319 /* TFO makes things complicated. */
5320 if (so
->so_flags1
& SOF1_TFO_REWIND
) {
5322 so
->so_flags1
&= ~SOF1_TFO_REWIND
;
5325 while (m
&& (!(so
->so_flags
& SOF_MP_SUBFLOW
) || rewinding
)) {
5327 VERIFY(m
->m_flags
& M_PKTHDR
);
5328 VERIFY(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
);
5330 sub_len
= m
->m_pkthdr
.mp_rlen
;
5332 if (sub_len
< len
) {
5333 m
->m_pkthdr
.mp_dsn
+= sub_len
;
5334 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_MPSO
)) {
5335 m
->m_pkthdr
.mp_rseq
+= sub_len
;
5337 m
->m_pkthdr
.mp_rlen
= 0;
5340 /* sub_len >= len */
5341 if (rewinding
== 0) {
5342 m
->m_pkthdr
.mp_dsn
+= len
;
5344 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_MPSO
)) {
5345 if (rewinding
== 0) {
5346 m
->m_pkthdr
.mp_rseq
+= len
;
5349 mptcplog((LOG_DEBUG
, "%s: dsn %u ssn %u len %d %d\n",
5350 __func__
, (u_int32_t
)m
->m_pkthdr
.mp_dsn
,
5351 m
->m_pkthdr
.mp_rseq
, m
->m_pkthdr
.mp_rlen
, len
),
5352 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
5353 m
->m_pkthdr
.mp_rlen
-= len
;
5359 if (so
->so_flags
& SOF_MP_SUBFLOW
&&
5360 !(sototcpcb(so
)->t_mpflags
& TMPF_TFO_REQUEST
) &&
5361 !(sototcpcb(so
)->t_mpflags
& TMPF_RCVD_DACK
)) {
5363 * Received an ack without receiving a DATA_ACK.
5364 * Need to fallback to regular TCP (or destroy this subflow).
5366 sototcpcb(so
)->t_mpflags
|= TMPF_INFIN_SENT
;
5367 mptcp_notify_mpfail(so
);
5371 /* Obtain the DSN mapping stored in the mbuf */
5373 mptcp_output_getm_dsnmap32(struct socket
*so
, int off
,
5374 uint32_t *dsn
, uint32_t *relseq
, uint16_t *data_len
, uint16_t *dss_csum
)
5378 mptcp_output_getm_dsnmap64(so
, off
, &dsn64
, relseq
, data_len
, dss_csum
);
5379 *dsn
= (u_int32_t
)MPTCP_DATASEQ_LOW32(dsn64
);
5383 mptcp_output_getm_dsnmap64(struct socket
*so
, int off
, uint64_t *dsn
,
5384 uint32_t *relseq
, uint16_t *data_len
,
5387 struct mbuf
*m
= so
->so_snd
.sb_mb
;
5392 if (m
== NULL
&& (so
->so_flags
& SOF_DEFUNCT
)) {
5401 * In the subflow socket, the DSN sequencing can be discontiguous,
5402 * but the subflow sequence mapping is contiguous. Use the subflow
5403 * sequence property to find the right mbuf and corresponding dsn
5408 VERIFY(m
->m_flags
& M_PKTHDR
);
5409 VERIFY(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
);
5411 if (off
>= m
->m_len
) {
5420 VERIFY(m
->m_pkthdr
.mp_rlen
<= UINT16_MAX
);
5422 *dsn
= m
->m_pkthdr
.mp_dsn
;
5423 *relseq
= m
->m_pkthdr
.mp_rseq
;
5424 *data_len
= m
->m_pkthdr
.mp_rlen
;
5425 *dss_csum
= m
->m_pkthdr
.mp_csum
;
5427 mptcplog((LOG_DEBUG
, "%s: dsn %u ssn %u data_len %d off %d off_orig %d\n",
5428 __func__
, (u_int32_t
)(*dsn
), *relseq
, *data_len
, off
, off_orig
),
5429 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
5433 * Note that this is called only from tcp_input() via mptcp_input_preproc()
5434 * tcp_input() may trim data after the dsn mapping is inserted into the mbuf.
5435 * When it trims data tcp_input calls m_adj() which does not remove the
5436 * m_pkthdr even if the m_len becomes 0 as a result of trimming the mbuf.
5437 * The dsn map insertion cannot be delayed after trim, because data can be in
5438 * the reassembly queue for a while and the DSN option info in tp will be
5439 * overwritten for every new packet received.
5440 * The dsn map will be adjusted just prior to appending to subflow sockbuf
5441 * with mptcp_adj_rmap()
5444 mptcp_insert_rmap(struct tcpcb
*tp
, struct mbuf
*m
, struct tcphdr
*th
)
5446 VERIFY(m
->m_flags
& M_PKTHDR
);
5447 VERIFY(!(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
));
5449 if (tp
->t_mpflags
& TMPF_EMBED_DSN
) {
5450 m
->m_pkthdr
.mp_dsn
= tp
->t_rcv_map
.mpt_dsn
;
5451 m
->m_pkthdr
.mp_rseq
= tp
->t_rcv_map
.mpt_sseq
;
5452 m
->m_pkthdr
.mp_rlen
= tp
->t_rcv_map
.mpt_len
;
5453 m
->m_pkthdr
.mp_csum
= tp
->t_rcv_map
.mpt_csum
;
5454 if (tp
->t_rcv_map
.mpt_dfin
) {
5455 m
->m_pkthdr
.pkt_flags
|= PKTF_MPTCP_DFIN
;
5458 m
->m_pkthdr
.pkt_flags
|= PKTF_MPTCP
;
5460 tp
->t_mpflags
&= ~TMPF_EMBED_DSN
;
5461 tp
->t_mpflags
|= TMPF_MPTCP_ACKNOW
;
5462 } else if (tp
->t_mpflags
& TMPF_TCP_FALLBACK
) {
5463 if (th
->th_flags
& TH_FIN
) {
5464 m
->m_pkthdr
.pkt_flags
|= PKTF_MPTCP_DFIN
;
5470 * Following routines help with failure detection and failover of data
5471 * transfer from one subflow to another.
5474 mptcp_act_on_txfail(struct socket
*so
)
5476 struct tcpcb
*tp
= NULL
;
5477 struct inpcb
*inp
= sotoinpcb(so
);
5483 tp
= intotcpcb(inp
);
5488 if (so
->so_flags
& SOF_MP_TRYFAILOVER
) {
5492 so
->so_flags
|= SOF_MP_TRYFAILOVER
;
5493 soevent(so
, (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MPFAILOVER
));
5497 * Support for MP_FAIL option
5500 mptcp_get_map_for_dsn(struct socket
*so
, uint64_t dsn_fail
, uint32_t *tcp_seq
)
5502 struct mbuf
*m
= so
->so_snd
.sb_mb
;
5512 VERIFY(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
);
5513 VERIFY(m
->m_flags
& M_PKTHDR
);
5514 dsn
= m
->m_pkthdr
.mp_dsn
;
5515 datalen
= m
->m_pkthdr
.mp_rlen
;
5516 if (MPTCP_SEQ_LEQ(dsn
, dsn_fail
) &&
5517 (MPTCP_SEQ_GEQ(dsn
+ datalen
, dsn_fail
))) {
5518 off
= (int)(dsn_fail
- dsn
);
5519 *tcp_seq
= m
->m_pkthdr
.mp_rseq
+ off
;
5527 * If there was no mbuf data and a fallback to TCP occurred, there's
5528 * not much else to do.
5531 os_log_error(mptcp_log_handle
, "%s: %llu not found \n", __func__
, dsn_fail
);
5536 * Support for sending contiguous MPTCP bytes in subflow
5537 * Also for preventing sending data with ACK in 3-way handshake
5540 mptcp_adj_sendlen(struct socket
*so
, int32_t off
)
5542 struct tcpcb
*tp
= sototcpcb(so
);
5543 struct mptsub
*mpts
= tp
->t_mpsub
;
5545 uint32_t mdss_subflow_seq
;
5546 int mdss_subflow_off
;
5547 uint16_t mdss_data_len
;
5550 if (so
->so_snd
.sb_mb
== NULL
&& (so
->so_flags
& SOF_DEFUNCT
)) {
5554 mptcp_output_getm_dsnmap64(so
, off
, &mdss_dsn
, &mdss_subflow_seq
,
5555 &mdss_data_len
, &dss_csum
);
5558 * We need to compute how much of the mapping still remains.
5559 * So, we compute the offset in the send-buffer of the dss-sub-seq.
5561 mdss_subflow_off
= (mdss_subflow_seq
+ mpts
->mpts_iss
) - tp
->snd_una
;
5564 * When TFO is used, we are sending the mpts->mpts_iss although the relative
5565 * seq has been set to 1 (while it should be 0).
5567 if (tp
->t_mpflags
& TMPF_TFO_REQUEST
) {
5571 VERIFY(off
>= mdss_subflow_off
);
5573 return mdss_data_len
- (off
- mdss_subflow_off
);
5577 mptcp_get_maxseg(struct mptses
*mpte
)
5579 struct mptsub
*mpts
;
5580 uint32_t maxseg
= 0;
5582 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
5583 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
5585 if (!TCPS_HAVEESTABLISHED(tp
->t_state
) ||
5586 TCPS_HAVERCVDFIN2(tp
->t_state
)) {
5590 if (tp
->t_maxseg
> maxseg
) {
5591 maxseg
= tp
->t_maxseg
;
5599 mptcp_get_rcvscale(struct mptses
*mpte
)
5601 struct mptsub
*mpts
;
5602 uint8_t rcvscale
= UINT8_MAX
;
5604 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
5605 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
5607 if (!TCPS_HAVEESTABLISHED(tp
->t_state
) ||
5608 TCPS_HAVERCVDFIN2(tp
->t_state
)) {
5612 if (tp
->rcv_scale
< rcvscale
) {
5613 rcvscale
= tp
->rcv_scale
;
5620 /* Similar to tcp_sbrcv_reserve */
5622 mptcp_sbrcv_reserve(struct mptcb
*mp_tp
, struct sockbuf
*sbrcv
,
5623 u_int32_t newsize
, u_int32_t idealsize
)
5625 uint8_t rcvscale
= mptcp_get_rcvscale(mp_tp
->mpt_mpte
);
5627 /* newsize should not exceed max */
5628 newsize
= min(newsize
, tcp_autorcvbuf_max
);
5630 /* The receive window scale negotiated at the
5631 * beginning of the connection will also set a
5632 * limit on the socket buffer size
5634 newsize
= min(newsize
, TCP_MAXWIN
<< rcvscale
);
5636 /* Set new socket buffer size */
5637 if (newsize
> sbrcv
->sb_hiwat
&&
5638 (sbreserve(sbrcv
, newsize
) == 1)) {
5639 sbrcv
->sb_idealsize
= min(max(sbrcv
->sb_idealsize
,
5640 (idealsize
!= 0) ? idealsize
: newsize
), tcp_autorcvbuf_max
);
5642 /* Again check the limit set by the advertised
5645 sbrcv
->sb_idealsize
= min(sbrcv
->sb_idealsize
,
5646 TCP_MAXWIN
<< rcvscale
);
5651 mptcp_sbrcv_grow(struct mptcb
*mp_tp
)
5653 struct mptses
*mpte
= mp_tp
->mpt_mpte
;
5654 struct socket
*mp_so
= mpte
->mpte_mppcb
->mpp_socket
;
5655 struct sockbuf
*sbrcv
= &mp_so
->so_rcv
;
5656 uint32_t hiwat_sum
= 0;
5657 uint32_t ideal_sum
= 0;
5658 struct mptsub
*mpts
;
5661 * Do not grow the receive socket buffer if
5662 * - auto resizing is disabled, globally or on this socket
5663 * - the high water mark already reached the maximum
5664 * - the stream is in background and receive side is being
5666 * - if there are segments in reassembly queue indicating loss,
5667 * do not need to increase recv window during recovery as more
5668 * data is not going to be sent. A duplicate ack sent during
5669 * recovery should not change the receive window
5671 if (tcp_do_autorcvbuf
== 0 ||
5672 (sbrcv
->sb_flags
& SB_AUTOSIZE
) == 0 ||
5673 tcp_cansbgrow(sbrcv
) == 0 ||
5674 sbrcv
->sb_hiwat
>= tcp_autorcvbuf_max
||
5675 (mp_so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) ||
5676 !LIST_EMPTY(&mp_tp
->mpt_segq
)) {
5677 /* Can not resize the socket buffer, just return */
5682 * Ideally, we want the rbuf to be (sum_i {bw_i} * rtt_max * 2)
5684 * But, for this we first need accurate receiver-RTT estimations, which
5685 * we currently don't have.
5687 * Let's use a dummy algorithm for now, just taking the sum of all
5688 * subflow's receive-buffers. It's too low, but that's all we can get
5692 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
5693 hiwat_sum
+= mpts
->mpts_socket
->so_rcv
.sb_hiwat
;
5694 ideal_sum
+= mpts
->mpts_socket
->so_rcv
.sb_idealsize
;
5697 mptcp_sbrcv_reserve(mp_tp
, sbrcv
, hiwat_sum
, ideal_sum
);
5701 * Determine if we can grow the recieve socket buffer to avoid sending
5702 * a zero window update to the peer. We allow even socket buffers that
5703 * have fixed size (set by the application) to grow if the resource
5704 * constraints are met. They will also be trimmed after the application
5707 * Similar to tcp_sbrcv_grow_rwin
5710 mptcp_sbrcv_grow_rwin(struct mptcb
*mp_tp
, struct sockbuf
*sb
)
5712 struct socket
*mp_so
= mp_tp
->mpt_mpte
->mpte_mppcb
->mpp_socket
;
5713 u_int32_t rcvbufinc
= mptcp_get_maxseg(mp_tp
->mpt_mpte
) << 4;
5714 u_int32_t rcvbuf
= sb
->sb_hiwat
;
5716 if (tcp_recv_bg
== 1 || IS_TCP_RECV_BG(mp_so
)) {
5720 if (tcp_do_autorcvbuf
== 1 &&
5721 tcp_cansbgrow(sb
) &&
5722 /* Diff to tcp_sbrcv_grow_rwin */
5723 (mp_so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) == 0 &&
5724 (rcvbuf
- sb
->sb_cc
) < rcvbufinc
&&
5725 rcvbuf
< tcp_autorcvbuf_max
&&
5726 (sb
->sb_idealsize
> 0 &&
5727 sb
->sb_hiwat
<= (sb
->sb_idealsize
+ rcvbufinc
))) {
5728 sbreserve(sb
, min((sb
->sb_hiwat
+ rcvbufinc
), tcp_autorcvbuf_max
));
5732 /* Similar to tcp_sbspace */
5734 mptcp_sbspace(struct mptcb
*mp_tp
)
5736 struct sockbuf
*sb
= &mp_tp
->mpt_mpte
->mpte_mppcb
->mpp_socket
->so_rcv
;
5739 int32_t pending
= 0;
5741 socket_lock_assert_owned(mptetoso(mp_tp
->mpt_mpte
));
5743 mptcp_sbrcv_grow_rwin(mp_tp
, sb
);
5745 /* hiwat might have changed */
5746 rcvbuf
= sb
->sb_hiwat
;
5748 space
= ((int32_t) imin((rcvbuf
- sb
->sb_cc
),
5749 (sb
->sb_mbmax
- sb
->sb_mbcnt
)));
5755 /* Compensate for data being processed by content filters */
5756 pending
= cfil_sock_data_space(sb
);
5757 #endif /* CONTENT_FILTER */
5758 if (pending
> space
) {
5768 * Support Fallback to Regular TCP
5771 mptcp_notify_mpready(struct socket
*so
)
5773 struct tcpcb
*tp
= NULL
;
5779 tp
= intotcpcb(sotoinpcb(so
));
5785 DTRACE_MPTCP4(multipath__ready
, struct socket
*, so
,
5786 struct sockbuf
*, &so
->so_rcv
, struct sockbuf
*, &so
->so_snd
,
5787 struct tcpcb
*, tp
);
5789 if (!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) {
5793 if (tp
->t_mpflags
& TMPF_MPTCP_READY
) {
5797 tp
->t_mpflags
&= ~TMPF_TCP_FALLBACK
;
5798 tp
->t_mpflags
|= TMPF_MPTCP_READY
;
5800 soevent(so
, (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MPSTATUS
));
5804 mptcp_notify_mpfail(struct socket
*so
)
5806 struct tcpcb
*tp
= NULL
;
5812 tp
= intotcpcb(sotoinpcb(so
));
5818 DTRACE_MPTCP4(multipath__failed
, struct socket
*, so
,
5819 struct sockbuf
*, &so
->so_rcv
, struct sockbuf
*, &so
->so_snd
,
5820 struct tcpcb
*, tp
);
5822 if (tp
->t_mpflags
& TMPF_TCP_FALLBACK
) {
5826 tp
->t_mpflags
&= ~(TMPF_MPTCP_READY
| TMPF_MPTCP_TRUE
);
5827 tp
->t_mpflags
|= TMPF_TCP_FALLBACK
;
5829 soevent(so
, (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MPSTATUS
));
5833 * Keepalive helper function
5836 mptcp_ok_to_keepalive(struct mptcb
*mp_tp
)
5840 socket_lock_assert_owned(mptetoso(mp_tp
->mpt_mpte
));
5842 if (mp_tp
->mpt_state
>= MPTCPS_CLOSE_WAIT
) {
5849 * MPTCP t_maxseg adjustment function
5852 mptcp_adj_mss(struct tcpcb
*tp
, boolean_t mtudisc
)
5855 struct mptcb
*mp_tp
= tptomptp(tp
);
5857 #define MPTCP_COMPUTE_LEN { \
5858 mss_lower = sizeof (struct mptcp_dss_ack_opt); \
5859 if (mp_tp->mpt_flags & MPTCPF_CHECKSUM) \
5862 /* adjust to 32-bit boundary + EOL */ \
5865 if (mp_tp
== NULL
) {
5869 socket_lock_assert_owned(mptetoso(mp_tp
->mpt_mpte
));
5872 * For the first subflow and subsequent subflows, adjust mss for
5873 * most common MPTCP option size, for case where tcp_mss is called
5874 * during option processing and MTU discovery.
5877 if (tp
->t_mpflags
& TMPF_MPTCP_TRUE
&&
5878 !(tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
5882 if (tp
->t_mpflags
& TMPF_PREESTABLISHED
&&
5883 tp
->t_mpflags
& TMPF_SENT_JOIN
) {
5887 if (tp
->t_mpflags
& TMPF_MPTCP_TRUE
) {
5896 * Update the pid, upid, uuid of the subflow so, based on parent so
5899 mptcp_update_last_owner(struct socket
*so
, struct socket
*mp_so
)
5901 if (so
->last_pid
!= mp_so
->last_pid
||
5902 so
->last_upid
!= mp_so
->last_upid
) {
5903 so
->last_upid
= mp_so
->last_upid
;
5904 so
->last_pid
= mp_so
->last_pid
;
5905 uuid_copy(so
->last_uuid
, mp_so
->last_uuid
);
5907 so_update_policy(so
);
5911 fill_mptcp_subflow(struct socket
*so
, mptcp_flow_t
*flow
, struct mptsub
*mpts
)
5915 tcp_getconninfo(so
, &flow
->flow_ci
);
5916 inp
= sotoinpcb(so
);
5917 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
5918 flow
->flow_src
.ss_family
= AF_INET6
;
5919 flow
->flow_dst
.ss_family
= AF_INET6
;
5920 flow
->flow_src
.ss_len
= sizeof(struct sockaddr_in6
);
5921 flow
->flow_dst
.ss_len
= sizeof(struct sockaddr_in6
);
5922 SIN6(&flow
->flow_src
)->sin6_port
= inp
->in6p_lport
;
5923 SIN6(&flow
->flow_dst
)->sin6_port
= inp
->in6p_fport
;
5924 SIN6(&flow
->flow_src
)->sin6_addr
= inp
->in6p_laddr
;
5925 SIN6(&flow
->flow_dst
)->sin6_addr
= inp
->in6p_faddr
;
5926 } else if ((inp
->inp_vflag
& INP_IPV4
) != 0) {
5927 flow
->flow_src
.ss_family
= AF_INET
;
5928 flow
->flow_dst
.ss_family
= AF_INET
;
5929 flow
->flow_src
.ss_len
= sizeof(struct sockaddr_in
);
5930 flow
->flow_dst
.ss_len
= sizeof(struct sockaddr_in
);
5931 SIN(&flow
->flow_src
)->sin_port
= inp
->inp_lport
;
5932 SIN(&flow
->flow_dst
)->sin_port
= inp
->inp_fport
;
5933 SIN(&flow
->flow_src
)->sin_addr
= inp
->inp_laddr
;
5934 SIN(&flow
->flow_dst
)->sin_addr
= inp
->inp_faddr
;
5936 flow
->flow_len
= sizeof(*flow
);
5937 flow
->flow_tcpci_offset
= offsetof(mptcp_flow_t
, flow_ci
);
5938 flow
->flow_flags
= mpts
->mpts_flags
;
5939 flow
->flow_cid
= mpts
->mpts_connid
;
5940 flow
->flow_relseq
= mpts
->mpts_rel_seq
;
5941 flow
->flow_soerror
= mpts
->mpts_socket
->so_error
;
5942 flow
->flow_probecnt
= mpts
->mpts_probecnt
;
5946 mptcp_pcblist SYSCTL_HANDLER_ARGS
5948 #pragma unused(oidp, arg1, arg2)
5952 struct mptses
*mpte
;
5953 struct mptcb
*mp_tp
;
5954 struct mptsub
*mpts
;
5956 conninfo_mptcp_t mptcpci
;
5957 mptcp_flow_t
*flows
= NULL
;
5959 if (req
->newptr
!= USER_ADDR_NULL
) {
5963 lck_mtx_lock(&mtcbinfo
.mppi_lock
);
5964 if (req
->oldptr
== USER_ADDR_NULL
) {
5965 size_t n
= mtcbinfo
.mppi_count
;
5966 lck_mtx_unlock(&mtcbinfo
.mppi_lock
);
5967 req
->oldidx
= (n
+ n
/ 8) * sizeof(conninfo_mptcp_t
) +
5968 4 * (n
+ n
/ 8) * sizeof(mptcp_flow_t
);
5971 TAILQ_FOREACH(mpp
, &mtcbinfo
.mppi_pcbs
, mpp_entry
) {
5973 socket_lock(mpp
->mpp_socket
, 1);
5974 VERIFY(mpp
->mpp_flags
& MPP_ATTACHED
);
5975 mpte
= mptompte(mpp
);
5977 socket_lock_assert_owned(mptetoso(mpte
));
5978 mp_tp
= mpte
->mpte_mptcb
;
5980 bzero(&mptcpci
, sizeof(mptcpci
));
5981 mptcpci
.mptcpci_state
= mp_tp
->mpt_state
;
5982 mptcpci
.mptcpci_flags
= mp_tp
->mpt_flags
;
5983 mptcpci
.mptcpci_ltoken
= mp_tp
->mpt_localtoken
;
5984 mptcpci
.mptcpci_rtoken
= mp_tp
->mpt_remotetoken
;
5985 mptcpci
.mptcpci_notsent_lowat
= mp_tp
->mpt_notsent_lowat
;
5986 mptcpci
.mptcpci_snduna
= mp_tp
->mpt_snduna
;
5987 mptcpci
.mptcpci_sndnxt
= mp_tp
->mpt_sndnxt
;
5988 mptcpci
.mptcpci_sndmax
= mp_tp
->mpt_sndmax
;
5989 mptcpci
.mptcpci_lidsn
= mp_tp
->mpt_local_idsn
;
5990 mptcpci
.mptcpci_sndwnd
= mp_tp
->mpt_sndwnd
;
5991 mptcpci
.mptcpci_rcvnxt
= mp_tp
->mpt_rcvnxt
;
5992 mptcpci
.mptcpci_rcvatmark
= mp_tp
->mpt_rcvnxt
;
5993 mptcpci
.mptcpci_ridsn
= mp_tp
->mpt_remote_idsn
;
5994 mptcpci
.mptcpci_rcvwnd
= mp_tp
->mpt_rcvwnd
;
5996 mptcpci
.mptcpci_nflows
= mpte
->mpte_numflows
;
5997 mptcpci
.mptcpci_mpte_flags
= mpte
->mpte_flags
;
5998 mptcpci
.mptcpci_mpte_addrid
= mpte
->mpte_addrid_last
;
5999 mptcpci
.mptcpci_flow_offset
=
6000 offsetof(conninfo_mptcp_t
, mptcpci_flows
);
6002 len
= sizeof(*flows
) * mpte
->mpte_numflows
;
6003 if (mpte
->mpte_numflows
!= 0) {
6004 flows
= _MALLOC(len
, M_TEMP
, M_WAITOK
| M_ZERO
);
6005 if (flows
== NULL
) {
6006 socket_unlock(mpp
->mpp_socket
, 1);
6009 mptcpci
.mptcpci_len
= sizeof(mptcpci
) +
6010 sizeof(*flows
) * (mptcpci
.mptcpci_nflows
- 1);
6011 error
= SYSCTL_OUT(req
, &mptcpci
,
6012 sizeof(mptcpci
) - sizeof(mptcp_flow_t
));
6014 mptcpci
.mptcpci_len
= sizeof(mptcpci
);
6015 error
= SYSCTL_OUT(req
, &mptcpci
, sizeof(mptcpci
));
6018 socket_unlock(mpp
->mpp_socket
, 1);
6019 FREE(flows
, M_TEMP
);
6023 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
6024 so
= mpts
->mpts_socket
;
6025 fill_mptcp_subflow(so
, &flows
[f
], mpts
);
6028 socket_unlock(mpp
->mpp_socket
, 1);
6030 error
= SYSCTL_OUT(req
, flows
, len
);
6031 FREE(flows
, M_TEMP
);
6037 lck_mtx_unlock(&mtcbinfo
.mppi_lock
);
6042 SYSCTL_PROC(_net_inet_mptcp
, OID_AUTO
, pcblist
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
6043 0, 0, mptcp_pcblist
, "S,conninfo_mptcp_t",
6044 "List of active MPTCP connections");
6047 * Set notsent lowat mark on the MPTCB
6050 mptcp_set_notsent_lowat(struct mptses
*mpte
, int optval
)
6052 struct mptcb
*mp_tp
= NULL
;
6055 if (mpte
->mpte_mppcb
->mpp_flags
& MPP_ATTACHED
) {
6056 mp_tp
= mpte
->mpte_mptcb
;
6060 mp_tp
->mpt_notsent_lowat
= optval
;
6069 mptcp_get_notsent_lowat(struct mptses
*mpte
)
6071 struct mptcb
*mp_tp
= NULL
;
6073 if (mpte
->mpte_mppcb
->mpp_flags
& MPP_ATTACHED
) {
6074 mp_tp
= mpte
->mpte_mptcb
;
6078 return mp_tp
->mpt_notsent_lowat
;
6085 mptcp_notsent_lowat_check(struct socket
*so
)
6087 struct mptses
*mpte
;
6089 struct mptcb
*mp_tp
;
6090 struct mptsub
*mpts
;
6094 mpp
= mpsotomppcb(so
);
6095 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
6099 mpte
= mptompte(mpp
);
6100 socket_lock_assert_owned(mptetoso(mpte
));
6101 mp_tp
= mpte
->mpte_mptcb
;
6103 notsent
= so
->so_snd
.sb_cc
;
6105 if ((notsent
== 0) ||
6106 ((notsent
- (mp_tp
->mpt_sndnxt
- mp_tp
->mpt_snduna
)) <=
6107 mp_tp
->mpt_notsent_lowat
)) {
6108 mptcplog((LOG_DEBUG
, "MPTCP Sender: "
6109 "lowat %d notsent %d actual %llu \n",
6110 mp_tp
->mpt_notsent_lowat
, notsent
,
6111 notsent
- (mp_tp
->mpt_sndnxt
- mp_tp
->mpt_snduna
)),
6112 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
6116 /* When Nagle's algorithm is not disabled, it is better
6117 * to wakeup the client even before there is atleast one
6118 * maxseg of data to write.
6120 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
6122 if (mpts
->mpts_flags
& MPTSF_ACTIVE
) {
6123 struct socket
*subf_so
= mpts
->mpts_socket
;
6124 struct tcpcb
*tp
= intotcpcb(sotoinpcb(subf_so
));
6126 notsent
= so
->so_snd
.sb_cc
-
6127 (tp
->snd_nxt
- tp
->snd_una
);
6129 if ((tp
->t_flags
& TF_NODELAY
) == 0 &&
6130 notsent
> 0 && (notsent
<= (int)tp
->t_maxseg
)) {
6133 mptcplog((LOG_DEBUG
, "MPTCP Sender: lowat %d notsent %d"
6134 " nodelay false \n",
6135 mp_tp
->mpt_notsent_lowat
, notsent
),
6136 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
6144 mptcp_symptoms_ctl_connect(kern_ctl_ref kctlref
, struct sockaddr_ctl
*sac
,
6147 #pragma unused(kctlref, sac, unitinfo)
6149 if (OSIncrementAtomic(&mptcp_kern_skt_inuse
) > 0) {
6150 os_log_error(mptcp_log_handle
, "%s: MPTCP kernel-control socket for Symptoms already open!", __func__
);
6153 mptcp_kern_skt_unit
= sac
->sc_unit
;
6159 mptcp_allow_uuid(uuid_t uuid
, int32_t rssi
)
6163 /* Iterate over all MPTCP connections */
6165 lck_mtx_lock(&mtcbinfo
.mppi_lock
);
6167 TAILQ_FOREACH(mpp
, &mtcbinfo
.mppi_pcbs
, mpp_entry
) {
6168 struct socket
*mp_so
= mpp
->mpp_socket
;
6169 struct mptses
*mpte
= mpp
->mpp_pcbe
;
6171 socket_lock(mp_so
, 1);
6173 if (mp_so
->so_flags
& SOF_DELEGATED
&&
6174 uuid_compare(uuid
, mp_so
->e_uuid
)) {
6176 } else if (!(mp_so
->so_flags
& SOF_DELEGATED
) &&
6177 uuid_compare(uuid
, mp_so
->last_uuid
)) {
6181 os_log(mptcp_log_handle
, "%s - %lx: Got allowance for useApp with rssi %d\n",
6182 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), rssi
);
6184 mpte
->mpte_flags
|= MPTE_ACCESS_GRANTED
;
6186 if (rssi
> MPTCP_TARGET_BASED_RSSI_THRESHOLD
) {
6187 mpte
->mpte_flags
|= MPTE_CELL_PROHIBITED
;
6190 mptcp_check_subflows_and_add(mpte
);
6191 mptcp_remove_subflows(mpte
);
6193 mpte
->mpte_flags
&= ~(MPTE_ACCESS_GRANTED
| MPTE_CELL_PROHIBITED
);
6196 socket_unlock(mp_so
, 1);
6199 lck_mtx_unlock(&mtcbinfo
.mppi_lock
);
6203 mptcp_wifi_status_changed(void)
6207 /* Iterate over all MPTCP connections */
6209 lck_mtx_lock(&mtcbinfo
.mppi_lock
);
6211 TAILQ_FOREACH(mpp
, &mtcbinfo
.mppi_pcbs
, mpp_entry
) {
6212 struct socket
*mp_so
= mpp
->mpp_socket
;
6213 struct mptses
*mpte
= mpp
->mpp_pcbe
;
6215 socket_lock(mp_so
, 1);
6217 /* Only handover- and urgency-mode are purely driven by Symptom's Wi-Fi status */
6218 if (mpte
->mpte_svctype
!= MPTCP_SVCTYPE_HANDOVER
&&
6219 mpte
->mpte_svctype
!= MPTCP_SVCTYPE_TARGET_BASED
) {
6223 mptcp_check_subflows_and_add(mpte
);
6224 mptcp_check_subflows_and_remove(mpte
);
6227 socket_unlock(mp_so
, 1);
6230 lck_mtx_unlock(&mtcbinfo
.mppi_lock
);
6234 mptcp_ask_symptoms(struct mptses
*mpte
)
6236 struct mptcp_symptoms_ask_uuid ask
;
6237 struct socket
*mp_so
;
6241 if (mptcp_kern_skt_unit
== 0) {
6242 os_log_error(mptcp_log_handle
, "%s - %lx: skt_unit is still 0\n",
6243 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
6247 mp_so
= mptetoso(mpte
);
6249 if (mp_so
->so_flags
& SOF_DELEGATED
) {
6252 pid
= mp_so
->last_pid
;
6256 if (p
== PROC_NULL
) {
6257 os_log_error(mptcp_log_handle
, "%s - %lx: Couldn't find proc for pid %u\n",
6258 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), pid
);
6262 ask
.cmd
= MPTCP_SYMPTOMS_ASK_UUID
;
6264 if (mp_so
->so_flags
& SOF_DELEGATED
) {
6265 uuid_copy(ask
.uuid
, mp_so
->e_uuid
);
6267 uuid_copy(ask
.uuid
, mp_so
->last_uuid
);
6270 prio
= proc_get_effective_task_policy(proc_task(p
), TASK_POLICY_ROLE
);
6272 if (prio
== TASK_BACKGROUND_APPLICATION
|| prio
== TASK_NONUI_APPLICATION
||
6273 prio
== TASK_DARWINBG_APPLICATION
) {
6274 ask
.priority
= MPTCP_SYMPTOMS_BACKGROUND
;
6275 } else if (prio
== TASK_FOREGROUND_APPLICATION
) {
6276 ask
.priority
= MPTCP_SYMPTOMS_FOREGROUND
;
6278 ask
.priority
= MPTCP_SYMPTOMS_UNKNOWN
;
6281 err
= ctl_enqueuedata(mptcp_kern_ctrl_ref
, mptcp_kern_skt_unit
,
6282 &ask
, sizeof(ask
), CTL_DATA_EOR
);
6284 os_log(mptcp_log_handle
, "%s - %lx: asked symptoms about pid %u, taskprio %u, prio %u, err %d\n",
6285 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), pid
, prio
, ask
.priority
, err
);
6292 mptcp_symptoms_ctl_disconnect(kern_ctl_ref kctlref
, u_int32_t kcunit
,
6295 #pragma unused(kctlref, kcunit, unitinfo)
6297 OSDecrementAtomic(&mptcp_kern_skt_inuse
);
6303 mptcp_symptoms_ctl_send(kern_ctl_ref kctlref
, u_int32_t kcunit
, void *unitinfo
,
6304 mbuf_t m
, int flags
)
6306 #pragma unused(kctlref, unitinfo, flags)
6307 symptoms_advisory_t
*sa
= NULL
;
6309 if (kcunit
!= mptcp_kern_skt_unit
) {
6310 os_log_error(mptcp_log_handle
, "%s: kcunit %u is different from expected one %u\n",
6311 __func__
, kcunit
, mptcp_kern_skt_unit
);
6314 if (mbuf_pkthdr_len(m
) < sizeof(*sa
)) {
6319 if (mbuf_len(m
) < sizeof(*sa
)) {
6320 os_log_error(mptcp_log_handle
, "%s: mbuf is %lu but need %lu\n",
6321 __func__
, mbuf_len(m
), sizeof(*sa
));
6328 if (sa
->sa_nwk_status
!= SYMPTOMS_ADVISORY_USEAPP
) {
6329 os_log(mptcp_log_handle
, "%s: wifi new,old: %d,%d, cell new, old: %d,%d\n", __func__
,
6330 sa
->sa_wifi_status
, mptcp_advisory
.sa_wifi_status
,
6331 sa
->sa_cell_status
, mptcp_advisory
.sa_cell_status
);
6333 if (sa
->sa_wifi_status
!= mptcp_advisory
.sa_wifi_status
) {
6334 mptcp_advisory
.sa_wifi_status
= sa
->sa_wifi_status
;
6335 mptcp_wifi_status_changed();
6338 struct mptcp_symptoms_answer answer
;
6341 /* We temporarily allow different sizes for ease of submission */
6342 if (mbuf_len(m
) != sizeof(uuid_t
) + sizeof(*sa
) &&
6343 mbuf_len(m
) != sizeof(answer
)) {
6344 os_log_error(mptcp_log_handle
, "%s: mbuf is %lu but need %lu or %lu\n",
6345 __func__
, mbuf_len(m
), sizeof(uuid_t
) + sizeof(*sa
),
6351 memset(&answer
, 0, sizeof(answer
));
6353 err
= mbuf_copydata(m
, 0, mbuf_len(m
), &answer
);
6355 os_log_error(mptcp_log_handle
, "%s: mbuf_copydata returned %d\n", __func__
, err
);
6360 mptcp_allow_uuid(answer
.uuid
, answer
.rssi
);
6368 mptcp_control_register(void)
6370 /* Set up the advisory control socket */
6371 struct kern_ctl_reg mptcp_kern_ctl
;
6373 bzero(&mptcp_kern_ctl
, sizeof(mptcp_kern_ctl
));
6374 strlcpy(mptcp_kern_ctl
.ctl_name
, MPTCP_KERN_CTL_NAME
,
6375 sizeof(mptcp_kern_ctl
.ctl_name
));
6376 mptcp_kern_ctl
.ctl_connect
= mptcp_symptoms_ctl_connect
;
6377 mptcp_kern_ctl
.ctl_disconnect
= mptcp_symptoms_ctl_disconnect
;
6378 mptcp_kern_ctl
.ctl_send
= mptcp_symptoms_ctl_send
;
6379 mptcp_kern_ctl
.ctl_flags
= CTL_FLAG_PRIVILEGED
;
6381 (void)ctl_register(&mptcp_kern_ctl
, &mptcp_kern_ctrl_ref
);
6385 * Three return-values:
6388 * -1 : WiFi-state is unknown
6391 mptcp_is_wifi_unusable_for_session(struct mptses
*mpte
)
6393 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
6394 if (mpte
->mpte_svctype
!= MPTCP_SVCTYPE_HANDOVER
&&
6395 mptcp_advisory
.sa_wifi_status
) {
6396 return symptoms_is_wifi_lossy() ? 1 : 0;
6400 * If it's a first-party app and we don't have any info
6401 * about the Wi-Fi state, let's be pessimistic.
6405 if (mptcp_advisory
.sa_wifi_status
& SYMPTOMS_ADVISORY_WIFI_BAD
) {
6410 * If we are target-based (meaning, we allow to be more lax on
6411 * the "unusable" target. We only *know* about the state once
6412 * we got the allowance from Symptoms (MPTE_ACCESS_GRANTED).
6414 * If RSSI is not bad enough, MPTE_CELL_PROHIBITED will then
6417 * In any other case (while in target-mode), consider WiFi bad
6418 * and we are going to ask for allowance from Symptoms anyway.
6420 if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_TARGET_BASED
) {
6421 if (mpte
->mpte_flags
& MPTE_ACCESS_GRANTED
&&
6422 mpte
->mpte_flags
& MPTE_CELL_PROHIBITED
) {
6434 symptoms_is_wifi_lossy(void)
6436 return (mptcp_advisory
.sa_wifi_status
& SYMPTOMS_ADVISORY_WIFI_OK
) ? false : true;
6439 /* If TFO data is succesfully acked, it must be dropped from the mptcp so */
6441 mptcp_drop_tfo_data(struct mptses
*mpte
, struct mptsub
*mpts
)
6443 struct socket
*mp_so
= mptetoso(mpte
);
6444 struct socket
*so
= mpts
->mpts_socket
;
6445 struct tcpcb
*tp
= intotcpcb(sotoinpcb(so
));
6446 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
6448 /* If data was sent with SYN, rewind state */
6449 if (tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
) {
6450 u_int64_t mp_droplen
= mp_tp
->mpt_sndnxt
- mp_tp
->mpt_snduna
;
6451 unsigned int tcp_droplen
= tp
->snd_una
- tp
->iss
- 1;
6453 VERIFY(mp_droplen
<= (UINT_MAX
));
6454 VERIFY(mp_droplen
>= tcp_droplen
);
6456 mpts
->mpts_flags
&= ~MPTSF_TFO_REQD
;
6457 mpts
->mpts_iss
+= tcp_droplen
;
6458 tp
->t_mpflags
&= ~TMPF_TFO_REQUEST
;
6460 if (mp_droplen
> tcp_droplen
) {
6461 /* handle partial TCP ack */
6462 mp_so
->so_flags1
|= SOF1_TFO_REWIND
;
6463 mp_tp
->mpt_sndnxt
= mp_tp
->mpt_snduna
+ (mp_droplen
- tcp_droplen
);
6464 mp_droplen
= tcp_droplen
;
6466 /* all data on SYN was acked */
6467 mpts
->mpts_rel_seq
= 1;
6468 mp_tp
->mpt_sndnxt
= mp_tp
->mpt_snduna
;
6470 mp_tp
->mpt_sndmax
-= tcp_droplen
;
6472 if (mp_droplen
!= 0) {
6473 VERIFY(mp_so
->so_snd
.sb_mb
!= NULL
);
6474 sbdrop(&mp_so
->so_snd
, (int)mp_droplen
);
6480 mptcp_freeq(struct mptcb
*mp_tp
)
6482 struct tseg_qent
*q
;
6485 while ((q
= LIST_FIRST(&mp_tp
->mpt_segq
)) != NULL
) {
6486 LIST_REMOVE(q
, tqe_q
);
6488 zfree(tcp_reass_zone
, q
);
6491 mp_tp
->mpt_reassqlen
= 0;
6496 mptcp_post_event(u_int32_t event_code
, int value
)
6498 struct kev_mptcp_data event_data
;
6499 struct kev_msg ev_msg
;
6501 memset(&ev_msg
, 0, sizeof(ev_msg
));
6503 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
6504 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
6505 ev_msg
.kev_subclass
= KEV_MPTCP_SUBCLASS
;
6506 ev_msg
.event_code
= event_code
;
6508 event_data
.value
= value
;
6510 ev_msg
.dv
[0].data_ptr
= &event_data
;
6511 ev_msg
.dv
[0].data_length
= sizeof(event_data
);
6513 return kev_post_msg(&ev_msg
);
6517 mptcp_set_cellicon(struct mptses
*mpte
, struct mptsub
*mpts
)
6519 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
6522 /* First-party apps (Siri) don't flip the cellicon */
6523 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
6527 /* Subflow is disappearing - don't set it on this one */
6528 if (mpts
->mpts_flags
& (MPTSF_DISCONNECTING
| MPTSF_DISCONNECTED
)) {
6532 /* Fallen back connections are not triggering the cellicon */
6533 if (mpte
->mpte_mptcb
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
6537 /* Remember the last time we set the cellicon. Needed for debouncing */
6538 mpte
->mpte_last_cellicon_set
= tcp_now
;
6540 tp
->t_timer
[TCPT_CELLICON
] = OFFSET_FROM_START(tp
, MPTCP_CELLICON_TOGGLE_RATE
);
6541 tcp_sched_timers(tp
);
6543 if (mpts
->mpts_flags
& MPTSF_CELLICON_SET
&&
6544 mpte
->mpte_cellicon_increments
!= 0) {
6545 if (mptcp_cellicon_refcount
== 0) {
6546 os_log_error(mptcp_log_handle
, "%s - %lx: Cell should be set (count is %u), but it's zero!\n",
6547 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpte
->mpte_cellicon_increments
);
6549 /* Continue, so that the icon gets set... */
6552 * In this case, the cellicon is already set. No need to bump it
6560 /* When tearing down this subflow, we need to decrement the
6563 mpts
->mpts_flags
|= MPTSF_CELLICON_SET
;
6565 /* This counter, so that when a session gets destroyed we decrement
6566 * the reference counter by whatever is left
6568 mpte
->mpte_cellicon_increments
++;
6570 if (OSIncrementAtomic(&mptcp_cellicon_refcount
)) {
6571 /* If cellicon is already set, get out of here! */
6575 error
= mptcp_post_event(KEV_MPTCP_CELLUSE
, 1);
6578 os_log_error(mptcp_log_handle
, "%s - %lx: Setting cellicon failed with %d\n",
6579 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), error
);
6581 os_log(mptcp_log_handle
, "%s - %lx: successfully set the cellicon\n",
6582 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
));
6587 mptcp_clear_cellicon(void)
6589 int error
= mptcp_post_event(KEV_MPTCP_CELLUSE
, 0);
6592 os_log_error(mptcp_log_handle
, "%s: Unsetting cellicon failed with %d\n",
6595 os_log(mptcp_log_handle
, "%s: successfully unset the cellicon\n",
6601 * Returns true if the icon has been flipped to WiFi.
6604 __mptcp_unset_cellicon(uint32_t val
)
6606 VERIFY(val
< INT32_MAX
);
6607 if (OSAddAtomic((int32_t)-val
, &mptcp_cellicon_refcount
) != 1) {
6611 mptcp_clear_cellicon();
6617 mptcp_unset_cellicon(struct mptses
*mpte
, struct mptsub
*mpts
, uint32_t val
)
6619 /* First-party apps (Siri) don't flip the cellicon */
6620 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
6624 if (mpte
->mpte_cellicon_increments
== 0) {
6625 /* This flow never used cell - get out of here! */
6629 if (mptcp_cellicon_refcount
== 0) {
6630 os_log_error(mptcp_log_handle
, "%s - %lx: Cell is off, but should be at least %u\n",
6631 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpte
->mpte_cellicon_increments
);
6637 if (!(mpts
->mpts_flags
& MPTSF_CELLICON_SET
)) {
6641 mpts
->mpts_flags
&= ~MPTSF_CELLICON_SET
;
6644 if (mpte
->mpte_cellicon_increments
< val
) {
6645 os_log_error(mptcp_log_handle
, "%s - %lx: Increments is %u but want to dec by %u.\n",
6646 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpte
->mpte_cellicon_increments
, val
);
6647 val
= mpte
->mpte_cellicon_increments
;
6650 mpte
->mpte_cellicon_increments
-= val
;
6652 if (__mptcp_unset_cellicon(val
) == false) {
6656 /* All flows are gone - our counter should be at zero too! */
6657 if (mpte
->mpte_cellicon_increments
!= 0) {
6658 os_log_error(mptcp_log_handle
, "%s - %lx: Inconsistent state! Cell refcount is zero but increments are at %u\n",
6659 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), mpte
->mpte_cellicon_increments
);
6664 mptcp_reset_rexmit_state(struct tcpcb
*tp
)
6666 struct mptsub
*mpts
;
6675 so
= inp
->inp_socket
;
6680 if (!(so
->so_flags
& SOF_MP_SUBFLOW
)) {
6686 mpts
->mpts_flags
&= ~MPTSF_WRITE_STALL
;
6687 so
->so_flags
&= ~SOF_MP_TRYFAILOVER
;
6691 mptcp_reset_keepalive(struct tcpcb
*tp
)
6693 struct mptsub
*mpts
= tp
->t_mpsub
;
6695 mpts
->mpts_flags
&= ~MPTSF_READ_STALL
;