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