1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions * Copyright (c) 2012-2017 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions *
4*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions *
6*5e3eaea3SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions *
15*5e3eaea3SApple OSS Distributions * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions *
18*5e3eaea3SApple OSS Distributions * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions * limitations under the License.
25*5e3eaea3SApple OSS Distributions *
26*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions */
28*5e3eaea3SApple OSS Distributions
29*5e3eaea3SApple OSS Distributions #include <sys/param.h>
30*5e3eaea3SApple OSS Distributions #include <sys/systm.h>
31*5e3eaea3SApple OSS Distributions #include <sys/kernel.h>
32*5e3eaea3SApple OSS Distributions #include <sys/mbuf.h>
33*5e3eaea3SApple OSS Distributions #include <sys/mcache.h>
34*5e3eaea3SApple OSS Distributions #include <sys/syslog.h>
35*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
36*5e3eaea3SApple OSS Distributions #include <sys/socketvar.h>
37*5e3eaea3SApple OSS Distributions #include <sys/protosw.h>
38*5e3eaea3SApple OSS Distributions #include <sys/proc_internal.h>
39*5e3eaea3SApple OSS Distributions
40*5e3eaea3SApple OSS Distributions #include <mach/boolean.h>
41*5e3eaea3SApple OSS Distributions #include <kern/zalloc.h>
42*5e3eaea3SApple OSS Distributions #include <kern/locks.h>
43*5e3eaea3SApple OSS Distributions
44*5e3eaea3SApple OSS Distributions #include <netinet/mp_pcb.h>
45*5e3eaea3SApple OSS Distributions #include <netinet/mptcp_var.h>
46*5e3eaea3SApple OSS Distributions #include <netinet6/in6_pcb.h>
47*5e3eaea3SApple OSS Distributions
48*5e3eaea3SApple OSS Distributions static LCK_GRP_DECLARE(mp_lock_grp, "multipath");
49*5e3eaea3SApple OSS Distributions static LCK_ATTR_DECLARE(mp_lock_attr, 0, 0);
50*5e3eaea3SApple OSS Distributions static LCK_MTX_DECLARE_ATTR(mp_lock, &mp_lock_grp, &mp_lock_attr);
51*5e3eaea3SApple OSS Distributions static LCK_MTX_DECLARE_ATTR(mp_timeout_lock, &mp_lock_grp, &mp_lock_attr);
52*5e3eaea3SApple OSS Distributions
53*5e3eaea3SApple OSS Distributions static TAILQ_HEAD(, mppcbinfo) mppi_head = TAILQ_HEAD_INITIALIZER(mppi_head);
54*5e3eaea3SApple OSS Distributions
55*5e3eaea3SApple OSS Distributions static boolean_t mp_timeout_run; /* MP timer is scheduled to run */
56*5e3eaea3SApple OSS Distributions static boolean_t mp_garbage_collecting;
57*5e3eaea3SApple OSS Distributions static boolean_t mp_ticking;
58*5e3eaea3SApple OSS Distributions static void mp_sched_timeout(void);
59*5e3eaea3SApple OSS Distributions static void mp_timeout(void *);
60*5e3eaea3SApple OSS Distributions
61*5e3eaea3SApple OSS Distributions static void
mpp_lock_assert_held(struct mppcb * mp)62*5e3eaea3SApple OSS Distributions mpp_lock_assert_held(struct mppcb *mp)
63*5e3eaea3SApple OSS Distributions {
64*5e3eaea3SApple OSS Distributions #if !MACH_ASSERT
65*5e3eaea3SApple OSS Distributions #pragma unused(mp)
66*5e3eaea3SApple OSS Distributions #endif
67*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(&mp->mpp_lock, LCK_MTX_ASSERT_OWNED);
68*5e3eaea3SApple OSS Distributions }
69*5e3eaea3SApple OSS Distributions
70*5e3eaea3SApple OSS Distributions static void
mp_timeout(void * arg)71*5e3eaea3SApple OSS Distributions mp_timeout(void *arg)
72*5e3eaea3SApple OSS Distributions {
73*5e3eaea3SApple OSS Distributions #pragma unused(arg)
74*5e3eaea3SApple OSS Distributions struct mppcbinfo *mppi;
75*5e3eaea3SApple OSS Distributions boolean_t t, gc;
76*5e3eaea3SApple OSS Distributions uint32_t t_act = 0;
77*5e3eaea3SApple OSS Distributions uint32_t gc_act = 0;
78*5e3eaea3SApple OSS Distributions
79*5e3eaea3SApple OSS Distributions /*
80*5e3eaea3SApple OSS Distributions * Update coarse-grained networking timestamp (in sec.); the idea
81*5e3eaea3SApple OSS Distributions * is to piggy-back on the timeout callout to update the counter
82*5e3eaea3SApple OSS Distributions * returnable via net_uptime().
83*5e3eaea3SApple OSS Distributions */
84*5e3eaea3SApple OSS Distributions net_update_uptime();
85*5e3eaea3SApple OSS Distributions
86*5e3eaea3SApple OSS Distributions lck_mtx_lock_spin(&mp_timeout_lock);
87*5e3eaea3SApple OSS Distributions gc = mp_garbage_collecting;
88*5e3eaea3SApple OSS Distributions mp_garbage_collecting = FALSE;
89*5e3eaea3SApple OSS Distributions
90*5e3eaea3SApple OSS Distributions t = mp_ticking;
91*5e3eaea3SApple OSS Distributions mp_ticking = FALSE;
92*5e3eaea3SApple OSS Distributions
93*5e3eaea3SApple OSS Distributions if (gc || t) {
94*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mp_timeout_lock);
95*5e3eaea3SApple OSS Distributions
96*5e3eaea3SApple OSS Distributions lck_mtx_lock(&mp_lock);
97*5e3eaea3SApple OSS Distributions TAILQ_FOREACH(mppi, &mppi_head, mppi_entry) {
98*5e3eaea3SApple OSS Distributions if ((gc && mppi->mppi_gc != NULL) ||
99*5e3eaea3SApple OSS Distributions (t && mppi->mppi_timer != NULL)) {
100*5e3eaea3SApple OSS Distributions lck_mtx_lock(&mppi->mppi_lock);
101*5e3eaea3SApple OSS Distributions if (gc && mppi->mppi_gc != NULL) {
102*5e3eaea3SApple OSS Distributions gc_act += mppi->mppi_gc(mppi);
103*5e3eaea3SApple OSS Distributions }
104*5e3eaea3SApple OSS Distributions if (t && mppi->mppi_timer != NULL) {
105*5e3eaea3SApple OSS Distributions t_act += mppi->mppi_timer(mppi);
106*5e3eaea3SApple OSS Distributions }
107*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mppi->mppi_lock);
108*5e3eaea3SApple OSS Distributions }
109*5e3eaea3SApple OSS Distributions }
110*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mp_lock);
111*5e3eaea3SApple OSS Distributions
112*5e3eaea3SApple OSS Distributions lck_mtx_lock_spin(&mp_timeout_lock);
113*5e3eaea3SApple OSS Distributions }
114*5e3eaea3SApple OSS Distributions
115*5e3eaea3SApple OSS Distributions /* lock was dropped above, so check first before overriding */
116*5e3eaea3SApple OSS Distributions if (!mp_garbage_collecting) {
117*5e3eaea3SApple OSS Distributions mp_garbage_collecting = (gc_act != 0);
118*5e3eaea3SApple OSS Distributions }
119*5e3eaea3SApple OSS Distributions if (!mp_ticking) {
120*5e3eaea3SApple OSS Distributions mp_ticking = (t_act != 0);
121*5e3eaea3SApple OSS Distributions }
122*5e3eaea3SApple OSS Distributions
123*5e3eaea3SApple OSS Distributions /* re-arm the timer if there's work to do */
124*5e3eaea3SApple OSS Distributions mp_timeout_run = FALSE;
125*5e3eaea3SApple OSS Distributions mp_sched_timeout();
126*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mp_timeout_lock);
127*5e3eaea3SApple OSS Distributions }
128*5e3eaea3SApple OSS Distributions
129*5e3eaea3SApple OSS Distributions static void
mp_sched_timeout(void)130*5e3eaea3SApple OSS Distributions mp_sched_timeout(void)
131*5e3eaea3SApple OSS Distributions {
132*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(&mp_timeout_lock, LCK_MTX_ASSERT_OWNED);
133*5e3eaea3SApple OSS Distributions
134*5e3eaea3SApple OSS Distributions if (!mp_timeout_run && (mp_garbage_collecting || mp_ticking)) {
135*5e3eaea3SApple OSS Distributions lck_mtx_convert_spin(&mp_timeout_lock);
136*5e3eaea3SApple OSS Distributions mp_timeout_run = TRUE;
137*5e3eaea3SApple OSS Distributions timeout(mp_timeout, NULL, hz);
138*5e3eaea3SApple OSS Distributions }
139*5e3eaea3SApple OSS Distributions }
140*5e3eaea3SApple OSS Distributions
141*5e3eaea3SApple OSS Distributions void
mp_gc_sched(void)142*5e3eaea3SApple OSS Distributions mp_gc_sched(void)
143*5e3eaea3SApple OSS Distributions {
144*5e3eaea3SApple OSS Distributions lck_mtx_lock_spin(&mp_timeout_lock);
145*5e3eaea3SApple OSS Distributions mp_garbage_collecting = TRUE;
146*5e3eaea3SApple OSS Distributions mp_sched_timeout();
147*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mp_timeout_lock);
148*5e3eaea3SApple OSS Distributions }
149*5e3eaea3SApple OSS Distributions
150*5e3eaea3SApple OSS Distributions void
mptcp_timer_sched(void)151*5e3eaea3SApple OSS Distributions mptcp_timer_sched(void)
152*5e3eaea3SApple OSS Distributions {
153*5e3eaea3SApple OSS Distributions lck_mtx_lock_spin(&mp_timeout_lock);
154*5e3eaea3SApple OSS Distributions mp_ticking = TRUE;
155*5e3eaea3SApple OSS Distributions mp_sched_timeout();
156*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mp_timeout_lock);
157*5e3eaea3SApple OSS Distributions }
158*5e3eaea3SApple OSS Distributions
159*5e3eaea3SApple OSS Distributions void
mp_pcbinfo_attach(struct mppcbinfo * mppi)160*5e3eaea3SApple OSS Distributions mp_pcbinfo_attach(struct mppcbinfo *mppi)
161*5e3eaea3SApple OSS Distributions {
162*5e3eaea3SApple OSS Distributions struct mppcbinfo *mppi0;
163*5e3eaea3SApple OSS Distributions
164*5e3eaea3SApple OSS Distributions lck_mtx_lock(&mp_lock);
165*5e3eaea3SApple OSS Distributions TAILQ_FOREACH(mppi0, &mppi_head, mppi_entry) {
166*5e3eaea3SApple OSS Distributions if (mppi0 == mppi) {
167*5e3eaea3SApple OSS Distributions panic("%s: mppi %p already in the list",
168*5e3eaea3SApple OSS Distributions __func__, mppi);
169*5e3eaea3SApple OSS Distributions /* NOTREACHED */
170*5e3eaea3SApple OSS Distributions }
171*5e3eaea3SApple OSS Distributions }
172*5e3eaea3SApple OSS Distributions TAILQ_INSERT_TAIL(&mppi_head, mppi, mppi_entry);
173*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mp_lock);
174*5e3eaea3SApple OSS Distributions }
175*5e3eaea3SApple OSS Distributions
176*5e3eaea3SApple OSS Distributions int
mp_pcbinfo_detach(struct mppcbinfo * mppi)177*5e3eaea3SApple OSS Distributions mp_pcbinfo_detach(struct mppcbinfo *mppi)
178*5e3eaea3SApple OSS Distributions {
179*5e3eaea3SApple OSS Distributions struct mppcbinfo *mppi0;
180*5e3eaea3SApple OSS Distributions int error = 0;
181*5e3eaea3SApple OSS Distributions
182*5e3eaea3SApple OSS Distributions lck_mtx_lock(&mp_lock);
183*5e3eaea3SApple OSS Distributions TAILQ_FOREACH(mppi0, &mppi_head, mppi_entry) {
184*5e3eaea3SApple OSS Distributions if (mppi0 == mppi) {
185*5e3eaea3SApple OSS Distributions break;
186*5e3eaea3SApple OSS Distributions }
187*5e3eaea3SApple OSS Distributions }
188*5e3eaea3SApple OSS Distributions if (mppi0 != NULL) {
189*5e3eaea3SApple OSS Distributions TAILQ_REMOVE(&mppi_head, mppi0, mppi_entry);
190*5e3eaea3SApple OSS Distributions } else {
191*5e3eaea3SApple OSS Distributions error = ENXIO;
192*5e3eaea3SApple OSS Distributions }
193*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mp_lock);
194*5e3eaea3SApple OSS Distributions
195*5e3eaea3SApple OSS Distributions return error;
196*5e3eaea3SApple OSS Distributions }
197*5e3eaea3SApple OSS Distributions
198*5e3eaea3SApple OSS Distributions int
mp_pcballoc(struct socket * so,struct mppcbinfo * mppi)199*5e3eaea3SApple OSS Distributions mp_pcballoc(struct socket *so, struct mppcbinfo *mppi)
200*5e3eaea3SApple OSS Distributions {
201*5e3eaea3SApple OSS Distributions struct mppcb *mpp = NULL;
202*5e3eaea3SApple OSS Distributions int error;
203*5e3eaea3SApple OSS Distributions
204*5e3eaea3SApple OSS Distributions VERIFY(mpsotomppcb(so) == NULL);
205*5e3eaea3SApple OSS Distributions
206*5e3eaea3SApple OSS Distributions mpp = mppi->mppi_alloc();
207*5e3eaea3SApple OSS Distributions lck_mtx_init(&mpp->mpp_lock, mppi->mppi_lock_grp, &mppi->mppi_lock_attr);
208*5e3eaea3SApple OSS Distributions mpp->mpp_pcbinfo = mppi;
209*5e3eaea3SApple OSS Distributions mpp->mpp_state = MPPCB_STATE_INUSE;
210*5e3eaea3SApple OSS Distributions mpp->mpp_socket = so;
211*5e3eaea3SApple OSS Distributions so->so_pcb = mpp;
212*5e3eaea3SApple OSS Distributions
213*5e3eaea3SApple OSS Distributions error = mptcp_session_create(mpp);
214*5e3eaea3SApple OSS Distributions if (error) {
215*5e3eaea3SApple OSS Distributions lck_mtx_destroy(&mpp->mpp_lock, mppi->mppi_lock_grp);
216*5e3eaea3SApple OSS Distributions mppi->mppi_free(mpp);
217*5e3eaea3SApple OSS Distributions return error;
218*5e3eaea3SApple OSS Distributions }
219*5e3eaea3SApple OSS Distributions
220*5e3eaea3SApple OSS Distributions lck_mtx_lock(&mppi->mppi_lock);
221*5e3eaea3SApple OSS Distributions mpp->mpp_flags |= MPP_ATTACHED;
222*5e3eaea3SApple OSS Distributions TAILQ_INSERT_TAIL(&mppi->mppi_pcbs, mpp, mpp_entry);
223*5e3eaea3SApple OSS Distributions mppi->mppi_count++;
224*5e3eaea3SApple OSS Distributions
225*5e3eaea3SApple OSS Distributions lck_mtx_unlock(&mppi->mppi_lock);
226*5e3eaea3SApple OSS Distributions
227*5e3eaea3SApple OSS Distributions return 0;
228*5e3eaea3SApple OSS Distributions }
229*5e3eaea3SApple OSS Distributions
230*5e3eaea3SApple OSS Distributions void
mp_pcbdetach(struct socket * mp_so)231*5e3eaea3SApple OSS Distributions mp_pcbdetach(struct socket *mp_so)
232*5e3eaea3SApple OSS Distributions {
233*5e3eaea3SApple OSS Distributions struct mppcb *mpp = mpsotomppcb(mp_so);
234*5e3eaea3SApple OSS Distributions
235*5e3eaea3SApple OSS Distributions mpp->mpp_state = MPPCB_STATE_DEAD;
236*5e3eaea3SApple OSS Distributions
237*5e3eaea3SApple OSS Distributions mp_gc_sched();
238*5e3eaea3SApple OSS Distributions }
239*5e3eaea3SApple OSS Distributions
240*5e3eaea3SApple OSS Distributions void
mptcp_pcbdispose(struct mppcb * mpp)241*5e3eaea3SApple OSS Distributions mptcp_pcbdispose(struct mppcb *mpp)
242*5e3eaea3SApple OSS Distributions {
243*5e3eaea3SApple OSS Distributions struct mppcbinfo *mppi = mpp->mpp_pcbinfo;
244*5e3eaea3SApple OSS Distributions struct socket *mp_so = mpp->mpp_socket;
245*5e3eaea3SApple OSS Distributions
246*5e3eaea3SApple OSS Distributions VERIFY(mppi != NULL);
247*5e3eaea3SApple OSS Distributions
248*5e3eaea3SApple OSS Distributions LCK_MTX_ASSERT(&mppi->mppi_lock, LCK_MTX_ASSERT_OWNED);
249*5e3eaea3SApple OSS Distributions mpp_lock_assert_held(mpp);
250*5e3eaea3SApple OSS Distributions
251*5e3eaea3SApple OSS Distributions VERIFY(mpp->mpp_state == MPPCB_STATE_DEAD);
252*5e3eaea3SApple OSS Distributions VERIFY(mpp->mpp_flags & MPP_ATTACHED);
253*5e3eaea3SApple OSS Distributions
254*5e3eaea3SApple OSS Distributions mpp->mpp_flags &= ~MPP_ATTACHED;
255*5e3eaea3SApple OSS Distributions TAILQ_REMOVE(&mppi->mppi_pcbs, mpp, mpp_entry);
256*5e3eaea3SApple OSS Distributions VERIFY(mppi->mppi_count != 0);
257*5e3eaea3SApple OSS Distributions mppi->mppi_count--;
258*5e3eaea3SApple OSS Distributions
259*5e3eaea3SApple OSS Distributions if (mppi->mppi_count == 0) {
260*5e3eaea3SApple OSS Distributions if (mptcp_cellicon_refcount) {
261*5e3eaea3SApple OSS Distributions os_log_error(mptcp_log_handle, "%s: No more MPTCP-flows, but cell icon counter is %u\n",
262*5e3eaea3SApple OSS Distributions __func__, mptcp_cellicon_refcount);
263*5e3eaea3SApple OSS Distributions mptcp_clear_cellicon();
264*5e3eaea3SApple OSS Distributions mptcp_cellicon_refcount = 0;
265*5e3eaea3SApple OSS Distributions }
266*5e3eaea3SApple OSS Distributions }
267*5e3eaea3SApple OSS Distributions
268*5e3eaea3SApple OSS Distributions VERIFY(mpp->mpp_inside == 0);
269*5e3eaea3SApple OSS Distributions mpp_unlock(mpp);
270*5e3eaea3SApple OSS Distributions
271*5e3eaea3SApple OSS Distributions #if NECP
272*5e3eaea3SApple OSS Distributions necp_mppcb_dispose(mpp);
273*5e3eaea3SApple OSS Distributions #endif /* NECP */
274*5e3eaea3SApple OSS Distributions
275*5e3eaea3SApple OSS Distributions sofreelastref(mp_so, 0);
276*5e3eaea3SApple OSS Distributions if (mp_so->so_rcv.sb_cc > 0 || mp_so->so_snd.sb_cc > 0) {
277*5e3eaea3SApple OSS Distributions /*
278*5e3eaea3SApple OSS Distributions * selthreadclear() already called
279*5e3eaea3SApple OSS Distributions * during sofreelastref() above.
280*5e3eaea3SApple OSS Distributions */
281*5e3eaea3SApple OSS Distributions sbrelease(&mp_so->so_rcv);
282*5e3eaea3SApple OSS Distributions sbrelease(&mp_so->so_snd);
283*5e3eaea3SApple OSS Distributions }
284*5e3eaea3SApple OSS Distributions
285*5e3eaea3SApple OSS Distributions lck_mtx_destroy(&mpp->mpp_lock, mppi->mppi_lock_grp);
286*5e3eaea3SApple OSS Distributions
287*5e3eaea3SApple OSS Distributions VERIFY(mpp->mpp_socket != NULL);
288*5e3eaea3SApple OSS Distributions VERIFY(mpp->mpp_socket->so_usecount == 0);
289*5e3eaea3SApple OSS Distributions mpp->mpp_socket->so_pcb = NULL;
290*5e3eaea3SApple OSS Distributions mpp->mpp_socket = NULL;
291*5e3eaea3SApple OSS Distributions mppi->mppi_free(mpp);
292*5e3eaea3SApple OSS Distributions }
293*5e3eaea3SApple OSS Distributions
294*5e3eaea3SApple OSS Distributions static int
mp_getaddr_v4(struct socket * mp_so,struct sockaddr ** nam,boolean_t peer)295*5e3eaea3SApple OSS Distributions mp_getaddr_v4(struct socket *mp_so, struct sockaddr **nam, boolean_t peer)
296*5e3eaea3SApple OSS Distributions {
297*5e3eaea3SApple OSS Distributions struct mptses *mpte = mpsotompte(mp_so);
298*5e3eaea3SApple OSS Distributions struct sockaddr_in *sin;
299*5e3eaea3SApple OSS Distributions
300*5e3eaea3SApple OSS Distributions /*
301*5e3eaea3SApple OSS Distributions * Do the malloc first in case it blocks.
302*5e3eaea3SApple OSS Distributions */
303*5e3eaea3SApple OSS Distributions sin = (struct sockaddr_in *)alloc_sockaddr(sizeof(*sin),
304*5e3eaea3SApple OSS Distributions Z_WAITOK | Z_NOFAIL);
305*5e3eaea3SApple OSS Distributions
306*5e3eaea3SApple OSS Distributions sin->sin_family = AF_INET;
307*5e3eaea3SApple OSS Distributions
308*5e3eaea3SApple OSS Distributions if (!peer) {
309*5e3eaea3SApple OSS Distributions sin->sin_port = mpte->__mpte_src_v4.sin_port;
310*5e3eaea3SApple OSS Distributions sin->sin_addr = mpte->__mpte_src_v4.sin_addr;
311*5e3eaea3SApple OSS Distributions } else {
312*5e3eaea3SApple OSS Distributions sin->sin_port = mpte->__mpte_dst_v4.sin_port;
313*5e3eaea3SApple OSS Distributions sin->sin_addr = mpte->__mpte_dst_v4.sin_addr;
314*5e3eaea3SApple OSS Distributions }
315*5e3eaea3SApple OSS Distributions
316*5e3eaea3SApple OSS Distributions *nam = (struct sockaddr *)sin;
317*5e3eaea3SApple OSS Distributions return 0;
318*5e3eaea3SApple OSS Distributions }
319*5e3eaea3SApple OSS Distributions
320*5e3eaea3SApple OSS Distributions static int
mp_getaddr_v6(struct socket * mp_so,struct sockaddr ** nam,boolean_t peer)321*5e3eaea3SApple OSS Distributions mp_getaddr_v6(struct socket *mp_so, struct sockaddr **nam, boolean_t peer)
322*5e3eaea3SApple OSS Distributions {
323*5e3eaea3SApple OSS Distributions struct mptses *mpte = mpsotompte(mp_so);
324*5e3eaea3SApple OSS Distributions struct in6_addr addr;
325*5e3eaea3SApple OSS Distributions in_port_t port;
326*5e3eaea3SApple OSS Distributions uint32_t ifscope;
327*5e3eaea3SApple OSS Distributions
328*5e3eaea3SApple OSS Distributions if (!peer) {
329*5e3eaea3SApple OSS Distributions port = mpte->__mpte_src_v6.sin6_port;
330*5e3eaea3SApple OSS Distributions addr = mpte->__mpte_src_v6.sin6_addr;
331*5e3eaea3SApple OSS Distributions ifscope = mpte->__mpte_src_v6.sin6_scope_id;
332*5e3eaea3SApple OSS Distributions } else {
333*5e3eaea3SApple OSS Distributions port = mpte->__mpte_dst_v6.sin6_port;
334*5e3eaea3SApple OSS Distributions addr = mpte->__mpte_dst_v6.sin6_addr;
335*5e3eaea3SApple OSS Distributions ifscope = mpte->__mpte_dst_v6.sin6_scope_id;
336*5e3eaea3SApple OSS Distributions }
337*5e3eaea3SApple OSS Distributions
338*5e3eaea3SApple OSS Distributions *nam = in6_sockaddr(port, &addr, ifscope);
339*5e3eaea3SApple OSS Distributions if (*nam == NULL) {
340*5e3eaea3SApple OSS Distributions return ENOBUFS;
341*5e3eaea3SApple OSS Distributions }
342*5e3eaea3SApple OSS Distributions
343*5e3eaea3SApple OSS Distributions return 0;
344*5e3eaea3SApple OSS Distributions }
345*5e3eaea3SApple OSS Distributions
346*5e3eaea3SApple OSS Distributions int
mp_getsockaddr(struct socket * mp_so,struct sockaddr ** nam)347*5e3eaea3SApple OSS Distributions mp_getsockaddr(struct socket *mp_so, struct sockaddr **nam)
348*5e3eaea3SApple OSS Distributions {
349*5e3eaea3SApple OSS Distributions struct mptses *mpte = mpsotompte(mp_so);
350*5e3eaea3SApple OSS Distributions
351*5e3eaea3SApple OSS Distributions if (mpte->mpte_src.sa_family == AF_INET || mpte->mpte_src.sa_family == 0) {
352*5e3eaea3SApple OSS Distributions return mp_getaddr_v4(mp_so, nam, false);
353*5e3eaea3SApple OSS Distributions } else if (mpte->mpte_src.sa_family == AF_INET6) {
354*5e3eaea3SApple OSS Distributions return mp_getaddr_v6(mp_so, nam, false);
355*5e3eaea3SApple OSS Distributions } else {
356*5e3eaea3SApple OSS Distributions return EINVAL;
357*5e3eaea3SApple OSS Distributions }
358*5e3eaea3SApple OSS Distributions }
359*5e3eaea3SApple OSS Distributions
360*5e3eaea3SApple OSS Distributions int
mp_getpeeraddr(struct socket * mp_so,struct sockaddr ** nam)361*5e3eaea3SApple OSS Distributions mp_getpeeraddr(struct socket *mp_so, struct sockaddr **nam)
362*5e3eaea3SApple OSS Distributions {
363*5e3eaea3SApple OSS Distributions struct mptses *mpte = mpsotompte(mp_so);
364*5e3eaea3SApple OSS Distributions
365*5e3eaea3SApple OSS Distributions if (mpte->mpte_src.sa_family == AF_INET || mpte->mpte_src.sa_family == 0) {
366*5e3eaea3SApple OSS Distributions return mp_getaddr_v4(mp_so, nam, true);
367*5e3eaea3SApple OSS Distributions } else if (mpte->mpte_src.sa_family == AF_INET6) {
368*5e3eaea3SApple OSS Distributions return mp_getaddr_v6(mp_so, nam, true);
369*5e3eaea3SApple OSS Distributions } else {
370*5e3eaea3SApple OSS Distributions return EINVAL;
371*5e3eaea3SApple OSS Distributions }
372*5e3eaea3SApple OSS Distributions }
373