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