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