xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision 058ac3e8063366dafa634d9107642e12b038bf09)
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 /* This is the same as the sctp_abort() could be made common */
438 static void
439 sctp6_abort(struct socket *so)
440 {
441 	struct epoch_tracker et;
442 	struct sctp_inpcb *inp;
443 	uint32_t flags;
444 
445 	inp = (struct sctp_inpcb *)so->so_pcb;
446 	if (inp == NULL) {
447 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
448 		return;
449 	}
450 	NET_EPOCH_ENTER(et);
451 sctp_must_try_again:
452 	flags = inp->sctp_flags;
453 #ifdef SCTP_LOG_CLOSING
454 	sctp_log_closing(inp, NULL, 17);
455 #endif
456 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
457 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
458 #ifdef SCTP_LOG_CLOSING
459 		sctp_log_closing(inp, NULL, 16);
460 #endif
461 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
462 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
463 		SOCK_LOCK(so);
464 		SCTP_SB_CLEAR(so->so_snd);
465 		/*
466 		 * same for the rcv ones, they are only here for the
467 		 * accounting/select.
468 		 */
469 		SCTP_SB_CLEAR(so->so_rcv);
470 		/* Now null out the reference, we are completely detached. */
471 		so->so_pcb = NULL;
472 		SOCK_UNLOCK(so);
473 	} else {
474 		flags = inp->sctp_flags;
475 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
476 			goto sctp_must_try_again;
477 		}
478 	}
479 	NET_EPOCH_EXIT(et);
480 	return;
481 }
482 
483 static int
484 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
485 {
486 	int error;
487 	struct sctp_inpcb *inp;
488 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
489 
490 	inp = (struct sctp_inpcb *)so->so_pcb;
491 	if (inp != NULL) {
492 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
493 		return (EINVAL);
494 	}
495 
496 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
497 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
498 		if (error)
499 			return (error);
500 	}
501 	error = sctp_inpcb_alloc(so, vrf_id);
502 	if (error)
503 		return (error);
504 	inp = (struct sctp_inpcb *)so->so_pcb;
505 	SCTP_INP_WLOCK(inp);
506 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
507 
508 	inp->ip_inp.inp.inp_vflag |= INP_IPV6;
509 	inp->ip_inp.inp.in6p_hops = -1;	/* use kernel default */
510 	inp->ip_inp.inp.in6p_cksum = -1;	/* just to be sure */
511 #ifdef INET
512 	/*
513 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
514 	 * socket as well, because the socket may be bound to an IPv6
515 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
516 	 */
517 	inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
518 #endif
519 	SCTP_INP_WUNLOCK(inp);
520 	return (0);
521 }
522 
523 static int
524 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
525 {
526 	struct sctp_inpcb *inp;
527 	int error;
528 	u_char vflagsav;
529 
530 	inp = (struct sctp_inpcb *)so->so_pcb;
531 	if (inp == NULL) {
532 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
533 		return (EINVAL);
534 	}
535 
536 	if (addr) {
537 		switch (addr->sa_family) {
538 #ifdef INET
539 		case AF_INET:
540 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
541 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
542 				return (EINVAL);
543 			}
544 			break;
545 #endif
546 #ifdef INET6
547 		case AF_INET6:
548 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
549 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
550 				return (EINVAL);
551 			}
552 			break;
553 #endif
554 		default:
555 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
556 			return (EINVAL);
557 		}
558 	}
559 	vflagsav = inp->ip_inp.inp.inp_vflag;
560 	inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
561 	inp->ip_inp.inp.inp_vflag |= INP_IPV6;
562 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
563 		switch (addr->sa_family) {
564 #ifdef INET
565 		case AF_INET:
566 			/* binding v4 addr to v6 socket, so reset flags */
567 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
568 			inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
569 			break;
570 #endif
571 #ifdef INET6
572 		case AF_INET6:
573 			{
574 				struct sockaddr_in6 *sin6_p;
575 
576 				sin6_p = (struct sockaddr_in6 *)addr;
577 
578 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
579 					inp->ip_inp.inp.inp_vflag |= INP_IPV4;
580 				}
581 #ifdef INET
582 				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
583 					struct sockaddr_in sin;
584 
585 					in6_sin6_2_sin(&sin, sin6_p);
586 					inp->ip_inp.inp.inp_vflag |= INP_IPV4;
587 					inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
588 					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
589 					goto out;
590 				}
591 #endif
592 				break;
593 			}
594 #endif
595 		default:
596 			break;
597 		}
598 	} else if (addr != NULL) {
599 		struct sockaddr_in6 *sin6_p;
600 
601 		/* IPV6_V6ONLY socket */
602 #ifdef INET
603 		if (addr->sa_family == AF_INET) {
604 			/* can't bind v4 addr to v6 only socket! */
605 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
606 			error = EINVAL;
607 			goto out;
608 		}
609 #endif
610 		sin6_p = (struct sockaddr_in6 *)addr;
611 
612 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
613 			/* can't bind v4-mapped addrs either! */
614 			/* NOTE: we don't support SIIT */
615 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
616 			error = EINVAL;
617 			goto out;
618 		}
619 	}
620 	error = sctp_inpcb_bind(so, addr, NULL, p);
621 out:
622 	if (error != 0)
623 		inp->ip_inp.inp.inp_vflag = vflagsav;
624 	return (error);
625 }
626 
627 static void
628 sctp6_close(struct socket *so)
629 {
630 	sctp_close(so);
631 }
632 
633 /* This could be made common with sctp_detach() since they are identical */
634 
635 static
636 int
637 sctp6_disconnect(struct socket *so)
638 {
639 	return (sctp_disconnect(so));
640 }
641 
642 int
643 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
644     struct mbuf *control, struct thread *p);
645 
646 static int
647 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
648     struct mbuf *control, struct thread *p)
649 {
650 	struct sctp_inpcb *inp;
651 
652 #ifdef INET
653 	struct sockaddr_in6 *sin6;
654 #endif				/* INET */
655 	/* No SPL needed since sctp_output does this */
656 
657 	inp = (struct sctp_inpcb *)so->so_pcb;
658 	if (inp == NULL) {
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 	/*
668 	 * For the TCP model we may get a NULL addr, if we are a connected
669 	 * socket thats ok.
670 	 */
671 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
672 	    (addr == NULL)) {
673 		goto connected_type;
674 	}
675 	if (addr == NULL) {
676 		SCTP_RELEASE_PKT(m);
677 		if (control) {
678 			SCTP_RELEASE_PKT(control);
679 			control = NULL;
680 		}
681 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
682 		return (EDESTADDRREQ);
683 	}
684 	switch (addr->sa_family) {
685 #ifdef INET
686 	case AF_INET:
687 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
688 			if (control) {
689 				SCTP_RELEASE_PKT(control);
690 				control = NULL;
691 			}
692 			SCTP_RELEASE_PKT(m);
693 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
694 			return (EINVAL);
695 		}
696 		break;
697 #endif
698 #ifdef INET6
699 	case AF_INET6:
700 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
701 			if (control) {
702 				SCTP_RELEASE_PKT(control);
703 				control = NULL;
704 			}
705 			SCTP_RELEASE_PKT(m);
706 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
707 			return (EINVAL);
708 		}
709 		break;
710 #endif
711 	default:
712 		if (control) {
713 			SCTP_RELEASE_PKT(control);
714 			control = NULL;
715 		}
716 		SCTP_RELEASE_PKT(m);
717 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
718 		return (EINVAL);
719 	}
720 #ifdef INET
721 	sin6 = (struct sockaddr_in6 *)addr;
722 	if (SCTP_IPV6_V6ONLY(inp)) {
723 		/*
724 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
725 		 * v4 addr or v4-mapped addr
726 		 */
727 		if (addr->sa_family == AF_INET) {
728 			if (control) {
729 				SCTP_RELEASE_PKT(control);
730 				control = NULL;
731 			}
732 			SCTP_RELEASE_PKT(m);
733 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
734 			return (EINVAL);
735 		}
736 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
737 			if (control) {
738 				SCTP_RELEASE_PKT(control);
739 				control = NULL;
740 			}
741 			SCTP_RELEASE_PKT(m);
742 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
743 			return (EINVAL);
744 		}
745 	}
746 	if ((addr->sa_family == AF_INET6) &&
747 	    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
748 		struct sockaddr_in sin;
749 
750 		/* convert v4-mapped into v4 addr and send */
751 		in6_sin6_2_sin(&sin, sin6);
752 		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
753 	}
754 #endif				/* INET */
755 connected_type:
756 	/* now what about control */
757 	if (control) {
758 		if (inp->control) {
759 			SCTP_PRINTF("huh? control set?\n");
760 			SCTP_RELEASE_PKT(inp->control);
761 			inp->control = NULL;
762 		}
763 		inp->control = control;
764 	}
765 	/* Place the data */
766 	if (inp->pkt) {
767 		SCTP_BUF_NEXT(inp->pkt_last) = m;
768 		inp->pkt_last = m;
769 	} else {
770 		inp->pkt_last = inp->pkt = m;
771 	}
772 	if (
773 	/* FreeBSD and MacOSX uses a flag passed */
774 	    ((flags & PRUS_MORETOCOME) == 0)
775 	    ) {
776 		/*
777 		 * note with the current version this code will only be used
778 		 * by OpenBSD, NetBSD and FreeBSD have methods for
779 		 * re-defining sosend() to use sctp_sosend().  One can
780 		 * optionaly switch back to this code (by changing back the
781 		 * defininitions but this is not advisable.
782 		 */
783 		struct epoch_tracker et;
784 		int ret;
785 
786 		NET_EPOCH_ENTER(et);
787 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
788 		NET_EPOCH_EXIT(et);
789 		inp->pkt = NULL;
790 		inp->control = NULL;
791 		return (ret);
792 	} else {
793 		return (0);
794 	}
795 }
796 
797 static int
798 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
799 {
800 	struct epoch_tracker et;
801 	uint32_t vrf_id;
802 	int error = 0;
803 	struct sctp_inpcb *inp;
804 	struct sctp_tcb *stcb;
805 #ifdef INET
806 	struct sockaddr_in6 *sin6;
807 	union sctp_sockstore store;
808 #endif
809 
810 	inp = (struct sctp_inpcb *)so->so_pcb;
811 	if (inp == NULL) {
812 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
813 		return (ECONNRESET);	/* I made the same as TCP since we are
814 					 * not setup? */
815 	}
816 	if (addr == NULL) {
817 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
818 		return (EINVAL);
819 	}
820 	switch (addr->sa_family) {
821 #ifdef INET
822 	case AF_INET:
823 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
824 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
825 			return (EINVAL);
826 		}
827 		break;
828 #endif
829 #ifdef INET6
830 	case AF_INET6:
831 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
832 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
833 			return (EINVAL);
834 		}
835 		break;
836 #endif
837 	default:
838 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
839 		return (EINVAL);
840 	}
841 
842 	vrf_id = inp->def_vrf_id;
843 	SCTP_ASOC_CREATE_LOCK(inp);
844 	SCTP_INP_RLOCK(inp);
845 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
846 	    SCTP_PCB_FLAGS_UNBOUND) {
847 		/* Bind a ephemeral port */
848 		SCTP_INP_RUNLOCK(inp);
849 		error = sctp6_bind(so, NULL, p);
850 		if (error) {
851 			SCTP_ASOC_CREATE_UNLOCK(inp);
852 
853 			return (error);
854 		}
855 		SCTP_INP_RLOCK(inp);
856 	}
857 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
858 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
859 		/* We are already connected AND the TCP model */
860 		SCTP_INP_RUNLOCK(inp);
861 		SCTP_ASOC_CREATE_UNLOCK(inp);
862 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
863 		return (EADDRINUSE);
864 	}
865 #ifdef INET
866 	sin6 = (struct sockaddr_in6 *)addr;
867 	if (SCTP_IPV6_V6ONLY(inp)) {
868 		/*
869 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
870 		 * addr or v4-mapped addr
871 		 */
872 		if (addr->sa_family == AF_INET) {
873 			SCTP_INP_RUNLOCK(inp);
874 			SCTP_ASOC_CREATE_UNLOCK(inp);
875 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
876 			return (EINVAL);
877 		}
878 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
879 			SCTP_INP_RUNLOCK(inp);
880 			SCTP_ASOC_CREATE_UNLOCK(inp);
881 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
882 			return (EINVAL);
883 		}
884 	}
885 	if ((addr->sa_family == AF_INET6) &&
886 	    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
887 		/* convert v4-mapped into v4 addr */
888 		in6_sin6_2_sin(&store.sin, sin6);
889 		addr = &store.sa;
890 	}
891 #endif				/* INET */
892 	/* Now do we connect? */
893 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
894 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
895 		if (stcb) {
896 			SCTP_TCB_LOCK(stcb);
897 		}
898 		SCTP_INP_RUNLOCK(inp);
899 	} else {
900 		SCTP_INP_RUNLOCK(inp);
901 		SCTP_INP_WLOCK(inp);
902 		SCTP_INP_INCR_REF(inp);
903 		SCTP_INP_WUNLOCK(inp);
904 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
905 		if (stcb == NULL) {
906 			SCTP_INP_WLOCK(inp);
907 			SCTP_INP_DECR_REF(inp);
908 			SCTP_INP_WUNLOCK(inp);
909 		}
910 	}
911 
912 	if (stcb != NULL) {
913 		/* Already have or am bring up an association */
914 		SCTP_ASOC_CREATE_UNLOCK(inp);
915 		SCTP_TCB_UNLOCK(stcb);
916 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
917 		return (EALREADY);
918 	}
919 	/* We are GOOD to go */
920 	stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
921 	    inp->sctp_ep.pre_open_stream_count,
922 	    inp->sctp_ep.port, p,
923 	    SCTP_INITIALIZE_AUTH_PARAMS);
924 	SCTP_ASOC_CREATE_UNLOCK(inp);
925 	if (stcb == NULL) {
926 		/* Gak! no memory */
927 		return (error);
928 	}
929 	SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
930 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
931 	NET_EPOCH_ENTER(et);
932 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
933 	SCTP_TCB_UNLOCK(stcb);
934 	NET_EPOCH_EXIT(et);
935 	return (error);
936 }
937 
938 static int
939 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
940 {
941 	struct sockaddr_in6 *sin6;
942 	struct sctp_inpcb *inp;
943 	uint32_t vrf_id;
944 	struct sctp_ifa *sctp_ifa;
945 
946 	int error;
947 
948 	/*
949 	 * Do the malloc first in case it blocks.
950 	 */
951 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
952 	if (sin6 == NULL)
953 		return (ENOMEM);
954 	sin6->sin6_family = AF_INET6;
955 	sin6->sin6_len = sizeof(*sin6);
956 
957 	inp = (struct sctp_inpcb *)so->so_pcb;
958 	if (inp == NULL) {
959 		SCTP_FREE_SONAME(sin6);
960 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
961 		return (ECONNRESET);
962 	}
963 	SCTP_INP_RLOCK(inp);
964 	sin6->sin6_port = inp->sctp_lport;
965 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
966 		/* For the bound all case you get back 0 */
967 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
968 			struct sctp_tcb *stcb;
969 			struct sockaddr_in6 *sin_a6;
970 			struct sctp_nets *net;
971 			int fnd;
972 
973 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
974 			if (stcb == NULL) {
975 				SCTP_INP_RUNLOCK(inp);
976 				SCTP_FREE_SONAME(sin6);
977 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
978 				return (ENOENT);
979 			}
980 			fnd = 0;
981 			sin_a6 = NULL;
982 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
983 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
984 				if (sin_a6 == NULL)
985 					/* this will make coverity happy */
986 					continue;
987 
988 				if (sin_a6->sin6_family == AF_INET6) {
989 					fnd = 1;
990 					break;
991 				}
992 			}
993 			if ((!fnd) || (sin_a6 == NULL)) {
994 				/* punt */
995 				SCTP_INP_RUNLOCK(inp);
996 				SCTP_FREE_SONAME(sin6);
997 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
998 				return (ENOENT);
999 			}
1000 			vrf_id = inp->def_vrf_id;
1001 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1002 			if (sctp_ifa) {
1003 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1004 			}
1005 		} else {
1006 			/* For the bound all case you get back 0 */
1007 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1008 		}
1009 	} else {
1010 		/* Take the first IPv6 address in the list */
1011 		struct sctp_laddr *laddr;
1012 		int fnd = 0;
1013 
1014 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1015 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1016 				struct sockaddr_in6 *sin_a;
1017 
1018 				sin_a = &laddr->ifa->address.sin6;
1019 				sin6->sin6_addr = sin_a->sin6_addr;
1020 				fnd = 1;
1021 				break;
1022 			}
1023 		}
1024 		if (!fnd) {
1025 			SCTP_FREE_SONAME(sin6);
1026 			SCTP_INP_RUNLOCK(inp);
1027 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1028 			return (ENOENT);
1029 		}
1030 	}
1031 	SCTP_INP_RUNLOCK(inp);
1032 	/* Scoping things for v6 */
1033 	if ((error = sa6_recoverscope(sin6)) != 0) {
1034 		SCTP_FREE_SONAME(sin6);
1035 		return (error);
1036 	}
1037 	(*addr) = (struct sockaddr *)sin6;
1038 	return (0);
1039 }
1040 
1041 static int
1042 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1043 {
1044 	struct sockaddr_in6 *sin6;
1045 	int fnd;
1046 	struct sockaddr_in6 *sin_a6;
1047 	struct sctp_inpcb *inp;
1048 	struct sctp_tcb *stcb;
1049 	struct sctp_nets *net;
1050 	int error;
1051 
1052 	/* Do the malloc first in case it blocks. */
1053 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1054 	if (sin6 == NULL)
1055 		return (ENOMEM);
1056 	sin6->sin6_family = AF_INET6;
1057 	sin6->sin6_len = sizeof(*sin6);
1058 
1059 	inp = (struct sctp_inpcb *)so->so_pcb;
1060 	if ((inp == NULL) ||
1061 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1062 		/* UDP type and listeners will drop out here */
1063 		SCTP_FREE_SONAME(sin6);
1064 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1065 		return (ENOTCONN);
1066 	}
1067 	SCTP_INP_RLOCK(inp);
1068 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1069 	if (stcb) {
1070 		SCTP_TCB_LOCK(stcb);
1071 	}
1072 	SCTP_INP_RUNLOCK(inp);
1073 	if (stcb == NULL) {
1074 		SCTP_FREE_SONAME(sin6);
1075 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1076 		return (ECONNRESET);
1077 	}
1078 	fnd = 0;
1079 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1080 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1081 		if (sin_a6->sin6_family == AF_INET6) {
1082 			fnd = 1;
1083 			sin6->sin6_port = stcb->rport;
1084 			sin6->sin6_addr = sin_a6->sin6_addr;
1085 			break;
1086 		}
1087 	}
1088 	SCTP_TCB_UNLOCK(stcb);
1089 	if (!fnd) {
1090 		/* No IPv4 address */
1091 		SCTP_FREE_SONAME(sin6);
1092 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1093 		return (ENOENT);
1094 	}
1095 	if ((error = sa6_recoverscope(sin6)) != 0) {
1096 		SCTP_FREE_SONAME(sin6);
1097 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1098 		return (error);
1099 	}
1100 	*addr = (struct sockaddr *)sin6;
1101 	return (0);
1102 }
1103 
1104 static int
1105 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1106 {
1107 	struct inpcb *inp = sotoinpcb(so);
1108 	int error;
1109 
1110 	if (inp == NULL) {
1111 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1112 		return (EINVAL);
1113 	}
1114 
1115 	/* allow v6 addresses precedence */
1116 	error = sctp6_getaddr(so, nam);
1117 #ifdef INET
1118 	if (error) {
1119 		struct sockaddr_in6 *sin6;
1120 
1121 		/* try v4 next if v6 failed */
1122 		error = sctp_ingetaddr(so, nam);
1123 		if (error) {
1124 			return (error);
1125 		}
1126 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1127 		if (sin6 == NULL) {
1128 			SCTP_FREE_SONAME(*nam);
1129 			return (ENOMEM);
1130 		}
1131 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1132 		SCTP_FREE_SONAME(*nam);
1133 		*nam = (struct sockaddr *)sin6;
1134 	}
1135 #endif
1136 	return (error);
1137 }
1138 
1139 static int
1140 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1141 {
1142 	struct inpcb *inp = sotoinpcb(so);
1143 	int error;
1144 
1145 	if (inp == NULL) {
1146 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1147 		return (EINVAL);
1148 	}
1149 
1150 	/* allow v6 addresses precedence */
1151 	error = sctp6_peeraddr(so, nam);
1152 #ifdef INET
1153 	if (error) {
1154 		struct sockaddr_in6 *sin6;
1155 
1156 		/* try v4 next if v6 failed */
1157 		error = sctp_peeraddr(so, nam);
1158 		if (error) {
1159 			return (error);
1160 		}
1161 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1162 		if (sin6 == NULL) {
1163 			SCTP_FREE_SONAME(*nam);
1164 			return (ENOMEM);
1165 		}
1166 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1167 		SCTP_FREE_SONAME(*nam);
1168 		*nam = (struct sockaddr *)sin6;
1169 	}
1170 #endif
1171 	return (error);
1172 }
1173 
1174 #define	SCTP6_PROTOSW							\
1175 	.pr_protocol =	IPPROTO_SCTP,					\
1176 	.pr_ctloutput =	sctp_ctloutput,					\
1177 	.pr_abort =	sctp6_abort,					\
1178 	.pr_accept =	sctp_accept,					\
1179 	.pr_attach =	sctp6_attach,					\
1180 	.pr_bind =	sctp6_bind,					\
1181 	.pr_connect =	sctp6_connect,					\
1182 	.pr_control =	in6_control,					\
1183 	.pr_close =	sctp6_close,					\
1184 	.pr_detach =	sctp6_close,					\
1185 	.pr_sopoll =	sopoll_generic,					\
1186 	.pr_flush =	sctp_flush,					\
1187 	.pr_disconnect = sctp6_disconnect,				\
1188 	.pr_listen =	sctp_listen,					\
1189 	.pr_peeraddr =	sctp6_getpeeraddr,				\
1190 	.pr_send =	sctp6_send,					\
1191 	.pr_shutdown =	sctp_shutdown,					\
1192 	.pr_sockaddr =	sctp6_in6getaddr,				\
1193 	.pr_sosend =	sctp_sosend,					\
1194 	.pr_soreceive =	sctp_soreceive
1195 
1196 struct protosw sctp6_seqpacket_protosw = {
1197 	.pr_type = SOCK_SEQPACKET,
1198 	.pr_flags = PR_WANTRCVD,
1199 	SCTP6_PROTOSW
1200 };
1201 
1202 struct protosw sctp6_stream_protosw = {
1203 	.pr_type = SOCK_STREAM,
1204 	.pr_flags = PR_CONNREQUIRED | PR_WANTRCVD,
1205 	SCTP6_PROTOSW
1206 };
1207 #endif
1208