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@
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <sys/protosw.h>
35 #include <sys/mcache.h>
36 #include <sys/syslog.h>
38 #include <sys/proc_internal.h>
39 #include <sys/resourcevar.h>
40 #include <sys/kauth.h>
44 #include <netinet/in.h>
45 #include <netinet/in_var.h>
46 #include <netinet/tcp.h>
47 #include <netinet/tcp_fsm.h>
48 #include <netinet/tcp_seq.h>
49 #include <netinet/tcp_var.h>
50 #include <netinet/tcp_timer.h>
51 #include <netinet/mptcp_var.h>
52 #include <netinet/mptcp_timer.h>
56 static int mptcp_usr_attach(struct socket
*, int, struct proc
*);
57 static int mptcp_usr_detach(struct socket
*);
58 static int mptcp_attach(struct socket
*, struct proc
*);
59 static int mptcp_usr_connectx(struct socket
*, struct sockaddr
*,
60 struct sockaddr
*, struct proc
*, uint32_t, sae_associd_t
,
61 sae_connid_t
*, uint32_t, void *, uint32_t, struct uio
*, user_ssize_t
*);
62 static int mptcp_getassocids(struct mptses
*, uint32_t *, user_addr_t
);
63 static int mptcp_getconnids(struct mptses
*, sae_associd_t
, uint32_t *,
65 static int mptcp_getconninfo(struct mptses
*, sae_connid_t
*, uint32_t *,
66 uint32_t *, int32_t *, user_addr_t
, socklen_t
*, user_addr_t
, socklen_t
*,
67 uint32_t *, user_addr_t
, uint32_t *);
68 static int mptcp_usr_control(struct socket
*, u_long
, caddr_t
, struct ifnet
*,
70 static int mptcp_disconnect(struct mptses
*);
71 static int mptcp_usr_disconnect(struct socket
*);
72 static int mptcp_usr_disconnectx(struct socket
*, sae_associd_t
, sae_connid_t
);
73 static struct mptses
*mptcp_usrclosed(struct mptses
*);
74 static int mptcp_usr_rcvd(struct socket
*, int);
75 static int mptcp_usr_send(struct socket
*, int, struct mbuf
*,
76 struct sockaddr
*, struct mbuf
*, struct proc
*);
77 static int mptcp_usr_shutdown(struct socket
*);
78 static int mptcp_usr_sosend(struct socket
*, struct sockaddr
*, struct uio
*,
79 struct mbuf
*, struct mbuf
*, int);
80 static int mptcp_usr_socheckopt(struct socket
*, struct sockopt
*);
81 static int mptcp_setopt(struct mptses
*, struct sockopt
*);
82 static int mptcp_getopt(struct mptses
*, struct sockopt
*);
83 static int mptcp_default_tcp_optval(struct mptses
*, struct sockopt
*, int *);
84 static int mptcp_usr_preconnect(struct socket
*so
);
86 struct pr_usrreqs mptcp_usrreqs
= {
87 .pru_attach
= mptcp_usr_attach
,
88 .pru_connectx
= mptcp_usr_connectx
,
89 .pru_control
= mptcp_usr_control
,
90 .pru_detach
= mptcp_usr_detach
,
91 .pru_disconnect
= mptcp_usr_disconnect
,
92 .pru_disconnectx
= mptcp_usr_disconnectx
,
93 .pru_peeraddr
= mp_getpeeraddr
,
94 .pru_rcvd
= mptcp_usr_rcvd
,
95 .pru_send
= mptcp_usr_send
,
96 .pru_shutdown
= mptcp_usr_shutdown
,
97 .pru_sockaddr
= mp_getsockaddr
,
98 .pru_sosend
= mptcp_usr_sosend
,
99 .pru_soreceive
= soreceive
,
100 .pru_socheckopt
= mptcp_usr_socheckopt
,
101 .pru_preconnect
= mptcp_usr_preconnect
,
105 #if (DEVELOPMENT || DEBUG)
106 static int mptcp_disable_entitlements
= 0;
107 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, disable_entitlements
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
108 &mptcp_disable_entitlements
, 0, "Disable Multipath TCP Entitlement Checking");
111 int mptcp_developer_mode
= 0;
112 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, allow_aggregate
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
113 &mptcp_developer_mode
, 0, "Allow the Multipath aggregation mode");
117 * Attaches an MPTCP control block to a socket.
120 mptcp_usr_attach(struct socket
*mp_so
, int proto
, struct proc
*p
)
122 #pragma unused(proto)
125 VERIFY(mpsotomppcb(mp_so
) == NULL
);
127 error
= mptcp_attach(mp_so
, p
);
133 * Might want to use a different SO_LINGER timeout than TCP's?
135 if ((mp_so
->so_options
& SO_LINGER
) && mp_so
->so_linger
== 0)
136 mp_so
->so_linger
= TCP_LINGERTIME
* hz
;
142 * Detaches an MPTCP control block from a socket.
145 mptcp_usr_detach(struct socket
*mp_so
)
147 struct mptses
*mpte
= mpsotompte(mp_so
);
148 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
150 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
151 mptcplog((LOG_ERR
, "%s state: %d\n", __func__
,
152 mpp
? mpp
->mpp_state
: -1),
153 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
158 * We are done with this MPTCP socket (it has been closed);
159 * trigger all subflows to be disconnected, if not already,
160 * by initiating the PCB detach sequence (SOF_PCBCLEARING
165 mptcp_disconnect(mpte
);
171 * Attach MPTCP protocol to socket, allocating MP control block,
172 * MPTCP session, control block, buffer space, etc.
175 mptcp_attach(struct socket
*mp_so
, struct proc
*p
)
178 struct mptses
*mpte
= NULL
;
179 struct mptcb
*mp_tp
= NULL
;
180 struct mppcb
*mpp
= NULL
;
183 if (mp_so
->so_snd
.sb_hiwat
== 0 || mp_so
->so_rcv
.sb_hiwat
== 0) {
184 error
= soreserve(mp_so
, tcp_sendspace
, tcp_recvspace
);
189 if (mp_so
->so_snd
.sb_preconn_hiwat
== 0) {
190 soreserve_preconnect(mp_so
, 2048);
193 if ((mp_so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0)
194 mp_so
->so_rcv
.sb_flags
|= SB_AUTOSIZE
;
195 if ((mp_so
->so_snd
.sb_flags
& SB_USRSIZE
) == 0)
196 mp_so
->so_snd
.sb_flags
|= SB_AUTOSIZE
;
199 * MPTCP socket buffers cannot be compressed, due to the
200 * fact that each mbuf chained via m_next is a M_PKTHDR
201 * which carries some MPTCP metadata.
203 mp_so
->so_snd
.sb_flags
|= SB_NOCOMPRESS
;
204 mp_so
->so_rcv
.sb_flags
|= SB_NOCOMPRESS
;
206 if ((error
= mp_pcballoc(mp_so
, &mtcbinfo
)) != 0) {
210 mpp
= mpsotomppcb(mp_so
);
212 mpte
= (struct mptses
*)mpp
->mpp_pcbe
;
213 VERIFY(mpte
!= NULL
);
214 mp_tp
= mpte
->mpte_mptcb
;
215 VERIFY(mp_tp
!= NULL
);
221 mptcp_entitlement_check(struct socket
*mp_so
)
223 struct mptses
*mpte
= mpsotompte(mp_so
);
225 if (soopt_cred_check(mp_so
, PRIV_NET_RESTRICTED_MULTIPATH_EXTENDED
, TRUE
) == 0) {
227 * This means the app has the extended entitlement. Thus,
228 * it's a first party app and can run without restrictions.
230 mpte
->mpte_flags
|= MPTE_FIRSTPARTY
;
234 #if (DEVELOPMENT || DEBUG)
235 if (mptcp_disable_entitlements
)
239 if (soopt_cred_check(mp_so
, PRIV_NET_PRIVILEGED_MULTIPATH
, TRUE
)) {
240 mptcplog((LOG_NOTICE
, "%s Multipath Capability needed\n", __func__
),
241 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
245 if (mpte
->mpte_svctype
> MPTCP_SVCTYPE_INTERACTIVE
&&
246 mptcp_developer_mode
== 0) {
247 mptcplog((LOG_NOTICE
, "%s need to set allow_aggregate sysctl\n",
248 __func__
), MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
253 mptcplog((LOG_NOTICE
, "%s entitlement granted for %u\n", __func__
, mpte
->mpte_svctype
),
254 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
260 * Common subroutine to open a MPTCP connection to one of the remote hosts
261 * specified by dst_sl. This includes allocating and establishing a
262 * subflow TCP connection, either initially to establish MPTCP connection,
263 * or to join an existing one. Returns a connection handle upon success.
266 mptcp_connectx(struct mptses
*mpte
, struct sockaddr
*src
,
267 struct sockaddr
*dst
, uint32_t ifscope
, sae_connid_t
*pcid
)
269 struct socket
*mp_so
= mptetoso(mpte
);
273 VERIFY(pcid
!= NULL
);
275 mptcplog((LOG_DEBUG
, "%s: mp_so 0x%llx\n", __func__
,
276 (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
)),
277 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
278 DTRACE_MPTCP2(connectx
, struct mptses
*, mpte
, struct socket
*, mp_so
);
280 error
= mptcp_subflow_add(mpte
, src
, dst
, ifscope
, pcid
);
286 * User-protocol pru_connectx callback.
289 mptcp_usr_connectx(struct socket
*mp_so
, struct sockaddr
*src
,
290 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope
,
291 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
292 uint32_t arglen
, struct uio
*auio
, user_ssize_t
*bytes_written
)
294 #pragma unused(p, aid, flags, arg, arglen)
295 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
296 struct mptses
*mpte
= NULL
;
297 struct mptcb
*mp_tp
= NULL
;
298 user_ssize_t datalen
;
301 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
302 mptcplog((LOG_ERR
, "%s state %d\n", __func__
,
303 mpp
? mpp
->mpp_state
: -1),
304 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
308 mpte
= mptompte(mpp
);
309 VERIFY(mpte
!= NULL
);
310 mpte_lock_assert_held(mpte
);
312 mp_tp
= mpte
->mpte_mptcb
;
313 VERIFY(mp_tp
!= NULL
);
315 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
316 mptcplog((LOG_ERR
, "%s fell back to TCP\n", __func__
),
317 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
322 if (dst
->sa_family
== AF_INET
&&
323 dst
->sa_len
!= sizeof(mpte
->__mpte_dst_v4
)) {
324 mptcplog((LOG_ERR
, "%s IPv4 dst len %u\n", __func__
,
326 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
331 if (dst
->sa_family
== AF_INET6
&&
332 dst
->sa_len
!= sizeof(mpte
->__mpte_dst_v6
)) {
333 mptcplog((LOG_ERR
, "%s IPv6 dst len %u\n", __func__
,
335 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
340 if (!(mpte
->mpte_flags
& MPTE_SVCTYPE_CHECKED
)) {
341 if (mptcp_entitlement_check(mp_so
) < 0) {
346 mpte
->mpte_flags
|= MPTE_SVCTYPE_CHECKED
;
349 if ((mp_so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0) {
350 memcpy(&mpte
->mpte_dst
, dst
, dst
->sa_len
);
354 if (src
->sa_family
== AF_INET
&&
355 src
->sa_len
!= sizeof(mpte
->__mpte_src_v4
)) {
356 mptcplog((LOG_ERR
, "%s IPv4 src len %u\n", __func__
,
358 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
363 if (src
->sa_family
== AF_INET6
&&
364 src
->sa_len
!= sizeof(mpte
->__mpte_src_v6
)) {
365 mptcplog((LOG_ERR
, "%s IPv6 src len %u\n", __func__
,
367 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
372 if ((mp_so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0) {
373 memcpy(&mpte
->mpte_src
, src
, src
->sa_len
);
377 error
= mptcp_connectx(mpte
, src
, dst
, ifscope
, pcid
);
379 /* If there is data, copy it */
381 datalen
= uio_resid(auio
);
382 socket_unlock(mp_so
, 0);
383 error
= mp_so
->so_proto
->pr_usrreqs
->pru_sosend(mp_so
, NULL
,
384 (uio_t
) auio
, NULL
, NULL
, 0);
386 if (error
== 0 || error
== EWOULDBLOCK
)
387 *bytes_written
= datalen
- uio_resid(auio
);
389 if (error
== EWOULDBLOCK
)
392 socket_lock(mp_so
, 0);
400 * Handle SIOCGASSOCIDS ioctl for PF_MULTIPATH domain.
403 mptcp_getassocids(struct mptses
*mpte
, uint32_t *cnt
, user_addr_t aidp
)
405 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
407 /* MPTCP has at most 1 association */
408 *cnt
= (mpte
->mpte_associd
!= SAE_ASSOCID_ANY
) ? 1 : 0;
410 /* just asking how many there are? */
411 if (aidp
== USER_ADDR_NULL
)
414 return (copyout(&mpte
->mpte_associd
, aidp
,
415 sizeof (mpte
->mpte_associd
)));
419 * Handle SIOCGCONNIDS ioctl for PF_MULTIPATH domain.
422 mptcp_getconnids(struct mptses
*mpte
, sae_associd_t aid
, uint32_t *cnt
,
428 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
430 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
&&
431 aid
!= mpte
->mpte_associd
)
434 *cnt
= mpte
->mpte_numflows
;
436 /* just asking how many there are? */
437 if (cidp
== USER_ADDR_NULL
)
440 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
441 if ((error
= copyout(&mpts
->mpts_connid
, cidp
,
442 sizeof (mpts
->mpts_connid
))) != 0)
445 cidp
+= sizeof (mpts
->mpts_connid
);
452 * Handle SIOCGCONNINFO ioctl for PF_MULTIPATH domain.
455 mptcp_getconninfo(struct mptses
*mpte
, sae_connid_t
*cid
, uint32_t *flags
,
456 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
457 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
458 user_addr_t aux_data
, uint32_t *aux_len
)
470 if (*cid
== SAE_CONNID_ALL
) {
471 struct socket
*mp_so
= mptetoso(mpte
);
472 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
473 struct conninfo_multipathtcp mptcp_ci
;
475 if (*aux_len
!= 0 && *aux_len
!= sizeof(mptcp_ci
))
478 if (mp_so
->so_state
& SS_ISCONNECTING
)
479 *flags
|= CIF_CONNECTING
;
480 if (mp_so
->so_state
& SS_ISCONNECTED
)
481 *flags
|= CIF_CONNECTED
;
482 if (mp_so
->so_state
& SS_ISDISCONNECTING
)
483 *flags
|= CIF_DISCONNECTING
;
484 if (mp_so
->so_state
& SS_ISDISCONNECTED
)
485 *flags
|= CIF_DISCONNECTED
;
486 if (!(mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
))
487 *flags
|= CIF_MP_CAPABLE
;
488 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
)
489 *flags
|= CIF_MP_DEGRADED
;
494 *aux_type
= CIAUX_MPTCP
;
495 *aux_len
= sizeof(mptcp_ci
);
497 if (aux_data
!= USER_ADDR_NULL
) {
499 int initial_info_set
= 0;
501 bzero(&mptcp_ci
, sizeof (mptcp_ci
));
502 mptcp_ci
.mptcpci_subflow_count
= mpte
->mpte_numflows
;
503 mptcp_ci
.mptcpci_switch_count
= mpte
->mpte_subflow_switches
;
505 VERIFY(sizeof(mptcp_ci
.mptcpci_itfstats
) == sizeof(mpte
->mpte_itfstats
));
506 memcpy(mptcp_ci
.mptcpci_itfstats
, mpte
->mpte_itfstats
, sizeof(mptcp_ci
.mptcpci_itfstats
));
508 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
509 if (i
>= sizeof(mptcp_ci
.mptcpci_subflow_connids
) / sizeof(sae_connid_t
))
511 mptcp_ci
.mptcpci_subflow_connids
[i
] = mpts
->mpts_connid
;
513 if (mpts
->mpts_flags
& MPTSF_INITIAL_SUB
) {
514 inp
= sotoinpcb(mpts
->mpts_socket
);
516 mptcp_ci
.mptcpci_init_rxbytes
= inp
->inp_stat
->rxbytes
;
517 mptcp_ci
.mptcpci_init_txbytes
= inp
->inp_stat
->txbytes
;
518 initial_info_set
= 1;
521 mptcpstats_update(mptcp_ci
.mptcpci_itfstats
, mpts
);
526 if (initial_info_set
== 0) {
527 mptcp_ci
.mptcpci_init_rxbytes
= mpte
->mpte_init_rxbytes
;
528 mptcp_ci
.mptcpci_init_txbytes
= mpte
->mpte_init_txbytes
;
531 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
)
532 mptcp_ci
.mptcpci_flags
|= MPTCPCI_FIRSTPARTY
;
534 error
= copyout(&mptcp_ci
, aux_data
, sizeof(mptcp_ci
));
536 mptcplog((LOG_ERR
, "%s copyout failed: %d\n",
538 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
546 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
547 if (mpts
->mpts_connid
== *cid
|| *cid
== SAE_CONNID_ANY
)
551 return ((*cid
== SAE_CONNID_ANY
) ? ENXIO
: EINVAL
);
553 so
= mpts
->mpts_socket
;
556 if (inp
->inp_vflag
& INP_IPV4
)
557 error
= in_getconninfo(so
, SAE_CONNID_ANY
, flags
, ifindex
,
558 soerror
, src
, src_len
, dst
, dst_len
,
559 aux_type
, aux_data
, aux_len
);
561 error
= in6_getconninfo(so
, SAE_CONNID_ANY
, flags
, ifindex
,
562 soerror
, src
, src_len
, dst
, dst_len
,
563 aux_type
, aux_data
, aux_len
);
566 mptcplog((LOG_ERR
, "%s error from in_getconninfo %d\n",
568 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
572 if (mpts
->mpts_flags
& MPTSF_MP_CAPABLE
)
573 *flags
|= CIF_MP_CAPABLE
;
574 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
)
575 *flags
|= CIF_MP_DEGRADED
;
576 if (mpts
->mpts_flags
& MPTSF_MP_READY
)
577 *flags
|= CIF_MP_READY
;
578 if (mpts
->mpts_flags
& MPTSF_ACTIVE
)
579 *flags
|= CIF_MP_ACTIVE
;
581 mptcplog((LOG_DEBUG
, "%s: cid %d flags %x \n", __func__
,
582 mpts
->mpts_connid
, mpts
->mpts_flags
),
583 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
589 * User-protocol pru_control callback.
592 mptcp_usr_control(struct socket
*mp_so
, u_long cmd
, caddr_t data
,
593 struct ifnet
*ifp
, struct proc
*p
)
595 #pragma unused(ifp, p)
596 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
600 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
604 mpte
= mptompte(mpp
);
605 VERIFY(mpte
!= NULL
);
607 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
610 case SIOCGASSOCIDS32
: { /* struct so_aidreq32 */
611 struct so_aidreq32 aidr
;
612 bcopy(data
, &aidr
, sizeof (aidr
));
613 error
= mptcp_getassocids(mpte
, &aidr
.sar_cnt
,
616 bcopy(&aidr
, data
, sizeof (aidr
));
620 case SIOCGASSOCIDS64
: { /* struct so_aidreq64 */
621 struct so_aidreq64 aidr
;
622 bcopy(data
, &aidr
, sizeof (aidr
));
623 error
= mptcp_getassocids(mpte
, &aidr
.sar_cnt
,
626 bcopy(&aidr
, data
, sizeof (aidr
));
630 case SIOCGCONNIDS32
: { /* struct so_cidreq32 */
631 struct so_cidreq32 cidr
;
632 bcopy(data
, &cidr
, sizeof (cidr
));
633 error
= mptcp_getconnids(mpte
, cidr
.scr_aid
, &cidr
.scr_cnt
,
636 bcopy(&cidr
, data
, sizeof (cidr
));
640 case SIOCGCONNIDS64
: { /* struct so_cidreq64 */
641 struct so_cidreq64 cidr
;
642 bcopy(data
, &cidr
, sizeof (cidr
));
643 error
= mptcp_getconnids(mpte
, cidr
.scr_aid
, &cidr
.scr_cnt
,
646 bcopy(&cidr
, data
, sizeof (cidr
));
650 case SIOCGCONNINFO32
: { /* struct so_cinforeq32 */
651 struct so_cinforeq32 cifr
;
652 bcopy(data
, &cifr
, sizeof (cifr
));
653 error
= mptcp_getconninfo(mpte
, &cifr
.scir_cid
,
654 &cifr
.scir_flags
, &cifr
.scir_ifindex
, &cifr
.scir_error
,
655 cifr
.scir_src
, &cifr
.scir_src_len
, cifr
.scir_dst
,
656 &cifr
.scir_dst_len
, &cifr
.scir_aux_type
, cifr
.scir_aux_data
,
659 bcopy(&cifr
, data
, sizeof (cifr
));
663 case SIOCGCONNINFO64
: { /* struct so_cinforeq64 */
664 struct so_cinforeq64 cifr
;
665 bcopy(data
, &cifr
, sizeof (cifr
));
666 error
= mptcp_getconninfo(mpte
, &cifr
.scir_cid
,
667 &cifr
.scir_flags
, &cifr
.scir_ifindex
, &cifr
.scir_error
,
668 cifr
.scir_src
, &cifr
.scir_src_len
, cifr
.scir_dst
,
669 &cifr
.scir_dst_len
, &cifr
.scir_aux_type
, cifr
.scir_aux_data
,
672 bcopy(&cifr
, data
, sizeof (cifr
));
685 mptcp_disconnect(struct mptses
*mpte
)
687 struct socket
*mp_so
;
691 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
693 mp_so
= mptetoso(mpte
);
694 mp_tp
= mpte
->mpte_mptcb
;
696 mptcplog((LOG_DEBUG
, "MPTCP Socket: "
697 "%s: mp_so 0x%llx %d\n", __func__
,
698 (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
), mp_so
->so_error
),
699 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
701 DTRACE_MPTCP3(disconnectx
, struct mptses
*, mpte
,
702 struct socket
*, mp_so
, struct mptcb
*, mp_tp
);
704 /* if we're not detached, go thru socket state checks */
705 if (!(mp_so
->so_flags
& SOF_PCBCLEARING
)) {
706 if (!(mp_so
->so_state
& (SS_ISCONNECTED
|
711 if (mp_so
->so_state
& SS_ISDISCONNECTING
) {
717 mptcp_cancel_all_timers(mp_tp
);
718 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
) {
719 mptcp_close(mpte
, mp_tp
);
720 } else if ((mp_so
->so_options
& SO_LINGER
) &&
721 mp_so
->so_linger
== 0) {
722 mptcp_drop(mpte
, mp_tp
, 0);
724 soisdisconnecting(mp_so
);
725 sbflush(&mp_so
->so_rcv
);
726 if (mptcp_usrclosed(mpte
) != NULL
)
731 mptcp_subflow_workloop(mpte
);
738 * Wrapper function to support disconnect on socket
741 mptcp_usr_disconnect(struct socket
*mp_so
)
743 return (mptcp_disconnect(mpsotompte(mp_so
)));
747 * User-protocol pru_disconnectx callback.
750 mptcp_usr_disconnectx(struct socket
*mp_so
, sae_associd_t aid
, sae_connid_t cid
)
752 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
755 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
)
758 return (mptcp_usr_disconnect(mp_so
));
762 * User issued close, and wish to trail thru shutdown states.
764 static struct mptses
*
765 mptcp_usrclosed(struct mptses
*mpte
)
767 struct socket
*mp_so
;
771 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
772 mp_so
= mptetoso(mpte
);
773 mp_tp
= mpte
->mpte_mptcb
;
775 mptcp_close_fsm(mp_tp
, MPCE_CLOSE
);
777 if (mp_tp
->mpt_state
== MPTCPS_CLOSED
) {
778 mpte
= mptcp_close(mpte
, mp_tp
);
779 } else if (mp_tp
->mpt_state
>= MPTCPS_FIN_WAIT_2
) {
780 soisdisconnected(mp_so
);
782 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
783 if ((mp_so
->so_state
& (SS_CANTRCVMORE
|SS_CANTSENDMORE
)) ==
784 (SS_CANTRCVMORE
| SS_CANTSENDMORE
))
785 mptcp_subflow_disconnect(mpte
, mpts
);
787 mptcp_subflow_shutdown(mpte
, mpts
);
795 * After a receive, possible send some update to peer.
798 mptcp_usr_rcvd(struct socket
*mp_so
, int flags
)
800 #pragma unused(flags)
801 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
805 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
809 mpte
= mptompte(mpp
);
810 VERIFY(mpte
!= NULL
);
812 error
= mptcp_output(mpte
);
818 * Do a send by putting data in the output queue.
821 mptcp_usr_send(struct socket
*mp_so
, int prus_flags
, struct mbuf
*m
,
822 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
824 #pragma unused(nam, p)
825 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
829 if (prus_flags
& (PRUS_OOB
|PRUS_EOF
)) {
839 if (control
!= NULL
&& control
->m_len
!= 0) {
844 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
848 mpte
= mptompte(mpp
);
849 VERIFY(mpte
!= NULL
);
851 if (!(mp_so
->so_state
& SS_ISCONNECTED
) &&
852 !(mp_so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
857 mptcp_insert_dsn(mpp
, m
);
858 VERIFY(mp_so
->so_snd
.sb_flags
& SB_NOCOMPRESS
);
859 sbappendstream(&mp_so
->so_snd
, m
);
862 error
= mptcp_output(mpte
);
866 if (mp_so
->so_state
& SS_ISCONNECTING
) {
867 if (mp_so
->so_state
& SS_NBIO
)
870 error
= sbwait(&mp_so
->so_snd
);
884 * Mark the MPTCP connection as being incapable of further output.
887 mptcp_usr_shutdown(struct socket
*mp_so
)
889 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
893 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
897 mpte
= mptompte(mpp
);
898 VERIFY(mpte
!= NULL
);
900 socantsendmore(mp_so
);
902 mpte
= mptcp_usrclosed(mpte
);
904 error
= mptcp_output(mpte
);
910 * Copy the contents of uio into a properly sized mbuf chain.
913 mptcp_uiotombuf(struct uio
*uio
, int how
, int space
, uint32_t align
,
916 struct mbuf
*m
, *mb
, *nm
= NULL
, *mtail
= NULL
;
917 user_ssize_t resid
, tot
, len
, progress
; /* must be user_ssize_t */
920 VERIFY(top
!= NULL
&& *top
== NULL
);
923 * space can be zero or an arbitrary large value bound by
924 * the total data supplied by the uio.
926 resid
= uio_resid(uio
);
928 tot
= imin(resid
, space
);
933 * The smallest unit is a single mbuf with pkthdr.
934 * We can't align past it.
940 * Give us the full allocation or nothing.
941 * If space is zero return the smallest empty mbuf.
943 if ((len
= tot
+ align
) == 0)
946 /* Loop and append maximum sized mbufs to the chain tail. */
948 uint32_t m_needed
= 1;
950 if (njcl
> 0 && len
> MBIGCLBYTES
)
951 mb
= m_getpackets_internal(&m_needed
, 1,
952 how
, 1, M16KCLBYTES
);
953 else if (len
> MCLBYTES
)
954 mb
= m_getpackets_internal(&m_needed
, 1,
955 how
, 1, MBIGCLBYTES
);
956 else if (len
>= (signed)MINCLSIZE
)
957 mb
= m_getpackets_internal(&m_needed
, 1,
960 mb
= m_gethdr(how
, MT_DATA
);
962 /* Fail the whole operation if one mbuf can't be allocated. */
970 VERIFY(mb
->m_flags
& M_PKTHDR
);
971 len
-= ((mb
->m_flags
& M_EXT
) ? mb
->m_ext
.ext_size
: MHLEN
);
983 /* Fill all mbufs with uio data and update header information. */
984 for (mb
= m
; mb
!= NULL
; mb
= mb
->m_next
) {
985 len
= imin(M_TRAILINGSPACE(mb
), tot
- progress
);
987 error
= uiomove(mtod(mb
, char *), len
, uio
);
993 /* each mbuf is M_PKTHDR chained via m_next */
995 mb
->m_pkthdr
.len
= len
;
999 VERIFY(progress
== tot
);
1005 * MPTCP socket protocol-user socket send routine, derived from sosend().
1008 mptcp_usr_sosend(struct socket
*mp_so
, struct sockaddr
*addr
, struct uio
*uio
,
1009 struct mbuf
*top
, struct mbuf
*control
, int flags
)
1011 #pragma unused(addr)
1014 int error
, sendflags
;
1015 struct proc
*p
= current_proc();
1018 /* UIO is required for now, due to per-mbuf M_PKTHDR constrains */
1019 if (uio
== NULL
|| top
!= NULL
) {
1023 resid
= uio_resid(uio
);
1025 socket_lock(mp_so
, 1);
1026 so_update_last_owner_locked(mp_so
, p
);
1027 so_update_policy(mp_so
);
1029 VERIFY(mp_so
->so_type
== SOCK_STREAM
);
1030 VERIFY(!(mp_so
->so_flags
& SOF_MP_SUBFLOW
));
1032 if ((flags
& (MSG_OOB
|MSG_DONTROUTE
|MSG_HOLD
|MSG_SEND
|MSG_FLUSH
)) ||
1033 (mp_so
->so_flags
& SOF_ENABLE_MSGS
)) {
1035 socket_unlock(mp_so
, 1);
1040 * In theory resid should be unsigned. However, space must be
1041 * signed, as it might be less than 0 if we over-committed, and we
1042 * must use a signed comparison of space and resid. On the other
1043 * hand, a negative resid causes us to loop sending 0-length
1044 * segments to the protocol.
1046 if (resid
< 0 || (flags
& MSG_EOR
) || control
!= NULL
) {
1048 socket_unlock(mp_so
, 1);
1052 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgsnd
);
1055 error
= sosendcheck(mp_so
, NULL
, resid
, 0, 0, flags
,
1060 space
= sbspace(&mp_so
->so_snd
);
1062 socket_unlock(mp_so
, 0);
1064 * Copy the data from userland into an mbuf chain.
1066 error
= mptcp_uiotombuf(uio
, M_WAITOK
, space
, 0, &top
);
1068 socket_lock(mp_so
, 0);
1071 VERIFY(top
!= NULL
);
1072 space
-= resid
- uio_resid(uio
);
1073 resid
= uio_resid(uio
);
1074 socket_lock(mp_so
, 0);
1077 * Compute flags here, for pru_send and NKEs.
1079 sendflags
= (resid
> 0 && space
> 0) ?
1080 PRUS_MORETOCOME
: 0;
1083 * Socket filter processing
1085 VERIFY(control
== NULL
);
1086 error
= sflt_data_out(mp_so
, NULL
, &top
, &control
, 0);
1088 if (error
== EJUSTRETURN
) {
1091 /* always free control if any */
1095 if (control
!= NULL
) {
1101 * Pass data to protocol.
1103 error
= (*mp_so
->so_proto
->pr_usrreqs
->pru_send
)
1104 (mp_so
, sendflags
, top
, NULL
, NULL
, p
);
1109 } while (resid
!= 0 && space
> 0);
1110 } while (resid
!= 0);
1114 sbunlock(&mp_so
->so_snd
, FALSE
); /* will unlock socket */
1116 socket_unlock(mp_so
, 1);
1120 if (control
!= NULL
)
1123 soclearfastopen(mp_so
);
1129 * Called to filter SOPT_{SET,GET} for SOL_SOCKET level socket options.
1130 * This routine simply indicates to the caller whether or not to proceed
1131 * further with the given socket option. This is invoked by sosetoptlock()
1132 * and sogetoptlock().
1135 mptcp_usr_socheckopt(struct socket
*mp_so
, struct sockopt
*sopt
)
1137 #pragma unused(mp_so)
1140 VERIFY(sopt
->sopt_level
== SOL_SOCKET
);
1143 * We could check for sopt_dir (set/get) here, but we'll just
1144 * let the caller deal with it as appropriate; therefore the
1145 * following is a superset of the socket options which we
1146 * allow for set/get.
1148 * XXX: adi@apple.com
1150 * Need to consider the following cases:
1152 * a. Certain socket options don't have a clear definition
1153 * on the expected behavior post connect(2). At the time
1154 * those options are issued on the MP socket, there may
1155 * be existing subflow sockets that are already connected.
1157 switch (sopt
->sopt_name
) {
1158 case SO_LINGER
: /* MP */
1159 case SO_LINGER_SEC
: /* MP */
1160 case SO_TYPE
: /* MP */
1161 case SO_NREAD
: /* MP */
1162 case SO_NWRITE
: /* MP */
1163 case SO_ERROR
: /* MP */
1164 case SO_SNDBUF
: /* MP */
1165 case SO_RCVBUF
: /* MP */
1166 case SO_SNDLOWAT
: /* MP */
1167 case SO_RCVLOWAT
: /* MP */
1168 case SO_SNDTIMEO
: /* MP */
1169 case SO_RCVTIMEO
: /* MP */
1170 case SO_NKE
: /* MP */
1171 case SO_NOSIGPIPE
: /* MP */
1172 case SO_NOADDRERR
: /* MP */
1173 case SO_LABEL
: /* MP */
1174 case SO_PEERLABEL
: /* MP */
1175 case SO_DEFUNCTOK
: /* MP */
1176 case SO_ISDEFUNCT
: /* MP */
1177 case SO_TRAFFIC_CLASS_DBG
: /* MP */
1178 case SO_DELEGATED
: /* MP */
1179 case SO_DELEGATED_UUID
: /* MP */
1181 case SO_NECP_ATTRIBUTES
:
1182 case SO_NECP_CLIENTUUID
:
1185 * Tell the caller that these options are to be processed.
1189 case SO_DEBUG
: /* MP + subflow */
1190 case SO_KEEPALIVE
: /* MP + subflow */
1191 case SO_USELOOPBACK
: /* MP + subflow */
1192 case SO_RANDOMPORT
: /* MP + subflow */
1193 case SO_TRAFFIC_CLASS
: /* MP + subflow */
1194 case SO_RECV_TRAFFIC_CLASS
: /* MP + subflow */
1195 case SO_PRIVILEGED_TRAFFIC_CLASS
: /* MP + subflow */
1196 case SO_RECV_ANYIF
: /* MP + subflow */
1197 case SO_RESTRICTIONS
: /* MP + subflow */
1198 case SO_FLUSH
: /* MP + subflow */
1199 case SO_NOWAKEFROMSLEEP
:
1200 case SO_NOAPNFALLBK
:
1201 case SO_MARK_CELLFALLBACK
:
1203 * Tell the caller that these options are to be processed;
1204 * these will also be recorded later by mptcp_setopt().
1206 * NOTE: Only support integer option value for now.
1208 if (sopt
->sopt_valsize
!= sizeof (int))
1214 * Tell the caller to stop immediately and return an error.
1216 error
= ENOPROTOOPT
;
1224 * Issue SOPT_SET for all MPTCP subflows (for integer option values.)
1227 mptcp_setopt_apply(struct mptses
*mpte
, struct mptopt
*mpo
)
1229 struct socket
*mp_so
;
1230 struct mptsub
*mpts
;
1234 /* just bail now if this isn't applicable to subflow sockets */
1235 if (!(mpo
->mpo_flags
& MPOF_SUBFLOW_OK
)) {
1236 error
= ENOPROTOOPT
;
1241 * Skip those that are handled internally; these options
1242 * should not have been recorded and marked with the
1243 * MPOF_SUBFLOW_OK by mptcp_setopt(), but just in case.
1245 if (mpo
->mpo_level
== SOL_SOCKET
&&
1246 (mpo
->mpo_name
== SO_NOSIGPIPE
|| mpo
->mpo_name
== SO_NOADDRERR
)) {
1247 error
= ENOPROTOOPT
;
1251 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1252 mp_so
= mptetoso(mpte
);
1255 * Don't bother going further if there's no subflow; mark the option
1256 * with MPOF_INTERIM so that we know whether or not to remove this
1257 * option upon encountering an error while issuing it during subflow
1260 if (mpte
->mpte_numflows
== 0) {
1261 VERIFY(TAILQ_EMPTY(&mpte
->mpte_subflows
));
1262 mpo
->mpo_flags
|= MPOF_INTERIM
;
1263 /* return success */
1267 bzero(&smpo
, sizeof (smpo
));
1268 smpo
.mpo_flags
|= MPOF_SUBFLOW_OK
;
1269 smpo
.mpo_level
= mpo
->mpo_level
;
1270 smpo
.mpo_name
= mpo
->mpo_name
;
1272 /* grab exisiting values in case we need to rollback */
1273 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1276 mpts
->mpts_flags
&= ~(MPTSF_SOPT_OLDVAL
|MPTSF_SOPT_INPROG
);
1277 mpts
->mpts_oldintval
= 0;
1278 smpo
.mpo_intval
= 0;
1279 VERIFY(mpts
->mpts_socket
!= NULL
);
1280 so
= mpts
->mpts_socket
;
1281 if (mptcp_subflow_sogetopt(mpte
, so
, &smpo
) == 0) {
1282 mpts
->mpts_flags
|= MPTSF_SOPT_OLDVAL
;
1283 mpts
->mpts_oldintval
= smpo
.mpo_intval
;
1287 /* apply socket option */
1288 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1291 mpts
->mpts_flags
|= MPTSF_SOPT_INPROG
;
1292 VERIFY(mpts
->mpts_socket
!= NULL
);
1293 so
= mpts
->mpts_socket
;
1294 error
= mptcp_subflow_sosetopt(mpte
, mpts
, mpo
);
1299 /* cleanup, and rollback if needed */
1300 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1303 if (!(mpts
->mpts_flags
& MPTSF_SOPT_INPROG
)) {
1304 /* clear in case it's set */
1305 mpts
->mpts_flags
&= ~MPTSF_SOPT_OLDVAL
;
1306 mpts
->mpts_oldintval
= 0;
1309 if (!(mpts
->mpts_flags
& MPTSF_SOPT_OLDVAL
)) {
1310 mpts
->mpts_flags
&= ~MPTSF_SOPT_INPROG
;
1311 VERIFY(mpts
->mpts_oldintval
== 0);
1314 /* error during sosetopt, so roll it back */
1316 VERIFY(mpts
->mpts_socket
!= NULL
);
1317 so
= mpts
->mpts_socket
;
1318 smpo
.mpo_intval
= mpts
->mpts_oldintval
;
1319 mptcp_subflow_sosetopt(mpte
, mpts
, &smpo
);
1321 mpts
->mpts_oldintval
= 0;
1322 mpts
->mpts_flags
&= ~(MPTSF_SOPT_OLDVAL
|MPTSF_SOPT_INPROG
);
1330 * Handle SOPT_SET for socket options issued on MP socket.
1333 mptcp_setopt(struct mptses
*mpte
, struct sockopt
*sopt
)
1335 int error
= 0, optval
= 0, level
, optname
, rec
= 1;
1336 struct mptopt smpo
, *mpo
= NULL
;
1337 struct socket
*mp_so
;
1339 level
= sopt
->sopt_level
;
1340 optname
= sopt
->sopt_name
;
1342 VERIFY(sopt
->sopt_dir
== SOPT_SET
);
1343 VERIFY(level
== SOL_SOCKET
|| level
== IPPROTO_TCP
);
1344 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1345 mp_so
= mptetoso(mpte
);
1348 * Record socket options which are applicable to subflow sockets so
1349 * that we can replay them for new ones; see mptcp_usr_socheckopt()
1350 * for the list of eligible socket-level options.
1352 if (level
== SOL_SOCKET
) {
1356 case SO_USELOOPBACK
:
1358 case SO_TRAFFIC_CLASS
:
1359 case SO_RECV_TRAFFIC_CLASS
:
1360 case SO_PRIVILEGED_TRAFFIC_CLASS
:
1362 case SO_RESTRICTIONS
:
1363 case SO_NOWAKEFROMSLEEP
:
1364 case SO_NOAPNFALLBK
:
1365 case SO_MARK_CELLFALLBACK
:
1369 /* don't record it */
1373 /* Next ones, record at MPTCP-level */
1375 case SO_NECP_CLIENTUUID
:
1376 if (!uuid_is_null(mpsotomppcb(mp_so
)->necp_client_uuid
)) {
1381 error
= sooptcopyin(sopt
, &mpsotomppcb(mp_so
)->necp_client_uuid
,
1382 sizeof(uuid_t
), sizeof(uuid_t
));
1387 mpsotomppcb(mp_so
)->necp_cb
= mptcp_session_necp_cb
;
1388 error
= necp_client_register_multipath_cb(mp_so
->last_pid
,
1389 mpsotomppcb(mp_so
)->necp_client_uuid
,
1390 mpsotomppcb(mp_so
));
1394 if (uuid_is_null(mpsotomppcb(mp_so
)->necp_client_uuid
)) {
1400 case SO_NECP_ATTRIBUTES
:
1403 /* nothing to do; just return */
1409 case TCP_RXT_FINDROP
:
1413 case TCP_CONNECTIONTIMEOUT
:
1414 case TCP_RXT_CONNDROPTIME
:
1415 case PERSIST_TIMEOUT
:
1416 case TCP_ADAPTIVE_READ_TIMEOUT
:
1417 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1418 /* eligible; record it */
1420 case TCP_NOTSENT_LOWAT
:
1421 /* record at MPTCP level */
1422 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1431 mp_so
->so_flags
&= ~SOF_NOTSENT_LOWAT
;
1432 error
= mptcp_set_notsent_lowat(mpte
,0);
1434 mp_so
->so_flags
|= SOF_NOTSENT_LOWAT
;
1435 error
= mptcp_set_notsent_lowat(mpte
,
1440 case MPTCP_SERVICE_TYPE
:
1441 /* record at MPTCP level */
1442 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1446 if (optval
< 0 || optval
>= MPTCP_SVCTYPE_MAX
) {
1451 mpte
->mpte_svctype
= optval
;
1453 if (mptcp_entitlement_check(mp_so
) < 0) {
1458 mpte
->mpte_flags
|= MPTE_SVCTYPE_CHECKED
;
1463 error
= ENOPROTOOPT
;
1468 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
1469 sizeof (optval
))) != 0)
1473 /* search for an existing one; if not found, allocate */
1474 if ((mpo
= mptcp_sopt_find(mpte
, sopt
)) == NULL
)
1475 mpo
= mptcp_sopt_alloc(M_WAITOK
);
1480 mptcplog((LOG_DEBUG
, "%s: mp_so 0x%llx sopt %s val %d %s\n",
1481 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1482 mptcp_sopt2str(level
, optname
), optval
,
1483 (mpo
->mpo_flags
& MPOF_ATTACHED
) ?
1484 "updated" : "recorded"),
1485 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1487 /* initialize or update, as needed */
1488 mpo
->mpo_intval
= optval
;
1489 if (!(mpo
->mpo_flags
& MPOF_ATTACHED
)) {
1490 mpo
->mpo_level
= level
;
1491 mpo
->mpo_name
= optname
;
1492 mptcp_sopt_insert(mpte
, mpo
);
1494 VERIFY(mpo
->mpo_flags
& MPOF_ATTACHED
);
1495 /* this can be issued on the subflow socket */
1496 mpo
->mpo_flags
|= MPOF_SUBFLOW_OK
;
1499 bzero(&smpo
, sizeof (smpo
));
1501 mpo
->mpo_flags
|= MPOF_SUBFLOW_OK
;
1502 mpo
->mpo_level
= level
;
1503 mpo
->mpo_name
= optname
;
1504 mpo
->mpo_intval
= optval
;
1506 VERIFY(mpo
== NULL
|| error
== 0);
1508 /* issue this socket option on existing subflows */
1510 error
= mptcp_setopt_apply(mpte
, mpo
);
1511 if (error
!= 0 && (mpo
->mpo_flags
& MPOF_ATTACHED
)) {
1512 VERIFY(mpo
!= &smpo
);
1513 mptcp_sopt_remove(mpte
, mpo
);
1514 mptcp_sopt_free(mpo
);
1517 mpo
->mpo_flags
&= ~MPOF_INTERIM
;
1520 if (error
== 0 && mpo
!= NULL
) {
1521 mptcplog((LOG_INFO
, "%s: mp_so 0x%llx sopt %s val %d set %s\n",
1522 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1523 mptcp_sopt2str(level
, optname
), optval
,
1524 (mpo
->mpo_flags
& MPOF_INTERIM
) ?
1525 "pending" : "successful"),
1526 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1527 } else if (error
!= 0) {
1528 mptcplog((LOG_ERR
, "%s: mp_so 0x%llx sopt %s (%d, %d) val %d can't be issued error %d\n",
1529 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1530 mptcp_sopt2str(level
, optname
), level
, optname
, optval
, error
),
1531 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
1537 * Handle SOPT_GET for socket options issued on MP socket.
1540 mptcp_getopt(struct mptses
*mpte
, struct sockopt
*sopt
)
1542 int error
= 0, optval
= 0;
1544 VERIFY(sopt
->sopt_dir
== SOPT_GET
);
1545 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1548 * We only handle SOPT_GET for TCP level socket options; we should
1549 * not get here for socket level options since they are already
1550 * handled at the socket layer.
1552 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
1553 error
= ENOPROTOOPT
;
1557 switch (sopt
->sopt_name
) {
1559 case TCP_RXT_FINDROP
:
1563 case TCP_CONNECTIONTIMEOUT
:
1564 case TCP_RXT_CONNDROPTIME
:
1565 case PERSIST_TIMEOUT
:
1566 case TCP_ADAPTIVE_READ_TIMEOUT
:
1567 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1568 case TCP_NOTSENT_LOWAT
:
1569 case MPTCP_SERVICE_TYPE
:
1570 /* eligible; get the default value just in case */
1571 error
= mptcp_default_tcp_optval(mpte
, sopt
, &optval
);
1575 error
= ENOPROTOOPT
;
1579 switch (sopt
->sopt_name
) {
1580 case TCP_NOTSENT_LOWAT
:
1581 if (mptetoso(mpte
)->so_flags
& SOF_NOTSENT_LOWAT
)
1582 optval
= mptcp_get_notsent_lowat(mpte
);
1586 case MPTCP_SERVICE_TYPE
:
1587 optval
= mpte
->mpte_svctype
;
1592 * Search for a previously-issued TCP level socket option and
1593 * return the recorded option value. This assumes that the
1594 * value did not get modified by the lower layer after it was
1595 * issued at setsockopt(2) time. If not found, we'll return
1596 * the default value obtained ealier.
1601 if ((mpo
= mptcp_sopt_find(mpte
, sopt
)) != NULL
)
1602 optval
= mpo
->mpo_intval
;
1604 error
= sooptcopyout(sopt
, &optval
, sizeof (int));
1611 * Return default values for TCP socket options. Ideally we would query the
1612 * subflow TCP socket, but that requires creating a subflow socket before
1613 * connectx(2) time. To simplify things, just return the default values
1617 mptcp_default_tcp_optval(struct mptses
*mpte
, struct sockopt
*sopt
, int *optval
)
1621 VERIFY(sopt
->sopt_level
== IPPROTO_TCP
);
1622 VERIFY(sopt
->sopt_dir
== SOPT_GET
);
1623 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1625 /* try to do what tcp_newtcpcb() does */
1626 switch (sopt
->sopt_name
) {
1628 case TCP_RXT_FINDROP
:
1631 case TCP_CONNECTIONTIMEOUT
:
1632 case TCP_RXT_CONNDROPTIME
:
1633 case TCP_NOTSENT_LOWAT
:
1634 case TCP_ADAPTIVE_READ_TIMEOUT
:
1635 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1636 case MPTCP_SERVICE_TYPE
:
1641 *optval
= mptcp_subflow_keeptime
;
1644 case PERSIST_TIMEOUT
:
1645 *optval
= tcp_max_persist_timeout
;
1649 error
= ENOPROTOOPT
;
1656 * MPTCP SOPT_{SET,GET} socket option handler, for options issued on the MP
1657 * socket, at SOL_SOCKET and IPPROTO_TCP levels. The former is restricted
1658 * to those that are allowed by mptcp_usr_socheckopt().
1661 mptcp_ctloutput(struct socket
*mp_so
, struct sockopt
*sopt
)
1663 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
1664 struct mptses
*mpte
;
1667 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
1671 mpte
= mptompte(mpp
);
1672 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1674 /* we only handle socket and TCP-level socket options for MPTCP */
1675 if (sopt
->sopt_level
!= SOL_SOCKET
&& sopt
->sopt_level
!= IPPROTO_TCP
) {
1676 mptcplog((LOG_DEBUG
, "MPTCP Socket: "
1677 "%s: mp_so 0x%llx sopt %s level not "
1678 "handled\n", __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1679 mptcp_sopt2str(sopt
->sopt_level
, sopt
->sopt_name
)),
1680 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1685 switch (sopt
->sopt_dir
) {
1687 error
= mptcp_setopt(mpte
, sopt
);
1691 error
= mptcp_getopt(mpte
, sopt
);
1699 mptcp_sopt2str(int level
, int optname
)
1705 return ("SO_LINGER");
1707 return ("SO_LINGER_SEC");
1709 return ("SO_DEBUG");
1711 return ("SO_KEEPALIVE");
1712 case SO_USELOOPBACK
:
1713 return ("SO_USELOOPBACK");
1717 return ("SO_NREAD");
1719 return ("SO_NWRITE");
1721 return ("SO_ERROR");
1723 return ("SO_SNDBUF");
1725 return ("SO_RCVBUF");
1727 return ("SO_SNDLOWAT");
1729 return ("SO_RCVLOWAT");
1731 return ("SO_SNDTIMEO");
1733 return ("SO_RCVTIMEO");
1737 return ("SO_NOSIGPIPE");
1739 return ("SO_NOADDRERR");
1740 case SO_RESTRICTIONS
:
1741 return ("SO_RESTRICTIONS");
1743 return ("SO_LABEL");
1745 return ("SO_PEERLABEL");
1747 return ("SO_RANDOMPORT");
1748 case SO_TRAFFIC_CLASS
:
1749 return ("SO_TRAFFIC_CLASS");
1750 case SO_RECV_TRAFFIC_CLASS
:
1751 return ("SO_RECV_TRAFFIC_CLASS");
1752 case SO_TRAFFIC_CLASS_DBG
:
1753 return ("SO_TRAFFIC_CLASS_DBG");
1754 case SO_PRIVILEGED_TRAFFIC_CLASS
:
1755 return ("SO_PRIVILEGED_TRAFFIC_CLASS");
1757 return ("SO_DEFUNCTOK");
1759 return ("SO_ISDEFUNCT");
1760 case SO_OPPORTUNISTIC
:
1761 return ("SO_OPPORTUNISTIC");
1763 return ("SO_FLUSH");
1765 return ("SO_RECV_ANYIF");
1766 case SO_NOWAKEFROMSLEEP
:
1767 return ("SO_NOWAKEFROMSLEEP");
1768 case SO_NOAPNFALLBK
:
1769 return ("SO_NOAPNFALLBK");
1770 case SO_MARK_CELLFALLBACK
:
1771 return ("SO_CELLFALLBACK");
1773 return ("SO_DELEGATED");
1774 case SO_DELEGATED_UUID
:
1775 return ("SO_DELEGATED_UUID");
1777 case SO_NECP_ATTRIBUTES
:
1778 return ("SO_NECP_ATTRIBUTES");
1779 case SO_NECP_CLIENTUUID
:
1780 return ("SO_NECP_CLIENTUUID");
1788 return ("TCP_NODELAY");
1790 return ("TCP_KEEPALIVE");
1792 return ("TCP_KEEPINTVL");
1794 return ("TCP_KEEPCNT");
1795 case TCP_CONNECTIONTIMEOUT
:
1796 return ("TCP_CONNECTIONTIMEOUT");
1797 case TCP_RXT_CONNDROPTIME
:
1798 return ("TCP_RXT_CONNDROPTIME");
1799 case PERSIST_TIMEOUT
:
1800 return ("PERSIST_TIMEOUT");
1801 case TCP_NOTSENT_LOWAT
:
1802 return ("NOTSENT_LOWAT");
1803 case TCP_ADAPTIVE_READ_TIMEOUT
:
1804 return ("ADAPTIVE_READ_TIMEOUT");
1805 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1806 return ("ADAPTIVE_WRITE_TIMEOUT");
1807 case MPTCP_SERVICE_TYPE
:
1808 return ("MPTCP_SERVICE_TYPE");
1818 mptcp_usr_preconnect(struct socket
*mp_so
)
1820 struct mptsub
*mpts
= NULL
;
1821 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
1822 struct mptses
*mpte
;
1824 struct tcpcb
*tp
= NULL
;
1827 mpte
= mptompte(mpp
);
1828 VERIFY(mpte
!= NULL
);
1829 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1831 mpts
= mptcp_get_subflow(mpte
, NULL
, NULL
);
1833 mptcplog((LOG_ERR
, "%s: mp_so 0x%llx invalid preconnect ",
1834 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
)),
1835 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
1838 mpts
->mpts_flags
&= ~MPTSF_TFO_REQD
;
1839 so
= mpts
->mpts_socket
;
1840 tp
= intotcpcb(sotoinpcb(so
));
1841 tp
->t_mpflags
&= ~TMPF_TFO_REQUEST
;
1842 error
= tcp_output(sototcpcb(so
));
1844 soclearfastopen(mp_so
);