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