xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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 	uint8_t compute_crc;
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 (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 		}
244 		/* Update the association MTU */
245 		if (stcb->asoc.smallest_mtu > next_mtu) {
246 			sctp_pathmtu_adjustment(stcb, next_mtu);
247 		}
248 		/* Finally, start the PMTU timer if it was running before. */
249 		if (timer_stopped) {
250 			sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
251 		}
252 		SCTP_TCB_UNLOCK(stcb);
253 		break;
254 	default:
255 		SCTP_TCB_UNLOCK(stcb);
256 		break;
257 	}
258 }
259 
260 void
261 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
262 {
263 	struct ip6ctlparam *ip6cp;
264 	struct sctp_inpcb *inp;
265 	struct sctp_tcb *stcb;
266 	struct sctp_nets *net;
267 	struct sctphdr sh;
268 	struct sockaddr_in6 src, dst;
269 
270 	if (pktdst->sa_family != AF_INET6 ||
271 	    pktdst->sa_len != sizeof(struct sockaddr_in6)) {
272 		return;
273 	}
274 	if ((unsigned)cmd >= PRC_NCMDS) {
275 		return;
276 	}
277 	if (PRC_IS_REDIRECT(cmd)) {
278 		d = NULL;
279 	} else if (inet6ctlerrmap[cmd] == 0) {
280 		return;
281 	}
282 	/* If the parameter is from icmp6, decode it. */
283 	if (d != NULL) {
284 		ip6cp = (struct ip6ctlparam *)d;
285 	} else {
286 		ip6cp = (struct ip6ctlparam *)NULL;
287 	}
288 
289 	if (ip6cp != NULL) {
290 		/*
291 		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
292 		 * valid.
293 		 */
294 		if (ip6cp->ip6c_m == NULL) {
295 			return;
296 		}
297 		/*
298 		 * Check if we can safely examine the ports and the
299 		 * verification tag of the SCTP common header.
300 		 */
301 		if (ip6cp->ip6c_m->m_pkthdr.len <
302 		    (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
303 			return;
304 		}
305 		/* Copy out the port numbers and the verification tag. */
306 		memset(&sh, 0, sizeof(sh));
307 		m_copydata(ip6cp->ip6c_m,
308 		    ip6cp->ip6c_off,
309 		    sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
310 		    (caddr_t)&sh);
311 		memset(&src, 0, sizeof(struct sockaddr_in6));
312 		src.sin6_family = AF_INET6;
313 		src.sin6_len = sizeof(struct sockaddr_in6);
314 		src.sin6_port = sh.src_port;
315 		src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
316 		if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
317 			return;
318 		}
319 		memset(&dst, 0, sizeof(struct sockaddr_in6));
320 		dst.sin6_family = AF_INET6;
321 		dst.sin6_len = sizeof(struct sockaddr_in6);
322 		dst.sin6_port = sh.dest_port;
323 		dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
324 		if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
325 			return;
326 		}
327 		inp = NULL;
328 		net = NULL;
329 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
330 		    (struct sockaddr *)&src,
331 		    &inp, &net, 1, SCTP_DEFAULT_VRFID);
332 		if ((stcb != NULL) &&
333 		    (net != NULL) &&
334 		    (inp != NULL)) {
335 			/* Check the verification tag */
336 			if (ntohl(sh.v_tag) != 0) {
337 				/*
338 				 * This must be the verification tag used
339 				 * for sending out packets. We don't
340 				 * consider packets reflecting the
341 				 * verification tag.
342 				 */
343 				if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
344 					SCTP_TCB_UNLOCK(stcb);
345 					return;
346 				}
347 			} else {
348 				if (ip6cp->ip6c_m->m_pkthdr.len >=
349 				    ip6cp->ip6c_off + sizeof(struct sctphdr) +
350 				    sizeof(struct sctp_chunkhdr) +
351 				    offsetof(struct sctp_init, a_rwnd)) {
352 					/*
353 					 * In this case we can check if we
354 					 * got an INIT chunk and if the
355 					 * initiate tag matches.
356 					 */
357 					uint32_t initiate_tag;
358 					uint8_t chunk_type;
359 
360 					m_copydata(ip6cp->ip6c_m,
361 					    ip6cp->ip6c_off +
362 					    sizeof(struct sctphdr),
363 					    sizeof(uint8_t),
364 					    (caddr_t)&chunk_type);
365 					m_copydata(ip6cp->ip6c_m,
366 					    ip6cp->ip6c_off +
367 					    sizeof(struct sctphdr) +
368 					    sizeof(struct sctp_chunkhdr),
369 					    sizeof(uint32_t),
370 					    (caddr_t)&initiate_tag);
371 					if ((chunk_type != SCTP_INITIATION) ||
372 					    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
373 						SCTP_TCB_UNLOCK(stcb);
374 						return;
375 					}
376 				} else {
377 					SCTP_TCB_UNLOCK(stcb);
378 					return;
379 				}
380 			}
381 			sctp6_notify(inp, stcb, net,
382 			    ip6cp->ip6c_icmp6->icmp6_type,
383 			    ip6cp->ip6c_icmp6->icmp6_code,
384 			    ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
385 		} else {
386 			if ((stcb == NULL) && (inp != NULL)) {
387 				/* reduce inp's ref-count */
388 				SCTP_INP_WLOCK(inp);
389 				SCTP_INP_DECR_REF(inp);
390 				SCTP_INP_WUNLOCK(inp);
391 			}
392 			if (stcb) {
393 				SCTP_TCB_UNLOCK(stcb);
394 			}
395 		}
396 	}
397 }
398 
399 /*
400  * this routine can probably be collasped into the one in sctp_userreq.c
401  * since they do the same thing and now we lookup with a sockaddr
402  */
403 static int
404 sctp6_getcred(SYSCTL_HANDLER_ARGS)
405 {
406 	struct xucred xuc;
407 	struct sockaddr_in6 addrs[2];
408 	struct sctp_inpcb *inp;
409 	struct sctp_nets *net;
410 	struct sctp_tcb *stcb;
411 	int error;
412 	uint32_t vrf_id;
413 
414 	vrf_id = SCTP_DEFAULT_VRFID;
415 
416 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
417 	if (error)
418 		return (error);
419 
420 	if (req->newlen != sizeof(addrs)) {
421 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
422 		return (EINVAL);
423 	}
424 	if (req->oldlen != sizeof(struct ucred)) {
425 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
426 		return (EINVAL);
427 	}
428 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
429 	if (error)
430 		return (error);
431 
432 	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
433 	    sin6tosa(&addrs[0]),
434 	    &inp, &net, 1, vrf_id);
435 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
436 		if ((inp != NULL) && (stcb == NULL)) {
437 			/* reduce ref-count */
438 			SCTP_INP_WLOCK(inp);
439 			SCTP_INP_DECR_REF(inp);
440 			goto cred_can_cont;
441 		}
442 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
443 		error = ENOENT;
444 		goto out;
445 	}
446 	SCTP_TCB_UNLOCK(stcb);
447 	/*
448 	 * We use the write lock here, only since in the error leg we need
449 	 * it. If we used RLOCK, then we would have to
450 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
451 	 * Better to use higher wlock.
452 	 */
453 	SCTP_INP_WLOCK(inp);
454 cred_can_cont:
455 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
456 	if (error) {
457 		SCTP_INP_WUNLOCK(inp);
458 		goto out;
459 	}
460 	cru2x(inp->sctp_socket->so_cred, &xuc);
461 	SCTP_INP_WUNLOCK(inp);
462 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
463 out:
464 	return (error);
465 }
466 
467 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
468     0, 0,
469     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
470 
471 
472 /* This is the same as the sctp_abort() could be made common */
473 static void
474 sctp6_abort(struct socket *so)
475 {
476 	struct sctp_inpcb *inp;
477 	uint32_t flags;
478 
479 	inp = (struct sctp_inpcb *)so->so_pcb;
480 	if (inp == NULL) {
481 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
482 		return;
483 	}
484 sctp_must_try_again:
485 	flags = inp->sctp_flags;
486 #ifdef SCTP_LOG_CLOSING
487 	sctp_log_closing(inp, NULL, 17);
488 #endif
489 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
490 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
491 #ifdef SCTP_LOG_CLOSING
492 		sctp_log_closing(inp, NULL, 16);
493 #endif
494 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
495 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
496 		SOCK_LOCK(so);
497 		SCTP_SB_CLEAR(so->so_snd);
498 		/*
499 		 * same for the rcv ones, they are only here for the
500 		 * accounting/select.
501 		 */
502 		SCTP_SB_CLEAR(so->so_rcv);
503 		/* Now null out the reference, we are completely detached. */
504 		so->so_pcb = NULL;
505 		SOCK_UNLOCK(so);
506 	} else {
507 		flags = inp->sctp_flags;
508 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
509 			goto sctp_must_try_again;
510 		}
511 	}
512 	return;
513 }
514 
515 static int
516 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
517 {
518 	struct in6pcb *inp6;
519 	int error;
520 	struct sctp_inpcb *inp;
521 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
522 
523 	inp = (struct sctp_inpcb *)so->so_pcb;
524 	if (inp != NULL) {
525 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
526 		return (EINVAL);
527 	}
528 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
529 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
530 		if (error)
531 			return (error);
532 	}
533 	error = sctp_inpcb_alloc(so, vrf_id);
534 	if (error)
535 		return (error);
536 	inp = (struct sctp_inpcb *)so->so_pcb;
537 	SCTP_INP_WLOCK(inp);
538 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
539 	inp6 = (struct in6pcb *)inp;
540 
541 	inp6->inp_vflag |= INP_IPV6;
542 	inp6->in6p_hops = -1;	/* use kernel default */
543 	inp6->in6p_cksum = -1;	/* just to be sure */
544 #ifdef INET
545 	/*
546 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
547 	 * socket as well, because the socket may be bound to an IPv6
548 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
549 	 */
550 	inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
551 #endif
552 	SCTP_INP_WUNLOCK(inp);
553 	return (0);
554 }
555 
556 static int
557 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
558 {
559 	struct sctp_inpcb *inp;
560 	struct in6pcb *inp6;
561 	int error;
562 
563 	inp = (struct sctp_inpcb *)so->so_pcb;
564 	if (inp == NULL) {
565 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
566 		return (EINVAL);
567 	}
568 	if (addr) {
569 		switch (addr->sa_family) {
570 #ifdef INET
571 		case AF_INET:
572 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
573 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
574 				return (EINVAL);
575 			}
576 			break;
577 #endif
578 #ifdef INET6
579 		case AF_INET6:
580 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
581 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
582 				return (EINVAL);
583 			}
584 			break;
585 #endif
586 		default:
587 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
588 			return (EINVAL);
589 		}
590 	}
591 	inp6 = (struct in6pcb *)inp;
592 	inp6->inp_vflag &= ~INP_IPV4;
593 	inp6->inp_vflag |= INP_IPV6;
594 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
595 		switch (addr->sa_family) {
596 #ifdef INET
597 		case AF_INET:
598 			/* binding v4 addr to v6 socket, so reset flags */
599 			inp6->inp_vflag |= INP_IPV4;
600 			inp6->inp_vflag &= ~INP_IPV6;
601 			break;
602 #endif
603 #ifdef INET6
604 		case AF_INET6:
605 			{
606 				struct sockaddr_in6 *sin6_p;
607 
608 				sin6_p = (struct sockaddr_in6 *)addr;
609 
610 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
611 					inp6->inp_vflag |= INP_IPV4;
612 				}
613 #ifdef INET
614 				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
615 					struct sockaddr_in sin;
616 
617 					in6_sin6_2_sin(&sin, sin6_p);
618 					inp6->inp_vflag |= INP_IPV4;
619 					inp6->inp_vflag &= ~INP_IPV6;
620 					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
621 					return (error);
622 				}
623 #endif
624 				break;
625 			}
626 #endif
627 		default:
628 			break;
629 		}
630 	} else if (addr != NULL) {
631 		struct sockaddr_in6 *sin6_p;
632 
633 		/* IPV6_V6ONLY socket */
634 #ifdef INET
635 		if (addr->sa_family == AF_INET) {
636 			/* can't bind v4 addr to v6 only socket! */
637 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
638 			return (EINVAL);
639 		}
640 #endif
641 		sin6_p = (struct sockaddr_in6 *)addr;
642 
643 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
644 			/* can't bind v4-mapped addrs either! */
645 			/* NOTE: we don't support SIIT */
646 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
647 			return (EINVAL);
648 		}
649 	}
650 	error = sctp_inpcb_bind(so, addr, NULL, p);
651 	return (error);
652 }
653 
654 
655 static void
656 sctp6_close(struct socket *so)
657 {
658 	sctp_close(so);
659 }
660 
661 /* This could be made common with sctp_detach() since they are identical */
662 
663 static
664 int
665 sctp6_disconnect(struct socket *so)
666 {
667 	return (sctp_disconnect(so));
668 }
669 
670 
671 int
672 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
673     struct mbuf *control, struct thread *p);
674 
675 
676 static int
677 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
678     struct mbuf *control, struct thread *p)
679 {
680 	struct sctp_inpcb *inp;
681 	struct in6pcb *inp6;
682 
683 #ifdef INET
684 	struct sockaddr_in6 *sin6;
685 #endif				/* INET */
686 	/* No SPL needed since sctp_output does this */
687 
688 	inp = (struct sctp_inpcb *)so->so_pcb;
689 	if (inp == NULL) {
690 		if (control) {
691 			SCTP_RELEASE_PKT(control);
692 			control = NULL;
693 		}
694 		SCTP_RELEASE_PKT(m);
695 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
696 		return (EINVAL);
697 	}
698 	inp6 = (struct in6pcb *)inp;
699 	/*
700 	 * For the TCP model we may get a NULL addr, if we are a connected
701 	 * socket thats ok.
702 	 */
703 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
704 	    (addr == NULL)) {
705 		goto connected_type;
706 	}
707 	if (addr == NULL) {
708 		SCTP_RELEASE_PKT(m);
709 		if (control) {
710 			SCTP_RELEASE_PKT(control);
711 			control = NULL;
712 		}
713 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
714 		return (EDESTADDRREQ);
715 	}
716 #ifdef INET
717 	sin6 = (struct sockaddr_in6 *)addr;
718 	if (SCTP_IPV6_V6ONLY(inp6)) {
719 		/*
720 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
721 		 * v4 addr or v4-mapped addr
722 		 */
723 		if (addr->sa_family == AF_INET) {
724 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
725 			return (EINVAL);
726 		}
727 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
728 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
729 			return (EINVAL);
730 		}
731 	}
732 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
733 		struct sockaddr_in sin;
734 
735 		/* convert v4-mapped into v4 addr and send */
736 		in6_sin6_2_sin(&sin, sin6);
737 		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
738 	}
739 #endif				/* INET */
740 connected_type:
741 	/* now what about control */
742 	if (control) {
743 		if (inp->control) {
744 			SCTP_PRINTF("huh? control set?\n");
745 			SCTP_RELEASE_PKT(inp->control);
746 			inp->control = NULL;
747 		}
748 		inp->control = control;
749 	}
750 	/* Place the data */
751 	if (inp->pkt) {
752 		SCTP_BUF_NEXT(inp->pkt_last) = m;
753 		inp->pkt_last = m;
754 	} else {
755 		inp->pkt_last = inp->pkt = m;
756 	}
757 	if (
758 	/* FreeBSD and MacOSX uses a flag passed */
759 	    ((flags & PRUS_MORETOCOME) == 0)
760 	    ) {
761 		/*
762 		 * note with the current version this code will only be used
763 		 * by OpenBSD, NetBSD and FreeBSD have methods for
764 		 * re-defining sosend() to use sctp_sosend().  One can
765 		 * optionaly switch back to this code (by changing back the
766 		 * defininitions but this is not advisable.
767 		 */
768 		int ret;
769 
770 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
771 		inp->pkt = NULL;
772 		inp->control = NULL;
773 		return (ret);
774 	} else {
775 		return (0);
776 	}
777 }
778 
779 static int
780 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
781 {
782 	uint32_t vrf_id;
783 	int error = 0;
784 	struct sctp_inpcb *inp;
785 	struct sctp_tcb *stcb;
786 #ifdef INET
787 	struct in6pcb *inp6;
788 	struct sockaddr_in6 *sin6;
789 	union sctp_sockstore store;
790 #endif
791 
792 #ifdef INET
793 	inp6 = (struct in6pcb *)so->so_pcb;
794 #endif
795 	inp = (struct sctp_inpcb *)so->so_pcb;
796 	if (inp == NULL) {
797 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
798 		return (ECONNRESET);	/* I made the same as TCP since we are
799 					 * not setup? */
800 	}
801 	if (addr == NULL) {
802 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
803 		return (EINVAL);
804 	}
805 	switch (addr->sa_family) {
806 #ifdef INET
807 	case AF_INET:
808 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
809 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
810 			return (EINVAL);
811 		}
812 		break;
813 #endif
814 #ifdef INET6
815 	case AF_INET6:
816 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
817 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
818 			return (EINVAL);
819 		}
820 		break;
821 #endif
822 	default:
823 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
824 		return (EINVAL);
825 	}
826 
827 	vrf_id = inp->def_vrf_id;
828 	SCTP_ASOC_CREATE_LOCK(inp);
829 	SCTP_INP_RLOCK(inp);
830 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
831 	    SCTP_PCB_FLAGS_UNBOUND) {
832 		/* Bind a ephemeral port */
833 		SCTP_INP_RUNLOCK(inp);
834 		error = sctp6_bind(so, NULL, p);
835 		if (error) {
836 			SCTP_ASOC_CREATE_UNLOCK(inp);
837 
838 			return (error);
839 		}
840 		SCTP_INP_RLOCK(inp);
841 	}
842 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
843 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
844 		/* We are already connected AND the TCP model */
845 		SCTP_INP_RUNLOCK(inp);
846 		SCTP_ASOC_CREATE_UNLOCK(inp);
847 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
848 		return (EADDRINUSE);
849 	}
850 #ifdef INET
851 	sin6 = (struct sockaddr_in6 *)addr;
852 	if (SCTP_IPV6_V6ONLY(inp6)) {
853 		/*
854 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
855 		 * addr or v4-mapped addr
856 		 */
857 		if (addr->sa_family == AF_INET) {
858 			SCTP_INP_RUNLOCK(inp);
859 			SCTP_ASOC_CREATE_UNLOCK(inp);
860 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
861 			return (EINVAL);
862 		}
863 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
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 	}
870 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
871 		/* convert v4-mapped into v4 addr */
872 		in6_sin6_2_sin(&store.sin, sin6);
873 		addr = &store.sa;
874 	}
875 #endif				/* INET */
876 	/* Now do we connect? */
877 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
878 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
879 		if (stcb) {
880 			SCTP_TCB_LOCK(stcb);
881 		}
882 		SCTP_INP_RUNLOCK(inp);
883 	} else {
884 		SCTP_INP_RUNLOCK(inp);
885 		SCTP_INP_WLOCK(inp);
886 		SCTP_INP_INCR_REF(inp);
887 		SCTP_INP_WUNLOCK(inp);
888 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
889 		if (stcb == NULL) {
890 			SCTP_INP_WLOCK(inp);
891 			SCTP_INP_DECR_REF(inp);
892 			SCTP_INP_WUNLOCK(inp);
893 		}
894 	}
895 
896 	if (stcb != NULL) {
897 		/* Already have or am bring up an association */
898 		SCTP_ASOC_CREATE_UNLOCK(inp);
899 		SCTP_TCB_UNLOCK(stcb);
900 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
901 		return (EALREADY);
902 	}
903 	/* We are GOOD to go */
904 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
905 	    inp->sctp_ep.pre_open_stream_count,
906 	    inp->sctp_ep.port, p);
907 	SCTP_ASOC_CREATE_UNLOCK(inp);
908 	if (stcb == NULL) {
909 		/* Gak! no memory */
910 		return (error);
911 	}
912 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
913 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
914 		/* Set the connected flag so we can queue data */
915 		soisconnecting(so);
916 	}
917 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
918 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
919 
920 	/* initialize authentication parameters for the assoc */
921 	sctp_initialize_auth_params(inp, stcb);
922 
923 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
924 	SCTP_TCB_UNLOCK(stcb);
925 	return (error);
926 }
927 
928 static int
929 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
930 {
931 	struct sockaddr_in6 *sin6;
932 	struct sctp_inpcb *inp;
933 	uint32_t vrf_id;
934 	struct sctp_ifa *sctp_ifa;
935 
936 	int error;
937 
938 	/*
939 	 * Do the malloc first in case it blocks.
940 	 */
941 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
942 	if (sin6 == NULL)
943 		return (ENOMEM);
944 	sin6->sin6_family = AF_INET6;
945 	sin6->sin6_len = sizeof(*sin6);
946 
947 	inp = (struct sctp_inpcb *)so->so_pcb;
948 	if (inp == NULL) {
949 		SCTP_FREE_SONAME(sin6);
950 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
951 		return (ECONNRESET);
952 	}
953 	SCTP_INP_RLOCK(inp);
954 	sin6->sin6_port = inp->sctp_lport;
955 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
956 		/* For the bound all case you get back 0 */
957 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
958 			struct sctp_tcb *stcb;
959 			struct sockaddr_in6 *sin_a6;
960 			struct sctp_nets *net;
961 			int fnd;
962 
963 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
964 			if (stcb == NULL) {
965 				SCTP_INP_RUNLOCK(inp);
966 				SCTP_FREE_SONAME(sin6);
967 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
968 				return (ENOENT);
969 			}
970 			fnd = 0;
971 			sin_a6 = NULL;
972 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
973 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
974 				if (sin_a6 == NULL)
975 					/* this will make coverity happy */
976 					continue;
977 
978 				if (sin_a6->sin6_family == AF_INET6) {
979 					fnd = 1;
980 					break;
981 				}
982 			}
983 			if ((!fnd) || (sin_a6 == NULL)) {
984 				/* punt */
985 				SCTP_INP_RUNLOCK(inp);
986 				SCTP_FREE_SONAME(sin6);
987 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
988 				return (ENOENT);
989 			}
990 			vrf_id = inp->def_vrf_id;
991 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
992 			if (sctp_ifa) {
993 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
994 			}
995 		} else {
996 			/* For the bound all case you get back 0 */
997 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
998 		}
999 	} else {
1000 		/* Take the first IPv6 address in the list */
1001 		struct sctp_laddr *laddr;
1002 		int fnd = 0;
1003 
1004 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1005 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1006 				struct sockaddr_in6 *sin_a;
1007 
1008 				sin_a = &laddr->ifa->address.sin6;
1009 				sin6->sin6_addr = sin_a->sin6_addr;
1010 				fnd = 1;
1011 				break;
1012 			}
1013 		}
1014 		if (!fnd) {
1015 			SCTP_FREE_SONAME(sin6);
1016 			SCTP_INP_RUNLOCK(inp);
1017 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1018 			return (ENOENT);
1019 		}
1020 	}
1021 	SCTP_INP_RUNLOCK(inp);
1022 	/* Scoping things for v6 */
1023 	if ((error = sa6_recoverscope(sin6)) != 0) {
1024 		SCTP_FREE_SONAME(sin6);
1025 		return (error);
1026 	}
1027 	(*addr) = (struct sockaddr *)sin6;
1028 	return (0);
1029 }
1030 
1031 static int
1032 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1033 {
1034 	struct sockaddr_in6 *sin6;
1035 	int fnd;
1036 	struct sockaddr_in6 *sin_a6;
1037 	struct sctp_inpcb *inp;
1038 	struct sctp_tcb *stcb;
1039 	struct sctp_nets *net;
1040 	int error;
1041 
1042 	/* Do the malloc first in case it blocks. */
1043 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1044 	if (sin6 == NULL)
1045 		return (ENOMEM);
1046 	sin6->sin6_family = AF_INET6;
1047 	sin6->sin6_len = sizeof(*sin6);
1048 
1049 	inp = (struct sctp_inpcb *)so->so_pcb;
1050 	if ((inp == NULL) ||
1051 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1052 		/* UDP type and listeners will drop out here */
1053 		SCTP_FREE_SONAME(sin6);
1054 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1055 		return (ENOTCONN);
1056 	}
1057 	SCTP_INP_RLOCK(inp);
1058 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1059 	if (stcb) {
1060 		SCTP_TCB_LOCK(stcb);
1061 	}
1062 	SCTP_INP_RUNLOCK(inp);
1063 	if (stcb == NULL) {
1064 		SCTP_FREE_SONAME(sin6);
1065 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1066 		return (ECONNRESET);
1067 	}
1068 	fnd = 0;
1069 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1070 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1071 		if (sin_a6->sin6_family == AF_INET6) {
1072 			fnd = 1;
1073 			sin6->sin6_port = stcb->rport;
1074 			sin6->sin6_addr = sin_a6->sin6_addr;
1075 			break;
1076 		}
1077 	}
1078 	SCTP_TCB_UNLOCK(stcb);
1079 	if (!fnd) {
1080 		/* No IPv4 address */
1081 		SCTP_FREE_SONAME(sin6);
1082 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1083 		return (ENOENT);
1084 	}
1085 	if ((error = sa6_recoverscope(sin6)) != 0) {
1086 		SCTP_FREE_SONAME(sin6);
1087 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1088 		return (error);
1089 	}
1090 	*addr = (struct sockaddr *)sin6;
1091 	return (0);
1092 }
1093 
1094 static int
1095 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1096 {
1097 	struct in6pcb *inp6 = sotoin6pcb(so);
1098 	int error;
1099 
1100 	if (inp6 == NULL) {
1101 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1102 		return (EINVAL);
1103 	}
1104 	/* allow v6 addresses precedence */
1105 	error = sctp6_getaddr(so, nam);
1106 #ifdef INET
1107 	if (error) {
1108 		struct sockaddr_in6 *sin6;
1109 
1110 		/* try v4 next if v6 failed */
1111 		error = sctp_ingetaddr(so, nam);
1112 		if (error) {
1113 			return (error);
1114 		}
1115 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1116 		if (sin6 == NULL) {
1117 			SCTP_FREE_SONAME(*nam);
1118 			return (ENOMEM);
1119 		}
1120 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1121 		SCTP_FREE_SONAME(*nam);
1122 		*nam = (struct sockaddr *)sin6;
1123 	}
1124 #endif
1125 	return (error);
1126 }
1127 
1128 
1129 static int
1130 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1131 {
1132 	struct in6pcb *inp6 = sotoin6pcb(so);
1133 	int error;
1134 
1135 	if (inp6 == NULL) {
1136 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1137 		return (EINVAL);
1138 	}
1139 	/* allow v6 addresses precedence */
1140 	error = sctp6_peeraddr(so, nam);
1141 #ifdef INET
1142 	if (error) {
1143 		struct sockaddr_in6 *sin6;
1144 
1145 		/* try v4 next if v6 failed */
1146 		error = sctp_peeraddr(so, nam);
1147 		if (error) {
1148 			return (error);
1149 		}
1150 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1151 		if (sin6 == NULL) {
1152 			SCTP_FREE_SONAME(*nam);
1153 			return (ENOMEM);
1154 		}
1155 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1156 		SCTP_FREE_SONAME(*nam);
1157 		*nam = (struct sockaddr *)sin6;
1158 	}
1159 #endif
1160 	return (error);
1161 }
1162 
1163 struct pr_usrreqs sctp6_usrreqs = {
1164 	.pru_abort = sctp6_abort,
1165 	.pru_accept = sctp_accept,
1166 	.pru_attach = sctp6_attach,
1167 	.pru_bind = sctp6_bind,
1168 	.pru_connect = sctp6_connect,
1169 	.pru_control = in6_control,
1170 	.pru_close = sctp6_close,
1171 	.pru_detach = sctp6_close,
1172 	.pru_sopoll = sopoll_generic,
1173 	.pru_flush = sctp_flush,
1174 	.pru_disconnect = sctp6_disconnect,
1175 	.pru_listen = sctp_listen,
1176 	.pru_peeraddr = sctp6_getpeeraddr,
1177 	.pru_send = sctp6_send,
1178 	.pru_shutdown = sctp_shutdown,
1179 	.pru_sockaddr = sctp6_in6getaddr,
1180 	.pru_sosend = sctp_sosend,
1181 	.pru_soreceive = sctp_soreceive
1182 };
1183 
1184 #endif
1185