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