xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision 82725ba9bf1fd59746a4006a06f24d4d61d142f2)
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 extern struct protosw inetsw[];
61 
62 int
63 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
64 {
65 	struct mbuf *m;
66 	int iphlen;
67 	uint32_t vrf_id;
68 	uint8_t ecn_bits;
69 	struct sockaddr_in6 src, dst;
70 	struct ip6_hdr *ip6;
71 	struct sctphdr *sh;
72 	struct sctp_chunkhdr *ch;
73 	int length, offset;
74 #if !defined(SCTP_WITH_NO_CSUM)
75 	uint8_t compute_crc;
76 #endif
77 	uint32_t mflowid;
78 	uint8_t mflowtype;
79 	uint16_t fibnum;
80 
81 	iphlen = *offp;
82 	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
83 		SCTP_RELEASE_PKT(*i_pak);
84 		return (IPPROTO_DONE);
85 	}
86 	m = SCTP_HEADER_TO_CHAIN(*i_pak);
87 #ifdef SCTP_MBUF_LOGGING
88 	/* Log in any input mbufs */
89 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
90 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
91 	}
92 #endif
93 #ifdef SCTP_PACKET_LOGGING
94 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
95 		sctp_packet_log(m);
96 	}
97 #endif
98 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
99 	    "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
100 	    m->m_pkthdr.len,
101 	    if_name(m->m_pkthdr.rcvif),
102 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
103 	mflowid = m->m_pkthdr.flowid;
104 	mflowtype = M_HASHTYPE_GET(m);
105 	fibnum = M_GETFIB(m);
106 	SCTP_STAT_INCR(sctps_recvpackets);
107 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
108 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
109 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
110 	ip6 = mtod(m, struct ip6_hdr *);
111 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
112 	    (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
113 	if (sh == NULL) {
114 		SCTP_STAT_INCR(sctps_hdrops);
115 		return (IPPROTO_DONE);
116 	}
117 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
118 	offset -= sizeof(struct sctp_chunkhdr);
119 	memset(&src, 0, sizeof(struct sockaddr_in6));
120 	src.sin6_family = AF_INET6;
121 	src.sin6_len = sizeof(struct sockaddr_in6);
122 	src.sin6_port = sh->src_port;
123 	src.sin6_addr = ip6->ip6_src;
124 	if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
125 		goto out;
126 	}
127 	memset(&dst, 0, sizeof(struct sockaddr_in6));
128 	dst.sin6_family = AF_INET6;
129 	dst.sin6_len = sizeof(struct sockaddr_in6);
130 	dst.sin6_port = sh->dest_port;
131 	dst.sin6_addr = ip6->ip6_dst;
132 	if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
133 		goto out;
134 	}
135 	length = ntohs(ip6->ip6_plen) + iphlen;
136 	/* Validate mbuf chain length with IP payload length. */
137 	if (SCTP_HEADER_LEN(m) != length) {
138 		SCTPDBG(SCTP_DEBUG_INPUT1,
139 		    "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
140 		SCTP_STAT_INCR(sctps_hdrops);
141 		goto out;
142 	}
143 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
144 		goto out;
145 	}
146 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
147 #if defined(SCTP_WITH_NO_CSUM)
148 	SCTP_STAT_INCR(sctps_recvnocrc);
149 #else
150 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
151 		SCTP_STAT_INCR(sctps_recvhwcrc);
152 		compute_crc = 0;
153 	} else {
154 		SCTP_STAT_INCR(sctps_recvswcrc);
155 		compute_crc = 1;
156 	}
157 #endif
158 	sctp_common_input_processing(&m, iphlen, offset, length,
159 	    (struct sockaddr *)&src,
160 	    (struct sockaddr *)&dst,
161 	    sh, ch,
162 #if !defined(SCTP_WITH_NO_CSUM)
163 	    compute_crc,
164 #endif
165 	    ecn_bits,
166 	    mflowtype, mflowid, fibnum,
167 	    vrf_id, port);
168 out:
169 	if (m) {
170 		sctp_m_freem(m);
171 	}
172 	return (IPPROTO_DONE);
173 }
174 
175 
176 int
177 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
178 {
179 	return (sctp6_input_with_port(i_pak, offp, 0));
180 }
181 
182 void
183 sctp6_notify(struct sctp_inpcb *inp,
184     struct sctp_tcb *stcb,
185     struct sctp_nets *net,
186     uint8_t icmp6_type,
187     uint8_t icmp6_code,
188     uint32_t next_mtu)
189 {
190 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
191 	struct socket *so;
192 #endif
193 	int timer_stopped;
194 
195 	switch (icmp6_type) {
196 	case ICMP6_DST_UNREACH:
197 		if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
198 		    (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
199 		    (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
200 		    (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
201 			/* Mark the net unreachable. */
202 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
203 				/* Ok that destination is not reachable */
204 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
205 				net->dest_state &= ~SCTP_ADDR_PF;
206 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
207 				    stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
208 			}
209 		}
210 		SCTP_TCB_UNLOCK(stcb);
211 		break;
212 	case ICMP6_PARAM_PROB:
213 		/* Treat it like an ABORT. */
214 		if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
215 			sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
216 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
217 			so = SCTP_INP_SO(inp);
218 			atomic_add_int(&stcb->asoc.refcnt, 1);
219 			SCTP_TCB_UNLOCK(stcb);
220 			SCTP_SOCKET_LOCK(so, 1);
221 			SCTP_TCB_LOCK(stcb);
222 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
223 #endif
224 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
225 			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
226 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
227 			SCTP_SOCKET_UNLOCK(so, 1);
228 #endif
229 		} else {
230 			SCTP_TCB_UNLOCK(stcb);
231 		}
232 		break;
233 	case ICMP6_PACKET_TOO_BIG:
234 		if ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0) {
235 			SCTP_TCB_UNLOCK(stcb);
236 			break;
237 		}
238 		if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
239 			timer_stopped = 1;
240 			sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
241 			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
242 		} else {
243 			timer_stopped = 0;
244 		}
245 		/* Update the path MTU. */
246 		if (net->port) {
247 			next_mtu -= sizeof(struct udphdr);
248 		}
249 		if (net->mtu > next_mtu) {
250 			net->mtu = next_mtu;
251 		}
252 		/* Update the association MTU */
253 		if (stcb->asoc.smallest_mtu > next_mtu) {
254 			sctp_pathmtu_adjustment(stcb, next_mtu);
255 		}
256 		/* Finally, start the PMTU timer if it was running before. */
257 		if (timer_stopped) {
258 			sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
259 		}
260 		SCTP_TCB_UNLOCK(stcb);
261 		break;
262 	default:
263 		SCTP_TCB_UNLOCK(stcb);
264 		break;
265 	}
266 }
267 
268 void
269 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
270 {
271 	struct ip6ctlparam *ip6cp;
272 	struct sctp_inpcb *inp;
273 	struct sctp_tcb *stcb;
274 	struct sctp_nets *net;
275 	struct sctphdr sh;
276 	struct sockaddr_in6 src, dst;
277 
278 	if (pktdst->sa_family != AF_INET6 ||
279 	    pktdst->sa_len != sizeof(struct sockaddr_in6)) {
280 		return;
281 	}
282 	if ((unsigned)cmd >= PRC_NCMDS) {
283 		return;
284 	}
285 	if (PRC_IS_REDIRECT(cmd)) {
286 		d = NULL;
287 	} else if (inet6ctlerrmap[cmd] == 0) {
288 		return;
289 	}
290 	/* If the parameter is from icmp6, decode it. */
291 	if (d != NULL) {
292 		ip6cp = (struct ip6ctlparam *)d;
293 	} else {
294 		ip6cp = (struct ip6ctlparam *)NULL;
295 	}
296 
297 	if (ip6cp != NULL) {
298 		/*
299 		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
300 		 * valid.
301 		 */
302 		if (ip6cp->ip6c_m == NULL) {
303 			return;
304 		}
305 		/*
306 		 * Check if we can safely examine the ports and the
307 		 * verification tag of the SCTP common header.
308 		 */
309 		if (ip6cp->ip6c_m->m_pkthdr.len <
310 		    (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
311 			return;
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 	struct in6pcb *inp6;
527 	int error;
528 	struct sctp_inpcb *inp;
529 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
530 
531 	inp = (struct sctp_inpcb *)so->so_pcb;
532 	if (inp != NULL) {
533 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
534 		return (EINVAL);
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 	inp6 = (struct in6pcb *)inp;
548 
549 	inp6->inp_vflag |= INP_IPV6;
550 	inp6->in6p_hops = -1;	/* use kernel default */
551 	inp6->in6p_cksum = -1;	/* just to be sure */
552 #ifdef INET
553 	/*
554 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
555 	 * socket as well, because the socket may be bound to an IPv6
556 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
557 	 */
558 	inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
559 #endif
560 	SCTP_INP_WUNLOCK(inp);
561 	return (0);
562 }
563 
564 static int
565 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
566 {
567 	struct sctp_inpcb *inp;
568 	struct in6pcb *inp6;
569 	int error;
570 
571 	inp = (struct sctp_inpcb *)so->so_pcb;
572 	if (inp == NULL) {
573 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
574 		return (EINVAL);
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 	inp6 = (struct in6pcb *)inp;
600 	inp6->inp_vflag &= ~INP_IPV4;
601 	inp6->inp_vflag |= INP_IPV6;
602 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
603 		switch (addr->sa_family) {
604 #ifdef INET
605 		case AF_INET:
606 			/* binding v4 addr to v6 socket, so reset flags */
607 			inp6->inp_vflag |= INP_IPV4;
608 			inp6->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 					inp6->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 					inp6->inp_vflag |= INP_IPV4;
627 					inp6->inp_vflag &= ~INP_IPV6;
628 					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
629 					return (error);
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 			return (EINVAL);
647 		}
648 #endif
649 		sin6_p = (struct sockaddr_in6 *)addr;
650 
651 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
652 			/* can't bind v4-mapped addrs either! */
653 			/* NOTE: we don't support SIIT */
654 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
655 			return (EINVAL);
656 		}
657 	}
658 	error = sctp_inpcb_bind(so, addr, NULL, p);
659 	return (error);
660 }
661 
662 
663 static void
664 sctp6_close(struct socket *so)
665 {
666 	sctp_close(so);
667 }
668 
669 /* This could be made common with sctp_detach() since they are identical */
670 
671 static
672 int
673 sctp6_disconnect(struct socket *so)
674 {
675 	return (sctp_disconnect(so));
676 }
677 
678 
679 int
680 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
681     struct mbuf *control, struct thread *p);
682 
683 
684 static int
685 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
686     struct mbuf *control, struct thread *p)
687 {
688 	struct sctp_inpcb *inp;
689 	struct in6pcb *inp6;
690 
691 #ifdef INET
692 	struct sockaddr_in6 *sin6;
693 #endif				/* INET */
694 	/* No SPL needed since sctp_output does this */
695 
696 	inp = (struct sctp_inpcb *)so->so_pcb;
697 	if (inp == NULL) {
698 		if (control) {
699 			SCTP_RELEASE_PKT(control);
700 			control = NULL;
701 		}
702 		SCTP_RELEASE_PKT(m);
703 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
704 		return (EINVAL);
705 	}
706 	inp6 = (struct in6pcb *)inp;
707 	/*
708 	 * For the TCP model we may get a NULL addr, if we are a connected
709 	 * socket thats ok.
710 	 */
711 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
712 	    (addr == NULL)) {
713 		goto connected_type;
714 	}
715 	if (addr == NULL) {
716 		SCTP_RELEASE_PKT(m);
717 		if (control) {
718 			SCTP_RELEASE_PKT(control);
719 			control = NULL;
720 		}
721 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
722 		return (EDESTADDRREQ);
723 	}
724 #ifdef INET
725 	sin6 = (struct sockaddr_in6 *)addr;
726 	if (SCTP_IPV6_V6ONLY(inp6)) {
727 		/*
728 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
729 		 * v4 addr or v4-mapped addr
730 		 */
731 		if (addr->sa_family == AF_INET) {
732 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
733 			return (EINVAL);
734 		}
735 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
736 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
737 			return (EINVAL);
738 		}
739 	}
740 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
741 		struct sockaddr_in sin;
742 
743 		/* convert v4-mapped into v4 addr and send */
744 		in6_sin6_2_sin(&sin, sin6);
745 		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
746 	}
747 #endif				/* INET */
748 connected_type:
749 	/* now what about control */
750 	if (control) {
751 		if (inp->control) {
752 			SCTP_PRINTF("huh? control set?\n");
753 			SCTP_RELEASE_PKT(inp->control);
754 			inp->control = NULL;
755 		}
756 		inp->control = control;
757 	}
758 	/* Place the data */
759 	if (inp->pkt) {
760 		SCTP_BUF_NEXT(inp->pkt_last) = m;
761 		inp->pkt_last = m;
762 	} else {
763 		inp->pkt_last = inp->pkt = m;
764 	}
765 	if (
766 	/* FreeBSD and MacOSX uses a flag passed */
767 	    ((flags & PRUS_MORETOCOME) == 0)
768 	    ) {
769 		/*
770 		 * note with the current version this code will only be used
771 		 * by OpenBSD, NetBSD and FreeBSD have methods for
772 		 * re-defining sosend() to use sctp_sosend().  One can
773 		 * optionaly switch back to this code (by changing back the
774 		 * defininitions but this is not advisable.
775 		 */
776 		int ret;
777 
778 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
779 		inp->pkt = NULL;
780 		inp->control = NULL;
781 		return (ret);
782 	} else {
783 		return (0);
784 	}
785 }
786 
787 static int
788 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
789 {
790 	uint32_t vrf_id;
791 	int error = 0;
792 	struct sctp_inpcb *inp;
793 	struct sctp_tcb *stcb;
794 #ifdef INET
795 	struct in6pcb *inp6;
796 	struct sockaddr_in6 *sin6;
797 	union sctp_sockstore store;
798 #endif
799 
800 #ifdef INET
801 	inp6 = (struct in6pcb *)so->so_pcb;
802 #endif
803 	inp = (struct sctp_inpcb *)so->so_pcb;
804 	if (inp == NULL) {
805 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
806 		return (ECONNRESET);	/* I made the same as TCP since we are
807 					 * not setup? */
808 	}
809 	if (addr == NULL) {
810 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
811 		return (EINVAL);
812 	}
813 	switch (addr->sa_family) {
814 #ifdef INET
815 	case AF_INET:
816 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
817 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
818 			return (EINVAL);
819 		}
820 		break;
821 #endif
822 #ifdef INET6
823 	case AF_INET6:
824 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
825 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
826 			return (EINVAL);
827 		}
828 		break;
829 #endif
830 	default:
831 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
832 		return (EINVAL);
833 	}
834 
835 	vrf_id = inp->def_vrf_id;
836 	SCTP_ASOC_CREATE_LOCK(inp);
837 	SCTP_INP_RLOCK(inp);
838 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
839 	    SCTP_PCB_FLAGS_UNBOUND) {
840 		/* Bind a ephemeral port */
841 		SCTP_INP_RUNLOCK(inp);
842 		error = sctp6_bind(so, NULL, p);
843 		if (error) {
844 			SCTP_ASOC_CREATE_UNLOCK(inp);
845 
846 			return (error);
847 		}
848 		SCTP_INP_RLOCK(inp);
849 	}
850 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
851 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
852 		/* We are already connected AND the TCP model */
853 		SCTP_INP_RUNLOCK(inp);
854 		SCTP_ASOC_CREATE_UNLOCK(inp);
855 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
856 		return (EADDRINUSE);
857 	}
858 #ifdef INET
859 	sin6 = (struct sockaddr_in6 *)addr;
860 	if (SCTP_IPV6_V6ONLY(inp6)) {
861 		/*
862 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
863 		 * addr or v4-mapped addr
864 		 */
865 		if (addr->sa_family == AF_INET) {
866 			SCTP_INP_RUNLOCK(inp);
867 			SCTP_ASOC_CREATE_UNLOCK(inp);
868 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
869 			return (EINVAL);
870 		}
871 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
872 			SCTP_INP_RUNLOCK(inp);
873 			SCTP_ASOC_CREATE_UNLOCK(inp);
874 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
875 			return (EINVAL);
876 		}
877 	}
878 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
879 		/* convert v4-mapped into v4 addr */
880 		in6_sin6_2_sin(&store.sin, sin6);
881 		addr = &store.sa;
882 	}
883 #endif				/* INET */
884 	/* Now do we connect? */
885 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
886 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
887 		if (stcb) {
888 			SCTP_TCB_LOCK(stcb);
889 		}
890 		SCTP_INP_RUNLOCK(inp);
891 	} else {
892 		SCTP_INP_RUNLOCK(inp);
893 		SCTP_INP_WLOCK(inp);
894 		SCTP_INP_INCR_REF(inp);
895 		SCTP_INP_WUNLOCK(inp);
896 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
897 		if (stcb == NULL) {
898 			SCTP_INP_WLOCK(inp);
899 			SCTP_INP_DECR_REF(inp);
900 			SCTP_INP_WUNLOCK(inp);
901 		}
902 	}
903 
904 	if (stcb != NULL) {
905 		/* Already have or am bring up an association */
906 		SCTP_ASOC_CREATE_UNLOCK(inp);
907 		SCTP_TCB_UNLOCK(stcb);
908 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
909 		return (EALREADY);
910 	}
911 	/* We are GOOD to go */
912 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
913 	    inp->sctp_ep.pre_open_stream_count,
914 	    inp->sctp_ep.port, p);
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 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
926 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
927 
928 	/* initialize authentication parameters for the assoc */
929 	sctp_initialize_auth_params(inp, stcb);
930 
931 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
932 	SCTP_TCB_UNLOCK(stcb);
933 	return (error);
934 }
935 
936 static int
937 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
938 {
939 	struct sockaddr_in6 *sin6;
940 	struct sctp_inpcb *inp;
941 	uint32_t vrf_id;
942 	struct sctp_ifa *sctp_ifa;
943 
944 	int error;
945 
946 	/*
947 	 * Do the malloc first in case it blocks.
948 	 */
949 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
950 	if (sin6 == NULL)
951 		return (ENOMEM);
952 	sin6->sin6_family = AF_INET6;
953 	sin6->sin6_len = sizeof(*sin6);
954 
955 	inp = (struct sctp_inpcb *)so->so_pcb;
956 	if (inp == NULL) {
957 		SCTP_FREE_SONAME(sin6);
958 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
959 		return (ECONNRESET);
960 	}
961 	SCTP_INP_RLOCK(inp);
962 	sin6->sin6_port = inp->sctp_lport;
963 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
964 		/* For the bound all case you get back 0 */
965 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
966 			struct sctp_tcb *stcb;
967 			struct sockaddr_in6 *sin_a6;
968 			struct sctp_nets *net;
969 			int fnd;
970 
971 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
972 			if (stcb == NULL) {
973 				SCTP_INP_RUNLOCK(inp);
974 				SCTP_FREE_SONAME(sin6);
975 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
976 				return (ENOENT);
977 			}
978 			fnd = 0;
979 			sin_a6 = NULL;
980 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
981 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
982 				if (sin_a6 == NULL)
983 					/* this will make coverity happy */
984 					continue;
985 
986 				if (sin_a6->sin6_family == AF_INET6) {
987 					fnd = 1;
988 					break;
989 				}
990 			}
991 			if ((!fnd) || (sin_a6 == NULL)) {
992 				/* punt */
993 				SCTP_INP_RUNLOCK(inp);
994 				SCTP_FREE_SONAME(sin6);
995 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
996 				return (ENOENT);
997 			}
998 			vrf_id = inp->def_vrf_id;
999 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1000 			if (sctp_ifa) {
1001 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1002 			}
1003 		} else {
1004 			/* For the bound all case you get back 0 */
1005 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1006 		}
1007 	} else {
1008 		/* Take the first IPv6 address in the list */
1009 		struct sctp_laddr *laddr;
1010 		int fnd = 0;
1011 
1012 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1013 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1014 				struct sockaddr_in6 *sin_a;
1015 
1016 				sin_a = &laddr->ifa->address.sin6;
1017 				sin6->sin6_addr = sin_a->sin6_addr;
1018 				fnd = 1;
1019 				break;
1020 			}
1021 		}
1022 		if (!fnd) {
1023 			SCTP_FREE_SONAME(sin6);
1024 			SCTP_INP_RUNLOCK(inp);
1025 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1026 			return (ENOENT);
1027 		}
1028 	}
1029 	SCTP_INP_RUNLOCK(inp);
1030 	/* Scoping things for v6 */
1031 	if ((error = sa6_recoverscope(sin6)) != 0) {
1032 		SCTP_FREE_SONAME(sin6);
1033 		return (error);
1034 	}
1035 	(*addr) = (struct sockaddr *)sin6;
1036 	return (0);
1037 }
1038 
1039 static int
1040 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1041 {
1042 	struct sockaddr_in6 *sin6;
1043 	int fnd;
1044 	struct sockaddr_in6 *sin_a6;
1045 	struct sctp_inpcb *inp;
1046 	struct sctp_tcb *stcb;
1047 	struct sctp_nets *net;
1048 	int error;
1049 
1050 	/* Do the malloc first in case it blocks. */
1051 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1052 	if (sin6 == NULL)
1053 		return (ENOMEM);
1054 	sin6->sin6_family = AF_INET6;
1055 	sin6->sin6_len = sizeof(*sin6);
1056 
1057 	inp = (struct sctp_inpcb *)so->so_pcb;
1058 	if ((inp == NULL) ||
1059 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1060 		/* UDP type and listeners will drop out here */
1061 		SCTP_FREE_SONAME(sin6);
1062 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1063 		return (ENOTCONN);
1064 	}
1065 	SCTP_INP_RLOCK(inp);
1066 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1067 	if (stcb) {
1068 		SCTP_TCB_LOCK(stcb);
1069 	}
1070 	SCTP_INP_RUNLOCK(inp);
1071 	if (stcb == NULL) {
1072 		SCTP_FREE_SONAME(sin6);
1073 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1074 		return (ECONNRESET);
1075 	}
1076 	fnd = 0;
1077 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1078 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1079 		if (sin_a6->sin6_family == AF_INET6) {
1080 			fnd = 1;
1081 			sin6->sin6_port = stcb->rport;
1082 			sin6->sin6_addr = sin_a6->sin6_addr;
1083 			break;
1084 		}
1085 	}
1086 	SCTP_TCB_UNLOCK(stcb);
1087 	if (!fnd) {
1088 		/* No IPv4 address */
1089 		SCTP_FREE_SONAME(sin6);
1090 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1091 		return (ENOENT);
1092 	}
1093 	if ((error = sa6_recoverscope(sin6)) != 0) {
1094 		SCTP_FREE_SONAME(sin6);
1095 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1096 		return (error);
1097 	}
1098 	*addr = (struct sockaddr *)sin6;
1099 	return (0);
1100 }
1101 
1102 static int
1103 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1104 {
1105 	struct in6pcb *inp6 = sotoin6pcb(so);
1106 	int error;
1107 
1108 	if (inp6 == NULL) {
1109 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1110 		return (EINVAL);
1111 	}
1112 	/* allow v6 addresses precedence */
1113 	error = sctp6_getaddr(so, nam);
1114 #ifdef INET
1115 	if (error) {
1116 		struct sockaddr_in6 *sin6;
1117 
1118 		/* try v4 next if v6 failed */
1119 		error = sctp_ingetaddr(so, nam);
1120 		if (error) {
1121 			return (error);
1122 		}
1123 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1124 		if (sin6 == NULL) {
1125 			SCTP_FREE_SONAME(*nam);
1126 			return (ENOMEM);
1127 		}
1128 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1129 		SCTP_FREE_SONAME(*nam);
1130 		*nam = (struct sockaddr *)sin6;
1131 	}
1132 #endif
1133 	return (error);
1134 }
1135 
1136 
1137 static int
1138 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1139 {
1140 	struct in6pcb *inp6 = sotoin6pcb(so);
1141 	int error;
1142 
1143 	if (inp6 == NULL) {
1144 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1145 		return (EINVAL);
1146 	}
1147 	/* allow v6 addresses precedence */
1148 	error = sctp6_peeraddr(so, nam);
1149 #ifdef INET
1150 	if (error) {
1151 		struct sockaddr_in6 *sin6;
1152 
1153 		/* try v4 next if v6 failed */
1154 		error = sctp_peeraddr(so, nam);
1155 		if (error) {
1156 			return (error);
1157 		}
1158 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1159 		if (sin6 == NULL) {
1160 			SCTP_FREE_SONAME(*nam);
1161 			return (ENOMEM);
1162 		}
1163 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1164 		SCTP_FREE_SONAME(*nam);
1165 		*nam = (struct sockaddr *)sin6;
1166 	}
1167 #endif
1168 	return (error);
1169 }
1170 
1171 struct pr_usrreqs sctp6_usrreqs = {
1172 	.pru_abort = sctp6_abort,
1173 	.pru_accept = sctp_accept,
1174 	.pru_attach = sctp6_attach,
1175 	.pru_bind = sctp6_bind,
1176 	.pru_connect = sctp6_connect,
1177 	.pru_control = in6_control,
1178 	.pru_close = sctp6_close,
1179 	.pru_detach = sctp6_close,
1180 	.pru_sopoll = sopoll_generic,
1181 	.pru_flush = sctp_flush,
1182 	.pru_disconnect = sctp6_disconnect,
1183 	.pru_listen = sctp_listen,
1184 	.pru_peeraddr = sctp6_getpeeraddr,
1185 	.pru_send = sctp6_send,
1186 	.pru_shutdown = sctp_shutdown,
1187 	.pru_sockaddr = sctp6_in6getaddr,
1188 	.pru_sosend = sctp_sosend,
1189 	.pru_soreceive = sctp_soreceive
1190 };
1191 
1192 #endif
1193