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