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