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