2 * Copyright (c) 2012-2017 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");
114 /* Number of times to try negotiating MPTCP on SYN retransmissions */
115 int mptcp_mpcap_retries
= MPTCP_CAPABLE_RETRIES
;
116 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, mptcp_cap_retr
,
117 CTLFLAG_RW
| CTLFLAG_LOCKED
,
118 &mptcp_mpcap_retries
, 0, "Number of MP Capable SYN Retries");
121 * By default, DSS checksum is turned off, revisit if we ever do
122 * MPTCP for non SSL Traffic.
124 int mptcp_dss_csum
= 0;
125 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, dss_csum
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
126 &mptcp_dss_csum
, 0, "Enable DSS checksum");
129 * When mptcp_fail_thresh number of retransmissions are sent, subflow failover
130 * is attempted on a different path.
132 int mptcp_fail_thresh
= 1;
133 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, fail
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
134 &mptcp_fail_thresh
, 0, "Failover threshold");
138 * MPTCP subflows have TCP keepalives set to ON. Set a conservative keeptime
139 * as carrier networks mostly have a 30 minute to 60 minute NAT Timeout.
140 * Some carrier networks have a timeout of 10 or 15 minutes.
142 int mptcp_subflow_keeptime
= 60*14;
143 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, keepalive
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
144 &mptcp_subflow_keeptime
, 0, "Keepalive in seconds");
146 int mptcp_rtthist_rtthresh
= 600;
147 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, rtthist_thresh
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
148 &mptcp_rtthist_rtthresh
, 0, "Rtt threshold");
151 * Use RTO history for sending new data
153 int mptcp_use_rto
= 1;
154 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, userto
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
155 &mptcp_use_rto
, 0, "Disable RTO for subflow selection");
157 int mptcp_rtothresh
= 1500;
158 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, rto_thresh
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
159 &mptcp_rtothresh
, 0, "RTO threshold");
162 * Probe the preferred path, when it is not in use
164 uint32_t mptcp_probeto
= 1000;
165 SYSCTL_UINT(_net_inet_mptcp
, OID_AUTO
, probeto
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
166 &mptcp_probeto
, 0, "Disable probing by setting to 0");
168 uint32_t mptcp_probecnt
= 5;
169 SYSCTL_UINT(_net_inet_mptcp
, OID_AUTO
, probecnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
170 &mptcp_probecnt
, 0, "Number of probe writes");
173 * Static declarations
175 static uint16_t mptcp_input_csum(struct tcpcb
*, struct mbuf
*, uint64_t,
176 uint32_t, uint16_t, uint16_t);
179 mptcp_reass_present(struct socket
*mp_so
)
181 struct mptcb
*mp_tp
= mpsotomppcb(mp_so
)->mpp_pcbe
->mpte_mptcb
;
186 * Present data to user, advancing rcv_nxt through
187 * completed sequence space.
189 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
)
191 q
= LIST_FIRST(&mp_tp
->mpt_segq
);
192 if (!q
|| q
->tqe_m
->m_pkthdr
.mp_dsn
!= mp_tp
->mpt_rcvnxt
)
196 * If there is already another thread doing reassembly for this
197 * connection, it is better to let it finish the job --
200 if (mp_tp
->mpt_flags
& MPTCPF_REASS_INPROG
)
203 mp_tp
->mpt_flags
|= MPTCPF_REASS_INPROG
;
206 mp_tp
->mpt_rcvnxt
+= q
->tqe_len
;
207 LIST_REMOVE(q
, tqe_q
);
208 if (mp_so
->so_state
& SS_CANTRCVMORE
) {
211 if (sbappendstream(&mp_so
->so_rcv
, q
->tqe_m
))
214 zfree(tcp_reass_zone
, q
);
215 mp_tp
->mpt_reassqlen
--;
216 q
= LIST_FIRST(&mp_tp
->mpt_segq
);
217 } while (q
&& q
->tqe_m
->m_pkthdr
.mp_dsn
== mp_tp
->mpt_rcvnxt
);
218 mp_tp
->mpt_flags
&= ~MPTCPF_REASS_INPROG
;
221 sorwakeup(mp_so
); /* done with socket lock held */
227 mptcp_reass(struct socket
*mp_so
, struct pkthdr
*phdr
, int *tlenp
, struct mbuf
*m
)
229 struct mptcb
*mp_tp
= mpsotomppcb(mp_so
)->mpp_pcbe
->mpte_mptcb
;
230 u_int64_t mb_dsn
= phdr
->mp_dsn
;
232 struct tseg_qent
*p
= NULL
;
233 struct tseg_qent
*nq
;
234 struct tseg_qent
*te
= NULL
;
238 * Limit the number of segments in the reassembly queue to prevent
239 * holding on to too many segments (and thus running out of mbufs).
240 * Make sure to let the missing segment through which caused this
241 * queue. Always keep one global queue entry spare to be able to
242 * process the missing segment.
244 qlimit
= min(max(100, mp_so
->so_rcv
.sb_hiwat
>> 10),
245 (tcp_autorcvbuf_max
>> 10));
246 if (mb_dsn
!= mp_tp
->mpt_rcvnxt
&&
247 (mp_tp
->mpt_reassqlen
+ 1) >= qlimit
) {
248 tcpstat
.tcps_mptcp_rcvmemdrop
++;
254 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
255 te
= (struct tseg_qent
*) zalloc(tcp_reass_zone
);
257 tcpstat
.tcps_mptcp_rcvmemdrop
++;
262 mp_tp
->mpt_reassqlen
++;
265 * Find a segment which begins after this one does.
267 LIST_FOREACH(q
, &mp_tp
->mpt_segq
, tqe_q
) {
268 if (MPTCP_SEQ_GT(q
->tqe_m
->m_pkthdr
.mp_dsn
, mb_dsn
))
274 * If there is a preceding segment, it may provide some of
275 * our data already. If so, drop the data from the incoming
276 * segment. If it provides all of our data, drop us.
280 /* conversion to int (in i) handles seq wraparound */
281 i
= p
->tqe_m
->m_pkthdr
.mp_dsn
+ p
->tqe_len
- mb_dsn
;
284 tcpstat
.tcps_mptcp_rcvduppack
++;
286 zfree(tcp_reass_zone
, te
);
288 mp_tp
->mpt_reassqlen
--;
290 * Try to present any queued data
291 * at the left window edge to the user.
292 * This is needed after the 3-WHS
303 tcpstat
.tcps_mp_oodata
++;
306 * While we overlap succeeding segments trim them or,
307 * if they are completely covered, dequeue them.
310 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
;
321 nq
= LIST_NEXT(q
, tqe_q
);
322 LIST_REMOVE(q
, tqe_q
);
324 zfree(tcp_reass_zone
, q
);
325 mp_tp
->mpt_reassqlen
--;
329 /* Insert the new segment queue entry into place. */
332 te
->tqe_len
= *tlenp
;
335 LIST_INSERT_HEAD(&mp_tp
->mpt_segq
, te
, tqe_q
);
337 LIST_INSERT_AFTER(p
, te
, tqe_q
);
341 return (mptcp_reass_present(mp_so
));
345 * MPTCP input, called when data has been read from a subflow socket.
348 mptcp_input(struct mptses
*mpte
, struct mbuf
*m
)
350 struct socket
*mp_so
;
351 struct mptcb
*mp_tp
= NULL
;
352 int count
= 0, wakeup
= 0;
353 struct mbuf
*save
= NULL
, *prev
= NULL
;
354 struct mbuf
*freelist
= NULL
, *tail
= NULL
;
356 VERIFY(m
->m_flags
& M_PKTHDR
);
358 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
360 mp_so
= mptetoso(mpte
);
361 mp_tp
= mpte
->mpte_mptcb
;
365 mp_tp
->mpt_rcvwnd
= mptcp_sbspace(mp_tp
);
368 * Each mbuf contains MPTCP Data Sequence Map
369 * Process the data for reassembly, delivery to MPTCP socket
373 count
= mp_so
->so_rcv
.sb_cc
;
376 * In the degraded fallback case, data is accepted without DSS map
378 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
380 mptcp_sbrcv_grow(mp_tp
);
383 * assume degraded flow as this may be the first packet
384 * without DSS, and the subflow state is not updated yet.
386 if (sbappendstream(&mp_so
->so_rcv
, m
))
388 DTRACE_MPTCP5(receive__degraded
, struct mbuf
*, m
,
389 struct socket
*, mp_so
,
390 struct sockbuf
*, &mp_so
->so_rcv
,
391 struct sockbuf
*, &mp_so
->so_snd
,
392 struct mptses
*, mpte
);
393 count
= mp_so
->so_rcv
.sb_cc
- count
;
394 mptcplog((LOG_DEBUG
, "%s: Fallback read %d bytes\n", __func__
,
395 count
), MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_VERBOSE
);
404 /* If fallback occurs, mbufs will not have PKTF_MPTCP set */
405 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
))
410 * A single TCP packet formed of multiple mbufs
411 * holds DSS mapping in the first mbuf of the chain.
412 * Other mbufs in the chain may have M_PKTHDR set
413 * even though they belong to the same TCP packet
414 * and therefore use the DSS mapping stored in the
415 * first mbuf of the mbuf chain. mptcp_input() can
416 * get an mbuf chain with multiple TCP packets.
418 while (save
&& (!(save
->m_flags
& M_PKTHDR
) ||
419 !(save
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
))) {
428 mb_dsn
= m
->m_pkthdr
.mp_dsn
;
429 mb_datalen
= m
->m_pkthdr
.mp_rlen
;
431 todrop
= (mb_dsn
+ mb_datalen
) - (mp_tp
->mpt_rcvnxt
+ mp_tp
->mpt_rcvwnd
);
433 tcpstat
.tcps_mptcp_rcvpackafterwin
++;
435 if (todrop
>= mb_datalen
) {
436 if (freelist
== NULL
)
451 mb_datalen
-= todrop
;
455 if (MPTCP_SEQ_GT(mb_dsn
, mp_tp
->mpt_rcvnxt
) ||
456 !LIST_EMPTY(&mp_tp
->mpt_segq
)) {
457 mptcp_reass(mp_so
, &m
->m_pkthdr
, &mb_datalen
, m
);
462 if (MPTCP_SEQ_LT(mb_dsn
, mp_tp
->mpt_rcvnxt
)) {
463 if (MPTCP_SEQ_LEQ((mb_dsn
+ mb_datalen
),
464 mp_tp
->mpt_rcvnxt
)) {
465 if (freelist
== NULL
)
479 m_adj(m
, (mp_tp
->mpt_rcvnxt
- mb_dsn
));
481 mptcplog((LOG_INFO
, "%s: Left Edge %llu\n", __func__
,
483 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_VERBOSE
);
486 mptcp_sbrcv_grow(mp_tp
);
488 if (sbappendstream(&mp_so
->so_rcv
, m
))
491 DTRACE_MPTCP6(receive
, struct mbuf
*, m
, struct socket
*, mp_so
,
492 struct sockbuf
*, &mp_so
->so_rcv
,
493 struct sockbuf
*, &mp_so
->so_snd
,
494 struct mptses
*, mpte
,
495 struct mptcb
*, mp_tp
);
496 count
= mp_so
->so_rcv
.sb_cc
- count
;
497 tcpstat
.tcps_mp_rcvtotal
++;
498 tcpstat
.tcps_mp_rcvbytes
+= count
;
499 mptcplog((LOG_DEBUG
, "%s: Read %d bytes\n", __func__
, count
),
500 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_VERBOSE
);
502 mp_tp
->mpt_rcvnxt
+= count
;
507 count
= mp_so
->so_rcv
.sb_cc
;
518 mptcp_can_send_more(struct mptcb
*mp_tp
)
520 struct socket
*mp_so
= mptetoso(mp_tp
->mpt_mpte
);
523 * Always send if there is data in the reinject-queue.
525 if (mp_tp
->mpt_mpte
->mpte_reinjectq
)
531 * 1. snd_nxt >= snd_max : Means, basically everything has been sent.
532 * Except when using TFO, we might be doing a 0-byte write.
533 * 2. snd_una + snd_wnd <= snd_nxt: No space in the receiver's window
534 * 3. snd_nxt + 1 == snd_max and we are closing: A DATA_FIN is scheduled.
537 if (!(mp_so
->so_flags1
& SOF1_PRECONNECT_DATA
) && MPTCP_SEQ_GEQ(mp_tp
->mpt_sndnxt
, mp_tp
->mpt_sndmax
))
540 if (MPTCP_SEQ_LEQ(mp_tp
->mpt_snduna
+ mp_tp
->mpt_sndwnd
, mp_tp
->mpt_sndnxt
))
543 if (mp_tp
->mpt_sndnxt
+ 1 == mp_tp
->mpt_sndmax
&& mp_tp
->mpt_state
> MPTCPS_CLOSE_WAIT
)
546 if (mp_tp
->mpt_state
>= MPTCPS_FIN_WAIT_2
)
556 mptcp_output(struct mptses
*mpte
)
560 struct mptsub
*mpts_tried
= NULL
;
561 struct socket
*mp_so
;
562 struct mptsub
*preferred_mpts
= NULL
;
563 uint64_t old_snd_nxt
;
566 mpte_lock_assert_held(mpte
);
567 mp_so
= mptetoso(mpte
);
568 mp_tp
= mpte
->mpte_mptcb
;
570 VERIFY(!(mpte
->mpte_mppcb
->mpp_flags
& MPP_WUPCALL
));
571 mpte
->mpte_mppcb
->mpp_flags
|= MPP_WUPCALL
;
573 mptcplog((LOG_DEBUG
, "%s: snxt %u sndmax %u suna %u swnd %u reinjectq %u state %u\n",
574 __func__
, (uint32_t)mp_tp
->mpt_sndnxt
, (uint32_t)mp_tp
->mpt_sndmax
,
575 (uint32_t)mp_tp
->mpt_snduna
, mp_tp
->mpt_sndwnd
,
576 mpte
->mpte_reinjectq
? 1 : 0,
578 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
580 old_snd_nxt
= mp_tp
->mpt_sndnxt
;
581 while (mptcp_can_send_more(mp_tp
)) {
582 /* get the "best" subflow to be used for transmission */
583 mpts
= mptcp_get_subflow(mpte
, NULL
, &preferred_mpts
);
585 mptcplog((LOG_INFO
, "%s: no subflow\n", __func__
),
586 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_LOG
);
590 mptcplog((LOG_DEBUG
, "%s: using id %u\n", __func__
, mpts
->mpts_connid
),
591 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
593 /* In case there's just one flow, we reattempt later */
594 if (mpts_tried
!= NULL
&&
595 (mpts
== mpts_tried
|| (mpts
->mpts_flags
& MPTSF_FAILINGOVER
))) {
596 mpts_tried
->mpts_flags
&= ~MPTSF_FAILINGOVER
;
597 mpts_tried
->mpts_flags
|= MPTSF_ACTIVE
;
598 mptcp_start_timer(mpte
, MPTT_REXMT
);
599 mptcplog((LOG_DEBUG
, "%s: retry later\n", __func__
),
600 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
605 * Automatic sizing of send socket buffer. Increase the send
606 * socket buffer size if all of the following criteria are met
607 * 1. the receiver has enough buffer space for this data
608 * 2. send buffer is filled to 7/8th with data (so we actually
609 * have data to make use of it);
611 if (tcp_do_autosendbuf
== 1 &&
612 (mp_so
->so_snd
.sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) == SB_AUTOSIZE
&&
613 tcp_cansbgrow(&mp_so
->so_snd
)) {
614 if ((mp_tp
->mpt_sndwnd
/ 4 * 5) >= mp_so
->so_snd
.sb_hiwat
&&
615 mp_so
->so_snd
.sb_cc
>= (mp_so
->so_snd
.sb_hiwat
/ 8 * 7)) {
616 if (sbreserve(&mp_so
->so_snd
,
617 min(mp_so
->so_snd
.sb_hiwat
+ tcp_autosndbuf_inc
,
618 tcp_autosndbuf_max
)) == 1) {
619 mp_so
->so_snd
.sb_idealsize
= mp_so
->so_snd
.sb_hiwat
;
621 mptcplog((LOG_DEBUG
, "%s: increased snd hiwat to %u lowat %u\n",
622 __func__
, mp_so
->so_snd
.sb_hiwat
,
623 mp_so
->so_snd
.sb_lowat
),
624 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
629 DTRACE_MPTCP3(output
, struct mptses
*, mpte
, struct mptsub
*, mpts
,
630 struct socket
*, mp_so
);
631 error
= mptcp_subflow_output(mpte
, mpts
, 0);
633 /* can be a temporary loss of source address or other error */
634 mpts
->mpts_flags
|= MPTSF_FAILINGOVER
;
635 mpts
->mpts_flags
&= ~MPTSF_ACTIVE
;
637 mptcplog((LOG_ERR
, "%s: Error = %d mpts_flags %#x\n", __func__
,
638 error
, mpts
->mpts_flags
),
639 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_ERR
);
642 /* The model is to have only one active flow at a time */
643 mpts
->mpts_flags
|= MPTSF_ACTIVE
;
644 mpts
->mpts_probesoon
= mpts
->mpts_probecnt
= 0;
646 /* Allows us to update the smoothed rtt */
647 if (mptcp_probeto
&& mpts
!= preferred_mpts
&& preferred_mpts
!= NULL
) {
648 if (preferred_mpts
->mpts_probesoon
) {
649 if ((tcp_now
- preferred_mpts
->mpts_probesoon
) > mptcp_probeto
) {
650 mptcp_subflow_output(mpte
, preferred_mpts
, MPTCP_SUBOUT_PROBING
);
651 if (preferred_mpts
->mpts_probecnt
>= mptcp_probecnt
) {
652 preferred_mpts
->mpts_probesoon
= 0;
653 preferred_mpts
->mpts_probecnt
= 0;
657 preferred_mpts
->mpts_probesoon
= tcp_now
;
658 preferred_mpts
->mpts_probecnt
= 0;
662 if (mpte
->mpte_active_sub
== NULL
) {
663 mpte
->mpte_active_sub
= mpts
;
664 } else if (mpte
->mpte_active_sub
!= mpts
) {
665 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
666 struct tcpcb
*acttp
= sototcpcb(mpte
->mpte_active_sub
->mpts_socket
);
668 mptcplog((LOG_DEBUG
, "%s: switch [%u, srtt %d] to [%u, srtt %d]\n", __func__
,
669 mpte
->mpte_active_sub
->mpts_connid
, acttp
->t_srtt
>> TCP_RTT_SHIFT
,
670 mpts
->mpts_connid
, tp
->t_srtt
>> TCP_RTT_SHIFT
),
671 (MPTCP_SENDER_DBG
| MPTCP_SOCKET_DBG
), MPTCP_LOGLVL_LOG
);
673 mpte
->mpte_active_sub
->mpts_flags
&= ~MPTSF_ACTIVE
;
674 mpte
->mpte_active_sub
= mpts
;
676 mptcpstats_inc_switch(mpte
, mpts
);
680 mptcp_handle_deferred_upcalls(mpte
->mpte_mppcb
, MPP_WUPCALL
);
682 /* subflow errors should not be percolated back up */
687 static struct mptsub
*
688 mptcp_choose_subflow(struct mptsub
*mpts
, struct mptsub
*curbest
, int *currtt
)
690 struct tcpcb
*tp
= sototcpcb(mpts
->mpts_socket
);
693 * Lower RTT? Take it, if it's our first one, or
694 * it doesn't has any loss, or the current one has
697 if (tp
->t_srtt
&& *currtt
> tp
->t_srtt
&&
698 (curbest
== NULL
|| tp
->t_rxtshift
== 0 ||
699 sototcpcb(curbest
->mpts_socket
)->t_rxtshift
)) {
700 *currtt
= tp
->t_srtt
;
705 * If we find a subflow without loss, take it always!
708 sototcpcb(curbest
->mpts_socket
)->t_rxtshift
&&
709 tp
->t_rxtshift
== 0) {
710 *currtt
= tp
->t_srtt
;
714 return (curbest
!= NULL
? curbest
: mpts
);
717 static struct mptsub
*
718 mptcp_return_subflow(struct mptsub
*mpts
)
720 if (mpts
&& mptcp_subflow_cwnd_space(mpts
->mpts_socket
) <= 0)
727 * Return the most eligible subflow to be used for sending data.
730 mptcp_get_subflow(struct mptses
*mpte
, struct mptsub
*ignore
, struct mptsub
**preferred
)
732 struct tcpcb
*besttp
, *secondtp
;
733 struct inpcb
*bestinp
, *secondinp
;
735 struct mptsub
*best
= NULL
;
736 struct mptsub
*second_best
= NULL
;
737 int exp_rtt
= INT_MAX
, cheap_rtt
= INT_MAX
;
741 * Choose the best subflow for cellular and non-cellular interfaces.
744 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
745 struct socket
*so
= mpts
->mpts_socket
;
746 struct tcpcb
*tp
= sototcpcb(so
);
747 struct inpcb
*inp
= sotoinpcb(so
);
749 mptcplog((LOG_DEBUG
, "%s mpts %u ignore %d, mpts_flags %#x, suspended %u sostate %#x tpstate %u cellular %d rtt %u rxtshift %u cheap %u exp %u cwnd %d\n",
750 __func__
, mpts
->mpts_connid
, ignore
? ignore
->mpts_connid
: -1, mpts
->mpts_flags
,
751 INP_WAIT_FOR_IF_FEEDBACK(inp
), so
->so_state
, tp
->t_state
,
752 inp
->inp_last_outifp
? IFNET_IS_CELLULAR(inp
->inp_last_outifp
) : -1,
753 tp
->t_srtt
, tp
->t_rxtshift
, cheap_rtt
, exp_rtt
,
754 mptcp_subflow_cwnd_space(so
)),
755 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
758 * First, the hard conditions to reject subflows
759 * (e.g., not connected,...)
761 if (mpts
== ignore
|| inp
->inp_last_outifp
== NULL
)
764 if (INP_WAIT_FOR_IF_FEEDBACK(inp
))
767 /* There can only be one subflow in degraded state */
768 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) {
774 * If this subflow is waiting to finally send, do it!
776 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
)
777 return (mptcp_return_subflow(mpts
));
780 * Only send if the subflow is MP_CAPABLE. The exceptions to
781 * this rule (degraded or TFO) have been taken care of above.
783 if (!(mpts
->mpts_flags
& MPTSF_MP_CAPABLE
))
786 if ((so
->so_state
& SS_ISDISCONNECTED
) ||
787 !(so
->so_state
& SS_ISCONNECTED
) ||
788 !TCPS_HAVEESTABLISHED(tp
->t_state
) ||
789 tp
->t_state
> TCPS_CLOSE_WAIT
)
793 * Second, the soft conditions to find the subflow with best
794 * conditions for each set (aka cellular vs non-cellular)
796 if (IFNET_IS_CELLULAR(inp
->inp_last_outifp
))
797 second_best
= mptcp_choose_subflow(mpts
, second_best
,
800 best
= mptcp_choose_subflow(mpts
, best
, &cheap_rtt
);
804 * If there is no preferred or backup subflow, and there is no active
805 * subflow use the last usable subflow.
808 return (mptcp_return_subflow(second_best
));
810 if (second_best
== NULL
)
811 return (mptcp_return_subflow(best
));
813 besttp
= sototcpcb(best
->mpts_socket
);
814 bestinp
= sotoinpcb(best
->mpts_socket
);
815 secondtp
= sototcpcb(second_best
->mpts_socket
);
816 secondinp
= sotoinpcb(second_best
->mpts_socket
);
818 if (preferred
!= NULL
)
819 *preferred
= mptcp_return_subflow(best
);
822 * Second Step: Among best and second_best. Choose the one that is
823 * most appropriate for this particular service-type.
825 if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_HANDOVER
) {
827 * Only handover if Symptoms tells us to do so.
829 if (IFNET_IS_WIFI(bestinp
->inp_last_outifp
) &&
830 mptcp_is_wifi_unusable() &&
831 besttp
->t_rxtshift
>= mptcp_fail_thresh
)
832 return (mptcp_return_subflow(second_best
));
834 return (mptcp_return_subflow(best
));
835 } else if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_INTERACTIVE
) {
836 int rtt_thresh
= mptcp_rtthist_rtthresh
<< TCP_RTT_SHIFT
;
837 int rto_thresh
= mptcp_rtothresh
;
839 /* Adjust with symptoms information */
840 if (IFNET_IS_WIFI(bestinp
->inp_last_outifp
) &&
841 mptcp_is_wifi_unusable()) {
846 if (besttp
->t_srtt
&& secondtp
->t_srtt
&&
847 besttp
->t_srtt
>= rtt_thresh
&&
848 secondtp
->t_srtt
< rtt_thresh
) {
849 tcpstat
.tcps_mp_sel_rtt
++;
850 mptcplog((LOG_DEBUG
, "%s: best cid %d at rtt %d, second cid %d at rtt %d\n", __func__
,
851 best
->mpts_connid
, besttp
->t_srtt
>> TCP_RTT_SHIFT
,
852 second_best
->mpts_connid
,
853 secondtp
->t_srtt
>> TCP_RTT_SHIFT
),
854 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_LOG
);
855 return (mptcp_return_subflow(second_best
));
858 if (besttp
->t_rxtshift
>= mptcp_fail_thresh
&&
859 secondtp
->t_rxtshift
== 0) {
860 return (mptcp_return_subflow(second_best
));
863 /* Compare RTOs, select second_best if best's rto exceeds rtothresh */
864 if (besttp
->t_rxtcur
&& secondtp
->t_rxtcur
&&
865 besttp
->t_rxtcur
>= rto_thresh
&&
866 secondtp
->t_rxtcur
< rto_thresh
) {
867 tcpstat
.tcps_mp_sel_rto
++;
868 mptcplog((LOG_DEBUG
, "%s: best cid %d at rto %d, second cid %d at rto %d\n", __func__
,
869 best
->mpts_connid
, besttp
->t_rxtcur
,
870 second_best
->mpts_connid
, secondtp
->t_rxtcur
),
871 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_LOG
);
873 return (mptcp_return_subflow(second_best
));
877 * None of the above conditions for sending on the secondary
878 * were true. So, let's schedule on the best one, if he still
879 * has some space in the congestion-window.
881 return (mptcp_return_subflow(best
));
882 } else if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_AGGREGATE
) {
886 * We only care about RTT when aggregating
888 if (besttp
->t_srtt
> secondtp
->t_srtt
) {
895 secondtp
= sototcpcb(second_best
->mpts_socket
);
896 secondinp
= sotoinpcb(second_best
->mpts_socket
);
899 /* Is there still space in the congestion window? */
900 if (mptcp_subflow_cwnd_space(bestinp
->inp_socket
) <= 0)
901 return (mptcp_return_subflow(second_best
));
903 return (mptcp_return_subflow(best
));
905 panic("Unknown service-type configured for MPTCP");
912 mptcp_event_to_str(uint32_t event
)
914 const char *c
= "UNDEFINED";
919 case MPCE_RECV_DATA_ACK
:
920 c
= "MPCE_RECV_DATA_ACK";
922 case MPCE_RECV_DATA_FIN
:
923 c
= "MPCE_RECV_DATA_FIN";
930 mptcp_state_to_str(mptcp_state_t state
)
932 const char *c
= "UNDEFINED";
940 case MPTCPS_ESTABLISHED
:
941 c
= "MPTCPS_ESTABLISHED";
943 case MPTCPS_CLOSE_WAIT
:
944 c
= "MPTCPS_CLOSE_WAIT";
946 case MPTCPS_FIN_WAIT_1
:
947 c
= "MPTCPS_FIN_WAIT_1";
950 c
= "MPTCPS_CLOSING";
952 case MPTCPS_LAST_ACK
:
953 c
= "MPTCPS_LAST_ACK";
955 case MPTCPS_FIN_WAIT_2
:
956 c
= "MPTCPS_FIN_WAIT_2";
958 case MPTCPS_TIME_WAIT
:
959 c
= "MPTCPS_TIME_WAIT";
961 case MPTCPS_TERMINATE
:
962 c
= "MPTCPS_TERMINATE";
969 mptcp_close_fsm(struct mptcb
*mp_tp
, uint32_t event
)
971 mpte_lock_assert_held(mp_tp
->mpt_mpte
);
972 mptcp_state_t old_state
= mp_tp
->mpt_state
;
974 DTRACE_MPTCP2(state__change
, struct mptcb
*, mp_tp
,
977 switch (mp_tp
->mpt_state
) {
980 mp_tp
->mpt_state
= MPTCPS_CLOSED
;
983 case MPTCPS_ESTABLISHED
:
984 if (event
== MPCE_CLOSE
) {
985 mp_tp
->mpt_state
= MPTCPS_FIN_WAIT_1
;
986 mp_tp
->mpt_sndmax
+= 1; /* adjust for Data FIN */
987 } else if (event
== MPCE_RECV_DATA_FIN
) {
988 mp_tp
->mpt_rcvnxt
+= 1; /* adj remote data FIN */
989 mp_tp
->mpt_state
= MPTCPS_CLOSE_WAIT
;
993 case MPTCPS_CLOSE_WAIT
:
994 if (event
== MPCE_CLOSE
) {
995 mp_tp
->mpt_state
= MPTCPS_LAST_ACK
;
996 mp_tp
->mpt_sndmax
+= 1; /* adjust for Data FIN */
1000 case MPTCPS_FIN_WAIT_1
:
1001 if (event
== MPCE_RECV_DATA_ACK
) {
1002 mp_tp
->mpt_state
= MPTCPS_FIN_WAIT_2
;
1003 } else if (event
== MPCE_RECV_DATA_FIN
) {
1004 mp_tp
->mpt_rcvnxt
+= 1; /* adj remote data FIN */
1005 mp_tp
->mpt_state
= MPTCPS_CLOSING
;
1009 case MPTCPS_CLOSING
:
1010 if (event
== MPCE_RECV_DATA_ACK
)
1011 mp_tp
->mpt_state
= MPTCPS_TIME_WAIT
;
1014 case MPTCPS_LAST_ACK
:
1015 if (event
== MPCE_RECV_DATA_ACK
)
1016 mptcp_close(mp_tp
->mpt_mpte
, mp_tp
);
1019 case MPTCPS_FIN_WAIT_2
:
1020 if (event
== MPCE_RECV_DATA_FIN
) {
1021 mp_tp
->mpt_rcvnxt
+= 1; /* adj remote data FIN */
1022 mp_tp
->mpt_state
= MPTCPS_TIME_WAIT
;
1026 case MPTCPS_TIME_WAIT
:
1027 case MPTCPS_TERMINATE
:
1034 DTRACE_MPTCP2(state__change
, struct mptcb
*, mp_tp
,
1036 mptcplog((LOG_INFO
, "%s: %s to %s on event %s\n", __func__
,
1037 mptcp_state_to_str(old_state
),
1038 mptcp_state_to_str(mp_tp
->mpt_state
),
1039 mptcp_event_to_str(event
)),
1040 MPTCP_STATE_DBG
, MPTCP_LOGLVL_LOG
);
1043 /* If you change this function, match up mptcp_update_rcv_state_f */
1045 mptcp_update_dss_rcv_state(struct mptcp_dsn_opt
*dss_info
, struct tcpcb
*tp
,
1048 struct mptcb
*mp_tp
= tptomptp(tp
);
1049 u_int64_t full_dsn
= 0;
1051 NTOHL(dss_info
->mdss_dsn
);
1052 NTOHL(dss_info
->mdss_subflow_seqn
);
1053 NTOHS(dss_info
->mdss_data_len
);
1055 /* XXX for autosndbuf grow sb here */
1056 MPTCP_EXTEND_DSN(mp_tp
->mpt_rcvnxt
, dss_info
->mdss_dsn
, full_dsn
);
1057 mptcp_update_rcv_state_meat(mp_tp
, tp
,
1058 full_dsn
, dss_info
->mdss_subflow_seqn
, dss_info
->mdss_data_len
,
1064 mptcp_update_rcv_state_meat(struct mptcb
*mp_tp
, struct tcpcb
*tp
,
1065 u_int64_t full_dsn
, u_int32_t seqn
, u_int16_t mdss_data_len
,
1068 if (mdss_data_len
== 0) {
1069 mptcplog((LOG_INFO
, "%s: Infinite Mapping.\n", __func__
),
1070 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_LOG
);
1072 if ((mp_tp
->mpt_flags
& MPTCPF_CHECKSUM
) && (csum
!= 0)) {
1073 mptcplog((LOG_ERR
, "%s: Bad checksum %x \n", __func__
,
1074 csum
), MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_ERR
);
1076 mptcp_notify_mpfail(tp
->t_inpcb
->inp_socket
);
1079 mptcplog((LOG_DEBUG
,
1080 "%s: seqn = %x len = %x full = %llx rcvnxt = %llu \n", __func__
,
1081 seqn
, mdss_data_len
, full_dsn
, mp_tp
->mpt_rcvnxt
),
1082 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_VERBOSE
);
1084 /* Process a Data FIN packet , handled in mptcp_do_fin_opt */
1085 if ((seqn
== 0) && (mdss_data_len
== 1)) {
1086 mptcplog((LOG_INFO
, "%s: Data FIN in %s state \n", __func__
,
1087 mptcp_state_to_str(mp_tp
->mpt_state
)),
1088 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_LOG
);
1091 mptcp_notify_mpready(tp
->t_inpcb
->inp_socket
);
1092 tp
->t_rcv_map
.mpt_dsn
= full_dsn
;
1093 tp
->t_rcv_map
.mpt_sseq
= seqn
;
1094 tp
->t_rcv_map
.mpt_len
= mdss_data_len
;
1095 tp
->t_rcv_map
.mpt_csum
= csum
;
1096 tp
->t_mpflags
|= TMPF_EMBED_DSN
;
1101 mptcp_validate_dss_map(struct socket
*so
, struct tcpcb
*tp
, struct mbuf
*m
,
1106 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
))
1109 datalen
= m
->m_pkthdr
.mp_rlen
;
1111 /* unacceptable DSS option, fallback to TCP */
1112 if (m
->m_pkthdr
.len
> ((int) datalen
+ hdrlen
)) {
1113 mptcplog((LOG_ERR
, "%s: mbuf len %d, MPTCP expected %d",
1114 __func__
, m
->m_pkthdr
.len
, datalen
),
1115 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_LOG
);
1119 tp
->t_mpflags
|= TMPF_SND_MPFAIL
;
1120 mptcp_notify_mpfail(so
);
1126 mptcp_input_preproc(struct tcpcb
*tp
, struct mbuf
*m
, int drop_hdrlen
)
1128 mptcp_insert_rmap(tp
, m
);
1129 if (mptcp_validate_dss_map(tp
->t_inpcb
->inp_socket
, tp
, m
,
1136 * MPTCP Checksum support
1137 * The checksum is calculated whenever the MPTCP DSS option is included
1138 * in the TCP packet. The checksum includes the sum of the MPTCP psuedo
1139 * header and the actual data indicated by the length specified in the
1144 mptcp_validate_csum(struct tcpcb
*tp
, struct mbuf
*m
, uint64_t dsn
,
1145 uint32_t sseq
, uint16_t dlen
, uint16_t csum
)
1147 uint16_t mptcp_csum
;
1149 mptcp_csum
= mptcp_input_csum(tp
, m
, dsn
, sseq
, dlen
, csum
);
1151 tp
->t_mpflags
|= TMPF_SND_MPFAIL
;
1152 mptcp_notify_mpfail(tp
->t_inpcb
->inp_socket
);
1154 tcpstat
.tcps_mp_badcsum
++;
1161 mptcp_input_csum(struct tcpcb
*tp
, struct mbuf
*m
, uint64_t dsn
, uint32_t sseq
,
1162 uint16_t dlen
, uint16_t csum
)
1164 struct mptcb
*mp_tp
= tptomptp(tp
);
1170 if (!(mp_tp
->mpt_flags
& MPTCPF_CHECKSUM
))
1173 if (tp
->t_mpflags
& TMPF_TCP_FALLBACK
)
1177 * The remote side may send a packet with fewer bytes than the
1178 * claimed DSS checksum length.
1180 if ((int)m_length2(m
, NULL
) < dlen
)
1184 sum
= m_sum16(m
, 0, dlen
);
1186 sum
+= in_pseudo64(htonll(dsn
), htonl(sseq
), htons(dlen
) + csum
);
1188 DTRACE_MPTCP3(checksum__result
, struct tcpcb
*, tp
, struct mbuf
*, m
,
1191 mptcplog((LOG_DEBUG
, "%s: sum = %x \n", __func__
, sum
),
1192 MPTCP_RECEIVER_DBG
, MPTCP_LOGLVL_VERBOSE
);
1193 return (~sum
& 0xffff);
1197 mptcp_output_csum(struct mbuf
*m
, uint64_t dss_val
, uint32_t sseq
, uint16_t dlen
)
1202 sum
= m_sum16(m
, 0, dlen
);
1204 dss_val
= mptcp_hton64(dss_val
);
1207 sum
+= in_pseudo64(dss_val
, sseq
, dlen
);
1210 sum
= ~sum
& 0xffff;
1211 DTRACE_MPTCP2(checksum__result
, struct mbuf
*, m
, uint32_t, sum
);
1212 mptcplog((LOG_DEBUG
, "%s: sum = %x \n", __func__
, sum
),
1213 MPTCP_SENDER_DBG
, MPTCP_LOGLVL_VERBOSE
);
1219 * When WiFi signal starts fading, there's more loss and RTT spikes.
1220 * Check if there has been a large spike by comparing against
1221 * a tolerable RTT spike threshold.
1224 mptcp_no_rto_spike(struct socket
*so
)
1226 struct tcpcb
*tp
= intotcpcb(sotoinpcb(so
));
1229 if (tp
->t_rxtcur
> mptcp_rtothresh
) {
1230 spike
= tp
->t_rxtcur
- mptcp_rtothresh
;
1232 mptcplog((LOG_DEBUG
, "%s: spike = %d rto = %d best = %d cur = %d\n",
1234 tp
->t_rxtcur
, tp
->t_rttbest
>> TCP_RTT_SHIFT
,
1236 (MPTCP_SOCKET_DBG
|MPTCP_SENDER_DBG
), MPTCP_LOGLVL_LOG
);
1248 mptcp_handle_deferred_upcalls(struct mppcb
*mpp
, uint32_t flag
)
1250 VERIFY(mpp
->mpp_flags
& flag
);
1251 mpp
->mpp_flags
&= ~flag
;
1253 if (mptcp_should_defer_upcall(mpp
))
1256 if (mpp
->mpp_flags
& MPP_SHOULD_WORKLOOP
) {
1257 mpp
->mpp_flags
&= ~MPP_SHOULD_WORKLOOP
;
1259 mptcp_subflow_workloop(mpp
->mpp_pcbe
);
1262 if (mpp
->mpp_flags
& MPP_SHOULD_RWAKEUP
) {
1263 mpp
->mpp_flags
&= ~MPP_SHOULD_RWAKEUP
;
1265 sorwakeup(mpp
->mpp_socket
);
1268 if (mpp
->mpp_flags
& MPP_SHOULD_WWAKEUP
) {
1269 mpp
->mpp_flags
&= ~MPP_SHOULD_WWAKEUP
;
1271 sowwakeup(mpp
->mpp_socket
);
1274 if (mpp
->mpp_flags
& MPP_SET_CELLICON
) {
1275 mpp
->mpp_flags
&= ~MPP_SET_CELLICON
;
1277 mptcp_set_cellicon(mpp
->mpp_pcbe
);
1280 if (mpp
->mpp_flags
& MPP_UNSET_CELLICON
) {
1281 mpp
->mpp_flags
&= ~MPP_UNSET_CELLICON
;
1283 mptcp_unset_cellicon();
1288 mptcp_ask_for_nat64(struct ifnet
*ifp
)
1290 in6_post_msg(ifp
, KEV_INET6_REQUEST_NAT64_PREFIX
, NULL
, NULL
);
1292 mptcplog((LOG_DEBUG
, "%s: asked for NAT64-prefix on %s\n",
1293 __func__
, ifp
->if_name
), MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
1297 mptcp_reset_itfinfo(struct mpt_itf_info
*info
)
1300 info
->has_v4_conn
= 0;
1301 info
->has_v6_conn
= 0;
1305 mptcp_session_necp_cb(void *handle
, int action
, struct necp_client_flow
*flow
)
1307 struct mppcb
*mp
= (struct mppcb
*)handle
;
1308 struct mptses
*mpte
= mptompte(mp
);
1309 struct socket
*mp_so
;
1310 struct mptcb
*mp_tp
;
1312 uint32_t i
, ifindex
;
1314 ifindex
= flow
->interface_index
;
1315 VERIFY(ifindex
!= IFSCOPE_NONE
);
1317 /* ToDo - remove after rdar://problem/32007628 */
1318 if (!IF_INDEX_IN_RANGE(ifindex
))
1319 printf("%s 1 ifindex %u not in range of flow %p action %d\n",
1320 __func__
, ifindex
, flow
, action
);
1322 /* About to be garbage-collected (see note about MPTCP/NECP interactions) */
1323 if (mp
->mpp_socket
->so_usecount
== 0)
1326 if (action
!= NECP_CLIENT_CBACTION_INITIAL
) {
1330 /* Check again, because it might have changed while waiting */
1331 if (mp
->mpp_socket
->so_usecount
== 0)
1335 mp_tp
= mpte
->mpte_mptcb
;
1336 mp_so
= mptetoso(mpte
);
1338 mptcplog((LOG_DEBUG
, "%s, action: %u ifindex %u usecount %u mpt_flags %#x state %u\n",
1339 __func__
, action
, ifindex
, mp
->mpp_socket
->so_usecount
, mp_tp
->mpt_flags
, mp_tp
->mpt_state
),
1340 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
1342 /* No need on fallen back sockets */
1343 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
)
1346 if (action
== NECP_CLIENT_CBACTION_NONVIABLE
) {
1347 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
1348 if (mpte
->mpte_itfinfo
[i
].ifindex
== ifindex
)
1349 mptcp_reset_itfinfo(&mpte
->mpte_itfinfo
[i
]);
1352 mptcp_sched_create_subflows(mpte
);
1353 } else if (action
== NECP_CLIENT_CBACTION_VIABLE
||
1354 action
== NECP_CLIENT_CBACTION_INITIAL
) {
1355 int found_empty
= 0, empty_index
= -1;
1358 /* ToDo - remove after rdar://problem/32007628 */
1359 if (!IF_INDEX_IN_RANGE(ifindex
))
1360 printf("%s 2 ifindex %u not in range of flow %p action %d\n",
1361 __func__
, ifindex
, flow
, action
);
1363 ifnet_head_lock_shared();
1364 ifp
= ifindex2ifnet
[ifindex
];
1367 /* ToDo - remove after rdar://problem/32007628 */
1368 if (!IF_INDEX_IN_RANGE(ifindex
))
1369 printf("%s 3 ifindex %u not in range of flow %p action %d\n",
1370 __func__
, ifindex
, flow
, action
);
1375 if (IFNET_IS_EXPENSIVE(ifp
) &&
1376 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_EXPENSIVE
))
1379 if (IFNET_IS_CELLULAR(ifp
) &&
1380 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_CELLULAR
))
1383 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
1384 if (mpte
->mpte_itfinfo
[i
].ifindex
== 0) {
1389 if (mpte
->mpte_itfinfo
[i
].ifindex
== ifindex
) {
1390 /* Ok, it's already there */
1395 if ((mpte
->mpte_dst
.sa_family
== AF_INET
|| mpte
->mpte_dst
.sa_family
== 0) &&
1396 !(flow
->necp_flow_flags
& NECP_CLIENT_RESULT_FLAG_HAS_IPV4
) &&
1397 ifnet_get_nat64prefix(ifp
, NULL
) == ENOENT
) {
1398 mptcp_ask_for_nat64(ifp
);
1402 if (found_empty
== 0) {
1403 int new_size
= mpte
->mpte_itfinfo_size
* 2;
1404 struct mpt_itf_info
*info
= _MALLOC(sizeof(*info
) * new_size
, M_TEMP
, M_ZERO
);
1407 mptcplog((LOG_ERR
, "%s malloc failed for %u\n", __func__
, new_size
),
1408 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
1412 memcpy(info
, mpte
->mpte_itfinfo
, mpte
->mpte_itfinfo_size
* sizeof(*info
));
1414 if (mpte
->mpte_itfinfo_size
> MPTE_ITFINFO_SIZE
)
1415 _FREE(mpte
->mpte_itfinfo
, M_TEMP
);
1417 /* We allocated a new one, thus the first must be empty */
1418 empty_index
= mpte
->mpte_itfinfo_size
;
1420 mpte
->mpte_itfinfo
= info
;
1421 mpte
->mpte_itfinfo_size
= new_size
;
1423 mptcplog((LOG_DEBUG
, "%s Needed to realloc to %u\n", __func__
, new_size
),
1424 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
1427 VERIFY(empty_index
>= 0 && empty_index
< (int)mpte
->mpte_itfinfo_size
);
1428 mpte
->mpte_itfinfo
[empty_index
].ifindex
= ifindex
;
1429 mpte
->mpte_itfinfo
[empty_index
].has_v4_conn
= !!(flow
->necp_flow_flags
& NECP_CLIENT_RESULT_FLAG_HAS_IPV4
);
1430 mpte
->mpte_itfinfo
[empty_index
].has_v6_conn
= !!(flow
->necp_flow_flags
& NECP_CLIENT_RESULT_FLAG_HAS_IPV6
);
1432 mptcp_sched_create_subflows(mpte
);
1441 mptcp_set_restrictions(struct socket
*mp_so
)
1443 struct mptses
*mpte
= mpsotompte(mp_so
);
1446 mpte_lock_assert_held(mpte
);
1448 ifnet_head_lock_shared();
1450 for (i
= 0; i
< mpte
->mpte_itfinfo_size
; i
++) {
1451 struct mpt_itf_info
*info
= &mpte
->mpte_itfinfo
[i
];
1452 uint32_t ifindex
= info
->ifindex
;
1455 if (ifindex
== IFSCOPE_NONE
)
1458 ifp
= ifindex2ifnet
[ifindex
];
1460 if (IFNET_IS_EXPENSIVE(ifp
) &&
1461 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_EXPENSIVE
))
1462 info
->ifindex
= IFSCOPE_NONE
;
1464 if (IFNET_IS_CELLULAR(ifp
) &&
1465 (mp_so
->so_restrictions
& SO_RESTRICT_DENY_CELLULAR
))
1466 info
->ifindex
= IFSCOPE_NONE
;