1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifdef __FreeBSD__
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 358083 2020-02-18 21:25:17Z tuexen $");
38 #endif
39 
40 #include <netinet/sctp_os.h>
41 #ifdef INET6
42 #ifdef __FreeBSD__
43 #include <sys/proc.h>
44 #endif
45 #include <netinet/sctp_pcb.h>
46 #include <netinet/sctp_header.h>
47 #include <netinet/sctp_var.h>
48 #include <netinet6/sctp6_var.h>
49 #include <netinet/sctp_sysctl.h>
50 #include <netinet/sctp_output.h>
51 #include <netinet/sctp_uio.h>
52 #include <netinet/sctp_asconf.h>
53 #include <netinet/sctputil.h>
54 #include <netinet/sctp_indata.h>
55 #include <netinet/sctp_timer.h>
56 #include <netinet/sctp_auth.h>
57 #include <netinet/sctp_input.h>
58 #include <netinet/sctp_output.h>
59 #include <netinet/sctp_bsd_addr.h>
60 #include <netinet/sctp_crc32.h>
61 #if !defined(__Userspace_os_Windows)
62 #include <netinet/icmp6.h>
63 #include <netinet/udp.h>
64 #endif
65 #if defined(__APPLE__)
66 #define APPLE_FILE_NO 9
67 #endif
68 #if defined(__Panda__) || defined(__Userspace__)
69 int ip6_v6only=0;
70 #endif
71 #if defined(__Userspace__)
72 #ifdef INET
73 void
in6_sin6_2_sin(struct sockaddr_in * sin,struct sockaddr_in6 * sin6)74 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
75 {
76 #if defined(__Userspace_os_Windows)
77 	uint32_t temp;
78 #endif
79 	memset(sin, 0, sizeof(*sin));
80 #ifdef HAVE_SIN_LEN
81 	sin->sin_len = sizeof(struct sockaddr_in);
82 #endif
83 	sin->sin_family = AF_INET;
84 	sin->sin_port = sin6->sin6_port;
85 #if defined(__Userspace_os_Windows)
86 	temp = sin6->sin6_addr.s6_addr16[7];
87 	temp = temp << 16;
88 	temp = temp | sin6->sin6_addr.s6_addr16[6];
89 	sin->sin_addr.s_addr = temp;
90 #else
91 	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
92 #endif
93 }
94 
95 void
in6_sin6_2_sin_in_sock(struct sockaddr * nam)96 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
97 {
98 	struct sockaddr_in *sin_p;
99 	struct sockaddr_in6 sin6;
100 
101 	/* save original sockaddr_in6 addr and convert it to sockaddr_in  */
102 	sin6 = *(struct sockaddr_in6 *)nam;
103 	sin_p = (struct sockaddr_in *)nam;
104 	in6_sin6_2_sin(sin_p, &sin6);
105 }
106 
107 void
in6_sin_2_v4mapsin6(const struct sockaddr_in * sin,struct sockaddr_in6 * sin6)108 in6_sin_2_v4mapsin6(const struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
109 {
110 	memset(sin6, 0, sizeof(struct sockaddr_in6));
111  	sin6->sin6_family = AF_INET6;
112 #ifdef HAVE_SIN6_LEN
113 	sin6->sin6_len = sizeof(struct sockaddr_in6);
114 #endif
115 	sin6->sin6_port = sin->sin_port;
116 #if defined(__Userspace_os_Windows)
117 	((uint32_t *)&sin6->sin6_addr)[0] = 0;
118 	((uint32_t *)&sin6->sin6_addr)[1] = 0;
119 	((uint32_t *)&sin6->sin6_addr)[2] = htonl(0xffff);
120 	((uint32_t *)&sin6->sin6_addr)[3] = sin->sin_addr.s_addr;
121 #else
122  	sin6->sin6_addr.s6_addr32[0] = 0;
123 	sin6->sin6_addr.s6_addr32[1] = 0;
124  	sin6->sin6_addr.s6_addr32[2] = htonl(0xffff);
125 	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
126 #endif
127 }
128 #endif
129 #endif
130 
131 #if !defined(__Userspace__)
132 int
133 #if defined(__APPLE__) || defined(__FreeBSD__)
sctp6_input_with_port(struct mbuf ** i_pak,int * offp,uint16_t port)134 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
135 #elif defined( __Panda__)
136 sctp6_input(pakhandle_type *i_pak)
137 #else
138 sctp6_input(struct mbuf **i_pak, int *offp, int proto)
139 #endif
140 {
141 	struct mbuf *m;
142 	int iphlen;
143 	uint32_t vrf_id;
144 	uint8_t ecn_bits;
145 	struct sockaddr_in6 src, dst;
146 	struct ip6_hdr *ip6;
147 	struct sctphdr *sh;
148 	struct sctp_chunkhdr *ch;
149 	int length, offset;
150 	uint8_t compute_crc;
151 #if defined(__FreeBSD__)
152 	uint32_t mflowid;
153 	uint8_t mflowtype;
154 	uint16_t fibnum;
155 #endif
156 #if !(defined(__APPLE__) || defined (__FreeBSD__))
157 	uint16_t port = 0;
158 #endif
159 
160 #if defined(__Panda__)
161 	/* This is Evil, but its the only way to make panda work right. */
162 	iphlen = sizeof(struct ip6_hdr);
163 #else
164 	iphlen = *offp;
165 #endif
166 	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
167 		SCTP_RELEASE_PKT(*i_pak);
168 		return (IPPROTO_DONE);
169 	}
170 	m = SCTP_HEADER_TO_CHAIN(*i_pak);
171 #ifdef __Panda__
172 	SCTP_DETACH_HEADER_FROM_CHAIN(*i_pak);
173 	(void)SCTP_RELEASE_HEADER(*i_pak);
174 #endif
175 #ifdef SCTP_MBUF_LOGGING
176 	/* Log in any input mbufs */
177 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
178 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
179 	}
180 #endif
181 #ifdef SCTP_PACKET_LOGGING
182 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
183 		sctp_packet_log(m);
184 	}
185 #endif
186 #if defined(__FreeBSD__)
187 #if __FreeBSD_version > 1000049
188 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
189 	        "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
190 	        m->m_pkthdr.len,
191 	        if_name(m->m_pkthdr.rcvif),
192 	        (int)m->m_pkthdr.csum_flags, CSUM_BITS);
193 #elif __FreeBSD_version >= 800000
194 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
195 	        "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
196 	        m->m_pkthdr.len,
197 	        if_name(m->m_pkthdr.rcvif),
198 	        m->m_pkthdr.csum_flags);
199 #else
200 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
201 	        "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
202 	        m->m_pkthdr.len,
203 	        m->m_pkthdr.rcvif->if_xname,
204 	        m->m_pkthdr.csum_flags);
205 #endif
206 #endif
207 #if defined(__APPLE__)
208 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
209 	        "sctp6_input(): Packet of length %d received on %s%d with csum_flags 0x%x.\n",
210 	        m->m_pkthdr.len,
211 	        m->m_pkthdr.rcvif->if_name,
212 	        m->m_pkthdr.rcvif->if_unit,
213 	        m->m_pkthdr.csum_flags);
214 #endif
215 #if defined(__Windows__)
216 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
217 	        "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
218 	        m->m_pkthdr.len,
219 	        m->m_pkthdr.rcvif->if_xname,
220 	        m->m_pkthdr.csum_flags);
221 #endif
222 #if defined(__FreeBSD__)
223 	mflowid = m->m_pkthdr.flowid;
224 	mflowtype = M_HASHTYPE_GET(m);
225 	fibnum = M_GETFIB(m);
226 #endif
227 	SCTP_STAT_INCR(sctps_recvpackets);
228 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
229 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
230 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
231 	if (m->m_len < offset) {
232 		m = m_pullup(m, offset);
233 		if (m == NULL) {
234 			SCTP_STAT_INCR(sctps_hdrops);
235 			return (IPPROTO_DONE);
236 		}
237 	}
238 	ip6 = mtod(m, struct ip6_hdr *);
239 	sh = (struct sctphdr *)(mtod(m, caddr_t) + iphlen);
240 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
241 	offset -= sizeof(struct sctp_chunkhdr);
242 	memset(&src, 0, sizeof(struct sockaddr_in6));
243 	src.sin6_family = AF_INET6;
244 #ifdef HAVE_SIN6_LEN
245 	src.sin6_len = sizeof(struct sockaddr_in6);
246 #endif
247 	src.sin6_port = sh->src_port;
248 	src.sin6_addr = ip6->ip6_src;
249 #if defined(__FreeBSD__)
250 #if defined(__APPLE__)
251 	/* XXX: This code should also be used on Apple */
252 #endif
253 	if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
254 		goto out;
255 	}
256 #endif
257 	memset(&dst, 0, sizeof(struct sockaddr_in6));
258 	dst.sin6_family = AF_INET6;
259 #ifdef HAVE_SIN6_LEN
260 	dst.sin6_len = sizeof(struct sockaddr_in6);
261 #endif
262 	dst.sin6_port = sh->dest_port;
263 	dst.sin6_addr = ip6->ip6_dst;
264 #if defined(__FreeBSD__)
265 #if defined(__APPLE__)
266 	/* XXX: This code should also be used on Apple */
267 #endif
268 	if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
269 		goto out;
270 	}
271 #endif
272 #if defined(__APPLE__)
273 #if defined(NFAITH) && 0 < NFAITH
274 	if (faithprefix(&dst.sin6_addr)) {
275 		goto out;
276 	}
277 #endif
278 #endif
279 	length = ntohs(ip6->ip6_plen) + iphlen;
280 	/* Validate mbuf chain length with IP payload length. */
281 	if (SCTP_HEADER_LEN(m) != length) {
282 		SCTPDBG(SCTP_DEBUG_INPUT1,
283 		        "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
284 		SCTP_STAT_INCR(sctps_hdrops);
285 		goto out;
286 	}
287 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
288 		goto out;
289 	}
290 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
291 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
292 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
293 		SCTP_STAT_INCR(sctps_recvhwcrc);
294 		compute_crc = 0;
295 	} else {
296 #else
297 	if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
298 	    (IN6_ARE_ADDR_EQUAL(&src.sin6_addr, &dst.sin6_addr))) {
299 		SCTP_STAT_INCR(sctps_recvhwcrc);
300 		compute_crc = 0;
301 	} else {
302 #endif
303 		SCTP_STAT_INCR(sctps_recvswcrc);
304 		compute_crc = 1;
305 	}
306 	sctp_common_input_processing(&m, iphlen, offset, length,
307 	                             (struct sockaddr *)&src,
308 	                             (struct sockaddr *)&dst,
309 	                             sh, ch,
310 	                             compute_crc,
311 	                             ecn_bits,
312 #if defined(__FreeBSD__)
313 	                             mflowtype, mflowid, fibnum,
314 #endif
315 	                             vrf_id, port);
316  out:
317 	if (m) {
318 		sctp_m_freem(m);
319 	}
320 	return (IPPROTO_DONE);
321 }
322 
323 #if defined(__APPLE__)
324 int
325 sctp6_input(struct mbuf **i_pak, int *offp)
326 {
327 	return (sctp6_input_with_port(i_pak, offp, 0));
328 }
329 #endif
330 
331 #if defined(__FreeBSD__)
332 int
333 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
334 {
335 	return (sctp6_input_with_port(i_pak, offp, 0));
336 }
337 #endif
338 
339 void
340 sctp6_notify(struct sctp_inpcb *inp,
341              struct sctp_tcb *stcb,
342              struct sctp_nets *net,
343              uint8_t icmp6_type,
344              uint8_t icmp6_code,
345              uint32_t next_mtu)
346 {
347 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
348 	struct socket *so;
349 #endif
350 	int timer_stopped;
351 
352 	switch (icmp6_type) {
353 	case ICMP6_DST_UNREACH:
354 		if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
355 		    (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
356 		    (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
357 		    (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
358 			/* Mark the net unreachable. */
359 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
360 				/* Ok that destination is not reachable */
361 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
362 				net->dest_state &= ~SCTP_ADDR_PF;
363 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
364 				                stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
365 			}
366 		}
367 		SCTP_TCB_UNLOCK(stcb);
368 		break;
369 	case ICMP6_PARAM_PROB:
370 		/* Treat it like an ABORT. */
371 		if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
372 			sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
373 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
374 			so = SCTP_INP_SO(inp);
375 			atomic_add_int(&stcb->asoc.refcnt, 1);
376 			SCTP_TCB_UNLOCK(stcb);
377 			SCTP_SOCKET_LOCK(so, 1);
378 			SCTP_TCB_LOCK(stcb);
379 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
380 #endif
381 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
382 					      SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
383 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
384 			SCTP_SOCKET_UNLOCK(so, 1);
385 #endif
386 		} else {
387 			SCTP_TCB_UNLOCK(stcb);
388 		}
389 		break;
390 	case ICMP6_PACKET_TOO_BIG:
391 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
392 			SCTP_TCB_UNLOCK(stcb);
393 			break;
394 		}
395 		if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
396 			timer_stopped = 1;
397 			sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
398 			                SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
399 		} else {
400 			timer_stopped = 0;
401 		}
402 		/* Update the path MTU. */
403 		if (net->port) {
404 			next_mtu -= sizeof(struct udphdr);
405 		}
406 		if (net->mtu > next_mtu) {
407 			net->mtu = next_mtu;
408 #if defined(__FreeBSD__)
409 			if (net->port) {
410 				sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
411 			} else {
412 				sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
413 			}
414 #endif
415 		}
416 		/* Update the association MTU */
417 		if (stcb->asoc.smallest_mtu > next_mtu) {
418 			sctp_pathmtu_adjustment(stcb, next_mtu);
419 		}
420 		/* Finally, start the PMTU timer if it was running before. */
421 		if (timer_stopped) {
422 			sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
423 		}
424 		SCTP_TCB_UNLOCK(stcb);
425 		break;
426 	default:
427 		SCTP_TCB_UNLOCK(stcb);
428 		break;
429 	}
430 }
431 
432 void
433 #if defined(__APPLE__) && !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) && !defined(APPLE_ELCAPITAN)
434 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d, struct ifnet *ifp SCTP_UNUSED)
435 #else
436 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
437 #endif
438 {
439 	struct ip6ctlparam *ip6cp;
440 	struct sctp_inpcb *inp;
441 	struct sctp_tcb *stcb;
442 	struct sctp_nets *net;
443 	struct sctphdr sh;
444 	struct sockaddr_in6 src, dst;
445 
446 #ifdef HAVE_SA_LEN
447 	if (pktdst->sa_family != AF_INET6 ||
448 	    pktdst->sa_len != sizeof(struct sockaddr_in6)) {
449 #else
450 	if (pktdst->sa_family != AF_INET6) {
451 #endif
452 		return;
453 	}
454 
455 	if ((unsigned)cmd >= PRC_NCMDS) {
456 		return;
457 	}
458 	if (PRC_IS_REDIRECT(cmd)) {
459 		d = NULL;
460 	} else if (inet6ctlerrmap[cmd] == 0) {
461 		return;
462 	}
463 	/* If the parameter is from icmp6, decode it. */
464 	if (d != NULL) {
465 		ip6cp = (struct ip6ctlparam *)d;
466 	} else {
467 		ip6cp = (struct ip6ctlparam *)NULL;
468 	}
469 
470 	if (ip6cp != NULL) {
471 		/*
472 		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
473 		 * valid.
474 		 */
475 		if (ip6cp->ip6c_m == NULL) {
476 			return;
477 		}
478 
479 		/* Check if we can safely examine the ports and the
480 		 * verification tag of the SCTP common header.
481 		 */
482 		if (ip6cp->ip6c_m->m_pkthdr.len <
483 		    (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
484 			return;
485 		}
486 
487 		/* Copy out the port numbers and the verification tag. */
488 		memset(&sh, 0, sizeof(sh));
489 		m_copydata(ip6cp->ip6c_m,
490 		           ip6cp->ip6c_off,
491 		           sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
492 		           (caddr_t)&sh);
493 		memset(&src, 0, sizeof(struct sockaddr_in6));
494 		src.sin6_family = AF_INET6;
495 #ifdef HAVE_SIN6_LEN
496 		src.sin6_len = sizeof(struct sockaddr_in6);
497 #endif
498 		src.sin6_port = sh.src_port;
499 		src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
500 #if defined(__FreeBSD__)
501 		if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
502 			return;
503 		}
504 #endif
505 		memset(&dst, 0, sizeof(struct sockaddr_in6));
506 		dst.sin6_family = AF_INET6;
507 #ifdef HAVE_SIN6_LEN
508 		dst.sin6_len = sizeof(struct sockaddr_in6);
509 #endif
510 		dst.sin6_port = sh.dest_port;
511 		dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
512 #if defined(__FreeBSD__)
513 		if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
514 			return;
515 		}
516 #endif
517 		inp = NULL;
518 		net = NULL;
519 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
520 		                                    (struct sockaddr *)&src,
521 		                                    &inp, &net, 1, SCTP_DEFAULT_VRFID);
522 		if ((stcb != NULL) &&
523 		    (net != NULL) &&
524 		    (inp != NULL)) {
525 			/* Check the verification tag */
526 			if (ntohl(sh.v_tag) != 0) {
527 				/*
528 				 * This must be the verification tag used for
529 				 * sending out packets. We don't consider
530 				 * packets reflecting the verification tag.
531 				 */
532 				if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
533 					SCTP_TCB_UNLOCK(stcb);
534 					return;
535 				}
536 			} else {
537 #if defined(__FreeBSD__)
538 				if (ip6cp->ip6c_m->m_pkthdr.len >=
539 				    ip6cp->ip6c_off + sizeof(struct sctphdr) +
540 				                      sizeof(struct sctp_chunkhdr) +
541 				                      offsetof(struct sctp_init, a_rwnd)) {
542 					/*
543 					 * In this case we can check if we
544 					 * got an INIT chunk and if the
545 					 * initiate tag matches.
546 					 */
547 					uint32_t initiate_tag;
548 					uint8_t chunk_type;
549 
550 					m_copydata(ip6cp->ip6c_m,
551 					           ip6cp->ip6c_off +
552 					           sizeof(struct sctphdr),
553 					           sizeof(uint8_t),
554 					           (caddr_t)&chunk_type);
555 					m_copydata(ip6cp->ip6c_m,
556 					           ip6cp->ip6c_off +
557 					           sizeof(struct sctphdr) +
558 					           sizeof(struct sctp_chunkhdr),
559 					           sizeof(uint32_t),
560 					           (caddr_t)&initiate_tag);
561 					if ((chunk_type != SCTP_INITIATION) ||
562 					    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
563 						SCTP_TCB_UNLOCK(stcb);
564 						return;
565 					}
566 				} else {
567 					SCTP_TCB_UNLOCK(stcb);
568 					return;
569 				}
570 #else
571 				SCTP_TCB_UNLOCK(stcb);
572 				return;
573 #endif
574 			}
575 			sctp6_notify(inp, stcb, net,
576 			             ip6cp->ip6c_icmp6->icmp6_type,
577 			             ip6cp->ip6c_icmp6->icmp6_code,
578 			             ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
579 #if defined(__Userspace__)
580 			if (!(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) &&
581 			    (stcb->sctp_socket != NULL) {
582 				struct socket *upcall_socket;
583 
584 				upcall_socket = stcb->sctp_socket;
585 				SOCK_LOCK(upcall_socket);
586 				soref(upcall_socket);
587 				SOCK_UNLOCK(upcall_socket);
588 				if ((upcall_socket->so_upcall != NULL) &&
589 				    (upcall_socket->so_error != 0)) {
590 					(*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
591 				}
592 				ACCEPT_LOCK();
593 				SOCK_LOCK(upcall_socket);
594 				sorele(upcall_socket);
595 			}
596 #endif
597 		} else {
598 #if defined(__FreeBSD__) && __FreeBSD_version < 500000
599 			if (PRC_IS_REDIRECT(cmd) && (inp != NULL)) {
600 				in6_rtchange(inp, inet6ctlerrmap[cmd]);
601 			}
602 #endif
603 			if ((stcb == NULL) && (inp != NULL)) {
604 				/* reduce inp's ref-count */
605 				SCTP_INP_WLOCK(inp);
606 				SCTP_INP_DECR_REF(inp);
607 				SCTP_INP_WUNLOCK(inp);
608 			}
609 			if (stcb) {
610 				SCTP_TCB_UNLOCK(stcb);
611 			}
612 		}
613 	}
614 }
615 #endif
616 
617 /*
618  * this routine can probably be collasped into the one in sctp_userreq.c
619  * since they do the same thing and now we lookup with a sockaddr
620  */
621 #ifdef __FreeBSD__
622 static int
623 sctp6_getcred(SYSCTL_HANDLER_ARGS)
624 {
625 	struct xucred xuc;
626 	struct sockaddr_in6 addrs[2];
627 	struct sctp_inpcb *inp;
628 	struct sctp_nets *net;
629 	struct sctp_tcb *stcb;
630 	int error;
631 	uint32_t vrf_id;
632 
633 #if defined(__FreeBSD__) || defined(__APPLE__)
634 	vrf_id = SCTP_DEFAULT_VRFID;
635 #else
636 	vrf_id = panda_get_vrf_from_call(); /* from connectx call? */
637 #endif
638 
639 #if defined(__FreeBSD__) && __FreeBSD_version > 602000
640 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
641 #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000
642 	error = suser(req->td);
643 #else
644 	error = suser(req->p);
645 #endif
646 	if (error)
647 		return (error);
648 
649 	if (req->newlen != sizeof(addrs)) {
650 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
651 		return (EINVAL);
652 	}
653 	if (req->oldlen != sizeof(struct ucred)) {
654 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
655 		return (EINVAL);
656 	}
657 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
658 	if (error)
659 		return (error);
660 
661 	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
662 	    sin6tosa(&addrs[0]),
663 	    &inp, &net, 1, vrf_id);
664 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
665 		if ((inp != NULL) && (stcb == NULL)) {
666 			/* reduce ref-count */
667 			SCTP_INP_WLOCK(inp);
668 			SCTP_INP_DECR_REF(inp);
669 			goto cred_can_cont;
670 		}
671 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
672 		error = ENOENT;
673 		goto out;
674 	}
675 	SCTP_TCB_UNLOCK(stcb);
676 	/* We use the write lock here, only
677 	 * since in the error leg we need it.
678 	 * If we used RLOCK, then we would have
679 	 * to wlock/decr/unlock/rlock. Which
680 	 * in theory could create a hole. Better
681 	 * to use higher wlock.
682 	 */
683 	SCTP_INP_WLOCK(inp);
684  cred_can_cont:
685 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
686 	if (error) {
687 		SCTP_INP_WUNLOCK(inp);
688 		goto out;
689 	}
690 	cru2x(inp->sctp_socket->so_cred, &xuc);
691 	SCTP_INP_WUNLOCK(inp);
692 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
693 out:
694 	return (error);
695 }
696 
697 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
698     0, 0,
699     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
700 
701 #endif
702 
703 /* This is the same as the sctp_abort() could be made common */
704 #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__)
705 static void
706 #elif defined(__Panda__) || defined(__Userspace__)
707 int
708 #else
709 static int
710 #endif
711 sctp6_abort(struct socket *so)
712 {
713 #if defined(__FreeBSD__)
714 	struct epoch_tracker et;
715 #endif
716 	struct sctp_inpcb *inp;
717 	uint32_t flags;
718 
719 	inp = (struct sctp_inpcb *)so->so_pcb;
720 	if (inp == NULL) {
721 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
722 #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__)
723 		return;
724 #else
725 		return (EINVAL);
726 #endif
727 	}
728 #if defined(__FreeBSD__)
729 	NET_EPOCH_ENTER(et);
730 #endif
731  sctp_must_try_again:
732 	flags = inp->sctp_flags;
733 #ifdef SCTP_LOG_CLOSING
734 	sctp_log_closing(inp, NULL, 17);
735 #endif
736 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
737 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
738 #ifdef SCTP_LOG_CLOSING
739 		sctp_log_closing(inp, NULL, 16);
740 #endif
741 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
742 				SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
743 		SOCK_LOCK(so);
744 		SCTP_SB_CLEAR(so->so_snd);
745 		/* same for the rcv ones, they are only
746 		 * here for the accounting/select.
747 		 */
748 		SCTP_SB_CLEAR(so->so_rcv);
749 #if defined(__APPLE__)
750 		so->so_usecount--;
751 #else
752 		/* Now null out the reference, we are completely detached. */
753 		so->so_pcb = NULL;
754 #endif
755 		SOCK_UNLOCK(so);
756 	} else {
757 		flags = inp->sctp_flags;
758 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
759 			goto sctp_must_try_again;
760 		}
761 	}
762 #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__)
763 	NET_EPOCH_EXIT(et);
764 	return;
765 #else
766 	return (0);
767 #endif
768 }
769 
770 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
771 static int
772 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
773 #elif defined(__Panda__) || defined(__Userspace__)
774 int
775 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, uint32_t vrf_id)
776 #elif defined(__Windows__)
777 static int
778 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, PKTHREAD p SCTP_UNUSED)
779 #else
780 static int
781 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct proc *p SCTP_UNUSED)
782 #endif
783 {
784 	int error;
785 	struct sctp_inpcb *inp;
786 #if !defined(__Panda__) && !defined(__Userspace__)
787 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
788 #endif
789 
790 	inp = (struct sctp_inpcb *)so->so_pcb;
791 	if (inp != NULL) {
792 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
793 		return (EINVAL);
794 	}
795 
796 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
797 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
798 		if (error)
799 			return (error);
800 	}
801 	error = sctp_inpcb_alloc(so, vrf_id);
802 	if (error)
803 		return (error);
804 	inp = (struct sctp_inpcb *)so->so_pcb;
805 	SCTP_INP_WLOCK(inp);
806 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
807 
808 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
809 	inp->ip_inp.inp.inp_vflag |= INP_IPV6;
810 #else
811 	inp->inp_vflag |= INP_IPV6;
812 #endif
813 #if !defined(__Panda__)
814 	inp->ip_inp.inp.in6p_hops = -1;	/* use kernel default */
815 	inp->ip_inp.inp.in6p_cksum = -1;	/* just to be sure */
816 #endif
817 #ifdef INET
818 	/*
819 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
820 	 * socket as well, because the socket may be bound to an IPv6
821 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
822 	 */
823 	inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
824 #endif
825 	SCTP_INP_WUNLOCK(inp);
826 	return (0);
827 }
828 
829 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
830 static int
831 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
832 {
833 #elif defined(__FreeBSD__) || defined(__APPLE__)
834 static int
835 sctp6_bind(struct socket *so, struct sockaddr *addr, struct proc *p)
836 {
837 #elif defined(__Panda__) || defined(__Userspace__)
838 int
839 sctp6_bind(struct socket *so, struct sockaddr *addr, void * p)
840 {
841 #elif defined(__Windows__)
842 static int
843 sctp6_bind(struct socket *so, struct sockaddr *addr, PKTHREAD p)
844 {
845 #else
846 static int
847 sctp6_bind(struct socket *so, struct mbuf *nam, struct proc *p)
848 {
849 	struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *): NULL;
850 
851 #endif
852 	struct sctp_inpcb *inp;
853 	int error;
854 	u_char vflagsav;
855 
856 	inp = (struct sctp_inpcb *)so->so_pcb;
857 	if (inp == NULL) {
858 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
859 		return (EINVAL);
860 	}
861 
862 #if !defined(__Windows__)
863 	if (addr) {
864 		switch (addr->sa_family) {
865 #ifdef INET
866 		case AF_INET:
867 #ifdef HAVE_SA_LEN
868 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
869 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
870 				return (EINVAL);
871 			}
872 #endif
873 			break;
874 #endif
875 #ifdef INET6
876 		case AF_INET6:
877 #ifdef HAVE_SA_LEN
878 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
879 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
880 				return (EINVAL);
881 			}
882 #endif
883 			break;
884 #endif
885 		default:
886 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
887 			return (EINVAL);
888 		}
889 	}
890 #endif
891 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
892 	vflagsav = inp->ip_inp.inp.inp_vflag;
893 	inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
894 	inp->ip_inp.inp.inp_vflag |= INP_IPV6;
895 #else
896 	vflagsav = inp->inp_vflag;
897 	inp->inp_vflag &= ~INP_IPV4;
898 	inp->inp_vflag |= INP_IPV6;
899 #endif
900 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
901 		switch (addr->sa_family) {
902 #ifdef INET
903 		case AF_INET:
904 			/* binding v4 addr to v6 socket, so reset flags */
905 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
906 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
907 			inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
908 #else
909 			inp->inp_vflag |= INP_IPV4;
910 			inp->inp_vflag &= ~INP_IPV6;
911 #endif
912 			break;
913 #endif
914 #ifdef INET6
915 		case AF_INET6:
916 		{
917 			struct sockaddr_in6 *sin6_p;
918 
919 			sin6_p = (struct sockaddr_in6 *)addr;
920 
921 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
922 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
923 				inp->ip_inp.inp.inp_vflag |= INP_IPV4;
924 #else
925 				inp->inp_vflag |= INP_IPV4;
926 #endif
927 			}
928 #ifdef INET
929 			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
930 				struct sockaddr_in sin;
931 
932 				in6_sin6_2_sin(&sin, sin6_p);
933 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
934 				inp->ip_inp.inp.inp_vflag |= INP_IPV4;
935 				inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
936 #else
937 				inp->inp_vflag |= INP_IPV4;
938 				inp->inp_vflag &= ~INP_IPV6;
939 #endif
940 				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
941 				goto out;
942 			}
943 #endif
944 			break;
945 		}
946 #endif
947 		default:
948 			break;
949 		}
950 	} else if (addr != NULL) {
951 		struct sockaddr_in6 *sin6_p;
952 
953 		/* IPV6_V6ONLY socket */
954 #ifdef INET
955 		if (addr->sa_family == AF_INET) {
956 			/* can't bind v4 addr to v6 only socket! */
957 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
958 			error = EINVAL;
959 			goto out;
960 		}
961 #endif
962 		sin6_p = (struct sockaddr_in6 *)addr;
963 
964 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
965 			/* can't bind v4-mapped addrs either! */
966 			/* NOTE: we don't support SIIT */
967 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
968 			error = EINVAL;
969 			goto out;
970 		}
971 	}
972 	error = sctp_inpcb_bind(so, addr, NULL, p);
973 out:
974 	if (error != 0)
975 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
976 		inp->ip_inp.inp.inp_vflag = vflagsav;
977 #else
978 		inp->inp_vflag = vflagsav;
979 #endif
980 	return (error);
981 }
982 
983 
984 #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__) || defined(__Userspace__)
985 #if !defined(__Userspace__)
986 static void
987 #else
988 void
989 #endif
990 sctp6_close(struct socket *so)
991 {
992 	sctp_close(so);
993 }
994 
995 /* This could be made common with sctp_detach() since they are identical */
996 #else
997 
998 #if !defined(__Panda__)
999 static
1000 #endif
1001 int
1002 sctp6_detach(struct socket *so)
1003 {
1004 #if defined(__Userspace__)
1005 	sctp_close(so);
1006 	return (0);
1007 #else
1008 	return (sctp_detach(so));
1009 #endif
1010 }
1011 
1012 #endif
1013 
1014 #if !defined(__Panda__) && !defined(__Userspace__)
1015 static
1016 #endif
1017 int
1018 sctp6_disconnect(struct socket *so)
1019 {
1020 	return (sctp_disconnect(so));
1021 }
1022 
1023 
1024 int
1025 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1026 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1027     struct mbuf *control, struct thread *p);
1028 
1029 #else
1030 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1031     struct mbuf *control, struct proc *p);
1032 
1033 #endif
1034 
1035 #if !defined(__Panda__) && !defined(__Windows__) && !defined(__Userspace__)
1036 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1037 static int
1038 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1039     struct mbuf *control, struct thread *p)
1040 {
1041 #elif defined(__FreeBSD__) || defined(__APPLE__)
1042 static int
1043 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1044     struct mbuf *control, struct proc *p)
1045 {
1046 #else
1047 static int
1048 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam,
1049     struct mbuf *control, struct proc *p)
1050 {
1051 	struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *): NULL;
1052 #endif
1053 	struct sctp_inpcb *inp;
1054 
1055 #ifdef INET
1056 	struct sockaddr_in6 *sin6;
1057 #endif /* INET */
1058 	/* No SPL needed since sctp_output does this */
1059 
1060 	inp = (struct sctp_inpcb *)so->so_pcb;
1061 	if (inp == NULL) {
1062 		if (control) {
1063 			SCTP_RELEASE_PKT(control);
1064 			control = NULL;
1065 		}
1066 		SCTP_RELEASE_PKT(m);
1067 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1068 		return (EINVAL);
1069 	}
1070 	/*
1071 	 * For the TCP model we may get a NULL addr, if we are a connected
1072 	 * socket thats ok.
1073 	 */
1074 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
1075 	    (addr == NULL)) {
1076 		goto connected_type;
1077 	}
1078 	if (addr == NULL) {
1079 		SCTP_RELEASE_PKT(m);
1080 		if (control) {
1081 			SCTP_RELEASE_PKT(control);
1082 			control = NULL;
1083 		}
1084 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
1085 		return (EDESTADDRREQ);
1086 	}
1087 #ifdef INET
1088 	sin6 = (struct sockaddr_in6 *)addr;
1089 	if (SCTP_IPV6_V6ONLY(inp)) {
1090 		/*
1091 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
1092 		 * v4 addr or v4-mapped addr
1093 		 */
1094 		if (addr->sa_family == AF_INET) {
1095 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1096 			return (EINVAL);
1097 		}
1098 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1099 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1100 			return (EINVAL);
1101 		}
1102 	}
1103 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1104 		struct sockaddr_in sin;
1105 
1106 		/* convert v4-mapped into v4 addr and send */
1107 		in6_sin6_2_sin(&sin, sin6);
1108 		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
1109 	}
1110 #endif				/* INET */
1111 connected_type:
1112 	/* now what about control */
1113 	if (control) {
1114 		if (inp->control) {
1115 			SCTP_PRINTF("huh? control set?\n");
1116 			SCTP_RELEASE_PKT(inp->control);
1117 			inp->control = NULL;
1118 		}
1119 		inp->control = control;
1120 	}
1121 	/* Place the data */
1122 	if (inp->pkt) {
1123 		SCTP_BUF_NEXT(inp->pkt_last) = m;
1124 		inp->pkt_last = m;
1125 	} else {
1126 		inp->pkt_last = inp->pkt = m;
1127 	}
1128 	if (
1129 #if defined(__FreeBSD__) || defined(__APPLE__)
1130 	/* FreeBSD and MacOSX uses a flag passed */
1131 	    ((flags & PRUS_MORETOCOME) == 0)
1132 #else
1133 	    1			/* Open BSD does not have any "more to come"
1134 				 * indication */
1135 #endif
1136 	    ) {
1137 		/*
1138 		 * note with the current version this code will only be used
1139 		 * by OpenBSD, NetBSD and FreeBSD have methods for
1140 		 * re-defining sosend() to use sctp_sosend().  One can
1141 		 * optionaly switch back to this code (by changing back the
1142 		 * defininitions but this is not advisable.
1143 		 */
1144 #if defined(__FreeBSD__)
1145 		struct epoch_tracker et;
1146 #endif
1147 		int ret;
1148 
1149 #if defined(__FreeBSD__)
1150 	NET_EPOCH_ENTER(et);
1151 #endif
1152 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
1153 #if defined(__FreeBSD__)
1154 	NET_EPOCH_EXIT(et);
1155 #endif
1156 		inp->pkt = NULL;
1157 		inp->control = NULL;
1158 		return (ret);
1159 	} else {
1160 		return (0);
1161 	}
1162 }
1163 #endif
1164 
1165 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1166 static int
1167 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
1168 {
1169 #elif defined(__FreeBSD__) || defined(__APPLE__)
1170 static int
1171 sctp6_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
1172 {
1173 #elif defined(__Panda__)
1174 int
1175 sctp6_connect(struct socket *so, struct sockaddr *addr, void *p)
1176 {
1177 #elif defined(__Windows__)
1178 static int
1179 sctp6_connect(struct socket *so, struct sockaddr *addr, PKTHREAD p)
1180 {
1181 #elif defined(__Userspace__)
1182 int
1183 sctp6_connect(struct socket *so, struct sockaddr *addr)
1184 {
1185 	void *p = NULL;
1186 #else
1187 static int
1188 sctp6_connect(struct socket *so, struct mbuf *nam, struct proc *p)
1189 {
1190 	struct sockaddr *addr = mtod(nam, struct sockaddr *);
1191 #endif
1192 #if defined(__FreeBSD__)
1193 	struct epoch_tracker et;
1194 #endif
1195 	uint32_t vrf_id;
1196 	int error = 0;
1197 	struct sctp_inpcb *inp;
1198 	struct sctp_tcb *stcb;
1199 #ifdef INET
1200 	struct sockaddr_in6 *sin6;
1201 	union sctp_sockstore store;
1202 #endif
1203 
1204 	inp = (struct sctp_inpcb *)so->so_pcb;
1205 	if (inp == NULL) {
1206 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1207 		return (ECONNRESET);	/* I made the same as TCP since we are
1208 					 * not setup? */
1209 	}
1210 	if (addr == NULL) {
1211 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1212 		return (EINVAL);
1213 	}
1214 #if !defined(__Windows__)
1215 	switch (addr->sa_family) {
1216 #ifdef INET
1217 	case AF_INET:
1218 #ifdef HAVE_SA_LEN
1219 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
1220 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1221 			return (EINVAL);
1222 		}
1223 #endif
1224 		break;
1225 #endif
1226 #ifdef INET6
1227 	case AF_INET6:
1228 #ifdef HAVE_SA_LEN
1229 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1230 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1231 			return (EINVAL);
1232 		}
1233 #endif
1234 		break;
1235 #endif
1236 	default:
1237 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1238 		return (EINVAL);
1239 	}
1240 #endif
1241 
1242 	vrf_id = inp->def_vrf_id;
1243 	SCTP_ASOC_CREATE_LOCK(inp);
1244 	SCTP_INP_RLOCK(inp);
1245 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1246 	    SCTP_PCB_FLAGS_UNBOUND) {
1247 		/* Bind a ephemeral port */
1248 		SCTP_INP_RUNLOCK(inp);
1249 		error = sctp6_bind(so, NULL, p);
1250 		if (error) {
1251 			SCTP_ASOC_CREATE_UNLOCK(inp);
1252 
1253 			return (error);
1254 		}
1255 		SCTP_INP_RLOCK(inp);
1256 	}
1257 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1258 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1259 		/* We are already connected AND the TCP model */
1260 		SCTP_INP_RUNLOCK(inp);
1261 		SCTP_ASOC_CREATE_UNLOCK(inp);
1262 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1263 		return (EADDRINUSE);
1264 	}
1265 #ifdef INET
1266 	sin6 = (struct sockaddr_in6 *)addr;
1267 	if (SCTP_IPV6_V6ONLY(inp)) {
1268 		/*
1269 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1270 		 * addr or v4-mapped addr
1271 		 */
1272 		if (addr->sa_family == AF_INET) {
1273 			SCTP_INP_RUNLOCK(inp);
1274 			SCTP_ASOC_CREATE_UNLOCK(inp);
1275 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1276 			return (EINVAL);
1277 		}
1278 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1279 			SCTP_INP_RUNLOCK(inp);
1280 			SCTP_ASOC_CREATE_UNLOCK(inp);
1281 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1282 			return (EINVAL);
1283 		}
1284 	}
1285 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1286 		/* convert v4-mapped into v4 addr */
1287 		in6_sin6_2_sin(&store.sin, sin6);
1288 		addr = &store.sa;
1289 	}
1290 #endif				/* INET */
1291 	/* Now do we connect? */
1292 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1293 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1294 		if (stcb) {
1295 			SCTP_TCB_LOCK(stcb);
1296 		}
1297 		SCTP_INP_RUNLOCK(inp);
1298 	} else {
1299 		SCTP_INP_RUNLOCK(inp);
1300 		SCTP_INP_WLOCK(inp);
1301 		SCTP_INP_INCR_REF(inp);
1302 		SCTP_INP_WUNLOCK(inp);
1303 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1304 		if (stcb == NULL) {
1305 			SCTP_INP_WLOCK(inp);
1306 			SCTP_INP_DECR_REF(inp);
1307 			SCTP_INP_WUNLOCK(inp);
1308 		}
1309 	}
1310 
1311 	if (stcb != NULL) {
1312 		/* Already have or am bring up an association */
1313 		SCTP_ASOC_CREATE_UNLOCK(inp);
1314 		SCTP_TCB_UNLOCK(stcb);
1315 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1316 		return (EALREADY);
1317 	}
1318 	/* We are GOOD to go */
1319 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
1320 	                       inp->sctp_ep.pre_open_stream_count,
1321 	                       inp->sctp_ep.port, p,
1322 	                       SCTP_INITIALIZE_AUTH_PARAMS);
1323 	SCTP_ASOC_CREATE_UNLOCK(inp);
1324 	if (stcb == NULL) {
1325 		/* Gak! no memory */
1326 		return (error);
1327 	}
1328 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1329 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1330 		/* Set the connected flag so we can queue data */
1331 		soisconnecting(so);
1332 	}
1333 	SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1334 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1335 #if defined(__FreeBSD__)
1336 	NET_EPOCH_ENTER(et);
1337 #endif
1338 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1339 	SCTP_TCB_UNLOCK(stcb);
1340 #if defined(__FreeBSD__)
1341 	NET_EPOCH_EXIT(et);
1342 #endif
1343 	return (error);
1344 }
1345 
1346 static int
1347 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1348 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1349 {
1350 	struct sockaddr_in6 *sin6;
1351 #elif defined(__Panda__)
1352 sctp6_getaddr(struct socket *so, struct sockaddr *addr)
1353 {
1354 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
1355 #else
1356 sctp6_getaddr(struct socket *so, struct mbuf *nam)
1357 {
1358 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1359 #endif
1360 	struct sctp_inpcb *inp;
1361 	uint32_t vrf_id;
1362 	struct sctp_ifa *sctp_ifa;
1363 
1364 #if defined(SCTP_KAME) && defined(SCTP_EMBEDDED_V6_SCOPE)
1365 	int error;
1366 #endif
1367 
1368 	/*
1369 	 * Do the malloc first in case it blocks.
1370 	 */
1371 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1372 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1373 	if (sin6 == NULL)
1374 		return (ENOMEM);
1375 #elif defined(__Panda__)
1376 	memset(sin6, 0, sizeof(*sin6));
1377 #else
1378 	SCTP_BUF_LEN(nam) = sizeof(*sin6);
1379 	memset(sin6, 0, sizeof(*sin6));
1380 #endif
1381 	sin6->sin6_family = AF_INET6;
1382 #ifdef HAVE_SIN6_LEN
1383 	sin6->sin6_len = sizeof(*sin6);
1384 #endif
1385 
1386 	inp = (struct sctp_inpcb *)so->so_pcb;
1387 	if (inp == NULL) {
1388 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1389 		SCTP_FREE_SONAME(sin6);
1390 #endif
1391 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1392 		return (ECONNRESET);
1393 	}
1394 	SCTP_INP_RLOCK(inp);
1395 	sin6->sin6_port = inp->sctp_lport;
1396 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1397 		/* For the bound all case you get back 0 */
1398 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1399 			struct sctp_tcb *stcb;
1400 			struct sockaddr_in6 *sin_a6;
1401 			struct sctp_nets *net;
1402 			int fnd;
1403 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1404 			if (stcb == NULL) {
1405 				SCTP_INP_RUNLOCK(inp);
1406 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1407 				SCTP_FREE_SONAME(sin6);
1408 #endif
1409 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1410 				return (ENOENT);
1411 			}
1412 			fnd = 0;
1413 			sin_a6 = NULL;
1414 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1415 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1416 				if (sin_a6 == NULL)
1417 					/* this will make coverity happy */
1418 					continue;
1419 
1420 				if (sin_a6->sin6_family == AF_INET6) {
1421 					fnd = 1;
1422 					break;
1423 				}
1424 			}
1425 			if ((!fnd) || (sin_a6 == NULL)) {
1426 				/* punt */
1427 				SCTP_INP_RUNLOCK(inp);
1428 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1429 				SCTP_FREE_SONAME(sin6);
1430 #endif
1431 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1432 				return (ENOENT);
1433 			}
1434 			vrf_id = inp->def_vrf_id;
1435 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1436 			if (sctp_ifa) {
1437 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1438 			}
1439 		} else {
1440 			/* For the bound all case you get back 0 */
1441 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1442 		}
1443 	} else {
1444 		/* Take the first IPv6 address in the list */
1445 		struct sctp_laddr *laddr;
1446 		int fnd = 0;
1447 
1448 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1449 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1450 				struct sockaddr_in6 *sin_a;
1451 
1452 				sin_a = &laddr->ifa->address.sin6;
1453 				sin6->sin6_addr = sin_a->sin6_addr;
1454 				fnd = 1;
1455 				break;
1456 			}
1457 		}
1458 		if (!fnd) {
1459 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1460 			SCTP_FREE_SONAME(sin6);
1461 #endif
1462 			SCTP_INP_RUNLOCK(inp);
1463 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1464 			return (ENOENT);
1465 		}
1466 	}
1467 	SCTP_INP_RUNLOCK(inp);
1468 	/* Scoping things for v6 */
1469 #ifdef SCTP_EMBEDDED_V6_SCOPE
1470 #ifdef SCTP_KAME
1471 	if ((error = sa6_recoverscope(sin6)) != 0) {
1472 		SCTP_FREE_SONAME(sin6);
1473 		return (error);
1474 	}
1475 #else
1476 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1477 		/* skip ifp check below */
1478 		in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1479 	else
1480 		sin6->sin6_scope_id = 0;	/* XXX */
1481 #endif /* SCTP_KAME */
1482 #endif /* SCTP_EMBEDDED_V6_SCOPE */
1483 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1484 	(*addr) = (struct sockaddr *)sin6;
1485 #endif
1486 	return (0);
1487 }
1488 
1489 static int
1490 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1491 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1492 {
1493 	struct sockaddr_in6 *sin6;
1494 #elif defined(__Panda__)
1495 sctp6_peeraddr(struct socket *so, struct sockaddr *addr)
1496 {
1497 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
1498 #else
1499 sctp6_peeraddr(struct socket *so, struct mbuf *nam)
1500 {
1501 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1502 #endif
1503 	int fnd;
1504 	struct sockaddr_in6 *sin_a6;
1505 	struct sctp_inpcb *inp;
1506 	struct sctp_tcb *stcb;
1507 	struct sctp_nets *net;
1508 #ifdef SCTP_KAME
1509 	int error;
1510 #endif
1511 
1512 	/* Do the malloc first in case it blocks. */
1513 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1514 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1515 	if (sin6 == NULL)
1516 		return (ENOMEM);
1517 #elif defined(__Panda__)
1518 	memset(sin6, 0, sizeof(*sin6));
1519 #else
1520 	SCTP_BUF_LEN(nam) = sizeof(*sin6);
1521 	memset(sin6, 0, sizeof(*sin6));
1522 #endif
1523 	sin6->sin6_family = AF_INET6;
1524 #ifdef HAVE_SIN6_LEN
1525 	sin6->sin6_len = sizeof(*sin6);
1526 #endif
1527 
1528 	inp = (struct sctp_inpcb *)so->so_pcb;
1529 	if ((inp == NULL) ||
1530 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1531 		/* UDP type and listeners will drop out here */
1532 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1533 		SCTP_FREE_SONAME(sin6);
1534 #endif
1535 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1536 		return (ENOTCONN);
1537 	}
1538 	SCTP_INP_RLOCK(inp);
1539 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1540 	if (stcb) {
1541 		SCTP_TCB_LOCK(stcb);
1542 	}
1543 	SCTP_INP_RUNLOCK(inp);
1544 	if (stcb == NULL) {
1545 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1546 		SCTP_FREE_SONAME(sin6);
1547 #endif
1548 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1549 		return (ECONNRESET);
1550 	}
1551 	fnd = 0;
1552 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1553 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1554 		if (sin_a6->sin6_family == AF_INET6) {
1555 			fnd = 1;
1556 			sin6->sin6_port = stcb->rport;
1557 			sin6->sin6_addr = sin_a6->sin6_addr;
1558 			break;
1559 		}
1560 	}
1561 	SCTP_TCB_UNLOCK(stcb);
1562 	if (!fnd) {
1563 		/* No IPv4 address */
1564 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1565 		SCTP_FREE_SONAME(sin6);
1566 #endif
1567 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1568 		return (ENOENT);
1569 	}
1570 #ifdef SCTP_EMBEDDED_V6_SCOPE
1571 #ifdef SCTP_KAME
1572 	if ((error = sa6_recoverscope(sin6)) != 0) {
1573 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1574 		SCTP_FREE_SONAME(sin6);
1575 #endif
1576 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1577 		return (error);
1578 	}
1579 #else
1580 	in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1581 #endif /* SCTP_KAME */
1582 #endif /* SCTP_EMBEDDED_V6_SCOPE */
1583 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1584 	*addr = (struct sockaddr *)sin6;
1585 #endif
1586 	return (0);
1587 }
1588 
1589 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1590 static int
1591 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1592 {
1593 #elif defined(__Panda__)
1594 int
1595 sctp6_in6getaddr(struct socket *so, struct sockaddr *nam, uint32_t *namelen)
1596 {
1597 #ifdef INET
1598 	struct sockaddr *addr = nam;
1599 #endif
1600 #elif defined(__Userspace__)
1601 int
1602 sctp6_in6getaddr(struct socket *so, struct mbuf *nam)
1603 {
1604 #ifdef INET
1605 	struct sockaddr *addr = mtod(nam, struct sockaddr *);
1606 #endif
1607 #else
1608 static int
1609 sctp6_in6getaddr(struct socket *so, struct mbuf *nam)
1610 {
1611 #ifdef INET
1612 	struct sockaddr *addr = mtod(nam, struct sockaddr *);
1613 #endif
1614 #endif
1615 	struct inpcb *inp = sotoinpcb(so);
1616 	int error;
1617 
1618 	if (inp == NULL) {
1619 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1620 		return (EINVAL);
1621 	}
1622 
1623 	/* allow v6 addresses precedence */
1624 	error = sctp6_getaddr(so, nam);
1625 #ifdef INET
1626 	if (error) {
1627 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1628 		struct sockaddr_in6 *sin6;
1629 #else
1630 		struct sockaddr_in6 sin6;
1631 #endif
1632 
1633 		/* try v4 next if v6 failed */
1634 		error = sctp_ingetaddr(so, nam);
1635 		if (error) {
1636 			return (error);
1637 		}
1638 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1639 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1640 		if (sin6 == NULL) {
1641 			SCTP_FREE_SONAME(*nam);
1642 			return (ENOMEM);
1643 		}
1644 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1645 		SCTP_FREE_SONAME(*nam);
1646 		*nam = (struct sockaddr *)sin6;
1647 #else
1648 		in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1649 		SCTP_BUF_LEN(nam) = sizeof(struct sockaddr_in6);
1650 		memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1651 #endif
1652 	}
1653 #endif
1654 #if defined(__Panda__)
1655 	*namelen = nam->sa_len;
1656 #endif
1657 	return (error);
1658 }
1659 
1660 
1661 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1662 static int
1663 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1664 {
1665 #elif defined(__Panda__)
1666 int
1667 sctp6_getpeeraddr(struct socket *so, struct sockaddr *nam, uint32_t *namelen)
1668 {
1669 #ifdef INET
1670 	struct sockaddr *addr = (struct sockaddr *)nam;
1671 #endif
1672 #elif defined(__Userspace__)
1673 int
1674 sctp6_getpeeraddr(struct socket *so, struct mbuf *nam)
1675 {
1676 #ifdef INET
1677 	struct sockaddr *addr = mtod(nam, struct sockaddr *);
1678 #endif
1679 #else
1680 static
1681 int
1682 sctp6_getpeeraddr(struct socket *so, struct mbuf *nam)
1683 {
1684 #ifdef INET
1685 	struct sockaddr *addr = mtod(nam, struct sockaddr *);
1686 #endif
1687 
1688 #endif
1689 	struct inpcb *inp = sotoinpcb(so);
1690 	int error;
1691 
1692 	if (inp == NULL) {
1693 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1694 		return (EINVAL);
1695 	}
1696 
1697 	/* allow v6 addresses precedence */
1698 	error = sctp6_peeraddr(so, nam);
1699 #ifdef INET
1700 	if (error) {
1701 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1702 		struct sockaddr_in6 *sin6;
1703 #else
1704 		struct sockaddr_in6 sin6;
1705 #endif
1706 
1707 		/* try v4 next if v6 failed */
1708 		error = sctp_peeraddr(so, nam);
1709 		if (error) {
1710 			return (error);
1711 		}
1712 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1713 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1714 		if (sin6 == NULL) {
1715 			SCTP_FREE_SONAME(*nam);
1716 			return (ENOMEM);
1717 		}
1718 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1719 		SCTP_FREE_SONAME(*nam);
1720 		*nam = (struct sockaddr *)sin6;
1721 #else
1722 		in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1723 		SCTP_BUF_LEN(nam) = sizeof(struct sockaddr_in6);
1724 		memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1725 #endif
1726 	}
1727 #endif
1728 #if defined(__Panda__)
1729 	*namelen = nam->sa_len;
1730 #endif
1731 	return (error);
1732 }
1733 
1734 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
1735 struct pr_usrreqs sctp6_usrreqs = {
1736 #if defined(__FreeBSD__)
1737 	.pru_abort = sctp6_abort,
1738 	.pru_accept = sctp_accept,
1739 	.pru_attach = sctp6_attach,
1740 	.pru_bind = sctp6_bind,
1741 	.pru_connect = sctp6_connect,
1742 	.pru_control = in6_control,
1743 #if __FreeBSD_version >= 690000
1744 	.pru_close = sctp6_close,
1745 	.pru_detach = sctp6_close,
1746 	.pru_sopoll = sopoll_generic,
1747 	.pru_flush = sctp_flush,
1748 #else
1749 	.pru_detach = sctp6_detach,
1750 	.pru_sopoll = sopoll,
1751 #endif
1752 	.pru_disconnect = sctp6_disconnect,
1753 	.pru_listen = sctp_listen,
1754 	.pru_peeraddr = sctp6_getpeeraddr,
1755 	.pru_send = sctp6_send,
1756 	.pru_shutdown = sctp_shutdown,
1757 	.pru_sockaddr = sctp6_in6getaddr,
1758 	.pru_sosend = sctp_sosend,
1759 	.pru_soreceive = sctp_soreceive
1760 #elif defined(__APPLE__)
1761 	.pru_abort = sctp6_abort,
1762 	.pru_accept = sctp_accept,
1763 	.pru_attach = sctp6_attach,
1764 	.pru_bind = sctp6_bind,
1765 	.pru_connect = sctp6_connect,
1766 	.pru_connect2 = pru_connect2_notsupp,
1767 	.pru_control = in6_control,
1768 	.pru_detach = sctp6_detach,
1769 	.pru_disconnect = sctp6_disconnect,
1770 	.pru_listen = sctp_listen,
1771 	.pru_peeraddr = sctp6_getpeeraddr,
1772 	.pru_rcvd = NULL,
1773 	.pru_rcvoob = pru_rcvoob_notsupp,
1774 	.pru_send = sctp6_send,
1775 	.pru_sense = pru_sense_null,
1776 	.pru_shutdown = sctp_shutdown,
1777 	.pru_sockaddr = sctp6_in6getaddr,
1778 	.pru_sosend = sctp_sosend,
1779 	.pru_soreceive = sctp_soreceive,
1780 	.pru_sopoll = sopoll
1781 #elif defined(__Windows__)
1782 	sctp6_abort,
1783 	sctp_accept,
1784 	sctp6_attach,
1785 	sctp6_bind,
1786 	sctp6_connect,
1787 	pru_connect2_notsupp,
1788 	NULL,
1789 	NULL,
1790 	sctp6_disconnect,
1791 	sctp_listen,
1792 	sctp6_getpeeraddr,
1793 	NULL,
1794 	pru_rcvoob_notsupp,
1795 	NULL,
1796 	pru_sense_null,
1797 	sctp_shutdown,
1798 	sctp_flush,
1799 	sctp6_in6getaddr,
1800 	sctp_sosend,
1801 	sctp_soreceive,
1802 	sopoll_generic,
1803 	NULL,
1804 	sctp6_close
1805 #endif
1806 };
1807 
1808 #elif !defined(__Panda__) && !defined(__Userspace__)
1809 int
1810 sctp6_usrreq(so, req, m, nam, control, p)
1811 	struct socket *so;
1812 	int req;
1813 	struct mbuf *m, *nam, *control;
1814 	struct proc *p;
1815 {
1816 	int error;
1817 	int family;
1818 
1819 	family = so->so_proto->pr_domain->dom_family;
1820 
1821 	if (req == PRU_CONTROL) {
1822 		switch (family) {
1823 		case PF_INET:
1824 			error = in_control(so, (long)m, (caddr_t)nam,
1825 			    (struct ifnet *)control );
1826 			break;
1827 #ifdef INET6
1828 		case PF_INET6:
1829 			error = in6_control(so, (long)m, (caddr_t)nam,
1830 			    (struct ifnet *)control, p);
1831 			break;
1832 #endif
1833 		default:
1834 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EAFNOSUPPORT);
1835 			error = EAFNOSUPPORT;
1836 		}
1837 		return (error);
1838 	}
1839 	switch (req) {
1840 	case PRU_ATTACH:
1841 		error = sctp6_attach(so, family, p);
1842 		break;
1843 	case PRU_DETACH:
1844 		error = sctp6_detach(so);
1845 		break;
1846 	case PRU_BIND:
1847 		if (nam == NULL) {
1848 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1849 			return (EINVAL);
1850 		}
1851 		error = sctp6_bind(so, nam, p);
1852 		break;
1853 	case PRU_LISTEN:
1854 		error = sctp_listen(so, p);
1855 		break;
1856 	case PRU_CONNECT:
1857 		if (nam == NULL) {
1858 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1859 			return (EINVAL);
1860 		}
1861 		error = sctp6_connect(so, nam, p);
1862 		break;
1863 	case PRU_DISCONNECT:
1864 		error = sctp6_disconnect(so);
1865 		break;
1866 	case PRU_ACCEPT:
1867 		if (nam == NULL) {
1868 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1869 			return (EINVAL);
1870 		}
1871 		error = sctp_accept(so, nam);
1872 		break;
1873 	case PRU_SHUTDOWN:
1874 		error = sctp_shutdown(so);
1875 		break;
1876 
1877 	case PRU_RCVD:
1878 		/*
1879 		 * For OpenBSD and NetBSD, this is real ugly. The (mbuf *)
1880 		 * nam that is passed (by soreceive()) is the int flags cast
1881 		 * as a (mbuf *) yuck!
1882 		 */
1883 		error = sctp_usr_recvd(so, (int)((long)nam));
1884 		break;
1885 
1886 	case PRU_SEND:
1887 		/* Flags are ignored */
1888 		error = sctp6_send(so, 0, m, nam, control, p);
1889 		break;
1890 	case PRU_ABORT:
1891 		error = sctp6_abort(so);
1892 		break;
1893 
1894 	case PRU_SENSE:
1895 		error = 0;
1896 		break;
1897 	case PRU_RCVOOB:
1898 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EAFNOSUPPORT);
1899 		error = EAFNOSUPPORT;
1900 		break;
1901 	case PRU_SENDOOB:
1902 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EAFNOSUPPORT);
1903 		error = EAFNOSUPPORT;
1904 		break;
1905 	case PRU_PEERADDR:
1906 		error = sctp6_getpeeraddr(so, nam);
1907 		break;
1908 	case PRU_SOCKADDR:
1909 		error = sctp6_in6getaddr(so, nam);
1910 		break;
1911 	case PRU_SLOWTIMO:
1912 		error = 0;
1913 		break;
1914 	default:
1915 		error = 0;
1916 		break;
1917 	}
1918 	return (error);
1919 }
1920 #endif
1921 #endif
1922