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