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_default_tcp_optval(struct mptses
*, struct sockopt
*, int *);
82 static int mptcp_usr_preconnect(struct socket
*so
);
84 struct pr_usrreqs mptcp_usrreqs
= {
85 .pru_attach
= mptcp_usr_attach
,
86 .pru_connectx
= mptcp_usr_connectx
,
87 .pru_control
= mptcp_usr_control
,
88 .pru_detach
= mptcp_usr_detach
,
89 .pru_disconnect
= mptcp_usr_disconnect
,
90 .pru_disconnectx
= mptcp_usr_disconnectx
,
91 .pru_peeraddr
= mp_getpeeraddr
,
92 .pru_rcvd
= mptcp_usr_rcvd
,
93 .pru_send
= mptcp_usr_send
,
94 .pru_shutdown
= mptcp_usr_shutdown
,
95 .pru_sockaddr
= mp_getsockaddr
,
96 .pru_sosend
= mptcp_usr_sosend
,
97 .pru_soreceive
= soreceive
,
98 .pru_socheckopt
= mptcp_usr_socheckopt
,
99 .pru_preconnect
= mptcp_usr_preconnect
,
103 #if (DEVELOPMENT || DEBUG)
104 static int mptcp_disable_entitlements
= 0;
105 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, disable_entitlements
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
106 &mptcp_disable_entitlements
, 0, "Disable Multipath TCP Entitlement Checking");
109 int mptcp_developer_mode
= 0;
110 SYSCTL_INT(_net_inet_mptcp
, OID_AUTO
, allow_aggregate
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
111 &mptcp_developer_mode
, 0, "Allow the Multipath aggregation mode");
115 * Attaches an MPTCP control block to a socket.
118 mptcp_usr_attach(struct socket
*mp_so
, int proto
, struct proc
*p
)
120 #pragma unused(proto)
123 VERIFY(mpsotomppcb(mp_so
) == NULL
);
125 error
= mptcp_attach(mp_so
, p
);
132 * Might want to use a different SO_LINGER timeout than TCP's?
134 if ((mp_so
->so_options
& SO_LINGER
) && mp_so
->so_linger
== 0) {
135 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
);
190 if (mp_so
->so_snd
.sb_preconn_hiwat
== 0) {
191 soreserve_preconnect(mp_so
, 2048);
194 if ((mp_so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0) {
195 mp_so
->so_rcv
.sb_flags
|= SB_AUTOSIZE
;
197 if ((mp_so
->so_snd
.sb_flags
& SB_USRSIZE
) == 0) {
198 mp_so
->so_snd
.sb_flags
|= SB_AUTOSIZE
;
202 * MPTCP socket buffers cannot be compressed, due to the
203 * fact that each mbuf chained via m_next is a M_PKTHDR
204 * which carries some MPTCP metadata.
206 mp_so
->so_snd
.sb_flags
|= SB_NOCOMPRESS
;
207 mp_so
->so_rcv
.sb_flags
|= SB_NOCOMPRESS
;
209 if ((error
= mp_pcballoc(mp_so
, &mtcbinfo
)) != 0) {
213 mpp
= mpsotomppcb(mp_so
);
215 mpte
= (struct mptses
*)mpp
->mpp_pcbe
;
216 VERIFY(mpte
!= NULL
);
217 mp_tp
= mpte
->mpte_mptcb
;
218 VERIFY(mp_tp
!= NULL
);
224 mptcp_entitlement_check(struct socket
*mp_so
)
226 struct mptses
*mpte
= mpsotompte(mp_so
);
228 if (soopt_cred_check(mp_so
, PRIV_NET_RESTRICTED_MULTIPATH_EXTENDED
, TRUE
) == 0) {
230 * This means the app has the extended entitlement. Thus,
231 * it's a first party app and can run without restrictions.
233 mpte
->mpte_flags
|= MPTE_FIRSTPARTY
;
237 #if (DEVELOPMENT || DEBUG)
238 if (mptcp_disable_entitlements
) {
243 if (soopt_cred_check(mp_so
, PRIV_NET_PRIVILEGED_MULTIPATH
, TRUE
)) {
244 mptcplog((LOG_NOTICE
, "%s Multipath Capability needed\n", __func__
),
245 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
249 if (mpte
->mpte_svctype
> MPTCP_SVCTYPE_INTERACTIVE
&&
250 mptcp_developer_mode
== 0) {
251 mptcplog((LOG_NOTICE
, "%s need to set allow_aggregate sysctl\n",
252 __func__
), MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
257 mptcplog((LOG_NOTICE
, "%s entitlement granted for %u\n", __func__
, mpte
->mpte_svctype
),
258 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
264 * Common subroutine to open a MPTCP connection to one of the remote hosts
265 * specified by dst_sl. This includes allocating and establishing a
266 * subflow TCP connection, either initially to establish MPTCP connection,
267 * or to join an existing one. Returns a connection handle upon success.
270 mptcp_connectx(struct mptses
*mpte
, struct sockaddr
*src
,
271 struct sockaddr
*dst
, uint32_t ifscope
, sae_connid_t
*pcid
)
273 struct socket
*mp_so
= mptetoso(mpte
);
277 VERIFY(pcid
!= NULL
);
279 mptcplog((LOG_DEBUG
, "%s: mp_so 0x%llx\n", __func__
,
280 (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
)),
281 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
282 DTRACE_MPTCP2(connectx
, struct mptses
*, mpte
, struct socket
*, mp_so
);
284 error
= mptcp_subflow_add(mpte
, src
, dst
, ifscope
, pcid
);
290 * User-protocol pru_connectx callback.
293 mptcp_usr_connectx(struct socket
*mp_so
, struct sockaddr
*src
,
294 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope
,
295 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
296 uint32_t arglen
, struct uio
*auio
, user_ssize_t
*bytes_written
)
298 #pragma unused(p, aid, flags, arg, arglen)
299 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
300 struct mptses
*mpte
= NULL
;
301 struct mptcb
*mp_tp
= NULL
;
302 user_ssize_t datalen
;
305 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
306 mptcplog((LOG_ERR
, "%s state %d\n", __func__
,
307 mpp
? mpp
->mpp_state
: -1),
308 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
312 mpte
= mptompte(mpp
);
313 VERIFY(mpte
!= NULL
);
314 mpte_lock_assert_held(mpte
);
316 mp_tp
= mpte
->mpte_mptcb
;
317 VERIFY(mp_tp
!= NULL
);
319 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
320 mptcplog((LOG_ERR
, "%s fell back to TCP\n", __func__
),
321 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
326 if (dst
->sa_family
!= AF_INET
&& dst
->sa_family
!= AF_INET6
) {
327 error
= EAFNOSUPPORT
;
331 if (dst
->sa_family
== AF_INET
&&
332 dst
->sa_len
!= sizeof(mpte
->__mpte_dst_v4
)) {
333 mptcplog((LOG_ERR
, "%s IPv4 dst len %u\n", __func__
,
335 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
340 if (dst
->sa_family
== AF_INET6
&&
341 dst
->sa_len
!= sizeof(mpte
->__mpte_dst_v6
)) {
342 mptcplog((LOG_ERR
, "%s IPv6 dst len %u\n", __func__
,
344 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
349 if (!(mpte
->mpte_flags
& MPTE_SVCTYPE_CHECKED
)) {
350 if (mptcp_entitlement_check(mp_so
) < 0) {
355 mpte
->mpte_flags
|= MPTE_SVCTYPE_CHECKED
;
358 if ((mp_so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
)) == 0) {
359 memcpy(&mpte
->mpte_dst
, dst
, dst
->sa_len
);
363 if (src
->sa_family
!= AF_INET
&& src
->sa_family
!= AF_INET6
) {
364 error
= EAFNOSUPPORT
;
368 if (src
->sa_family
== AF_INET
&&
369 src
->sa_len
!= sizeof(mpte
->__mpte_src_v4
)) {
370 mptcplog((LOG_ERR
, "%s IPv4 src len %u\n", __func__
,
372 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
377 if (src
->sa_family
== AF_INET6
&&
378 src
->sa_len
!= sizeof(mpte
->__mpte_src_v6
)) {
379 mptcplog((LOG_ERR
, "%s IPv6 src len %u\n", __func__
,
381 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
386 if ((mp_so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
)) == 0) {
387 memcpy(&mpte
->mpte_src
, src
, src
->sa_len
);
391 error
= mptcp_connectx(mpte
, src
, dst
, ifscope
, pcid
);
393 /* If there is data, copy it */
395 datalen
= uio_resid(auio
);
396 socket_unlock(mp_so
, 0);
397 error
= mp_so
->so_proto
->pr_usrreqs
->pru_sosend(mp_so
, NULL
,
398 (uio_t
) auio
, NULL
, NULL
, 0);
400 if (error
== 0 || error
== EWOULDBLOCK
) {
401 *bytes_written
= datalen
- uio_resid(auio
);
404 if (error
== EWOULDBLOCK
) {
408 socket_lock(mp_so
, 0);
416 * Handle SIOCGASSOCIDS ioctl for PF_MULTIPATH domain.
419 mptcp_getassocids(struct mptses
*mpte
, uint32_t *cnt
, user_addr_t aidp
)
421 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
423 /* MPTCP has at most 1 association */
424 *cnt
= (mpte
->mpte_associd
!= SAE_ASSOCID_ANY
) ? 1 : 0;
426 /* just asking how many there are? */
427 if (aidp
== USER_ADDR_NULL
) {
431 return copyout(&mpte
->mpte_associd
, aidp
,
432 sizeof(mpte
->mpte_associd
));
436 * Handle SIOCGCONNIDS ioctl for PF_MULTIPATH domain.
439 mptcp_getconnids(struct mptses
*mpte
, sae_associd_t aid
, uint32_t *cnt
,
445 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
447 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
&&
448 aid
!= mpte
->mpte_associd
) {
452 *cnt
= mpte
->mpte_numflows
;
454 /* just asking how many there are? */
455 if (cidp
== USER_ADDR_NULL
) {
459 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
460 if ((error
= copyout(&mpts
->mpts_connid
, cidp
,
461 sizeof(mpts
->mpts_connid
))) != 0) {
465 cidp
+= sizeof(mpts
->mpts_connid
);
472 * Handle SIOCGCONNINFO ioctl for PF_MULTIPATH domain.
475 mptcp_getconninfo(struct mptses
*mpte
, sae_connid_t
*cid
, uint32_t *flags
,
476 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
477 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
478 user_addr_t aux_data
, uint32_t *aux_len
)
490 if (*cid
== SAE_CONNID_ALL
) {
491 struct socket
*mp_so
= mptetoso(mpte
);
492 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
493 struct conninfo_multipathtcp mptcp_ci
;
495 if (*aux_len
!= 0 && *aux_len
!= sizeof(mptcp_ci
)) {
499 if (mp_so
->so_state
& SS_ISCONNECTING
) {
500 *flags
|= CIF_CONNECTING
;
502 if (mp_so
->so_state
& SS_ISCONNECTED
) {
503 *flags
|= CIF_CONNECTED
;
505 if (mp_so
->so_state
& SS_ISDISCONNECTING
) {
506 *flags
|= CIF_DISCONNECTING
;
508 if (mp_so
->so_state
& SS_ISDISCONNECTED
) {
509 *flags
|= CIF_DISCONNECTED
;
511 if (!(mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
)) {
512 *flags
|= CIF_MP_CAPABLE
;
514 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
515 *flags
|= CIF_MP_DEGRADED
;
521 *aux_type
= CIAUX_MPTCP
;
522 *aux_len
= sizeof(mptcp_ci
);
524 if (aux_data
!= USER_ADDR_NULL
) {
526 int initial_info_set
= 0;
528 bzero(&mptcp_ci
, sizeof(mptcp_ci
));
529 mptcp_ci
.mptcpci_subflow_count
= mpte
->mpte_numflows
;
530 mptcp_ci
.mptcpci_switch_count
= mpte
->mpte_subflow_switches
;
532 VERIFY(sizeof(mptcp_ci
.mptcpci_itfstats
) == sizeof(mpte
->mpte_itfstats
));
533 memcpy(mptcp_ci
.mptcpci_itfstats
, mpte
->mpte_itfstats
, sizeof(mptcp_ci
.mptcpci_itfstats
));
535 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
536 if (i
>= sizeof(mptcp_ci
.mptcpci_subflow_connids
) / sizeof(sae_connid_t
)) {
539 mptcp_ci
.mptcpci_subflow_connids
[i
] = mpts
->mpts_connid
;
541 if (mpts
->mpts_flags
& MPTSF_INITIAL_SUB
) {
542 inp
= sotoinpcb(mpts
->mpts_socket
);
544 mptcp_ci
.mptcpci_init_rxbytes
= inp
->inp_stat
->rxbytes
;
545 mptcp_ci
.mptcpci_init_txbytes
= inp
->inp_stat
->txbytes
;
546 initial_info_set
= 1;
549 mptcpstats_update(mptcp_ci
.mptcpci_itfstats
, mpts
);
554 if (initial_info_set
== 0) {
555 mptcp_ci
.mptcpci_init_rxbytes
= mpte
->mpte_init_rxbytes
;
556 mptcp_ci
.mptcpci_init_txbytes
= mpte
->mpte_init_txbytes
;
559 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
560 mptcp_ci
.mptcpci_flags
|= MPTCPCI_FIRSTPARTY
;
563 error
= copyout(&mptcp_ci
, aux_data
, sizeof(mptcp_ci
));
565 mptcplog((LOG_ERR
, "%s copyout failed: %d\n",
567 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
575 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
576 if (mpts
->mpts_connid
== *cid
|| *cid
== SAE_CONNID_ANY
) {
581 return (*cid
== SAE_CONNID_ANY
) ? ENXIO
: EINVAL
;
584 so
= mpts
->mpts_socket
;
587 if (inp
->inp_vflag
& INP_IPV4
) {
588 error
= in_getconninfo(so
, SAE_CONNID_ANY
, flags
, ifindex
,
589 soerror
, src
, src_len
, dst
, dst_len
,
590 aux_type
, aux_data
, aux_len
);
592 error
= in6_getconninfo(so
, SAE_CONNID_ANY
, flags
, ifindex
,
593 soerror
, src
, src_len
, dst
, dst_len
,
594 aux_type
, aux_data
, aux_len
);
598 mptcplog((LOG_ERR
, "%s error from in_getconninfo %d\n",
600 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
604 if (mpts
->mpts_flags
& MPTSF_MP_CAPABLE
) {
605 *flags
|= CIF_MP_CAPABLE
;
607 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
) {
608 *flags
|= CIF_MP_DEGRADED
;
610 if (mpts
->mpts_flags
& MPTSF_MP_READY
) {
611 *flags
|= CIF_MP_READY
;
613 if (mpts
->mpts_flags
& MPTSF_ACTIVE
) {
614 *flags
|= CIF_MP_ACTIVE
;
617 mptcplog((LOG_DEBUG
, "%s: cid %d flags %x \n", __func__
,
618 mpts
->mpts_connid
, mpts
->mpts_flags
),
619 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
625 * User-protocol pru_control callback.
628 mptcp_usr_control(struct socket
*mp_so
, u_long cmd
, caddr_t data
,
629 struct ifnet
*ifp
, struct proc
*p
)
631 #pragma unused(ifp, p)
632 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
636 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
640 mpte
= mptompte(mpp
);
641 VERIFY(mpte
!= NULL
);
643 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
646 case SIOCGASSOCIDS32
: { /* struct so_aidreq32 */
647 struct so_aidreq32 aidr
;
648 bcopy(data
, &aidr
, sizeof(aidr
));
649 error
= mptcp_getassocids(mpte
, &aidr
.sar_cnt
,
652 bcopy(&aidr
, data
, sizeof(aidr
));
657 case SIOCGASSOCIDS64
: { /* struct so_aidreq64 */
658 struct so_aidreq64 aidr
;
659 bcopy(data
, &aidr
, sizeof(aidr
));
660 error
= mptcp_getassocids(mpte
, &aidr
.sar_cnt
,
663 bcopy(&aidr
, data
, sizeof(aidr
));
668 case SIOCGCONNIDS32
: { /* struct so_cidreq32 */
669 struct so_cidreq32 cidr
;
670 bcopy(data
, &cidr
, sizeof(cidr
));
671 error
= mptcp_getconnids(mpte
, cidr
.scr_aid
, &cidr
.scr_cnt
,
674 bcopy(&cidr
, data
, sizeof(cidr
));
679 case SIOCGCONNIDS64
: { /* struct so_cidreq64 */
680 struct so_cidreq64 cidr
;
681 bcopy(data
, &cidr
, sizeof(cidr
));
682 error
= mptcp_getconnids(mpte
, cidr
.scr_aid
, &cidr
.scr_cnt
,
685 bcopy(&cidr
, data
, sizeof(cidr
));
690 case SIOCGCONNINFO32
: { /* struct so_cinforeq32 */
691 struct so_cinforeq32 cifr
;
692 bcopy(data
, &cifr
, sizeof(cifr
));
693 error
= mptcp_getconninfo(mpte
, &cifr
.scir_cid
,
694 &cifr
.scir_flags
, &cifr
.scir_ifindex
, &cifr
.scir_error
,
695 cifr
.scir_src
, &cifr
.scir_src_len
, cifr
.scir_dst
,
696 &cifr
.scir_dst_len
, &cifr
.scir_aux_type
, cifr
.scir_aux_data
,
699 bcopy(&cifr
, data
, sizeof(cifr
));
704 case SIOCGCONNINFO64
: { /* struct so_cinforeq64 */
705 struct so_cinforeq64 cifr
;
706 bcopy(data
, &cifr
, sizeof(cifr
));
707 error
= mptcp_getconninfo(mpte
, &cifr
.scir_cid
,
708 &cifr
.scir_flags
, &cifr
.scir_ifindex
, &cifr
.scir_error
,
709 cifr
.scir_src
, &cifr
.scir_src_len
, cifr
.scir_dst
,
710 &cifr
.scir_dst_len
, &cifr
.scir_aux_type
, cifr
.scir_aux_data
,
713 bcopy(&cifr
, data
, sizeof(cifr
));
727 mptcp_disconnect(struct mptses
*mpte
)
729 struct socket
*mp_so
;
733 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
735 mp_so
= mptetoso(mpte
);
736 mp_tp
= mpte
->mpte_mptcb
;
738 mptcplog((LOG_DEBUG
, "%s: mp_so 0x%llx %d\n", __func__
,
739 (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
), mp_so
->so_error
),
740 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
742 DTRACE_MPTCP3(disconnectx
, struct mptses
*, mpte
,
743 struct socket
*, mp_so
, struct mptcb
*, mp_tp
);
745 /* if we're not detached, go thru socket state checks */
746 if (!(mp_so
->so_flags
& SOF_PCBCLEARING
)) {
747 if (!(mp_so
->so_state
& (SS_ISCONNECTED
|
752 if (mp_so
->so_state
& SS_ISDISCONNECTING
) {
758 mptcp_cancel_all_timers(mp_tp
);
759 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
) {
760 mptcp_close(mpte
, mp_tp
);
761 } else if ((mp_so
->so_options
& SO_LINGER
) &&
762 mp_so
->so_linger
== 0) {
763 mptcp_drop(mpte
, mp_tp
, 0);
765 soisdisconnecting(mp_so
);
766 sbflush(&mp_so
->so_rcv
);
767 if (mptcp_usrclosed(mpte
) != NULL
) {
773 mptcp_subflow_workloop(mpte
);
781 * Wrapper function to support disconnect on socket
784 mptcp_usr_disconnect(struct socket
*mp_so
)
786 return mptcp_disconnect(mpsotompte(mp_so
));
790 * User-protocol pru_disconnectx callback.
793 mptcp_usr_disconnectx(struct socket
*mp_so
, sae_associd_t aid
, sae_connid_t cid
)
795 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
) {
799 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
) {
803 return mptcp_usr_disconnect(mp_so
);
807 mptcp_finish_usrclosed(struct mptses
*mpte
)
809 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
810 struct socket
*mp_so
= mptetoso(mpte
);
812 if (mp_tp
->mpt_state
== MPTCPS_CLOSED
) {
813 mpte
= mptcp_close(mpte
, mp_tp
);
814 } else if (mp_tp
->mpt_state
>= MPTCPS_FIN_WAIT_2
) {
815 soisdisconnected(mp_so
);
819 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
820 if ((mp_so
->so_state
& (SS_CANTRCVMORE
| SS_CANTSENDMORE
)) ==
821 (SS_CANTRCVMORE
| SS_CANTSENDMORE
)) {
822 mptcp_subflow_disconnect(mpte
, mpts
);
824 mptcp_subflow_shutdown(mpte
, mpts
);
831 * User issued close, and wish to trail thru shutdown states.
833 static struct mptses
*
834 mptcp_usrclosed(struct mptses
*mpte
)
836 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
838 mptcp_close_fsm(mp_tp
, MPCE_CLOSE
);
840 /* Not everything has been acknowledged - don't close the subflows! */
841 if (mp_tp
->mpt_sndnxt
+ 1 != mp_tp
->mpt_sndmax
) {
845 mptcp_finish_usrclosed(mpte
);
851 * After a receive, possible send some update to peer.
854 mptcp_usr_rcvd(struct socket
*mp_so
, int flags
)
856 #pragma unused(flags)
857 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
861 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
865 mpte
= mptompte(mpp
);
866 VERIFY(mpte
!= NULL
);
868 error
= mptcp_output(mpte
);
874 * Do a send by putting data in the output queue.
877 mptcp_usr_send(struct socket
*mp_so
, int prus_flags
, struct mbuf
*m
,
878 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
880 #pragma unused(nam, p)
881 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
885 if (prus_flags
& (PRUS_OOB
| PRUS_EOF
)) {
895 if (control
!= NULL
&& control
->m_len
!= 0) {
900 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
904 mpte
= mptompte(mpp
);
905 VERIFY(mpte
!= NULL
);
907 if (!(mp_so
->so_state
& SS_ISCONNECTED
) &&
908 !(mp_so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
913 mptcp_insert_dsn(mpp
, m
);
914 VERIFY(mp_so
->so_snd
.sb_flags
& SB_NOCOMPRESS
);
915 sbappendstream(&mp_so
->so_snd
, m
);
918 error
= mptcp_output(mpte
);
923 if (mp_so
->so_state
& SS_ISCONNECTING
) {
924 if (mp_so
->so_state
& SS_NBIO
) {
927 error
= sbwait(&mp_so
->so_snd
);
936 if (control
!= NULL
) {
944 * Mark the MPTCP connection as being incapable of further output.
947 mptcp_usr_shutdown(struct socket
*mp_so
)
949 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
953 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
957 mpte
= mptompte(mpp
);
958 VERIFY(mpte
!= NULL
);
960 socantsendmore(mp_so
);
962 mpte
= mptcp_usrclosed(mpte
);
964 error
= mptcp_output(mpte
);
971 * Copy the contents of uio into a properly sized mbuf chain.
974 mptcp_uiotombuf(struct uio
*uio
, int how
, int space
, uint32_t align
,
977 struct mbuf
*m
, *mb
, *nm
= NULL
, *mtail
= NULL
;
978 user_ssize_t resid
, tot
, len
, progress
; /* must be user_ssize_t */
981 VERIFY(top
!= NULL
&& *top
== NULL
);
984 * space can be zero or an arbitrary large value bound by
985 * the total data supplied by the uio.
987 resid
= uio_resid(uio
);
989 tot
= imin(resid
, space
);
995 * The smallest unit is a single mbuf with pkthdr.
996 * We can't align past it.
998 if (align
>= MHLEN
) {
1003 * Give us the full allocation or nothing.
1004 * If space is zero return the smallest empty mbuf.
1006 if ((len
= tot
+ align
) == 0) {
1010 /* Loop and append maximum sized mbufs to the chain tail. */
1012 uint32_t m_needed
= 1;
1014 if (njcl
> 0 && len
> MBIGCLBYTES
) {
1015 mb
= m_getpackets_internal(&m_needed
, 1,
1016 how
, 1, M16KCLBYTES
);
1017 } else if (len
> MCLBYTES
) {
1018 mb
= m_getpackets_internal(&m_needed
, 1,
1019 how
, 1, MBIGCLBYTES
);
1020 } else if (len
>= (signed)MINCLSIZE
) {
1021 mb
= m_getpackets_internal(&m_needed
, 1,
1024 mb
= m_gethdr(how
, MT_DATA
);
1027 /* Fail the whole operation if one mbuf can't be allocated. */
1036 VERIFY(mb
->m_flags
& M_PKTHDR
);
1037 len
-= ((mb
->m_flags
& M_EXT
) ? mb
->m_ext
.ext_size
: MHLEN
);
1038 if (mtail
!= NULL
) {
1050 /* Fill all mbufs with uio data and update header information. */
1051 for (mb
= m
; mb
!= NULL
; mb
= mb
->m_next
) {
1052 len
= imin(M_TRAILINGSPACE(mb
), tot
- progress
);
1054 error
= uiomove(mtod(mb
, char *), len
, uio
);
1060 /* each mbuf is M_PKTHDR chained via m_next */
1062 mb
->m_pkthdr
.len
= len
;
1066 VERIFY(progress
== tot
);
1072 * MPTCP socket protocol-user socket send routine, derived from sosend().
1075 mptcp_usr_sosend(struct socket
*mp_so
, struct sockaddr
*addr
, struct uio
*uio
,
1076 struct mbuf
*top
, struct mbuf
*control
, int flags
)
1078 #pragma unused(addr)
1081 int error
, sendflags
;
1082 struct proc
*p
= current_proc();
1085 /* UIO is required for now, due to per-mbuf M_PKTHDR constrains */
1086 if (uio
== NULL
|| top
!= NULL
) {
1090 resid
= uio_resid(uio
);
1092 socket_lock(mp_so
, 1);
1093 so_update_last_owner_locked(mp_so
, p
);
1094 so_update_policy(mp_so
);
1096 VERIFY(mp_so
->so_type
== SOCK_STREAM
);
1097 VERIFY(!(mp_so
->so_flags
& SOF_MP_SUBFLOW
));
1099 if ((flags
& (MSG_OOB
| MSG_DONTROUTE
| MSG_HOLD
| MSG_SEND
| MSG_FLUSH
)) ||
1100 (mp_so
->so_flags
& SOF_ENABLE_MSGS
)) {
1102 socket_unlock(mp_so
, 1);
1107 * In theory resid should be unsigned. However, space must be
1108 * signed, as it might be less than 0 if we over-committed, and we
1109 * must use a signed comparison of space and resid. On the other
1110 * hand, a negative resid causes us to loop sending 0-length
1111 * segments to the protocol.
1113 if (resid
< 0 || (flags
& MSG_EOR
) || control
!= NULL
) {
1115 socket_unlock(mp_so
, 1);
1119 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgsnd
);
1122 error
= sosendcheck(mp_so
, NULL
, resid
, 0, 0, flags
,
1128 space
= sbspace(&mp_so
->so_snd
);
1130 socket_unlock(mp_so
, 0);
1132 * Copy the data from userland into an mbuf chain.
1134 error
= mptcp_uiotombuf(uio
, M_WAITOK
, space
, 0, &top
);
1136 socket_lock(mp_so
, 0);
1139 VERIFY(top
!= NULL
);
1140 space
-= resid
- uio_resid(uio
);
1141 resid
= uio_resid(uio
);
1142 socket_lock(mp_so
, 0);
1145 * Compute flags here, for pru_send and NKEs.
1147 sendflags
= (resid
> 0 && space
> 0) ?
1148 PRUS_MORETOCOME
: 0;
1151 * Socket filter processing
1153 VERIFY(control
== NULL
);
1154 error
= sflt_data_out(mp_so
, NULL
, &top
, &control
, 0);
1156 if (error
== EJUSTRETURN
) {
1159 /* always free control if any */
1163 if (control
!= NULL
) {
1169 * Pass data to protocol.
1171 error
= (*mp_so
->so_proto
->pr_usrreqs
->pru_send
)
1172 (mp_so
, sendflags
, top
, NULL
, NULL
, p
);
1178 } while (resid
!= 0 && space
> 0);
1179 } while (resid
!= 0);
1183 sbunlock(&mp_so
->so_snd
, FALSE
); /* will unlock socket */
1185 socket_unlock(mp_so
, 1);
1191 if (control
!= NULL
) {
1195 soclearfastopen(mp_so
);
1201 * Called to filter SOPT_{SET,GET} for SOL_SOCKET level socket options.
1202 * This routine simply indicates to the caller whether or not to proceed
1203 * further with the given socket option. This is invoked by sosetoptlock()
1204 * and sogetoptlock().
1207 mptcp_usr_socheckopt(struct socket
*mp_so
, struct sockopt
*sopt
)
1209 #pragma unused(mp_so)
1212 VERIFY(sopt
->sopt_level
== SOL_SOCKET
);
1215 * We could check for sopt_dir (set/get) here, but we'll just
1216 * let the caller deal with it as appropriate; therefore the
1217 * following is a superset of the socket options which we
1218 * allow for set/get.
1220 * XXX: adi@apple.com
1222 * Need to consider the following cases:
1224 * a. Certain socket options don't have a clear definition
1225 * on the expected behavior post connect(2). At the time
1226 * those options are issued on the MP socket, there may
1227 * be existing subflow sockets that are already connected.
1229 switch (sopt
->sopt_name
) {
1230 case SO_LINGER
: /* MP */
1231 case SO_LINGER_SEC
: /* MP */
1232 case SO_TYPE
: /* MP */
1233 case SO_NREAD
: /* MP */
1234 case SO_NWRITE
: /* MP */
1235 case SO_ERROR
: /* MP */
1236 case SO_SNDBUF
: /* MP */
1237 case SO_RCVBUF
: /* MP */
1238 case SO_SNDLOWAT
: /* MP */
1239 case SO_RCVLOWAT
: /* MP */
1240 case SO_SNDTIMEO
: /* MP */
1241 case SO_RCVTIMEO
: /* MP */
1242 case SO_NKE
: /* MP */
1243 case SO_NOSIGPIPE
: /* MP */
1244 case SO_NOADDRERR
: /* MP */
1245 case SO_LABEL
: /* MP */
1246 case SO_PEERLABEL
: /* MP */
1247 case SO_DEFUNCTOK
: /* MP */
1248 case SO_ISDEFUNCT
: /* MP */
1249 case SO_TRAFFIC_CLASS_DBG
: /* MP */
1250 case SO_DELEGATED
: /* MP */
1251 case SO_DELEGATED_UUID
: /* MP */
1253 case SO_NECP_ATTRIBUTES
:
1254 case SO_NECP_CLIENTUUID
:
1257 * Tell the caller that these options are to be processed.
1261 case SO_DEBUG
: /* MP + subflow */
1262 case SO_KEEPALIVE
: /* MP + subflow */
1263 case SO_USELOOPBACK
: /* MP + subflow */
1264 case SO_RANDOMPORT
: /* MP + subflow */
1265 case SO_TRAFFIC_CLASS
: /* MP + subflow */
1266 case SO_RECV_TRAFFIC_CLASS
: /* MP + subflow */
1267 case SO_PRIVILEGED_TRAFFIC_CLASS
: /* MP + subflow */
1268 case SO_RECV_ANYIF
: /* MP + subflow */
1269 case SO_RESTRICTIONS
: /* MP + subflow */
1270 case SO_FLUSH
: /* MP + subflow */
1271 case SO_NOWAKEFROMSLEEP
:
1272 case SO_NOAPNFALLBK
:
1273 case SO_MARK_CELLFALLBACK
:
1275 * Tell the caller that these options are to be processed;
1276 * these will also be recorded later by mptcp_setopt().
1278 * NOTE: Only support integer option value for now.
1280 if (sopt
->sopt_valsize
!= sizeof(int)) {
1287 * Tell the caller to stop immediately and return an error.
1289 error
= ENOPROTOOPT
;
1297 * Issue SOPT_SET for all MPTCP subflows (for integer option values.)
1300 mptcp_setopt_apply(struct mptses
*mpte
, struct mptopt
*mpo
)
1302 struct socket
*mp_so
;
1303 struct mptsub
*mpts
;
1307 /* just bail now if this isn't applicable to subflow sockets */
1308 if (!(mpo
->mpo_flags
& MPOF_SUBFLOW_OK
)) {
1309 error
= ENOPROTOOPT
;
1314 * Skip those that are handled internally; these options
1315 * should not have been recorded and marked with the
1316 * MPOF_SUBFLOW_OK by mptcp_setopt(), but just in case.
1318 if (mpo
->mpo_level
== SOL_SOCKET
&&
1319 (mpo
->mpo_name
== SO_NOSIGPIPE
|| mpo
->mpo_name
== SO_NOADDRERR
)) {
1320 error
= ENOPROTOOPT
;
1324 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1325 mp_so
= mptetoso(mpte
);
1328 * Don't bother going further if there's no subflow; mark the option
1329 * with MPOF_INTERIM so that we know whether or not to remove this
1330 * option upon encountering an error while issuing it during subflow
1333 if (mpte
->mpte_numflows
== 0) {
1334 VERIFY(TAILQ_EMPTY(&mpte
->mpte_subflows
));
1335 mpo
->mpo_flags
|= MPOF_INTERIM
;
1336 /* return success */
1340 bzero(&smpo
, sizeof(smpo
));
1341 smpo
.mpo_flags
|= MPOF_SUBFLOW_OK
;
1342 smpo
.mpo_level
= mpo
->mpo_level
;
1343 smpo
.mpo_name
= mpo
->mpo_name
;
1345 /* grab exisiting values in case we need to rollback */
1346 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1349 mpts
->mpts_flags
&= ~(MPTSF_SOPT_OLDVAL
| MPTSF_SOPT_INPROG
);
1350 mpts
->mpts_oldintval
= 0;
1351 smpo
.mpo_intval
= 0;
1352 VERIFY(mpts
->mpts_socket
!= NULL
);
1353 so
= mpts
->mpts_socket
;
1354 if (mptcp_subflow_sogetopt(mpte
, so
, &smpo
) == 0) {
1355 mpts
->mpts_flags
|= MPTSF_SOPT_OLDVAL
;
1356 mpts
->mpts_oldintval
= smpo
.mpo_intval
;
1360 /* apply socket option */
1361 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1364 mpts
->mpts_flags
|= MPTSF_SOPT_INPROG
;
1365 VERIFY(mpts
->mpts_socket
!= NULL
);
1366 so
= mpts
->mpts_socket
;
1367 error
= mptcp_subflow_sosetopt(mpte
, mpts
, mpo
);
1373 /* cleanup, and rollback if needed */
1374 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1377 if (!(mpts
->mpts_flags
& MPTSF_SOPT_INPROG
)) {
1378 /* clear in case it's set */
1379 mpts
->mpts_flags
&= ~MPTSF_SOPT_OLDVAL
;
1380 mpts
->mpts_oldintval
= 0;
1383 if (!(mpts
->mpts_flags
& MPTSF_SOPT_OLDVAL
)) {
1384 mpts
->mpts_flags
&= ~MPTSF_SOPT_INPROG
;
1385 VERIFY(mpts
->mpts_oldintval
== 0);
1388 /* error during sosetopt, so roll it back */
1390 VERIFY(mpts
->mpts_socket
!= NULL
);
1391 so
= mpts
->mpts_socket
;
1392 smpo
.mpo_intval
= mpts
->mpts_oldintval
;
1393 mptcp_subflow_sosetopt(mpte
, mpts
, &smpo
);
1395 mpts
->mpts_oldintval
= 0;
1396 mpts
->mpts_flags
&= ~(MPTSF_SOPT_OLDVAL
| MPTSF_SOPT_INPROG
);
1404 * Handle SOPT_SET for socket options issued on MP socket.
1407 mptcp_setopt(struct mptses
*mpte
, struct sockopt
*sopt
)
1409 int error
= 0, optval
= 0, level
, optname
, rec
= 1;
1410 struct mptopt smpo
, *mpo
= NULL
;
1411 struct socket
*mp_so
;
1413 level
= sopt
->sopt_level
;
1414 optname
= sopt
->sopt_name
;
1416 mp_so
= mptetoso(mpte
);
1419 * Record socket options which are applicable to subflow sockets so
1420 * that we can replay them for new ones; see mptcp_usr_socheckopt()
1421 * for the list of eligible socket-level options.
1423 if (level
== SOL_SOCKET
) {
1427 case SO_USELOOPBACK
:
1429 case SO_TRAFFIC_CLASS
:
1430 case SO_RECV_TRAFFIC_CLASS
:
1431 case SO_PRIVILEGED_TRAFFIC_CLASS
:
1433 case SO_RESTRICTIONS
:
1434 case SO_NOWAKEFROMSLEEP
:
1435 case SO_NOAPNFALLBK
:
1436 case SO_MARK_CELLFALLBACK
:
1440 /* don't record it */
1444 /* Next ones, record at MPTCP-level */
1446 case SO_NECP_CLIENTUUID
:
1447 if (!uuid_is_null(mpsotomppcb(mp_so
)->necp_client_uuid
)) {
1452 error
= sooptcopyin(sopt
, &mpsotomppcb(mp_so
)->necp_client_uuid
,
1453 sizeof(uuid_t
), sizeof(uuid_t
));
1458 mpsotomppcb(mp_so
)->necp_cb
= mptcp_session_necp_cb
;
1459 error
= necp_client_register_multipath_cb(mp_so
->last_pid
,
1460 mpsotomppcb(mp_so
)->necp_client_uuid
,
1461 mpsotomppcb(mp_so
));
1466 if (uuid_is_null(mpsotomppcb(mp_so
)->necp_client_uuid
)) {
1472 case SO_NECP_ATTRIBUTES
:
1475 /* nothing to do; just return */
1481 case TCP_RXT_FINDROP
:
1485 case TCP_CONNECTIONTIMEOUT
:
1486 case TCP_RXT_CONNDROPTIME
:
1487 case PERSIST_TIMEOUT
:
1488 case TCP_ADAPTIVE_READ_TIMEOUT
:
1489 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1490 /* eligible; record it */
1492 case TCP_NOTSENT_LOWAT
:
1493 /* record at MPTCP level */
1494 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1504 mp_so
->so_flags
&= ~SOF_NOTSENT_LOWAT
;
1505 error
= mptcp_set_notsent_lowat(mpte
, 0);
1507 mp_so
->so_flags
|= SOF_NOTSENT_LOWAT
;
1508 error
= mptcp_set_notsent_lowat(mpte
,
1513 case MPTCP_SERVICE_TYPE
:
1514 /* record at MPTCP level */
1515 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1520 if (optval
< 0 || optval
>= MPTCP_SVCTYPE_MAX
) {
1525 mpte
->mpte_svctype
= optval
;
1527 if (mptcp_entitlement_check(mp_so
) < 0) {
1532 mpte
->mpte_flags
|= MPTE_SVCTYPE_CHECKED
;
1535 case MPTCP_ALTERNATE_PORT
:
1536 /* record at MPTCP level */
1537 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1543 if (optval
< 0 || optval
> UINT16_MAX
) {
1548 mpte
->mpte_alternate_port
= optval
;
1553 error
= ENOPROTOOPT
;
1558 if ((error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1559 sizeof(optval
))) != 0) {
1564 /* search for an existing one; if not found, allocate */
1565 if ((mpo
= mptcp_sopt_find(mpte
, sopt
)) == NULL
) {
1566 mpo
= mptcp_sopt_alloc(M_WAITOK
);
1572 mptcplog((LOG_DEBUG
, "%s: mp_so 0x%llx sopt %s val %d %s\n",
1573 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1574 mptcp_sopt2str(level
, optname
), optval
,
1575 (mpo
->mpo_flags
& MPOF_ATTACHED
) ?
1576 "updated" : "recorded"),
1577 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1579 /* initialize or update, as needed */
1580 mpo
->mpo_intval
= optval
;
1581 if (!(mpo
->mpo_flags
& MPOF_ATTACHED
)) {
1582 mpo
->mpo_level
= level
;
1583 mpo
->mpo_name
= optname
;
1584 mptcp_sopt_insert(mpte
, mpo
);
1586 /* this can be issued on the subflow socket */
1587 mpo
->mpo_flags
|= MPOF_SUBFLOW_OK
;
1590 bzero(&smpo
, sizeof(smpo
));
1592 mpo
->mpo_flags
|= MPOF_SUBFLOW_OK
;
1593 mpo
->mpo_level
= level
;
1594 mpo
->mpo_name
= optname
;
1595 mpo
->mpo_intval
= optval
;
1598 /* issue this socket option on existing subflows */
1600 error
= mptcp_setopt_apply(mpte
, mpo
);
1601 if (error
!= 0 && (mpo
->mpo_flags
& MPOF_ATTACHED
)) {
1602 VERIFY(mpo
!= &smpo
);
1603 mptcp_sopt_remove(mpte
, mpo
);
1604 mptcp_sopt_free(mpo
);
1607 mpo
->mpo_flags
&= ~MPOF_INTERIM
;
1611 if (error
== 0 && mpo
!= NULL
) {
1612 mptcplog((LOG_INFO
, "%s: mp_so 0x%llx sopt %s val %d set %s\n",
1613 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1614 mptcp_sopt2str(level
, optname
), optval
,
1615 (mpo
->mpo_flags
& MPOF_INTERIM
) ?
1616 "pending" : "successful"),
1617 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1618 } else if (error
!= 0) {
1619 mptcplog((LOG_ERR
, "%s: mp_so 0x%llx sopt %s (%d, %d) val %d can't be issued error %d\n",
1620 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1621 mptcp_sopt2str(level
, optname
), level
, optname
, optval
, error
),
1622 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
1628 * Handle SOPT_GET for socket options issued on MP socket.
1631 mptcp_getopt(struct mptses
*mpte
, struct sockopt
*sopt
)
1633 int error
= 0, optval
= 0;
1636 * We only handle SOPT_GET for TCP level socket options; we should
1637 * not get here for socket level options since they are already
1638 * handled at the socket layer.
1640 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
1641 error
= ENOPROTOOPT
;
1645 switch (sopt
->sopt_name
) {
1647 case TCP_RXT_FINDROP
:
1651 case TCP_CONNECTIONTIMEOUT
:
1652 case TCP_RXT_CONNDROPTIME
:
1653 case PERSIST_TIMEOUT
:
1654 case TCP_ADAPTIVE_READ_TIMEOUT
:
1655 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1656 case TCP_NOTSENT_LOWAT
:
1657 case MPTCP_SERVICE_TYPE
:
1658 case MPTCP_ALTERNATE_PORT
:
1659 /* eligible; get the default value just in case */
1660 error
= mptcp_default_tcp_optval(mpte
, sopt
, &optval
);
1664 error
= ENOPROTOOPT
;
1668 switch (sopt
->sopt_name
) {
1669 case TCP_NOTSENT_LOWAT
:
1670 if (mptetoso(mpte
)->so_flags
& SOF_NOTSENT_LOWAT
) {
1671 optval
= mptcp_get_notsent_lowat(mpte
);
1676 case MPTCP_SERVICE_TYPE
:
1677 optval
= mpte
->mpte_svctype
;
1679 case MPTCP_ALTERNATE_PORT
:
1680 optval
= mpte
->mpte_alternate_port
;
1685 * Search for a previously-issued TCP level socket option and
1686 * return the recorded option value. This assumes that the
1687 * value did not get modified by the lower layer after it was
1688 * issued at setsockopt(2) time. If not found, we'll return
1689 * the default value obtained ealier.
1694 if ((mpo
= mptcp_sopt_find(mpte
, sopt
)) != NULL
) {
1695 optval
= mpo
->mpo_intval
;
1698 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1705 * Return default values for TCP socket options. Ideally we would query the
1706 * subflow TCP socket, but that requires creating a subflow socket before
1707 * connectx(2) time. To simplify things, just return the default values
1711 mptcp_default_tcp_optval(struct mptses
*mpte
, struct sockopt
*sopt
, int *optval
)
1715 VERIFY(sopt
->sopt_level
== IPPROTO_TCP
);
1716 VERIFY(sopt
->sopt_dir
== SOPT_GET
);
1717 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1719 /* try to do what tcp_newtcpcb() does */
1720 switch (sopt
->sopt_name
) {
1722 case TCP_RXT_FINDROP
:
1725 case TCP_CONNECTIONTIMEOUT
:
1726 case TCP_RXT_CONNDROPTIME
:
1727 case TCP_NOTSENT_LOWAT
:
1728 case TCP_ADAPTIVE_READ_TIMEOUT
:
1729 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1730 case MPTCP_SERVICE_TYPE
:
1731 case MPTCP_ALTERNATE_PORT
:
1736 *optval
= mptcp_subflow_keeptime
;
1739 case PERSIST_TIMEOUT
:
1740 *optval
= tcp_max_persist_timeout
;
1744 error
= ENOPROTOOPT
;
1751 * MPTCP SOPT_{SET,GET} socket option handler, for options issued on the MP
1752 * socket, at SOL_SOCKET and IPPROTO_TCP levels. The former is restricted
1753 * to those that are allowed by mptcp_usr_socheckopt().
1756 mptcp_ctloutput(struct socket
*mp_so
, struct sockopt
*sopt
)
1758 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
1759 struct mptses
*mpte
;
1762 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
1766 mpte
= mptompte(mpp
);
1767 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1769 /* we only handle socket and TCP-level socket options for MPTCP */
1770 if (sopt
->sopt_level
!= SOL_SOCKET
&& sopt
->sopt_level
!= IPPROTO_TCP
) {
1771 mptcplog((LOG_DEBUG
, "MPTCP Socket: "
1772 "%s: mp_so 0x%llx sopt %s level not "
1773 "handled\n", __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1774 mptcp_sopt2str(sopt
->sopt_level
, sopt
->sopt_name
)),
1775 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1780 switch (sopt
->sopt_dir
) {
1782 error
= mptcp_setopt(mpte
, sopt
);
1786 error
= mptcp_getopt(mpte
, sopt
);
1794 mptcp_sopt2str(int level
, int optname
)
1802 return "SO_LINGER_SEC";
1806 return "SO_KEEPALIVE";
1807 case SO_USELOOPBACK
:
1808 return "SO_USELOOPBACK";
1822 return "SO_SNDLOWAT";
1824 return "SO_RCVLOWAT";
1826 return "SO_SNDTIMEO";
1828 return "SO_RCVTIMEO";
1832 return "SO_NOSIGPIPE";
1834 return "SO_NOADDRERR";
1835 case SO_RESTRICTIONS
:
1836 return "SO_RESTRICTIONS";
1840 return "SO_PEERLABEL";
1842 return "SO_RANDOMPORT";
1843 case SO_TRAFFIC_CLASS
:
1844 return "SO_TRAFFIC_CLASS";
1845 case SO_RECV_TRAFFIC_CLASS
:
1846 return "SO_RECV_TRAFFIC_CLASS";
1847 case SO_TRAFFIC_CLASS_DBG
:
1848 return "SO_TRAFFIC_CLASS_DBG";
1849 case SO_PRIVILEGED_TRAFFIC_CLASS
:
1850 return "SO_PRIVILEGED_TRAFFIC_CLASS";
1852 return "SO_DEFUNCTOK";
1854 return "SO_ISDEFUNCT";
1855 case SO_OPPORTUNISTIC
:
1856 return "SO_OPPORTUNISTIC";
1860 return "SO_RECV_ANYIF";
1861 case SO_NOWAKEFROMSLEEP
:
1862 return "SO_NOWAKEFROMSLEEP";
1863 case SO_NOAPNFALLBK
:
1864 return "SO_NOAPNFALLBK";
1865 case SO_MARK_CELLFALLBACK
:
1866 return "SO_CELLFALLBACK";
1868 return "SO_DELEGATED";
1869 case SO_DELEGATED_UUID
:
1870 return "SO_DELEGATED_UUID";
1872 case SO_NECP_ATTRIBUTES
:
1873 return "SO_NECP_ATTRIBUTES";
1874 case SO_NECP_CLIENTUUID
:
1875 return "SO_NECP_CLIENTUUID";
1883 return "TCP_NODELAY";
1885 return "TCP_KEEPALIVE";
1887 return "TCP_KEEPINTVL";
1889 return "TCP_KEEPCNT";
1890 case TCP_CONNECTIONTIMEOUT
:
1891 return "TCP_CONNECTIONTIMEOUT";
1892 case TCP_RXT_CONNDROPTIME
:
1893 return "TCP_RXT_CONNDROPTIME";
1894 case PERSIST_TIMEOUT
:
1895 return "PERSIST_TIMEOUT";
1896 case TCP_NOTSENT_LOWAT
:
1897 return "NOTSENT_LOWAT";
1898 case TCP_ADAPTIVE_READ_TIMEOUT
:
1899 return "ADAPTIVE_READ_TIMEOUT";
1900 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1901 return "ADAPTIVE_WRITE_TIMEOUT";
1902 case MPTCP_SERVICE_TYPE
:
1903 return "MPTCP_SERVICE_TYPE";
1904 case MPTCP_ALTERNATE_PORT
:
1905 return "MPTCP_ALTERNATE_PORT";
1915 mptcp_usr_preconnect(struct socket
*mp_so
)
1917 struct mptsub
*mpts
= NULL
;
1918 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
1919 struct mptses
*mpte
;
1921 struct tcpcb
*tp
= NULL
;
1924 mpte
= mptompte(mpp
);
1925 VERIFY(mpte
!= NULL
);
1926 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1928 mpts
= mptcp_get_subflow(mpte
, NULL
, NULL
);
1930 mptcplog((LOG_ERR
, "%s: mp_so 0x%llx invalid preconnect ",
1931 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
)),
1932 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
1935 mpts
->mpts_flags
&= ~MPTSF_TFO_REQD
;
1936 so
= mpts
->mpts_socket
;
1937 tp
= intotcpcb(sotoinpcb(so
));
1938 tp
->t_mpflags
&= ~TMPF_TFO_REQUEST
;
1939 error
= tcp_output(sototcpcb(so
));
1941 soclearfastopen(mp_so
);