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
);
131 * Might want to use a different SO_LINGER timeout than TCP's?
133 if ((mp_so
->so_options
& SO_LINGER
) && mp_so
->so_linger
== 0)
134 mp_so
->so_linger
= TCP_LINGERTIME
* hz
;
140 * Detaches an MPTCP control block from a socket.
143 mptcp_usr_detach(struct socket
*mp_so
)
145 struct mptses
*mpte
= mpsotompte(mp_so
);
146 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
148 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
149 mptcplog((LOG_ERR
, "%s state: %d\n", __func__
,
150 mpp
? mpp
->mpp_state
: -1),
151 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
156 * We are done with this MPTCP socket (it has been closed);
157 * trigger all subflows to be disconnected, if not already,
158 * by initiating the PCB detach sequence (SOF_PCBCLEARING
163 mptcp_disconnect(mpte
);
169 * Attach MPTCP protocol to socket, allocating MP control block,
170 * MPTCP session, control block, buffer space, etc.
173 mptcp_attach(struct socket
*mp_so
, struct proc
*p
)
176 struct mptses
*mpte
= NULL
;
177 struct mptcb
*mp_tp
= NULL
;
178 struct mppcb
*mpp
= NULL
;
181 if (mp_so
->so_snd
.sb_hiwat
== 0 || mp_so
->so_rcv
.sb_hiwat
== 0) {
182 error
= soreserve(mp_so
, tcp_sendspace
, tcp_recvspace
);
187 if (mp_so
->so_snd
.sb_preconn_hiwat
== 0) {
188 soreserve_preconnect(mp_so
, 2048);
191 if ((mp_so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0)
192 mp_so
->so_rcv
.sb_flags
|= SB_AUTOSIZE
;
193 if ((mp_so
->so_snd
.sb_flags
& SB_USRSIZE
) == 0)
194 mp_so
->so_snd
.sb_flags
|= SB_AUTOSIZE
;
197 * MPTCP socket buffers cannot be compressed, due to the
198 * fact that each mbuf chained via m_next is a M_PKTHDR
199 * which carries some MPTCP metadata.
201 mp_so
->so_snd
.sb_flags
|= SB_NOCOMPRESS
;
202 mp_so
->so_rcv
.sb_flags
|= SB_NOCOMPRESS
;
204 if ((error
= mp_pcballoc(mp_so
, &mtcbinfo
)) != 0) {
208 mpp
= mpsotomppcb(mp_so
);
210 mpte
= (struct mptses
*)mpp
->mpp_pcbe
;
211 VERIFY(mpte
!= NULL
);
212 mp_tp
= mpte
->mpte_mptcb
;
213 VERIFY(mp_tp
!= NULL
);
219 mptcp_entitlement_check(struct socket
*mp_so
)
221 struct mptses
*mpte
= mpsotompte(mp_so
);
223 if (soopt_cred_check(mp_so
, PRIV_NET_RESTRICTED_MULTIPATH_EXTENDED
, TRUE
) == 0) {
225 * This means the app has the extended entitlement. Thus,
226 * it's a first party app and can run without restrictions.
228 mpte
->mpte_flags
|= MPTE_FIRSTPARTY
;
232 #if (DEVELOPMENT || DEBUG)
233 if (mptcp_disable_entitlements
)
237 if (soopt_cred_check(mp_so
, PRIV_NET_PRIVILEGED_MULTIPATH
, TRUE
)) {
238 mptcplog((LOG_NOTICE
, "%s Multipath Capability needed\n", __func__
),
239 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
243 if (mpte
->mpte_svctype
> MPTCP_SVCTYPE_INTERACTIVE
&&
244 mptcp_developer_mode
== 0) {
245 mptcplog((LOG_NOTICE
, "%s need to set allow_aggregate sysctl\n",
246 __func__
), MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
251 mptcplog((LOG_NOTICE
, "%s entitlement granted for %u\n", __func__
, mpte
->mpte_svctype
),
252 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
258 * Common subroutine to open a MPTCP connection to one of the remote hosts
259 * specified by dst_sl. This includes allocating and establishing a
260 * subflow TCP connection, either initially to establish MPTCP connection,
261 * or to join an existing one. Returns a connection handle upon success.
264 mptcp_connectx(struct mptses
*mpte
, struct sockaddr
*src
,
265 struct sockaddr
*dst
, uint32_t ifscope
, sae_connid_t
*pcid
)
267 struct socket
*mp_so
= mptetoso(mpte
);
271 VERIFY(pcid
!= NULL
);
273 mptcplog((LOG_DEBUG
, "%s: mp_so 0x%llx\n", __func__
,
274 (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
)),
275 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
276 DTRACE_MPTCP2(connectx
, struct mptses
*, mpte
, struct socket
*, mp_so
);
278 error
= mptcp_subflow_add(mpte
, src
, dst
, ifscope
, pcid
);
284 * User-protocol pru_connectx callback.
287 mptcp_usr_connectx(struct socket
*mp_so
, struct sockaddr
*src
,
288 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope
,
289 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
290 uint32_t arglen
, struct uio
*auio
, user_ssize_t
*bytes_written
)
292 #pragma unused(p, aid, flags, arg, arglen)
293 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
294 struct mptses
*mpte
= NULL
;
295 struct mptcb
*mp_tp
= NULL
;
296 user_ssize_t datalen
;
299 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
300 mptcplog((LOG_ERR
, "%s state %d\n", __func__
,
301 mpp
? mpp
->mpp_state
: -1),
302 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
306 mpte
= mptompte(mpp
);
307 VERIFY(mpte
!= NULL
);
308 mpte_lock_assert_held(mpte
);
310 mp_tp
= mpte
->mpte_mptcb
;
311 VERIFY(mp_tp
!= NULL
);
313 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
) {
314 mptcplog((LOG_ERR
, "%s fell back to TCP\n", __func__
),
315 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
320 if (dst
->sa_family
!= AF_INET
&& dst
->sa_family
!= AF_INET6
) {
321 error
= EAFNOSUPPORT
;
325 if (dst
->sa_family
== AF_INET
&&
326 dst
->sa_len
!= sizeof(mpte
->__mpte_dst_v4
)) {
327 mptcplog((LOG_ERR
, "%s IPv4 dst len %u\n", __func__
,
329 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
334 if (dst
->sa_family
== AF_INET6
&&
335 dst
->sa_len
!= sizeof(mpte
->__mpte_dst_v6
)) {
336 mptcplog((LOG_ERR
, "%s IPv6 dst len %u\n", __func__
,
338 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
343 if (!(mpte
->mpte_flags
& MPTE_SVCTYPE_CHECKED
)) {
344 if (mptcp_entitlement_check(mp_so
) < 0) {
349 mpte
->mpte_flags
|= MPTE_SVCTYPE_CHECKED
;
352 if ((mp_so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0) {
353 memcpy(&mpte
->mpte_dst
, dst
, dst
->sa_len
);
357 if (src
->sa_family
!= AF_INET
&& src
->sa_family
!= AF_INET6
) {
358 error
= EAFNOSUPPORT
;
362 if (src
->sa_family
== AF_INET
&&
363 src
->sa_len
!= sizeof(mpte
->__mpte_src_v4
)) {
364 mptcplog((LOG_ERR
, "%s IPv4 src len %u\n", __func__
,
366 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
371 if (src
->sa_family
== AF_INET6
&&
372 src
->sa_len
!= sizeof(mpte
->__mpte_src_v6
)) {
373 mptcplog((LOG_ERR
, "%s IPv6 src len %u\n", __func__
,
375 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
380 if ((mp_so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0) {
381 memcpy(&mpte
->mpte_src
, src
, src
->sa_len
);
385 error
= mptcp_connectx(mpte
, src
, dst
, ifscope
, pcid
);
387 /* If there is data, copy it */
389 datalen
= uio_resid(auio
);
390 socket_unlock(mp_so
, 0);
391 error
= mp_so
->so_proto
->pr_usrreqs
->pru_sosend(mp_so
, NULL
,
392 (uio_t
) auio
, NULL
, NULL
, 0);
394 if (error
== 0 || error
== EWOULDBLOCK
)
395 *bytes_written
= datalen
- uio_resid(auio
);
397 if (error
== EWOULDBLOCK
)
400 socket_lock(mp_so
, 0);
408 * Handle SIOCGASSOCIDS ioctl for PF_MULTIPATH domain.
411 mptcp_getassocids(struct mptses
*mpte
, uint32_t *cnt
, user_addr_t aidp
)
413 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
415 /* MPTCP has at most 1 association */
416 *cnt
= (mpte
->mpte_associd
!= SAE_ASSOCID_ANY
) ? 1 : 0;
418 /* just asking how many there are? */
419 if (aidp
== USER_ADDR_NULL
)
422 return (copyout(&mpte
->mpte_associd
, aidp
,
423 sizeof (mpte
->mpte_associd
)));
427 * Handle SIOCGCONNIDS ioctl for PF_MULTIPATH domain.
430 mptcp_getconnids(struct mptses
*mpte
, sae_associd_t aid
, uint32_t *cnt
,
436 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
438 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
&&
439 aid
!= mpte
->mpte_associd
)
442 *cnt
= mpte
->mpte_numflows
;
444 /* just asking how many there are? */
445 if (cidp
== USER_ADDR_NULL
)
448 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
449 if ((error
= copyout(&mpts
->mpts_connid
, cidp
,
450 sizeof (mpts
->mpts_connid
))) != 0)
453 cidp
+= sizeof (mpts
->mpts_connid
);
460 * Handle SIOCGCONNINFO ioctl for PF_MULTIPATH domain.
463 mptcp_getconninfo(struct mptses
*mpte
, sae_connid_t
*cid
, uint32_t *flags
,
464 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
465 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
466 user_addr_t aux_data
, uint32_t *aux_len
)
478 if (*cid
== SAE_CONNID_ALL
) {
479 struct socket
*mp_so
= mptetoso(mpte
);
480 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
481 struct conninfo_multipathtcp mptcp_ci
;
483 if (*aux_len
!= 0 && *aux_len
!= sizeof(mptcp_ci
))
486 if (mp_so
->so_state
& SS_ISCONNECTING
)
487 *flags
|= CIF_CONNECTING
;
488 if (mp_so
->so_state
& SS_ISCONNECTED
)
489 *flags
|= CIF_CONNECTED
;
490 if (mp_so
->so_state
& SS_ISDISCONNECTING
)
491 *flags
|= CIF_DISCONNECTING
;
492 if (mp_so
->so_state
& SS_ISDISCONNECTED
)
493 *flags
|= CIF_DISCONNECTED
;
494 if (!(mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
))
495 *flags
|= CIF_MP_CAPABLE
;
496 if (mp_tp
->mpt_flags
& MPTCPF_FALLBACK_TO_TCP
)
497 *flags
|= CIF_MP_DEGRADED
;
502 *aux_type
= CIAUX_MPTCP
;
503 *aux_len
= sizeof(mptcp_ci
);
505 if (aux_data
!= USER_ADDR_NULL
) {
507 int initial_info_set
= 0;
509 bzero(&mptcp_ci
, sizeof (mptcp_ci
));
510 mptcp_ci
.mptcpci_subflow_count
= mpte
->mpte_numflows
;
511 mptcp_ci
.mptcpci_switch_count
= mpte
->mpte_subflow_switches
;
513 VERIFY(sizeof(mptcp_ci
.mptcpci_itfstats
) == sizeof(mpte
->mpte_itfstats
));
514 memcpy(mptcp_ci
.mptcpci_itfstats
, mpte
->mpte_itfstats
, sizeof(mptcp_ci
.mptcpci_itfstats
));
516 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
517 if (i
>= sizeof(mptcp_ci
.mptcpci_subflow_connids
) / sizeof(sae_connid_t
))
519 mptcp_ci
.mptcpci_subflow_connids
[i
] = mpts
->mpts_connid
;
521 if (mpts
->mpts_flags
& MPTSF_INITIAL_SUB
) {
522 inp
= sotoinpcb(mpts
->mpts_socket
);
524 mptcp_ci
.mptcpci_init_rxbytes
= inp
->inp_stat
->rxbytes
;
525 mptcp_ci
.mptcpci_init_txbytes
= inp
->inp_stat
->txbytes
;
526 initial_info_set
= 1;
529 mptcpstats_update(mptcp_ci
.mptcpci_itfstats
, mpts
);
534 if (initial_info_set
== 0) {
535 mptcp_ci
.mptcpci_init_rxbytes
= mpte
->mpte_init_rxbytes
;
536 mptcp_ci
.mptcpci_init_txbytes
= mpte
->mpte_init_txbytes
;
539 if (mpte
->mpte_flags
& MPTE_FIRSTPARTY
)
540 mptcp_ci
.mptcpci_flags
|= MPTCPCI_FIRSTPARTY
;
542 error
= copyout(&mptcp_ci
, aux_data
, sizeof(mptcp_ci
));
544 mptcplog((LOG_ERR
, "%s copyout failed: %d\n",
546 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
554 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
555 if (mpts
->mpts_connid
== *cid
|| *cid
== SAE_CONNID_ANY
)
559 return ((*cid
== SAE_CONNID_ANY
) ? ENXIO
: EINVAL
);
561 so
= mpts
->mpts_socket
;
564 if (inp
->inp_vflag
& INP_IPV4
)
565 error
= in_getconninfo(so
, SAE_CONNID_ANY
, flags
, ifindex
,
566 soerror
, src
, src_len
, dst
, dst_len
,
567 aux_type
, aux_data
, aux_len
);
569 error
= in6_getconninfo(so
, SAE_CONNID_ANY
, flags
, ifindex
,
570 soerror
, src
, src_len
, dst
, dst_len
,
571 aux_type
, aux_data
, aux_len
);
574 mptcplog((LOG_ERR
, "%s error from in_getconninfo %d\n",
576 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
580 if (mpts
->mpts_flags
& MPTSF_MP_CAPABLE
)
581 *flags
|= CIF_MP_CAPABLE
;
582 if (mpts
->mpts_flags
& MPTSF_MP_DEGRADED
)
583 *flags
|= CIF_MP_DEGRADED
;
584 if (mpts
->mpts_flags
& MPTSF_MP_READY
)
585 *flags
|= CIF_MP_READY
;
586 if (mpts
->mpts_flags
& MPTSF_ACTIVE
)
587 *flags
|= CIF_MP_ACTIVE
;
589 mptcplog((LOG_DEBUG
, "%s: cid %d flags %x \n", __func__
,
590 mpts
->mpts_connid
, mpts
->mpts_flags
),
591 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_VERBOSE
);
597 * User-protocol pru_control callback.
600 mptcp_usr_control(struct socket
*mp_so
, u_long cmd
, caddr_t data
,
601 struct ifnet
*ifp
, struct proc
*p
)
603 #pragma unused(ifp, p)
604 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
608 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
612 mpte
= mptompte(mpp
);
613 VERIFY(mpte
!= NULL
);
615 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
618 case SIOCGASSOCIDS32
: { /* struct so_aidreq32 */
619 struct so_aidreq32 aidr
;
620 bcopy(data
, &aidr
, sizeof (aidr
));
621 error
= mptcp_getassocids(mpte
, &aidr
.sar_cnt
,
624 bcopy(&aidr
, data
, sizeof (aidr
));
628 case SIOCGASSOCIDS64
: { /* struct so_aidreq64 */
629 struct so_aidreq64 aidr
;
630 bcopy(data
, &aidr
, sizeof (aidr
));
631 error
= mptcp_getassocids(mpte
, &aidr
.sar_cnt
,
634 bcopy(&aidr
, data
, sizeof (aidr
));
638 case SIOCGCONNIDS32
: { /* struct so_cidreq32 */
639 struct so_cidreq32 cidr
;
640 bcopy(data
, &cidr
, sizeof (cidr
));
641 error
= mptcp_getconnids(mpte
, cidr
.scr_aid
, &cidr
.scr_cnt
,
644 bcopy(&cidr
, data
, sizeof (cidr
));
648 case SIOCGCONNIDS64
: { /* struct so_cidreq64 */
649 struct so_cidreq64 cidr
;
650 bcopy(data
, &cidr
, sizeof (cidr
));
651 error
= mptcp_getconnids(mpte
, cidr
.scr_aid
, &cidr
.scr_cnt
,
654 bcopy(&cidr
, data
, sizeof (cidr
));
658 case SIOCGCONNINFO32
: { /* struct so_cinforeq32 */
659 struct so_cinforeq32 cifr
;
660 bcopy(data
, &cifr
, sizeof (cifr
));
661 error
= mptcp_getconninfo(mpte
, &cifr
.scir_cid
,
662 &cifr
.scir_flags
, &cifr
.scir_ifindex
, &cifr
.scir_error
,
663 cifr
.scir_src
, &cifr
.scir_src_len
, cifr
.scir_dst
,
664 &cifr
.scir_dst_len
, &cifr
.scir_aux_type
, cifr
.scir_aux_data
,
667 bcopy(&cifr
, data
, sizeof (cifr
));
671 case SIOCGCONNINFO64
: { /* struct so_cinforeq64 */
672 struct so_cinforeq64 cifr
;
673 bcopy(data
, &cifr
, sizeof (cifr
));
674 error
= mptcp_getconninfo(mpte
, &cifr
.scir_cid
,
675 &cifr
.scir_flags
, &cifr
.scir_ifindex
, &cifr
.scir_error
,
676 cifr
.scir_src
, &cifr
.scir_src_len
, cifr
.scir_dst
,
677 &cifr
.scir_dst_len
, &cifr
.scir_aux_type
, cifr
.scir_aux_data
,
680 bcopy(&cifr
, data
, sizeof (cifr
));
693 mptcp_disconnect(struct mptses
*mpte
)
695 struct socket
*mp_so
;
699 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
701 mp_so
= mptetoso(mpte
);
702 mp_tp
= mpte
->mpte_mptcb
;
704 mptcplog((LOG_DEBUG
, "%s: mp_so 0x%llx %d\n", __func__
,
705 (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
), mp_so
->so_error
),
706 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
708 DTRACE_MPTCP3(disconnectx
, struct mptses
*, mpte
,
709 struct socket
*, mp_so
, struct mptcb
*, mp_tp
);
711 /* if we're not detached, go thru socket state checks */
712 if (!(mp_so
->so_flags
& SOF_PCBCLEARING
)) {
713 if (!(mp_so
->so_state
& (SS_ISCONNECTED
|
718 if (mp_so
->so_state
& SS_ISDISCONNECTING
) {
724 mptcp_cancel_all_timers(mp_tp
);
725 if (mp_tp
->mpt_state
< MPTCPS_ESTABLISHED
) {
726 mptcp_close(mpte
, mp_tp
);
727 } else if ((mp_so
->so_options
& SO_LINGER
) &&
728 mp_so
->so_linger
== 0) {
729 mptcp_drop(mpte
, mp_tp
, 0);
731 soisdisconnecting(mp_so
);
732 sbflush(&mp_so
->so_rcv
);
733 if (mptcp_usrclosed(mpte
) != NULL
)
738 mptcp_subflow_workloop(mpte
);
745 * Wrapper function to support disconnect on socket
748 mptcp_usr_disconnect(struct socket
*mp_so
)
750 return (mptcp_disconnect(mpsotompte(mp_so
)));
754 * User-protocol pru_disconnectx callback.
757 mptcp_usr_disconnectx(struct socket
*mp_so
, sae_associd_t aid
, sae_connid_t cid
)
759 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
762 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
)
765 return (mptcp_usr_disconnect(mp_so
));
769 mptcp_finish_usrclosed(struct mptses
*mpte
)
771 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
772 struct socket
*mp_so
= mptetoso(mpte
);
774 if (mp_tp
->mpt_state
== MPTCPS_CLOSED
) {
775 mpte
= mptcp_close(mpte
, mp_tp
);
776 } else if (mp_tp
->mpt_state
>= MPTCPS_FIN_WAIT_2
) {
777 soisdisconnected(mp_so
);
781 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
782 if ((mp_so
->so_state
& (SS_CANTRCVMORE
|SS_CANTSENDMORE
)) ==
783 (SS_CANTRCVMORE
| SS_CANTSENDMORE
))
784 mptcp_subflow_disconnect(mpte
, mpts
);
786 mptcp_subflow_shutdown(mpte
, mpts
);
792 * User issued close, and wish to trail thru shutdown states.
794 static struct mptses
*
795 mptcp_usrclosed(struct mptses
*mpte
)
797 struct mptcb
*mp_tp
= mpte
->mpte_mptcb
;
799 mptcp_close_fsm(mp_tp
, MPCE_CLOSE
);
801 /* Not everything has been acknowledged - don't close the subflows! */
802 if (mp_tp
->mpt_sndnxt
+ 1 != mp_tp
->mpt_sndmax
)
805 mptcp_finish_usrclosed(mpte
);
811 * After a receive, possible send some update to peer.
814 mptcp_usr_rcvd(struct socket
*mp_so
, int flags
)
816 #pragma unused(flags)
817 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
821 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
825 mpte
= mptompte(mpp
);
826 VERIFY(mpte
!= NULL
);
828 error
= mptcp_output(mpte
);
834 * Do a send by putting data in the output queue.
837 mptcp_usr_send(struct socket
*mp_so
, int prus_flags
, struct mbuf
*m
,
838 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
840 #pragma unused(nam, p)
841 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
845 if (prus_flags
& (PRUS_OOB
|PRUS_EOF
)) {
855 if (control
!= NULL
&& control
->m_len
!= 0) {
860 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
864 mpte
= mptompte(mpp
);
865 VERIFY(mpte
!= NULL
);
867 if (!(mp_so
->so_state
& SS_ISCONNECTED
) &&
868 !(mp_so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
873 mptcp_insert_dsn(mpp
, m
);
874 VERIFY(mp_so
->so_snd
.sb_flags
& SB_NOCOMPRESS
);
875 sbappendstream(&mp_so
->so_snd
, m
);
878 error
= mptcp_output(mpte
);
882 if (mp_so
->so_state
& SS_ISCONNECTING
) {
883 if (mp_so
->so_state
& SS_NBIO
)
886 error
= sbwait(&mp_so
->so_snd
);
900 * Mark the MPTCP connection as being incapable of further output.
903 mptcp_usr_shutdown(struct socket
*mp_so
)
905 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
909 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
913 mpte
= mptompte(mpp
);
914 VERIFY(mpte
!= NULL
);
916 socantsendmore(mp_so
);
918 mpte
= mptcp_usrclosed(mpte
);
920 error
= mptcp_output(mpte
);
926 * Copy the contents of uio into a properly sized mbuf chain.
929 mptcp_uiotombuf(struct uio
*uio
, int how
, int space
, uint32_t align
,
932 struct mbuf
*m
, *mb
, *nm
= NULL
, *mtail
= NULL
;
933 user_ssize_t resid
, tot
, len
, progress
; /* must be user_ssize_t */
936 VERIFY(top
!= NULL
&& *top
== NULL
);
939 * space can be zero or an arbitrary large value bound by
940 * the total data supplied by the uio.
942 resid
= uio_resid(uio
);
944 tot
= imin(resid
, space
);
949 * The smallest unit is a single mbuf with pkthdr.
950 * We can't align past it.
956 * Give us the full allocation or nothing.
957 * If space is zero return the smallest empty mbuf.
959 if ((len
= tot
+ align
) == 0)
962 /* Loop and append maximum sized mbufs to the chain tail. */
964 uint32_t m_needed
= 1;
966 if (njcl
> 0 && len
> MBIGCLBYTES
)
967 mb
= m_getpackets_internal(&m_needed
, 1,
968 how
, 1, M16KCLBYTES
);
969 else if (len
> MCLBYTES
)
970 mb
= m_getpackets_internal(&m_needed
, 1,
971 how
, 1, MBIGCLBYTES
);
972 else if (len
>= (signed)MINCLSIZE
)
973 mb
= m_getpackets_internal(&m_needed
, 1,
976 mb
= m_gethdr(how
, MT_DATA
);
978 /* Fail the whole operation if one mbuf can't be allocated. */
986 VERIFY(mb
->m_flags
& M_PKTHDR
);
987 len
-= ((mb
->m_flags
& M_EXT
) ? mb
->m_ext
.ext_size
: MHLEN
);
999 /* Fill all mbufs with uio data and update header information. */
1000 for (mb
= m
; mb
!= NULL
; mb
= mb
->m_next
) {
1001 len
= imin(M_TRAILINGSPACE(mb
), tot
- progress
);
1003 error
= uiomove(mtod(mb
, char *), len
, uio
);
1009 /* each mbuf is M_PKTHDR chained via m_next */
1011 mb
->m_pkthdr
.len
= len
;
1015 VERIFY(progress
== tot
);
1021 * MPTCP socket protocol-user socket send routine, derived from sosend().
1024 mptcp_usr_sosend(struct socket
*mp_so
, struct sockaddr
*addr
, struct uio
*uio
,
1025 struct mbuf
*top
, struct mbuf
*control
, int flags
)
1027 #pragma unused(addr)
1030 int error
, sendflags
;
1031 struct proc
*p
= current_proc();
1034 /* UIO is required for now, due to per-mbuf M_PKTHDR constrains */
1035 if (uio
== NULL
|| top
!= NULL
) {
1039 resid
= uio_resid(uio
);
1041 socket_lock(mp_so
, 1);
1042 so_update_last_owner_locked(mp_so
, p
);
1043 so_update_policy(mp_so
);
1045 VERIFY(mp_so
->so_type
== SOCK_STREAM
);
1046 VERIFY(!(mp_so
->so_flags
& SOF_MP_SUBFLOW
));
1048 if ((flags
& (MSG_OOB
|MSG_DONTROUTE
|MSG_HOLD
|MSG_SEND
|MSG_FLUSH
)) ||
1049 (mp_so
->so_flags
& SOF_ENABLE_MSGS
)) {
1051 socket_unlock(mp_so
, 1);
1056 * In theory resid should be unsigned. However, space must be
1057 * signed, as it might be less than 0 if we over-committed, and we
1058 * must use a signed comparison of space and resid. On the other
1059 * hand, a negative resid causes us to loop sending 0-length
1060 * segments to the protocol.
1062 if (resid
< 0 || (flags
& MSG_EOR
) || control
!= NULL
) {
1064 socket_unlock(mp_so
, 1);
1068 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_msgsnd
);
1071 error
= sosendcheck(mp_so
, NULL
, resid
, 0, 0, flags
,
1076 space
= sbspace(&mp_so
->so_snd
);
1078 socket_unlock(mp_so
, 0);
1080 * Copy the data from userland into an mbuf chain.
1082 error
= mptcp_uiotombuf(uio
, M_WAITOK
, space
, 0, &top
);
1084 socket_lock(mp_so
, 0);
1087 VERIFY(top
!= NULL
);
1088 space
-= resid
- uio_resid(uio
);
1089 resid
= uio_resid(uio
);
1090 socket_lock(mp_so
, 0);
1093 * Compute flags here, for pru_send and NKEs.
1095 sendflags
= (resid
> 0 && space
> 0) ?
1096 PRUS_MORETOCOME
: 0;
1099 * Socket filter processing
1101 VERIFY(control
== NULL
);
1102 error
= sflt_data_out(mp_so
, NULL
, &top
, &control
, 0);
1104 if (error
== EJUSTRETURN
) {
1107 /* always free control if any */
1111 if (control
!= NULL
) {
1117 * Pass data to protocol.
1119 error
= (*mp_so
->so_proto
->pr_usrreqs
->pru_send
)
1120 (mp_so
, sendflags
, top
, NULL
, NULL
, p
);
1125 } while (resid
!= 0 && space
> 0);
1126 } while (resid
!= 0);
1130 sbunlock(&mp_so
->so_snd
, FALSE
); /* will unlock socket */
1132 socket_unlock(mp_so
, 1);
1136 if (control
!= NULL
)
1139 soclearfastopen(mp_so
);
1145 * Called to filter SOPT_{SET,GET} for SOL_SOCKET level socket options.
1146 * This routine simply indicates to the caller whether or not to proceed
1147 * further with the given socket option. This is invoked by sosetoptlock()
1148 * and sogetoptlock().
1151 mptcp_usr_socheckopt(struct socket
*mp_so
, struct sockopt
*sopt
)
1153 #pragma unused(mp_so)
1156 VERIFY(sopt
->sopt_level
== SOL_SOCKET
);
1159 * We could check for sopt_dir (set/get) here, but we'll just
1160 * let the caller deal with it as appropriate; therefore the
1161 * following is a superset of the socket options which we
1162 * allow for set/get.
1164 * XXX: adi@apple.com
1166 * Need to consider the following cases:
1168 * a. Certain socket options don't have a clear definition
1169 * on the expected behavior post connect(2). At the time
1170 * those options are issued on the MP socket, there may
1171 * be existing subflow sockets that are already connected.
1173 switch (sopt
->sopt_name
) {
1174 case SO_LINGER
: /* MP */
1175 case SO_LINGER_SEC
: /* MP */
1176 case SO_TYPE
: /* MP */
1177 case SO_NREAD
: /* MP */
1178 case SO_NWRITE
: /* MP */
1179 case SO_ERROR
: /* MP */
1180 case SO_SNDBUF
: /* MP */
1181 case SO_RCVBUF
: /* MP */
1182 case SO_SNDLOWAT
: /* MP */
1183 case SO_RCVLOWAT
: /* MP */
1184 case SO_SNDTIMEO
: /* MP */
1185 case SO_RCVTIMEO
: /* MP */
1186 case SO_NKE
: /* MP */
1187 case SO_NOSIGPIPE
: /* MP */
1188 case SO_NOADDRERR
: /* MP */
1189 case SO_LABEL
: /* MP */
1190 case SO_PEERLABEL
: /* MP */
1191 case SO_DEFUNCTOK
: /* MP */
1192 case SO_ISDEFUNCT
: /* MP */
1193 case SO_TRAFFIC_CLASS_DBG
: /* MP */
1194 case SO_DELEGATED
: /* MP */
1195 case SO_DELEGATED_UUID
: /* MP */
1197 case SO_NECP_ATTRIBUTES
:
1198 case SO_NECP_CLIENTUUID
:
1201 * Tell the caller that these options are to be processed.
1205 case SO_DEBUG
: /* MP + subflow */
1206 case SO_KEEPALIVE
: /* MP + subflow */
1207 case SO_USELOOPBACK
: /* MP + subflow */
1208 case SO_RANDOMPORT
: /* MP + subflow */
1209 case SO_TRAFFIC_CLASS
: /* MP + subflow */
1210 case SO_RECV_TRAFFIC_CLASS
: /* MP + subflow */
1211 case SO_PRIVILEGED_TRAFFIC_CLASS
: /* MP + subflow */
1212 case SO_RECV_ANYIF
: /* MP + subflow */
1213 case SO_RESTRICTIONS
: /* MP + subflow */
1214 case SO_FLUSH
: /* MP + subflow */
1215 case SO_NOWAKEFROMSLEEP
:
1216 case SO_NOAPNFALLBK
:
1217 case SO_MARK_CELLFALLBACK
:
1219 * Tell the caller that these options are to be processed;
1220 * these will also be recorded later by mptcp_setopt().
1222 * NOTE: Only support integer option value for now.
1224 if (sopt
->sopt_valsize
!= sizeof (int))
1230 * Tell the caller to stop immediately and return an error.
1232 error
= ENOPROTOOPT
;
1240 * Issue SOPT_SET for all MPTCP subflows (for integer option values.)
1243 mptcp_setopt_apply(struct mptses
*mpte
, struct mptopt
*mpo
)
1245 struct socket
*mp_so
;
1246 struct mptsub
*mpts
;
1250 /* just bail now if this isn't applicable to subflow sockets */
1251 if (!(mpo
->mpo_flags
& MPOF_SUBFLOW_OK
)) {
1252 error
= ENOPROTOOPT
;
1257 * Skip those that are handled internally; these options
1258 * should not have been recorded and marked with the
1259 * MPOF_SUBFLOW_OK by mptcp_setopt(), but just in case.
1261 if (mpo
->mpo_level
== SOL_SOCKET
&&
1262 (mpo
->mpo_name
== SO_NOSIGPIPE
|| mpo
->mpo_name
== SO_NOADDRERR
)) {
1263 error
= ENOPROTOOPT
;
1267 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1268 mp_so
= mptetoso(mpte
);
1271 * Don't bother going further if there's no subflow; mark the option
1272 * with MPOF_INTERIM so that we know whether or not to remove this
1273 * option upon encountering an error while issuing it during subflow
1276 if (mpte
->mpte_numflows
== 0) {
1277 VERIFY(TAILQ_EMPTY(&mpte
->mpte_subflows
));
1278 mpo
->mpo_flags
|= MPOF_INTERIM
;
1279 /* return success */
1283 bzero(&smpo
, sizeof (smpo
));
1284 smpo
.mpo_flags
|= MPOF_SUBFLOW_OK
;
1285 smpo
.mpo_level
= mpo
->mpo_level
;
1286 smpo
.mpo_name
= mpo
->mpo_name
;
1288 /* grab exisiting values in case we need to rollback */
1289 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1292 mpts
->mpts_flags
&= ~(MPTSF_SOPT_OLDVAL
|MPTSF_SOPT_INPROG
);
1293 mpts
->mpts_oldintval
= 0;
1294 smpo
.mpo_intval
= 0;
1295 VERIFY(mpts
->mpts_socket
!= NULL
);
1296 so
= mpts
->mpts_socket
;
1297 if (mptcp_subflow_sogetopt(mpte
, so
, &smpo
) == 0) {
1298 mpts
->mpts_flags
|= MPTSF_SOPT_OLDVAL
;
1299 mpts
->mpts_oldintval
= smpo
.mpo_intval
;
1303 /* apply socket option */
1304 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1307 mpts
->mpts_flags
|= MPTSF_SOPT_INPROG
;
1308 VERIFY(mpts
->mpts_socket
!= NULL
);
1309 so
= mpts
->mpts_socket
;
1310 error
= mptcp_subflow_sosetopt(mpte
, mpts
, mpo
);
1315 /* cleanup, and rollback if needed */
1316 TAILQ_FOREACH(mpts
, &mpte
->mpte_subflows
, mpts_entry
) {
1319 if (!(mpts
->mpts_flags
& MPTSF_SOPT_INPROG
)) {
1320 /* clear in case it's set */
1321 mpts
->mpts_flags
&= ~MPTSF_SOPT_OLDVAL
;
1322 mpts
->mpts_oldintval
= 0;
1325 if (!(mpts
->mpts_flags
& MPTSF_SOPT_OLDVAL
)) {
1326 mpts
->mpts_flags
&= ~MPTSF_SOPT_INPROG
;
1327 VERIFY(mpts
->mpts_oldintval
== 0);
1330 /* error during sosetopt, so roll it back */
1332 VERIFY(mpts
->mpts_socket
!= NULL
);
1333 so
= mpts
->mpts_socket
;
1334 smpo
.mpo_intval
= mpts
->mpts_oldintval
;
1335 mptcp_subflow_sosetopt(mpte
, mpts
, &smpo
);
1337 mpts
->mpts_oldintval
= 0;
1338 mpts
->mpts_flags
&= ~(MPTSF_SOPT_OLDVAL
|MPTSF_SOPT_INPROG
);
1346 * Handle SOPT_SET for socket options issued on MP socket.
1349 mptcp_setopt(struct mptses
*mpte
, struct sockopt
*sopt
)
1351 int error
= 0, optval
= 0, level
, optname
, rec
= 1;
1352 struct mptopt smpo
, *mpo
= NULL
;
1353 struct socket
*mp_so
;
1355 level
= sopt
->sopt_level
;
1356 optname
= sopt
->sopt_name
;
1358 mp_so
= mptetoso(mpte
);
1361 * Record socket options which are applicable to subflow sockets so
1362 * that we can replay them for new ones; see mptcp_usr_socheckopt()
1363 * for the list of eligible socket-level options.
1365 if (level
== SOL_SOCKET
) {
1369 case SO_USELOOPBACK
:
1371 case SO_TRAFFIC_CLASS
:
1372 case SO_RECV_TRAFFIC_CLASS
:
1373 case SO_PRIVILEGED_TRAFFIC_CLASS
:
1375 case SO_RESTRICTIONS
:
1376 case SO_NOWAKEFROMSLEEP
:
1377 case SO_NOAPNFALLBK
:
1378 case SO_MARK_CELLFALLBACK
:
1382 /* don't record it */
1386 /* Next ones, record at MPTCP-level */
1388 case SO_NECP_CLIENTUUID
:
1389 if (!uuid_is_null(mpsotomppcb(mp_so
)->necp_client_uuid
)) {
1394 error
= sooptcopyin(sopt
, &mpsotomppcb(mp_so
)->necp_client_uuid
,
1395 sizeof(uuid_t
), sizeof(uuid_t
));
1400 mpsotomppcb(mp_so
)->necp_cb
= mptcp_session_necp_cb
;
1401 error
= necp_client_register_multipath_cb(mp_so
->last_pid
,
1402 mpsotomppcb(mp_so
)->necp_client_uuid
,
1403 mpsotomppcb(mp_so
));
1407 if (uuid_is_null(mpsotomppcb(mp_so
)->necp_client_uuid
)) {
1413 case SO_NECP_ATTRIBUTES
:
1416 /* nothing to do; just return */
1422 case TCP_RXT_FINDROP
:
1426 case TCP_CONNECTIONTIMEOUT
:
1427 case TCP_RXT_CONNDROPTIME
:
1428 case PERSIST_TIMEOUT
:
1429 case TCP_ADAPTIVE_READ_TIMEOUT
:
1430 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1431 /* eligible; record it */
1433 case TCP_NOTSENT_LOWAT
:
1434 /* record at MPTCP level */
1435 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1444 mp_so
->so_flags
&= ~SOF_NOTSENT_LOWAT
;
1445 error
= mptcp_set_notsent_lowat(mpte
,0);
1447 mp_so
->so_flags
|= SOF_NOTSENT_LOWAT
;
1448 error
= mptcp_set_notsent_lowat(mpte
,
1453 case MPTCP_SERVICE_TYPE
:
1454 /* record at MPTCP level */
1455 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1459 if (optval
< 0 || optval
>= MPTCP_SVCTYPE_MAX
) {
1464 mpte
->mpte_svctype
= optval
;
1466 if (mptcp_entitlement_check(mp_so
) < 0) {
1471 mpte
->mpte_flags
|= MPTE_SVCTYPE_CHECKED
;
1474 case MPTCP_ALTERNATE_PORT
:
1475 /* record at MPTCP level */
1476 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1481 if (optval
< 0 || optval
> UINT16_MAX
) {
1486 mpte
->mpte_alternate_port
= optval
;
1491 error
= ENOPROTOOPT
;
1496 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
1497 sizeof (optval
))) != 0)
1501 /* search for an existing one; if not found, allocate */
1502 if ((mpo
= mptcp_sopt_find(mpte
, sopt
)) == NULL
)
1503 mpo
= mptcp_sopt_alloc(M_WAITOK
);
1508 mptcplog((LOG_DEBUG
, "%s: mp_so 0x%llx sopt %s val %d %s\n",
1509 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1510 mptcp_sopt2str(level
, optname
), optval
,
1511 (mpo
->mpo_flags
& MPOF_ATTACHED
) ?
1512 "updated" : "recorded"),
1513 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1515 /* initialize or update, as needed */
1516 mpo
->mpo_intval
= optval
;
1517 if (!(mpo
->mpo_flags
& MPOF_ATTACHED
)) {
1518 mpo
->mpo_level
= level
;
1519 mpo
->mpo_name
= optname
;
1520 mptcp_sopt_insert(mpte
, mpo
);
1522 /* this can be issued on the subflow socket */
1523 mpo
->mpo_flags
|= MPOF_SUBFLOW_OK
;
1526 bzero(&smpo
, sizeof (smpo
));
1528 mpo
->mpo_flags
|= MPOF_SUBFLOW_OK
;
1529 mpo
->mpo_level
= level
;
1530 mpo
->mpo_name
= optname
;
1531 mpo
->mpo_intval
= optval
;
1534 /* issue this socket option on existing subflows */
1536 error
= mptcp_setopt_apply(mpte
, mpo
);
1537 if (error
!= 0 && (mpo
->mpo_flags
& MPOF_ATTACHED
)) {
1538 VERIFY(mpo
!= &smpo
);
1539 mptcp_sopt_remove(mpte
, mpo
);
1540 mptcp_sopt_free(mpo
);
1543 mpo
->mpo_flags
&= ~MPOF_INTERIM
;
1546 if (error
== 0 && mpo
!= NULL
) {
1547 mptcplog((LOG_INFO
, "%s: mp_so 0x%llx sopt %s val %d set %s\n",
1548 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1549 mptcp_sopt2str(level
, optname
), optval
,
1550 (mpo
->mpo_flags
& MPOF_INTERIM
) ?
1551 "pending" : "successful"),
1552 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1553 } else if (error
!= 0) {
1554 mptcplog((LOG_ERR
, "%s: mp_so 0x%llx sopt %s (%d, %d) val %d can't be issued error %d\n",
1555 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1556 mptcp_sopt2str(level
, optname
), level
, optname
, optval
, error
),
1557 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
1563 * Handle SOPT_GET for socket options issued on MP socket.
1566 mptcp_getopt(struct mptses
*mpte
, struct sockopt
*sopt
)
1568 int error
= 0, optval
= 0;
1571 * We only handle SOPT_GET for TCP level socket options; we should
1572 * not get here for socket level options since they are already
1573 * handled at the socket layer.
1575 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
1576 error
= ENOPROTOOPT
;
1580 switch (sopt
->sopt_name
) {
1582 case TCP_RXT_FINDROP
:
1586 case TCP_CONNECTIONTIMEOUT
:
1587 case TCP_RXT_CONNDROPTIME
:
1588 case PERSIST_TIMEOUT
:
1589 case TCP_ADAPTIVE_READ_TIMEOUT
:
1590 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1591 case TCP_NOTSENT_LOWAT
:
1592 case MPTCP_SERVICE_TYPE
:
1593 case MPTCP_ALTERNATE_PORT
:
1594 /* eligible; get the default value just in case */
1595 error
= mptcp_default_tcp_optval(mpte
, sopt
, &optval
);
1599 error
= ENOPROTOOPT
;
1603 switch (sopt
->sopt_name
) {
1604 case TCP_NOTSENT_LOWAT
:
1605 if (mptetoso(mpte
)->so_flags
& SOF_NOTSENT_LOWAT
)
1606 optval
= mptcp_get_notsent_lowat(mpte
);
1610 case MPTCP_SERVICE_TYPE
:
1611 optval
= mpte
->mpte_svctype
;
1613 case MPTCP_ALTERNATE_PORT
:
1614 optval
= mpte
->mpte_alternate_port
;
1619 * Search for a previously-issued TCP level socket option and
1620 * return the recorded option value. This assumes that the
1621 * value did not get modified by the lower layer after it was
1622 * issued at setsockopt(2) time. If not found, we'll return
1623 * the default value obtained ealier.
1628 if ((mpo
= mptcp_sopt_find(mpte
, sopt
)) != NULL
)
1629 optval
= mpo
->mpo_intval
;
1631 error
= sooptcopyout(sopt
, &optval
, sizeof (int));
1638 * Return default values for TCP socket options. Ideally we would query the
1639 * subflow TCP socket, but that requires creating a subflow socket before
1640 * connectx(2) time. To simplify things, just return the default values
1644 mptcp_default_tcp_optval(struct mptses
*mpte
, struct sockopt
*sopt
, int *optval
)
1648 VERIFY(sopt
->sopt_level
== IPPROTO_TCP
);
1649 VERIFY(sopt
->sopt_dir
== SOPT_GET
);
1650 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1652 /* try to do what tcp_newtcpcb() does */
1653 switch (sopt
->sopt_name
) {
1655 case TCP_RXT_FINDROP
:
1658 case TCP_CONNECTIONTIMEOUT
:
1659 case TCP_RXT_CONNDROPTIME
:
1660 case TCP_NOTSENT_LOWAT
:
1661 case TCP_ADAPTIVE_READ_TIMEOUT
:
1662 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1663 case MPTCP_SERVICE_TYPE
:
1664 case MPTCP_ALTERNATE_PORT
:
1669 *optval
= mptcp_subflow_keeptime
;
1672 case PERSIST_TIMEOUT
:
1673 *optval
= tcp_max_persist_timeout
;
1677 error
= ENOPROTOOPT
;
1684 * MPTCP SOPT_{SET,GET} socket option handler, for options issued on the MP
1685 * socket, at SOL_SOCKET and IPPROTO_TCP levels. The former is restricted
1686 * to those that are allowed by mptcp_usr_socheckopt().
1689 mptcp_ctloutput(struct socket
*mp_so
, struct sockopt
*sopt
)
1691 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
1692 struct mptses
*mpte
;
1695 if (mpp
== NULL
|| mpp
->mpp_state
== MPPCB_STATE_DEAD
) {
1699 mpte
= mptompte(mpp
);
1700 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1702 /* we only handle socket and TCP-level socket options for MPTCP */
1703 if (sopt
->sopt_level
!= SOL_SOCKET
&& sopt
->sopt_level
!= IPPROTO_TCP
) {
1704 mptcplog((LOG_DEBUG
, "MPTCP Socket: "
1705 "%s: mp_so 0x%llx sopt %s level not "
1706 "handled\n", __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
),
1707 mptcp_sopt2str(sopt
->sopt_level
, sopt
->sopt_name
)),
1708 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_LOG
);
1713 switch (sopt
->sopt_dir
) {
1715 error
= mptcp_setopt(mpte
, sopt
);
1719 error
= mptcp_getopt(mpte
, sopt
);
1727 mptcp_sopt2str(int level
, int optname
)
1733 return ("SO_LINGER");
1735 return ("SO_LINGER_SEC");
1737 return ("SO_DEBUG");
1739 return ("SO_KEEPALIVE");
1740 case SO_USELOOPBACK
:
1741 return ("SO_USELOOPBACK");
1745 return ("SO_NREAD");
1747 return ("SO_NWRITE");
1749 return ("SO_ERROR");
1751 return ("SO_SNDBUF");
1753 return ("SO_RCVBUF");
1755 return ("SO_SNDLOWAT");
1757 return ("SO_RCVLOWAT");
1759 return ("SO_SNDTIMEO");
1761 return ("SO_RCVTIMEO");
1765 return ("SO_NOSIGPIPE");
1767 return ("SO_NOADDRERR");
1768 case SO_RESTRICTIONS
:
1769 return ("SO_RESTRICTIONS");
1771 return ("SO_LABEL");
1773 return ("SO_PEERLABEL");
1775 return ("SO_RANDOMPORT");
1776 case SO_TRAFFIC_CLASS
:
1777 return ("SO_TRAFFIC_CLASS");
1778 case SO_RECV_TRAFFIC_CLASS
:
1779 return ("SO_RECV_TRAFFIC_CLASS");
1780 case SO_TRAFFIC_CLASS_DBG
:
1781 return ("SO_TRAFFIC_CLASS_DBG");
1782 case SO_PRIVILEGED_TRAFFIC_CLASS
:
1783 return ("SO_PRIVILEGED_TRAFFIC_CLASS");
1785 return ("SO_DEFUNCTOK");
1787 return ("SO_ISDEFUNCT");
1788 case SO_OPPORTUNISTIC
:
1789 return ("SO_OPPORTUNISTIC");
1791 return ("SO_FLUSH");
1793 return ("SO_RECV_ANYIF");
1794 case SO_NOWAKEFROMSLEEP
:
1795 return ("SO_NOWAKEFROMSLEEP");
1796 case SO_NOAPNFALLBK
:
1797 return ("SO_NOAPNFALLBK");
1798 case SO_MARK_CELLFALLBACK
:
1799 return ("SO_CELLFALLBACK");
1801 return ("SO_DELEGATED");
1802 case SO_DELEGATED_UUID
:
1803 return ("SO_DELEGATED_UUID");
1805 case SO_NECP_ATTRIBUTES
:
1806 return ("SO_NECP_ATTRIBUTES");
1807 case SO_NECP_CLIENTUUID
:
1808 return ("SO_NECP_CLIENTUUID");
1816 return ("TCP_NODELAY");
1818 return ("TCP_KEEPALIVE");
1820 return ("TCP_KEEPINTVL");
1822 return ("TCP_KEEPCNT");
1823 case TCP_CONNECTIONTIMEOUT
:
1824 return ("TCP_CONNECTIONTIMEOUT");
1825 case TCP_RXT_CONNDROPTIME
:
1826 return ("TCP_RXT_CONNDROPTIME");
1827 case PERSIST_TIMEOUT
:
1828 return ("PERSIST_TIMEOUT");
1829 case TCP_NOTSENT_LOWAT
:
1830 return ("NOTSENT_LOWAT");
1831 case TCP_ADAPTIVE_READ_TIMEOUT
:
1832 return ("ADAPTIVE_READ_TIMEOUT");
1833 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1834 return ("ADAPTIVE_WRITE_TIMEOUT");
1835 case MPTCP_SERVICE_TYPE
:
1836 return ("MPTCP_SERVICE_TYPE");
1837 case MPTCP_ALTERNATE_PORT
:
1838 return ("MPTCP_ALTERNATE_PORT");
1848 mptcp_usr_preconnect(struct socket
*mp_so
)
1850 struct mptsub
*mpts
= NULL
;
1851 struct mppcb
*mpp
= mpsotomppcb(mp_so
);
1852 struct mptses
*mpte
;
1854 struct tcpcb
*tp
= NULL
;
1857 mpte
= mptompte(mpp
);
1858 VERIFY(mpte
!= NULL
);
1859 mpte_lock_assert_held(mpte
); /* same as MP socket lock */
1861 mpts
= mptcp_get_subflow(mpte
, NULL
, NULL
);
1863 mptcplog((LOG_ERR
, "%s: mp_so 0x%llx invalid preconnect ",
1864 __func__
, (u_int64_t
)VM_KERNEL_ADDRPERM(mp_so
)),
1865 MPTCP_SOCKET_DBG
, MPTCP_LOGLVL_ERR
);
1868 mpts
->mpts_flags
&= ~MPTSF_TFO_REQD
;
1869 so
= mpts
->mpts_socket
;
1870 tp
= intotcpcb(sotoinpcb(so
));
1871 tp
->t_mpflags
&= ~TMPF_TFO_REQUEST
;
1872 error
= tcp_output(sototcpcb(so
));
1874 soclearfastopen(mp_so
);