xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #ifdef INET6
38 #include <sys/proc.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_var.h>
42 #ifdef INET6
43 #include <netinet6/sctp6_var.h>
44 #endif
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/udp.h>
58 
59 #ifdef IPSEC
60 #include <netipsec/ipsec.h>
61 #ifdef INET6
62 #include <netipsec/ipsec6.h>
63 #endif				/* INET6 */
64 #endif				/* IPSEC */
65 
66 extern struct protosw inetsw[];
67 
68 int
69 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
70 {
71 	struct mbuf *m;
72 	int iphlen;
73 	uint32_t vrf_id;
74 	uint8_t ecn_bits;
75 	struct sockaddr_in6 src, dst;
76 	struct ip6_hdr *ip6;
77 	struct sctphdr *sh;
78 	struct sctp_chunkhdr *ch;
79 	int length, offset;
80 
81 #if !defined(SCTP_WITH_NO_CSUM)
82 	uint8_t compute_crc;
83 
84 #endif
85 	uint32_t mflowid;
86 	uint8_t use_mflowid;
87 
88 	iphlen = *offp;
89 	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
90 		SCTP_RELEASE_PKT(*i_pak);
91 		return (IPPROTO_DONE);
92 	}
93 	m = SCTP_HEADER_TO_CHAIN(*i_pak);
94 #ifdef SCTP_MBUF_LOGGING
95 	/* Log in any input mbufs */
96 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
97 		struct mbuf *mat;
98 
99 		for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
100 			if (SCTP_BUF_IS_EXTENDED(mat)) {
101 				sctp_log_mb(mat, SCTP_MBUF_INPUT);
102 			}
103 		}
104 	}
105 #endif
106 #ifdef SCTP_PACKET_LOGGING
107 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
108 		sctp_packet_log(m);
109 	}
110 #endif
111 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
112 	    "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
113 	    m->m_pkthdr.len,
114 	    if_name(m->m_pkthdr.rcvif),
115 	    m->m_pkthdr.csum_flags);
116 	if (m->m_flags & M_FLOWID) {
117 		mflowid = m->m_pkthdr.flowid;
118 		use_mflowid = 1;
119 	} else {
120 		mflowid = 0;
121 		use_mflowid = 0;
122 	}
123 	SCTP_STAT_INCR(sctps_recvpackets);
124 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
125 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
126 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
127 	ip6 = mtod(m, struct ip6_hdr *);
128 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
129 	    (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
130 	if (sh == NULL) {
131 		SCTP_STAT_INCR(sctps_hdrops);
132 		return (IPPROTO_DONE);
133 	}
134 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
135 	offset -= sizeof(struct sctp_chunkhdr);
136 	memset(&src, 0, sizeof(struct sockaddr_in6));
137 	src.sin6_family = AF_INET6;
138 	src.sin6_len = sizeof(struct sockaddr_in6);
139 	src.sin6_port = sh->src_port;
140 	src.sin6_addr = ip6->ip6_src;
141 	if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
142 		goto out;
143 	}
144 	memset(&dst, 0, sizeof(struct sockaddr_in6));
145 	dst.sin6_family = AF_INET6;
146 	dst.sin6_len = sizeof(struct sockaddr_in6);
147 	dst.sin6_port = sh->dest_port;
148 	dst.sin6_addr = ip6->ip6_dst;
149 	if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
150 		goto out;
151 	}
152 	if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
153 		/* XXX send icmp6 host/port unreach? */
154 		goto out;
155 	}
156 	length = ntohs(ip6->ip6_plen) + iphlen;
157 	/* Validate mbuf chain length with IP payload length. */
158 	if (SCTP_HEADER_LEN(m) != length) {
159 		SCTPDBG(SCTP_DEBUG_INPUT1,
160 		    "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
161 		SCTP_STAT_INCR(sctps_hdrops);
162 		goto out;
163 	}
164 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
165 		goto out;
166 	}
167 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
168 #if defined(SCTP_WITH_NO_CSUM)
169 	SCTP_STAT_INCR(sctps_recvnocrc);
170 #else
171 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
172 		SCTP_STAT_INCR(sctps_recvhwcrc);
173 		compute_crc = 0;
174 	} else {
175 		SCTP_STAT_INCR(sctps_recvswcrc);
176 		compute_crc = 1;
177 	}
178 #endif
179 	sctp_common_input_processing(&m, iphlen, offset, length,
180 	    (struct sockaddr *)&src,
181 	    (struct sockaddr *)&dst,
182 	    sh, ch,
183 #if !defined(SCTP_WITH_NO_CSUM)
184 	    compute_crc,
185 #endif
186 	    ecn_bits,
187 	    use_mflowid, mflowid,
188 	    vrf_id, port);
189 out:
190 	if (m) {
191 		sctp_m_freem(m);
192 	}
193 	return (IPPROTO_DONE);
194 }
195 
196 
197 int
198 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
199 {
200 	return (sctp6_input_with_port(i_pak, offp, 0));
201 }
202 
203 static void
204 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
205     struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
206 {
207 	uint32_t nxtsz;
208 
209 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
210 	    (icmp6 == NULL) || (sh == NULL)) {
211 		goto out;
212 	}
213 	/* First do we even look at it? */
214 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
215 		goto out;
216 
217 	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
218 		/* not PACKET TO BIG */
219 		goto out;
220 	}
221 	/*
222 	 * ok we need to look closely. We could even get smarter and look at
223 	 * anyone that we sent to in case we get a different ICMP that tells
224 	 * us there is no way to reach a host, but for this impl, all we
225 	 * care about is MTU discovery.
226 	 */
227 	nxtsz = ntohl(icmp6->icmp6_mtu);
228 	/* Stop any PMTU timer */
229 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
230 
231 	/* Adjust destination size limit */
232 	if (net->mtu > nxtsz) {
233 		net->mtu = nxtsz;
234 		if (net->port) {
235 			net->mtu -= sizeof(struct udphdr);
236 		}
237 	}
238 	/* now what about the ep? */
239 	if (stcb->asoc.smallest_mtu > nxtsz) {
240 		struct sctp_tmit_chunk *chk;
241 
242 		/* Adjust that too */
243 		stcb->asoc.smallest_mtu = nxtsz;
244 		/* now off to subtract IP_DF flag if needed */
245 
246 		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
247 			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
248 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
249 			}
250 		}
251 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
252 			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
253 				/*
254 				 * For this guy we also mark for immediate
255 				 * resend since we sent to big of chunk
256 				 */
257 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
258 				if (chk->sent != SCTP_DATAGRAM_RESEND)
259 					stcb->asoc.sent_queue_retran_cnt++;
260 				chk->sent = SCTP_DATAGRAM_RESEND;
261 				chk->rec.data.doing_fast_retransmit = 0;
262 
263 				chk->sent = SCTP_DATAGRAM_RESEND;
264 				/* Clear any time so NO RTT is being done */
265 				chk->sent_rcv_time.tv_sec = 0;
266 				chk->sent_rcv_time.tv_usec = 0;
267 				stcb->asoc.total_flight -= chk->send_size;
268 				net->flight_size -= chk->send_size;
269 			}
270 		}
271 	}
272 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
273 out:
274 	if (stcb) {
275 		SCTP_TCB_UNLOCK(stcb);
276 	}
277 }
278 
279 
280 void
281 sctp6_notify(struct sctp_inpcb *inp,
282     struct icmp6_hdr *icmph,
283     struct sctphdr *sh,
284     struct sockaddr *to,
285     struct sctp_tcb *stcb,
286     struct sctp_nets *net)
287 {
288 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
289 	struct socket *so;
290 
291 #endif
292 
293 	/* protection */
294 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
295 	    (sh == NULL) || (to == NULL)) {
296 		if (stcb)
297 			SCTP_TCB_UNLOCK(stcb);
298 		return;
299 	}
300 	/* First job is to verify the vtag matches what I would send */
301 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
302 		SCTP_TCB_UNLOCK(stcb);
303 		return;
304 	}
305 	if (icmph->icmp6_type != ICMP_UNREACH) {
306 		/* We only care about unreachable */
307 		SCTP_TCB_UNLOCK(stcb);
308 		return;
309 	}
310 	if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
311 	    (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
312 	    (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
313 	    (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
314 	    (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
315 	    (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
316 	    (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
317 	    (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
318 
319 		/*
320 		 * Hmm reachablity problems we must examine closely. If its
321 		 * not reachable, we may have lost a network. Or if there is
322 		 * NO protocol at the other end named SCTP. well we consider
323 		 * it a OOTB abort.
324 		 */
325 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
326 			/* Ok that destination is NOT reachable */
327 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
328 			net->dest_state &= ~SCTP_ADDR_PF;
329 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
330 			    stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
331 		}
332 		SCTP_TCB_UNLOCK(stcb);
333 	} else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
334 	    (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
335 		/*
336 		 * Here the peer is either playing tricks on us, including
337 		 * an address that belongs to someone who does not support
338 		 * SCTP OR was a userland implementation that shutdown and
339 		 * now is dead. In either case treat it like a OOTB abort
340 		 * with no TCB
341 		 */
342 		sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
343 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
344 		so = SCTP_INP_SO(inp);
345 		atomic_add_int(&stcb->asoc.refcnt, 1);
346 		SCTP_TCB_UNLOCK(stcb);
347 		SCTP_SOCKET_LOCK(so, 1);
348 		SCTP_TCB_LOCK(stcb);
349 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
350 #endif
351 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
352 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
353 		SCTP_SOCKET_UNLOCK(so, 1);
354 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
355 #endif
356 		/* no need to unlock here, since the TCB is gone */
357 	} else {
358 		SCTP_TCB_UNLOCK(stcb);
359 	}
360 }
361 
362 
363 
364 void
365 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
366 {
367 	struct sctphdr sh;
368 	struct ip6ctlparam *ip6cp = NULL;
369 	uint32_t vrf_id;
370 
371 	vrf_id = SCTP_DEFAULT_VRFID;
372 
373 	if (pktdst->sa_family != AF_INET6 ||
374 	    pktdst->sa_len != sizeof(struct sockaddr_in6))
375 		return;
376 
377 	if ((unsigned)cmd >= PRC_NCMDS)
378 		return;
379 	if (PRC_IS_REDIRECT(cmd)) {
380 		d = NULL;
381 	} else if (inet6ctlerrmap[cmd] == 0) {
382 		return;
383 	}
384 	/* if the parameter is from icmp6, decode it. */
385 	if (d != NULL) {
386 		ip6cp = (struct ip6ctlparam *)d;
387 	} else {
388 		ip6cp = (struct ip6ctlparam *)NULL;
389 	}
390 
391 	if (ip6cp) {
392 		/*
393 		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
394 		 * valid.
395 		 */
396 		/* check if we can safely examine src and dst ports */
397 		struct sctp_inpcb *inp = NULL;
398 		struct sctp_tcb *stcb = NULL;
399 		struct sctp_nets *net = NULL;
400 		struct sockaddr_in6 final;
401 
402 		if (ip6cp->ip6c_m == NULL)
403 			return;
404 
405 		bzero(&sh, sizeof(sh));
406 		bzero(&final, sizeof(final));
407 		inp = NULL;
408 		net = NULL;
409 		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
410 		    (caddr_t)&sh);
411 		ip6cp->ip6c_src->sin6_port = sh.src_port;
412 		final.sin6_len = sizeof(final);
413 		final.sin6_family = AF_INET6;
414 		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
415 		final.sin6_port = sh.dest_port;
416 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
417 		    (struct sockaddr *)ip6cp->ip6c_src,
418 		    &inp, &net, 1, vrf_id);
419 		/* inp's ref-count increased && stcb locked */
420 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
421 			if (cmd == PRC_MSGSIZE) {
422 				sctp6_notify_mbuf(inp,
423 				    ip6cp->ip6c_icmp6,
424 				    &sh,
425 				    stcb,
426 				    net);
427 				/* inp's ref-count reduced && stcb unlocked */
428 			} else {
429 				sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
430 				    (struct sockaddr *)&final,
431 				    stcb, net);
432 				/* inp's ref-count reduced && stcb unlocked */
433 			}
434 		} else {
435 			if (PRC_IS_REDIRECT(cmd) && inp) {
436 				in6_rtchange((struct in6pcb *)inp,
437 				    inet6ctlerrmap[cmd]);
438 			}
439 			if (inp) {
440 				/* reduce inp's ref-count */
441 				SCTP_INP_WLOCK(inp);
442 				SCTP_INP_DECR_REF(inp);
443 				SCTP_INP_WUNLOCK(inp);
444 			}
445 			if (stcb)
446 				SCTP_TCB_UNLOCK(stcb);
447 		}
448 	}
449 }
450 
451 /*
452  * this routine can probably be collasped into the one in sctp_userreq.c
453  * since they do the same thing and now we lookup with a sockaddr
454  */
455 static int
456 sctp6_getcred(SYSCTL_HANDLER_ARGS)
457 {
458 	struct xucred xuc;
459 	struct sockaddr_in6 addrs[2];
460 	struct sctp_inpcb *inp;
461 	struct sctp_nets *net;
462 	struct sctp_tcb *stcb;
463 	int error;
464 	uint32_t vrf_id;
465 
466 	vrf_id = SCTP_DEFAULT_VRFID;
467 
468 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
469 	if (error)
470 		return (error);
471 
472 	if (req->newlen != sizeof(addrs)) {
473 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
474 		return (EINVAL);
475 	}
476 	if (req->oldlen != sizeof(struct ucred)) {
477 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
478 		return (EINVAL);
479 	}
480 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
481 	if (error)
482 		return (error);
483 
484 	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
485 	    sin6tosa(&addrs[0]),
486 	    &inp, &net, 1, vrf_id);
487 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
488 		if ((inp != NULL) && (stcb == NULL)) {
489 			/* reduce ref-count */
490 			SCTP_INP_WLOCK(inp);
491 			SCTP_INP_DECR_REF(inp);
492 			goto cred_can_cont;
493 		}
494 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
495 		error = ENOENT;
496 		goto out;
497 	}
498 	SCTP_TCB_UNLOCK(stcb);
499 	/*
500 	 * We use the write lock here, only since in the error leg we need
501 	 * it. If we used RLOCK, then we would have to
502 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
503 	 * Better to use higher wlock.
504 	 */
505 	SCTP_INP_WLOCK(inp);
506 cred_can_cont:
507 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
508 	if (error) {
509 		SCTP_INP_WUNLOCK(inp);
510 		goto out;
511 	}
512 	cru2x(inp->sctp_socket->so_cred, &xuc);
513 	SCTP_INP_WUNLOCK(inp);
514 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
515 out:
516 	return (error);
517 }
518 
519 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
520     0, 0,
521     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
522 
523 
524 /* This is the same as the sctp_abort() could be made common */
525 static void
526 sctp6_abort(struct socket *so)
527 {
528 	struct sctp_inpcb *inp;
529 	uint32_t flags;
530 
531 	inp = (struct sctp_inpcb *)so->so_pcb;
532 	if (inp == NULL) {
533 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
534 		return;
535 	}
536 sctp_must_try_again:
537 	flags = inp->sctp_flags;
538 #ifdef SCTP_LOG_CLOSING
539 	sctp_log_closing(inp, NULL, 17);
540 #endif
541 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
542 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
543 #ifdef SCTP_LOG_CLOSING
544 		sctp_log_closing(inp, NULL, 16);
545 #endif
546 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
547 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
548 		SOCK_LOCK(so);
549 		SCTP_SB_CLEAR(so->so_snd);
550 		/*
551 		 * same for the rcv ones, they are only here for the
552 		 * accounting/select.
553 		 */
554 		SCTP_SB_CLEAR(so->so_rcv);
555 		/* Now null out the reference, we are completely detached. */
556 		so->so_pcb = NULL;
557 		SOCK_UNLOCK(so);
558 	} else {
559 		flags = inp->sctp_flags;
560 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
561 			goto sctp_must_try_again;
562 		}
563 	}
564 	return;
565 }
566 
567 static int
568 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
569 {
570 	struct in6pcb *inp6;
571 	int error;
572 	struct sctp_inpcb *inp;
573 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
574 
575 	inp = (struct sctp_inpcb *)so->so_pcb;
576 	if (inp != NULL) {
577 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
578 		return (EINVAL);
579 	}
580 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
581 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
582 		if (error)
583 			return (error);
584 	}
585 	error = sctp_inpcb_alloc(so, vrf_id);
586 	if (error)
587 		return (error);
588 	inp = (struct sctp_inpcb *)so->so_pcb;
589 	SCTP_INP_WLOCK(inp);
590 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
591 	inp6 = (struct in6pcb *)inp;
592 
593 	inp6->inp_vflag |= INP_IPV6;
594 	inp6->in6p_hops = -1;	/* use kernel default */
595 	inp6->in6p_cksum = -1;	/* just to be sure */
596 #ifdef INET
597 	/*
598 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
599 	 * socket as well, because the socket may be bound to an IPv6
600 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
601 	 */
602 	inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
603 #endif
604 	/*
605 	 * Hmm what about the IPSEC stuff that is missing here but in
606 	 * sctp_attach()?
607 	 */
608 	SCTP_INP_WUNLOCK(inp);
609 	return (0);
610 }
611 
612 static int
613 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
614 {
615 	struct sctp_inpcb *inp;
616 	struct in6pcb *inp6;
617 	int error;
618 
619 	inp = (struct sctp_inpcb *)so->so_pcb;
620 	if (inp == NULL) {
621 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
622 		return (EINVAL);
623 	}
624 	if (addr) {
625 		switch (addr->sa_family) {
626 #ifdef INET
627 		case AF_INET:
628 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
629 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
630 				return (EINVAL);
631 			}
632 			break;
633 #endif
634 #ifdef INET6
635 		case AF_INET6:
636 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
637 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
638 				return (EINVAL);
639 			}
640 			break;
641 #endif
642 		default:
643 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
644 			return (EINVAL);
645 		}
646 	}
647 	inp6 = (struct in6pcb *)inp;
648 	inp6->inp_vflag &= ~INP_IPV4;
649 	inp6->inp_vflag |= INP_IPV6;
650 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
651 		switch (addr->sa_family) {
652 #ifdef INET
653 		case AF_INET:
654 			/* binding v4 addr to v6 socket, so reset flags */
655 			inp6->inp_vflag |= INP_IPV4;
656 			inp6->inp_vflag &= ~INP_IPV6;
657 			break;
658 #endif
659 #ifdef INET6
660 		case AF_INET6:
661 			{
662 				struct sockaddr_in6 *sin6_p;
663 
664 				sin6_p = (struct sockaddr_in6 *)addr;
665 
666 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
667 					inp6->inp_vflag |= INP_IPV4;
668 				}
669 #ifdef INET
670 				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
671 					struct sockaddr_in sin;
672 
673 					in6_sin6_2_sin(&sin, sin6_p);
674 					inp6->inp_vflag |= INP_IPV4;
675 					inp6->inp_vflag &= ~INP_IPV6;
676 					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
677 					return (error);
678 				}
679 #endif
680 				break;
681 			}
682 #endif
683 		default:
684 			break;
685 		}
686 	} else if (addr != NULL) {
687 		struct sockaddr_in6 *sin6_p;
688 
689 		/* IPV6_V6ONLY socket */
690 #ifdef INET
691 		if (addr->sa_family == AF_INET) {
692 			/* can't bind v4 addr to v6 only socket! */
693 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
694 			return (EINVAL);
695 		}
696 #endif
697 		sin6_p = (struct sockaddr_in6 *)addr;
698 
699 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
700 			/* can't bind v4-mapped addrs either! */
701 			/* NOTE: we don't support SIIT */
702 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
703 			return (EINVAL);
704 		}
705 	}
706 	error = sctp_inpcb_bind(so, addr, NULL, p);
707 	return (error);
708 }
709 
710 
711 static void
712 sctp6_close(struct socket *so)
713 {
714 	sctp_close(so);
715 }
716 
717 /* This could be made common with sctp_detach() since they are identical */
718 
719 static
720 int
721 sctp6_disconnect(struct socket *so)
722 {
723 	return (sctp_disconnect(so));
724 }
725 
726 
727 int
728 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
729     struct mbuf *control, struct thread *p);
730 
731 
732 static int
733 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
734     struct mbuf *control, struct thread *p)
735 {
736 	struct sctp_inpcb *inp;
737 	struct in6pcb *inp6;
738 
739 #ifdef INET
740 	struct sockaddr_in6 *sin6;
741 
742 #endif				/* INET */
743 	/* No SPL needed since sctp_output does this */
744 
745 	inp = (struct sctp_inpcb *)so->so_pcb;
746 	if (inp == NULL) {
747 		if (control) {
748 			SCTP_RELEASE_PKT(control);
749 			control = NULL;
750 		}
751 		SCTP_RELEASE_PKT(m);
752 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
753 		return (EINVAL);
754 	}
755 	inp6 = (struct in6pcb *)inp;
756 	/*
757 	 * For the TCP model we may get a NULL addr, if we are a connected
758 	 * socket thats ok.
759 	 */
760 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
761 	    (addr == NULL)) {
762 		goto connected_type;
763 	}
764 	if (addr == NULL) {
765 		SCTP_RELEASE_PKT(m);
766 		if (control) {
767 			SCTP_RELEASE_PKT(control);
768 			control = NULL;
769 		}
770 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
771 		return (EDESTADDRREQ);
772 	}
773 #ifdef INET
774 	sin6 = (struct sockaddr_in6 *)addr;
775 	if (SCTP_IPV6_V6ONLY(inp6)) {
776 		/*
777 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
778 		 * v4 addr or v4-mapped addr
779 		 */
780 		if (addr->sa_family == AF_INET) {
781 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
782 			return (EINVAL);
783 		}
784 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
785 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
786 			return (EINVAL);
787 		}
788 	}
789 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
790 		if (!MODULE_GLOBAL(ip6_v6only)) {
791 			struct sockaddr_in sin;
792 
793 			/* convert v4-mapped into v4 addr and send */
794 			in6_sin6_2_sin(&sin, sin6);
795 			return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
796 			    control, p));
797 		} else {
798 			/* mapped addresses aren't enabled */
799 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
800 			return (EINVAL);
801 		}
802 	}
803 #endif				/* INET */
804 connected_type:
805 	/* now what about control */
806 	if (control) {
807 		if (inp->control) {
808 			SCTP_PRINTF("huh? control set?\n");
809 			SCTP_RELEASE_PKT(inp->control);
810 			inp->control = NULL;
811 		}
812 		inp->control = control;
813 	}
814 	/* Place the data */
815 	if (inp->pkt) {
816 		SCTP_BUF_NEXT(inp->pkt_last) = m;
817 		inp->pkt_last = m;
818 	} else {
819 		inp->pkt_last = inp->pkt = m;
820 	}
821 	if (
822 	/* FreeBSD and MacOSX uses a flag passed */
823 	    ((flags & PRUS_MORETOCOME) == 0)
824 	    ) {
825 		/*
826 		 * note with the current version this code will only be used
827 		 * by OpenBSD, NetBSD and FreeBSD have methods for
828 		 * re-defining sosend() to use sctp_sosend().  One can
829 		 * optionaly switch back to this code (by changing back the
830 		 * defininitions but this is not advisable.
831 		 */
832 		int ret;
833 
834 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
835 		inp->pkt = NULL;
836 		inp->control = NULL;
837 		return (ret);
838 	} else {
839 		return (0);
840 	}
841 }
842 
843 static int
844 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
845 {
846 	uint32_t vrf_id;
847 	int error = 0;
848 	struct sctp_inpcb *inp;
849 	struct in6pcb *inp6;
850 	struct sctp_tcb *stcb;
851 
852 #ifdef INET
853 	struct sockaddr_in6 *sin6;
854 	struct sockaddr_storage ss;
855 
856 #endif
857 
858 	inp6 = (struct in6pcb *)so->so_pcb;
859 	inp = (struct sctp_inpcb *)so->so_pcb;
860 	if (inp == NULL) {
861 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
862 		return (ECONNRESET);	/* I made the same as TCP since we are
863 					 * not setup? */
864 	}
865 	if (addr == NULL) {
866 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
867 		return (EINVAL);
868 	}
869 	switch (addr->sa_family) {
870 #ifdef INET
871 	case AF_INET:
872 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
873 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
874 			return (EINVAL);
875 		}
876 		break;
877 #endif
878 #ifdef INET6
879 	case AF_INET6:
880 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
881 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
882 			return (EINVAL);
883 		}
884 		break;
885 #endif
886 	default:
887 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
888 		return (EINVAL);
889 	}
890 
891 	vrf_id = inp->def_vrf_id;
892 	SCTP_ASOC_CREATE_LOCK(inp);
893 	SCTP_INP_RLOCK(inp);
894 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
895 	    SCTP_PCB_FLAGS_UNBOUND) {
896 		/* Bind a ephemeral port */
897 		SCTP_INP_RUNLOCK(inp);
898 		error = sctp6_bind(so, NULL, p);
899 		if (error) {
900 			SCTP_ASOC_CREATE_UNLOCK(inp);
901 
902 			return (error);
903 		}
904 		SCTP_INP_RLOCK(inp);
905 	}
906 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
907 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
908 		/* We are already connected AND the TCP model */
909 		SCTP_INP_RUNLOCK(inp);
910 		SCTP_ASOC_CREATE_UNLOCK(inp);
911 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
912 		return (EADDRINUSE);
913 	}
914 #ifdef INET
915 	sin6 = (struct sockaddr_in6 *)addr;
916 	if (SCTP_IPV6_V6ONLY(inp6)) {
917 		/*
918 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
919 		 * addr or v4-mapped addr
920 		 */
921 		if (addr->sa_family == AF_INET) {
922 			SCTP_INP_RUNLOCK(inp);
923 			SCTP_ASOC_CREATE_UNLOCK(inp);
924 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
925 			return (EINVAL);
926 		}
927 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
928 			SCTP_INP_RUNLOCK(inp);
929 			SCTP_ASOC_CREATE_UNLOCK(inp);
930 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
931 			return (EINVAL);
932 		}
933 	}
934 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
935 		if (!MODULE_GLOBAL(ip6_v6only)) {
936 			/* convert v4-mapped into v4 addr */
937 			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
938 			addr = (struct sockaddr *)&ss;
939 		} else {
940 			/* mapped addresses aren't enabled */
941 			SCTP_INP_RUNLOCK(inp);
942 			SCTP_ASOC_CREATE_UNLOCK(inp);
943 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
944 			return (EINVAL);
945 		}
946 	}
947 #endif				/* INET */
948 	/* Now do we connect? */
949 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
950 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
951 		if (stcb) {
952 			SCTP_TCB_UNLOCK(stcb);
953 		}
954 		SCTP_INP_RUNLOCK(inp);
955 	} else {
956 		SCTP_INP_RUNLOCK(inp);
957 		SCTP_INP_WLOCK(inp);
958 		SCTP_INP_INCR_REF(inp);
959 		SCTP_INP_WUNLOCK(inp);
960 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
961 		if (stcb == NULL) {
962 			SCTP_INP_WLOCK(inp);
963 			SCTP_INP_DECR_REF(inp);
964 			SCTP_INP_WUNLOCK(inp);
965 		}
966 	}
967 
968 	if (stcb != NULL) {
969 		/* Already have or am bring up an association */
970 		SCTP_ASOC_CREATE_UNLOCK(inp);
971 		SCTP_TCB_UNLOCK(stcb);
972 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
973 		return (EALREADY);
974 	}
975 	/* We are GOOD to go */
976 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
977 	SCTP_ASOC_CREATE_UNLOCK(inp);
978 	if (stcb == NULL) {
979 		/* Gak! no memory */
980 		return (error);
981 	}
982 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
983 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
984 		/* Set the connected flag so we can queue data */
985 		soisconnecting(so);
986 	}
987 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
988 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
989 
990 	/* initialize authentication parameters for the assoc */
991 	sctp_initialize_auth_params(inp, stcb);
992 
993 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
994 	SCTP_TCB_UNLOCK(stcb);
995 	return (error);
996 }
997 
998 static int
999 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1000 {
1001 	struct sockaddr_in6 *sin6;
1002 	struct sctp_inpcb *inp;
1003 	uint32_t vrf_id;
1004 	struct sctp_ifa *sctp_ifa;
1005 
1006 	int error;
1007 
1008 	/*
1009 	 * Do the malloc first in case it blocks.
1010 	 */
1011 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1012 	if (sin6 == NULL)
1013 		return (ENOMEM);
1014 	sin6->sin6_family = AF_INET6;
1015 	sin6->sin6_len = sizeof(*sin6);
1016 
1017 	inp = (struct sctp_inpcb *)so->so_pcb;
1018 	if (inp == NULL) {
1019 		SCTP_FREE_SONAME(sin6);
1020 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1021 		return (ECONNRESET);
1022 	}
1023 	SCTP_INP_RLOCK(inp);
1024 	sin6->sin6_port = inp->sctp_lport;
1025 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1026 		/* For the bound all case you get back 0 */
1027 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1028 			struct sctp_tcb *stcb;
1029 			struct sockaddr_in6 *sin_a6;
1030 			struct sctp_nets *net;
1031 			int fnd;
1032 
1033 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1034 			if (stcb == NULL) {
1035 				goto notConn6;
1036 			}
1037 			fnd = 0;
1038 			sin_a6 = NULL;
1039 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1040 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1041 				if (sin_a6 == NULL)
1042 					/* this will make coverity happy */
1043 					continue;
1044 
1045 				if (sin_a6->sin6_family == AF_INET6) {
1046 					fnd = 1;
1047 					break;
1048 				}
1049 			}
1050 			if ((!fnd) || (sin_a6 == NULL)) {
1051 				/* punt */
1052 				goto notConn6;
1053 			}
1054 			vrf_id = inp->def_vrf_id;
1055 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1056 			if (sctp_ifa) {
1057 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1058 			}
1059 		} else {
1060 			/* For the bound all case you get back 0 */
1061 	notConn6:
1062 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1063 		}
1064 	} else {
1065 		/* Take the first IPv6 address in the list */
1066 		struct sctp_laddr *laddr;
1067 		int fnd = 0;
1068 
1069 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1070 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1071 				struct sockaddr_in6 *sin_a;
1072 
1073 				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1074 				sin6->sin6_addr = sin_a->sin6_addr;
1075 				fnd = 1;
1076 				break;
1077 			}
1078 		}
1079 		if (!fnd) {
1080 			SCTP_FREE_SONAME(sin6);
1081 			SCTP_INP_RUNLOCK(inp);
1082 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1083 			return (ENOENT);
1084 		}
1085 	}
1086 	SCTP_INP_RUNLOCK(inp);
1087 	/* Scoping things for v6 */
1088 	if ((error = sa6_recoverscope(sin6)) != 0) {
1089 		SCTP_FREE_SONAME(sin6);
1090 		return (error);
1091 	}
1092 	(*addr) = (struct sockaddr *)sin6;
1093 	return (0);
1094 }
1095 
1096 static int
1097 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1098 {
1099 	struct sockaddr_in6 *sin6;
1100 	int fnd;
1101 	struct sockaddr_in6 *sin_a6;
1102 	struct sctp_inpcb *inp;
1103 	struct sctp_tcb *stcb;
1104 	struct sctp_nets *net;
1105 	int error;
1106 
1107 	/* Do the malloc first in case it blocks. */
1108 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1109 	if (sin6 == NULL)
1110 		return (ENOMEM);
1111 	sin6->sin6_family = AF_INET6;
1112 	sin6->sin6_len = sizeof(*sin6);
1113 
1114 	inp = (struct sctp_inpcb *)so->so_pcb;
1115 	if ((inp == NULL) ||
1116 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1117 		/* UDP type and listeners will drop out here */
1118 		SCTP_FREE_SONAME(sin6);
1119 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1120 		return (ENOTCONN);
1121 	}
1122 	SCTP_INP_RLOCK(inp);
1123 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1124 	if (stcb) {
1125 		SCTP_TCB_LOCK(stcb);
1126 	}
1127 	SCTP_INP_RUNLOCK(inp);
1128 	if (stcb == NULL) {
1129 		SCTP_FREE_SONAME(sin6);
1130 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1131 		return (ECONNRESET);
1132 	}
1133 	fnd = 0;
1134 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1135 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1136 		if (sin_a6->sin6_family == AF_INET6) {
1137 			fnd = 1;
1138 			sin6->sin6_port = stcb->rport;
1139 			sin6->sin6_addr = sin_a6->sin6_addr;
1140 			break;
1141 		}
1142 	}
1143 	SCTP_TCB_UNLOCK(stcb);
1144 	if (!fnd) {
1145 		/* No IPv4 address */
1146 		SCTP_FREE_SONAME(sin6);
1147 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1148 		return (ENOENT);
1149 	}
1150 	if ((error = sa6_recoverscope(sin6)) != 0)
1151 		return (error);
1152 	*addr = (struct sockaddr *)sin6;
1153 	return (0);
1154 }
1155 
1156 static int
1157 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1158 {
1159 #ifdef INET
1160 	struct sockaddr *addr;
1161 
1162 #endif
1163 	struct in6pcb *inp6 = sotoin6pcb(so);
1164 	int error;
1165 
1166 	if (inp6 == NULL) {
1167 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1168 		return (EINVAL);
1169 	}
1170 	/* allow v6 addresses precedence */
1171 	error = sctp6_getaddr(so, nam);
1172 #ifdef INET
1173 	if (error) {
1174 		/* try v4 next if v6 failed */
1175 		error = sctp_ingetaddr(so, nam);
1176 		if (error) {
1177 			return (error);
1178 		}
1179 		addr = *nam;
1180 		/* if I'm V6ONLY, convert it to v4-mapped */
1181 		if (SCTP_IPV6_V6ONLY(inp6)) {
1182 			struct sockaddr_in6 sin6;
1183 
1184 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1185 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1186 		}
1187 	}
1188 #endif
1189 	return (error);
1190 }
1191 
1192 
1193 static int
1194 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1195 {
1196 #ifdef INET
1197 	struct sockaddr *addr;
1198 
1199 #endif
1200 	struct in6pcb *inp6 = sotoin6pcb(so);
1201 	int error;
1202 
1203 	if (inp6 == NULL) {
1204 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1205 		return (EINVAL);
1206 	}
1207 	/* allow v6 addresses precedence */
1208 	error = sctp6_peeraddr(so, nam);
1209 #ifdef INET
1210 	if (error) {
1211 		/* try v4 next if v6 failed */
1212 		error = sctp_peeraddr(so, nam);
1213 		if (error) {
1214 			return (error);
1215 		}
1216 		addr = *nam;
1217 		/* if I'm V6ONLY, convert it to v4-mapped */
1218 		if (SCTP_IPV6_V6ONLY(inp6)) {
1219 			struct sockaddr_in6 sin6;
1220 
1221 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1222 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1223 		}
1224 	}
1225 #endif
1226 	return (error);
1227 }
1228 
1229 struct pr_usrreqs sctp6_usrreqs = {
1230 	.pru_abort = sctp6_abort,
1231 	.pru_accept = sctp_accept,
1232 	.pru_attach = sctp6_attach,
1233 	.pru_bind = sctp6_bind,
1234 	.pru_connect = sctp6_connect,
1235 	.pru_control = in6_control,
1236 	.pru_close = sctp6_close,
1237 	.pru_detach = sctp6_close,
1238 	.pru_sopoll = sopoll_generic,
1239 	.pru_flush = sctp_flush,
1240 	.pru_disconnect = sctp6_disconnect,
1241 	.pru_listen = sctp_listen,
1242 	.pru_peeraddr = sctp6_getpeeraddr,
1243 	.pru_send = sctp6_send,
1244 	.pru_shutdown = sctp_shutdown,
1245 	.pru_sockaddr = sctp6_in6getaddr,
1246 	.pru_sosend = sctp_sosend,
1247 	.pru_soreceive = sctp_soreceive
1248 };
1249 
1250 #endif
1251