xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision a678f77962278b50da97a88aac8abe77c192a4f1)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #ifdef INET6
38 #include <sys/proc.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet6/sctp6_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_output.h>
53 #include <netinet/sctp_bsd_addr.h>
54 #include <netinet/sctp_crc32.h>
55 #include <netinet/icmp6.h>
56 #include <netinet/udp.h>
57 
58 extern struct protosw inetsw[];
59 
60 int
61 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
62 {
63 	struct mbuf *m;
64 	int iphlen;
65 	uint32_t vrf_id;
66 	uint8_t ecn_bits;
67 	struct sockaddr_in6 src, dst;
68 	struct ip6_hdr *ip6;
69 	struct sctphdr *sh;
70 	struct sctp_chunkhdr *ch;
71 	int length, offset;
72 #if !defined(SCTP_WITH_NO_CSUM)
73 	uint8_t compute_crc;
74 #endif
75 	uint32_t mflowid;
76 	uint8_t mflowtype;
77 	uint16_t fibnum;
78 
79 	iphlen = *offp;
80 	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
81 		SCTP_RELEASE_PKT(*i_pak);
82 		return (IPPROTO_DONE);
83 	}
84 	m = SCTP_HEADER_TO_CHAIN(*i_pak);
85 #ifdef SCTP_MBUF_LOGGING
86 	/* Log in any input mbufs */
87 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
88 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
89 	}
90 #endif
91 #ifdef SCTP_PACKET_LOGGING
92 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
93 		sctp_packet_log(m);
94 	}
95 #endif
96 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
97 	    "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
98 	    m->m_pkthdr.len,
99 	    if_name(m->m_pkthdr.rcvif),
100 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
101 	mflowid = m->m_pkthdr.flowid;
102 	mflowtype = M_HASHTYPE_GET(m);
103 	fibnum = M_GETFIB(m);
104 	SCTP_STAT_INCR(sctps_recvpackets);
105 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
106 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
107 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
108 	ip6 = mtod(m, struct ip6_hdr *);
109 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
110 	    (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
111 	if (sh == NULL) {
112 		SCTP_STAT_INCR(sctps_hdrops);
113 		return (IPPROTO_DONE);
114 	}
115 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
116 	offset -= sizeof(struct sctp_chunkhdr);
117 	memset(&src, 0, sizeof(struct sockaddr_in6));
118 	src.sin6_family = AF_INET6;
119 	src.sin6_len = sizeof(struct sockaddr_in6);
120 	src.sin6_port = sh->src_port;
121 	src.sin6_addr = ip6->ip6_src;
122 	if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
123 		goto out;
124 	}
125 	memset(&dst, 0, sizeof(struct sockaddr_in6));
126 	dst.sin6_family = AF_INET6;
127 	dst.sin6_len = sizeof(struct sockaddr_in6);
128 	dst.sin6_port = sh->dest_port;
129 	dst.sin6_addr = ip6->ip6_dst;
130 	if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
131 		goto out;
132 	}
133 	length = ntohs(ip6->ip6_plen) + iphlen;
134 	/* Validate mbuf chain length with IP payload length. */
135 	if (SCTP_HEADER_LEN(m) != length) {
136 		SCTPDBG(SCTP_DEBUG_INPUT1,
137 		    "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
138 		SCTP_STAT_INCR(sctps_hdrops);
139 		goto out;
140 	}
141 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
142 		goto out;
143 	}
144 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
145 #if defined(SCTP_WITH_NO_CSUM)
146 	SCTP_STAT_INCR(sctps_recvnocrc);
147 #else
148 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
149 		SCTP_STAT_INCR(sctps_recvhwcrc);
150 		compute_crc = 0;
151 	} else {
152 		SCTP_STAT_INCR(sctps_recvswcrc);
153 		compute_crc = 1;
154 	}
155 #endif
156 	sctp_common_input_processing(&m, iphlen, offset, length,
157 	    (struct sockaddr *)&src,
158 	    (struct sockaddr *)&dst,
159 	    sh, ch,
160 #if !defined(SCTP_WITH_NO_CSUM)
161 	    compute_crc,
162 #endif
163 	    ecn_bits,
164 	    mflowtype, mflowid, fibnum,
165 	    vrf_id, port);
166 out:
167 	if (m) {
168 		sctp_m_freem(m);
169 	}
170 	return (IPPROTO_DONE);
171 }
172 
173 
174 int
175 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
176 {
177 	return (sctp6_input_with_port(i_pak, offp, 0));
178 }
179 
180 void
181 sctp6_notify(struct sctp_inpcb *inp,
182     struct sctp_tcb *stcb,
183     struct sctp_nets *net,
184     uint8_t icmp6_type,
185     uint8_t icmp6_code,
186     uint16_t next_mtu)
187 {
188 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
189 	struct socket *so;
190 #endif
191 	int timer_stopped;
192 
193 	switch (icmp6_type) {
194 	case ICMP6_DST_UNREACH:
195 		if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
196 		    (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
197 		    (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
198 		    (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
199 			/* Mark the net unreachable. */
200 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
201 				/* Ok that destination is not reachable */
202 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
203 				net->dest_state &= ~SCTP_ADDR_PF;
204 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
205 				    stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
206 			}
207 		}
208 		SCTP_TCB_UNLOCK(stcb);
209 		break;
210 	case ICMP6_PARAM_PROB:
211 		/* Treat it like an ABORT. */
212 		if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
213 			sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
214 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
215 			so = SCTP_INP_SO(inp);
216 			atomic_add_int(&stcb->asoc.refcnt, 1);
217 			SCTP_TCB_UNLOCK(stcb);
218 			SCTP_SOCKET_LOCK(so, 1);
219 			SCTP_TCB_LOCK(stcb);
220 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
221 #endif
222 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
223 			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
224 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
225 			SCTP_SOCKET_UNLOCK(so, 1);
226 #endif
227 		} else {
228 			SCTP_TCB_UNLOCK(stcb);
229 		}
230 		break;
231 	case ICMP6_PACKET_TOO_BIG:
232 		if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
233 			timer_stopped = 1;
234 			sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
235 			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
236 		} else {
237 			timer_stopped = 0;
238 		}
239 		/* Update the path MTU. */
240 		if (net->mtu > next_mtu) {
241 			net->mtu = next_mtu;
242 			if (net->port) {
243 				net->mtu -= sizeof(struct udphdr);
244 			}
245 		}
246 		/* Update the association MTU */
247 		if (stcb->asoc.smallest_mtu > next_mtu) {
248 			sctp_pathmtu_adjustment(stcb, next_mtu);
249 		}
250 		/* Finally, start the PMTU timer if it was running before. */
251 		if (timer_stopped) {
252 			sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
253 		}
254 		SCTP_TCB_UNLOCK(stcb);
255 		break;
256 	default:
257 		SCTP_TCB_UNLOCK(stcb);
258 		break;
259 	}
260 }
261 
262 void
263 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
264 {
265 	struct ip6ctlparam *ip6cp;
266 	struct sctp_inpcb *inp;
267 	struct sctp_tcb *stcb;
268 	struct sctp_nets *net;
269 	struct sctphdr sh;
270 	struct sockaddr_in6 src, dst;
271 
272 	if (pktdst->sa_family != AF_INET6 ||
273 	    pktdst->sa_len != sizeof(struct sockaddr_in6)) {
274 		return;
275 	}
276 	if ((unsigned)cmd >= PRC_NCMDS) {
277 		return;
278 	}
279 	if (PRC_IS_REDIRECT(cmd)) {
280 		d = NULL;
281 	} else if (inet6ctlerrmap[cmd] == 0) {
282 		return;
283 	}
284 	/* If the parameter is from icmp6, decode it. */
285 	if (d != NULL) {
286 		ip6cp = (struct ip6ctlparam *)d;
287 	} else {
288 		ip6cp = (struct ip6ctlparam *)NULL;
289 	}
290 
291 	if (ip6cp != NULL) {
292 		/*
293 		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
294 		 * valid.
295 		 */
296 		if (ip6cp->ip6c_m == NULL) {
297 			return;
298 		}
299 		/*
300 		 * Check if we can safely examine the ports and the
301 		 * verification tag of the SCTP common header.
302 		 */
303 		if (ip6cp->ip6c_m->m_pkthdr.len <
304 		    (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
305 			return;
306 		}
307 		/* Copy out the port numbers and the verification tag. */
308 		bzero(&sh, sizeof(sh));
309 		m_copydata(ip6cp->ip6c_m,
310 		    ip6cp->ip6c_off,
311 		    sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
312 		    (caddr_t)&sh);
313 		memset(&src, 0, sizeof(struct sockaddr_in6));
314 		src.sin6_family = AF_INET6;
315 		src.sin6_len = sizeof(struct sockaddr_in6);
316 		src.sin6_port = sh.src_port;
317 		src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
318 		if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
319 			return;
320 		}
321 		memset(&dst, 0, sizeof(struct sockaddr_in6));
322 		dst.sin6_family = AF_INET6;
323 		dst.sin6_len = sizeof(struct sockaddr_in6);
324 		dst.sin6_port = sh.dest_port;
325 		dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
326 		if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
327 			return;
328 		}
329 		inp = NULL;
330 		net = NULL;
331 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
332 		    (struct sockaddr *)&src,
333 		    &inp, &net, 1, SCTP_DEFAULT_VRFID);
334 		if ((stcb != NULL) &&
335 		    (net != NULL) &&
336 		    (inp != NULL)) {
337 			/* Check the verification tag */
338 			if (ntohl(sh.v_tag) != 0) {
339 				/*
340 				 * This must be the verification tag used
341 				 * for sending out packets. We don't
342 				 * consider packets reflecting the
343 				 * verification tag.
344 				 */
345 				if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
346 					SCTP_TCB_UNLOCK(stcb);
347 					return;
348 				}
349 			} else {
350 				if (ip6cp->ip6c_m->m_pkthdr.len >=
351 				    ip6cp->ip6c_off + sizeof(struct sctphdr) +
352 				    sizeof(struct sctp_chunkhdr) +
353 				    offsetof(struct sctp_init, a_rwnd)) {
354 					/*
355 					 * In this case we can check if we
356 					 * got an INIT chunk and if the
357 					 * initiate tag matches.
358 					 */
359 					uint32_t initiate_tag;
360 					uint8_t chunk_type;
361 
362 					m_copydata(ip6cp->ip6c_m,
363 					    ip6cp->ip6c_off +
364 					    sizeof(struct sctphdr),
365 					    sizeof(uint8_t),
366 					    (caddr_t)&chunk_type);
367 					m_copydata(ip6cp->ip6c_m,
368 					    ip6cp->ip6c_off +
369 					    sizeof(struct sctphdr) +
370 					    sizeof(struct sctp_chunkhdr),
371 					    sizeof(uint32_t),
372 					    (caddr_t)&initiate_tag);
373 					if ((chunk_type != SCTP_INITIATION) ||
374 					    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
375 						SCTP_TCB_UNLOCK(stcb);
376 						return;
377 					}
378 				} else {
379 					SCTP_TCB_UNLOCK(stcb);
380 					return;
381 				}
382 			}
383 			sctp6_notify(inp, stcb, net,
384 			    ip6cp->ip6c_icmp6->icmp6_type,
385 			    ip6cp->ip6c_icmp6->icmp6_code,
386 			    (uint16_t)ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
387 		} else {
388 			if ((stcb == NULL) && (inp != NULL)) {
389 				/* reduce inp's ref-count */
390 				SCTP_INP_WLOCK(inp);
391 				SCTP_INP_DECR_REF(inp);
392 				SCTP_INP_WUNLOCK(inp);
393 			}
394 			if (stcb) {
395 				SCTP_TCB_UNLOCK(stcb);
396 			}
397 		}
398 	}
399 }
400 
401 /*
402  * this routine can probably be collasped into the one in sctp_userreq.c
403  * since they do the same thing and now we lookup with a sockaddr
404  */
405 static int
406 sctp6_getcred(SYSCTL_HANDLER_ARGS)
407 {
408 	struct xucred xuc;
409 	struct sockaddr_in6 addrs[2];
410 	struct sctp_inpcb *inp;
411 	struct sctp_nets *net;
412 	struct sctp_tcb *stcb;
413 	int error;
414 	uint32_t vrf_id;
415 
416 	vrf_id = SCTP_DEFAULT_VRFID;
417 
418 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
419 	if (error)
420 		return (error);
421 
422 	if (req->newlen != sizeof(addrs)) {
423 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
424 		return (EINVAL);
425 	}
426 	if (req->oldlen != sizeof(struct ucred)) {
427 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
428 		return (EINVAL);
429 	}
430 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
431 	if (error)
432 		return (error);
433 
434 	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
435 	    sin6tosa(&addrs[0]),
436 	    &inp, &net, 1, vrf_id);
437 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
438 		if ((inp != NULL) && (stcb == NULL)) {
439 			/* reduce ref-count */
440 			SCTP_INP_WLOCK(inp);
441 			SCTP_INP_DECR_REF(inp);
442 			goto cred_can_cont;
443 		}
444 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
445 		error = ENOENT;
446 		goto out;
447 	}
448 	SCTP_TCB_UNLOCK(stcb);
449 	/*
450 	 * We use the write lock here, only since in the error leg we need
451 	 * it. If we used RLOCK, then we would have to
452 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
453 	 * Better to use higher wlock.
454 	 */
455 	SCTP_INP_WLOCK(inp);
456 cred_can_cont:
457 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
458 	if (error) {
459 		SCTP_INP_WUNLOCK(inp);
460 		goto out;
461 	}
462 	cru2x(inp->sctp_socket->so_cred, &xuc);
463 	SCTP_INP_WUNLOCK(inp);
464 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
465 out:
466 	return (error);
467 }
468 
469 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
470     0, 0,
471     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
472 
473 
474 /* This is the same as the sctp_abort() could be made common */
475 static void
476 sctp6_abort(struct socket *so)
477 {
478 	struct sctp_inpcb *inp;
479 	uint32_t flags;
480 
481 	inp = (struct sctp_inpcb *)so->so_pcb;
482 	if (inp == NULL) {
483 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
484 		return;
485 	}
486 sctp_must_try_again:
487 	flags = inp->sctp_flags;
488 #ifdef SCTP_LOG_CLOSING
489 	sctp_log_closing(inp, NULL, 17);
490 #endif
491 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
492 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
493 #ifdef SCTP_LOG_CLOSING
494 		sctp_log_closing(inp, NULL, 16);
495 #endif
496 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
497 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
498 		SOCK_LOCK(so);
499 		SCTP_SB_CLEAR(so->so_snd);
500 		/*
501 		 * same for the rcv ones, they are only here for the
502 		 * accounting/select.
503 		 */
504 		SCTP_SB_CLEAR(so->so_rcv);
505 		/* Now null out the reference, we are completely detached. */
506 		so->so_pcb = NULL;
507 		SOCK_UNLOCK(so);
508 	} else {
509 		flags = inp->sctp_flags;
510 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
511 			goto sctp_must_try_again;
512 		}
513 	}
514 	return;
515 }
516 
517 static int
518 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
519 {
520 	struct in6pcb *inp6;
521 	int error;
522 	struct sctp_inpcb *inp;
523 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
524 
525 	inp = (struct sctp_inpcb *)so->so_pcb;
526 	if (inp != NULL) {
527 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
528 		return (EINVAL);
529 	}
530 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
531 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
532 		if (error)
533 			return (error);
534 	}
535 	error = sctp_inpcb_alloc(so, vrf_id);
536 	if (error)
537 		return (error);
538 	inp = (struct sctp_inpcb *)so->so_pcb;
539 	SCTP_INP_WLOCK(inp);
540 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
541 	inp6 = (struct in6pcb *)inp;
542 
543 	inp6->inp_vflag |= INP_IPV6;
544 	inp6->in6p_hops = -1;	/* use kernel default */
545 	inp6->in6p_cksum = -1;	/* just to be sure */
546 #ifdef INET
547 	/*
548 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
549 	 * socket as well, because the socket may be bound to an IPv6
550 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
551 	 */
552 	inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
553 #endif
554 	/*
555 	 * Hmm what about the IPSEC stuff that is missing here but in
556 	 * sctp_attach()?
557 	 */
558 	SCTP_INP_WUNLOCK(inp);
559 	return (0);
560 }
561 
562 static int
563 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
564 {
565 	struct sctp_inpcb *inp;
566 	struct in6pcb *inp6;
567 	int error;
568 
569 	inp = (struct sctp_inpcb *)so->so_pcb;
570 	if (inp == NULL) {
571 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
572 		return (EINVAL);
573 	}
574 	if (addr) {
575 		switch (addr->sa_family) {
576 #ifdef INET
577 		case AF_INET:
578 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
579 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
580 				return (EINVAL);
581 			}
582 			break;
583 #endif
584 #ifdef INET6
585 		case AF_INET6:
586 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
587 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
588 				return (EINVAL);
589 			}
590 			break;
591 #endif
592 		default:
593 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
594 			return (EINVAL);
595 		}
596 	}
597 	inp6 = (struct in6pcb *)inp;
598 	inp6->inp_vflag &= ~INP_IPV4;
599 	inp6->inp_vflag |= INP_IPV6;
600 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
601 		switch (addr->sa_family) {
602 #ifdef INET
603 		case AF_INET:
604 			/* binding v4 addr to v6 socket, so reset flags */
605 			inp6->inp_vflag |= INP_IPV4;
606 			inp6->inp_vflag &= ~INP_IPV6;
607 			break;
608 #endif
609 #ifdef INET6
610 		case AF_INET6:
611 			{
612 				struct sockaddr_in6 *sin6_p;
613 
614 				sin6_p = (struct sockaddr_in6 *)addr;
615 
616 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
617 					inp6->inp_vflag |= INP_IPV4;
618 				}
619 #ifdef INET
620 				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
621 					struct sockaddr_in sin;
622 
623 					in6_sin6_2_sin(&sin, sin6_p);
624 					inp6->inp_vflag |= INP_IPV4;
625 					inp6->inp_vflag &= ~INP_IPV6;
626 					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
627 					return (error);
628 				}
629 #endif
630 				break;
631 			}
632 #endif
633 		default:
634 			break;
635 		}
636 	} else if (addr != NULL) {
637 		struct sockaddr_in6 *sin6_p;
638 
639 		/* IPV6_V6ONLY socket */
640 #ifdef INET
641 		if (addr->sa_family == AF_INET) {
642 			/* can't bind v4 addr to v6 only socket! */
643 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
644 			return (EINVAL);
645 		}
646 #endif
647 		sin6_p = (struct sockaddr_in6 *)addr;
648 
649 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
650 			/* can't bind v4-mapped addrs either! */
651 			/* NOTE: we don't support SIIT */
652 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
653 			return (EINVAL);
654 		}
655 	}
656 	error = sctp_inpcb_bind(so, addr, NULL, p);
657 	return (error);
658 }
659 
660 
661 static void
662 sctp6_close(struct socket *so)
663 {
664 	sctp_close(so);
665 }
666 
667 /* This could be made common with sctp_detach() since they are identical */
668 
669 static
670 int
671 sctp6_disconnect(struct socket *so)
672 {
673 	return (sctp_disconnect(so));
674 }
675 
676 
677 int
678 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
679     struct mbuf *control, struct thread *p);
680 
681 
682 static int
683 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
684     struct mbuf *control, struct thread *p)
685 {
686 	struct sctp_inpcb *inp;
687 	struct in6pcb *inp6;
688 
689 #ifdef INET
690 	struct sockaddr_in6 *sin6;
691 #endif				/* INET */
692 	/* No SPL needed since sctp_output does this */
693 
694 	inp = (struct sctp_inpcb *)so->so_pcb;
695 	if (inp == NULL) {
696 		if (control) {
697 			SCTP_RELEASE_PKT(control);
698 			control = NULL;
699 		}
700 		SCTP_RELEASE_PKT(m);
701 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
702 		return (EINVAL);
703 	}
704 	inp6 = (struct in6pcb *)inp;
705 	/*
706 	 * For the TCP model we may get a NULL addr, if we are a connected
707 	 * socket thats ok.
708 	 */
709 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
710 	    (addr == NULL)) {
711 		goto connected_type;
712 	}
713 	if (addr == NULL) {
714 		SCTP_RELEASE_PKT(m);
715 		if (control) {
716 			SCTP_RELEASE_PKT(control);
717 			control = NULL;
718 		}
719 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
720 		return (EDESTADDRREQ);
721 	}
722 #ifdef INET
723 	sin6 = (struct sockaddr_in6 *)addr;
724 	if (SCTP_IPV6_V6ONLY(inp6)) {
725 		/*
726 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
727 		 * v4 addr or v4-mapped addr
728 		 */
729 		if (addr->sa_family == AF_INET) {
730 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
731 			return (EINVAL);
732 		}
733 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
734 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
735 			return (EINVAL);
736 		}
737 	}
738 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
739 		struct sockaddr_in sin;
740 
741 		/* convert v4-mapped into v4 addr and send */
742 		in6_sin6_2_sin(&sin, sin6);
743 		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
744 	}
745 #endif				/* INET */
746 connected_type:
747 	/* now what about control */
748 	if (control) {
749 		if (inp->control) {
750 			SCTP_PRINTF("huh? control set?\n");
751 			SCTP_RELEASE_PKT(inp->control);
752 			inp->control = NULL;
753 		}
754 		inp->control = control;
755 	}
756 	/* Place the data */
757 	if (inp->pkt) {
758 		SCTP_BUF_NEXT(inp->pkt_last) = m;
759 		inp->pkt_last = m;
760 	} else {
761 		inp->pkt_last = inp->pkt = m;
762 	}
763 	if (
764 	/* FreeBSD and MacOSX uses a flag passed */
765 	    ((flags & PRUS_MORETOCOME) == 0)
766 	    ) {
767 		/*
768 		 * note with the current version this code will only be used
769 		 * by OpenBSD, NetBSD and FreeBSD have methods for
770 		 * re-defining sosend() to use sctp_sosend().  One can
771 		 * optionaly switch back to this code (by changing back the
772 		 * defininitions but this is not advisable.
773 		 */
774 		int ret;
775 
776 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
777 		inp->pkt = NULL;
778 		inp->control = NULL;
779 		return (ret);
780 	} else {
781 		return (0);
782 	}
783 }
784 
785 static int
786 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
787 {
788 	uint32_t vrf_id;
789 	int error = 0;
790 	struct sctp_inpcb *inp;
791 	struct sctp_tcb *stcb;
792 #ifdef INET
793 	struct in6pcb *inp6;
794 	struct sockaddr_in6 *sin6;
795 	union sctp_sockstore store;
796 #endif
797 
798 #ifdef INET
799 	inp6 = (struct in6pcb *)so->so_pcb;
800 #endif
801 	inp = (struct sctp_inpcb *)so->so_pcb;
802 	if (inp == NULL) {
803 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
804 		return (ECONNRESET);	/* I made the same as TCP since we are
805 					 * not setup? */
806 	}
807 	if (addr == NULL) {
808 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
809 		return (EINVAL);
810 	}
811 	switch (addr->sa_family) {
812 #ifdef INET
813 	case AF_INET:
814 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
815 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
816 			return (EINVAL);
817 		}
818 		break;
819 #endif
820 #ifdef INET6
821 	case AF_INET6:
822 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
823 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
824 			return (EINVAL);
825 		}
826 		break;
827 #endif
828 	default:
829 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
830 		return (EINVAL);
831 	}
832 
833 	vrf_id = inp->def_vrf_id;
834 	SCTP_ASOC_CREATE_LOCK(inp);
835 	SCTP_INP_RLOCK(inp);
836 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
837 	    SCTP_PCB_FLAGS_UNBOUND) {
838 		/* Bind a ephemeral port */
839 		SCTP_INP_RUNLOCK(inp);
840 		error = sctp6_bind(so, NULL, p);
841 		if (error) {
842 			SCTP_ASOC_CREATE_UNLOCK(inp);
843 
844 			return (error);
845 		}
846 		SCTP_INP_RLOCK(inp);
847 	}
848 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
849 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
850 		/* We are already connected AND the TCP model */
851 		SCTP_INP_RUNLOCK(inp);
852 		SCTP_ASOC_CREATE_UNLOCK(inp);
853 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
854 		return (EADDRINUSE);
855 	}
856 #ifdef INET
857 	sin6 = (struct sockaddr_in6 *)addr;
858 	if (SCTP_IPV6_V6ONLY(inp6)) {
859 		/*
860 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
861 		 * addr or v4-mapped addr
862 		 */
863 		if (addr->sa_family == AF_INET) {
864 			SCTP_INP_RUNLOCK(inp);
865 			SCTP_ASOC_CREATE_UNLOCK(inp);
866 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
867 			return (EINVAL);
868 		}
869 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
870 			SCTP_INP_RUNLOCK(inp);
871 			SCTP_ASOC_CREATE_UNLOCK(inp);
872 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
873 			return (EINVAL);
874 		}
875 	}
876 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
877 		/* convert v4-mapped into v4 addr */
878 		in6_sin6_2_sin(&store.sin, sin6);
879 		addr = &store.sa;
880 	}
881 #endif				/* INET */
882 	/* Now do we connect? */
883 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
884 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
885 		if (stcb) {
886 			SCTP_TCB_UNLOCK(stcb);
887 		}
888 		SCTP_INP_RUNLOCK(inp);
889 	} else {
890 		SCTP_INP_RUNLOCK(inp);
891 		SCTP_INP_WLOCK(inp);
892 		SCTP_INP_INCR_REF(inp);
893 		SCTP_INP_WUNLOCK(inp);
894 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
895 		if (stcb == NULL) {
896 			SCTP_INP_WLOCK(inp);
897 			SCTP_INP_DECR_REF(inp);
898 			SCTP_INP_WUNLOCK(inp);
899 		}
900 	}
901 
902 	if (stcb != NULL) {
903 		/* Already have or am bring up an association */
904 		SCTP_ASOC_CREATE_UNLOCK(inp);
905 		SCTP_TCB_UNLOCK(stcb);
906 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
907 		return (EALREADY);
908 	}
909 	/* We are GOOD to go */
910 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
911 	    inp->sctp_ep.pre_open_stream_count,
912 	    inp->sctp_ep.port, p);
913 	SCTP_ASOC_CREATE_UNLOCK(inp);
914 	if (stcb == NULL) {
915 		/* Gak! no memory */
916 		return (error);
917 	}
918 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
919 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
920 		/* Set the connected flag so we can queue data */
921 		soisconnecting(so);
922 	}
923 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
924 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
925 
926 	/* initialize authentication parameters for the assoc */
927 	sctp_initialize_auth_params(inp, stcb);
928 
929 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
930 	SCTP_TCB_UNLOCK(stcb);
931 	return (error);
932 }
933 
934 static int
935 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
936 {
937 	struct sockaddr_in6 *sin6;
938 	struct sctp_inpcb *inp;
939 	uint32_t vrf_id;
940 	struct sctp_ifa *sctp_ifa;
941 
942 	int error;
943 
944 	/*
945 	 * Do the malloc first in case it blocks.
946 	 */
947 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
948 	if (sin6 == NULL)
949 		return (ENOMEM);
950 	sin6->sin6_family = AF_INET6;
951 	sin6->sin6_len = sizeof(*sin6);
952 
953 	inp = (struct sctp_inpcb *)so->so_pcb;
954 	if (inp == NULL) {
955 		SCTP_FREE_SONAME(sin6);
956 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
957 		return (ECONNRESET);
958 	}
959 	SCTP_INP_RLOCK(inp);
960 	sin6->sin6_port = inp->sctp_lport;
961 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
962 		/* For the bound all case you get back 0 */
963 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
964 			struct sctp_tcb *stcb;
965 			struct sockaddr_in6 *sin_a6;
966 			struct sctp_nets *net;
967 			int fnd;
968 
969 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
970 			if (stcb == NULL) {
971 				SCTP_INP_RUNLOCK(inp);
972 				SCTP_FREE_SONAME(sin6);
973 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
974 				return (ENOENT);
975 			}
976 			fnd = 0;
977 			sin_a6 = NULL;
978 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
979 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
980 				if (sin_a6 == NULL)
981 					/* this will make coverity happy */
982 					continue;
983 
984 				if (sin_a6->sin6_family == AF_INET6) {
985 					fnd = 1;
986 					break;
987 				}
988 			}
989 			if ((!fnd) || (sin_a6 == NULL)) {
990 				/* punt */
991 				SCTP_INP_RUNLOCK(inp);
992 				SCTP_FREE_SONAME(sin6);
993 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
994 				return (ENOENT);
995 			}
996 			vrf_id = inp->def_vrf_id;
997 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
998 			if (sctp_ifa) {
999 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1000 			}
1001 		} else {
1002 			/* For the bound all case you get back 0 */
1003 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1004 		}
1005 	} else {
1006 		/* Take the first IPv6 address in the list */
1007 		struct sctp_laddr *laddr;
1008 		int fnd = 0;
1009 
1010 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1011 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1012 				struct sockaddr_in6 *sin_a;
1013 
1014 				sin_a = &laddr->ifa->address.sin6;
1015 				sin6->sin6_addr = sin_a->sin6_addr;
1016 				fnd = 1;
1017 				break;
1018 			}
1019 		}
1020 		if (!fnd) {
1021 			SCTP_FREE_SONAME(sin6);
1022 			SCTP_INP_RUNLOCK(inp);
1023 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1024 			return (ENOENT);
1025 		}
1026 	}
1027 	SCTP_INP_RUNLOCK(inp);
1028 	/* Scoping things for v6 */
1029 	if ((error = sa6_recoverscope(sin6)) != 0) {
1030 		SCTP_FREE_SONAME(sin6);
1031 		return (error);
1032 	}
1033 	(*addr) = (struct sockaddr *)sin6;
1034 	return (0);
1035 }
1036 
1037 static int
1038 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1039 {
1040 	struct sockaddr_in6 *sin6;
1041 	int fnd;
1042 	struct sockaddr_in6 *sin_a6;
1043 	struct sctp_inpcb *inp;
1044 	struct sctp_tcb *stcb;
1045 	struct sctp_nets *net;
1046 	int error;
1047 
1048 	/* Do the malloc first in case it blocks. */
1049 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1050 	if (sin6 == NULL)
1051 		return (ENOMEM);
1052 	sin6->sin6_family = AF_INET6;
1053 	sin6->sin6_len = sizeof(*sin6);
1054 
1055 	inp = (struct sctp_inpcb *)so->so_pcb;
1056 	if ((inp == NULL) ||
1057 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1058 		/* UDP type and listeners will drop out here */
1059 		SCTP_FREE_SONAME(sin6);
1060 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1061 		return (ENOTCONN);
1062 	}
1063 	SCTP_INP_RLOCK(inp);
1064 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1065 	if (stcb) {
1066 		SCTP_TCB_LOCK(stcb);
1067 	}
1068 	SCTP_INP_RUNLOCK(inp);
1069 	if (stcb == NULL) {
1070 		SCTP_FREE_SONAME(sin6);
1071 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1072 		return (ECONNRESET);
1073 	}
1074 	fnd = 0;
1075 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1076 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1077 		if (sin_a6->sin6_family == AF_INET6) {
1078 			fnd = 1;
1079 			sin6->sin6_port = stcb->rport;
1080 			sin6->sin6_addr = sin_a6->sin6_addr;
1081 			break;
1082 		}
1083 	}
1084 	SCTP_TCB_UNLOCK(stcb);
1085 	if (!fnd) {
1086 		/* No IPv4 address */
1087 		SCTP_FREE_SONAME(sin6);
1088 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1089 		return (ENOENT);
1090 	}
1091 	if ((error = sa6_recoverscope(sin6)) != 0) {
1092 		SCTP_FREE_SONAME(sin6);
1093 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1094 		return (error);
1095 	}
1096 	*addr = (struct sockaddr *)sin6;
1097 	return (0);
1098 }
1099 
1100 static int
1101 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1102 {
1103 	struct in6pcb *inp6 = sotoin6pcb(so);
1104 	int error;
1105 
1106 	if (inp6 == NULL) {
1107 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1108 		return (EINVAL);
1109 	}
1110 	/* allow v6 addresses precedence */
1111 	error = sctp6_getaddr(so, nam);
1112 #ifdef INET
1113 	if (error) {
1114 		struct sockaddr_in6 *sin6;
1115 
1116 		/* try v4 next if v6 failed */
1117 		error = sctp_ingetaddr(so, nam);
1118 		if (error) {
1119 			return (error);
1120 		}
1121 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1122 		if (sin6 == NULL) {
1123 			SCTP_FREE_SONAME(*nam);
1124 			return (ENOMEM);
1125 		}
1126 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1127 		SCTP_FREE_SONAME(*nam);
1128 		*nam = (struct sockaddr *)sin6;
1129 	}
1130 #endif
1131 	return (error);
1132 }
1133 
1134 
1135 static int
1136 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1137 {
1138 	struct in6pcb *inp6 = sotoin6pcb(so);
1139 	int error;
1140 
1141 	if (inp6 == NULL) {
1142 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1143 		return (EINVAL);
1144 	}
1145 	/* allow v6 addresses precedence */
1146 	error = sctp6_peeraddr(so, nam);
1147 #ifdef INET
1148 	if (error) {
1149 		struct sockaddr_in6 *sin6;
1150 
1151 		/* try v4 next if v6 failed */
1152 		error = sctp_peeraddr(so, nam);
1153 		if (error) {
1154 			return (error);
1155 		}
1156 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1157 		if (sin6 == NULL) {
1158 			SCTP_FREE_SONAME(*nam);
1159 			return (ENOMEM);
1160 		}
1161 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1162 		SCTP_FREE_SONAME(*nam);
1163 		*nam = (struct sockaddr *)sin6;
1164 	}
1165 #endif
1166 	return (error);
1167 }
1168 
1169 struct pr_usrreqs sctp6_usrreqs = {
1170 	.pru_abort = sctp6_abort,
1171 	.pru_accept = sctp_accept,
1172 	.pru_attach = sctp6_attach,
1173 	.pru_bind = sctp6_bind,
1174 	.pru_connect = sctp6_connect,
1175 	.pru_control = in6_control,
1176 	.pru_close = sctp6_close,
1177 	.pru_detach = sctp6_close,
1178 	.pru_sopoll = sopoll_generic,
1179 	.pru_flush = sctp_flush,
1180 	.pru_disconnect = sctp6_disconnect,
1181 	.pru_listen = sctp_listen,
1182 	.pru_peeraddr = sctp6_getpeeraddr,
1183 	.pru_send = sctp6_send,
1184 	.pru_shutdown = sctp_shutdown,
1185 	.pru_sockaddr = sctp6_in6getaddr,
1186 	.pru_sosend = sctp_sosend,
1187 	.pru_soreceive = sctp_soreceive
1188 };
1189 
1190 #endif
1191