2 * Copyright (c) 2012-2018 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@
30 * A note on the MPTCP/NECP-interactions:
32 * MPTCP uses NECP-callbacks to get notified of interface/policy events.
33 * MPTCP registers to these events at the MPTCP-layer for interface-events
34 * through a call to necp_client_register_multipath_cb.
35 * To get per-flow events (aka per TCP-subflow), we register to it with
36 * necp_client_register_socket_flow. Both registrations happen by using the
37 * necp-client-uuid that comes from the app.
39 * The locking is rather tricky. In general, we expect the lock-ordering to
40 * happen from necp-fd -> necp->client -> mpp_lock.
42 * There are however some subtleties.
44 * 1. When registering the multipath_cb, we are holding the mpp_lock. This is
45 * safe, because it is the very first time this MPTCP-connection goes into NECP.
46 * As we go into NECP we take the NECP-locks and thus are guaranteed that no
47 * NECP-locks will deadlock us. Because these NECP-events will also first take
48 * the NECP-locks. Either they win the race and thus won't find our
49 * MPTCP-connection. Or, MPTCP wins the race and thus it will safely install
50 * the callbacks while holding the NECP lock.
52 * 2. When registering the subflow-callbacks we must unlock the mpp_lock. This,
53 * because we have already registered callbacks and we might race against an
54 * NECP-event that will match on our socket. So, we have to unlock to be safe.
56 * 3. When removing the multipath_cb, we do it in mp_pcbdispose(). The
57 * so_usecount has reached 0. We must be careful to not remove the mpp_socket
58 * pointers before we unregistered the callback. Because, again we might be
59 * racing against an NECP-event. Unregistering must happen with an unlocked
60 * mpp_lock, because of the lock-ordering constraint. It could be that
61 * before we had a chance to unregister an NECP-event triggers. That's why
62 * we need to check for the so_usecount in mptcp_session_necp_cb. If we get
63 * there while the socket is being garbage-collected, the use-count will go
64 * down to 0 and we exit. Removal of the multipath_cb again happens by taking
65 * the NECP-locks so any running NECP-events will finish first and exit cleanly.
67 * 4. When removing the subflow-callback, we do it in in_pcbdispose(). Again,
68 * the socket-lock must be unlocked for lock-ordering constraints. This gets a
69 * bit tricky here, as in tcp_garbage_collect we hold the mp_so and so lock.
70 * So, we drop the mp_so-lock as soon as the subflow is unlinked with
71 * mptcp_subflow_del. Then, in in_pcbdispose we drop the subflow-lock.
72 * If an NECP-event was waiting on the lock in mptcp_subflow_necp_cb, when it
73 * gets it, it will realize that the subflow became non-MPTCP and retry (see
74 * tcp_lock). Then it waits again on the subflow-lock. When we drop this lock
75 * in in_pcbdispose, and enter necp_inpcb_dispose, this one will have to wait
76 * for the NECP-lock (held by the other thread that is taking care of the NECP-
77 * event). So, the event now finally gets the subflow-lock and then hits an
78 * so_usecount that is 0 and exits. Eventually, we can remove the subflow from
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
86 #include <sys/mcache.h>
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/syslog.h>
90 #include <sys/protosw.h>
92 #include <kern/zalloc.h>
93 #include <kern/locks.h>
98 #include <netinet/in.h>
99 #include <netinet/in_var.h>
100 #include <netinet/tcp.h>
101 #include <netinet/tcp_fsm.h>
102 #include <netinet/tcp_seq.h>
103 #include <netinet/tcp_var.h>
104 #include <netinet/mptcp_var.h>
105 #include <netinet/mptcp.h>
106 #include <netinet/mptcp_seq.h>
107 #include <netinet/mptcp_opt.h>
108 #include <netinet/mptcp_timer.h>
110 int mptcp_enable
= 1;
111 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, enable
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
112 &mptcp_enable
, 0, "Enable Multipath TCP Support");
115 * Number of times to try negotiating MPTCP on SYN retransmissions.
116 * We haven't seen any reports of a middlebox that is dropping all SYN-segments
117 * that have an MPTCP-option. Thus, let's be generous and retransmit it 4 times.
119 int mptcp_mpcap_retries
= 4;
120 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, mptcp_cap_retr
,
121 CTLFLAG_RW
| CTLFLAG_LOCKED
,
122 &mptcp_mpcap_retries
, 0, "Number of MP Capable SYN Retries");
125 * By default, DSS checksum is turned off, revisit if we ever do
126 * MPTCP for non SSL Traffic.
128 int mptcp_dss_csum
= 0;
129 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, dss_csum
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
130 &mptcp_dss_csum
, 0, "Enable DSS checksum");
133 * When mptcp_fail_thresh number of retransmissions are sent, subflow failover
134 * is attempted on a different path.
136 int mptcp_fail_thresh
= 1;
137 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, fail
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
138 &mptcp_fail_thresh
, 0, "Failover threshold");
141 * MPTCP subflows have TCP keepalives set to ON. Set a conservative keeptime
142 * as carrier networks mostly have a 30 minute to 60 minute NAT Timeout.
143 * Some carrier networks have a timeout of 10 or 15 minutes.
145 int mptcp_subflow_keeptime
= 60 * 14;
146 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, keepalive
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
147 &mptcp_subflow_keeptime
, 0, "Keepalive in seconds");
149 int mptcp_rtthist_rtthresh
= 600;
150 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, rtthist_thresh
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
151 &mptcp_rtthist_rtthresh
, 0, "Rtt threshold");
153 int mptcp_rtothresh
= 1500;
154 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, rto_thresh
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
155 &mptcp_rtothresh
, 0, "RTO threshold");
158 * Probe the preferred path, when it is not in use
160 uint32_t mptcp_probeto
= 1000;
161 SYSCTL_UINT(_net_inet_mptcp
, OID_AUTO
, probeto
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
162 &mptcp_probeto
, 0, "Disable probing by setting to 0");
164 uint32_t mptcp_probecnt
= 5;
165 SYSCTL_UINT(_net_inet_mptcp
, OID_AUTO
, probecnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
166 &mptcp_probecnt
, 0, "Number of probe writes");
169 mptcp_reass_present(struct socket
*mp_so
)
171 struct mptses
*mpte
= mpsotompte(mp_so
);
172 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
178 * Present data to user, advancing rcv_nxt through
179 * completed sequence space.
181 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
) {
184 q
= LIST_FIRST(&mp_tp
->mpt_segq
);
185 if (!q
|| q
->tqe_m
->m_pkthdr
.mp_dsn
!= mp_tp
->mpt_rcvnxt
) {
190 * If there is already another thread doing reassembly for this
191 * connection, it is better to let it finish the job --
194 if (mp_tp
->mpt_flags
& MPTCPF_REASS_INPROG
) {
198 mp_tp
->mpt_flags
|= MPTCPF_REASS_INPROG
;
201 mp_tp
->mpt_rcvnxt
+= q
->tqe_len
;
202 LIST_REMOVE(q
, tqe_q
);
203 if (mp_so
->so_state
& SS_CANTRCVMORE
) {
206 flags
= !!(q
->tqe_m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP_DFIN
);
207 if (sbappendstream_rcvdemux(mp_so
, q
->tqe_m
)) {
211 zfree(tcp_reass_zone
, q
);
212 mp_tp
->mpt_reassqlen
--;
213 q
= LIST_FIRST(&mp_tp
->mpt_segq
);
214 } while (q
&& q
->tqe_m
->m_pkthdr
.mp_dsn
== mp_tp
->mpt_rcvnxt
);
215 mp_tp
->mpt_flags
&= ~MPTCPF_REASS_INPROG
;
218 sorwakeup(mp_so
); /* done with socket lock held */
224 mptcp_reass(struct socket
*mp_so
, struct pkthdr
*phdr
, int *tlenp
, struct mbuf
*m
)
226 struct mptcb
*mp_tp
= mpsotomppcb(mp_so
)->mpp_pcbe
->mpte_mptcb
;
227 u_int64_t mb_dsn
= phdr
->mp_dsn
;
229 struct tseg_qent
*p
= NULL
;
230 struct tseg_qent
*nq
;
231 struct tseg_qent
*te
= NULL
;
235 * Limit the number of segments in the reassembly queue to prevent
236 * holding on to too many segments (and thus running out of mbufs).
237 * Make sure to let the missing segment through which caused this
238 * queue. Always keep one global queue entry spare to be able to
239 * process the missing segment.
241 qlimit
= MIN(MAX(100, mp_so
->so_rcv
.sb_hiwat
>> 10),
242 (tcp_autorcvbuf_max
>> 10));
243 if (mb_dsn
!= mp_tp
->mpt_rcvnxt
&&
244 (mp_tp
->mpt_reassqlen
+ 1) >= qlimit
) {
245 tcpstat
.tcps_mptcp_rcvmemdrop
++;
251 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
252 te
= (struct tseg_qent
*) zalloc(tcp_reass_zone
);
254 tcpstat
.tcps_mptcp_rcvmemdrop
++;
259 mp_tp
->mpt_reassqlen
++;
262 * Find a segment which begins after this one does.
264 LIST_FOREACH(q
, &mp_tp
->mpt_segq
, tqe_q
) {
265 if (MPTCP_SEQ_GT(q
->tqe_m
->m_pkthdr
.mp_dsn
, mb_dsn
)) {
272 * If there is a preceding segment, it may provide some of
273 * our data already. If so, drop the data from the incoming
274 * segment. If it provides all of our data, drop us.
278 /* conversion to int (in i) handles seq wraparound */
279 i
= p
->tqe_m
->m_pkthdr
.mp_dsn
+ p
->tqe_len
- mb_dsn
;
282 tcpstat
.tcps_mptcp_rcvduppack
++;
284 zfree(tcp_reass_zone
, te
);
286 mp_tp
->mpt_reassqlen
--;
288 * Try to present any queued data
289 * at the left window edge to the user.
290 * This is needed after the 3-WHS
295 VERIFY(i
<= INT_MAX
);
302 tcpstat
.tcps_mp_oodata
++;
305 * While we overlap succeeding segments trim them or,
306 * if they are completely covered, dequeue them.
309 int64_t i
= (mb_dsn
+ *tlenp
) - q
->tqe_m
->m_pkthdr
.mp_dsn
;
314 if (i
< q
->tqe_len
) {
315 q
->tqe_m
->m_pkthdr
.mp_dsn
+= i
;
318 VERIFY(i
<= INT_MAX
);
319 m_adj(q
->tqe_m
, (int)i
);
323 nq
= LIST_NEXT(q
, tqe_q
);
324 LIST_REMOVE(q
, tqe_q
);
326 zfree(tcp_reass_zone
, q
);
327 mp_tp
->mpt_reassqlen
--;
331 /* Insert the new segment queue entry into place. */
334 te
->tqe_len
= *tlenp
;
337 LIST_INSERT_HEAD(&mp_tp
->mpt_segq
, te
, tqe_q
);
339 LIST_INSERT_AFTER(p
, te
, tqe_q
);
343 return mptcp_reass_present(mp_so
);
347 * MPTCP input, called when data has been read from a subflow socket.
350 mptcp_input(struct mptses
*mpte
, struct mbuf
*m
)
352 struct socket
*mp_so
;
353 struct mptcb
*mp_tp
= NULL
;
354 int count
= 0, wakeup
= 0;
355 struct mbuf
*save
= NULL
, *prev
= NULL
;
356 struct mbuf
*freelist
= NULL
, *tail
= NULL
;
358 VERIFY(m
->m_flags
& M_PKTHDR
);
360 mp_so
= mptetoso(mpte
);
361 mp_tp
= mpte
->mpte_mptcb
;
363 socket_lock_assert_owned(mp_so
);
367 mp_tp
->mpt_rcvwnd
= mptcp_sbspace(mp_tp
);
370 * Each mbuf contains MPTCP Data Sequence Map
371 * Process the data for reassembly, delivery to MPTCP socket
375 count
= mp_so
->so_rcv
.sb_cc
;
378 * In the degraded fallback case, data is accepted without DSS map
380 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
384 mptcp_sbrcv_grow(mp_tp
);
388 if ((iter
->m_flags
& M_PKTHDR
) &&
389 (iter
->m_pkthdr
.pkt_flags
& PKTF_MPTCP_DFIN
)) {
393 if ((iter
->m_flags
& M_PKTHDR
) && m_pktlen(iter
) == 0) {
394 /* Don't add zero-length packets, so jump it! */
400 prev
->m_next
= iter
->m_next
;
405 /* It was a zero-length packet so next one must be a pkthdr */
406 VERIFY(iter
== NULL
|| iter
->m_flags
& M_PKTHDR
);
414 * assume degraded flow as this may be the first packet
415 * without DSS, and the subflow state is not updated yet.
417 if (sbappendstream_rcvdemux(mp_so
, m
)) {
421 DTRACE_MPTCP5(receive__degraded
, struct mbuf
*, m
,
422 struct socket
*, mp_so
,
423 struct sockbuf
*, &mp_so
->so_rcv
,
424 struct sockbuf
*, &mp_so
->so_snd
,
425 struct mptses
*, mpte
);
426 count
= mp_so
->so_rcv
.sb_cc
- count
;
428 mp_tp
->mpt_rcvnxt
+= count
;
431 mptcp_close_fsm(mp_tp
, MPCE_RECV_DATA_FIN
);
432 socantrcvmore(mp_so
);
443 VERIFY(m
->m_flags
& M_PKTHDR
);
445 /* If fallback occurs, mbufs will not have PKTF_MPTCP set */
446 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
)) {
452 * A single TCP packet formed of multiple mbufs
453 * holds DSS mapping in the first mbuf of the chain.
454 * Other mbufs in the chain may have M_PKTHDR set
455 * even though they belong to the same TCP packet
456 * and therefore use the DSS mapping stored in the
457 * first mbuf of the mbuf chain. mptcp_input() can
458 * get an mbuf chain with multiple TCP packets.
460 while (save
&& (!(save
->m_flags
& M_PKTHDR
) ||
461 !(save
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
))) {
471 mb_dsn
= m
->m_pkthdr
.mp_dsn
;
472 mb_datalen
= m
->m_pkthdr
.mp_rlen
;
474 todrop
= (mb_dsn
+ mb_datalen
) - (mp_tp
->mpt_rcvnxt
+ mp_tp
->mpt_rcvwnd
);
476 tcpstat
.tcps_mptcp_rcvpackafterwin
++;
478 os_log_info(mptcp_log_handle
, "%s - %lx: dropping dsn %u dlen %u rcvnxt %u rcvwnd %u todrop %lld\n",
479 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
480 (uint32_t)mb_dsn
, mb_datalen
, (uint32_t)mp_tp
->mpt_rcvnxt
,
481 mp_tp
->mpt_rcvwnd
, todrop
);
483 if (todrop
>= mb_datalen
) {
484 if (freelist
== NULL
) {
500 VERIFY(todrop
<= INT_MAX
);
501 m_adj(m
, (int)-todrop
);
502 mb_datalen
-= todrop
;
503 m
->m_pkthdr
.mp_rlen
-= todrop
;
507 * We drop from the right edge of the mbuf, thus the
508 * DATA_FIN is dropped as well
510 m
->m_pkthdr
.pkt_flags
&= ~PKTF_MPTCP_DFIN
;
513 if (MPTCP_SEQ_LT(mb_dsn
, mp_tp
->mpt_rcvnxt
)) {
514 if (MPTCP_SEQ_LEQ((mb_dsn
+ mb_datalen
),
515 mp_tp
->mpt_rcvnxt
)) {
516 if (freelist
== NULL
) {
532 VERIFY((mp_tp
->mpt_rcvnxt
- mb_dsn
) <= INT_MAX
);
533 m_adj(m
, (int)(mp_tp
->mpt_rcvnxt
- mb_dsn
));
534 mb_datalen
-= (mp_tp
->mpt_rcvnxt
- mb_dsn
);
535 mb_dsn
= mp_tp
->mpt_rcvnxt
;
536 VERIFY(mb_datalen
>= 0 && mb_datalen
<= USHRT_MAX
);
537 m
->m_pkthdr
.mp_rlen
= (uint16_t)mb_datalen
;
538 m
->m_pkthdr
.mp_dsn
= mb_dsn
;
542 if (MPTCP_SEQ_GT(mb_dsn
, mp_tp
->mpt_rcvnxt
) ||
543 !LIST_EMPTY(&mp_tp
->mpt_segq
)) {
544 mb_dfin
= mptcp_reass(mp_so
, &m
->m_pkthdr
, &mb_datalen
, m
);
548 mb_dfin
= !!(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP_DFIN
);
550 mptcp_sbrcv_grow(mp_tp
);
552 if (sbappendstream_rcvdemux(mp_so
, m
)) {
556 DTRACE_MPTCP6(receive
, struct mbuf
*, m
, struct socket
*, mp_so
,
557 struct sockbuf
*, &mp_so
->so_rcv
,
558 struct sockbuf
*, &mp_so
->so_snd
,
559 struct mptses
*, mpte
,
560 struct mptcb
*, mp_tp
);
561 count
= mp_so
->so_rcv
.sb_cc
- count
;
562 tcpstat
.tcps_mp_rcvtotal
++;
563 tcpstat
.tcps_mp_rcvbytes
+= count
;
565 mp_tp
->mpt_rcvnxt
+= count
;
569 mptcp_close_fsm(mp_tp
, MPCE_RECV_DATA_FIN
);
570 socantrcvmore(mp_so
);
574 count
= mp_so
->so_rcv
.sb_cc
;
587 mptcp_can_send_more(struct mptcb
*mp_tp
, boolean_t ignore_reinject
)
589 struct socket
*mp_so
= mptetoso(mp_tp
->mpt_mpte
);
592 * Always send if there is data in the reinject-queue.
594 if (!ignore_reinject
&& mp_tp
->mpt_mpte
->mpte_reinjectq
) {
601 * 1. snd_nxt >= snd_max : Means, basically everything has been sent.
602 * Except when using TFO, we might be doing a 0-byte write.
603 * 2. snd_una + snd_wnd <= snd_nxt: No space in the receiver's window
604 * 3. snd_nxt + 1 == snd_max and we are closing: A DATA_FIN is scheduled.
607 if (!(mp_so
->so_flags1
& SOF1_PRECONNECT_DATA
) && MPTCP_SEQ_GEQ(mp_tp
->mpt_sndnxt
, mp_tp
->mpt_sndmax
)) {
611 if (MPTCP_SEQ_LEQ(mp_tp
->mpt_snduna
+ mp_tp
->mpt_sndwnd
, mp_tp
->mpt_sndnxt
)) {
615 if (mp_tp
->mpt_sndnxt
+ 1 == mp_tp
->mpt_sndmax
&& mp_tp
->mpt_state
> MPTCPS_CLOSE_WAIT
) {
619 if (mp_tp
->mpt_state
>= MPTCPS_FIN_WAIT_2
) {
630 mptcp_output(struct mptses
*mpte
)
634 struct mptsub
*mpts_tried
= NULL
;
635 struct socket
*mp_so
;
636 struct mptsub
*preferred_mpts
= NULL
;
637 uint64_t old_snd_nxt
;
640 mp_so
= mptetoso(mpte
);
641 mp_tp
= mpte
->mpte_mptcb
;
643 socket_lock_assert_owned(mp_so
);
645 if (mp_so
->so_flags
& SOF_DEFUNCT
) {
649 VERIFY(!(mpte
->mpte_mppcb
->mpp_flags
& MPP_WUPCALL
));
650 mpte
->mpte_mppcb
->mpp_flags
|= MPP_WUPCALL
;
652 old_snd_nxt
= mp_tp
->mpt_sndnxt
;
653 while (mptcp_can_send_more(mp_tp
, FALSE
)) {
654 /* get the "best" subflow to be used for transmission */
655 mpts
= mptcp_get_subflow(mpte
, &preferred_mpts
);
657 mptcplog((LOG_INFO
, "%s: no subflow\n", __func__
),
658 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_LOG
);
662 /* In case there's just one flow, we reattempt later */
663 if (mpts_tried
!= NULL
&&
664 (mpts
== mpts_tried
|| (mpts
->mpts_flags
& MPTSF_FAILINGOVER
))) {
665 mpts_tried
->mpts_flags
&= ~MPTSF_FAILINGOVER
;
666 mpts_tried
->mpts_flags
|= MPTSF_ACTIVE
;
667 mptcp_start_timer(mpte
, MPTT_REXMT
);
672 * Automatic sizing of send socket buffer. Increase the send
673 * socket buffer size if all of the following criteria are met
674 * 1. the receiver has enough buffer space for this data
675 * 2. send buffer is filled to 7/8th with data (so we actually
676 * have data to make use of it);
678 if ((mp_so
->so_snd
.sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) == SB_AUTOSIZE
&&
679 tcp_cansbgrow(&mp_so
->so_snd
)) {
680 if ((mp_tp
->mpt_sndwnd
/ 4 * 5) >= mp_so
->so_snd
.sb_hiwat
&&
681 mp_so
->so_snd
.sb_cc
>= (mp_so
->so_snd
.sb_hiwat
/ 8 * 7)) {
682 if (sbreserve(&mp_so
->so_snd
,
683 min(mp_so
->so_snd
.sb_hiwat
+ tcp_autosndbuf_inc
,
684 tcp_autosndbuf_max
)) == 1) {
685 mp_so
->so_snd
.sb_idealsize
= mp_so
->so_snd
.sb_hiwat
;
690 DTRACE_MPTCP3(output
, struct mptses
*, mpte
, struct mptsub
*, mpts
,
691 struct socket
*, mp_so
);
692 error
= mptcp_subflow_output(mpte
, mpts
, 0);
694 /* can be a temporary loss of source address or other error */
695 mpts
->mpts_flags
|= MPTSF_FAILINGOVER
;
696 mpts
->mpts_flags
&= ~MPTSF_ACTIVE
;
698 if (error
!= ECANCELED
) {
699 os_log_error(mptcp_log_handle
, "%s - %lx: Error = %d mpts_flags %#x\n",
700 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
),
701 error
, mpts
->mpts_flags
);
705 /* The model is to have only one active flow at a time */
706 mpts
->mpts_flags
|= MPTSF_ACTIVE
;
707 mpts
->mpts_probesoon
= mpts
->mpts_probecnt
= 0;
709 /* Allows us to update the smoothed rtt */
710 if (mptcp_probeto
&& mpts
!= preferred_mpts
&& preferred_mpts
!= NULL
) {
711 if (preferred_mpts
->mpts_probesoon
) {
712 if ((tcp_now
- preferred_mpts
->mpts_probesoon
) > mptcp_probeto
) {
713 mptcp_subflow_output(mpte
, preferred_mpts
, MPTCP_SUBOUT_PROBING
);
714 if (preferred_mpts
->mpts_probecnt
>= mptcp_probecnt
) {
715 preferred_mpts
->mpts_probesoon
= 0;
716 preferred_mpts
->mpts_probecnt
= 0;
720 preferred_mpts
->mpts_probesoon
= tcp_now
;
721 preferred_mpts
->mpts_probecnt
= 0;
725 if (mpte
->mpte_active_sub
== NULL
) {
726 mpte
->mpte_active_sub
= mpts
;
727 } else if (mpte
->mpte_active_sub
!= mpts
) {
728 mpte
->mpte_active_sub
->mpts_flags
&= ~MPTSF_ACTIVE
;
729 mpte
->mpte_active_sub
= mpts
;
731 mptcpstats_inc_switch(mpte
, mpts
);
735 if (mp_tp
->mpt_state
> MPTCPS_CLOSE_WAIT
) {
736 if (mp_tp
->mpt_sndnxt
+ 1 == mp_tp
->mpt_sndmax
&&
737 mp_tp
->mpt_snduna
== mp_tp
->mpt_sndnxt
) {
738 mptcp_finish_usrclosed(mpte
);
742 mptcp_handle_deferred_upcalls(mpte
->mpte_mppcb
, MPP_WUPCALL
);
744 /* subflow errors should not be percolated back up */
749 static struct mptsub
*
750 mptcp_choose_subflow(struct mptsub
*mpts
, struct mptsub
*curbest
, int *currtt
)
752 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
755 * Lower RTT? Take it, if it's our first one, or
756 * it doesn't has any loss, or the current one has
759 if (tp
->t_srtt
&& *currtt
> tp
->t_srtt
&&
760 (curbest
== NULL
|| tp
->t_rxtshift
== 0 ||
761 sototcpcb(curbest
->mpts_socket
)->t_rxtshift
)) {
762 *currtt
= tp
->t_srtt
;
767 * If we find a subflow without loss, take it always!
770 sototcpcb(curbest
->mpts_socket
)->t_rxtshift
&&
771 tp
->t_rxtshift
== 0) {
772 *currtt
= tp
->t_srtt
;
776 return curbest
!= NULL
? curbest
: mpts
;
779 static struct mptsub
*
780 mptcp_return_subflow(struct mptsub
*mpts
)
782 if (mpts
&& mptcp_subflow_cwnd_space(mpts
->mpts_socket
) <= 0) {
790 mptcp_subflow_is_slow(struct mptses
*mpte
, struct mptsub
*mpts
)
792 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
793 int fail_thresh
= mptcp_fail_thresh
;
795 if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_HANDOVER
) {
799 return tp
->t_rxtshift
>= fail_thresh
&&
800 (mptetoso(mpte
)->so_snd
.sb_cc
|| mpte
->mpte_reinjectq
);
804 * Return the most eligible subflow to be used for sending data.
807 mptcp_get_subflow(struct mptses
*mpte
, struct mptsub
**preferred
)
809 struct tcpcb
*besttp
, *secondtp
;
810 struct inpcb
*bestinp
, *secondinp
;
812 struct mptsub
*best
= NULL
;
813 struct mptsub
*second_best
= NULL
;
814 int exp_rtt
= INT_MAX
, cheap_rtt
= INT_MAX
;
818 * Choose the best subflow for cellular and non-cellular interfaces.
821 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
822 struct socket
*so
= mpts
->mpts_socket
;
823 struct tcpcb
*tp
= sototcpcb(so
);
824 struct inpcb
*inp
= sotoinpcb(so
);
826 mptcplog((LOG_DEBUG
, "%s mpts %u mpts_flags %#x, suspended %u sostate %#x tpstate %u cellular %d rtt %u rxtshift %u cheap %u exp %u cwnd %d\n",
827 __func__
, mpts
->mpts_connid
, mpts
->mpts_flags
,
828 INP_WAIT_FOR_IF_FEEDBACK(inp
), so
->so_state
, tp
->t_state
,
829 inp
->inp_last_outifp
? IFNET_IS_CELLULAR(inp
->inp_last_outifp
) : -1,
830 tp
->t_srtt
, tp
->t_rxtshift
, cheap_rtt
, exp_rtt
,
831 mptcp_subflow_cwnd_space(so
)),
832 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
835 * First, the hard conditions to reject subflows
836 * (e.g., not connected,...)
838 if (inp
->inp_last_outifp
== NULL
) {
842 if (INP_WAIT_FOR_IF_FEEDBACK(inp
)) {
846 /* There can only be one subflow in degraded state */
847 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) {
853 * If this subflow is waiting to finally send, do it!
855 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
856 return mptcp_return_subflow(mpts
);
860 * Only send if the subflow is MP_CAPABLE. The exceptions to
861 * this rule (degraded or TFO) have been taken care of above.
863 if (!(mpts
->mpts_flags
& MPTSF_MP_CAPABLE
)) {
867 if ((so
->so_state
& SS_ISDISCONNECTED
) ||
868 !(so
->so_state
& SS_ISCONNECTED
) ||
869 !TCPS_HAVEESTABLISHED(tp
->t_state
) ||
870 tp
->t_state
> TCPS_CLOSE_WAIT
) {
875 * Second, the soft conditions to find the subflow with best
876 * conditions for each set (aka cellular vs non-cellular)
878 if (IFNET_IS_CELLULAR(inp
->inp_last_outifp
)) {
879 second_best
= mptcp_choose_subflow(mpts
, second_best
,
882 best
= mptcp_choose_subflow(mpts
, best
, &cheap_rtt
);
887 * If there is no preferred or backup subflow, and there is no active
888 * subflow use the last usable subflow.
891 return mptcp_return_subflow(second_best
);
894 if (second_best
== NULL
) {
895 return mptcp_return_subflow(best
);
898 besttp
= sototcpcb(best
->mpts_socket
);
899 bestinp
= sotoinpcb(best
->mpts_socket
);
900 secondtp
= sototcpcb(second_best
->mpts_socket
);
901 secondinp
= sotoinpcb(second_best
->mpts_socket
);
903 if (preferred
!= NULL
) {
904 *preferred
= mptcp_return_subflow(best
);
908 * Second Step: Among best and second_best. Choose the one that is
909 * most appropriate for this particular service-type.
911 if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_HANDOVER
) {
913 * Only handover if Symptoms tells us to do so.
915 if (!IFNET_IS_CELLULAR(bestinp
->inp_last_outifp
) &&
916 mptcp_is_wifi_unusable_for_session(mpte
) != 0 && mptcp_subflow_is_slow(mpte
, best
)) {
917 return mptcp_return_subflow(second_best
);
920 return mptcp_return_subflow(best
);
921 } else if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_INTERACTIVE
) {
922 int rtt_thresh
= mptcp_rtthist_rtthresh
<< TCP_RTT_SHIFT
;
923 int rto_thresh
= mptcp_rtothresh
;
925 /* Adjust with symptoms information */
926 if (!IFNET_IS_CELLULAR(bestinp
->inp_last_outifp
) &&
927 mptcp_is_wifi_unusable_for_session(mpte
) != 0) {
932 if (besttp
->t_srtt
&& secondtp
->t_srtt
&&
933 besttp
->t_srtt
>= rtt_thresh
&&
934 secondtp
->t_srtt
< rtt_thresh
) {
935 tcpstat
.tcps_mp_sel_rtt
++;
936 mptcplog((LOG_DEBUG
, "%s: best cid %d at rtt %d, second cid %d at rtt %d\n", __func__
,
937 best
->mpts_connid
, besttp
->t_srtt
>> TCP_RTT_SHIFT
,
938 second_best
->mpts_connid
,
939 secondtp
->t_srtt
>> TCP_RTT_SHIFT
),
940 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_LOG
);
941 return mptcp_return_subflow(second_best
);
944 if (mptcp_subflow_is_slow(mpte
, best
) &&
945 secondtp
->t_rxtshift
== 0) {
946 return mptcp_return_subflow(second_best
);
949 /* Compare RTOs, select second_best if best's rto exceeds rtothresh */
950 if (besttp
->t_rxtcur
&& secondtp
->t_rxtcur
&&
951 besttp
->t_rxtcur
>= rto_thresh
&&
952 secondtp
->t_rxtcur
< rto_thresh
) {
953 tcpstat
.tcps_mp_sel_rto
++;
954 mptcplog((LOG_DEBUG
, "%s: best cid %d at rto %d, second cid %d at rto %d\n", __func__
,
955 best
->mpts_connid
, besttp
->t_rxtcur
,
956 second_best
->mpts_connid
, secondtp
->t_rxtcur
),
957 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_LOG
);
959 return mptcp_return_subflow(second_best
);
963 * None of the above conditions for sending on the secondary
964 * were true. So, let's schedule on the best one, if he still
965 * has some space in the congestion-window.
967 return mptcp_return_subflow(best
);
968 } else if (mpte
->mpte_svctype
>= MPTCP_SVCTYPE_AGGREGATE
) {
972 * We only care about RTT when aggregating
974 if (besttp
->t_srtt
> secondtp
->t_srtt
) {
981 secondtp
= sototcpcb(second_best
->mpts_socket
);
982 secondinp
= sotoinpcb(second_best
->mpts_socket
);
985 /* Is there still space in the congestion window? */
986 if (mptcp_subflow_cwnd_space(bestinp
->inp_socket
) <= 0) {
987 return mptcp_return_subflow(second_best
);
990 return mptcp_return_subflow(best
);
992 panic("Unknown service-type configured for MPTCP");
999 mptcp_event_to_str(uint32_t event
)
1001 const char *c
= "UNDEFINED";
1006 case MPCE_RECV_DATA_ACK
:
1007 c
= "MPCE_RECV_DATA_ACK";
1009 case MPCE_RECV_DATA_FIN
:
1010 c
= "MPCE_RECV_DATA_FIN";
1017 mptcp_state_to_str(mptcp_state_t state
)
1019 const char *c
= "UNDEFINED";
1022 c
= "MPTCPS_CLOSED";
1025 c
= "MPTCPS_LISTEN";
1027 case MPTCPS_ESTABLISHED
:
1028 c
= "MPTCPS_ESTABLISHED";
1030 case MPTCPS_CLOSE_WAIT
:
1031 c
= "MPTCPS_CLOSE_WAIT";
1033 case MPTCPS_FIN_WAIT_1
:
1034 c
= "MPTCPS_FIN_WAIT_1";
1036 case MPTCPS_CLOSING
:
1037 c
= "MPTCPS_CLOSING";
1039 case MPTCPS_LAST_ACK
:
1040 c
= "MPTCPS_LAST_ACK";
1042 case MPTCPS_FIN_WAIT_2
:
1043 c
= "MPTCPS_FIN_WAIT_2";
1045 case MPTCPS_TIME_WAIT
:
1046 c
= "MPTCPS_TIME_WAIT";
1048 case MPTCPS_TERMINATE
:
1049 c
= "MPTCPS_TERMINATE";
1056 mptcp_close_fsm(struct mptcb
*mp_tp
, uint32_t event
)
1058 struct socket
*mp_so
= mptetoso(mp_tp
->mpt_mpte
);
1060 socket_lock_assert_owned(mp_so
);
1062 mptcp_state_t old_state
= mp_tp
->mpt_state
;
1064 DTRACE_MPTCP2(state__change
, struct mptcb
*, mp_tp
,
1067 switch (mp_tp
->mpt_state
) {
1070 mp_tp
->mpt_state
= MPTCPS_TERMINATE
;
1073 case MPTCPS_ESTABLISHED
:
1074 if (event
== MPCE_CLOSE
) {
1075 mp_tp
->mpt_state
= MPTCPS_FIN_WAIT_1
;
1076 mp_tp
->mpt_sndmax
+= 1; /* adjust for Data FIN */
1077 } else if (event
== MPCE_RECV_DATA_FIN
) {
1078 mp_tp
->mpt_rcvnxt
+= 1; /* adj remote data FIN */
1079 mp_tp
->mpt_state
= MPTCPS_CLOSE_WAIT
;
1083 case MPTCPS_CLOSE_WAIT
:
1084 if (event
== MPCE_CLOSE
) {
1085 mp_tp
->mpt_state
= MPTCPS_LAST_ACK
;
1086 mp_tp
->mpt_sndmax
+= 1; /* adjust for Data FIN */
1090 case MPTCPS_FIN_WAIT_1
:
1091 if (event
== MPCE_RECV_DATA_ACK
) {
1092 mp_tp
->mpt_state
= MPTCPS_FIN_WAIT_2
;
1093 } else if (event
== MPCE_RECV_DATA_FIN
) {
1094 mp_tp
->mpt_rcvnxt
+= 1; /* adj remote data FIN */
1095 mp_tp
->mpt_state
= MPTCPS_CLOSING
;
1099 case MPTCPS_CLOSING
:
1100 if (event
== MPCE_RECV_DATA_ACK
) {
1101 mp_tp
->mpt_state
= MPTCPS_TIME_WAIT
;
1105 case MPTCPS_LAST_ACK
:
1106 if (event
== MPCE_RECV_DATA_ACK
) {
1107 mptcp_close(mp_tp
->mpt_mpte
, mp_tp
);
1111 case MPTCPS_FIN_WAIT_2
:
1112 if (event
== MPCE_RECV_DATA_FIN
) {
1113 mp_tp
->mpt_rcvnxt
+= 1; /* adj remote data FIN */
1114 mp_tp
->mpt_state
= MPTCPS_TIME_WAIT
;
1118 case MPTCPS_TIME_WAIT
:
1119 case MPTCPS_TERMINATE
:
1126 DTRACE_MPTCP2(state__change
, struct mptcb
*, mp_tp
,
1128 mptcplog((LOG_INFO
, "%s: %s to %s on event %s\n", __func__
,
1129 mptcp_state_to_str(old_state
),
1130 mptcp_state_to_str(mp_tp
->mpt_state
),
1131 mptcp_event_to_str(event
)),
1132 MPTCP_STATE_DBG
, MPTCP_LOGLVL_LOG
);
1135 /* If you change this function, match up mptcp_update_rcv_state_f */
1137 mptcp_update_dss_rcv_state(struct mptcp_dsn_opt
*dss_info
, struct tcpcb
*tp
,
1140 struct mptcb
*mp_tp
= tptomptp(tp
);
1141 u_int64_t full_dsn
= 0;
1143 NTOHL(dss_info
->mdss_dsn
);
1144 NTOHL(dss_info
->mdss_subflow_seqn
);
1145 NTOHS(dss_info
->mdss_data_len
);
1147 /* XXX for autosndbuf grow sb here */
1148 MPTCP_EXTEND_DSN(mp_tp
->mpt_rcvnxt
, dss_info
->mdss_dsn
, full_dsn
);
1149 mptcp_update_rcv_state_meat(mp_tp
, tp
,
1150 full_dsn
, dss_info
->mdss_subflow_seqn
, dss_info
->mdss_data_len
,
1155 mptcp_update_rcv_state_meat(struct mptcb
*mp_tp
, struct tcpcb
*tp
,
1156 u_int64_t full_dsn
, u_int32_t seqn
, u_int16_t mdss_data_len
,
1159 if (mdss_data_len
== 0) {
1160 os_log_error(mptcp_log_handle
, "%s - %lx: Infinite Mapping.\n",
1161 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mp_tp
->mpt_mpte
));
1163 if ((mp_tp
->mpt_flags
& MPTCPF_CHECKSUM
) && (csum
!= 0)) {
1164 os_log_error(mptcp_log_handle
, "%s - %lx: Bad checksum %x \n",
1165 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mp_tp
->mpt_mpte
), csum
);
1167 mptcp_notify_mpfail(tp
->t_inpcb
->inp_socket
);
1171 mptcp_notify_mpready(tp
->t_inpcb
->inp_socket
);
1173 tp
->t_rcv_map
.mpt_dsn
= full_dsn
;
1174 tp
->t_rcv_map
.mpt_sseq
= seqn
;
1175 tp
->t_rcv_map
.mpt_len
= mdss_data_len
;
1176 tp
->t_rcv_map
.mpt_csum
= csum
;
1177 tp
->t_mpflags
|= TMPF_EMBED_DSN
;
1182 mptcp_validate_dss_map(struct socket
*so
, struct tcpcb
*tp
, struct mbuf
*m
,
1187 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
)) {
1191 datalen
= m
->m_pkthdr
.mp_rlen
;
1193 /* unacceptable DSS option, fallback to TCP */
1194 if (m
->m_pkthdr
.len
> ((int) datalen
+ hdrlen
)) {
1195 os_log_error(mptcp_log_handle
, "%s - %lx: mbuf len %d, MPTCP expected %d",
1196 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(tptomptp(tp
)->mpt_mpte
), m
->m_pkthdr
.len
, datalen
);
1200 tp
->t_mpflags
|= TMPF_SND_MPFAIL
;
1201 mptcp_notify_mpfail(so
);
1207 mptcp_input_preproc(struct tcpcb
*tp
, struct mbuf
*m
, struct tcphdr
*th
,
1210 mptcp_insert_rmap(tp
, m
, th
);
1211 if (mptcp_validate_dss_map(tp
->t_inpcb
->inp_socket
, tp
, m
,
1212 drop_hdrlen
) != 0) {
1219 mptcp_input_csum(struct tcpcb
*tp
, struct mbuf
*m
, uint64_t dsn
, uint32_t sseq
,
1220 uint16_t dlen
, uint16_t csum
, int dfin
)
1222 struct mptcb
*mp_tp
= tptomptp(tp
);
1223 int real_len
= dlen
- dfin
;
1226 VERIFY(real_len
>= 0);
1228 if (mp_tp
== NULL
) {
1232 if (!(mp_tp
->mpt_flags
& MPTCPF_CHECKSUM
)) {
1236 if (tp
->t_mpflags
& TMPF_TCP_FALLBACK
) {
1241 * The remote side may send a packet with fewer bytes than the
1242 * claimed DSS checksum length.
1244 if ((int)m_length2(m
, NULL
) < real_len
) {
1248 if (real_len
!= 0) {
1249 sum
= m_sum16(m
, 0, real_len
);
1252 sum
+= in_pseudo64(htonll(dsn
), htonl(sseq
), htons(dlen
) + csum
);
1255 DTRACE_MPTCP3(checksum__result
, struct tcpcb
*, tp
, struct mbuf
*, m
,
1258 return ~sum
& 0xffff;
1262 * MPTCP Checksum support
1263 * The checksum is calculated whenever the MPTCP DSS option is included
1264 * in the TCP packet. The checksum includes the sum of the MPTCP psuedo
1265 * header and the actual data indicated by the length specified in the
1270 mptcp_validate_csum(struct tcpcb
*tp
, struct mbuf
*m
, uint64_t dsn
,
1271 uint32_t sseq
, uint16_t dlen
, uint16_t csum
, int dfin
)
1273 uint16_t mptcp_csum
;
1275 mptcp_csum
= mptcp_input_csum(tp
, m
, dsn
, sseq
, dlen
, csum
, dfin
);
1277 tp
->t_mpflags
|= TMPF_SND_MPFAIL
;
1278 mptcp_notify_mpfail(tp
->t_inpcb
->inp_socket
);
1280 tcpstat
.tcps_mp_badcsum
++;
1287 mptcp_output_csum(struct mbuf
*m
, uint64_t dss_val
, uint32_t sseq
, uint16_t dlen
)
1292 sum
= m_sum16(m
, 0, dlen
);
1295 dss_val
= mptcp_hton64(dss_val
);
1298 sum
+= in_pseudo64(dss_val
, sseq
, dlen
);
1301 sum
= ~sum
& 0xffff;
1302 DTRACE_MPTCP2(checksum__result
, struct mbuf
*, m
, uint32_t, sum
);
1303 mptcplog((LOG_DEBUG
, "%s: sum = %x \n", __func__
, sum
),
1304 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
1306 return (uint16_t)sum
;
1310 * When WiFi signal starts fading, there's more loss and RTT spikes.
1311 * Check if there has been a large spike by comparing against
1312 * a tolerable RTT spike threshold.
1315 mptcp_no_rto_spike(struct socket
*so
)
1317 struct tcpcb
*tp
= intotcpcb(sotoinpcb(so
));
1320 if (tp
->t_rxtcur
> mptcp_rtothresh
) {
1321 spike
= tp
->t_rxtcur
- mptcp_rtothresh
;
1323 mptcplog((LOG_DEBUG
, "%s: spike = %d rto = %d best = %d cur = %d\n",
1325 tp
->t_rxtcur
, tp
->t_rttbest
>> TCP_RTT_SHIFT
,
1327 (MPTCP_SOCKET_DBG
| MPTCP_SENDER_DBG
), MPTCP_LOGLVL_LOG
);
1338 mptcp_handle_deferred_upcalls(struct mppcb
*mpp
, uint32_t flag
)
1340 VERIFY(mpp
->mpp_flags
& flag
);
1341 mpp
->mpp_flags
&= ~flag
;
1343 if (mptcp_should_defer_upcall(mpp
)) {
1347 if (mpp
->mpp_flags
& MPP_SHOULD_WORKLOOP
) {
1348 mpp
->mpp_flags
&= ~MPP_SHOULD_WORKLOOP
;
1350 mptcp_subflow_workloop(mpp
->mpp_pcbe
);
1353 if (mpp
->mpp_flags
& MPP_SHOULD_RWAKEUP
) {
1354 mpp
->mpp_flags
&= ~MPP_SHOULD_RWAKEUP
;
1356 sorwakeup(mpp
->mpp_socket
);
1359 if (mpp
->mpp_flags
& MPP_SHOULD_WWAKEUP
) {
1360 mpp
->mpp_flags
&= ~MPP_SHOULD_WWAKEUP
;
1362 sowwakeup(mpp
->mpp_socket
);
1367 mptcp_ask_for_nat64(struct ifnet
*ifp
)
1369 in6_post_msg(ifp
, KEV_INET6_REQUEST_NAT64_PREFIX
, NULL
, NULL
);
1371 os_log_info(mptcp_log_handle
,
1372 "%s: asked for NAT64-prefix on %s\n", __func__
,
1377 mptcp_reset_itfinfo(struct mpt_itf_info
*info
)
1379 memset(info
, 0, sizeof(*info
));
1383 mptcp_session_necp_cb(void *handle
, int action
, uint32_t interface_index
,
1384 uint32_t necp_flags
, __unused
bool *viable
)
1386 boolean_t has_v4
= !!(necp_flags
& NECP_CLIENT_RESULT_FLAG_HAS_IPV4
);
1387 boolean_t has_v6
= !!(necp_flags
& NECP_CLIENT_RESULT_FLAG_HAS_IPV6
);
1388 boolean_t has_nat64
= !!(necp_flags
& NECP_CLIENT_RESULT_FLAG_HAS_NAT64
);
1389 boolean_t low_power
= !!(necp_flags
& NECP_CLIENT_RESULT_FLAG_INTERFACE_LOW_POWER
);
1390 struct mppcb
*mp
= (struct mppcb
*)handle
;
1391 struct mptses
*mpte
= mptompte(mp
);
1392 struct socket
*mp_so
;
1393 struct mptcb
*mp_tp
;
1394 uint32_t i
, ifindex
;
1398 ifindex
= interface_index
;
1399 VERIFY(ifindex
!= IFSCOPE_NONE
);
1401 /* About to be garbage-collected (see note about MPTCP/NECP interactions) */
1402 if (mp
->mpp_socket
->so_usecount
== 0) {
1406 mp_so
= mptetoso(mpte
);
1408 if (action
!= NECP_CLIENT_CBACTION_INITIAL
) {
1409 socket_lock(mp_so
, 1);
1412 /* Check again, because it might have changed while waiting */
1413 if (mp
->mpp_socket
->so_usecount
== 0) {
1418 socket_lock_assert_owned(mp_so
);
1420 mp_tp
= mpte
->mpte_mptcb
;
1422 ifnet_head_lock_shared();
1423 ifp
= ifindex2ifnet
[ifindex
];
1426 os_log(mptcp_log_handle
, "%s - %lx: action: %u ifindex %u delegated to %u usecount %u mpt_flags %#x state %u v4 %u v6 %u nat64 %u power %u\n",
1427 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), action
, ifindex
,
1428 ifp
&& ifp
->if_delegated
.ifp
? ifp
->if_delegated
.ifp
->if_index
: IFSCOPE_NONE
,
1429 mp
->mpp_socket
->so_usecount
, mp_tp
->mpt_flags
, mp_tp
->mpt_state
,
1430 has_v4
, has_v6
, has_nat64
, low_power
);
1432 /* No need on fallen back sockets */
1433 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
1438 * When the interface goes in low-power mode we don't want to establish
1439 * new subflows on it. Thus, mark it internally as non-viable.
1442 action
= NECP_CLIENT_CBACTION_NONVIABLE
;
1445 if (action
== NECP_CLIENT_CBACTION_NONVIABLE
) {
1446 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
1447 if (mpte
->mpte_itfinfo
[i
].ifindex
== IFSCOPE_NONE
) {
1451 if (mpte
->mpte_itfinfo
[i
].ifindex
== ifindex
) {
1452 mptcp_reset_itfinfo(&mpte
->mpte_itfinfo
[i
]);
1456 mptcp_sched_create_subflows(mpte
);
1457 } else if (action
== NECP_CLIENT_CBACTION_VIABLE
||
1458 action
== NECP_CLIENT_CBACTION_INITIAL
) {
1459 int found_slot
= 0, slot_index
= -1;
1460 struct sockaddr
*dst
;
1466 if (IFNET_IS_COMPANION_LINK(ifp
)) {
1470 if (IFNET_IS_EXPENSIVE(ifp
) &&
1471 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_EXPENSIVE
)) {
1475 if (IFNET_IS_CONSTRAINED(ifp
) &&
1476 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_CONSTRAINED
)) {
1480 if (IFNET_IS_CELLULAR(ifp
) &&
1481 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_CELLULAR
)) {
1485 if (IS_INTF_CLAT46(ifp
)) {
1489 /* Look for the slot on where to store/update the interface-info. */
1490 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
1491 /* Found a potential empty slot where we can put it */
1492 if (mpte
->mpte_itfinfo
[i
].ifindex
== 0) {
1498 * The interface is already in our array. Check if we
1499 * need to update it.
1501 if (mpte
->mpte_itfinfo
[i
].ifindex
== ifindex
&&
1502 (mpte
->mpte_itfinfo
[i
].has_v4_conn
!= has_v4
||
1503 mpte
->mpte_itfinfo
[i
].has_v6_conn
!= has_v6
||
1504 mpte
->mpte_itfinfo
[i
].has_nat64_conn
!= has_nat64
)) {
1510 if (mpte
->mpte_itfinfo
[i
].ifindex
== ifindex
) {
1512 * Ok, it's already there and we don't need
1519 dst
= mptcp_get_session_dst(mpte
, has_v6
, has_v4
);
1520 if (dst
&& (dst
->sa_family
== AF_INET
|| dst
->sa_family
== 0) &&
1521 has_v6
&& !has_nat64
&& !has_v4
) {
1523 mpte
->mpte_itfinfo
[slot_index
].ifindex
= ifindex
;
1524 mpte
->mpte_itfinfo
[slot_index
].has_v4_conn
= has_v4
;
1525 mpte
->mpte_itfinfo
[slot_index
].has_v6_conn
= has_v6
;
1526 mpte
->mpte_itfinfo
[slot_index
].has_nat64_conn
= has_nat64
;
1528 mptcp_ask_for_nat64(ifp
);
1532 if (found_slot
== 0) {
1533 int new_size
= mpte
->mpte_itfinfo_size
* 2;
1534 struct mpt_itf_info
*info
= _MALLOC(sizeof(*info
) * new_size
, M_TEMP
, M_ZERO
);
1537 os_log_error(mptcp_log_handle
, "%s - %lx: malloc failed for %u\n",
1538 __func__
, (unsigned long)VM_KERNEL_ADDRPERM(mpte
), new_size
);
1542 memcpy(info
, mpte
->mpte_itfinfo
, mpte
->mpte_itfinfo_size
* sizeof(*info
));
1544 if (mpte
->mpte_itfinfo_size
> MPTE_ITFINFO_SIZE
) {
1545 _FREE(mpte
->mpte_itfinfo
, M_TEMP
);
1548 /* We allocated a new one, thus the first must be empty */
1549 slot_index
= mpte
->mpte_itfinfo_size
;
1551 mpte
->mpte_itfinfo
= info
;
1552 mpte
->mpte_itfinfo_size
= new_size
;
1555 VERIFY(slot_index
>= 0 && slot_index
< (int)mpte
->mpte_itfinfo_size
);
1556 mpte
->mpte_itfinfo
[slot_index
].ifindex
= ifindex
;
1557 mpte
->mpte_itfinfo
[slot_index
].has_v4_conn
= has_v4
;
1558 mpte
->mpte_itfinfo
[slot_index
].has_v6_conn
= has_v6
;
1559 mpte
->mpte_itfinfo
[slot_index
].has_nat64_conn
= has_nat64
;
1561 mptcp_sched_create_subflows(mpte
);
1566 socket_unlock(mp_so
, 1);
1571 mptcp_set_restrictions(struct socket
*mp_so
)
1573 struct mptses
*mpte
= mpsotompte(mp_so
);
1576 socket_lock_assert_owned(mp_so
);
1578 ifnet_head_lock_shared();
1580 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
1581 struct mpt_itf_info
*info
= &mpte
->mpte_itfinfo
[i
];
1582 uint32_t ifindex
= info
->ifindex
;
1585 if (ifindex
== IFSCOPE_NONE
) {
1589 ifp
= ifindex2ifnet
[ifindex
];
1594 if (IFNET_IS_EXPENSIVE(ifp
) &&
1595 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_EXPENSIVE
)) {
1596 info
->ifindex
= IFSCOPE_NONE
;
1599 if (IFNET_IS_CONSTRAINED(ifp
) &&
1600 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_CONSTRAINED
)) {
1601 info
->ifindex
= IFSCOPE_NONE
;
1604 if (IFNET_IS_CELLULAR(ifp
) &&
1605 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_CELLULAR
)) {
1606 info
->ifindex
= IFSCOPE_NONE
;