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