xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision c7587f7a3f8ddcc8dd209c9cff7b9ec3bf353dec)
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 int
636 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
637     struct mbuf *control, struct thread *p);
638 
639 static int
640 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
641     struct mbuf *control, struct thread *p)
642 {
643 	struct sctp_inpcb *inp;
644 
645 #ifdef INET
646 	struct sockaddr_in6 *sin6;
647 #endif				/* INET */
648 	/* No SPL needed since sctp_output does this */
649 
650 	inp = (struct sctp_inpcb *)so->so_pcb;
651 	if (inp == NULL) {
652 		if (control) {
653 			SCTP_RELEASE_PKT(control);
654 			control = NULL;
655 		}
656 		SCTP_RELEASE_PKT(m);
657 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
658 		return (EINVAL);
659 	}
660 	/*
661 	 * For the TCP model we may get a NULL addr, if we are a connected
662 	 * socket thats ok.
663 	 */
664 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
665 	    (addr == NULL)) {
666 		goto connected_type;
667 	}
668 	if (addr == NULL) {
669 		SCTP_RELEASE_PKT(m);
670 		if (control) {
671 			SCTP_RELEASE_PKT(control);
672 			control = NULL;
673 		}
674 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
675 		return (EDESTADDRREQ);
676 	}
677 	switch (addr->sa_family) {
678 #ifdef INET
679 	case AF_INET:
680 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
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 		break;
690 #endif
691 #ifdef INET6
692 	case AF_INET6:
693 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
694 			if (control) {
695 				SCTP_RELEASE_PKT(control);
696 				control = NULL;
697 			}
698 			SCTP_RELEASE_PKT(m);
699 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
700 			return (EINVAL);
701 		}
702 		break;
703 #endif
704 	default:
705 		if (control) {
706 			SCTP_RELEASE_PKT(control);
707 			control = NULL;
708 		}
709 		SCTP_RELEASE_PKT(m);
710 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
711 		return (EINVAL);
712 	}
713 #ifdef INET
714 	sin6 = (struct sockaddr_in6 *)addr;
715 	if (SCTP_IPV6_V6ONLY(inp)) {
716 		/*
717 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
718 		 * v4 addr or v4-mapped addr
719 		 */
720 		if (addr->sa_family == AF_INET) {
721 			if (control) {
722 				SCTP_RELEASE_PKT(control);
723 				control = NULL;
724 			}
725 			SCTP_RELEASE_PKT(m);
726 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
727 			return (EINVAL);
728 		}
729 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
730 			if (control) {
731 				SCTP_RELEASE_PKT(control);
732 				control = NULL;
733 			}
734 			SCTP_RELEASE_PKT(m);
735 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
736 			return (EINVAL);
737 		}
738 	}
739 	if ((addr->sa_family == AF_INET6) &&
740 	    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
741 		struct sockaddr_in sin;
742 
743 		/* convert v4-mapped into v4 addr and send */
744 		in6_sin6_2_sin(&sin, sin6);
745 		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
746 	}
747 #endif				/* INET */
748 connected_type:
749 	/* now what about control */
750 	if (control) {
751 		if (inp->control) {
752 			SCTP_PRINTF("huh? control set?\n");
753 			SCTP_RELEASE_PKT(inp->control);
754 			inp->control = NULL;
755 		}
756 		inp->control = control;
757 	}
758 	/* Place the data */
759 	if (inp->pkt) {
760 		SCTP_BUF_NEXT(inp->pkt_last) = m;
761 		inp->pkt_last = m;
762 	} else {
763 		inp->pkt_last = inp->pkt = m;
764 	}
765 	if (
766 	/* FreeBSD and MacOSX uses a flag passed */
767 	    ((flags & PRUS_MORETOCOME) == 0)
768 	    ) {
769 		/*
770 		 * note with the current version this code will only be used
771 		 * by OpenBSD, NetBSD and FreeBSD have methods for
772 		 * re-defining sosend() to use sctp_sosend().  One can
773 		 * optionaly switch back to this code (by changing back the
774 		 * defininitions but this is not advisable.
775 		 */
776 		struct epoch_tracker et;
777 		int ret;
778 
779 		NET_EPOCH_ENTER(et);
780 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
781 		NET_EPOCH_EXIT(et);
782 		inp->pkt = NULL;
783 		inp->control = NULL;
784 		return (ret);
785 	} else {
786 		return (0);
787 	}
788 }
789 
790 static int
791 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
792 {
793 	struct epoch_tracker et;
794 	uint32_t vrf_id;
795 	int error = 0;
796 	struct sctp_inpcb *inp;
797 	struct sctp_tcb *stcb;
798 #ifdef INET
799 	struct sockaddr_in6 *sin6;
800 	union sctp_sockstore store;
801 #endif
802 
803 	inp = (struct sctp_inpcb *)so->so_pcb;
804 	if (inp == NULL) {
805 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
806 		return (ECONNRESET);	/* I made the same as TCP since we are
807 					 * not setup? */
808 	}
809 	if (addr == NULL) {
810 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
811 		return (EINVAL);
812 	}
813 	switch (addr->sa_family) {
814 #ifdef INET
815 	case AF_INET:
816 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
817 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
818 			return (EINVAL);
819 		}
820 		break;
821 #endif
822 #ifdef INET6
823 	case AF_INET6:
824 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
825 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
826 			return (EINVAL);
827 		}
828 		break;
829 #endif
830 	default:
831 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
832 		return (EINVAL);
833 	}
834 
835 	vrf_id = inp->def_vrf_id;
836 	SCTP_ASOC_CREATE_LOCK(inp);
837 	SCTP_INP_RLOCK(inp);
838 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
839 	    SCTP_PCB_FLAGS_UNBOUND) {
840 		/* Bind a ephemeral port */
841 		SCTP_INP_RUNLOCK(inp);
842 		error = sctp6_bind(so, NULL, p);
843 		if (error) {
844 			SCTP_ASOC_CREATE_UNLOCK(inp);
845 
846 			return (error);
847 		}
848 		SCTP_INP_RLOCK(inp);
849 	}
850 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
851 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
852 		/* We are already connected AND the TCP model */
853 		SCTP_INP_RUNLOCK(inp);
854 		SCTP_ASOC_CREATE_UNLOCK(inp);
855 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
856 		return (EADDRINUSE);
857 	}
858 #ifdef INET
859 	sin6 = (struct sockaddr_in6 *)addr;
860 	if (SCTP_IPV6_V6ONLY(inp)) {
861 		/*
862 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
863 		 * addr or v4-mapped addr
864 		 */
865 		if (addr->sa_family == AF_INET) {
866 			SCTP_INP_RUNLOCK(inp);
867 			SCTP_ASOC_CREATE_UNLOCK(inp);
868 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
869 			return (EINVAL);
870 		}
871 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
872 			SCTP_INP_RUNLOCK(inp);
873 			SCTP_ASOC_CREATE_UNLOCK(inp);
874 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
875 			return (EINVAL);
876 		}
877 	}
878 	if ((addr->sa_family == AF_INET6) &&
879 	    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
880 		/* convert v4-mapped into v4 addr */
881 		in6_sin6_2_sin(&store.sin, sin6);
882 		addr = &store.sa;
883 	}
884 #endif				/* INET */
885 	/* Now do we connect? */
886 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
887 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
888 		if (stcb) {
889 			SCTP_TCB_LOCK(stcb);
890 		}
891 		SCTP_INP_RUNLOCK(inp);
892 	} else {
893 		SCTP_INP_RUNLOCK(inp);
894 		SCTP_INP_WLOCK(inp);
895 		SCTP_INP_INCR_REF(inp);
896 		SCTP_INP_WUNLOCK(inp);
897 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
898 		if (stcb == NULL) {
899 			SCTP_INP_WLOCK(inp);
900 			SCTP_INP_DECR_REF(inp);
901 			SCTP_INP_WUNLOCK(inp);
902 		}
903 	}
904 
905 	if (stcb != NULL) {
906 		/* Already have or am bring up an association */
907 		SCTP_ASOC_CREATE_UNLOCK(inp);
908 		SCTP_TCB_UNLOCK(stcb);
909 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
910 		return (EALREADY);
911 	}
912 	/* We are GOOD to go */
913 	stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
914 	    inp->sctp_ep.pre_open_stream_count,
915 	    inp->sctp_ep.port, p,
916 	    SCTP_INITIALIZE_AUTH_PARAMS);
917 	SCTP_ASOC_CREATE_UNLOCK(inp);
918 	if (stcb == NULL) {
919 		/* Gak! no memory */
920 		return (error);
921 	}
922 	SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
923 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
924 	NET_EPOCH_ENTER(et);
925 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
926 	SCTP_TCB_UNLOCK(stcb);
927 	NET_EPOCH_EXIT(et);
928 	return (error);
929 }
930 
931 static int
932 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
933 {
934 	struct sockaddr_in6 *sin6;
935 	struct sctp_inpcb *inp;
936 	uint32_t vrf_id;
937 	struct sctp_ifa *sctp_ifa;
938 
939 	int error;
940 
941 	/*
942 	 * Do the malloc first in case it blocks.
943 	 */
944 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
945 	if (sin6 == NULL)
946 		return (ENOMEM);
947 	sin6->sin6_family = AF_INET6;
948 	sin6->sin6_len = sizeof(*sin6);
949 
950 	inp = (struct sctp_inpcb *)so->so_pcb;
951 	if (inp == NULL) {
952 		SCTP_FREE_SONAME(sin6);
953 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
954 		return (ECONNRESET);
955 	}
956 	SCTP_INP_RLOCK(inp);
957 	sin6->sin6_port = inp->sctp_lport;
958 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
959 		/* For the bound all case you get back 0 */
960 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
961 			struct sctp_tcb *stcb;
962 			struct sockaddr_in6 *sin_a6;
963 			struct sctp_nets *net;
964 			int fnd;
965 
966 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
967 			if (stcb == NULL) {
968 				SCTP_INP_RUNLOCK(inp);
969 				SCTP_FREE_SONAME(sin6);
970 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
971 				return (ENOENT);
972 			}
973 			fnd = 0;
974 			sin_a6 = NULL;
975 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
976 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
977 				if (sin_a6 == NULL)
978 					/* this will make coverity happy */
979 					continue;
980 
981 				if (sin_a6->sin6_family == AF_INET6) {
982 					fnd = 1;
983 					break;
984 				}
985 			}
986 			if ((!fnd) || (sin_a6 == NULL)) {
987 				/* punt */
988 				SCTP_INP_RUNLOCK(inp);
989 				SCTP_FREE_SONAME(sin6);
990 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
991 				return (ENOENT);
992 			}
993 			vrf_id = inp->def_vrf_id;
994 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
995 			if (sctp_ifa) {
996 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
997 			}
998 		} else {
999 			/* For the bound all case you get back 0 */
1000 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1001 		}
1002 	} else {
1003 		/* Take the first IPv6 address in the list */
1004 		struct sctp_laddr *laddr;
1005 		int fnd = 0;
1006 
1007 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1008 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1009 				struct sockaddr_in6 *sin_a;
1010 
1011 				sin_a = &laddr->ifa->address.sin6;
1012 				sin6->sin6_addr = sin_a->sin6_addr;
1013 				fnd = 1;
1014 				break;
1015 			}
1016 		}
1017 		if (!fnd) {
1018 			SCTP_FREE_SONAME(sin6);
1019 			SCTP_INP_RUNLOCK(inp);
1020 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1021 			return (ENOENT);
1022 		}
1023 	}
1024 	SCTP_INP_RUNLOCK(inp);
1025 	/* Scoping things for v6 */
1026 	if ((error = sa6_recoverscope(sin6)) != 0) {
1027 		SCTP_FREE_SONAME(sin6);
1028 		return (error);
1029 	}
1030 	(*addr) = (struct sockaddr *)sin6;
1031 	return (0);
1032 }
1033 
1034 static int
1035 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1036 {
1037 	struct sockaddr_in6 *sin6;
1038 	int fnd;
1039 	struct sockaddr_in6 *sin_a6;
1040 	struct sctp_inpcb *inp;
1041 	struct sctp_tcb *stcb;
1042 	struct sctp_nets *net;
1043 	int error;
1044 
1045 	/* Do the malloc first in case it blocks. */
1046 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1047 	if (sin6 == NULL)
1048 		return (ENOMEM);
1049 	sin6->sin6_family = AF_INET6;
1050 	sin6->sin6_len = sizeof(*sin6);
1051 
1052 	inp = (struct sctp_inpcb *)so->so_pcb;
1053 	if ((inp == NULL) ||
1054 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1055 		/* UDP type and listeners will drop out here */
1056 		SCTP_FREE_SONAME(sin6);
1057 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1058 		return (ENOTCONN);
1059 	}
1060 	SCTP_INP_RLOCK(inp);
1061 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1062 	if (stcb) {
1063 		SCTP_TCB_LOCK(stcb);
1064 	}
1065 	SCTP_INP_RUNLOCK(inp);
1066 	if (stcb == NULL) {
1067 		SCTP_FREE_SONAME(sin6);
1068 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1069 		return (ECONNRESET);
1070 	}
1071 	fnd = 0;
1072 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1073 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1074 		if (sin_a6->sin6_family == AF_INET6) {
1075 			fnd = 1;
1076 			sin6->sin6_port = stcb->rport;
1077 			sin6->sin6_addr = sin_a6->sin6_addr;
1078 			break;
1079 		}
1080 	}
1081 	SCTP_TCB_UNLOCK(stcb);
1082 	if (!fnd) {
1083 		/* No IPv4 address */
1084 		SCTP_FREE_SONAME(sin6);
1085 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1086 		return (ENOENT);
1087 	}
1088 	if ((error = sa6_recoverscope(sin6)) != 0) {
1089 		SCTP_FREE_SONAME(sin6);
1090 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1091 		return (error);
1092 	}
1093 	*addr = (struct sockaddr *)sin6;
1094 	return (0);
1095 }
1096 
1097 static int
1098 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1099 {
1100 	struct inpcb *inp = sotoinpcb(so);
1101 	int error;
1102 
1103 	if (inp == NULL) {
1104 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1105 		return (EINVAL);
1106 	}
1107 
1108 	/* allow v6 addresses precedence */
1109 	error = sctp6_getaddr(so, nam);
1110 #ifdef INET
1111 	if (error) {
1112 		struct sockaddr_in6 *sin6;
1113 
1114 		/* try v4 next if v6 failed */
1115 		error = sctp_ingetaddr(so, nam);
1116 		if (error) {
1117 			return (error);
1118 		}
1119 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1120 		if (sin6 == NULL) {
1121 			SCTP_FREE_SONAME(*nam);
1122 			return (ENOMEM);
1123 		}
1124 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1125 		SCTP_FREE_SONAME(*nam);
1126 		*nam = (struct sockaddr *)sin6;
1127 	}
1128 #endif
1129 	return (error);
1130 }
1131 
1132 static int
1133 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1134 {
1135 	struct inpcb *inp = sotoinpcb(so);
1136 	int error;
1137 
1138 	if (inp == NULL) {
1139 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1140 		return (EINVAL);
1141 	}
1142 
1143 	/* allow v6 addresses precedence */
1144 	error = sctp6_peeraddr(so, nam);
1145 #ifdef INET
1146 	if (error) {
1147 		struct sockaddr_in6 *sin6;
1148 
1149 		/* try v4 next if v6 failed */
1150 		error = sctp_peeraddr(so, nam);
1151 		if (error) {
1152 			return (error);
1153 		}
1154 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1155 		if (sin6 == NULL) {
1156 			SCTP_FREE_SONAME(*nam);
1157 			return (ENOMEM);
1158 		}
1159 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1160 		SCTP_FREE_SONAME(*nam);
1161 		*nam = (struct sockaddr *)sin6;
1162 	}
1163 #endif
1164 	return (error);
1165 }
1166 
1167 #define	SCTP6_PROTOSW							\
1168 	.pr_protocol =	IPPROTO_SCTP,					\
1169 	.pr_ctloutput =	sctp_ctloutput,					\
1170 	.pr_abort =	sctp6_abort,					\
1171 	.pr_accept =	sctp_accept,					\
1172 	.pr_attach =	sctp6_attach,					\
1173 	.pr_bind =	sctp6_bind,					\
1174 	.pr_connect =	sctp6_connect,					\
1175 	.pr_control =	in6_control,					\
1176 	.pr_close =	sctp6_close,					\
1177 	.pr_detach =	sctp6_close,					\
1178 	.pr_sopoll =	sopoll_generic,					\
1179 	.pr_flush =	sctp_flush,					\
1180 	.pr_disconnect = sctp_disconnect,				\
1181 	.pr_listen =	sctp_listen,					\
1182 	.pr_peeraddr =	sctp6_getpeeraddr,				\
1183 	.pr_send =	sctp6_send,					\
1184 	.pr_shutdown =	sctp_shutdown,					\
1185 	.pr_sockaddr =	sctp6_in6getaddr,				\
1186 	.pr_sosend =	sctp_sosend,					\
1187 	.pr_soreceive =	sctp_soreceive
1188 
1189 struct protosw sctp6_seqpacket_protosw = {
1190 	.pr_type = SOCK_SEQPACKET,
1191 	.pr_flags = PR_WANTRCVD,
1192 	SCTP6_PROTOSW
1193 };
1194 
1195 struct protosw sctp6_stream_protosw = {
1196 	.pr_type = SOCK_STREAM,
1197 	.pr_flags = PR_CONNREQUIRED | PR_WANTRCVD,
1198 	SCTP6_PROTOSW
1199 };
1200 #endif
1201